aboutsummaryrefslogtreecommitdiffstats
path: root/core/tx_pool_test.go
blob: fa1a740dc247bfed33e10940e472743f7adb8a12 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
// Copyright 2015 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package core

import (
    "crypto/ecdsa"
    "math/big"
    "testing"

    "github.com/ethereum/go-ethereum/common"
    "github.com/ethereum/go-ethereum/core/state"
    "github.com/ethereum/go-ethereum/core/types"
    "github.com/ethereum/go-ethereum/crypto"
    "github.com/ethereum/go-ethereum/ethdb"
    "github.com/ethereum/go-ethereum/event"
)

func transaction(nonce uint64, gaslimit *big.Int, key *ecdsa.PrivateKey) *types.Transaction {
    tx, _ := types.NewTransaction(nonce, common.Address{}, big.NewInt(100), gaslimit, big.NewInt(1), nil).SignECDSA(key)
    return tx
}

func setupTxPool() (*TxPool, *ecdsa.PrivateKey) {
    db, _ := ethdb.NewMemDatabase()
    statedb, _ := state.New(common.Hash{}, db)

    var m event.TypeMux
    key, _ := crypto.GenerateKey()
    newPool := NewTxPool(&m, func() (*state.StateDB, error) { return statedb, nil }, func() *big.Int { return big.NewInt(1000000) })
    newPool.resetState()
    return newPool, key
}

func TestInvalidTransactions(t *testing.T) {
    pool, key := setupTxPool()

    tx := transaction(0, big.NewInt(100), key)
    if err := pool.Add(tx); err != ErrNonExistentAccount {
        t.Error("expected", ErrNonExistentAccount)
    }

    from, _ := tx.From()
    currentState, _ := pool.currentState()
    currentState.AddBalance(from, big.NewInt(1))
    if err := pool.Add(tx); err != ErrInsufficientFunds {
        t.Error("expected", ErrInsufficientFunds)
    }

    balance := new(big.Int).Add(tx.Value(), new(big.Int).Mul(tx.Gas(), tx.GasPrice()))
    currentState.AddBalance(from, balance)
    if err := pool.Add(tx); err != ErrIntrinsicGas {
        t.Error("expected", ErrIntrinsicGas, "got", err)
    }

    currentState.SetNonce(from, 1)
    currentState.AddBalance(from, big.NewInt(0xffffffffffffff))
    tx = transaction(0, big.NewInt(100000), key)
    if err := pool.Add(tx); err != ErrNonce {
        t.Error("expected", ErrNonce)
    }

    tx = transaction(1, big.NewInt(100000), key)
    pool.minGasPrice = big.NewInt(1000)
    if err := pool.Add(tx); err != ErrCheap {
        t.Error("expected", ErrCheap, "got", err)
    }

    pool.SetLocal(tx)
    if err := pool.Add(tx); err != nil {
        t.Error("expected", nil, "got", err)
    }
}

func TestTransactionQueue(t *testing.T) {
    pool, key := setupTxPool()
    tx := transaction(0, big.NewInt(100), key)
    from, _ := tx.From()
    currentState, _ := pool.currentState()
    currentState.AddBalance(from, big.NewInt(1000))
    pool.queueTx(tx.Hash(), tx)

    pool.checkQueue()
    if len(pool.pending) != 1 {
        t.Error("expected valid txs to be 1 is", len(pool.pending))
    }

    tx = transaction(1, big.NewInt(100), key)
    from, _ = tx.From()
    currentState.SetNonce(from, 2)
    pool.queueTx(tx.Hash(), tx)
    pool.checkQueue()
    if _, ok := pool.pending[tx.Hash()]; ok {
        t.Error("expected transaction to be in tx pool")
    }

    if len(pool.queue[from]) > 0 {
        t.Error("expected transaction queue to be empty. is", len(pool.queue[from]))
    }

    pool, key = setupTxPool()
    tx1 := transaction(0, big.NewInt(100), key)
    tx2 := transaction(10, big.NewInt(100), key)
    tx3 := transaction(11, big.NewInt(100), key)
    from, _ = tx1.From()
    currentState, _ = pool.currentState()
    currentState.AddBalance(from, big.NewInt(1000))
    pool.queueTx(tx1.Hash(), tx1)
    pool.queueTx(tx2.Hash(), tx2)
    pool.queueTx(tx3.Hash(), tx3)

    pool.checkQueue()

    if len(pool.pending) != 1 {
        t.Error("expected tx pool to be 1, got", len(pool.pending))
    }
    if len(pool.queue[from]) != 2 {
        t.Error("expected len(queue) == 2, got", len(pool.queue[from]))
    }
}

