Skip to content

Commit 192e3c2

Browse files
committed
[cip-50] Optional replay protection
1 parent 6219cae commit 192e3c2

File tree

4 files changed

+48
-36
lines changed

4 files changed

+48
-36
lines changed

core/state_processor.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg
121121
}
122122

123123
func applyTransaction(msg types.Message, config *params.ChainConfig, gp *GasPool, statedb *state.StateDB, blockNumber *big.Int, blockHash common.Hash, tx *types.Transaction, usedGas *uint64, evm *vm.EVM, vmRunner vm.EVMRunner, sysCtx *SysContractCallCtx) (*types.Receipt, error) {
124-
if config.IsDonut(blockNumber) && !tx.Protected() {
124+
if config.IsDonut(blockNumber) && !config.IsEHardfork(blockNumber) && !tx.Protected() {
125125
return nil, ErrUnprotectedTransaction
126126
}
127127

core/tx_pool.go

+1-19
Original file line numberDiff line numberDiff line change
@@ -504,20 +504,6 @@ func (pool *TxPool) setGasLimit(gasLimit uint64) {
504504
}
505505
}
506506

507-
// handleDonutActivation removes from the pool all transactions without EIP-155 replay protection
508-
func (pool *TxPool) handleDonutActivation() {
509-
toRemove := make(map[common.Hash]struct{})
510-
pool.all.Range(func(hash common.Hash, tx *types.Transaction, _ bool) bool {
511-
if !tx.Protected() {
512-
toRemove[hash] = struct{}{}
513-
}
514-
return true
515-
}, true, true)
516-
for hash := range toRemove {
517-
pool.removeTx(hash, true)
518-
}
519-
}
520-
521507
// Nonce returns the next nonce of an account, with all transactions executable
522508
// by the pool already applied on top.
523509
func (pool *TxPool) Nonce(addr common.Address) uint64 {
@@ -649,7 +635,7 @@ func (pool *TxPool) ctx() *txPoolContext {
649635
// validateTx checks whether a transaction is valid according to the consensus
650636
// rules and adheres to some heuristic limits of the local node (price and size).
651637
func (pool *TxPool) validateTx(tx *types.Transaction, local bool) error {
652-
if pool.donut && !tx.Protected() {
638+
if pool.donut && !pool.espresso && !tx.Protected() {
653639
return ErrUnprotectedTransaction
654640
}
655641
if tx.EthCompatible() && !pool.donut {
@@ -1381,12 +1367,8 @@ func (pool *TxPool) reset(oldHead, newHead *types.Header) {
13811367

13821368
// Update all fork indicator by next pending block number.
13831369
next := new(big.Int).Add(newHead.Number, big.NewInt(1))
1384-
wasDonut := pool.donut
13851370
pool.istanbul = pool.chainconfig.IsIstanbul(next)
13861371
pool.donut = pool.chainconfig.IsDonut(next)
1387-
if pool.donut && !wasDonut {
1388-
pool.handleDonutActivation()
1389-
}
13901372
pool.espresso = pool.chainconfig.IsEspresso(next)
13911373
}
13921374

core/tx_pool_test.go

+45-15
Original file line numberDiff line numberDiff line change
@@ -876,42 +876,72 @@ func TestTransactionGapFilling(t *testing.T) {
876876
// (a) to set pool.donut = false at its start (so we can add unprotected transactions)
877877
// (b) different functions to generate protected vs unprotected transactions, since we will
878878
// need to update transaction() and the others to use replay protection
879-
func TestHandleDonutActivation(t *testing.T) {
879+
func TestPoolReAcceptingUnprotectedTxsFromEFork(t *testing.T) {
880880
t.Parallel()
881881

882-
// Create a test account and fund it
883882
pool, key := setupTxPool()
883+
// Create a test account and fund it
884884
defer pool.Stop()
885885

886886
account := crypto.PubkeyToAddress(key.PublicKey)
887887
pool.currentState.AddBalance(account, big.NewInt(1000000))
888888

889+
// flag it as before donut
890+
pool.donut = false
891+
pool.eHardfork = false
892+
889893
pool.AddRemotesSync([]*types.Transaction{
890894
protectedTransaction(0, 100000, key),
891895
transaction(1, 100000, key),
892-
protectedTransaction(2, 100000, key),
893-
transaction(7, 100000, key),
894-
protectedTransaction(8, 100000, key),
895-
transaction(9, 100000, key),
896-
transaction(10, 100000, key),
896+
897+
protectedTransaction(10, 100000, key),
898+
transaction(11, 100000, key),
897899
})
898900

899901
pending, queued := pool.Stats()
902+
if pending != 2 {
903+
t.Fatalf("before donut, pending transactions mismatched: have %d, want %d", pending, 2)
904+
}
905+
if queued != 2 {
906+
t.Fatalf("before donut, queued transactions mismatched: have %d, want %d", queued, 2)
907+
}
908+
909+
// In donut fork
910+
pool.donut = true
911+
912+
pool.AddRemotesSync([]*types.Transaction{
913+
protectedTransaction(2, 100000, key),
914+
transaction(3, 100000, key),
915+
916+
protectedTransaction(12, 100000, key),
917+
transaction(13, 100000, key),
918+
})
919+
920+
pending, queued = pool.Stats()
900921
if pending != 3 {
901-
t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 3)
922+
t.Fatalf("after donut, pending transactions mismatched: have %d, want %d", pending, 3)
902923
}
903-
if queued != 4 {
904-
t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 4)
924+
if queued != 3 {
925+
t.Fatalf("after donut, queued transactions mismatched: have %d, want %d", queued, 3)
905926
}
906927

907-
pool.handleDonutActivation()
928+
// In E fork
929+
// flag it as E hard fork
930+
pool.eHardfork = true
931+
pool.AddRemotesSync([]*types.Transaction{
932+
transaction(3, 100000, key),
933+
protectedTransaction(4, 100000, key),
934+
935+
transaction(13, 100000, key),
936+
protectedTransaction(14, 100000, key),
937+
})
908938

909939
pending, queued = pool.Stats()
910-
if pending != 1 {
911-
t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 1)
940+
if pending != 5 {
941+
t.Fatalf("after expresso, pending transactions mismatched: have %d, want %d", pending, 5)
912942
}
913-
if queued != 2 {
914-
t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 2)
943+
if queued != 5 {
944+
t.Fatalf("after expresso, queued transactions mismatched: have %d, want %d", queued, 5)
915945
}
916946

917947
if err := validateTxPoolInternals(pool); err != nil {

light/txpool.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ func (pool *TxPool) validateTx(ctx context.Context, tx *types.Transaction) error
362362
err error
363363
)
364364

365-
if pool.donut && !tx.Protected() {
365+
if pool.donut && !pool.espresso && !tx.Protected() {
366366
return core.ErrUnprotectedTransaction
367367
}
368368
if tx.EthCompatible() && !pool.donut {

0 commit comments

Comments
 (0)