2015-07-07 03:54:22 +03:00
|
|
|
// Copyright 2015 The go-ethereum Authors
|
2015-07-22 19:48:40 +03:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-07 03:54:22 +03:00
|
|
|
//
|
2015-07-23 19:35:11 +03:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
2015-07-07 03:54:22 +03:00
|
|
|
// 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.
|
|
|
|
//
|
2015-07-22 19:48:40 +03:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-07 03:54:22 +03:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 19:48:40 +03:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-07 03:54:22 +03:00
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
2015-07-22 19:48:40 +03:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-07 03:54:22 +03:00
|
|
|
|
2015-01-02 13:09:38 +02:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
2015-04-04 22:41:24 +03:00
|
|
|
"math/big"
|
2015-01-02 13:09:38 +02:00
|
|
|
"testing"
|
|
|
|
|
2015-03-18 14:38:47 +02:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2015-04-04 22:41:24 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core/state"
|
2015-01-02 13:09:38 +02:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2015-01-07 14:17:48 +02:00
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
2015-01-02 13:09:38 +02:00
|
|
|
"github.com/ethereum/go-ethereum/event"
|
|
|
|
)
|
|
|
|
|
2015-06-11 15:05:32 +03:00
|
|
|
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
|
2015-01-02 13:09:38 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 21:47:32 +03:00
|
|
|
func setupTxPool() (*TxPool, *ecdsa.PrivateKey) {
|
|
|
|
db, _ := ethdb.NewMemDatabase()
|
2015-10-06 17:35:55 +03:00
|
|
|
statedb, _ := state.New(common.Hash{}, db)
|
2015-04-08 21:47:32 +03:00
|
|
|
|
2015-01-02 13:09:38 +02:00
|
|
|
var m event.TypeMux
|
|
|
|
key, _ := crypto.GenerateKey()
|
2016-03-02 00:32:43 +02:00
|
|
|
newPool := NewTxPool(testChainConfig(), &m, func() (*state.StateDB, error) { return statedb, nil }, func() *big.Int { return big.NewInt(1000000) })
|
2015-10-06 17:35:55 +03:00
|
|
|
newPool.resetState()
|
|
|
|
return newPool, key
|
2015-01-02 13:09:38 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 21:47:32 +03:00
|
|
|
func TestInvalidTransactions(t *testing.T) {
|
|
|
|
pool, key := setupTxPool()
|
2015-01-02 13:09:38 +02:00
|
|
|
|
2015-06-11 15:05:32 +03:00
|
|
|
tx := transaction(0, big.NewInt(100), key)
|
|
|
|
if err := pool.Add(tx); err != ErrNonExistentAccount {
|
2015-04-08 21:47:32 +03:00
|
|
|
t.Error("expected", ErrNonExistentAccount)
|
2015-01-02 13:09:38 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 21:47:32 +03:00
|
|
|
from, _ := tx.From()
|
2015-10-06 17:35:55 +03:00
|
|
|
currentState, _ := pool.currentState()
|
|
|
|
currentState.AddBalance(from, big.NewInt(1))
|
2015-06-11 15:05:32 +03:00
|
|
|
if err := pool.Add(tx); err != ErrInsufficientFunds {
|
2015-04-08 21:47:32 +03:00
|
|
|
t.Error("expected", ErrInsufficientFunds)
|
2015-01-02 13:09:38 +02:00
|
|
|
}
|
|
|
|
|
2015-04-26 12:19:40 +03:00
|
|
|
balance := new(big.Int).Add(tx.Value(), new(big.Int).Mul(tx.Gas(), tx.GasPrice()))
|
2015-10-06 17:35:55 +03:00
|
|
|
currentState.AddBalance(from, balance)
|
2015-06-11 15:05:32 +03:00
|
|
|
if err := pool.Add(tx); err != ErrIntrinsicGas {
|
2015-04-26 12:19:40 +03:00
|
|
|
t.Error("expected", ErrIntrinsicGas, "got", err)
|
2015-01-02 13:09:38 +02:00
|
|
|
}
|
2015-01-02 13:18:23 +02:00
|
|
|
|
2015-10-06 17:35:55 +03:00
|
|
|
currentState.SetNonce(from, 1)
|
|
|
|
currentState.AddBalance(from, big.NewInt(0xffffffffffffff))
|
2015-06-11 15:05:32 +03:00
|
|
|
tx = transaction(0, big.NewInt(100000), key)
|
|
|
|
if err := pool.Add(tx); err != ErrNonce {
|
2015-04-21 23:01:04 +03:00
|
|
|
t.Error("expected", ErrNonce)
|
|
|
|
}
|
2015-11-21 01:40:36 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2015-04-21 23:01:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTransactionQueue(t *testing.T) {
|
|
|
|
pool, key := setupTxPool()
|
2015-06-11 15:05:32 +03:00
|
|
|
tx := transaction(0, big.NewInt(100), key)
|
2015-04-21 23:01:04 +03:00
|
|
|
from, _ := tx.From()
|
2015-10-06 17:35:55 +03:00
|
|
|
currentState, _ := pool.currentState()
|
2015-12-30 18:31:37 +02:00
|
|
|
currentState.AddBalance(from, big.NewInt(1000))
|
2015-06-03 15:06:20 +03:00
|
|
|
pool.queueTx(tx.Hash(), tx)
|
2015-04-21 23:01:04 +03:00
|
|
|
|
|
|
|
pool.checkQueue()
|
2015-06-04 13:47:46 +03:00
|
|
|
if len(pool.pending) != 1 {
|
|
|
|
t.Error("expected valid txs to be 1 is", len(pool.pending))
|
2015-04-21 23:01:04 +03:00
|
|
|
}
|
|
|
|
|
2015-06-11 15:05:32 +03:00
|
|
|
tx = transaction(1, big.NewInt(100), key)
|
2015-04-21 23:01:04 +03:00
|
|
|
from, _ = tx.From()
|
2015-10-06 17:35:55 +03:00
|
|
|
currentState.SetNonce(from, 2)
|
2015-06-03 15:06:20 +03:00
|
|
|
pool.queueTx(tx.Hash(), tx)
|
2015-04-21 23:01:04 +03:00
|
|
|
pool.checkQueue()
|
2016-06-27 18:28:34 +03:00
|
|
|
if _, ok := pool.pending[from][tx.Nonce()]; ok {
|
2015-04-21 23:01:04 +03:00
|
|
|
t.Error("expected transaction to be in tx pool")
|
|
|
|
}
|
|
|
|
|
2015-06-03 23:22:20 +03:00
|
|
|
if len(pool.queue[from]) > 0 {
|
2015-04-21 23:01:04 +03:00
|
|
|
t.Error("expected transaction queue to be empty. is", len(pool.queue[from]))
|
|
|
|
}
|
|
|
|
|
|
|
|
pool, key = setupTxPool()
|
2015-06-11 15:05:32 +03:00
|
|
|
tx1 := transaction(0, big.NewInt(100), key)
|
|
|
|
tx2 := transaction(10, big.NewInt(100), key)
|
|
|
|
tx3 := transaction(11, big.NewInt(100), key)
|
2015-12-30 18:31:37 +02:00
|
|
|
from, _ = tx1.From()
|
|
|
|
currentState, _ = pool.currentState()
|
|
|
|
currentState.AddBalance(from, big.NewInt(1000))
|
2015-06-03 15:06:20 +03:00
|
|
|
pool.queueTx(tx1.Hash(), tx1)
|
|
|
|
pool.queueTx(tx2.Hash(), tx2)
|
|
|
|
pool.queueTx(tx3.Hash(), tx3)
|
|
|
|
|
2015-04-21 23:01:04 +03:00
|
|
|
pool.checkQueue()
|
|
|
|
|
2015-06-04 13:47:46 +03:00
|
|
|
if len(pool.pending) != 1 {
|
2015-12-30 18:31:37 +02:00
|
|
|
t.Error("expected tx pool to be 1, got", len(pool.pending))
|
2015-04-21 23:01:04 +03:00
|
|
|
}
|
2015-06-03 15:06:20 +03:00
|
|
|
if len(pool.queue[from]) != 2 {
|
|
|
|
t.Error("expected len(queue) == 2, got", len(pool.queue[from]))
|
2015-01-31 18:22:17 +02:00
|
|
|
}
|
|
|
|
}
|
2015-04-30 01:20:59 +03:00
|
|
|
|
|
|
|
func TestRemoveTx(t *testing.T) {
|
|
|
|
pool, key := setupTxPool()
|
2015-06-11 15:05:32 +03:00
|
|
|
tx := transaction(0, big.NewInt(100), key)
|
2015-04-30 01:20:59 +03:00
|
|
|
from, _ := tx.From()
|
2015-10-06 17:35:55 +03:00
|
|
|
currentState, _ := pool.currentState()
|
|
|
|
currentState.AddBalance(from, big.NewInt(1))
|
2015-06-03 15:06:20 +03:00
|
|
|
pool.queueTx(tx.Hash(), tx)
|
2016-06-27 18:28:34 +03:00
|
|
|
pool.addTx(from, tx)
|
2015-04-30 01:20:59 +03:00
|
|
|
if len(pool.queue) != 1 {
|
|
|
|
t.Error("expected queue to be 1, got", len(pool.queue))
|
|
|
|
}
|
|
|
|
|
2015-06-04 13:47:46 +03:00
|
|
|
if len(pool.pending) != 1 {
|
|
|
|
t.Error("expected txs to be 1, got", len(pool.pending))
|
2015-04-30 01:20:59 +03:00
|
|
|
}
|
|
|
|
|
2015-07-10 12:35:15 +03:00
|
|
|
pool.RemoveTx(tx.Hash())
|
2015-04-30 01:20:59 +03:00
|
|
|
|
|
|
|
if len(pool.queue) > 0 {
|
|
|
|
t.Error("expected queue to be 0, got", len(pool.queue))
|
|
|
|
}
|
|
|
|
|
2015-06-04 13:47:46 +03:00
|
|
|
if len(pool.pending) > 0 {
|
|
|
|
t.Error("expected txs to be 0, got", len(pool.pending))
|
2015-04-30 01:20:59 +03:00
|
|
|
}
|
|
|
|
}
|
2015-05-26 20:50:42 +03:00
|
|
|
|
|
|
|
func TestNegativeValue(t *testing.T) {
|
|
|
|
pool, key := setupTxPool()
|
|
|
|
|
2015-06-11 15:05:32 +03:00
|
|
|
tx, _ := types.NewTransaction(0, common.Address{}, big.NewInt(-1), big.NewInt(100), big.NewInt(1), nil).SignECDSA(key)
|
2015-05-26 20:50:42 +03:00
|
|
|
from, _ := tx.From()
|
2015-10-06 17:35:55 +03:00
|
|
|
currentState, _ := pool.currentState()
|
|
|
|
currentState.AddBalance(from, big.NewInt(1))
|
2015-06-11 15:05:32 +03:00
|
|
|
if err := pool.Add(tx); err != ErrNegativeValue {
|
2015-05-26 20:50:42 +03:00
|
|
|
t.Error("expected", ErrNegativeValue, "got", err)
|
|
|
|
}
|
|
|
|
}
|
2015-06-04 18:28:09 +03:00
|
|
|
|
|
|
|
func TestTransactionChainFork(t *testing.T) {
|
|
|
|
pool, key := setupTxPool()
|
|
|
|
addr := crypto.PubkeyToAddress(key.PublicKey)
|
2015-06-09 19:14:46 +03:00
|
|
|
resetState := func() {
|
|
|
|
db, _ := ethdb.NewMemDatabase()
|
2015-10-06 17:35:55 +03:00
|
|
|
statedb, _ := state.New(common.Hash{}, db)
|
|
|
|
pool.currentState = func() (*state.StateDB, error) { return statedb, nil }
|
|
|
|
currentState, _ := pool.currentState()
|
|
|
|
currentState.AddBalance(addr, big.NewInt(100000000000000))
|
2015-06-09 19:14:46 +03:00
|
|
|
pool.resetState()
|
|
|
|
}
|
|
|
|
resetState()
|
|
|
|
|
2015-06-11 15:05:32 +03:00
|
|
|
tx := transaction(0, big.NewInt(100000), key)
|
|
|
|
if err := pool.add(tx); err != nil {
|
2015-06-04 18:28:09 +03:00
|
|
|
t.Error("didn't expect error", err)
|
|
|
|
}
|
|
|
|
pool.RemoveTransactions([]*types.Transaction{tx})
|
|
|
|
|
|
|
|
// reset the pool's internal state
|
2015-06-09 19:14:46 +03:00
|
|
|
resetState()
|
2015-06-11 15:05:32 +03:00
|
|
|
if err := pool.add(tx); err != nil {
|
2015-06-04 18:28:09 +03:00
|
|
|
t.Error("didn't expect error", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTransactionDoubleNonce(t *testing.T) {
|
|
|
|
pool, key := setupTxPool()
|
|
|
|
addr := crypto.PubkeyToAddress(key.PublicKey)
|
2015-06-09 19:14:46 +03:00
|
|
|
resetState := func() {
|
|
|
|
db, _ := ethdb.NewMemDatabase()
|
2015-10-06 17:35:55 +03:00
|
|
|
statedb, _ := state.New(common.Hash{}, db)
|
|
|
|
pool.currentState = func() (*state.StateDB, error) { return statedb, nil }
|
|
|
|
currentState, _ := pool.currentState()
|
|
|
|
currentState.AddBalance(addr, big.NewInt(100000000000000))
|
2015-06-09 19:14:46 +03:00
|
|
|
pool.resetState()
|
|
|
|
}
|
|
|
|
resetState()
|
|
|
|
|
2016-06-27 18:28:34 +03:00
|
|
|
tx1, _ := types.NewTransaction(0, common.Address{}, big.NewInt(100), big.NewInt(100000), big.NewInt(1), nil).SignECDSA(key)
|
|
|
|
tx2, _ := types.NewTransaction(0, common.Address{}, big.NewInt(100), big.NewInt(1000000), big.NewInt(2), nil).SignECDSA(key)
|
|
|
|
tx3, _ := types.NewTransaction(0, common.Address{}, big.NewInt(100), big.NewInt(1000000), big.NewInt(1), nil).SignECDSA(key)
|
|
|
|
|
|
|
|
// Add the first two transaction, ensure higher priced stays only
|
|
|
|
if err := pool.add(tx1); err != nil {
|
2015-06-04 18:28:09 +03:00
|
|
|
t.Error("didn't expect error", err)
|
|
|
|
}
|
2015-06-11 15:05:32 +03:00
|
|
|
if err := pool.add(tx2); err != nil {
|
2015-06-04 18:28:09 +03:00
|
|
|
t.Error("didn't expect error", err)
|
|
|
|
}
|
2015-06-30 12:04:30 +03:00
|
|
|
pool.checkQueue()
|
2016-06-27 18:28:34 +03:00
|
|
|
if len(pool.pending[addr]) != 1 {
|
|
|
|
t.Error("expected 1 pending transactions, got", len(pool.pending))
|
|
|
|
}
|
|
|
|
if tx := pool.pending[addr][0]; tx.Hash() != tx2.Hash() {
|
|
|
|
t.Errorf("transaction mismatch: have %x, want %x", tx.Hash(), tx2.Hash())
|
|
|
|
}
|
|
|
|
// Add the thid transaction and ensure it's not saved (smaller price)
|
|
|
|
if err := pool.add(tx3); err != nil {
|
|
|
|
t.Error("didn't expect error", err)
|
|
|
|
}
|
|
|
|
pool.checkQueue()
|
|
|
|
if len(pool.pending[addr]) != 1 {
|
|
|
|
t.Error("expected 1 pending transactions, got", len(pool.pending))
|
|
|
|
}
|
|
|
|
if tx := pool.pending[addr][0]; tx.Hash() != tx2.Hash() {
|
|
|
|
t.Errorf("transaction mismatch: have %x, want %x", tx.Hash(), tx2.Hash())
|
|
|
|
}
|
|
|
|
// Ensure the total transaction count is correct
|
|
|
|
if len(pool.all) != 1 {
|
|
|
|
t.Error("expected 1 total transactions, got", len(pool.all))
|
2015-06-04 18:28:09 +03:00
|
|
|
}
|
|
|
|
}
|
2015-06-09 01:41:47 +03:00
|
|
|
|
|
|
|
func TestMissingNonce(t *testing.T) {
|
|
|
|
pool, key := setupTxPool()
|
|
|
|
addr := crypto.PubkeyToAddress(key.PublicKey)
|
2015-10-06 17:35:55 +03:00
|
|
|
currentState, _ := pool.currentState()
|
|
|
|
currentState.AddBalance(addr, big.NewInt(100000000000000))
|
2015-06-11 15:05:32 +03:00
|
|
|
tx := transaction(1, big.NewInt(100000), key)
|
|
|
|
if err := pool.add(tx); err != nil {
|
2015-06-09 01:41:47 +03:00
|
|
|
t.Error("didn't expect error", err)
|
|
|
|
}
|
2016-06-27 18:28:34 +03:00
|
|
|
if len(pool.pending[addr]) != 0 {
|
|
|
|
t.Error("expected 0 pending transactions, got", len(pool.pending[addr]))
|
2015-06-09 01:41:47 +03:00
|
|
|
}
|
|
|
|
if len(pool.queue[addr]) != 1 {
|
|
|
|
t.Error("expected 1 queued transaction, got", len(pool.queue[addr]))
|
|
|
|
}
|
2016-06-27 18:28:34 +03:00
|
|
|
if len(pool.all) != 1 {
|
|
|
|
t.Error("expected 1 total transactions, got", len(pool.all))
|
|
|
|
}
|
2015-06-09 01:41:47 +03:00
|
|
|
}
|
2015-09-18 12:59:21 +03:00
|
|
|
|
|
|
|
func TestNonceRecovery(t *testing.T) {
|
|
|
|
const n = 10
|
|
|
|
pool, key := setupTxPool()
|
|
|
|
addr := crypto.PubkeyToAddress(key.PublicKey)
|
2015-10-06 17:35:55 +03:00
|
|
|
currentState, _ := pool.currentState()
|
|
|
|
currentState.SetNonce(addr, n)
|
|
|
|
currentState.AddBalance(addr, big.NewInt(100000000000000))
|
2015-09-18 12:59:21 +03:00
|
|
|
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)
|
2015-10-06 17:35:55 +03:00
|
|
|
currentState.SetNonce(addr, n-1)
|
2015-09-18 12:59:21 +03:00
|
|
|
pool.resetState()
|
|
|
|
if fn := pool.pendingState.GetNonce(addr); fn != n+1 {
|
|
|
|
t.Errorf("expected nonce to be %d, got %d", n+1, fn)
|
|
|
|
}
|
|
|
|
}
|
2015-08-17 15:01:41 +03:00
|
|
|
|
|
|
|
func TestRemovedTxEvent(t *testing.T) {
|
|
|
|
pool, key := setupTxPool()
|
|
|
|
tx := transaction(0, big.NewInt(1000000), key)
|
|
|
|
from, _ := tx.From()
|
2015-10-06 17:35:55 +03:00
|
|
|
currentState, _ := pool.currentState()
|
|
|
|
currentState.AddBalance(from, big.NewInt(1000000000000))
|
2015-08-17 15:01:41 +03:00
|
|
|
pool.eventMux.Post(RemovedTransactionEvent{types.Transactions{tx}})
|
|
|
|
pool.eventMux.Post(ChainHeadEvent{nil})
|
2016-06-27 18:28:34 +03:00
|
|
|
if len(pool.pending[from]) != 1 {
|
|
|
|
t.Error("expected 1 pending tx, got", len(pool.pending[from]))
|
|
|
|
}
|
|
|
|
if len(pool.all) != 1 {
|
|
|
|
t.Error("expected 1 total transactions, got", len(pool.all))
|
2015-08-17 15:01:41 +03:00
|
|
|
}
|
|
|
|
}
|
2015-12-30 18:31:37 +02:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
)
|
2016-06-27 18:28:34 +03:00
|
|
|
pool.addTx(account, tx0)
|
|
|
|
pool.addTx(account, tx1)
|
2015-12-30 18:31:37 +02:00
|
|
|
pool.queueTx(tx10.Hash(), tx10)
|
|
|
|
pool.queueTx(tx11.Hash(), tx11)
|
|
|
|
|
|
|
|
// Check that pre and post validations leave the pool as is
|
2016-06-27 18:28:34 +03:00
|
|
|
if len(pool.pending[account]) != 2 {
|
|
|
|
t.Errorf("pending transaction mismatch: have %d, want %d", len(pool.pending[account]), 2)
|
2015-12-30 18:31:37 +02:00
|
|
|
}
|
|
|
|
if len(pool.queue[account]) != 2 {
|
2016-06-27 18:28:34 +03:00
|
|
|
t.Errorf("queued transaction mismatch: have %d, want %d", len(pool.queue[account]), 2)
|
|
|
|
}
|
|
|
|
if len(pool.all) != 4 {
|
|
|
|
t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), 4)
|
2015-12-30 18:31:37 +02:00
|
|
|
}
|
|
|
|
pool.resetState()
|
2016-06-27 18:28:34 +03:00
|
|
|
if len(pool.pending[account]) != 2 {
|
|
|
|
t.Errorf("pending transaction mismatch: have %d, want %d", len(pool.pending[account]), 2)
|
2015-12-30 18:31:37 +02:00
|
|
|
}
|
|
|
|
if len(pool.queue[account]) != 2 {
|
2016-06-27 18:28:34 +03:00
|
|
|
t.Errorf("queued transaction mismatch: have %d, want %d", len(pool.queue[account]), 2)
|
|
|
|
}
|
|
|
|
if len(pool.all) != 4 {
|
|
|
|
t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), 4)
|
2015-12-30 18:31:37 +02:00
|
|
|
}
|
|
|
|
// Reduce the balance of the account, and check that invalidated transactions are dropped
|
|
|
|
state.AddBalance(account, big.NewInt(-750))
|
|
|
|
pool.resetState()
|
|
|
|
|
2016-06-27 18:28:34 +03:00
|
|
|
if _, ok := pool.pending[account][tx0.Nonce()]; !ok {
|
2015-12-30 18:31:37 +02:00
|
|
|
t.Errorf("funded pending transaction missing: %v", tx0)
|
|
|
|
}
|
2016-06-27 18:28:34 +03:00
|
|
|
if _, ok := pool.pending[account][tx1.Nonce()]; ok {
|
2015-12-30 18:31:37 +02:00
|
|
|
t.Errorf("out-of-fund pending transaction present: %v", tx1)
|
|
|
|
}
|
2016-06-27 18:28:34 +03:00
|
|
|
if _, ok := pool.queue[account][tx10.Nonce()]; !ok {
|
2015-12-30 18:31:37 +02:00
|
|
|
t.Errorf("funded queued transaction missing: %v", tx10)
|
|
|
|
}
|
2016-06-27 18:28:34 +03:00
|
|
|
if _, ok := pool.queue[account][tx11.Nonce()]; ok {
|
2015-12-30 18:31:37 +02:00
|
|
|
t.Errorf("out-of-fund queued transaction present: %v", tx11)
|
|
|
|
}
|
2016-06-27 18:28:34 +03:00
|
|
|
if len(pool.all) != 2 {
|
|
|
|
t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), 2)
|
|
|
|
}
|
2015-12-30 18:31:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2016-03-15 20:08:18 +02:00
|
|
|
// postponed back into the future queue to prevent broadcasting them.
|
2015-12-30 18:31:37 +02:00
|
|
|
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)
|
|
|
|
}
|
2016-06-27 18:28:34 +03:00
|
|
|
pool.addTx(account, tx)
|
2015-12-30 18:31:37 +02:00
|
|
|
txns = append(txns, tx)
|
|
|
|
}
|
|
|
|
// Check that pre and post validations leave the pool as is
|
2016-06-27 18:28:34 +03:00
|
|
|
if len(pool.pending[account]) != len(txns) {
|
|
|
|
t.Errorf("pending transaction mismatch: have %d, want %d", len(pool.pending[account]), len(txns))
|
2015-12-30 18:31:37 +02:00
|
|
|
}
|
|
|
|
if len(pool.queue[account]) != 0 {
|
2016-06-27 18:28:34 +03:00
|
|
|
t.Errorf("queued transaction mismatch: have %d, want %d", len(pool.queue[account]), 0)
|
|
|
|
}
|
|
|
|
if len(pool.all) != len(txns) {
|
|
|
|
t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), len(txns))
|
2015-12-30 18:31:37 +02:00
|
|
|
}
|
|
|
|
pool.resetState()
|
2016-06-27 18:28:34 +03:00
|
|
|
if len(pool.pending[account]) != len(txns) {
|
|
|
|
t.Errorf("pending transaction mismatch: have %d, want %d", len(pool.pending[account]), len(txns))
|
2015-12-30 18:31:37 +02:00
|
|
|
}
|
|
|
|
if len(pool.queue[account]) != 0 {
|
2016-06-27 18:28:34 +03:00
|
|
|
t.Errorf("queued transaction mismatch: have %d, want %d", len(pool.queue[account]), 0)
|
|
|
|
}
|
|
|
|
if len(pool.all) != len(txns) {
|
|
|
|
t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), len(txns))
|
2015-12-30 18:31:37 +02:00
|
|
|
}
|
2016-03-15 20:55:39 +02:00
|
|
|
// Reduce the balance of the account, and check that transactions are reorganised
|
2015-12-30 18:31:37 +02:00
|
|
|
state.AddBalance(account, big.NewInt(-750))
|
|
|
|
pool.resetState()
|
|
|
|
|
2016-06-27 18:28:34 +03:00
|
|
|
if _, ok := pool.pending[account][txns[0].Nonce()]; !ok {
|
2015-12-30 18:31:37 +02:00
|
|
|
t.Errorf("tx %d: valid and funded transaction missing from pending pool: %v", 0, txns[0])
|
|
|
|
}
|
2016-06-27 18:28:34 +03:00
|
|
|
if _, ok := pool.queue[account][txns[0].Nonce()]; ok {
|
2015-12-30 18:31:37 +02:00
|
|
|
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 {
|
2016-06-27 18:28:34 +03:00
|
|
|
if _, ok := pool.pending[account][tx.Nonce()]; ok {
|
2015-12-30 18:31:37 +02:00
|
|
|
t.Errorf("tx %d: valid but future transaction present in pending pool: %v", i+1, tx)
|
|
|
|
}
|
2016-06-27 18:28:34 +03:00
|
|
|
if _, ok := pool.queue[account][tx.Nonce()]; !ok {
|
2015-12-30 18:31:37 +02:00
|
|
|
t.Errorf("tx %d: valid but future transaction missing from future queue: %v", i+1, tx)
|
|
|
|
}
|
|
|
|
} else {
|
2016-06-27 18:28:34 +03:00
|
|
|
if _, ok := pool.pending[account][tx.Nonce()]; ok {
|
2015-12-30 18:31:37 +02:00
|
|
|
t.Errorf("tx %d: out-of-fund transaction present in pending pool: %v", i+1, tx)
|
|
|
|
}
|
2016-06-27 18:28:34 +03:00
|
|
|
if _, ok := pool.queue[account][tx.Nonce()]; ok {
|
2015-12-30 18:31:37 +02:00
|
|
|
t.Errorf("tx %d: out-of-fund transaction present in future queue: %v", i+1, tx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-27 18:28:34 +03:00
|
|
|
if len(pool.all) != len(txns)/2 {
|
|
|
|
t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), len(txns)/2)
|
|
|
|
}
|
2015-12-30 18:31:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-27 18:28:34 +03:00
|
|
|
if len(pool.all) != maxQueued {
|
|
|
|
t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), maxQueued)
|
|
|
|
}
|
2015-12-30 18:31:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
2016-06-27 18:28:34 +03:00
|
|
|
if len(pool.pending[account]) != int(i)+1 {
|
|
|
|
t.Errorf("tx %d: pending pool size mismatch: have %d, want %d", i, len(pool.pending[account]), i+1)
|
2015-12-30 18:31:37 +02:00
|
|
|
}
|
|
|
|
if len(pool.queue[account]) != 0 {
|
|
|
|
t.Errorf("tx %d: queue size mismatch: have %d, want %d", i, len(pool.queue[account]), 0)
|
|
|
|
}
|
|
|
|
}
|
2016-06-27 18:28:34 +03:00
|
|
|
if len(pool.all) != maxQueued+5 {
|
|
|
|
t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), maxQueued+5)
|
|
|
|
}
|
2015-12-30 18:31:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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]))
|
|
|
|
}
|
2016-06-27 18:28:34 +03:00
|
|
|
if len(pool1.all) != len(pool2.all) {
|
|
|
|
t.Errorf("total transaction count mismatch: one-by-one algo %d, batch algo %d", len(pool1.all), len(pool2.all))
|
|
|
|
}
|
2015-12-30 18:31:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
2016-06-27 18:28:34 +03:00
|
|
|
pool.addTx(account, tx)
|
2015-12-30 18:31:37 +02:00
|
|
|
}
|
|
|
|
// 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()
|
|
|
|
}
|
|
|
|
}
|