func TestRemoveTx(t *testing.T) {
    pool, key := setupTxPool()
    tx := transaction(0, big.NewInt(100), key)
    from, _ := tx.From()
    currentState, _ := pool.currentState()
    currentState.AddBalance(from, big.NewInt(1))
    pool.queueTx(tx.Hash(), tx)
    pool.addTx(tx.Hash(), from, tx)
    if len(pool.queue) != 1 {
        t.Error("expected queue to be 1, got", len(pool.queue))
    }

    if len(pool.pending) != 1 {
        t.Error("expected txs to be 1, got", len(pool.pending))
    }

    pool.RemoveTx(tx.Hash())

    if len(pool.queue) > 0 {
        t.Error("expected queue to be 0, got", len(pool.queue))
    }

    if len(pool.pending) > 0 {
        t.Error("expected txs to be 0, got", len(pool.pending))
    }
}

func TestNegativeValue(t *testing.T) {
    pool, key := setupTxPool()

    tx, _ := types.NewTransaction(0, common.Address{}, big.NewInt(-1), big.NewInt(100), big.NewInt(1), nil).SignECDSA(key)
    from, _ := tx.From()
    currentState, _ := pool.currentState()
    currentState.AddBalance(from, big.NewInt(1))
    if err := pool.Add(tx); err != ErrNegativeValue {
        t.Error("expected", ErrNegativeValue, "got", err)
    }
}

func TestTransactionChainFork(t *testing.T) {
    pool, key := setupTxPool()
    addr := crypto.PubkeyToAddress(key.PublicKey)
    resetState := func() {
        db, _ := ethdb.NewMemDatabase()
        statedb, _ := state.New(common.Hash{}, db)
        pool.currentState = func() (*state.StateDB, error) { return statedb, nil }
        currentState, _ := pool.currentState()
        currentState.AddBalance(addr, big.NewInt(100000000000000))
        pool.resetState()
    }
    resetState()

    tx := transaction(0, big.NewInt(100000), key)
    if err := pool.add(tx); err != nil {
        t.Error("didn't expect error", err)
    }
    pool.RemoveTransactions([]*types.Transaction{tx})

    // reset the pool's internal state
    resetState()
    if err := pool.add(tx); err != nil {
        t.Error("didn't expect error", err)
    }
}

func TestTransactionDoubleNonce(t *testing.T) {
    pool, key := setupTxPool()
    addr := crypto.PubkeyToAddress(key.PublicKey)
    resetState := func() {
        db, _ := ethdb.NewMemDatabase()
        statedb, _ := state.New(common.Hash{}, db)
        pool.currentState = func() (*state.StateDB, error) { return statedb, nil }
        currentState, _ := pool.currentState()
        currentState.AddBalance(addr, big.NewInt(100000000000000))
        pool.resetState()
    }
    resetState()

    tx := transaction(0, big.NewInt(100000), key)
    tx2 := transaction(0, big.NewInt(1000000), key)
    if err := pool.add(tx); err != nil {
        t.Error("didn't expect error", err)
    }
    if err := pool.add(tx2); err != nil {
        t.Error("didn't expect error", err)
    }

    pool.checkQueue()
    if len(pool.pending) != 2 {
        t.Error("expected 2 pending txs. Got", len(pool.pending))
    }
}

func TestMissingNonce(t *testing.T) {
    pool, key := setupTxPool()
    addr := crypto.PubkeyToAddress(key.PublicKey)
    currentState, _ := pool.currentState()
    currentState.AddBalance(addr, big.NewInt(100000000000000))
    tx := transaction(1, big.NewInt(100000), key)
    if err := pool.add(tx); err != nil {
        t.Error("didn't expect error", err)
    }
    if len(pool.pending) != 0 {
        t.Error("expected 0 pending transactions, got", len(pool.pending))
    }
    if len(pool.queue[addr]) != 1 {
        t.Error("expected 1 queued transaction, got", len(pool.queue[addr]))
    }
}

