Skip to content

Commit b0b3cf2

Browse files
holimankaralabe
authored andcommitted
core: add testcase for txpool
1 parent 58a1e13 commit b0b3cf2

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

core/tx_pool.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,19 @@ func (pool *TxPool) Stats() (int, int) {
301301
return pool.stats()
302302
}
303303

304+
// validateInternals checks if the content in pool.all
305+
// is consistent with the numbers reported in pending and queued
306+
func (pool *TxPool) validateInternals() error {
307+
pool.mu.RLock()
308+
defer pool.mu.RUnlock()
309+
p, q := pool.stats()
310+
a := len(pool.all)
311+
if a != p+q {
312+
return fmt.Errorf("Pool.all size %d != %d pending + %d queued", a, p, q)
313+
}
314+
return nil
315+
}
316+
304317
// stats retrieves the current pool stats, namely the number of pending and the
305318
// number of queued (non-executable) transactions.
306319
func (pool *TxPool) stats() (int, int) {

core/tx_pool_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,12 @@ func testTransactionLimitingEquivalency(t *testing.T, origin uint64) {
719719
txns = append(txns, transaction(origin+i, big.NewInt(100000), key2))
720720
}
721721
pool2.AddBatch(txns)
722+
if err := pool2.validateInternals(); err != nil {
723+
t.Error(err)
724+
}
725+
if err := pool1.validateInternals(); err != nil {
726+
t.Error(err)
727+
}
722728

723729
// Ensure the batch optimization honors the same pool mechanics
724730
if len(pool1.pending) != len(pool2.pending) {
@@ -769,6 +775,9 @@ func TestTransactionPendingGlobalLimiting(t *testing.T) {
769775
// Import the batch and verify that limits have been enforced
770776
pool.AddBatch(txs)
771777

778+
if err := pool.validateInternals(); err != nil {
779+
t.Error(err)
780+
}
772781
pending := 0
773782
for _, list := range pool.pending {
774783
pending += list.Len()
@@ -778,6 +787,42 @@ func TestTransactionPendingGlobalLimiting(t *testing.T) {
778787
}
779788
}
780789

790+
// Tests that if transactions start being capped, transasctions are also removed from 'all'
791+
func TestTransactionCapClearsFromAll(t *testing.T) {
792+
// Reduce the queue limits to shorten test time
793+
defer func(old uint64) { DefaultTxPoolConfig.GlobalSlots = old }(DefaultTxPoolConfig.GlobalSlots)
794+
DefaultTxPoolConfig.AccountSlots = 2
795+
DefaultTxPoolConfig.AccountQueue = 2
796+
DefaultTxPoolConfig.GlobalSlots = 8
797+
798+
// Create the pool to test the limit enforcement with
799+
db, _ := ethdb.NewMemDatabase()
800+
statedb, _ := state.New(common.Hash{}, db)
801+
802+
pool := NewTxPool(DefaultTxPoolConfig, params.TestChainConfig, new(event.TypeMux), func() (*state.StateDB, error) { return statedb, nil }, func() *big.Int { return big.NewInt(1000000) })
803+
pool.resetState()
804+
805+
// Create a number of test accounts and fund them
806+
state, _ := pool.currentState()
807+
808+
key, _ := crypto.GenerateKey()
809+
addr := crypto.PubkeyToAddress(key.PublicKey)
810+
state.AddBalance(addr, big.NewInt(1000000))
811+
812+
txs := types.Transactions{}
813+
nonce := uint64(0)
814+
for j := 0; j < int(DefaultTxPoolConfig.GlobalSlots)*2; j++ {
815+
tx := transaction(nonce, big.NewInt(100000), key)
816+
txs = append(txs, tx)
817+
nonce++
818+
}
819+
// Import the batch and verify that limits have been enforced
820+
pool.AddBatch(txs)
821+
if err := pool.validateInternals(); err != nil {
822+
t.Error(err)
823+
}
824+
}
825+
781826
// Tests that if the transaction count belonging to multiple accounts go above
782827
// some hard threshold, if they are under the minimum guaranteed slot count then
783828
// the transactions are still kept.
@@ -815,6 +860,10 @@ func TestTransactionPendingMinimumAllowance(t *testing.T) {
815860
// Import the batch and verify that limits have been enforced
816861
pool.AddBatch(txs)
817862

863+
if err := pool.validateInternals(); err != nil {
864+
t.Error(err)
865+
}
866+
818867
for addr, list := range pool.pending {
819868
if list.Len() != int(DefaultTxPoolConfig.AccountSlots) {
820869
t.Errorf("addr %x: total pending transactions mismatch: have %d, want %d", addr, list.Len(), DefaultTxPoolConfig.AccountSlots)
@@ -860,6 +909,10 @@ func TestTransactionPoolRepricing(t *testing.T) {
860909
// Import the batch and that both pending and queued transactions match up
861910
pool.AddBatch(txs)
862911

912+
if err := pool.validateInternals(); err != nil {
913+
t.Error(err)
914+
}
915+
863916
pending, queued := pool.stats()
864917
if pending != 4 {
865918
t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 4)
@@ -894,6 +947,10 @@ func TestTransactionPoolRepricing(t *testing.T) {
894947
if pending, _ = pool.stats(); pending != 3 {
895948
t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 3)
896949
}
950+
if err := pool.validateInternals(); err != nil {
951+
t.Error(err)
952+
}
953+
897954
}
898955

899956
// Tests that when the pool reaches its global transaction limit, underpriced
@@ -937,6 +994,9 @@ func TestTransactionPoolUnderpricing(t *testing.T) {
937994

938995
// Import the batch and that both pending and queued transactions match up
939996
pool.AddBatch(txs)
997+
if err := pool.validateInternals(); err != nil {
998+
t.Error(err)
999+
}
9401000

9411001
pending, queued := pool.stats()
9421002
if pending != 3 {
@@ -980,6 +1040,9 @@ func TestTransactionPoolUnderpricing(t *testing.T) {
9801040
if queued != 2 {
9811041
t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 2)
9821042
}
1043+
if err := pool.validateInternals(); err != nil {
1044+
t.Error(err)
1045+
}
9831046
}
9841047

9851048
// Tests that the pool rejects replacement transactions that don't meet the minimum
@@ -1041,6 +1104,9 @@ func TestTransactionReplacement(t *testing.T) {
10411104
if err := pool.Add(pricedTransaction(2, big.NewInt(100000), big.NewInt(threshold+1), key)); err != nil {
10421105
t.Fatalf("failed to replace original queued transaction: %v", err)
10431106
}
1107+
if err := pool.validateInternals(); err != nil {
1108+
t.Error(err)
1109+
}
10441110
}
10451111

10461112
// Benchmarks the speed of validating the contents of the pending queue of the

0 commit comments

Comments
 (0)