Skip to content

Commit 6f4bb08

Browse files
committed
core/txpool/legacypool: add setCodeTx reorg test
PR: ethereum/go-ethereum#31206
1 parent b81f6c2 commit 6f4bb08

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

core/txpool/legacypool/legacypool_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,10 @@ func pricedSetCodeTx(nonce uint64, gaslimit uint64, gasFee, tip *uint256.Int, ke
228228
}, u.key)
229229
authList = append(authList, auth)
230230
}
231+
return pricedSetCodeTxWithAuth(nonce, gaslimit, gasFee, tip, key, authList)
232+
}
233+
234+
func pricedSetCodeTxWithAuth(nonce uint64, gaslimit uint64, gasFee, tip *uint256.Int, key *ecdsa.PrivateKey, authList []types.Authorization) *types.Transaction {
231235
return types.MustSignNewTx(key, types.LatestSignerForChainID(params.TestChainConfig.ChainID), &types.SetCodeTx{
232236
ChainID: params.TestChainConfig.ChainID.Uint64(),
233237
Nonce: nonce,
@@ -2791,6 +2795,75 @@ func TestSetCodeTransactions(t *testing.T) {
27912795
}
27922796
}
27932797

2798+
func TestSetCodeTransactionsReorg(t *testing.T) {
2799+
t.Parallel()
2800+
2801+
// Create the pool to test the status retrievals with
2802+
// statedb, _ := state.New(types.EmptyRootHash, state.NewDatabaseForTesting())
2803+
statedb, _ := state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
2804+
// blockchain := newTestBlockChain(params.MergedTestChainConfig, 1000000, statedb, new(event.Feed))
2805+
blockchain := &testBlockChain{1000000, statedb, new(event.Feed), 0}
2806+
2807+
pool := New(testTxPoolConfig, params.TestChainConfig, blockchain)
2808+
pool.Init(
2809+
testTxPoolConfig.PriceLimit,
2810+
blockchain.CurrentBlock().Header(),
2811+
makeAddressReserver(),
2812+
)
2813+
2814+
// wait for the pool to initialize
2815+
<-pool.initDoneCh
2816+
2817+
defer pool.Close()
2818+
2819+
// Create the test accounts
2820+
var (
2821+
keyA, _ = crypto.GenerateKey()
2822+
addrA = crypto.PubkeyToAddress(keyA.PublicKey)
2823+
)
2824+
testAddBalance(pool, addrA, big.NewInt(params.Ether))
2825+
// Send an authorization for 0x42
2826+
var authList []types.Authorization
2827+
auth, _ := types.SignAuth(types.Authorization{
2828+
ChainID: params.TestChainConfig.ChainID.Uint64(),
2829+
Address: common.Address{0x42},
2830+
Nonce: 0,
2831+
}, keyA)
2832+
authList = append(authList, auth)
2833+
if err := pool.addRemoteSync(pricedSetCodeTxWithAuth(0, 250000, uint256.NewInt(10), uint256.NewInt(3), keyA, authList)); err != nil {
2834+
t.Fatalf("failed to add with remote setcode transaction: %v", err)
2835+
}
2836+
// Simulate the chain moving
2837+
blockchain.statedb.SetNonce(addrA, 1)
2838+
blockchain.statedb.SetCode(addrA, types.AddressToDelegation(auth.Address))
2839+
<-pool.requestReset(nil, nil)
2840+
// Set an authorization for 0x00
2841+
auth, _ = types.SignAuth(types.Authorization{
2842+
ChainID: params.TestChainConfig.ChainID.Uint64(),
2843+
Address: common.Address{},
2844+
Nonce: 0,
2845+
}, keyA)
2846+
authList = append(authList, auth)
2847+
if err := pool.addRemoteSync(pricedSetCodeTxWithAuth(1, 250000, uint256.NewInt(10), uint256.NewInt(3), keyA, authList)); err != nil {
2848+
t.Fatalf("failed to add with remote setcode transaction: %v", err)
2849+
}
2850+
// Try to add a transactions in
2851+
if err := pool.addRemoteSync(pricedTransaction(2, 100000, big.NewInt(1000), keyA)); !errors.Is(err, ErrInflightTxLimitReached) {
2852+
t.Fatalf("unexpected error %v, expecting %v", err, ErrInflightTxLimitReached)
2853+
}
2854+
// Simulate the chain moving
2855+
blockchain.statedb.SetNonce(addrA, 2)
2856+
blockchain.statedb.SetCode(addrA, nil)
2857+
<-pool.requestReset(nil, nil)
2858+
// Now send two transactions from addrA
2859+
if err := pool.addRemoteSync(pricedTransaction(2, 100000, big.NewInt(1000), keyA)); err != nil {
2860+
t.Fatalf("failed to added single transaction: %v", err)
2861+
}
2862+
if err := pool.addRemoteSync(pricedTransaction(3, 100000, big.NewInt(1000), keyA)); err != nil {
2863+
t.Fatalf("failed to added single transaction: %v", err)
2864+
}
2865+
}
2866+
27942867
// Benchmarks the speed of validating the contents of the pending queue of the
27952868
// transaction pool.
27962869
func BenchmarkPendingDemotion100(b *testing.B) { benchmarkPendingDemotion(b, 100) }

0 commit comments

Comments
 (0)