func TestNonceRecovery(t *testing.T) {
    const n = 10
    pool, key := setupTxPool()
    addr := crypto.PubkeyToAddress(key.PublicKey)
    currentState, _ := pool.currentState()
    currentState.SetNonce(addr, n)
    currentState.AddBalance(addr, big.NewInt(100000000000000))
    pool.resetState()
    tx := transaction(n, big.NewInt(100000), key)
    if err := pool.Add(tx); err != nil {
        t.Error(err)
    }
    // simulate some weird re-order of transactions and missing nonce(s)
    currentState.SetNonce(addr, n-1)
    pool.resetState()
    if fn := pool.pendingState.GetNonce(addr); fn != n+1 {
        t.Errorf("expected nonce to be %d, got %d", n+1, fn)
    }
}

func TestRemovedTxEvent(t *testing.T) {
    pool, key := setupTxPool()
    tx := transaction(0, big.NewInt(1000000), key)
    from, _ := tx.From()
    currentState, _ := pool.currentState()
    currentState.AddBalance(from, big.NewInt(1000000000000))
    pool.eventMux.Post(RemovedTransactionEvent{types.Transactions{tx}})
    pool.eventMux.Post(ChainHeadEvent{nil})
    if len(pool.pending) != 1 {
        t.Error("expected 1 pending tx, got", len(pool.pending))
    }
}

// Tests that if an account runs out of funds, any pending and queued transactions
// are dropped.
func TestTransactionDropping(t *testing.T) {
    // Create a test account and fund it
    pool, key := setupTxPool()
    account, _ := transaction(0, big.NewInt(0), key).From()

    state, _ := pool.currentState()
    state.AddBalance(account, big.NewInt(1000))

    // Add some pending and some queued transactions
    var (
        tx0  = transaction(0, big.NewInt(100), key)
        tx1  = transaction(1, big.NewInt(200), key)
        tx10 = transaction(10, big.NewInt(100), key)
        tx11 = transaction(11, big.NewInt(200), key)
    )
    pool.addTx(tx0.Hash(), account, tx0)
    pool.addTx(tx1.Hash(), account, tx1)
    pool.queueTx(tx10.Hash(), tx10)
    pool.queueTx(tx11.Hash(), tx11)

    // Check that pre and post validations leave the pool as is
    if len(pool.pending) != 2 {
        t.Errorf("pending transaction mismatch: have %d, want %d", len(pool.pending), 2)
    }
    if len(pool.queue[account]) != 2 {
        t.Errorf("queued transaction mismatch: have %d, want %d", len(pool.queue), 2)
    }
    pool.resetState()
    if len(pool.pending) != 2 {
        t.Errorf("pending transaction mismatch: have %d, want %d", len(pool.pending), 2)
    }
    if len(pool.queue[account]) != 2 {
        t.Errorf("queued transaction mismatch: have %d, want %d", len(pool.queue), 2)
    }
    // Reduce the balance of the account, and check that invalidated transactions are dropped
    state.AddBalance(account, big.NewInt(-750))
    pool.resetState()

    if _, ok := pool.pending[tx0.Hash()]; !ok {
        t.Errorf("funded pending transaction missing: %v", tx0)
    }
    if _, ok := pool.pending[tx1.Hash()]; ok {
        t.Errorf("out-of-fund pending transaction present: %v", tx1)
    }
    if _, ok := pool.queue[account][tx10.Hash()]; !ok {
        t.Errorf("funded queued transaction missing: %v", tx10)
    }
    if _, ok := pool.queue[account][tx11.Hash()]; ok {
        t.Errorf("out-of-fund queued transaction present: %v", tx11)
    }
}

