Skip to content

Commit 2b5d1a4

Browse files
committed
core: update txpool tests for the removal fix
1 parent f860143 commit 2b5d1a4

2 files changed

Lines changed: 112 additions & 53 deletions

File tree

‎core/tx_pool.go‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -877,17 +877,15 @@ func (pool *TxPool) removeTx(hash common.Hash) {
877877
// Remove the transaction from the pending lists and reset the account nonce
878878
if pending := pool.pending[addr]; pending != nil {
879879
if removed, invalids := pending.Remove(tx); removed {
880-
// If no more transactions are left, remove the list
880+
// If no more pending transactions are left, remove the list
881881
if pending.Empty() {
882882
delete(pool.pending, addr)
883883
delete(pool.beats, addr)
884884
}
885-
886-
// Otherwise postpone any invalidated transactions
885+
// Postpone any invalidated transactions
887886
for _, tx := range invalids {
888887
pool.enqueueTx(tx.Hash(), tx)
889888
}
890-
891889
// Update the account nonce if needed
892890
if nonce := tx.Nonce(); pool.pendingState.GetNonce(addr) > nonce {
893891
pool.pendingState.SetNonce(addr, nonce)

‎core/tx_pool_test.go‎

Lines changed: 110 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -557,74 +557,112 @@ func TestTransactionDropping(t *testing.T) {
557557
func TestTransactionPostponing(t *testing.T) {
558558
t.Parallel()
559559

560-
// Create a test account and fund it
561-
pool, key := setupTxPool()
560+
// Create the pool to test the postponing with
561+
db, _ := ethdb.NewMemDatabase()
562+
statedb, _ := state.New(common.Hash{}, state.NewDatabase(db))
563+
blockchain := &testBlockChain{statedb, 1000000, new(event.Feed)}
564+
565+
pool := NewTxPool(testTxPoolConfig, params.TestChainConfig, blockchain)
562566
defer pool.Stop()
563567

564-
account, _ := deriveSender(transaction(0, 0, key))
565-
pool.currentState.AddBalance(account, big.NewInt(1000))
568+
// Create two test accounts to produce different gap profiles with
569+
keys := make([]*ecdsa.PrivateKey, 2)
570+
accs := make([]common.Address, len(keys))
571+
572+
for i := 0; i < len(keys); i++ {
573+
keys[i], _ = crypto.GenerateKey()
574+
accs[i] = crypto.PubkeyToAddress(keys[i].PublicKey)
566575

576+
pool.currentState.AddBalance(crypto.PubkeyToAddress(keys[i].PublicKey), big.NewInt(50100))
577+
}
567578
// Add a batch consecutive pending transactions for validation
568-
txns := []*types.Transaction{}
569-
for i := 0; i < 100; i++ {
570-
var tx *types.Transaction
571-
if i%2 == 0 {
572-
tx = transaction(uint64(i), 100, key)
573-
} else {
574-
tx = transaction(uint64(i), 500, key)
579+
txs := []*types.Transaction{}
580+
for i, key := range keys {
581+
582+
for j := 0; j < 100; j++ {
583+
var tx *types.Transaction
584+
if (i+j)%2 == 0 {
585+
tx = transaction(uint64(j), 25000, key)
586+
} else {
587+
tx = transaction(uint64(j), 50000, key)
588+
}
589+
txs = append(txs, tx)
590+
}
591+
}
592+
for i, err := range pool.AddRemotes(txs) {
593+
if err != nil {
594+
t.Fatalf("tx %d: failed to add transactions: %v", i, err)
575595
}
576-
pool.promoteTx(account, tx.Hash(), tx)
577-
txns = append(txns, tx)
578596
}
579597
// Check that pre and post validations leave the pool as is
580-
if pool.pending[account].Len() != len(txns) {
581-
t.Errorf("pending transaction mismatch: have %d, want %d", pool.pending[account].Len(), len(txns))
598+
if pending := pool.pending[accs[0]].Len() + pool.pending[accs[1]].Len(); pending != len(txs) {
599+
t.Errorf("pending transaction mismatch: have %d, want %d", pending, len(txs))
582600
}
583601
if len(pool.queue) != 0 {
584-
t.Errorf("queued transaction mismatch: have %d, want %d", pool.queue[account].Len(), 0)
602+
t.Errorf("queued accounts mismatch: have %d, want %d", len(pool.queue), 0)
585603
}
586-
if len(pool.all) != len(txns) {
587-
t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), len(txns))
604+
if len(pool.all) != len(txs) {
605+
t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), len(txs))
588606
}
589607
pool.lockedReset(nil, nil)
590-
if pool.pending[account].Len() != len(txns) {
591-
t.Errorf("pending transaction mismatch: have %d, want %d", pool.pending[account].Len(), len(txns))
608+
if pending := pool.pending[accs[0]].Len() + pool.pending[accs[1]].Len(); pending != len(txs) {
609+
t.Errorf("pending transaction mismatch: have %d, want %d", pending, len(txs))
592610
}
593611
if len(pool.queue) != 0 {
594-
t.Errorf("queued transaction mismatch: have %d, want %d", pool.queue[account].Len(), 0)
612+
t.Errorf("queued accounts mismatch: have %d, want %d", len(pool.queue), 0)
595613
}
596-
if len(pool.all) != len(txns) {
597-
t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), len(txns))
614+
if len(pool.all) != len(txs) {
615+
t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), len(txs))
598616
}
599617
// Reduce the balance of the account, and check that transactions are reorganised
600-
pool.currentState.AddBalance(account, big.NewInt(-750))
618+
for _, addr := range accs {
619+
pool.currentState.AddBalance(addr, big.NewInt(-1))
620+
}
601621
pool.lockedReset(nil, nil)
602622

603-
if _, ok := pool.pending[account].txs.items[txns[0].Nonce()]; !ok {
604-
t.Errorf("tx %d: valid and funded transaction missing from pending pool: %v", 0, txns[0])
623+
// The first account's first transaction remains valid, check that subsequent
624+
// ones are either filtered out, or queued up for later.
625+
if _, ok := pool.pending[accs[0]].txs.items[txs[0].Nonce()]; !ok {
626+
t.Errorf("tx %d: valid and funded transaction missing from pending pool: %v", 0, txs[0])
605627
}
606-
if _, ok := pool.queue[account].txs.items[txns[0].Nonce()]; ok {
607-
t.Errorf("tx %d: valid and funded transaction present in future queue: %v", 0, txns[0])
628+
if _, ok := pool.queue[accs[0]].txs.items[txs[0].Nonce()]; ok {
629+
t.Errorf("tx %d: valid and funded transaction present in future queue: %v", 0, txs[0])
608630
}
609-
for i, tx := range txns[1:] {
631+
for i, tx := range txs[1:100] {
610632
if i%2 == 1 {
611-
if _, ok := pool.pending[account].txs.items[tx.Nonce()]; ok {
633+
if _, ok := pool.pending[accs[0]].txs.items[tx.Nonce()]; ok {
612634
t.Errorf("tx %d: valid but future transaction present in pending pool: %v", i+1, tx)
613635
}
614-
if _, ok := pool.queue[account].txs.items[tx.Nonce()]; !ok {
636+
if _, ok := pool.queue[accs[0]].txs.items[tx.Nonce()]; !ok {
615637
t.Errorf("tx %d: valid but future transaction missing from future queue: %v", i+1, tx)
616638
}
617639
} else {
618-
if _, ok := pool.pending[account].txs.items[tx.Nonce()]; ok {
640+
if _, ok := pool.pending[accs[0]].txs.items[tx.Nonce()]; ok {
619641
t.Errorf("tx %d: out-of-fund transaction present in pending pool: %v", i+1, tx)
620642
}
621-
if _, ok := pool.queue[account].txs.items[tx.Nonce()]; ok {
643+
if _, ok := pool.queue[accs[0]].txs.items[tx.Nonce()]; ok {
622644
t.Errorf("tx %d: out-of-fund transaction present in future queue: %v", i+1, tx)
623645
}
624646
}
625647
}
626-
if len(pool.all) != len(txns)/2 {
627-
t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), len(txns)/2)
648+
// The second account's first transaction got invalid, check that all transactions
649+
// are either filtered out, or queued up for later.
650+
if pool.pending[accs[1]] != nil {
651+
t.Errorf("invalidated account still has pending transactions")
652+
}
653+
for i, tx := range txs[100:] {
654+
if i%2 == 1 {
655+
if _, ok := pool.queue[accs[1]].txs.items[tx.Nonce()]; !ok {
656+
t.Errorf("tx %d: valid but future transaction missing from future queue: %v", 100+i, tx)
657+
}
658+
} else {
659+
if _, ok := pool.queue[accs[1]].txs.items[tx.Nonce()]; ok {
660+
t.Errorf("tx %d: out-of-fund transaction present in future queue: %v", 100+i, tx)
661+
}
662+
}
663+
}
664+
if len(pool.all) != len(txs)/2 {
665+
t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), len(txs)/2)
628666
}
629667
}
630668

@@ -949,11 +987,11 @@ func testTransactionLimitingEquivalency(t *testing.T, origin uint64) {
949987
account2, _ := deriveSender(transaction(0, 0, key2))
950988
pool2.currentState.AddBalance(account2, big.NewInt(1000000))
951989

952-
txns := []*types.Transaction{}
990+
txs := []*types.Transaction{}
953991
for i := uint64(0); i < testTxPoolConfig.AccountQueue+5; i++ {
954-
txns = append(txns, transaction(origin+i, 100000, key2))
992+
txs = append(txs, transaction(origin+i, 100000, key2))
955993
}
956-
pool2.AddRemotes(txns)
994+
pool2.AddRemotes(txs)
957995

958996
// Ensure the batch optimization honors the same pool mechanics
959997
if len(pool1.pending) != len(pool2.pending) {
@@ -1124,7 +1162,7 @@ func TestTransactionPoolRepricing(t *testing.T) {
11241162
defer sub.Unsubscribe()
11251163

11261164
// Create a number of test accounts and fund them
1127-
keys := make([]*ecdsa.PrivateKey, 3)
1165+
keys := make([]*ecdsa.PrivateKey, 4)
11281166
for i := 0; i < len(keys); i++ {
11291167
keys[i], _ = crypto.GenerateKey()
11301168
pool.currentState.AddBalance(crypto.PubkeyToAddress(keys[i].PublicKey), big.NewInt(1000000))
@@ -1136,24 +1174,28 @@ func TestTransactionPoolRepricing(t *testing.T) {
11361174
txs = append(txs, pricedTransaction(1, 100000, big.NewInt(1), keys[0]))
11371175
txs = append(txs, pricedTransaction(2, 100000, big.NewInt(2), keys[0]))
11381176

1177+
txs = append(txs, pricedTransaction(0, 100000, big.NewInt(1), keys[1]))
11391178
txs = append(txs, pricedTransaction(1, 100000, big.NewInt(2), keys[1]))
1140-
txs = append(txs, pricedTransaction(2, 100000, big.NewInt(1), keys[1]))
1141-
txs = append(txs, pricedTransaction(3, 100000, big.NewInt(2), keys[1]))
1179+
txs = append(txs, pricedTransaction(2, 100000, big.NewInt(2), keys[1]))
11421180

1143-
ltx := pricedTransaction(0, 100000, big.NewInt(1), keys[2])
1181+
txs = append(txs, pricedTransaction(1, 100000, big.NewInt(2), keys[2]))
1182+
txs = append(txs, pricedTransaction(2, 100000, big.NewInt(1), keys[2]))
1183+
txs = append(txs, pricedTransaction(3, 100000, big.NewInt(2), keys[2]))
1184+
1185+
ltx := pricedTransaction(0, 100000, big.NewInt(1), keys[3])
11441186

11451187
// Import the batch and that both pending and queued transactions match up
11461188
pool.AddRemotes(txs)
11471189
pool.AddLocal(ltx)
11481190

11491191
pending, queued := pool.Stats()
1150-
if pending != 4 {
1151-
t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 4)
1192+
if pending != 7 {
1193+
t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 7)
11521194
}
11531195
if queued != 3 {
11541196
t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 3)
11551197
}
1156-
if err := validateEvents(events, 4); err != nil {
1198+
if err := validateEvents(events, 7); err != nil {
11571199
t.Fatalf("original event firing failed: %v", err)
11581200
}
11591201
if err := validateTxPoolInternals(pool); err != nil {
@@ -1166,8 +1208,8 @@ func TestTransactionPoolRepricing(t *testing.T) {
11661208
if pending != 2 {
11671209
t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 2)
11681210
}
1169-
if queued != 3 {
1170-
t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 3)
1211+
if queued != 5 {
1212+
t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 5)
11711213
}
11721214
if err := validateEvents(events, 0); err != nil {
11731215
t.Fatalf("reprice event firing failed: %v", err)
@@ -1179,7 +1221,10 @@ func TestTransactionPoolRepricing(t *testing.T) {
11791221
if err := pool.AddRemote(pricedTransaction(1, 100000, big.NewInt(1), keys[0])); err != ErrUnderpriced {
11801222
t.Fatalf("adding underpriced pending transaction error mismatch: have %v, want %v", err, ErrUnderpriced)
11811223
}
1182-
if err := pool.AddRemote(pricedTransaction(2, 100000, big.NewInt(1), keys[1])); err != ErrUnderpriced {
1224+
if err := pool.AddRemote(pricedTransaction(0, 100000, big.NewInt(1), keys[1])); err != ErrUnderpriced {
1225+
t.Fatalf("adding underpriced pending transaction error mismatch: have %v, want %v", err, ErrUnderpriced)
1226+
}
1227+
if err := pool.AddRemote(pricedTransaction(2, 100000, big.NewInt(1), keys[2])); err != ErrUnderpriced {
11831228
t.Fatalf("adding underpriced queued transaction error mismatch: have %v, want %v", err, ErrUnderpriced)
11841229
}
11851230
if err := validateEvents(events, 0); err != nil {
@@ -1189,7 +1234,7 @@ func TestTransactionPoolRepricing(t *testing.T) {
11891234
t.Fatalf("pool internal state corrupted: %v", err)
11901235
}
11911236
// However we can add local underpriced transactions
1192-
tx := pricedTransaction(1, 100000, big.NewInt(1), keys[2])
1237+
tx := pricedTransaction(1, 100000, big.NewInt(1), keys[3])
11931238
if err := pool.AddLocal(tx); err != nil {
11941239
t.Fatalf("failed to add underpriced local transaction: %v", err)
11951240
}
@@ -1202,6 +1247,22 @@ func TestTransactionPoolRepricing(t *testing.T) {
12021247
if err := validateTxPoolInternals(pool); err != nil {
12031248
t.Fatalf("pool internal state corrupted: %v", err)
12041249
}
1250+
// And we can fill gaps with properly priced transactions
1251+
if err := pool.AddRemote(pricedTransaction(1, 100000, big.NewInt(2), keys[0])); err != nil {
1252+
t.Fatalf("failed to add pending transaction: %v", err)
1253+
}
1254+
if err := pool.AddRemote(pricedTransaction(0, 100000, big.NewInt(2), keys[1])); err != nil {
1255+
t.Fatalf("failed to add pending transaction: %v", err)
1256+
}
1257+
if err := pool.AddRemote(pricedTransaction(2, 100000, big.NewInt(2), keys[2])); err != nil {
1258+
t.Fatalf("failed to add queued transaction: %v", err)
1259+
}
1260+
if err := validateEvents(events, 5); err != nil {
1261+
t.Fatalf("post-reprice event firing failed: %v", err)
1262+
}
1263+
if err := validateTxPoolInternals(pool); err != nil {
1264+
t.Fatalf("pool internal state corrupted: %v", err)
1265+
}
12051266
}
12061267

12071268
// Tests that setting the transaction pool gas price to a higher value does not

0 commit comments

Comments
 (0)