2015-07-07 00:54:22 +00:00
|
|
|
// Copyright 2015 The go-ethereum Authors
|
2015-07-22 16:48:40 +00:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-07 00:54:22 +00:00
|
|
|
//
|
2015-07-23 16:35:11 +00:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
2015-07-07 00:54:22 +00: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 16:48:40 +00:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-07 00:54:22 +00:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 16:48:40 +00:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-07 00:54:22 +00: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 16:48:40 +00:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-07 00:54:22 +00:00
|
|
|
|
2015-01-02 11:09:38 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
2017-06-22 14:01:49 +00:00
|
|
|
"fmt"
|
2017-07-28 13:09:39 +00:00
|
|
|
"io/ioutil"
|
2015-04-04 19:41:24 +00:00
|
|
|
"math/big"
|
2016-08-17 13:53:15 +00:00
|
|
|
"math/rand"
|
2017-07-28 13:09:39 +00:00
|
|
|
"os"
|
2015-01-02 11:09:38 +00:00
|
|
|
"testing"
|
2016-08-17 13:53:15 +00:00
|
|
|
"time"
|
2015-01-02 11:09:38 +00:00
|
|
|
|
2015-03-18 12:38:47 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2015-04-04 19:41:24 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/state"
|
2015-01-02 11:09:38 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2015-01-07 12:17:48 +00:00
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
2015-01-02 11:09:38 +00:00
|
|
|
"github.com/ethereum/go-ethereum/event"
|
common: move big integer math to common/math (#3699)
* common: remove CurrencyToString
Move denomination values to params instead.
* common: delete dead code
* common: move big integer operations to common/math
This commit consolidates all big integer operations into common/math and
adds tests and documentation.
There should be no change in semantics for BigPow, BigMin, BigMax, S256,
U256, Exp and their behaviour is now locked in by tests.
The BigD, BytesToBig and Bytes2Big functions don't provide additional
value, all uses are replaced by new(big.Int).SetBytes().
BigToBytes is now called PaddedBigBytes, its minimum output size
parameter is now specified as the number of bytes instead of bits. The
single use of this function is in the EVM's MSTORE instruction.
Big and String2Big are replaced by ParseBig, which is slightly stricter.
It previously accepted leading zeros for hexadecimal inputs but treated
decimal inputs as octal if a leading zero digit was present.
ParseUint64 is used in places where String2Big was used to decode a
uint64.
The new functions MustParseBig and MustParseUint64 are now used in many
places where parsing errors were previously ignored.
* common: delete unused big integer variables
* accounts/abi: replace uses of BytesToBig with use of encoding/binary
* common: remove BytesToBig
* common: remove Bytes2Big
* common: remove BigTrue
* cmd/utils: add BigFlag and use it for error-checked integer flags
While here, remove environment variable processing for DirectoryFlag
because we don't use it.
* core: add missing error checks in genesis block parser
* common: remove String2Big
* cmd/evm: use utils.BigFlag
* common/math: check for 256 bit overflow in ParseBig
This is supposed to prevent silent overflow/truncation of values in the
genesis block JSON. Without this check, a genesis block that set a
balance larger than 256 bits would lead to weird behaviour in the VM.
* cmd/utils: fixup import
2017-02-26 21:21:51 +00:00
|
|
|
"github.com/ethereum/go-ethereum/params"
|
2015-01-02 11:09:38 +00:00
|
|
|
)
|
|
|
|
|
2017-07-28 13:09:39 +00:00
|
|
|
// testTxPoolConfig is a transaction pool configuration without stateful disk
|
|
|
|
// sideeffects used during testing.
|
|
|
|
var testTxPoolConfig TxPoolConfig
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
testTxPoolConfig = DefaultTxPoolConfig
|
|
|
|
testTxPoolConfig.Journal = ""
|
|
|
|
}
|
|
|
|
|
2015-06-11 12:05:32 +00:00
|
|
|
func transaction(nonce uint64, gaslimit *big.Int, key *ecdsa.PrivateKey) *types.Transaction {
|
2017-05-16 19:07:27 +00:00
|
|
|
return pricedTransaction(nonce, gaslimit, big.NewInt(1), key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func pricedTransaction(nonce uint64, gaslimit, gasprice *big.Int, key *ecdsa.PrivateKey) *types.Transaction {
|
|
|
|
tx, _ := types.SignTx(types.NewTransaction(nonce, common.Address{}, big.NewInt(100), gaslimit, gasprice, nil), types.HomesteadSigner{}, key)
|
2015-06-11 12:05:32 +00:00
|
|
|
return tx
|
2015-01-02 11:09:38 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 18:47:32 +00:00
|
|
|
func setupTxPool() (*TxPool, *ecdsa.PrivateKey) {
|
|
|
|
db, _ := ethdb.NewMemDatabase()
|
2017-06-27 13:57:06 +00:00
|
|
|
statedb, _ := state.New(common.Hash{}, state.NewDatabase(db))
|
2015-04-08 18:47:32 +00:00
|
|
|
|
2015-01-02 11:09:38 +00:00
|
|
|
key, _ := crypto.GenerateKey()
|
2017-07-28 13:09:39 +00:00
|
|
|
pool := NewTxPool(testTxPoolConfig, params.TestChainConfig, new(event.TypeMux), func() (*state.StateDB, error) { return statedb, nil }, func() *big.Int { return big.NewInt(1000000) })
|
2016-08-17 13:53:15 +00:00
|
|
|
|
2017-07-06 08:51:59 +00:00
|
|
|
return pool, key
|
2015-01-02 11:09:38 +00:00
|
|
|
}
|
|
|
|
|
2017-06-22 14:01:49 +00:00
|
|
|
// validateTxPoolInternals checks various consistency invariants within the pool.
|
|
|
|
func validateTxPoolInternals(pool *TxPool) error {
|
|
|
|
pool.mu.RLock()
|
|
|
|
defer pool.mu.RUnlock()
|
|
|
|
|
|
|
|
// Ensure the total transaction set is consistent with pending + queued
|
|
|
|
pending, queued := pool.stats()
|
|
|
|
if total := len(pool.all); total != pending+queued {
|
|
|
|
return fmt.Errorf("total transaction count %d != %d pending + %d queued", total, pending, queued)
|
|
|
|
}
|
|
|
|
if priced := pool.priced.items.Len() - pool.priced.stales; priced != pending+queued {
|
|
|
|
return fmt.Errorf("total priced transaction count %d != %d pending + %d queued", priced, pending, queued)
|
|
|
|
}
|
|
|
|
// Ensure the next nonce to assign is the correct one
|
|
|
|
for addr, txs := range pool.pending {
|
|
|
|
// Find the last transaction
|
|
|
|
var last uint64
|
|
|
|
for nonce, _ := range txs.txs.items {
|
|
|
|
if last < nonce {
|
|
|
|
last = nonce
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if nonce := pool.pendingState.GetNonce(addr); nonce != last+1 {
|
|
|
|
return fmt.Errorf("pending nonce mismatch: have %v, want %v", nonce, last+1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-11-02 12:44:13 +00:00
|
|
|
func deriveSender(tx *types.Transaction) (common.Address, error) {
|
|
|
|
return types.Sender(types.HomesteadSigner{}, tx)
|
|
|
|
}
|
|
|
|
|
2016-12-10 22:54:58 +00:00
|
|
|
// This test simulates a scenario where a new block is imported during a
|
|
|
|
// state reset and tests whether the pending state is in sync with the
|
|
|
|
// block head event that initiated the resetState().
|
|
|
|
func TestStateChangeDuringPoolReset(t *testing.T) {
|
|
|
|
var (
|
|
|
|
db, _ = ethdb.NewMemDatabase()
|
|
|
|
key, _ = crypto.GenerateKey()
|
|
|
|
address = crypto.PubkeyToAddress(key.PublicKey)
|
|
|
|
mux = new(event.TypeMux)
|
2017-06-27 13:57:06 +00:00
|
|
|
statedb, _ = state.New(common.Hash{}, state.NewDatabase(db))
|
2016-12-10 22:54:58 +00:00
|
|
|
trigger = false
|
|
|
|
)
|
|
|
|
|
|
|
|
// setup pool with 2 transaction in it
|
common: move big integer math to common/math (#3699)
* common: remove CurrencyToString
Move denomination values to params instead.
* common: delete dead code
* common: move big integer operations to common/math
This commit consolidates all big integer operations into common/math and
adds tests and documentation.
There should be no change in semantics for BigPow, BigMin, BigMax, S256,
U256, Exp and their behaviour is now locked in by tests.
The BigD, BytesToBig and Bytes2Big functions don't provide additional
value, all uses are replaced by new(big.Int).SetBytes().
BigToBytes is now called PaddedBigBytes, its minimum output size
parameter is now specified as the number of bytes instead of bits. The
single use of this function is in the EVM's MSTORE instruction.
Big and String2Big are replaced by ParseBig, which is slightly stricter.
It previously accepted leading zeros for hexadecimal inputs but treated
decimal inputs as octal if a leading zero digit was present.
ParseUint64 is used in places where String2Big was used to decode a
uint64.
The new functions MustParseBig and MustParseUint64 are now used in many
places where parsing errors were previously ignored.
* common: delete unused big integer variables
* accounts/abi: replace uses of BytesToBig with use of encoding/binary
* common: remove BytesToBig
* common: remove Bytes2Big
* common: remove BigTrue
* cmd/utils: add BigFlag and use it for error-checked integer flags
While here, remove environment variable processing for DirectoryFlag
because we don't use it.
* core: add missing error checks in genesis block parser
* common: remove String2Big
* cmd/evm: use utils.BigFlag
* common/math: check for 256 bit overflow in ParseBig
This is supposed to prevent silent overflow/truncation of values in the
genesis block JSON. Without this check, a genesis block that set a
balance larger than 256 bits would lead to weird behaviour in the VM.
* cmd/utils: fixup import
2017-02-26 21:21:51 +00:00
|
|
|
statedb.SetBalance(address, new(big.Int).SetUint64(params.Ether))
|
2016-12-10 22:54:58 +00:00
|
|
|
|
|
|
|
tx0 := transaction(0, big.NewInt(100000), key)
|
|
|
|
tx1 := transaction(1, big.NewInt(100000), key)
|
|
|
|
|
|
|
|
// stateFunc is used multiple times to reset the pending state.
|
|
|
|
// when simulate is true it will create a state that indicates
|
|
|
|
// that tx0 and tx1 are included in the chain.
|
|
|
|
stateFunc := func() (*state.StateDB, error) {
|
|
|
|
// delay "state change" by one. The tx pool fetches the
|
|
|
|
// state multiple times and by delaying it a bit we simulate
|
|
|
|
// a state change between those fetches.
|
|
|
|
stdb := statedb
|
|
|
|
if trigger {
|
2017-06-27 13:57:06 +00:00
|
|
|
statedb, _ = state.New(common.Hash{}, state.NewDatabase(db))
|
2016-12-10 22:54:58 +00:00
|
|
|
// simulate that the new head block included tx0 and tx1
|
|
|
|
statedb.SetNonce(address, 2)
|
common: move big integer math to common/math (#3699)
* common: remove CurrencyToString
Move denomination values to params instead.
* common: delete dead code
* common: move big integer operations to common/math
This commit consolidates all big integer operations into common/math and
adds tests and documentation.
There should be no change in semantics for BigPow, BigMin, BigMax, S256,
U256, Exp and their behaviour is now locked in by tests.
The BigD, BytesToBig and Bytes2Big functions don't provide additional
value, all uses are replaced by new(big.Int).SetBytes().
BigToBytes is now called PaddedBigBytes, its minimum output size
parameter is now specified as the number of bytes instead of bits. The
single use of this function is in the EVM's MSTORE instruction.
Big and String2Big are replaced by ParseBig, which is slightly stricter.
It previously accepted leading zeros for hexadecimal inputs but treated
decimal inputs as octal if a leading zero digit was present.
ParseUint64 is used in places where String2Big was used to decode a
uint64.
The new functions MustParseBig and MustParseUint64 are now used in many
places where parsing errors were previously ignored.
* common: delete unused big integer variables
* accounts/abi: replace uses of BytesToBig with use of encoding/binary
* common: remove BytesToBig
* common: remove Bytes2Big
* common: remove BigTrue
* cmd/utils: add BigFlag and use it for error-checked integer flags
While here, remove environment variable processing for DirectoryFlag
because we don't use it.
* core: add missing error checks in genesis block parser
* common: remove String2Big
* cmd/evm: use utils.BigFlag
* common/math: check for 256 bit overflow in ParseBig
This is supposed to prevent silent overflow/truncation of values in the
genesis block JSON. Without this check, a genesis block that set a
balance larger than 256 bits would lead to weird behaviour in the VM.
* cmd/utils: fixup import
2017-02-26 21:21:51 +00:00
|
|
|
statedb.SetBalance(address, new(big.Int).SetUint64(params.Ether))
|
2016-12-10 22:54:58 +00:00
|
|
|
trigger = false
|
|
|
|
}
|
|
|
|
return stdb, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
gasLimitFunc := func() *big.Int { return big.NewInt(1000000000) }
|
|
|
|
|
2017-07-28 13:09:39 +00:00
|
|
|
pool := NewTxPool(testTxPoolConfig, params.TestChainConfig, mux, stateFunc, gasLimitFunc)
|
2017-07-06 08:51:59 +00:00
|
|
|
defer pool.Stop()
|
2016-12-10 22:54:58 +00:00
|
|
|
|
2017-07-06 08:51:59 +00:00
|
|
|
nonce := pool.State().GetNonce(address)
|
2016-12-10 22:54:58 +00:00
|
|
|
if nonce != 0 {
|
|
|
|
t.Fatalf("Invalid nonce, want 0, got %d", nonce)
|
|
|
|
}
|
|
|
|
|
2017-07-06 08:51:59 +00:00
|
|
|
pool.AddRemotes(types.Transactions{tx0, tx1})
|
2016-12-10 22:54:58 +00:00
|
|
|
|
2017-07-06 08:51:59 +00:00
|
|
|
nonce = pool.State().GetNonce(address)
|
2016-12-10 22:54:58 +00:00
|
|
|
if nonce != 2 {
|
|
|
|
t.Fatalf("Invalid nonce, want 2, got %d", nonce)
|
|
|
|
}
|
|
|
|
|
|
|
|
// trigger state change in the background
|
|
|
|
trigger = true
|
|
|
|
|
2017-08-08 08:59:34 +00:00
|
|
|
pool.lockedReset()
|
2016-12-10 22:54:58 +00:00
|
|
|
|
2017-07-06 08:51:59 +00:00
|
|
|
pendingTx, err := pool.Pending()
|
2016-12-10 22:54:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not fetch pending transactions: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for addr, txs := range pendingTx {
|
|
|
|
t.Logf("%0x: %d\n", addr, len(txs))
|
|
|
|
}
|
|
|
|
|
2017-07-06 08:51:59 +00:00
|
|
|
nonce = pool.State().GetNonce(address)
|
2016-12-10 22:54:58 +00:00
|
|
|
if nonce != 2 {
|
|
|
|
t.Fatalf("Invalid nonce, want 2, got %d", nonce)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-08 18:47:32 +00:00
|
|
|
func TestInvalidTransactions(t *testing.T) {
|
|
|
|
pool, key := setupTxPool()
|
2017-07-06 08:51:59 +00:00
|
|
|
defer pool.Stop()
|
2015-01-02 11:09:38 +00:00
|
|
|
|
2015-06-11 12:05:32 +00:00
|
|
|
tx := transaction(0, big.NewInt(100), key)
|
2016-11-02 12:44:13 +00:00
|
|
|
from, _ := deriveSender(tx)
|
2015-10-06 14:35:55 +00:00
|
|
|
currentState, _ := pool.currentState()
|
|
|
|
currentState.AddBalance(from, big.NewInt(1))
|
2017-07-05 13:51:55 +00:00
|
|
|
if err := pool.AddRemote(tx); err != ErrInsufficientFunds {
|
2015-04-08 18:47:32 +00:00
|
|
|
t.Error("expected", ErrInsufficientFunds)
|
2015-01-02 11:09:38 +00:00
|
|
|
}
|
|
|
|
|
2015-04-26 09:19:40 +00:00
|
|
|
balance := new(big.Int).Add(tx.Value(), new(big.Int).Mul(tx.Gas(), tx.GasPrice()))
|
2015-10-06 14:35:55 +00:00
|
|
|
currentState.AddBalance(from, balance)
|
2017-07-05 13:51:55 +00:00
|
|
|
if err := pool.AddRemote(tx); err != ErrIntrinsicGas {
|
2015-04-26 09:19:40 +00:00
|
|
|
t.Error("expected", ErrIntrinsicGas, "got", err)
|
2015-01-02 11:09:38 +00:00
|
|
|
}
|
2015-01-02 11:18:23 +00:00
|
|
|
|
2015-10-06 14:35:55 +00:00
|
|
|
currentState.SetNonce(from, 1)
|
|
|
|
currentState.AddBalance(from, big.NewInt(0xffffffffffffff))
|
2015-06-11 12:05:32 +00:00
|
|
|
tx = transaction(0, big.NewInt(100000), key)
|
2017-07-05 13:51:55 +00:00
|
|
|
if err := pool.AddRemote(tx); err != ErrNonceTooLow {
|
2017-06-22 14:01:49 +00:00
|
|
|
t.Error("expected", ErrNonceTooLow)
|
2015-04-21 20:01:04 +00:00
|
|
|
}
|
2015-11-20 23:40:36 +00:00
|
|
|
|
|
|
|
tx = transaction(1, big.NewInt(100000), key)
|
2017-05-16 19:07:27 +00:00
|
|
|
pool.gasPrice = big.NewInt(1000)
|
2017-07-05 13:51:55 +00:00
|
|
|
if err := pool.AddRemote(tx); err != ErrUnderpriced {
|
2017-05-16 19:07:27 +00:00
|
|
|
t.Error("expected", ErrUnderpriced, "got", err)
|
2015-11-20 23:40:36 +00:00
|
|
|
}
|
2017-07-05 13:51:55 +00:00
|
|
|
if err := pool.AddLocal(tx); err != nil {
|
2015-11-20 23:40:36 +00:00
|
|
|
t.Error("expected", nil, "got", err)
|
|
|
|
}
|
2015-04-21 20:01:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTransactionQueue(t *testing.T) {
|
|
|
|
pool, key := setupTxPool()
|
2017-07-06 08:51:59 +00:00
|
|
|
defer pool.Stop()
|
|
|
|
|
2015-06-11 12:05:32 +00:00
|
|
|
tx := transaction(0, big.NewInt(100), key)
|
2016-11-02 12:44:13 +00:00
|
|
|
from, _ := deriveSender(tx)
|
2015-10-06 14:35:55 +00:00
|
|
|
currentState, _ := pool.currentState()
|
2015-12-30 16:31:37 +00:00
|
|
|
currentState.AddBalance(from, big.NewInt(1000))
|
2017-08-08 08:59:34 +00:00
|
|
|
pool.lockedReset()
|
2016-07-01 15:59:55 +00:00
|
|
|
pool.enqueueTx(tx.Hash(), tx)
|
2015-04-21 20:01:04 +00:00
|
|
|
|
2017-05-31 18:49:20 +00:00
|
|
|
pool.promoteExecutables(currentState, []common.Address{from})
|
2015-06-04 10:47:46 +00:00
|
|
|
if len(pool.pending) != 1 {
|
|
|
|
t.Error("expected valid txs to be 1 is", len(pool.pending))
|
2015-04-21 20:01:04 +00:00
|
|
|
}
|
|
|
|
|
2015-06-11 12:05:32 +00:00
|
|
|
tx = transaction(1, big.NewInt(100), key)
|
2016-11-02 12:44:13 +00:00
|
|
|
from, _ = deriveSender(tx)
|
2015-10-06 14:35:55 +00:00
|
|
|
currentState.SetNonce(from, 2)
|
2016-07-01 15:59:55 +00:00
|
|
|
pool.enqueueTx(tx.Hash(), tx)
|
2017-05-31 18:49:20 +00:00
|
|
|
pool.promoteExecutables(currentState, []common.Address{from})
|
2016-08-25 16:04:40 +00:00
|
|
|
if _, ok := pool.pending[from].txs.items[tx.Nonce()]; ok {
|
2015-04-21 20:01:04 +00:00
|
|
|
t.Error("expected transaction to be in tx pool")
|
|
|
|
}
|
|
|
|
|
2016-07-01 15:59:55 +00:00
|
|
|
if len(pool.queue) > 0 {
|
|
|
|
t.Error("expected transaction queue to be empty. is", len(pool.queue))
|
2015-04-21 20:01:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pool, key = setupTxPool()
|
2017-08-07 12:47:25 +00:00
|
|
|
defer pool.Stop()
|
|
|
|
|
2015-06-11 12:05:32 +00:00
|
|
|
tx1 := transaction(0, big.NewInt(100), key)
|
|
|
|
tx2 := transaction(10, big.NewInt(100), key)
|
|
|
|
tx3 := transaction(11, big.NewInt(100), key)
|
2016-11-02 12:44:13 +00:00
|
|
|
from, _ = deriveSender(tx1)
|
2015-12-30 16:31:37 +00:00
|
|
|
currentState, _ = pool.currentState()
|
|
|
|
currentState.AddBalance(from, big.NewInt(1000))
|
2017-08-08 08:59:34 +00:00
|
|
|
pool.lockedReset()
|
2016-12-10 22:54:58 +00:00
|
|
|
|
2016-07-01 15:59:55 +00:00
|
|
|
pool.enqueueTx(tx1.Hash(), tx1)
|
|
|
|
pool.enqueueTx(tx2.Hash(), tx2)
|
|
|
|
pool.enqueueTx(tx3.Hash(), tx3)
|
2015-06-03 12:06:20 +00:00
|
|
|
|
2017-05-31 18:49:20 +00:00
|
|
|
pool.promoteExecutables(currentState, []common.Address{from})
|
2015-04-21 20:01:04 +00:00
|
|
|
|
2015-06-04 10:47:46 +00:00
|
|
|
if len(pool.pending) != 1 {
|
2015-12-30 16:31:37 +00:00
|
|
|
t.Error("expected tx pool to be 1, got", len(pool.pending))
|
2015-04-21 20:01:04 +00:00
|
|
|
}
|
2016-07-01 15:59:55 +00:00
|
|
|
if pool.queue[from].Len() != 2 {
|
|
|
|
t.Error("expected len(queue) == 2, got", pool.queue[from].Len())
|
2015-01-31 16:22:17 +00:00
|
|
|
}
|
|
|
|
}
|
2015-04-29 22:20:59 +00:00
|
|
|
|
|
|
|
func TestRemoveTx(t *testing.T) {
|
|
|
|
pool, key := setupTxPool()
|
2017-07-06 08:51:59 +00:00
|
|
|
defer pool.Stop()
|
|
|
|
|
2017-06-22 14:01:49 +00:00
|
|
|
addr := crypto.PubkeyToAddress(key.PublicKey)
|
2015-10-06 14:35:55 +00:00
|
|
|
currentState, _ := pool.currentState()
|
2017-06-22 14:01:49 +00:00
|
|
|
currentState.AddBalance(addr, big.NewInt(1))
|
|
|
|
|
|
|
|
tx1 := transaction(0, big.NewInt(100), key)
|
|
|
|
tx2 := transaction(2, big.NewInt(100), key)
|
|
|
|
|
|
|
|
pool.promoteTx(addr, tx1.Hash(), tx1)
|
|
|
|
pool.enqueueTx(tx2.Hash(), tx2)
|
2016-07-01 15:59:55 +00:00
|
|
|
|
2015-04-29 22:20:59 +00:00
|
|
|
if len(pool.queue) != 1 {
|
|
|
|
t.Error("expected queue to be 1, got", len(pool.queue))
|
|
|
|
}
|
2015-06-04 10:47:46 +00:00
|
|
|
if len(pool.pending) != 1 {
|
2016-07-01 15:59:55 +00:00
|
|
|
t.Error("expected pending to be 1, got", len(pool.pending))
|
2015-04-29 22:20:59 +00:00
|
|
|
}
|
2017-06-22 14:01:49 +00:00
|
|
|
pool.Remove(tx1.Hash())
|
|
|
|
pool.Remove(tx2.Hash())
|
|
|
|
|
2015-04-29 22:20:59 +00:00
|
|
|
if len(pool.queue) > 0 {
|
|
|
|
t.Error("expected queue to be 0, got", len(pool.queue))
|
|
|
|
}
|
2015-06-04 10:47:46 +00:00
|
|
|
if len(pool.pending) > 0 {
|
2016-07-01 15:59:55 +00:00
|
|
|
t.Error("expected pending to be 0, got", len(pool.pending))
|
2015-04-29 22:20:59 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-26 17:50:42 +00:00
|
|
|
|
|
|
|
func TestNegativeValue(t *testing.T) {
|
|
|
|
pool, key := setupTxPool()
|
2017-07-06 08:51:59 +00:00
|
|
|
defer pool.Stop()
|
2015-05-26 17:50:42 +00:00
|
|
|
|
2017-01-05 11:59:17 +00:00
|
|
|
tx, _ := types.SignTx(types.NewTransaction(0, common.Address{}, big.NewInt(-1), big.NewInt(100), big.NewInt(1), nil), types.HomesteadSigner{}, key)
|
2016-11-02 12:44:13 +00:00
|
|
|
from, _ := deriveSender(tx)
|
2015-10-06 14:35:55 +00:00
|
|
|
currentState, _ := pool.currentState()
|
|
|
|
currentState.AddBalance(from, big.NewInt(1))
|
2017-07-05 13:51:55 +00:00
|
|
|
if err := pool.AddRemote(tx); err != ErrNegativeValue {
|
2015-05-26 17:50:42 +00:00
|
|
|
t.Error("expected", ErrNegativeValue, "got", err)
|
|
|
|
}
|
|
|
|
}
|
2015-06-04 15:28:09 +00:00
|
|
|
|
|
|
|
func TestTransactionChainFork(t *testing.T) {
|
|
|
|
pool, key := setupTxPool()
|
2017-07-06 08:51:59 +00:00
|
|
|
defer pool.Stop()
|
|
|
|
|
2015-06-04 15:28:09 +00:00
|
|
|
addr := crypto.PubkeyToAddress(key.PublicKey)
|
2015-06-09 16:14:46 +00:00
|
|
|
resetState := func() {
|
|
|
|
db, _ := ethdb.NewMemDatabase()
|
2017-06-27 13:57:06 +00:00
|
|
|
statedb, _ := state.New(common.Hash{}, state.NewDatabase(db))
|
2015-10-06 14:35:55 +00:00
|
|
|
pool.currentState = func() (*state.StateDB, error) { return statedb, nil }
|
|
|
|
currentState, _ := pool.currentState()
|
|
|
|
currentState.AddBalance(addr, big.NewInt(100000000000000))
|
2017-08-08 08:59:34 +00:00
|
|
|
pool.lockedReset()
|
2015-06-09 16:14:46 +00:00
|
|
|
}
|
|
|
|
resetState()
|
|
|
|
|
2015-06-11 12:05:32 +00:00
|
|
|
tx := transaction(0, big.NewInt(100000), key)
|
2017-07-05 13:51:55 +00:00
|
|
|
if _, err := pool.add(tx, false); err != nil {
|
2015-06-04 15:28:09 +00:00
|
|
|
t.Error("didn't expect error", err)
|
|
|
|
}
|
2016-07-01 15:59:55 +00:00
|
|
|
pool.RemoveBatch([]*types.Transaction{tx})
|
2015-06-04 15:28:09 +00:00
|
|
|
|
|
|
|
// reset the pool's internal state
|
2015-06-09 16:14:46 +00:00
|
|
|
resetState()
|
2017-07-05 13:51:55 +00:00
|
|
|
if _, err := pool.add(tx, false); err != nil {
|
2015-06-04 15:28:09 +00:00
|
|
|
t.Error("didn't expect error", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTransactionDoubleNonce(t *testing.T) {
|
|
|
|
pool, key := setupTxPool()
|
2017-07-06 08:51:59 +00:00
|
|
|
defer pool.Stop()
|
|
|
|
|
2015-06-04 15:28:09 +00:00
|
|
|
addr := crypto.PubkeyToAddress(key.PublicKey)
|
2015-06-09 16:14:46 +00:00
|
|
|
resetState := func() {
|
|
|
|
db, _ := ethdb.NewMemDatabase()
|
2017-06-27 13:57:06 +00:00
|
|
|
statedb, _ := state.New(common.Hash{}, state.NewDatabase(db))
|
2015-10-06 14:35:55 +00:00
|
|
|
pool.currentState = func() (*state.StateDB, error) { return statedb, nil }
|
|
|
|
currentState, _ := pool.currentState()
|
|
|
|
currentState.AddBalance(addr, big.NewInt(100000000000000))
|
2017-08-08 08:59:34 +00:00
|
|
|
pool.lockedReset()
|
2015-06-09 16:14:46 +00:00
|
|
|
}
|
|
|
|
resetState()
|
|
|
|
|
2016-11-02 12:44:13 +00:00
|
|
|
signer := types.HomesteadSigner{}
|
2017-01-05 11:59:17 +00:00
|
|
|
tx1, _ := types.SignTx(types.NewTransaction(0, common.Address{}, big.NewInt(100), big.NewInt(100000), big.NewInt(1), nil), signer, key)
|
|
|
|
tx2, _ := types.SignTx(types.NewTransaction(0, common.Address{}, big.NewInt(100), big.NewInt(1000000), big.NewInt(2), nil), signer, key)
|
|
|
|
tx3, _ := types.SignTx(types.NewTransaction(0, common.Address{}, big.NewInt(100), big.NewInt(1000000), big.NewInt(1), nil), signer, key)
|
2016-06-27 15:28:34 +00:00
|
|
|
|
|
|
|
// Add the first two transaction, ensure higher priced stays only
|
2017-07-05 13:51:55 +00:00
|
|
|
if replace, err := pool.add(tx1, false); err != nil || replace {
|
2017-05-16 19:07:27 +00:00
|
|
|
t.Errorf("first transaction insert failed (%v) or reported replacement (%v)", err, replace)
|
2015-06-04 15:28:09 +00:00
|
|
|
}
|
2017-07-05 13:51:55 +00:00
|
|
|
if replace, err := pool.add(tx2, false); err != nil || !replace {
|
2017-05-16 19:07:27 +00:00
|
|
|
t.Errorf("second transaction insert failed (%v) or not reported replacement (%v)", err, replace)
|
2015-06-04 15:28:09 +00:00
|
|
|
}
|
2016-12-10 22:54:58 +00:00
|
|
|
state, _ := pool.currentState()
|
2017-05-31 18:49:20 +00:00
|
|
|
pool.promoteExecutables(state, []common.Address{addr})
|
2016-07-01 15:59:55 +00:00
|
|
|
if pool.pending[addr].Len() != 1 {
|
|
|
|
t.Error("expected 1 pending transactions, got", pool.pending[addr].Len())
|
2016-06-27 15:28:34 +00:00
|
|
|
}
|
2016-08-25 16:04:40 +00:00
|
|
|
if tx := pool.pending[addr].txs.items[0]; tx.Hash() != tx2.Hash() {
|
2016-06-27 15:28:34 +00:00
|
|
|
t.Errorf("transaction mismatch: have %x, want %x", tx.Hash(), tx2.Hash())
|
|
|
|
}
|
2017-05-31 18:49:20 +00:00
|
|
|
// Add the third transaction and ensure it's not saved (smaller price)
|
2017-07-05 13:51:55 +00:00
|
|
|
pool.add(tx3, false)
|
2017-05-31 18:49:20 +00:00
|
|
|
pool.promoteExecutables(state, []common.Address{addr})
|
2016-07-01 15:59:55 +00:00
|
|
|
if pool.pending[addr].Len() != 1 {
|
|
|
|
t.Error("expected 1 pending transactions, got", pool.pending[addr].Len())
|
2016-06-27 15:28:34 +00:00
|
|
|
}
|
2016-08-25 16:04:40 +00:00
|
|
|
if tx := pool.pending[addr].txs.items[0]; tx.Hash() != tx2.Hash() {
|
2016-06-27 15:28:34 +00:00
|
|
|
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 15:28:09 +00:00
|
|
|
}
|
|
|
|
}
|
2015-06-08 22:41:47 +00:00
|
|
|
|
|
|
|
func TestMissingNonce(t *testing.T) {
|
|
|
|
pool, key := setupTxPool()
|
2017-07-06 08:51:59 +00:00
|
|
|
defer pool.Stop()
|
|
|
|
|
2015-06-08 22:41:47 +00:00
|
|
|
addr := crypto.PubkeyToAddress(key.PublicKey)
|
2015-10-06 14:35:55 +00:00
|
|
|
currentState, _ := pool.currentState()
|
|
|
|
currentState.AddBalance(addr, big.NewInt(100000000000000))
|
2015-06-11 12:05:32 +00:00
|
|
|
tx := transaction(1, big.NewInt(100000), key)
|
2017-07-05 13:51:55 +00:00
|
|
|
if _, err := pool.add(tx, false); err != nil {
|
2015-06-08 22:41:47 +00:00
|
|
|
t.Error("didn't expect error", err)
|
|
|
|
}
|
2016-07-01 15:59:55 +00:00
|
|
|
if len(pool.pending) != 0 {
|
|
|
|
t.Error("expected 0 pending transactions, got", len(pool.pending))
|
2015-06-08 22:41:47 +00:00
|
|
|
}
|
2016-07-01 15:59:55 +00:00
|
|
|
if pool.queue[addr].Len() != 1 {
|
|
|
|
t.Error("expected 1 queued transaction, got", pool.queue[addr].Len())
|
2015-06-08 22:41:47 +00:00
|
|
|
}
|
2016-06-27 15:28:34 +00:00
|
|
|
if len(pool.all) != 1 {
|
|
|
|
t.Error("expected 1 total transactions, got", len(pool.all))
|
|
|
|
}
|
2015-06-08 22:41:47 +00:00
|
|
|
}
|
2015-09-18 09:59:21 +00:00
|
|
|
|
|
|
|
func TestNonceRecovery(t *testing.T) {
|
|
|
|
const n = 10
|
|
|
|
pool, key := setupTxPool()
|
2017-07-06 08:51:59 +00:00
|
|
|
defer pool.Stop()
|
|
|
|
|
2015-09-18 09:59:21 +00:00
|
|
|
addr := crypto.PubkeyToAddress(key.PublicKey)
|
2015-10-06 14:35:55 +00:00
|
|
|
currentState, _ := pool.currentState()
|
|
|
|
currentState.SetNonce(addr, n)
|
|
|
|
currentState.AddBalance(addr, big.NewInt(100000000000000))
|
2017-08-08 08:59:34 +00:00
|
|
|
pool.lockedReset()
|
2015-09-18 09:59:21 +00:00
|
|
|
tx := transaction(n, big.NewInt(100000), key)
|
2017-07-05 13:51:55 +00:00
|
|
|
if err := pool.AddRemote(tx); err != nil {
|
2015-09-18 09:59:21 +00:00
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
// simulate some weird re-order of transactions and missing nonce(s)
|
2015-10-06 14:35:55 +00:00
|
|
|
currentState.SetNonce(addr, n-1)
|
2017-08-08 08:59:34 +00:00
|
|
|
pool.lockedReset()
|
2015-09-18 09:59:21 +00:00
|
|
|
if fn := pool.pendingState.GetNonce(addr); fn != n+1 {
|
|
|
|
t.Errorf("expected nonce to be %d, got %d", n+1, fn)
|
|
|
|
}
|
|
|
|
}
|
2015-08-17 12:01:41 +00:00
|
|
|
|
|
|
|
func TestRemovedTxEvent(t *testing.T) {
|
|
|
|
pool, key := setupTxPool()
|
2017-07-06 08:51:59 +00:00
|
|
|
defer pool.Stop()
|
|
|
|
|
2015-08-17 12:01:41 +00:00
|
|
|
tx := transaction(0, big.NewInt(1000000), key)
|
2016-11-02 12:44:13 +00:00
|
|
|
from, _ := deriveSender(tx)
|
2015-10-06 14:35:55 +00:00
|
|
|
currentState, _ := pool.currentState()
|
|
|
|
currentState.AddBalance(from, big.NewInt(1000000000000))
|
2017-08-08 08:59:34 +00:00
|
|
|
pool.lockedReset()
|
2015-08-17 12:01:41 +00:00
|
|
|
pool.eventMux.Post(RemovedTransactionEvent{types.Transactions{tx}})
|
|
|
|
pool.eventMux.Post(ChainHeadEvent{nil})
|
2016-07-01 15:59:55 +00:00
|
|
|
if pool.pending[from].Len() != 1 {
|
|
|
|
t.Error("expected 1 pending tx, got", pool.pending[from].Len())
|
2016-06-27 15:28:34 +00:00
|
|
|
}
|
|
|
|
if len(pool.all) != 1 {
|
|
|
|
t.Error("expected 1 total transactions, got", len(pool.all))
|
2015-08-17 12:01:41 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-30 16:31:37 +00: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()
|
2017-07-06 08:51:59 +00:00
|
|
|
defer pool.Stop()
|
|
|
|
|
2016-11-02 12:44:13 +00:00
|
|
|
account, _ := deriveSender(transaction(0, big.NewInt(0), key))
|
2015-12-30 16:31:37 +00:00
|
|
|
|
|
|
|
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)
|
2017-05-29 21:31:37 +00:00
|
|
|
tx2 = transaction(2, big.NewInt(300), key)
|
2015-12-30 16:31:37 +00:00
|
|
|
tx10 = transaction(10, big.NewInt(100), key)
|
|
|
|
tx11 = transaction(11, big.NewInt(200), key)
|
2017-05-29 21:31:37 +00:00
|
|
|
tx12 = transaction(12, big.NewInt(300), key)
|
2015-12-30 16:31:37 +00:00
|
|
|
)
|
2016-07-01 15:59:55 +00:00
|
|
|
pool.promoteTx(account, tx0.Hash(), tx0)
|
|
|
|
pool.promoteTx(account, tx1.Hash(), tx1)
|
2017-06-22 14:01:49 +00:00
|
|
|
pool.promoteTx(account, tx2.Hash(), tx2)
|
2016-07-01 15:59:55 +00:00
|
|
|
pool.enqueueTx(tx10.Hash(), tx10)
|
|
|
|
pool.enqueueTx(tx11.Hash(), tx11)
|
2017-06-22 14:01:49 +00:00
|
|
|
pool.enqueueTx(tx12.Hash(), tx12)
|
2015-12-30 16:31:37 +00:00
|
|
|
|
|
|
|
// Check that pre and post validations leave the pool as is
|
2017-05-29 21:31:37 +00:00
|
|
|
if pool.pending[account].Len() != 3 {
|
|
|
|
t.Errorf("pending transaction mismatch: have %d, want %d", pool.pending[account].Len(), 3)
|
2015-12-30 16:31:37 +00:00
|
|
|
}
|
2017-05-29 21:31:37 +00:00
|
|
|
if pool.queue[account].Len() != 3 {
|
|
|
|
t.Errorf("queued transaction mismatch: have %d, want %d", pool.queue[account].Len(), 3)
|
2016-06-27 15:28:34 +00:00
|
|
|
}
|
2017-06-22 14:01:49 +00:00
|
|
|
if len(pool.all) != 6 {
|
|
|
|
t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), 6)
|
2015-12-30 16:31:37 +00:00
|
|
|
}
|
2017-08-08 08:59:34 +00:00
|
|
|
pool.lockedReset()
|
2017-05-29 21:31:37 +00:00
|
|
|
if pool.pending[account].Len() != 3 {
|
|
|
|
t.Errorf("pending transaction mismatch: have %d, want %d", pool.pending[account].Len(), 3)
|
2015-12-30 16:31:37 +00:00
|
|
|
}
|
2017-05-29 21:31:37 +00:00
|
|
|
if pool.queue[account].Len() != 3 {
|
|
|
|
t.Errorf("queued transaction mismatch: have %d, want %d", pool.queue[account].Len(), 3)
|
2016-06-27 15:28:34 +00:00
|
|
|
}
|
2017-06-22 14:01:49 +00:00
|
|
|
if len(pool.all) != 6 {
|
|
|
|
t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), 6)
|
2015-12-30 16:31:37 +00:00
|
|
|
}
|
|
|
|
// Reduce the balance of the account, and check that invalidated transactions are dropped
|
2017-05-29 21:31:37 +00:00
|
|
|
state.AddBalance(account, big.NewInt(-650))
|
2017-08-08 08:59:34 +00:00
|
|
|
pool.lockedReset()
|
2015-12-30 16:31:37 +00:00
|
|
|
|
2016-08-25 16:04:40 +00:00
|
|
|
if _, ok := pool.pending[account].txs.items[tx0.Nonce()]; !ok {
|
2015-12-30 16:31:37 +00:00
|
|
|
t.Errorf("funded pending transaction missing: %v", tx0)
|
|
|
|
}
|
2017-05-29 21:31:37 +00:00
|
|
|
if _, ok := pool.pending[account].txs.items[tx1.Nonce()]; !ok {
|
|
|
|
t.Errorf("funded pending transaction missing: %v", tx0)
|
|
|
|
}
|
|
|
|
if _, ok := pool.pending[account].txs.items[tx2.Nonce()]; ok {
|
2015-12-30 16:31:37 +00:00
|
|
|
t.Errorf("out-of-fund pending transaction present: %v", tx1)
|
|
|
|
}
|
2016-08-25 16:04:40 +00:00
|
|
|
if _, ok := pool.queue[account].txs.items[tx10.Nonce()]; !ok {
|
2015-12-30 16:31:37 +00:00
|
|
|
t.Errorf("funded queued transaction missing: %v", tx10)
|
|
|
|
}
|
2017-05-29 21:31:37 +00:00
|
|
|
if _, ok := pool.queue[account].txs.items[tx11.Nonce()]; !ok {
|
|
|
|
t.Errorf("funded queued transaction missing: %v", tx10)
|
|
|
|
}
|
|
|
|
if _, ok := pool.queue[account].txs.items[tx12.Nonce()]; ok {
|
2015-12-30 16:31:37 +00:00
|
|
|
t.Errorf("out-of-fund queued transaction present: %v", tx11)
|
|
|
|
}
|
2017-05-29 21:31:37 +00:00
|
|
|
if len(pool.all) != 4 {
|
|
|
|
t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), 4)
|
|
|
|
}
|
|
|
|
// Reduce the block gas limit, check that invalidated transactions are dropped
|
|
|
|
pool.gasLimit = func() *big.Int { return big.NewInt(100) }
|
2017-08-08 08:59:34 +00:00
|
|
|
pool.lockedReset()
|
2017-05-29 21:31:37 +00:00
|
|
|
|
|
|
|
if _, ok := pool.pending[account].txs.items[tx0.Nonce()]; !ok {
|
|
|
|
t.Errorf("funded pending transaction missing: %v", tx0)
|
|
|
|
}
|
|
|
|
if _, ok := pool.pending[account].txs.items[tx1.Nonce()]; ok {
|
|
|
|
t.Errorf("over-gased pending transaction present: %v", tx1)
|
|
|
|
}
|
|
|
|
if _, ok := pool.queue[account].txs.items[tx10.Nonce()]; !ok {
|
|
|
|
t.Errorf("funded queued transaction missing: %v", tx10)
|
|
|
|
}
|
|
|
|
if _, ok := pool.queue[account].txs.items[tx11.Nonce()]; ok {
|
|
|
|
t.Errorf("over-gased queued transaction present: %v", tx11)
|
|
|
|
}
|
2016-06-27 15:28:34 +00:00
|
|
|
if len(pool.all) != 2 {
|
|
|
|
t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), 2)
|
|
|
|
}
|
2015-12-30 16:31:37 +00: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 18:08:18 +00:00
|
|
|
// postponed back into the future queue to prevent broadcasting them.
|
2015-12-30 16:31:37 +00:00
|
|
|
func TestTransactionPostponing(t *testing.T) {
|
|
|
|
// Create a test account and fund it
|
|
|
|
pool, key := setupTxPool()
|
2017-07-06 08:51:59 +00:00
|
|
|
defer pool.Stop()
|
|
|
|
|
2016-11-02 12:44:13 +00:00
|
|
|
account, _ := deriveSender(transaction(0, big.NewInt(0), key))
|
2015-12-30 16:31:37 +00:00
|
|
|
|
|
|
|
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-07-01 15:59:55 +00:00
|
|
|
pool.promoteTx(account, tx.Hash(), tx)
|
2015-12-30 16:31:37 +00:00
|
|
|
txns = append(txns, tx)
|
|
|
|
}
|
|
|
|
// Check that pre and post validations leave the pool as is
|
2016-07-01 15:59:55 +00:00
|
|
|
if pool.pending[account].Len() != len(txns) {
|
|
|
|
t.Errorf("pending transaction mismatch: have %d, want %d", pool.pending[account].Len(), len(txns))
|
2015-12-30 16:31:37 +00:00
|
|
|
}
|
2016-07-01 15:59:55 +00:00
|
|
|
if len(pool.queue) != 0 {
|
|
|
|
t.Errorf("queued transaction mismatch: have %d, want %d", pool.queue[account].Len(), 0)
|
2016-06-27 15:28:34 +00:00
|
|
|
}
|
|
|
|
if len(pool.all) != len(txns) {
|
|
|
|
t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), len(txns))
|
2015-12-30 16:31:37 +00:00
|
|
|
}
|
2017-08-08 08:59:34 +00:00
|
|
|
pool.lockedReset()
|
2016-07-01 15:59:55 +00:00
|
|
|
if pool.pending[account].Len() != len(txns) {
|
|
|
|
t.Errorf("pending transaction mismatch: have %d, want %d", pool.pending[account].Len(), len(txns))
|
2015-12-30 16:31:37 +00:00
|
|
|
}
|
2016-07-01 15:59:55 +00:00
|
|
|
if len(pool.queue) != 0 {
|
|
|
|
t.Errorf("queued transaction mismatch: have %d, want %d", pool.queue[account].Len(), 0)
|
2016-06-27 15:28:34 +00:00
|
|
|
}
|
|
|
|
if len(pool.all) != len(txns) {
|
|
|
|
t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), len(txns))
|
2015-12-30 16:31:37 +00:00
|
|
|
}
|
2016-03-15 18:55:39 +00:00
|
|
|
// Reduce the balance of the account, and check that transactions are reorganised
|
2015-12-30 16:31:37 +00:00
|
|
|
state.AddBalance(account, big.NewInt(-750))
|
2017-08-08 08:59:34 +00:00
|
|
|
pool.lockedReset()
|
2015-12-30 16:31:37 +00:00
|
|
|
|
2016-08-25 16:04:40 +00:00
|
|
|
if _, ok := pool.pending[account].txs.items[txns[0].Nonce()]; !ok {
|
2015-12-30 16:31:37 +00:00
|
|
|
t.Errorf("tx %d: valid and funded transaction missing from pending pool: %v", 0, txns[0])
|
|
|
|
}
|
2016-08-25 16:04:40 +00:00
|
|
|
if _, ok := pool.queue[account].txs.items[txns[0].Nonce()]; ok {
|
2015-12-30 16:31:37 +00: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-08-25 16:04:40 +00:00
|
|
|
if _, ok := pool.pending[account].txs.items[tx.Nonce()]; ok {
|
2015-12-30 16:31:37 +00:00
|
|
|
t.Errorf("tx %d: valid but future transaction present in pending pool: %v", i+1, tx)
|
|
|
|
}
|
2016-08-25 16:04:40 +00:00
|
|
|
if _, ok := pool.queue[account].txs.items[tx.Nonce()]; !ok {
|
2015-12-30 16:31:37 +00:00
|
|
|
t.Errorf("tx %d: valid but future transaction missing from future queue: %v", i+1, tx)
|
|
|
|
}
|
|
|
|
} else {
|
2016-08-25 16:04:40 +00:00
|
|
|
if _, ok := pool.pending[account].txs.items[tx.Nonce()]; ok {
|
2015-12-30 16:31:37 +00:00
|
|
|
t.Errorf("tx %d: out-of-fund transaction present in pending pool: %v", i+1, tx)
|
|
|
|
}
|
2016-08-25 16:04:40 +00:00
|
|
|
if _, ok := pool.queue[account].txs.items[tx.Nonce()]; ok {
|
2015-12-30 16:31:37 +00:00
|
|
|
t.Errorf("tx %d: out-of-fund transaction present in future queue: %v", i+1, tx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-27 15:28:34 +00: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 16:31:37 +00: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.
|
2016-08-17 13:53:15 +00:00
|
|
|
func TestTransactionQueueAccountLimiting(t *testing.T) {
|
2015-12-30 16:31:37 +00:00
|
|
|
// Create a test account and fund it
|
|
|
|
pool, key := setupTxPool()
|
2017-07-06 08:51:59 +00:00
|
|
|
defer pool.Stop()
|
|
|
|
|
2016-11-02 12:44:13 +00:00
|
|
|
account, _ := deriveSender(transaction(0, big.NewInt(0), key))
|
2015-12-30 16:31:37 +00:00
|
|
|
|
|
|
|
state, _ := pool.currentState()
|
|
|
|
state.AddBalance(account, big.NewInt(1000000))
|
2017-08-08 08:59:34 +00:00
|
|
|
pool.lockedReset()
|
2015-12-30 16:31:37 +00:00
|
|
|
|
|
|
|
// Keep queuing up transactions and make sure all above a limit are dropped
|
2017-07-28 13:09:39 +00:00
|
|
|
for i := uint64(1); i <= testTxPoolConfig.AccountQueue+5; i++ {
|
2017-07-05 13:51:55 +00:00
|
|
|
if err := pool.AddRemote(transaction(i, big.NewInt(100000), key)); err != nil {
|
2015-12-30 16:31:37 +00:00
|
|
|
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)
|
|
|
|
}
|
2017-07-28 13:09:39 +00:00
|
|
|
if i <= testTxPoolConfig.AccountQueue {
|
2016-07-01 15:59:55 +00:00
|
|
|
if pool.queue[account].Len() != int(i) {
|
|
|
|
t.Errorf("tx %d: queue size mismatch: have %d, want %d", i, pool.queue[account].Len(), i)
|
2015-12-30 16:31:37 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-07-28 13:09:39 +00:00
|
|
|
if pool.queue[account].Len() != int(testTxPoolConfig.AccountQueue) {
|
|
|
|
t.Errorf("tx %d: queue limit mismatch: have %d, want %d", i, pool.queue[account].Len(), testTxPoolConfig.AccountQueue)
|
2015-12-30 16:31:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-07-28 13:09:39 +00:00
|
|
|
if len(pool.all) != int(testTxPoolConfig.AccountQueue) {
|
|
|
|
t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), testTxPoolConfig.AccountQueue)
|
2016-08-17 13:53:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that if the transaction count belonging to multiple accounts go above
|
|
|
|
// some threshold, the higher transactions are dropped to prevent DOS attacks.
|
2017-07-06 08:51:59 +00:00
|
|
|
//
|
|
|
|
// This logic should not hold for local transactions, unless the local tracking
|
|
|
|
// mechanism is disabled.
|
2016-08-17 13:53:15 +00:00
|
|
|
func TestTransactionQueueGlobalLimiting(t *testing.T) {
|
2017-07-06 08:51:59 +00:00
|
|
|
testTransactionQueueGlobalLimiting(t, false)
|
|
|
|
}
|
|
|
|
func TestTransactionQueueGlobalLimitingNoLocals(t *testing.T) {
|
|
|
|
testTransactionQueueGlobalLimiting(t, true)
|
|
|
|
}
|
2016-08-17 13:53:15 +00:00
|
|
|
|
2017-07-06 08:51:59 +00:00
|
|
|
func testTransactionQueueGlobalLimiting(t *testing.T, nolocals bool) {
|
2016-08-17 13:53:15 +00:00
|
|
|
// Create the pool to test the limit enforcement with
|
|
|
|
db, _ := ethdb.NewMemDatabase()
|
2017-06-27 13:57:06 +00:00
|
|
|
statedb, _ := state.New(common.Hash{}, state.NewDatabase(db))
|
2016-08-17 13:53:15 +00:00
|
|
|
|
2017-07-28 13:09:39 +00:00
|
|
|
config := testTxPoolConfig
|
2017-07-06 08:51:59 +00:00
|
|
|
config.NoLocals = nolocals
|
|
|
|
config.GlobalQueue = config.AccountQueue*3 - 1 // reduce the queue limits to shorten test time (-1 to make it non divisible)
|
|
|
|
|
|
|
|
pool := NewTxPool(config, params.TestChainConfig, new(event.TypeMux), func() (*state.StateDB, error) { return statedb, nil }, func() *big.Int { return big.NewInt(1000000) })
|
|
|
|
defer pool.Stop()
|
2016-08-17 13:53:15 +00:00
|
|
|
|
2017-07-06 08:51:59 +00:00
|
|
|
// Create a number of test accounts and fund them (last one will be the local)
|
2016-08-17 13:53:15 +00:00
|
|
|
state, _ := pool.currentState()
|
|
|
|
|
|
|
|
keys := make([]*ecdsa.PrivateKey, 5)
|
|
|
|
for i := 0; i < len(keys); i++ {
|
|
|
|
keys[i], _ = crypto.GenerateKey()
|
|
|
|
state.AddBalance(crypto.PubkeyToAddress(keys[i].PublicKey), big.NewInt(1000000))
|
|
|
|
}
|
2017-07-06 08:51:59 +00:00
|
|
|
local := keys[len(keys)-1]
|
|
|
|
|
2016-08-17 13:53:15 +00:00
|
|
|
// Generate and queue a batch of transactions
|
|
|
|
nonces := make(map[common.Address]uint64)
|
|
|
|
|
2017-07-06 08:51:59 +00:00
|
|
|
txs := make(types.Transactions, 0, 3*config.GlobalQueue)
|
2016-08-17 13:53:15 +00:00
|
|
|
for len(txs) < cap(txs) {
|
2017-07-06 08:51:59 +00:00
|
|
|
key := keys[rand.Intn(len(keys)-1)] // skip adding transactions with the local account
|
2016-08-17 13:53:15 +00:00
|
|
|
addr := crypto.PubkeyToAddress(key.PublicKey)
|
|
|
|
|
|
|
|
txs = append(txs, transaction(nonces[addr]+1, big.NewInt(100000), key))
|
|
|
|
nonces[addr]++
|
|
|
|
}
|
|
|
|
// Import the batch and verify that limits have been enforced
|
2017-07-05 13:51:55 +00:00
|
|
|
pool.AddRemotes(txs)
|
2016-08-17 13:53:15 +00:00
|
|
|
|
|
|
|
queued := 0
|
|
|
|
for addr, list := range pool.queue {
|
2017-07-06 08:51:59 +00:00
|
|
|
if list.Len() > int(config.AccountQueue) {
|
|
|
|
t.Errorf("addr %x: queued accounts overflown allowance: %d > %d", addr, list.Len(), config.AccountQueue)
|
2016-08-17 13:53:15 +00:00
|
|
|
}
|
|
|
|
queued += list.Len()
|
|
|
|
}
|
2017-07-06 08:51:59 +00:00
|
|
|
if queued > int(config.GlobalQueue) {
|
|
|
|
t.Fatalf("total transactions overflow allowance: %d > %d", queued, config.GlobalQueue)
|
|
|
|
}
|
|
|
|
// Generate a batch of transactions from the local account and import them
|
|
|
|
txs = txs[:0]
|
|
|
|
for i := uint64(0); i < 3*config.GlobalQueue; i++ {
|
|
|
|
txs = append(txs, transaction(i+1, big.NewInt(100000), local))
|
|
|
|
}
|
|
|
|
pool.AddLocals(txs)
|
|
|
|
|
|
|
|
// If locals are disabled, the previous eviction algorithm should apply here too
|
|
|
|
if nolocals {
|
|
|
|
queued := 0
|
|
|
|
for addr, list := range pool.queue {
|
|
|
|
if list.Len() > int(config.AccountQueue) {
|
|
|
|
t.Errorf("addr %x: queued accounts overflown allowance: %d > %d", addr, list.Len(), config.AccountQueue)
|
|
|
|
}
|
|
|
|
queued += list.Len()
|
|
|
|
}
|
|
|
|
if queued > int(config.GlobalQueue) {
|
|
|
|
t.Fatalf("total transactions overflow allowance: %d > %d", queued, config.GlobalQueue)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Local exemptions are enabled, make sure the local account owned the queue
|
|
|
|
if len(pool.queue) != 1 {
|
|
|
|
t.Errorf("multiple accounts in queue: have %v, want %v", len(pool.queue), 1)
|
|
|
|
}
|
|
|
|
// Also ensure no local transactions are ever dropped, even if above global limits
|
|
|
|
if queued := pool.queue[crypto.PubkeyToAddress(local.PublicKey)].Len(); uint64(queued) != 3*config.GlobalQueue {
|
|
|
|
t.Fatalf("local account queued transaction count mismatch: have %v, want %v", queued, 3*config.GlobalQueue)
|
|
|
|
}
|
2016-08-17 13:53:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that if an account remains idle for a prolonged amount of time, any
|
|
|
|
// non-executable transactions queued up are dropped to prevent wasting resources
|
|
|
|
// on shuffling them around.
|
2017-07-06 08:51:59 +00:00
|
|
|
//
|
|
|
|
// This logic should not hold for local transactions, unless the local tracking
|
|
|
|
// mechanism is disabled.
|
|
|
|
func TestTransactionQueueTimeLimiting(t *testing.T) { testTransactionQueueTimeLimiting(t, false) }
|
|
|
|
func TestTransactionQueueTimeLimitingNoLocals(t *testing.T) { testTransactionQueueTimeLimiting(t, true) }
|
|
|
|
|
|
|
|
func testTransactionQueueTimeLimiting(t *testing.T, nolocals bool) {
|
|
|
|
// Reduce the eviction interval to a testable amount
|
2016-08-17 13:53:15 +00:00
|
|
|
defer func(old time.Duration) { evictionInterval = old }(evictionInterval)
|
2017-08-07 09:53:32 +00:00
|
|
|
evictionInterval = time.Second
|
2016-08-17 13:53:15 +00:00
|
|
|
|
2017-07-06 08:51:59 +00:00
|
|
|
// Create the pool to test the non-expiration enforcement
|
|
|
|
db, _ := ethdb.NewMemDatabase()
|
|
|
|
statedb, _ := state.New(common.Hash{}, state.NewDatabase(db))
|
|
|
|
|
2017-07-28 13:09:39 +00:00
|
|
|
config := testTxPoolConfig
|
2017-08-07 09:53:32 +00:00
|
|
|
config.Lifetime = time.Second
|
2017-07-06 08:51:59 +00:00
|
|
|
config.NoLocals = nolocals
|
|
|
|
|
|
|
|
pool := NewTxPool(config, params.TestChainConfig, new(event.TypeMux), func() (*state.StateDB, error) { return statedb, nil }, func() *big.Int { return big.NewInt(1000000) })
|
|
|
|
defer pool.Stop()
|
|
|
|
|
|
|
|
// Create two test accounts to ensure remotes expire but locals do not
|
|
|
|
local, _ := crypto.GenerateKey()
|
|
|
|
remote, _ := crypto.GenerateKey()
|
2016-08-17 13:53:15 +00:00
|
|
|
|
|
|
|
state, _ := pool.currentState()
|
2017-07-06 08:51:59 +00:00
|
|
|
state.AddBalance(crypto.PubkeyToAddress(local.PublicKey), big.NewInt(1000000000))
|
|
|
|
state.AddBalance(crypto.PubkeyToAddress(remote.PublicKey), big.NewInt(1000000000))
|
2016-08-17 13:53:15 +00:00
|
|
|
|
2017-07-06 08:51:59 +00:00
|
|
|
// Add the two transactions and ensure they both are queued up
|
|
|
|
if err := pool.AddLocal(pricedTransaction(1, big.NewInt(100000), big.NewInt(1), local)); err != nil {
|
|
|
|
t.Fatalf("failed to add local transaction: %v", err)
|
|
|
|
}
|
|
|
|
if err := pool.AddRemote(pricedTransaction(1, big.NewInt(100000), big.NewInt(1), remote)); err != nil {
|
|
|
|
t.Fatalf("failed to add remote transaction: %v", err)
|
|
|
|
}
|
2017-08-08 08:59:34 +00:00
|
|
|
pending, queued := pool.Stats()
|
2017-07-06 08:51:59 +00:00
|
|
|
if pending != 0 {
|
|
|
|
t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 0)
|
|
|
|
}
|
|
|
|
if queued != 2 {
|
|
|
|
t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 2)
|
|
|
|
}
|
|
|
|
if err := validateTxPoolInternals(pool); err != nil {
|
|
|
|
t.Fatalf("pool internal state corrupted: %v", err)
|
|
|
|
}
|
|
|
|
// Wait a bit for eviction to run and clean up any leftovers, and ensure only the local remains
|
|
|
|
time.Sleep(2 * config.Lifetime)
|
|
|
|
|
2017-08-08 08:59:34 +00:00
|
|
|
pending, queued = pool.Stats()
|
2017-07-06 08:51:59 +00:00
|
|
|
if pending != 0 {
|
|
|
|
t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 0)
|
|
|
|
}
|
|
|
|
if nolocals {
|
|
|
|
if queued != 0 {
|
|
|
|
t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 0)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if queued != 1 {
|
|
|
|
t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 1)
|
2016-08-17 13:53:15 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-06 08:51:59 +00:00
|
|
|
if err := validateTxPoolInternals(pool); err != nil {
|
|
|
|
t.Fatalf("pool internal state corrupted: %v", err)
|
2016-06-27 15:28:34 +00:00
|
|
|
}
|
2015-12-30 16:31:37 +00: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()
|
2017-07-06 08:51:59 +00:00
|
|
|
defer pool.Stop()
|
|
|
|
|
2016-11-02 12:44:13 +00:00
|
|
|
account, _ := deriveSender(transaction(0, big.NewInt(0), key))
|
2015-12-30 16:31:37 +00:00
|
|
|
|
|
|
|
state, _ := pool.currentState()
|
|
|
|
state.AddBalance(account, big.NewInt(1000000))
|
2017-08-08 08:59:34 +00:00
|
|
|
pool.lockedReset()
|
2015-12-30 16:31:37 +00:00
|
|
|
|
|
|
|
// Keep queuing up transactions and make sure all above a limit are dropped
|
2017-07-28 13:09:39 +00:00
|
|
|
for i := uint64(0); i < testTxPoolConfig.AccountQueue+5; i++ {
|
2017-07-05 13:51:55 +00:00
|
|
|
if err := pool.AddRemote(transaction(i, big.NewInt(100000), key)); err != nil {
|
2015-12-30 16:31:37 +00:00
|
|
|
t.Fatalf("tx %d: failed to add transaction: %v", i, err)
|
|
|
|
}
|
2016-07-01 15:59:55 +00:00
|
|
|
if pool.pending[account].Len() != int(i)+1 {
|
|
|
|
t.Errorf("tx %d: pending pool size mismatch: have %d, want %d", i, pool.pending[account].Len(), i+1)
|
2015-12-30 16:31:37 +00:00
|
|
|
}
|
2016-07-01 15:59:55 +00:00
|
|
|
if len(pool.queue) != 0 {
|
|
|
|
t.Errorf("tx %d: queue size mismatch: have %d, want %d", i, pool.queue[account].Len(), 0)
|
2015-12-30 16:31:37 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-28 13:09:39 +00:00
|
|
|
if len(pool.all) != int(testTxPoolConfig.AccountQueue+5) {
|
|
|
|
t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), testTxPoolConfig.AccountQueue+5)
|
2016-06-27 15:28:34 +00:00
|
|
|
}
|
2015-12-30 16:31:37 +00: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()
|
2017-08-07 12:47:25 +00:00
|
|
|
defer pool1.Stop()
|
|
|
|
|
2016-11-02 12:44:13 +00:00
|
|
|
account1, _ := deriveSender(transaction(0, big.NewInt(0), key1))
|
2015-12-30 16:31:37 +00:00
|
|
|
state1, _ := pool1.currentState()
|
|
|
|
state1.AddBalance(account1, big.NewInt(1000000))
|
|
|
|
|
2017-07-28 13:09:39 +00:00
|
|
|
for i := uint64(0); i < testTxPoolConfig.AccountQueue+5; i++ {
|
2017-07-05 13:51:55 +00:00
|
|
|
if err := pool1.AddRemote(transaction(origin+i, big.NewInt(100000), key1)); err != nil {
|
2015-12-30 16:31:37 +00:00
|
|
|
t.Fatalf("tx %d: failed to add transaction: %v", i, err)
|
|
|
|
}
|
|
|
|
}
|
2016-07-01 15:59:55 +00:00
|
|
|
// Add a batch of transactions to a pool in one big batch
|
2015-12-30 16:31:37 +00:00
|
|
|
pool2, key2 := setupTxPool()
|
2017-08-07 12:47:25 +00:00
|
|
|
defer pool2.Stop()
|
|
|
|
|
2016-11-02 12:44:13 +00:00
|
|
|
account2, _ := deriveSender(transaction(0, big.NewInt(0), key2))
|
2015-12-30 16:31:37 +00:00
|
|
|
state2, _ := pool2.currentState()
|
|
|
|
state2.AddBalance(account2, big.NewInt(1000000))
|
|
|
|
|
|
|
|
txns := []*types.Transaction{}
|
2017-07-28 13:09:39 +00:00
|
|
|
for i := uint64(0); i < testTxPoolConfig.AccountQueue+5; i++ {
|
2015-12-30 16:31:37 +00:00
|
|
|
txns = append(txns, transaction(origin+i, big.NewInt(100000), key2))
|
|
|
|
}
|
2017-07-05 13:51:55 +00:00
|
|
|
pool2.AddRemotes(txns)
|
2015-12-30 16:31:37 +00:00
|
|
|
|
|
|
|
// 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))
|
|
|
|
}
|
2016-07-01 15:59:55 +00:00
|
|
|
if len(pool1.queue) != len(pool2.queue) {
|
|
|
|
t.Errorf("queued transaction count mismatch: one-by-one algo: %d, batch algo: %d", len(pool1.queue), len(pool2.queue))
|
2015-12-30 16:31:37 +00:00
|
|
|
}
|
2016-06-27 15:28:34 +00: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))
|
|
|
|
}
|
2017-06-22 14:01:49 +00:00
|
|
|
if err := validateTxPoolInternals(pool1); err != nil {
|
|
|
|
t.Errorf("pool 1 internal state corrupted: %v", err)
|
|
|
|
}
|
|
|
|
if err := validateTxPoolInternals(pool2); err != nil {
|
|
|
|
t.Errorf("pool 2 internal state corrupted: %v", err)
|
|
|
|
}
|
2015-12-30 16:31:37 +00:00
|
|
|
}
|
|
|
|
|
2016-10-14 12:32:06 +00:00
|
|
|
// Tests that if the transaction count belonging to multiple accounts go above
|
|
|
|
// some hard threshold, the higher transactions are dropped to prevent DOS
|
|
|
|
// attacks.
|
|
|
|
func TestTransactionPendingGlobalLimiting(t *testing.T) {
|
|
|
|
// Create the pool to test the limit enforcement with
|
|
|
|
db, _ := ethdb.NewMemDatabase()
|
2017-06-27 13:57:06 +00:00
|
|
|
statedb, _ := state.New(common.Hash{}, state.NewDatabase(db))
|
2016-10-14 12:32:06 +00:00
|
|
|
|
2017-07-28 13:09:39 +00:00
|
|
|
config := testTxPoolConfig
|
2017-07-06 08:51:59 +00:00
|
|
|
config.GlobalSlots = config.AccountSlots * 10
|
|
|
|
|
|
|
|
pool := NewTxPool(config, params.TestChainConfig, new(event.TypeMux), func() (*state.StateDB, error) { return statedb, nil }, func() *big.Int { return big.NewInt(1000000) })
|
|
|
|
defer pool.Stop()
|
2016-10-14 12:32:06 +00:00
|
|
|
|
|
|
|
// Create a number of test accounts and fund them
|
|
|
|
state, _ := pool.currentState()
|
|
|
|
|
|
|
|
keys := make([]*ecdsa.PrivateKey, 5)
|
|
|
|
for i := 0; i < len(keys); i++ {
|
|
|
|
keys[i], _ = crypto.GenerateKey()
|
|
|
|
state.AddBalance(crypto.PubkeyToAddress(keys[i].PublicKey), big.NewInt(1000000))
|
|
|
|
}
|
|
|
|
// Generate and queue a batch of transactions
|
|
|
|
nonces := make(map[common.Address]uint64)
|
|
|
|
|
|
|
|
txs := types.Transactions{}
|
|
|
|
for _, key := range keys {
|
|
|
|
addr := crypto.PubkeyToAddress(key.PublicKey)
|
2017-07-06 08:51:59 +00:00
|
|
|
for j := 0; j < int(config.GlobalSlots)/len(keys)*2; j++ {
|
2016-10-14 12:32:06 +00:00
|
|
|
txs = append(txs, transaction(nonces[addr], big.NewInt(100000), key))
|
|
|
|
nonces[addr]++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Import the batch and verify that limits have been enforced
|
2017-07-05 13:51:55 +00:00
|
|
|
pool.AddRemotes(txs)
|
2016-10-14 12:32:06 +00:00
|
|
|
|
|
|
|
pending := 0
|
|
|
|
for _, list := range pool.pending {
|
|
|
|
pending += list.Len()
|
|
|
|
}
|
2017-07-06 08:51:59 +00:00
|
|
|
if pending > int(config.GlobalSlots) {
|
|
|
|
t.Fatalf("total pending transactions overflow allowance: %d > %d", pending, config.GlobalSlots)
|
2016-10-14 12:32:06 +00:00
|
|
|
}
|
2017-06-22 14:01:49 +00:00
|
|
|
if err := validateTxPoolInternals(pool); err != nil {
|
|
|
|
t.Fatalf("pool internal state corrupted: %v", err)
|
|
|
|
}
|
2016-10-14 12:32:06 +00:00
|
|
|
}
|
|
|
|
|
2017-06-22 08:14:31 +00:00
|
|
|
// Tests that if transactions start being capped, transasctions are also removed from 'all'
|
|
|
|
func TestTransactionCapClearsFromAll(t *testing.T) {
|
|
|
|
// Create the pool to test the limit enforcement with
|
|
|
|
db, _ := ethdb.NewMemDatabase()
|
2017-06-27 13:57:06 +00:00
|
|
|
statedb, _ := state.New(common.Hash{}, state.NewDatabase(db))
|
2017-06-22 08:14:31 +00:00
|
|
|
|
2017-07-28 13:09:39 +00:00
|
|
|
config := testTxPoolConfig
|
2017-07-06 08:51:59 +00:00
|
|
|
config.AccountSlots = 2
|
|
|
|
config.AccountQueue = 2
|
|
|
|
config.GlobalSlots = 8
|
|
|
|
|
|
|
|
pool := NewTxPool(config, params.TestChainConfig, new(event.TypeMux), func() (*state.StateDB, error) { return statedb, nil }, func() *big.Int { return big.NewInt(1000000) })
|
|
|
|
defer pool.Stop()
|
2017-06-22 08:14:31 +00:00
|
|
|
|
|
|
|
// Create a number of test accounts and fund them
|
|
|
|
state, _ := pool.currentState()
|
|
|
|
|
|
|
|
key, _ := crypto.GenerateKey()
|
|
|
|
addr := crypto.PubkeyToAddress(key.PublicKey)
|
|
|
|
state.AddBalance(addr, big.NewInt(1000000))
|
|
|
|
|
|
|
|
txs := types.Transactions{}
|
2017-07-06 08:51:59 +00:00
|
|
|
for j := 0; j < int(config.GlobalSlots)*2; j++ {
|
2017-06-22 14:01:49 +00:00
|
|
|
txs = append(txs, transaction(uint64(j), big.NewInt(100000), key))
|
2017-06-22 08:14:31 +00:00
|
|
|
}
|
|
|
|
// Import the batch and verify that limits have been enforced
|
2017-07-05 13:51:55 +00:00
|
|
|
pool.AddRemotes(txs)
|
2017-06-22 14:01:49 +00:00
|
|
|
if err := validateTxPoolInternals(pool); err != nil {
|
|
|
|
t.Fatalf("pool internal state corrupted: %v", err)
|
2017-06-22 08:14:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-14 12:32:06 +00:00
|
|
|
// Tests that if the transaction count belonging to multiple accounts go above
|
|
|
|
// some hard threshold, if they are under the minimum guaranteed slot count then
|
|
|
|
// the transactions are still kept.
|
|
|
|
func TestTransactionPendingMinimumAllowance(t *testing.T) {
|
|
|
|
// Create the pool to test the limit enforcement with
|
|
|
|
db, _ := ethdb.NewMemDatabase()
|
2017-06-27 13:57:06 +00:00
|
|
|
statedb, _ := state.New(common.Hash{}, state.NewDatabase(db))
|
2016-10-14 12:32:06 +00:00
|
|
|
|
2017-07-28 13:09:39 +00:00
|
|
|
config := testTxPoolConfig
|
2017-07-06 08:51:59 +00:00
|
|
|
config.GlobalSlots = 0
|
|
|
|
|
|
|
|
pool := NewTxPool(config, params.TestChainConfig, new(event.TypeMux), func() (*state.StateDB, error) { return statedb, nil }, func() *big.Int { return big.NewInt(1000000) })
|
|
|
|
defer pool.Stop()
|
2016-10-14 12:32:06 +00:00
|
|
|
|
|
|
|
// Create a number of test accounts and fund them
|
|
|
|
state, _ := pool.currentState()
|
|
|
|
|
|
|
|
keys := make([]*ecdsa.PrivateKey, 5)
|
|
|
|
for i := 0; i < len(keys); i++ {
|
|
|
|
keys[i], _ = crypto.GenerateKey()
|
|
|
|
state.AddBalance(crypto.PubkeyToAddress(keys[i].PublicKey), big.NewInt(1000000))
|
|
|
|
}
|
|
|
|
// Generate and queue a batch of transactions
|
|
|
|
nonces := make(map[common.Address]uint64)
|
|
|
|
|
|
|
|
txs := types.Transactions{}
|
|
|
|
for _, key := range keys {
|
|
|
|
addr := crypto.PubkeyToAddress(key.PublicKey)
|
2017-07-06 08:51:59 +00:00
|
|
|
for j := 0; j < int(config.AccountSlots)*2; j++ {
|
2016-10-14 12:32:06 +00:00
|
|
|
txs = append(txs, transaction(nonces[addr], big.NewInt(100000), key))
|
|
|
|
nonces[addr]++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Import the batch and verify that limits have been enforced
|
2017-07-05 13:51:55 +00:00
|
|
|
pool.AddRemotes(txs)
|
2016-10-14 12:32:06 +00:00
|
|
|
|
|
|
|
for addr, list := range pool.pending {
|
2017-07-06 08:51:59 +00:00
|
|
|
if list.Len() != int(config.AccountSlots) {
|
|
|
|
t.Errorf("addr %x: total pending transactions mismatch: have %d, want %d", addr, list.Len(), config.AccountSlots)
|
2016-10-14 12:32:06 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-22 14:01:49 +00:00
|
|
|
if err := validateTxPoolInternals(pool); err != nil {
|
|
|
|
t.Fatalf("pool internal state corrupted: %v", err)
|
|
|
|
}
|
2016-10-14 12:32:06 +00:00
|
|
|
}
|
|
|
|
|
2017-05-16 19:07:27 +00:00
|
|
|
// Tests that setting the transaction pool gas price to a higher value correctly
|
|
|
|
// discards everything cheaper than that and moves any gapped transactions back
|
|
|
|
// from the pending pool to the queue.
|
|
|
|
//
|
|
|
|
// Note, local transactions are never allowed to be dropped.
|
|
|
|
func TestTransactionPoolRepricing(t *testing.T) {
|
|
|
|
// Create the pool to test the pricing enforcement with
|
|
|
|
db, _ := ethdb.NewMemDatabase()
|
2017-06-27 13:57:06 +00:00
|
|
|
statedb, _ := state.New(common.Hash{}, state.NewDatabase(db))
|
2017-05-16 19:07:27 +00:00
|
|
|
|
2017-07-28 13:09:39 +00:00
|
|
|
pool := NewTxPool(testTxPoolConfig, params.TestChainConfig, new(event.TypeMux), func() (*state.StateDB, error) { return statedb, nil }, func() *big.Int { return big.NewInt(1000000) })
|
2017-07-06 08:51:59 +00:00
|
|
|
defer pool.Stop()
|
2017-05-16 19:07:27 +00:00
|
|
|
|
|
|
|
// Create a number of test accounts and fund them
|
|
|
|
state, _ := pool.currentState()
|
|
|
|
|
|
|
|
keys := make([]*ecdsa.PrivateKey, 3)
|
|
|
|
for i := 0; i < len(keys); i++ {
|
|
|
|
keys[i], _ = crypto.GenerateKey()
|
|
|
|
state.AddBalance(crypto.PubkeyToAddress(keys[i].PublicKey), big.NewInt(1000000))
|
|
|
|
}
|
|
|
|
// Generate and queue a batch of transactions, both pending and queued
|
|
|
|
txs := types.Transactions{}
|
|
|
|
|
|
|
|
txs = append(txs, pricedTransaction(0, big.NewInt(100000), big.NewInt(2), keys[0]))
|
|
|
|
txs = append(txs, pricedTransaction(1, big.NewInt(100000), big.NewInt(1), keys[0]))
|
|
|
|
txs = append(txs, pricedTransaction(2, big.NewInt(100000), big.NewInt(2), keys[0]))
|
|
|
|
|
|
|
|
txs = append(txs, pricedTransaction(1, big.NewInt(100000), big.NewInt(2), keys[1]))
|
|
|
|
txs = append(txs, pricedTransaction(2, big.NewInt(100000), big.NewInt(1), keys[1]))
|
|
|
|
txs = append(txs, pricedTransaction(3, big.NewInt(100000), big.NewInt(2), keys[1]))
|
|
|
|
|
2017-07-05 13:51:55 +00:00
|
|
|
ltx := pricedTransaction(0, big.NewInt(100000), big.NewInt(1), keys[2])
|
2017-05-16 19:07:27 +00:00
|
|
|
|
|
|
|
// Import the batch and that both pending and queued transactions match up
|
2017-07-05 13:51:55 +00:00
|
|
|
pool.AddRemotes(txs)
|
|
|
|
pool.AddLocal(ltx)
|
2017-05-16 19:07:27 +00:00
|
|
|
|
2017-08-08 08:59:34 +00:00
|
|
|
pending, queued := pool.Stats()
|
2017-05-16 19:07:27 +00:00
|
|
|
if pending != 4 {
|
|
|
|
t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 4)
|
|
|
|
}
|
|
|
|
if queued != 3 {
|
|
|
|
t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 3)
|
|
|
|
}
|
2017-06-22 14:01:49 +00:00
|
|
|
if err := validateTxPoolInternals(pool); err != nil {
|
|
|
|
t.Fatalf("pool internal state corrupted: %v", err)
|
|
|
|
}
|
2017-05-16 19:07:27 +00:00
|
|
|
// Reprice the pool and check that underpriced transactions get dropped
|
|
|
|
pool.SetGasPrice(big.NewInt(2))
|
|
|
|
|
2017-08-08 08:59:34 +00:00
|
|
|
pending, queued = pool.Stats()
|
2017-05-16 19:07:27 +00:00
|
|
|
if pending != 2 {
|
|
|
|
t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 2)
|
|
|
|
}
|
|
|
|
if queued != 3 {
|
|
|
|
t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 3)
|
|
|
|
}
|
2017-06-22 14:01:49 +00:00
|
|
|
if err := validateTxPoolInternals(pool); err != nil {
|
|
|
|
t.Fatalf("pool internal state corrupted: %v", err)
|
|
|
|
}
|
2017-05-16 19:07:27 +00:00
|
|
|
// Check that we can't add the old transactions back
|
2017-07-05 13:51:55 +00:00
|
|
|
if err := pool.AddRemote(pricedTransaction(1, big.NewInt(100000), big.NewInt(1), keys[0])); err != ErrUnderpriced {
|
2017-05-16 19:07:27 +00:00
|
|
|
t.Fatalf("adding underpriced pending transaction error mismatch: have %v, want %v", err, ErrUnderpriced)
|
|
|
|
}
|
2017-07-05 13:51:55 +00:00
|
|
|
if err := pool.AddRemote(pricedTransaction(2, big.NewInt(100000), big.NewInt(1), keys[1])); err != ErrUnderpriced {
|
2017-05-16 19:07:27 +00:00
|
|
|
t.Fatalf("adding underpriced queued transaction error mismatch: have %v, want %v", err, ErrUnderpriced)
|
|
|
|
}
|
2017-06-22 14:01:49 +00:00
|
|
|
if err := validateTxPoolInternals(pool); err != nil {
|
|
|
|
t.Fatalf("pool internal state corrupted: %v", err)
|
|
|
|
}
|
2017-05-16 19:07:27 +00:00
|
|
|
// However we can add local underpriced transactions
|
|
|
|
tx := pricedTransaction(1, big.NewInt(100000), big.NewInt(1), keys[2])
|
2017-07-05 13:51:55 +00:00
|
|
|
if err := pool.AddLocal(tx); err != nil {
|
2017-05-16 19:07:27 +00:00
|
|
|
t.Fatalf("failed to add underpriced local transaction: %v", err)
|
|
|
|
}
|
2017-08-08 08:59:34 +00:00
|
|
|
if pending, _ = pool.Stats(); pending != 3 {
|
2017-05-16 19:07:27 +00:00
|
|
|
t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 3)
|
|
|
|
}
|
2017-06-22 14:01:49 +00:00
|
|
|
if err := validateTxPoolInternals(pool); err != nil {
|
|
|
|
t.Fatalf("pool internal state corrupted: %v", err)
|
2017-06-22 08:14:31 +00:00
|
|
|
}
|
2017-05-16 19:07:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that when the pool reaches its global transaction limit, underpriced
|
|
|
|
// transactions are gradually shifted out for more expensive ones and any gapped
|
|
|
|
// pending transactions are moved into te queue.
|
|
|
|
//
|
|
|
|
// Note, local transactions are never allowed to be dropped.
|
|
|
|
func TestTransactionPoolUnderpricing(t *testing.T) {
|
|
|
|
// Create the pool to test the pricing enforcement with
|
|
|
|
db, _ := ethdb.NewMemDatabase()
|
2017-06-27 13:57:06 +00:00
|
|
|
statedb, _ := state.New(common.Hash{}, state.NewDatabase(db))
|
2017-05-16 19:07:27 +00:00
|
|
|
|
2017-07-28 13:09:39 +00:00
|
|
|
config := testTxPoolConfig
|
2017-07-06 08:51:59 +00:00
|
|
|
config.GlobalSlots = 2
|
|
|
|
config.GlobalQueue = 2
|
|
|
|
|
|
|
|
pool := NewTxPool(config, params.TestChainConfig, new(event.TypeMux), func() (*state.StateDB, error) { return statedb, nil }, func() *big.Int { return big.NewInt(1000000) })
|
|
|
|
defer pool.Stop()
|
2017-05-16 19:07:27 +00:00
|
|
|
|
|
|
|
// Create a number of test accounts and fund them
|
|
|
|
state, _ := pool.currentState()
|
|
|
|
|
|
|
|
keys := make([]*ecdsa.PrivateKey, 3)
|
|
|
|
for i := 0; i < len(keys); i++ {
|
|
|
|
keys[i], _ = crypto.GenerateKey()
|
|
|
|
state.AddBalance(crypto.PubkeyToAddress(keys[i].PublicKey), big.NewInt(1000000))
|
|
|
|
}
|
|
|
|
// Generate and queue a batch of transactions, both pending and queued
|
|
|
|
txs := types.Transactions{}
|
|
|
|
|
|
|
|
txs = append(txs, pricedTransaction(0, big.NewInt(100000), big.NewInt(1), keys[0]))
|
|
|
|
txs = append(txs, pricedTransaction(1, big.NewInt(100000), big.NewInt(2), keys[0]))
|
|
|
|
|
|
|
|
txs = append(txs, pricedTransaction(1, big.NewInt(100000), big.NewInt(1), keys[1]))
|
|
|
|
|
2017-07-05 13:51:55 +00:00
|
|
|
ltx := pricedTransaction(0, big.NewInt(100000), big.NewInt(1), keys[2])
|
2017-05-16 19:07:27 +00:00
|
|
|
|
|
|
|
// Import the batch and that both pending and queued transactions match up
|
2017-07-05 13:51:55 +00:00
|
|
|
pool.AddRemotes(txs)
|
|
|
|
pool.AddLocal(ltx)
|
2017-05-16 19:07:27 +00:00
|
|
|
|
2017-08-08 08:59:34 +00:00
|
|
|
pending, queued := pool.Stats()
|
2017-05-16 19:07:27 +00:00
|
|
|
if pending != 3 {
|
|
|
|
t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 3)
|
|
|
|
}
|
|
|
|
if queued != 1 {
|
|
|
|
t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 1)
|
|
|
|
}
|
2017-06-22 14:01:49 +00:00
|
|
|
if err := validateTxPoolInternals(pool); err != nil {
|
|
|
|
t.Fatalf("pool internal state corrupted: %v", err)
|
|
|
|
}
|
2017-05-16 19:07:27 +00:00
|
|
|
// Ensure that adding an underpriced transaction on block limit fails
|
2017-07-05 13:51:55 +00:00
|
|
|
if err := pool.AddRemote(pricedTransaction(0, big.NewInt(100000), big.NewInt(1), keys[1])); err != ErrUnderpriced {
|
2017-05-16 19:07:27 +00:00
|
|
|
t.Fatalf("adding underpriced pending transaction error mismatch: have %v, want %v", err, ErrUnderpriced)
|
|
|
|
}
|
|
|
|
// Ensure that adding high priced transactions drops cheap ones, but not own
|
2017-07-05 13:51:55 +00:00
|
|
|
if err := pool.AddRemote(pricedTransaction(0, big.NewInt(100000), big.NewInt(3), keys[1])); err != nil {
|
2017-05-16 19:07:27 +00:00
|
|
|
t.Fatalf("failed to add well priced transaction: %v", err)
|
|
|
|
}
|
2017-07-05 13:51:55 +00:00
|
|
|
if err := pool.AddRemote(pricedTransaction(2, big.NewInt(100000), big.NewInt(4), keys[1])); err != nil {
|
2017-05-16 19:07:27 +00:00
|
|
|
t.Fatalf("failed to add well priced transaction: %v", err)
|
|
|
|
}
|
2017-07-05 13:51:55 +00:00
|
|
|
if err := pool.AddRemote(pricedTransaction(3, big.NewInt(100000), big.NewInt(5), keys[1])); err != nil {
|
2017-05-16 19:07:27 +00:00
|
|
|
t.Fatalf("failed to add well priced transaction: %v", err)
|
|
|
|
}
|
2017-08-08 08:59:34 +00:00
|
|
|
pending, queued = pool.Stats()
|
2017-05-16 19:07:27 +00:00
|
|
|
if pending != 2 {
|
|
|
|
t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 2)
|
|
|
|
}
|
|
|
|
if queued != 2 {
|
|
|
|
t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 2)
|
|
|
|
}
|
2017-06-22 14:01:49 +00:00
|
|
|
if err := validateTxPoolInternals(pool); err != nil {
|
|
|
|
t.Fatalf("pool internal state corrupted: %v", err)
|
|
|
|
}
|
2017-05-16 19:07:27 +00:00
|
|
|
// Ensure that adding local transactions can push out even higher priced ones
|
|
|
|
tx := pricedTransaction(1, big.NewInt(100000), big.NewInt(0), keys[2])
|
2017-07-05 13:51:55 +00:00
|
|
|
if err := pool.AddLocal(tx); err != nil {
|
2017-05-16 19:07:27 +00:00
|
|
|
t.Fatalf("failed to add underpriced local transaction: %v", err)
|
|
|
|
}
|
2017-08-08 08:59:34 +00:00
|
|
|
pending, queued = pool.Stats()
|
2017-05-16 19:07:27 +00:00
|
|
|
if pending != 2 {
|
|
|
|
t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 2)
|
|
|
|
}
|
|
|
|
if queued != 2 {
|
|
|
|
t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 2)
|
|
|
|
}
|
2017-06-22 14:01:49 +00:00
|
|
|
if err := validateTxPoolInternals(pool); err != nil {
|
|
|
|
t.Fatalf("pool internal state corrupted: %v", err)
|
2017-06-22 08:14:31 +00:00
|
|
|
}
|
2017-05-16 19:07:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that the pool rejects replacement transactions that don't meet the minimum
|
|
|
|
// price bump required.
|
|
|
|
func TestTransactionReplacement(t *testing.T) {
|
|
|
|
// Create the pool to test the pricing enforcement with
|
|
|
|
db, _ := ethdb.NewMemDatabase()
|
2017-06-27 13:57:06 +00:00
|
|
|
statedb, _ := state.New(common.Hash{}, state.NewDatabase(db))
|
2017-05-16 19:07:27 +00:00
|
|
|
|
2017-07-28 13:09:39 +00:00
|
|
|
pool := NewTxPool(testTxPoolConfig, params.TestChainConfig, new(event.TypeMux), func() (*state.StateDB, error) { return statedb, nil }, func() *big.Int { return big.NewInt(1000000) })
|
2017-07-06 08:51:59 +00:00
|
|
|
defer pool.Stop()
|
2017-05-16 19:07:27 +00:00
|
|
|
|
2017-07-06 08:51:59 +00:00
|
|
|
// Create a test account to add transactions with
|
2017-05-16 19:07:27 +00:00
|
|
|
key, _ := crypto.GenerateKey()
|
|
|
|
|
|
|
|
state, _ := pool.currentState()
|
|
|
|
state.AddBalance(crypto.PubkeyToAddress(key.PublicKey), big.NewInt(1000000000))
|
|
|
|
|
|
|
|
// Add pending transactions, ensuring the minimum price bump is enforced for replacement (for ultra low prices too)
|
|
|
|
price := int64(100)
|
2017-07-28 13:09:39 +00:00
|
|
|
threshold := (price * (100 + int64(testTxPoolConfig.PriceBump))) / 100
|
2017-05-16 19:07:27 +00:00
|
|
|
|
2017-07-05 13:51:55 +00:00
|
|
|
if err := pool.AddRemote(pricedTransaction(0, big.NewInt(100000), big.NewInt(1), key)); err != nil {
|
2017-05-16 19:07:27 +00:00
|
|
|
t.Fatalf("failed to add original cheap pending transaction: %v", err)
|
|
|
|
}
|
2017-07-05 13:51:55 +00:00
|
|
|
if err := pool.AddRemote(pricedTransaction(0, big.NewInt(100001), big.NewInt(1), key)); err != ErrReplaceUnderpriced {
|
2017-05-16 19:07:27 +00:00
|
|
|
t.Fatalf("original cheap pending transaction replacement error mismatch: have %v, want %v", err, ErrReplaceUnderpriced)
|
|
|
|
}
|
2017-07-05 13:51:55 +00:00
|
|
|
if err := pool.AddRemote(pricedTransaction(0, big.NewInt(100000), big.NewInt(2), key)); err != nil {
|
2017-05-16 19:07:27 +00:00
|
|
|
t.Fatalf("failed to replace original cheap pending transaction: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-07-05 13:51:55 +00:00
|
|
|
if err := pool.AddRemote(pricedTransaction(0, big.NewInt(100000), big.NewInt(price), key)); err != nil {
|
2017-05-16 19:07:27 +00:00
|
|
|
t.Fatalf("failed to add original proper pending transaction: %v", err)
|
|
|
|
}
|
2017-07-05 13:51:55 +00:00
|
|
|
if err := pool.AddRemote(pricedTransaction(0, big.NewInt(100000), big.NewInt(threshold), key)); err != ErrReplaceUnderpriced {
|
2017-05-16 19:07:27 +00:00
|
|
|
t.Fatalf("original proper pending transaction replacement error mismatch: have %v, want %v", err, ErrReplaceUnderpriced)
|
|
|
|
}
|
2017-07-05 13:51:55 +00:00
|
|
|
if err := pool.AddRemote(pricedTransaction(0, big.NewInt(100000), big.NewInt(threshold+1), key)); err != nil {
|
2017-05-16 19:07:27 +00:00
|
|
|
t.Fatalf("failed to replace original proper pending transaction: %v", err)
|
|
|
|
}
|
|
|
|
// Add queued transactions, ensuring the minimum price bump is enforced for replacement (for ultra low prices too)
|
2017-07-05 13:51:55 +00:00
|
|
|
if err := pool.AddRemote(pricedTransaction(2, big.NewInt(100000), big.NewInt(1), key)); err != nil {
|
2017-05-16 19:07:27 +00:00
|
|
|
t.Fatalf("failed to add original queued transaction: %v", err)
|
|
|
|
}
|
2017-07-05 13:51:55 +00:00
|
|
|
if err := pool.AddRemote(pricedTransaction(2, big.NewInt(100001), big.NewInt(1), key)); err != ErrReplaceUnderpriced {
|
2017-05-16 19:07:27 +00:00
|
|
|
t.Fatalf("original queued transaction replacement error mismatch: have %v, want %v", err, ErrReplaceUnderpriced)
|
|
|
|
}
|
2017-07-05 13:51:55 +00:00
|
|
|
if err := pool.AddRemote(pricedTransaction(2, big.NewInt(100000), big.NewInt(2), key)); err != nil {
|
2017-05-16 19:07:27 +00:00
|
|
|
t.Fatalf("failed to replace original queued transaction: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-07-05 13:51:55 +00:00
|
|
|
if err := pool.AddRemote(pricedTransaction(2, big.NewInt(100000), big.NewInt(price), key)); err != nil {
|
2017-05-16 19:07:27 +00:00
|
|
|
t.Fatalf("failed to add original queued transaction: %v", err)
|
|
|
|
}
|
2017-07-05 13:51:55 +00:00
|
|
|
if err := pool.AddRemote(pricedTransaction(2, big.NewInt(100001), big.NewInt(threshold), key)); err != ErrReplaceUnderpriced {
|
2017-05-16 19:07:27 +00:00
|
|
|
t.Fatalf("original queued transaction replacement error mismatch: have %v, want %v", err, ErrReplaceUnderpriced)
|
|
|
|
}
|
2017-07-05 13:51:55 +00:00
|
|
|
if err := pool.AddRemote(pricedTransaction(2, big.NewInt(100000), big.NewInt(threshold+1), key)); err != nil {
|
2017-05-16 19:07:27 +00:00
|
|
|
t.Fatalf("failed to replace original queued transaction: %v", err)
|
|
|
|
}
|
2017-06-22 14:01:49 +00:00
|
|
|
if err := validateTxPoolInternals(pool); err != nil {
|
|
|
|
t.Fatalf("pool internal state corrupted: %v", err)
|
2017-06-22 08:14:31 +00:00
|
|
|
}
|
2017-05-16 19:07:27 +00:00
|
|
|
}
|
|
|
|
|
2017-07-28 13:09:39 +00:00
|
|
|
// Tests that local transactions are journaled to disk, but remote transactions
|
|
|
|
// get discarded between restarts.
|
|
|
|
func TestTransactionJournaling(t *testing.T) { testTransactionJournaling(t, false) }
|
|
|
|
func TestTransactionJournalingNoLocals(t *testing.T) { testTransactionJournaling(t, true) }
|
|
|
|
|
|
|
|
func testTransactionJournaling(t *testing.T, nolocals bool) {
|
|
|
|
// Create a temporary file for the journal
|
|
|
|
file, err := ioutil.TempFile("", "")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to create temporary journal: %v", err)
|
|
|
|
}
|
|
|
|
journal := file.Name()
|
|
|
|
defer os.Remove(journal)
|
|
|
|
|
|
|
|
// Clean up the temporary file, we only need the path for now
|
|
|
|
file.Close()
|
|
|
|
os.Remove(journal)
|
|
|
|
|
|
|
|
// Create the original pool to inject transaction into the journal
|
|
|
|
db, _ := ethdb.NewMemDatabase()
|
|
|
|
statedb, _ := state.New(common.Hash{}, state.NewDatabase(db))
|
|
|
|
|
|
|
|
config := testTxPoolConfig
|
|
|
|
config.NoLocals = nolocals
|
|
|
|
config.Journal = journal
|
|
|
|
config.Rejournal = time.Second
|
|
|
|
|
|
|
|
pool := NewTxPool(config, params.TestChainConfig, new(event.TypeMux), func() (*state.StateDB, error) { return statedb, nil }, func() *big.Int { return big.NewInt(1000000) })
|
|
|
|
|
|
|
|
// Create two test accounts to ensure remotes expire but locals do not
|
|
|
|
local, _ := crypto.GenerateKey()
|
|
|
|
remote, _ := crypto.GenerateKey()
|
|
|
|
|
|
|
|
statedb, _ = pool.currentState()
|
|
|
|
statedb.AddBalance(crypto.PubkeyToAddress(local.PublicKey), big.NewInt(1000000000))
|
|
|
|
statedb.AddBalance(crypto.PubkeyToAddress(remote.PublicKey), big.NewInt(1000000000))
|
|
|
|
|
|
|
|
// Add three local and a remote transactions and ensure they are queued up
|
|
|
|
if err := pool.AddLocal(pricedTransaction(0, big.NewInt(100000), big.NewInt(1), local)); err != nil {
|
|
|
|
t.Fatalf("failed to add local transaction: %v", err)
|
|
|
|
}
|
|
|
|
if err := pool.AddLocal(pricedTransaction(1, big.NewInt(100000), big.NewInt(1), local)); err != nil {
|
|
|
|
t.Fatalf("failed to add local transaction: %v", err)
|
|
|
|
}
|
|
|
|
if err := pool.AddLocal(pricedTransaction(2, big.NewInt(100000), big.NewInt(1), local)); err != nil {
|
|
|
|
t.Fatalf("failed to add local transaction: %v", err)
|
|
|
|
}
|
|
|
|
if err := pool.AddRemote(pricedTransaction(0, big.NewInt(100000), big.NewInt(1), remote)); err != nil {
|
|
|
|
t.Fatalf("failed to add remote transaction: %v", err)
|
|
|
|
}
|
2017-08-08 08:59:34 +00:00
|
|
|
pending, queued := pool.Stats()
|
2017-07-28 13:09:39 +00:00
|
|
|
if pending != 4 {
|
|
|
|
t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 4)
|
|
|
|
}
|
|
|
|
if queued != 0 {
|
|
|
|
t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 0)
|
|
|
|
}
|
|
|
|
if err := validateTxPoolInternals(pool); err != nil {
|
|
|
|
t.Fatalf("pool internal state corrupted: %v", err)
|
|
|
|
}
|
|
|
|
// Terminate the old pool, bump the local nonce, create a new pool and ensure relevant transaction survive
|
|
|
|
pool.Stop()
|
|
|
|
statedb.SetNonce(crypto.PubkeyToAddress(local.PublicKey), 1)
|
|
|
|
pool = NewTxPool(config, params.TestChainConfig, new(event.TypeMux), func() (*state.StateDB, error) { return statedb, nil }, func() *big.Int { return big.NewInt(1000000) })
|
|
|
|
|
2017-08-08 08:59:34 +00:00
|
|
|
pending, queued = pool.Stats()
|
2017-07-28 13:09:39 +00:00
|
|
|
if queued != 0 {
|
|
|
|
t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 0)
|
|
|
|
}
|
|
|
|
if nolocals {
|
|
|
|
if pending != 0 {
|
|
|
|
t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 0)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if pending != 2 {
|
|
|
|
t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := validateTxPoolInternals(pool); err != nil {
|
|
|
|
t.Fatalf("pool internal state corrupted: %v", err)
|
|
|
|
}
|
|
|
|
// Bump the nonce temporarily and ensure the newly invalidated transaction is removed
|
|
|
|
statedb.SetNonce(crypto.PubkeyToAddress(local.PublicKey), 2)
|
2017-08-08 08:59:34 +00:00
|
|
|
pool.lockedReset()
|
2017-07-28 13:09:39 +00:00
|
|
|
time.Sleep(2 * config.Rejournal)
|
|
|
|
pool.Stop()
|
|
|
|
statedb.SetNonce(crypto.PubkeyToAddress(local.PublicKey), 1)
|
|
|
|
pool = NewTxPool(config, params.TestChainConfig, new(event.TypeMux), func() (*state.StateDB, error) { return statedb, nil }, func() *big.Int { return big.NewInt(1000000) })
|
|
|
|
|
2017-08-08 08:59:34 +00:00
|
|
|
pending, queued = pool.Stats()
|
2017-07-28 13:09:39 +00:00
|
|
|
if pending != 0 {
|
|
|
|
t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 0)
|
|
|
|
}
|
|
|
|
if nolocals {
|
|
|
|
if queued != 0 {
|
|
|
|
t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 0)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if queued != 1 {
|
|
|
|
t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := validateTxPoolInternals(pool); err != nil {
|
|
|
|
t.Fatalf("pool internal state corrupted: %v", err)
|
|
|
|
}
|
2017-08-07 12:47:25 +00:00
|
|
|
pool.Stop()
|
2017-07-28 13:09:39 +00:00
|
|
|
}
|
|
|
|
|
2015-12-30 16:31:37 +00:00
|
|
|
// Benchmarks the speed of validating the contents of the pending queue of the
|
|
|
|
// transaction pool.
|
2016-07-01 15:59:55 +00:00
|
|
|
func BenchmarkPendingDemotion100(b *testing.B) { benchmarkPendingDemotion(b, 100) }
|
|
|
|
func BenchmarkPendingDemotion1000(b *testing.B) { benchmarkPendingDemotion(b, 1000) }
|
|
|
|
func BenchmarkPendingDemotion10000(b *testing.B) { benchmarkPendingDemotion(b, 10000) }
|
2015-12-30 16:31:37 +00:00
|
|
|
|
2016-07-01 15:59:55 +00:00
|
|
|
func benchmarkPendingDemotion(b *testing.B, size int) {
|
2015-12-30 16:31:37 +00:00
|
|
|
// Add a batch of transactions to a pool one by one
|
|
|
|
pool, key := setupTxPool()
|
2017-07-06 08:51:59 +00:00
|
|
|
defer pool.Stop()
|
|
|
|
|
2016-11-02 12:44:13 +00:00
|
|
|
account, _ := deriveSender(transaction(0, big.NewInt(0), key))
|
2015-12-30 16:31:37 +00:00
|
|
|
state, _ := pool.currentState()
|
|
|
|
state.AddBalance(account, big.NewInt(1000000))
|
|
|
|
|
|
|
|
for i := 0; i < size; i++ {
|
|
|
|
tx := transaction(uint64(i), big.NewInt(100000), key)
|
2016-07-01 15:59:55 +00:00
|
|
|
pool.promoteTx(account, tx.Hash(), tx)
|
2015-12-30 16:31:37 +00:00
|
|
|
}
|
|
|
|
// Benchmark the speed of pool validation
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2016-12-10 22:54:58 +00:00
|
|
|
pool.demoteUnexecutables(state)
|
2015-12-30 16:31:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Benchmarks the speed of scheduling the contents of the future queue of the
|
|
|
|
// transaction pool.
|
2016-07-01 15:59:55 +00:00
|
|
|
func BenchmarkFuturePromotion100(b *testing.B) { benchmarkFuturePromotion(b, 100) }
|
|
|
|
func BenchmarkFuturePromotion1000(b *testing.B) { benchmarkFuturePromotion(b, 1000) }
|
|
|
|
func BenchmarkFuturePromotion10000(b *testing.B) { benchmarkFuturePromotion(b, 10000) }
|
2015-12-30 16:31:37 +00:00
|
|
|
|
2016-07-01 15:59:55 +00:00
|
|
|
func benchmarkFuturePromotion(b *testing.B, size int) {
|
2015-12-30 16:31:37 +00:00
|
|
|
// Add a batch of transactions to a pool one by one
|
|
|
|
pool, key := setupTxPool()
|
2017-07-06 08:51:59 +00:00
|
|
|
defer pool.Stop()
|
|
|
|
|
2016-11-02 12:44:13 +00:00
|
|
|
account, _ := deriveSender(transaction(0, big.NewInt(0), key))
|
2015-12-30 16:31:37 +00:00
|
|
|
state, _ := pool.currentState()
|
|
|
|
state.AddBalance(account, big.NewInt(1000000))
|
|
|
|
|
|
|
|
for i := 0; i < size; i++ {
|
|
|
|
tx := transaction(uint64(1+i), big.NewInt(100000), key)
|
2016-07-01 15:59:55 +00:00
|
|
|
pool.enqueueTx(tx.Hash(), tx)
|
2015-12-30 16:31:37 +00:00
|
|
|
}
|
|
|
|
// Benchmark the speed of pool validation
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2017-05-31 18:49:20 +00:00
|
|
|
pool.promoteExecutables(state, nil)
|
2016-07-01 15:59:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Benchmarks the speed of iterative transaction insertion.
|
|
|
|
func BenchmarkPoolInsert(b *testing.B) {
|
|
|
|
// Generate a batch of transactions to enqueue into the pool
|
|
|
|
pool, key := setupTxPool()
|
2017-07-06 08:51:59 +00:00
|
|
|
defer pool.Stop()
|
|
|
|
|
2016-11-02 12:44:13 +00:00
|
|
|
account, _ := deriveSender(transaction(0, big.NewInt(0), key))
|
2016-07-01 15:59:55 +00:00
|
|
|
state, _ := pool.currentState()
|
|
|
|
state.AddBalance(account, big.NewInt(1000000))
|
|
|
|
|
|
|
|
txs := make(types.Transactions, b.N)
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
txs[i] = transaction(uint64(i), big.NewInt(100000), key)
|
|
|
|
}
|
|
|
|
// Benchmark importing the transactions into the queue
|
|
|
|
b.ResetTimer()
|
|
|
|
for _, tx := range txs {
|
2017-07-05 13:51:55 +00:00
|
|
|
pool.AddRemote(tx)
|
2016-07-01 15:59:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Benchmarks the speed of batched transaction insertion.
|
|
|
|
func BenchmarkPoolBatchInsert100(b *testing.B) { benchmarkPoolBatchInsert(b, 100) }
|
|
|
|
func BenchmarkPoolBatchInsert1000(b *testing.B) { benchmarkPoolBatchInsert(b, 1000) }
|
|
|
|
func BenchmarkPoolBatchInsert10000(b *testing.B) { benchmarkPoolBatchInsert(b, 10000) }
|
|
|
|
|
|
|
|
func benchmarkPoolBatchInsert(b *testing.B, size int) {
|
|
|
|
// Generate a batch of transactions to enqueue into the pool
|
|
|
|
pool, key := setupTxPool()
|
2017-07-06 08:51:59 +00:00
|
|
|
defer pool.Stop()
|
|
|
|
|
2016-11-02 12:44:13 +00:00
|
|
|
account, _ := deriveSender(transaction(0, big.NewInt(0), key))
|
2016-07-01 15:59:55 +00:00
|
|
|
state, _ := pool.currentState()
|
|
|
|
state.AddBalance(account, big.NewInt(1000000))
|
|
|
|
|
|
|
|
batches := make([]types.Transactions, b.N)
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
batches[i] = make(types.Transactions, size)
|
|
|
|
for j := 0; j < size; j++ {
|
|
|
|
batches[i][j] = transaction(uint64(size*i+j), big.NewInt(100000), key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Benchmark importing the transactions into the queue
|
|
|
|
b.ResetTimer()
|
|
|
|
for _, batch := range batches {
|
2017-07-05 13:51:55 +00:00
|
|
|
pool.AddRemotes(batch)
|
2015-12-30 16:31:37 +00:00
|
|
|
}
|
|
|
|
}
|