// Tests that if a transaction is dropped from the current pending pool (e.g. out
// of fund), all consecutive (still valid, but not executable) transactions are
// postponed back into the future queue to prevent broadcasting them.
func TestTransactionPostponing(t *testing.T) {
    // Create a test account and fund it
    pool, key := setupTxPool()
    account, _ := transaction(0, big.NewInt(0), key).From()

    state, _ := pool.currentState()
    state.AddBalance(account, big.NewInt(1000))

    // Add a batch consecutive pending transactions for validation
    txns := []*types.Transaction{}
    for i := 0; i < 100; i++ {
        var tx *types.Transaction
        if i%2 == 0 {
            tx = transaction(uint64(i), big.NewInt(100), key)
        } else {
            tx = transaction(uint64(i), big.NewInt(500), key)
        }
        pool.addTx(tx.Hash(), account, tx)
        txns = append(txns, tx)
    }
    // Check that pre and post validations leave the pool as is
    if len(pool.pending) != len(txns) {
        t.Errorf("pending transaction mismatch: have %d, want %d", len(pool.pending), len(txns))
    }
    if len(pool.queue[account]) != 0 {
        t.Errorf("queued transaction mismatch: have %d, want %d", len(pool.queue), 0)
    }
    pool.resetState()
    if len(pool.pending) != len(txns) {
        t.Errorf("pending transaction mismatch: have %d, want %d", len(pool.pending), len(txns))
    }
    if len(pool.queue[account]) != 0 {
        t.Errorf("queued transaction mismatch: have %d, want %d", len(pool.queue), 0)
    }
    // Reduce the balance of the account, and check that transactions are reorganised
    state.AddBalance(account, big.NewInt(-750))
    pool.resetState()

    if _, ok := pool.pending[txns[0].Hash()]; !ok {
        t.Errorf("tx %d: valid and funded transaction missing from pending pool: %v", 0, txns[0])
    }
    if _, ok := pool.queue[account][txns[0].Hash()]; ok {
        t.Errorf("tx %d: valid and funded transaction present in future queue: %v", 0, txns[0])
    }
    for i, tx := range txns[1:] {
        if i%2 == 1 {
            if _, ok := pool.pending[tx.Hash()]; ok {
                t.Errorf("tx %d: valid but future transaction present in pending pool: %v", i+1, tx)
            }
            if _, ok := pool.queue[account][tx.Hash()]; !ok {
                t.Errorf("tx %d: valid but future transaction missing from future queue: %v", i+1, tx)
            }
        } else {
            if _, ok := pool.pending[tx.Hash()]; ok {
                t.Errorf("tx %d: out-of-fund transaction present in pending pool: %v", i+1, tx)
            }
            if _, ok := pool.queue[account][tx.Hash()]; ok {
                t.Errorf("tx %d: out-of-fund transaction present in future queue: %v", i+1, tx)
            }
        }
    }
}

// Tests that if the transaction count belonging to a single account goes above
// some threshold, the higher transactions are dropped to prevent DOS attacks.
func TestTransactionQueueLimiting(t *testing.T) {
    // Create a test account and fund it
    pool, key := setupTxPool()
    account, _ := transaction(0, big.NewInt(0), key).From()

    state, _ := pool.currentState()
    state.AddBalance(account, big.NewInt(1000000))

    // Keep queuing up transactions and make sure all above a limit are dropped
    for i := uint64(1); i <= maxQueued+5; i++ {
        if err := pool.Add(transaction(i, big.NewInt(100000), key)); err != nil {
            t.Fatalf("tx %d: failed to add transaction: %v", i, err)
        }
        if len(pool.pending) != 0 {
            t.Errorf("tx %d: pending pool size mismatch: have %d, want %d", i, len(pool.pending), 0)
        }
        if i <= maxQueued {
            if len(pool.queue[account]) != int(i) {
                t.Errorf("tx %d: queue size mismatch: have %d, want %d", i, len(pool.queue[account]), i)
            }
        } else {
            if len(pool.queue[account]) != maxQueued {
                t.Errorf("tx %d: queue limit mismatch: have %d, want %d", i, len(pool.queue[account]), maxQueued)
            }
        }
    }
}

// Tests that even if the transaction count belonging to a single account goes
// above some threshold, as long as the transactions are executable, they are
// accepted.
func TestTransactionPendingLimiting(t *testing.T) {
    // Create a test account and fund it
    pool, key := setupTxPool()
    account, _ := transaction(0, big.NewInt(0), key).From()

    state, _ := pool.currentState()
    state.AddBalance(account, big.NewInt(1000000))

    // Keep queuing up transactions and make sure all above a limit are dropped
    for i := uint64(0); i < maxQueued+5; i++ {
        if err := pool.Add(transaction(i, big.NewInt(100000), key)); err != nil {
            t.Fatalf("tx %d: failed to add transaction: %v", i, err)
        }
        if len(pool.pending) != int(i)+1 {
            t.Errorf("tx %d: pending pool size mismatch: have %d, want %d", i, len(pool.pending), i+1)
        }
        if len(pool.queue[account]) != 0 {
            t.Errorf("tx %d: queue size mismatch: have %d, want %d", i, len(pool.queue[account]), 0)
        }
    }
}

// Tests that the transaction limits are enforced the same way irrelevant whether
// the transactions are added one by one or in batches.
func TestTransactionQueueLimitingEquivalency(t *testing.T)   { testTransactionLimitingEquivalency(t, 1) }
func TestTransactionPendingLimitingEquivalency(t *testing.T) { testTransactionLimitingEquivalency(t, 0) }

func testTransactionLimitingEquivalency(t *testing.T, origin uint64) {
    // Add a batch of transactions to a pool one by one
    pool1, key1 := setupTxPool()
    account1, _ := transaction(0, big.NewInt(0), key1).From()
    state1, _ := pool1.currentState()
    state1.AddBalance(account1, big.NewInt(1000000))

    for i := uint64(0); i < maxQueued+5; i++ {
        if err := pool1.Add(transaction(origin+i, big.NewInt(100000), key1)); err != nil {
            t.Fatalf("tx %d: failed to add transaction: %v", i, err)
        }
    }
    // Add a batch of transactions to a pool in one bit batch
    pool2, key2 := setupTxPool()
    account2, _ := transaction(0, big.NewInt(0), key2).From()
    state2, _ := pool2.currentState()
    state2.AddBalance(account2, big.NewInt(1000000))

    txns := []*types.Transaction{}
    for i := uint64(0); i < maxQueued+5; i++ {
        txns = append(txns, transaction(origin+i, big.NewInt(100000), key2))
    }
    pool2.AddTransactions(txns)

    // Ensure the batch optimization honors the same pool mechanics
    if len(pool1.pending) != len(pool2.pending) {
        t.Errorf("pending transaction count mismatch: one-by-one algo: %d, batch algo: %d", len(pool1.pending), len(pool2.pending))
    }
    if len(pool1.queue[account1]) != len(pool2.queue[account2]) {
        t.Errorf("queued transaction count mismatch: one-by-one algo: %d, batch algo: %d", len(pool1.queue[account1]), len(pool2.queue[account2]))
    }
}

// Benchmarks the speed of validating the contents of the pending queue of the
// transaction pool.
func BenchmarkValidatePool100(b *testing.B)   { benchmarkValidatePool(b, 100) }
func BenchmarkValidatePool1000(b *testing.B)  { benchmarkValidatePool(b, 1000) }
func BenchmarkValidatePool10000(b *testing.B) { benchmarkValidatePool(b, 10000) }

func benchmarkValidatePool(b *testing.B, size int) {
    // Add a batch of transactions to a pool one by one
    pool, key := setupTxPool()
    account, _ := transaction(0, big.NewInt(0), key).From()
    state, _ := pool.currentState()
    state.AddBalance(account, big.NewInt(1000000))

    for i := 0; i < size; i++ {
        tx := transaction(uint64(i), big.NewInt(100000), key)
        pool.addTx(tx.Hash(), account, tx)
    }
    // Benchmark the speed of pool validation
    b.ResetTimer()
    for i := 0; i < b.N; i++ {
        pool.validatePool()
    }
}

// Benchmarks the speed of scheduling the contents of the future queue of the
// transaction pool.
func BenchmarkCheckQueue100(b *testing.B)   { benchmarkCheckQueue(b, 100) }
func BenchmarkCheckQueue1000(b *testing.B)  { benchmarkCheckQueue(b, 1000) }
func BenchmarkCheckQueue10000(b *testing.B) { benchmarkCheckQueue(b, 10000) }

func benchmarkCheckQueue(b *testing.B, size int) {
    // Add a batch of transactions to a pool one by one
    pool, key := setupTxPool()
    account, _ := transaction(0, big.NewInt(0), key).From()
    state, _ := pool.currentState()
    state.AddBalance(account, big.NewInt(1000000))

    for i := 0; i < size; i++ {
        tx := transaction(uint64(1+i), big.NewInt(100000), key)
        pool.queueTx(tx.Hash(), tx)
    }
    // Benchmark the speed of pool validation
    b.ResetTimer()
    for i := 0; i < b.N; i++ {
        pool.checkQueue()
    }
}