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
|
|
|
|
2023-06-16 15:29:40 +03:00
|
|
|
package legacypool
|
2015-01-02 13:09:38 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
2023-02-16 22:36:58 +03:00
|
|
|
crand "crypto/rand"
|
2020-07-01 20:54:21 +03:00
|
|
|
"errors"
|
2017-06-22 17:01:49 +03:00
|
|
|
"fmt"
|
2015-04-04 22:41:24 +03:00
|
|
|
"math/big"
|
2016-08-17 16:53:15 +03:00
|
|
|
"math/rand"
|
2017-07-28 16:09:39 +03:00
|
|
|
"os"
|
2021-08-31 11:33:39 +03:00
|
|
|
"sync/atomic"
|
2015-01-02 13:09:38 +02:00
|
|
|
"testing"
|
2016-08-17 16:53:15 +03:00
|
|
|
"time"
|
2015-01-02 13:09:38 +02:00
|
|
|
|
2015-03-18 14:38:47 +02:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2022-10-24 16:13:55 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core"
|
2018-09-24 15:57:49 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
2015-04-04 22:41:24 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core/state"
|
2023-06-16 15:29:40 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core/txpool"
|
2015-01-02 13:09:38 +02:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"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-27 00:21:51 +03:00
|
|
|
"github.com/ethereum/go-ethereum/params"
|
2020-08-21 15:10:40 +03:00
|
|
|
"github.com/ethereum/go-ethereum/trie"
|
2015-01-02 13:09:38 +02:00
|
|
|
)
|
|
|
|
|
2021-05-28 11:28:07 +03:00
|
|
|
var (
|
|
|
|
// testTxPoolConfig is a transaction pool configuration without stateful disk
|
|
|
|
// sideeffects used during testing.
|
2022-10-24 16:13:55 +03:00
|
|
|
testTxPoolConfig Config
|
2021-05-28 11:28:07 +03:00
|
|
|
|
|
|
|
// eip1559Config is a chain config with EIP-1559 enabled at block 0.
|
|
|
|
eip1559Config *params.ChainConfig
|
|
|
|
)
|
2017-07-28 16:09:39 +03:00
|
|
|
|
|
|
|
func init() {
|
2022-10-24 16:13:55 +03:00
|
|
|
testTxPoolConfig = DefaultConfig
|
2017-07-28 16:09:39 +03:00
|
|
|
testTxPoolConfig.Journal = ""
|
2021-05-28 11:28:07 +03:00
|
|
|
|
|
|
|
cpy := *params.TestChainConfig
|
|
|
|
eip1559Config = &cpy
|
|
|
|
eip1559Config.BerlinBlock = common.Big0
|
|
|
|
eip1559Config.LondonBlock = common.Big0
|
2017-07-28 16:09:39 +03:00
|
|
|
}
|
|
|
|
|
2017-08-18 13:58:36 +03:00
|
|
|
type testBlockChain struct {
|
2023-06-16 15:29:40 +03:00
|
|
|
config *params.ChainConfig
|
2023-03-20 11:12:24 +03:00
|
|
|
gasLimit atomic.Uint64
|
2017-08-18 13:58:36 +03:00
|
|
|
statedb *state.StateDB
|
|
|
|
chainHeadFeed *event.Feed
|
|
|
|
}
|
|
|
|
|
2023-06-16 15:29:40 +03:00
|
|
|
func newTestBlockChain(config *params.ChainConfig, gasLimit uint64, statedb *state.StateDB, chainHeadFeed *event.Feed) *testBlockChain {
|
|
|
|
bc := testBlockChain{config: config, statedb: statedb, chainHeadFeed: new(event.Feed)}
|
2023-03-20 11:12:24 +03:00
|
|
|
bc.gasLimit.Store(gasLimit)
|
|
|
|
return &bc
|
|
|
|
}
|
|
|
|
|
2023-06-16 15:29:40 +03:00
|
|
|
func (bc *testBlockChain) Config() *params.ChainConfig {
|
|
|
|
return bc.config
|
|
|
|
}
|
|
|
|
|
2023-03-02 09:29:15 +03:00
|
|
|
func (bc *testBlockChain) CurrentBlock() *types.Header {
|
|
|
|
return &types.Header{
|
|
|
|
Number: new(big.Int),
|
2023-03-20 11:12:24 +03:00
|
|
|
GasLimit: bc.gasLimit.Load(),
|
2023-03-02 09:29:15 +03:00
|
|
|
}
|
2017-08-18 13:58:36 +03:00
|
|
|
}
|
|
|
|
|
2017-09-04 22:35:00 +03:00
|
|
|
func (bc *testBlockChain) GetBlock(hash common.Hash, number uint64) *types.Block {
|
2023-03-02 09:29:15 +03:00
|
|
|
return types.NewBlock(bc.CurrentBlock(), nil, nil, nil, trie.NewStackTrie(nil))
|
2017-09-04 22:35:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (bc *testBlockChain) StateAt(common.Hash) (*state.StateDB, error) {
|
|
|
|
return bc.statedb, nil
|
2017-08-18 13:58:36 +03:00
|
|
|
}
|
|
|
|
|
2022-10-24 16:13:55 +03:00
|
|
|
func (bc *testBlockChain) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription {
|
2017-09-05 19:49:37 +03:00
|
|
|
return bc.chainHeadFeed.Subscribe(ch)
|
|
|
|
}
|
|
|
|
|
2017-11-13 14:47:27 +03:00
|
|
|
func transaction(nonce uint64, gaslimit uint64, key *ecdsa.PrivateKey) *types.Transaction {
|
2017-05-16 22:07:27 +03:00
|
|
|
return pricedTransaction(nonce, gaslimit, big.NewInt(1), key)
|
|
|
|
}
|
|
|
|
|
2017-11-13 14:47:27 +03:00
|
|
|
func pricedTransaction(nonce uint64, gaslimit uint64, gasprice *big.Int, key *ecdsa.PrivateKey) *types.Transaction {
|
2017-05-16 22:07:27 +03:00
|
|
|
tx, _ := types.SignTx(types.NewTransaction(nonce, common.Address{}, big.NewInt(100), gaslimit, gasprice, nil), types.HomesteadSigner{}, key)
|
2015-06-11 15:05:32 +03:00
|
|
|
return tx
|
2015-01-02 13:09:38 +02:00
|
|
|
}
|
|
|
|
|
2020-01-10 12:40:03 +03:00
|
|
|
func pricedDataTransaction(nonce uint64, gaslimit uint64, gasprice *big.Int, key *ecdsa.PrivateKey, bytes uint64) *types.Transaction {
|
|
|
|
data := make([]byte, bytes)
|
2023-02-16 22:36:58 +03:00
|
|
|
crand.Read(data)
|
2020-01-10 12:40:03 +03:00
|
|
|
|
|
|
|
tx, _ := types.SignTx(types.NewTransaction(nonce, common.Address{}, big.NewInt(0), gaslimit, gasprice, data), types.HomesteadSigner{}, key)
|
|
|
|
return tx
|
|
|
|
}
|
|
|
|
|
2021-05-28 11:28:07 +03:00
|
|
|
func dynamicFeeTx(nonce uint64, gaslimit uint64, gasFee *big.Int, tip *big.Int, key *ecdsa.PrivateKey) *types.Transaction {
|
|
|
|
tx, _ := types.SignNewTx(key, types.LatestSignerForChainID(params.TestChainConfig.ChainID), &types.DynamicFeeTx{
|
|
|
|
ChainID: params.TestChainConfig.ChainID,
|
|
|
|
Nonce: nonce,
|
2021-06-08 13:05:41 +03:00
|
|
|
GasTipCap: tip,
|
|
|
|
GasFeeCap: gasFee,
|
2021-05-28 11:28:07 +03:00
|
|
|
Gas: gaslimit,
|
|
|
|
To: &common.Address{},
|
|
|
|
Value: big.NewInt(100),
|
|
|
|
Data: nil,
|
|
|
|
AccessList: nil,
|
|
|
|
})
|
|
|
|
return tx
|
|
|
|
}
|
|
|
|
|
2023-06-16 15:29:40 +03:00
|
|
|
func setupPool() (*LegacyPool, *ecdsa.PrivateKey) {
|
2022-10-24 16:13:55 +03:00
|
|
|
return setupPoolWithConfig(params.TestChainConfig)
|
2021-05-28 11:28:07 +03:00
|
|
|
}
|
|
|
|
|
2023-06-16 15:29:40 +03:00
|
|
|
func setupPoolWithConfig(config *params.ChainConfig) (*LegacyPool, *ecdsa.PrivateKey) {
|
2023-05-11 10:19:42 +03:00
|
|
|
statedb, _ := state.New(types.EmptyRootHash, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
|
2023-06-16 15:29:40 +03:00
|
|
|
blockchain := newTestBlockChain(config, 10000000, statedb, new(event.Feed))
|
2015-04-08 21:47:32 +03:00
|
|
|
|
2015-01-02 13:09:38 +02:00
|
|
|
key, _ := crypto.GenerateKey()
|
2023-06-16 15:29:40 +03:00
|
|
|
pool := New(testTxPoolConfig, blockchain)
|
|
|
|
if err := pool.Init(new(big.Int).SetUint64(testTxPoolConfig.PriceLimit), blockchain.CurrentBlock()); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-08-31 11:33:39 +03:00
|
|
|
// wait for the pool to initialize
|
|
|
|
<-pool.initDoneCh
|
2017-07-06 11:51:59 +03:00
|
|
|
return pool, key
|
2015-01-02 13:09:38 +02:00
|
|
|
}
|
|
|
|
|
2022-10-24 16:13:55 +03:00
|
|
|
// validatePoolInternals checks various consistency invariants within the pool.
|
2023-06-16 15:29:40 +03:00
|
|
|
func validatePoolInternals(pool *LegacyPool) error {
|
2017-06-22 17:01:49 +03:00
|
|
|
pool.mu.RLock()
|
|
|
|
defer pool.mu.RUnlock()
|
|
|
|
|
|
|
|
// Ensure the total transaction set is consistent with pending + queued
|
|
|
|
pending, queued := pool.stats()
|
2018-05-23 15:55:42 +03:00
|
|
|
if total := pool.all.Count(); total != pending+queued {
|
2017-06-22 17:01:49 +03:00
|
|
|
return fmt.Errorf("total transaction count %d != %d pending + %d queued", total, pending, queued)
|
|
|
|
}
|
2020-12-11 12:44:57 +03:00
|
|
|
pool.priced.Reheap()
|
2021-05-28 11:28:07 +03:00
|
|
|
priced, remote := pool.priced.urgent.Len()+pool.priced.floating.Len(), pool.all.RemoteCount()
|
2020-12-11 12:44:57 +03:00
|
|
|
if priced != remote {
|
|
|
|
return fmt.Errorf("total priced transaction count %d != %d", priced, remote)
|
2017-06-22 17:01:49 +03:00
|
|
|
}
|
|
|
|
// Ensure the next nonce to assign is the correct one
|
|
|
|
for addr, txs := range pool.pending {
|
|
|
|
// Find the last transaction
|
|
|
|
var last uint64
|
2017-11-08 13:45:52 +03:00
|
|
|
for nonce := range txs.txs.items {
|
2017-06-22 17:01:49 +03:00
|
|
|
if last < nonce {
|
|
|
|
last = nonce
|
|
|
|
}
|
|
|
|
}
|
2020-05-06 16:47:59 +03:00
|
|
|
if nonce := pool.pendingNonces.get(addr); nonce != last+1 {
|
2017-06-22 17:01:49 +03:00
|
|
|
return fmt.Errorf("pending nonce mismatch: have %v, want %v", nonce, last+1)
|
|
|
|
}
|
2023-03-10 20:30:26 +03:00
|
|
|
if txs.totalcost.Cmp(common.Big0) < 0 {
|
|
|
|
return fmt.Errorf("totalcost went negative: %v", txs.totalcost)
|
|
|
|
}
|
2017-06-22 17:01:49 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-10-20 12:34:43 +03:00
|
|
|
// validateEvents checks that the correct number of transaction addition events
|
|
|
|
// were fired on the pool's event feed.
|
2022-10-24 16:13:55 +03:00
|
|
|
func validateEvents(events chan core.NewTxsEvent, count int) error {
|
2018-05-18 11:45:52 +03:00
|
|
|
var received []*types.Transaction
|
|
|
|
|
|
|
|
for len(received) < count {
|
2017-10-20 12:34:43 +03:00
|
|
|
select {
|
2018-05-10 10:04:45 +03:00
|
|
|
case ev := <-events:
|
2018-05-18 11:45:52 +03:00
|
|
|
received = append(received, ev.Txs...)
|
2017-10-20 12:34:43 +03:00
|
|
|
case <-time.After(time.Second):
|
2019-02-26 07:12:13 +03:00
|
|
|
return fmt.Errorf("event #%d not fired", len(received))
|
2017-10-20 12:34:43 +03:00
|
|
|
}
|
|
|
|
}
|
2018-05-18 11:45:52 +03:00
|
|
|
if len(received) > count {
|
|
|
|
return fmt.Errorf("more than %d events fired: %v", count, received[count:])
|
|
|
|
}
|
2017-10-20 12:34:43 +03:00
|
|
|
select {
|
2018-05-10 10:04:45 +03:00
|
|
|
case ev := <-events:
|
|
|
|
return fmt.Errorf("more than %d events fired: %v", count, ev.Txs)
|
2017-10-20 12:34:43 +03:00
|
|
|
|
|
|
|
case <-time.After(50 * time.Millisecond):
|
|
|
|
// This branch should be "default", but it's a data race between goroutines,
|
2018-05-10 10:04:45 +03:00
|
|
|
// reading the event channel and pushing into it, so better wait a bit ensuring
|
2017-10-20 12:34:43 +03:00
|
|
|
// really nothing gets injected.
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-11-02 15:44:13 +03:00
|
|
|
func deriveSender(tx *types.Transaction) (common.Address, error) {
|
|
|
|
return types.Sender(types.HomesteadSigner{}, tx)
|
|
|
|
}
|
|
|
|
|
2017-08-18 13:58:36 +03:00
|
|
|
type testChain struct {
|
|
|
|
*testBlockChain
|
|
|
|
address common.Address
|
|
|
|
trigger *bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// testChain.State() 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.
|
|
|
|
func (c *testChain) State() (*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 := c.statedb
|
|
|
|
if *c.trigger {
|
2023-05-11 10:19:42 +03:00
|
|
|
c.statedb, _ = state.New(types.EmptyRootHash, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
|
2017-08-18 13:58:36 +03:00
|
|
|
// simulate that the new head block included tx0 and tx1
|
|
|
|
c.statedb.SetNonce(c.address, 2)
|
|
|
|
c.statedb.SetBalance(c.address, new(big.Int).SetUint64(params.Ether))
|
|
|
|
*c.trigger = false
|
|
|
|
}
|
|
|
|
return stdb, nil
|
|
|
|
}
|
|
|
|
|
2016-12-11 01:54:58 +03: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().
|
2022-10-24 16:13:55 +03:00
|
|
|
func TestStateChangeDuringReset(t *testing.T) {
|
2017-10-20 12:34:43 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2016-12-11 01:54:58 +03:00
|
|
|
var (
|
|
|
|
key, _ = crypto.GenerateKey()
|
|
|
|
address = crypto.PubkeyToAddress(key.PublicKey)
|
2023-05-11 10:19:42 +03:00
|
|
|
statedb, _ = state.New(types.EmptyRootHash, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
|
2016-12-11 01:54:58 +03: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-27 00:21:51 +03:00
|
|
|
statedb.SetBalance(address, new(big.Int).SetUint64(params.Ether))
|
2023-06-16 15:29:40 +03:00
|
|
|
blockchain := &testChain{newTestBlockChain(params.TestChainConfig, 1000000000, statedb, new(event.Feed)), address, &trigger}
|
2016-12-11 01:54:58 +03:00
|
|
|
|
2017-11-13 14:47:27 +03:00
|
|
|
tx0 := transaction(0, 100000, key)
|
|
|
|
tx1 := transaction(1, 100000, key)
|
2016-12-11 01:54:58 +03:00
|
|
|
|
2023-06-16 15:29:40 +03:00
|
|
|
pool := New(testTxPoolConfig, blockchain)
|
|
|
|
pool.Init(new(big.Int).SetUint64(testTxPoolConfig.PriceLimit), blockchain.CurrentBlock())
|
|
|
|
defer pool.Close()
|
2016-12-11 01:54:58 +03:00
|
|
|
|
2019-07-09 10:34:35 +03:00
|
|
|
nonce := pool.Nonce(address)
|
2016-12-11 01:54:58 +03:00
|
|
|
if nonce != 0 {
|
|
|
|
t.Fatalf("Invalid nonce, want 0, got %d", nonce)
|
|
|
|
}
|
|
|
|
|
2023-06-16 15:29:40 +03:00
|
|
|
pool.addRemotesSync([]*types.Transaction{tx0, tx1})
|
2016-12-11 01:54:58 +03:00
|
|
|
|
2019-07-09 10:34:35 +03:00
|
|
|
nonce = pool.Nonce(address)
|
2016-12-11 01:54:58 +03:00
|
|
|
if nonce != 2 {
|
|
|
|
t.Fatalf("Invalid nonce, want 2, got %d", nonce)
|
|
|
|
}
|
|
|
|
|
|
|
|
// trigger state change in the background
|
|
|
|
trigger = true
|
2019-06-21 11:29:14 +03:00
|
|
|
<-pool.requestReset(nil, nil)
|
2016-12-11 01:54:58 +03:00
|
|
|
|
2019-07-09 10:34:35 +03:00
|
|
|
nonce = pool.Nonce(address)
|
2016-12-11 01:54:58 +03:00
|
|
|
if nonce != 2 {
|
|
|
|
t.Fatalf("Invalid nonce, want 2, got %d", nonce)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-16 15:29:40 +03:00
|
|
|
func testAddBalance(pool *LegacyPool, addr common.Address, amount *big.Int) {
|
2021-05-28 11:28:07 +03:00
|
|
|
pool.mu.Lock()
|
|
|
|
pool.currentState.AddBalance(addr, amount)
|
|
|
|
pool.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2023-06-16 15:29:40 +03:00
|
|
|
func testSetNonce(pool *LegacyPool, addr common.Address, nonce uint64) {
|
2021-05-28 11:28:07 +03:00
|
|
|
pool.mu.Lock()
|
|
|
|
pool.currentState.SetNonce(addr, nonce)
|
|
|
|
pool.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2015-04-08 21:47:32 +03:00
|
|
|
func TestInvalidTransactions(t *testing.T) {
|
2017-10-20 12:34:43 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2022-10-24 16:13:55 +03:00
|
|
|
pool, key := setupPool()
|
2023-06-16 15:29:40 +03:00
|
|
|
defer pool.Close()
|
2015-01-02 13:09:38 +02:00
|
|
|
|
2017-11-13 14:47:27 +03:00
|
|
|
tx := transaction(0, 100, key)
|
2016-11-02 15:44:13 +03:00
|
|
|
from, _ := deriveSender(tx)
|
2017-09-04 22:35:00 +03:00
|
|
|
|
2023-04-03 14:16:57 +03:00
|
|
|
// Intrinsic gas too low
|
2021-05-28 11:28:07 +03:00
|
|
|
testAddBalance(pool, from, big.NewInt(1))
|
2023-06-16 15:29:40 +03:00
|
|
|
if err, want := pool.addRemote(tx), core.ErrIntrinsicGas; !errors.Is(err, want) {
|
2023-04-03 14:16:57 +03:00
|
|
|
t.Errorf("want %v have %v", want, err)
|
2015-01-02 13:09:38 +02:00
|
|
|
}
|
|
|
|
|
2023-04-03 14:16:57 +03:00
|
|
|
// Insufficient funds
|
|
|
|
tx = transaction(0, 100000, key)
|
2023-06-16 15:29:40 +03:00
|
|
|
if err, want := pool.addRemote(tx), core.ErrInsufficientFunds; !errors.Is(err, want) {
|
2023-04-03 14:16:57 +03:00
|
|
|
t.Errorf("want %v have %v", want, err)
|
2015-01-02 13:09:38 +02:00
|
|
|
}
|
2015-01-02 13:18:23 +02:00
|
|
|
|
2021-05-28 11:28:07 +03:00
|
|
|
testSetNonce(pool, from, 1)
|
|
|
|
testAddBalance(pool, from, big.NewInt(0xffffffffffffff))
|
2017-11-13 14:47:27 +03:00
|
|
|
tx = transaction(0, 100000, key)
|
2023-06-16 15:29:40 +03:00
|
|
|
if err, want := pool.addRemote(tx), core.ErrNonceTooLow; !errors.Is(err, want) {
|
2023-04-03 14:16:57 +03:00
|
|
|
t.Errorf("want %v have %v", want, err)
|
2015-04-21 23:01:04 +03:00
|
|
|
}
|
2015-11-21 01:40:36 +02:00
|
|
|
|
2017-11-13 14:47:27 +03:00
|
|
|
tx = transaction(1, 100000, key)
|
2023-06-06 12:53:29 +03:00
|
|
|
pool.gasTip.Store(big.NewInt(1000))
|
2023-06-16 15:29:40 +03:00
|
|
|
if err, want := pool.addRemote(tx), txpool.ErrUnderpriced; !errors.Is(err, want) {
|
2023-04-03 14:16:57 +03:00
|
|
|
t.Errorf("want %v have %v", want, err)
|
2015-11-21 01:40:36 +02:00
|
|
|
}
|
2023-06-16 15:29:40 +03:00
|
|
|
if err := pool.addLocal(tx); err != nil {
|
2015-11-21 01:40:36 +02:00
|
|
|
t.Error("expected", nil, "got", err)
|
|
|
|
}
|
2015-04-21 23:01:04 +03:00
|
|
|
}
|
|
|
|
|
2022-10-24 16:13:55 +03:00
|
|
|
func TestQueue(t *testing.T) {
|
2017-10-20 12:34:43 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2022-10-24 16:13:55 +03:00
|
|
|
pool, key := setupPool()
|
2023-06-16 15:29:40 +03:00
|
|
|
defer pool.Close()
|
2017-07-06 11:51:59 +03:00
|
|
|
|
2017-11-13 14:47:27 +03:00
|
|
|
tx := transaction(0, 100, key)
|
2016-11-02 15:44:13 +03:00
|
|
|
from, _ := deriveSender(tx)
|
2021-05-28 11:28:07 +03:00
|
|
|
testAddBalance(pool, from, big.NewInt(1000))
|
2019-06-21 11:29:14 +03:00
|
|
|
<-pool.requestReset(nil, nil)
|
2015-04-21 23:01:04 +03:00
|
|
|
|
2020-12-11 12:44:57 +03:00
|
|
|
pool.enqueueTx(tx.Hash(), tx, false, true)
|
2019-06-21 11:29:14 +03:00
|
|
|
<-pool.requestPromoteExecutables(newAccountSet(pool.signer, from))
|
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
|
|
|
}
|
|
|
|
|
2017-11-13 14:47:27 +03:00
|
|
|
tx = transaction(1, 100, key)
|
2016-11-02 15:44:13 +03:00
|
|
|
from, _ = deriveSender(tx)
|
2021-05-28 11:28:07 +03:00
|
|
|
testSetNonce(pool, from, 2)
|
2020-12-11 12:44:57 +03:00
|
|
|
pool.enqueueTx(tx.Hash(), tx, false, true)
|
2019-06-21 11:29:14 +03:00
|
|
|
|
|
|
|
<-pool.requestPromoteExecutables(newAccountSet(pool.signer, from))
|
2016-08-25 19:04:40 +03:00
|
|
|
if _, ok := pool.pending[from].txs.items[tx.Nonce()]; ok {
|
2015-04-21 23:01:04 +03:00
|
|
|
t.Error("expected transaction to be in tx pool")
|
|
|
|
}
|
2016-07-01 18:59:55 +03:00
|
|
|
if len(pool.queue) > 0 {
|
|
|
|
t.Error("expected transaction queue to be empty. is", len(pool.queue))
|
2015-04-21 23:01:04 +03:00
|
|
|
}
|
2019-06-21 11:29:14 +03:00
|
|
|
}
|
2015-04-21 23:01:04 +03:00
|
|
|
|
2022-10-24 16:13:55 +03:00
|
|
|
func TestQueue2(t *testing.T) {
|
2019-06-21 11:29:14 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2022-10-24 16:13:55 +03:00
|
|
|
pool, key := setupPool()
|
2023-06-16 15:29:40 +03:00
|
|
|
defer pool.Close()
|
2017-08-07 15:47:25 +03:00
|
|
|
|
2017-11-13 14:47:27 +03:00
|
|
|
tx1 := transaction(0, 100, key)
|
|
|
|
tx2 := transaction(10, 100, key)
|
|
|
|
tx3 := transaction(11, 100, key)
|
2019-06-21 11:29:14 +03:00
|
|
|
from, _ := deriveSender(tx1)
|
2021-05-28 11:28:07 +03:00
|
|
|
testAddBalance(pool, from, big.NewInt(1000))
|
2019-06-21 11:29:14 +03:00
|
|
|
pool.reset(nil, nil)
|
2016-12-11 01:54:58 +03:00
|
|
|
|
2020-12-11 12:44:57 +03:00
|
|
|
pool.enqueueTx(tx1.Hash(), tx1, false, true)
|
|
|
|
pool.enqueueTx(tx2.Hash(), tx2, false, true)
|
|
|
|
pool.enqueueTx(tx3.Hash(), tx3, false, true)
|
2015-06-03 15:06:20 +03:00
|
|
|
|
2017-09-04 22:35:00 +03:00
|
|
|
pool.promoteExecutables([]common.Address{from})
|
2015-06-04 13:47:46 +03:00
|
|
|
if len(pool.pending) != 1 {
|
2019-06-21 11:29:14 +03:00
|
|
|
t.Error("expected pending length to be 1, got", len(pool.pending))
|
2015-04-21 23:01:04 +03:00
|
|
|
}
|
2016-07-01 18:59:55 +03:00
|
|
|
if pool.queue[from].Len() != 2 {
|
|
|
|
t.Error("expected len(queue) == 2, got", pool.queue[from].Len())
|
2015-01-31 18:22:17 +02:00
|
|
|
}
|
|
|
|
}
|
2015-04-30 01:20:59 +03:00
|
|
|
|
2022-10-24 16:13:55 +03:00
|
|
|
func TestNegativeValue(t *testing.T) {
|
2017-10-20 12:34:43 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2022-10-24 16:13:55 +03:00
|
|
|
pool, key := setupPool()
|
2023-06-16 15:29:40 +03:00
|
|
|
defer pool.Close()
|
2015-05-26 20:50:42 +03:00
|
|
|
|
2017-11-13 14:47:27 +03:00
|
|
|
tx, _ := types.SignTx(types.NewTransaction(0, common.Address{}, big.NewInt(-1), 100, big.NewInt(1), nil), types.HomesteadSigner{}, key)
|
2016-11-02 15:44:13 +03:00
|
|
|
from, _ := deriveSender(tx)
|
2021-05-28 11:28:07 +03:00
|
|
|
testAddBalance(pool, from, big.NewInt(1))
|
2023-06-16 15:29:40 +03:00
|
|
|
if err := pool.addRemote(tx); err != txpool.ErrNegativeValue {
|
|
|
|
t.Error("expected", txpool.ErrNegativeValue, "got", err)
|
2015-05-26 20:50:42 +03:00
|
|
|
}
|
|
|
|
}
|
2015-06-04 18:28:09 +03:00
|
|
|
|
2022-10-24 16:13:55 +03:00
|
|
|
func TestTipAboveFeeCap(t *testing.T) {
|
2021-05-28 11:28:07 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2022-10-24 16:13:55 +03:00
|
|
|
pool, key := setupPoolWithConfig(eip1559Config)
|
2023-06-16 15:29:40 +03:00
|
|
|
defer pool.Close()
|
2021-05-28 11:28:07 +03:00
|
|
|
|
|
|
|
tx := dynamicFeeTx(0, 100, big.NewInt(1), big.NewInt(2), key)
|
|
|
|
|
2023-06-16 15:29:40 +03:00
|
|
|
if err := pool.addRemote(tx); err != core.ErrTipAboveFeeCap {
|
2022-10-24 16:13:55 +03:00
|
|
|
t.Error("expected", core.ErrTipAboveFeeCap, "got", err)
|
2021-05-28 11:28:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-24 16:13:55 +03:00
|
|
|
func TestVeryHighValues(t *testing.T) {
|
2021-05-30 20:37:52 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2022-10-24 16:13:55 +03:00
|
|
|
pool, key := setupPoolWithConfig(eip1559Config)
|
2023-06-16 15:29:40 +03:00
|
|
|
defer pool.Close()
|
2021-05-30 20:37:52 +03:00
|
|
|
|
|
|
|
veryBigNumber := big.NewInt(1)
|
|
|
|
veryBigNumber.Lsh(veryBigNumber, 300)
|
|
|
|
|
|
|
|
tx := dynamicFeeTx(0, 100, big.NewInt(1), veryBigNumber, key)
|
2023-06-16 15:29:40 +03:00
|
|
|
if err := pool.addRemote(tx); err != core.ErrTipVeryHigh {
|
2022-10-24 16:13:55 +03:00
|
|
|
t.Error("expected", core.ErrTipVeryHigh, "got", err)
|
2021-05-30 20:37:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
tx2 := dynamicFeeTx(0, 100, veryBigNumber, big.NewInt(1), key)
|
2023-06-16 15:29:40 +03:00
|
|
|
if err := pool.addRemote(tx2); err != core.ErrFeeCapVeryHigh {
|
2022-10-24 16:13:55 +03:00
|
|
|
t.Error("expected", core.ErrFeeCapVeryHigh, "got", err)
|
2021-05-30 20:37:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-24 16:13:55 +03:00
|
|
|
func TestChainFork(t *testing.T) {
|
2017-10-20 12:34:43 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2022-10-24 16:13:55 +03:00
|
|
|
pool, key := setupPool()
|
2023-06-16 15:29:40 +03:00
|
|
|
defer pool.Close()
|
2017-07-06 11:51:59 +03:00
|
|
|
|
2015-06-04 18:28:09 +03:00
|
|
|
addr := crypto.PubkeyToAddress(key.PublicKey)
|
2015-06-09 19:14:46 +03:00
|
|
|
resetState := func() {
|
2023-05-11 10:19:42 +03:00
|
|
|
statedb, _ := state.New(types.EmptyRootHash, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
|
2017-09-04 22:35:00 +03:00
|
|
|
statedb.AddBalance(addr, big.NewInt(100000000000000))
|
|
|
|
|
2023-06-16 15:29:40 +03:00
|
|
|
pool.chain = newTestBlockChain(pool.chainconfig, 1000000, statedb, new(event.Feed))
|
2019-06-21 11:29:14 +03:00
|
|
|
<-pool.requestReset(nil, nil)
|
2015-06-09 19:14:46 +03:00
|
|
|
}
|
|
|
|
resetState()
|
|
|
|
|
2017-11-13 14:47:27 +03:00
|
|
|
tx := transaction(0, 100000, key)
|
2017-07-05 16:51:55 +03:00
|
|
|
if _, err := pool.add(tx, false); err != nil {
|
2015-06-04 18:28:09 +03:00
|
|
|
t.Error("didn't expect error", err)
|
|
|
|
}
|
2018-04-12 12:17:52 +03:00
|
|
|
pool.removeTx(tx.Hash(), true)
|
2015-06-04 18:28:09 +03:00
|
|
|
|
|
|
|
// reset the pool's internal state
|
2015-06-09 19:14:46 +03:00
|
|
|
resetState()
|
2017-07-05 16:51:55 +03:00
|
|
|
if _, err := pool.add(tx, false); err != nil {
|
2015-06-04 18:28:09 +03:00
|
|
|
t.Error("didn't expect error", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-24 16:13:55 +03:00
|
|
|
func TestDoubleNonce(t *testing.T) {
|
2017-10-20 12:34:43 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2022-10-24 16:13:55 +03:00
|
|
|
pool, key := setupPool()
|
2023-06-16 15:29:40 +03:00
|
|
|
defer pool.Close()
|
2017-07-06 11:51:59 +03:00
|
|
|
|
2015-06-04 18:28:09 +03:00
|
|
|
addr := crypto.PubkeyToAddress(key.PublicKey)
|
2015-06-09 19:14:46 +03:00
|
|
|
resetState := func() {
|
2023-05-11 10:19:42 +03:00
|
|
|
statedb, _ := state.New(types.EmptyRootHash, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
|
2017-09-04 22:35:00 +03:00
|
|
|
statedb.AddBalance(addr, big.NewInt(100000000000000))
|
|
|
|
|
2023-06-16 15:29:40 +03:00
|
|
|
pool.chain = newTestBlockChain(pool.chainconfig, 1000000, statedb, new(event.Feed))
|
2019-06-21 11:29:14 +03:00
|
|
|
<-pool.requestReset(nil, nil)
|
2015-06-09 19:14:46 +03:00
|
|
|
}
|
|
|
|
resetState()
|
|
|
|
|
2016-11-02 15:44:13 +03:00
|
|
|
signer := types.HomesteadSigner{}
|
2017-11-13 14:47:27 +03:00
|
|
|
tx1, _ := types.SignTx(types.NewTransaction(0, common.Address{}, big.NewInt(100), 100000, big.NewInt(1), nil), signer, key)
|
|
|
|
tx2, _ := types.SignTx(types.NewTransaction(0, common.Address{}, big.NewInt(100), 1000000, big.NewInt(2), nil), signer, key)
|
|
|
|
tx3, _ := types.SignTx(types.NewTransaction(0, common.Address{}, big.NewInt(100), 1000000, big.NewInt(1), nil), signer, key)
|
2016-06-27 18:28:34 +03:00
|
|
|
|
|
|
|
// Add the first two transaction, ensure higher priced stays only
|
2017-07-05 16:51:55 +03:00
|
|
|
if replace, err := pool.add(tx1, false); err != nil || replace {
|
2017-05-16 22:07:27 +03:00
|
|
|
t.Errorf("first transaction insert failed (%v) or reported replacement (%v)", err, replace)
|
2015-06-04 18:28:09 +03:00
|
|
|
}
|
2017-07-05 16:51:55 +03:00
|
|
|
if replace, err := pool.add(tx2, false); err != nil || !replace {
|
2017-05-16 22:07:27 +03:00
|
|
|
t.Errorf("second transaction insert failed (%v) or not reported replacement (%v)", err, replace)
|
2015-06-04 18:28:09 +03:00
|
|
|
}
|
2019-06-21 11:29:14 +03:00
|
|
|
<-pool.requestPromoteExecutables(newAccountSet(signer, addr))
|
2016-07-01 18:59:55 +03:00
|
|
|
if pool.pending[addr].Len() != 1 {
|
|
|
|
t.Error("expected 1 pending transactions, got", pool.pending[addr].Len())
|
2016-06-27 18:28:34 +03:00
|
|
|
}
|
2016-08-25 19:04:40 +03:00
|
|
|
if tx := pool.pending[addr].txs.items[0]; tx.Hash() != tx2.Hash() {
|
2016-06-27 18:28:34 +03:00
|
|
|
t.Errorf("transaction mismatch: have %x, want %x", tx.Hash(), tx2.Hash())
|
|
|
|
}
|
2019-06-21 11:29:14 +03:00
|
|
|
|
2017-05-31 21:49:20 +03:00
|
|
|
// Add the third transaction and ensure it's not saved (smaller price)
|
2017-07-05 16:51:55 +03:00
|
|
|
pool.add(tx3, false)
|
2019-06-21 11:29:14 +03:00
|
|
|
<-pool.requestPromoteExecutables(newAccountSet(signer, addr))
|
2016-07-01 18:59:55 +03:00
|
|
|
if pool.pending[addr].Len() != 1 {
|
|
|
|
t.Error("expected 1 pending transactions, got", pool.pending[addr].Len())
|
2016-06-27 18:28:34 +03:00
|
|
|
}
|
2016-08-25 19:04:40 +03:00
|
|
|
if tx := pool.pending[addr].txs.items[0]; tx.Hash() != tx2.Hash() {
|
2016-06-27 18:28:34 +03:00
|
|
|
t.Errorf("transaction mismatch: have %x, want %x", tx.Hash(), tx2.Hash())
|
|
|
|
}
|
|
|
|
// Ensure the total transaction count is correct
|
2018-05-23 15:55:42 +03:00
|
|
|
if pool.all.Count() != 1 {
|
|
|
|
t.Error("expected 1 total transactions, got", pool.all.Count())
|
2015-06-04 18:28:09 +03:00
|
|
|
}
|
|
|
|
}
|
2015-06-09 01:41:47 +03:00
|
|
|
|
2022-10-24 16:13:55 +03:00
|
|
|
func TestMissingNonce(t *testing.T) {
|
2017-10-20 12:34:43 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2022-10-24 16:13:55 +03:00
|
|
|
pool, key := setupPool()
|
2023-06-16 15:29:40 +03:00
|
|
|
defer pool.Close()
|
2017-07-06 11:51:59 +03:00
|
|
|
|
2015-06-09 01:41:47 +03:00
|
|
|
addr := crypto.PubkeyToAddress(key.PublicKey)
|
2021-05-28 11:28:07 +03:00
|
|
|
testAddBalance(pool, addr, big.NewInt(100000000000000))
|
2017-11-13 14:47:27 +03:00
|
|
|
tx := transaction(1, 100000, key)
|
2017-07-05 16:51:55 +03:00
|
|
|
if _, err := pool.add(tx, false); err != nil {
|
2015-06-09 01:41:47 +03:00
|
|
|
t.Error("didn't expect error", err)
|
|
|
|
}
|
2016-07-01 18:59:55 +03:00
|
|
|
if len(pool.pending) != 0 {
|
|
|
|
t.Error("expected 0 pending transactions, got", len(pool.pending))
|
2015-06-09 01:41:47 +03:00
|
|
|
}
|
2016-07-01 18:59:55 +03:00
|
|
|
if pool.queue[addr].Len() != 1 {
|
|
|
|
t.Error("expected 1 queued transaction, got", pool.queue[addr].Len())
|
2015-06-09 01:41:47 +03:00
|
|
|
}
|
2018-05-23 15:55:42 +03:00
|
|
|
if pool.all.Count() != 1 {
|
|
|
|
t.Error("expected 1 total transactions, got", pool.all.Count())
|
2016-06-27 18:28:34 +03:00
|
|
|
}
|
2015-06-09 01:41:47 +03:00
|
|
|
}
|
2015-09-18 12:59:21 +03:00
|
|
|
|
2022-10-24 16:13:55 +03:00
|
|
|
func TestNonceRecovery(t *testing.T) {
|
2017-10-20 12:34:43 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2015-09-18 12:59:21 +03:00
|
|
|
const n = 10
|
2022-10-24 16:13:55 +03:00
|
|
|
pool, key := setupPool()
|
2023-06-16 15:29:40 +03:00
|
|
|
defer pool.Close()
|
2017-07-06 11:51:59 +03:00
|
|
|
|
2015-09-18 12:59:21 +03:00
|
|
|
addr := crypto.PubkeyToAddress(key.PublicKey)
|
2021-05-28 11:28:07 +03:00
|
|
|
testSetNonce(pool, addr, n)
|
|
|
|
testAddBalance(pool, addr, big.NewInt(100000000000000))
|
2019-06-21 11:29:14 +03:00
|
|
|
<-pool.requestReset(nil, nil)
|
2017-09-04 22:35:00 +03:00
|
|
|
|
2017-11-13 14:47:27 +03:00
|
|
|
tx := transaction(n, 100000, key)
|
2023-06-16 15:29:40 +03:00
|
|
|
if err := pool.addRemote(tx); err != nil {
|
2015-09-18 12:59:21 +03:00
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
// simulate some weird re-order of transactions and missing nonce(s)
|
2021-05-28 11:28:07 +03:00
|
|
|
testSetNonce(pool, addr, n-1)
|
2019-06-21 11:29:14 +03:00
|
|
|
<-pool.requestReset(nil, nil)
|
2019-07-09 10:34:35 +03:00
|
|
|
if fn := pool.Nonce(addr); fn != n-1 {
|
2017-09-04 22:35:00 +03:00
|
|
|
t.Errorf("expected nonce to be %d, got %d", n-1, fn)
|
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.
|
2022-10-24 16:13:55 +03:00
|
|
|
func TestDropping(t *testing.T) {
|
2017-10-20 12:34:43 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2015-12-30 18:31:37 +02:00
|
|
|
// Create a test account and fund it
|
2022-10-24 16:13:55 +03:00
|
|
|
pool, key := setupPool()
|
2023-06-16 15:29:40 +03:00
|
|
|
defer pool.Close()
|
2017-07-06 11:51:59 +03:00
|
|
|
|
2020-01-10 12:40:03 +03:00
|
|
|
account := crypto.PubkeyToAddress(key.PublicKey)
|
2021-05-28 11:28:07 +03:00
|
|
|
testAddBalance(pool, account, big.NewInt(1000))
|
2015-12-30 18:31:37 +02:00
|
|
|
|
|
|
|
// Add some pending and some queued transactions
|
|
|
|
var (
|
2017-11-13 14:47:27 +03:00
|
|
|
tx0 = transaction(0, 100, key)
|
|
|
|
tx1 = transaction(1, 200, key)
|
|
|
|
tx2 = transaction(2, 300, key)
|
|
|
|
tx10 = transaction(10, 100, key)
|
|
|
|
tx11 = transaction(11, 200, key)
|
|
|
|
tx12 = transaction(12, 300, key)
|
2015-12-30 18:31:37 +02:00
|
|
|
)
|
2020-12-11 12:44:57 +03:00
|
|
|
pool.all.Add(tx0, false)
|
|
|
|
pool.priced.Put(tx0, false)
|
2016-07-01 18:59:55 +03:00
|
|
|
pool.promoteTx(account, tx0.Hash(), tx0)
|
2020-12-11 12:44:57 +03:00
|
|
|
|
|
|
|
pool.all.Add(tx1, false)
|
|
|
|
pool.priced.Put(tx1, false)
|
2016-07-01 18:59:55 +03:00
|
|
|
pool.promoteTx(account, tx1.Hash(), tx1)
|
2020-12-11 12:44:57 +03:00
|
|
|
|
|
|
|
pool.all.Add(tx2, false)
|
|
|
|
pool.priced.Put(tx2, false)
|
2017-06-22 17:01:49 +03:00
|
|
|
pool.promoteTx(account, tx2.Hash(), tx2)
|
2020-12-11 12:44:57 +03:00
|
|
|
|
|
|
|
pool.enqueueTx(tx10.Hash(), tx10, false, true)
|
|
|
|
pool.enqueueTx(tx11.Hash(), tx11, false, true)
|
|
|
|
pool.enqueueTx(tx12.Hash(), tx12, false, true)
|
2015-12-30 18:31:37 +02:00
|
|
|
|
|
|
|
// Check that pre and post validations leave the pool as is
|
2017-05-30 00:31:37 +03:00
|
|
|
if pool.pending[account].Len() != 3 {
|
|
|
|
t.Errorf("pending transaction mismatch: have %d, want %d", pool.pending[account].Len(), 3)
|
2015-12-30 18:31:37 +02:00
|
|
|
}
|
2017-05-30 00:31:37 +03:00
|
|
|
if pool.queue[account].Len() != 3 {
|
|
|
|
t.Errorf("queued transaction mismatch: have %d, want %d", pool.queue[account].Len(), 3)
|
2016-06-27 18:28:34 +03:00
|
|
|
}
|
2018-05-23 15:55:42 +03:00
|
|
|
if pool.all.Count() != 6 {
|
|
|
|
t.Errorf("total transaction mismatch: have %d, want %d", pool.all.Count(), 6)
|
2015-12-30 18:31:37 +02:00
|
|
|
}
|
2019-06-21 11:29:14 +03:00
|
|
|
<-pool.requestReset(nil, nil)
|
2017-05-30 00:31:37 +03:00
|
|
|
if pool.pending[account].Len() != 3 {
|
|
|
|
t.Errorf("pending transaction mismatch: have %d, want %d", pool.pending[account].Len(), 3)
|
2015-12-30 18:31:37 +02:00
|
|
|
}
|
2017-05-30 00:31:37 +03:00
|
|
|
if pool.queue[account].Len() != 3 {
|
|
|
|
t.Errorf("queued transaction mismatch: have %d, want %d", pool.queue[account].Len(), 3)
|
2016-06-27 18:28:34 +03:00
|
|
|
}
|
2018-05-23 15:55:42 +03:00
|
|
|
if pool.all.Count() != 6 {
|
|
|
|
t.Errorf("total transaction mismatch: have %d, want %d", pool.all.Count(), 6)
|
2015-12-30 18:31:37 +02:00
|
|
|
}
|
|
|
|
// Reduce the balance of the account, and check that invalidated transactions are dropped
|
2021-05-28 11:28:07 +03:00
|
|
|
testAddBalance(pool, account, big.NewInt(-650))
|
2019-06-21 11:29:14 +03:00
|
|
|
<-pool.requestReset(nil, nil)
|
2015-12-30 18:31:37 +02:00
|
|
|
|
2016-08-25 19:04:40 +03:00
|
|
|
if _, ok := pool.pending[account].txs.items[tx0.Nonce()]; !ok {
|
2015-12-30 18:31:37 +02:00
|
|
|
t.Errorf("funded pending transaction missing: %v", tx0)
|
|
|
|
}
|
2017-05-30 00:31:37 +03: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 18:31:37 +02:00
|
|
|
t.Errorf("out-of-fund pending transaction present: %v", tx1)
|
|
|
|
}
|
2016-08-25 19:04:40 +03:00
|
|
|
if _, ok := pool.queue[account].txs.items[tx10.Nonce()]; !ok {
|
2015-12-30 18:31:37 +02:00
|
|
|
t.Errorf("funded queued transaction missing: %v", tx10)
|
|
|
|
}
|
2017-05-30 00:31:37 +03: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 18:31:37 +02:00
|
|
|
t.Errorf("out-of-fund queued transaction present: %v", tx11)
|
|
|
|
}
|
2018-05-23 15:55:42 +03:00
|
|
|
if pool.all.Count() != 4 {
|
|
|
|
t.Errorf("total transaction mismatch: have %d, want %d", pool.all.Count(), 4)
|
2017-05-30 00:31:37 +03:00
|
|
|
}
|
|
|
|
// Reduce the block gas limit, check that invalidated transactions are dropped
|
2023-03-20 11:12:24 +03:00
|
|
|
pool.chain.(*testBlockChain).gasLimit.Store(100)
|
2019-06-21 11:29:14 +03:00
|
|
|
<-pool.requestReset(nil, nil)
|
2017-05-30 00:31:37 +03: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)
|
|
|
|
}
|
2018-05-23 15:55:42 +03:00
|
|
|
if pool.all.Count() != 2 {
|
|
|
|
t.Errorf("total transaction mismatch: have %d, want %d", pool.all.Count(), 2)
|
2016-06-27 18:28:34 +03:00
|
|
|
}
|
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.
|
2022-10-24 16:13:55 +03:00
|
|
|
func TestPostponing(t *testing.T) {
|
2017-10-20 12:34:43 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2018-03-07 11:55:59 +03:00
|
|
|
// Create the pool to test the postponing with
|
2023-05-11 10:19:42 +03:00
|
|
|
statedb, _ := state.New(types.EmptyRootHash, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
|
2023-06-16 15:29:40 +03:00
|
|
|
blockchain := newTestBlockChain(params.TestChainConfig, 1000000, statedb, new(event.Feed))
|
2018-03-07 11:55:59 +03:00
|
|
|
|
2023-06-16 15:29:40 +03:00
|
|
|
pool := New(testTxPoolConfig, blockchain)
|
|
|
|
pool.Init(new(big.Int).SetUint64(testTxPoolConfig.PriceLimit), blockchain.CurrentBlock())
|
|
|
|
defer pool.Close()
|
2017-07-06 11:51:59 +03:00
|
|
|
|
2018-03-07 11:55:59 +03:00
|
|
|
// Create two test accounts to produce different gap profiles with
|
|
|
|
keys := make([]*ecdsa.PrivateKey, 2)
|
|
|
|
accs := make([]common.Address, len(keys))
|
2015-12-30 18:31:37 +02:00
|
|
|
|
2018-03-07 11:55:59 +03:00
|
|
|
for i := 0; i < len(keys); i++ {
|
|
|
|
keys[i], _ = crypto.GenerateKey()
|
|
|
|
accs[i] = crypto.PubkeyToAddress(keys[i].PublicKey)
|
|
|
|
|
2021-05-28 11:28:07 +03:00
|
|
|
testAddBalance(pool, crypto.PubkeyToAddress(keys[i].PublicKey), big.NewInt(50100))
|
2018-03-07 11:55:59 +03:00
|
|
|
}
|
2015-12-30 18:31:37 +02:00
|
|
|
// Add a batch consecutive pending transactions for validation
|
2018-03-07 11:55:59 +03:00
|
|
|
txs := []*types.Transaction{}
|
|
|
|
for i, key := range keys {
|
|
|
|
for j := 0; j < 100; j++ {
|
|
|
|
var tx *types.Transaction
|
|
|
|
if (i+j)%2 == 0 {
|
|
|
|
tx = transaction(uint64(j), 25000, key)
|
|
|
|
} else {
|
|
|
|
tx = transaction(uint64(j), 50000, key)
|
|
|
|
}
|
|
|
|
txs = append(txs, tx)
|
|
|
|
}
|
|
|
|
}
|
2023-06-16 15:29:40 +03:00
|
|
|
for i, err := range pool.addRemotesSync(txs) {
|
2018-03-07 11:55:59 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("tx %d: failed to add transactions: %v", i, err)
|
2015-12-30 18:31:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Check that pre and post validations leave the pool as is
|
2018-03-07 11:55:59 +03:00
|
|
|
if pending := pool.pending[accs[0]].Len() + pool.pending[accs[1]].Len(); pending != len(txs) {
|
|
|
|
t.Errorf("pending transaction mismatch: have %d, want %d", pending, len(txs))
|
2015-12-30 18:31:37 +02:00
|
|
|
}
|
2016-07-01 18:59:55 +03:00
|
|
|
if len(pool.queue) != 0 {
|
2018-03-07 11:55:59 +03:00
|
|
|
t.Errorf("queued accounts mismatch: have %d, want %d", len(pool.queue), 0)
|
2016-06-27 18:28:34 +03:00
|
|
|
}
|
2018-05-23 15:55:42 +03:00
|
|
|
if pool.all.Count() != len(txs) {
|
|
|
|
t.Errorf("total transaction mismatch: have %d, want %d", pool.all.Count(), len(txs))
|
2015-12-30 18:31:37 +02:00
|
|
|
}
|
2019-06-21 11:29:14 +03:00
|
|
|
<-pool.requestReset(nil, nil)
|
2018-03-07 11:55:59 +03:00
|
|
|
if pending := pool.pending[accs[0]].Len() + pool.pending[accs[1]].Len(); pending != len(txs) {
|
|
|
|
t.Errorf("pending transaction mismatch: have %d, want %d", pending, len(txs))
|
2015-12-30 18:31:37 +02:00
|
|
|
}
|
2016-07-01 18:59:55 +03:00
|
|
|
if len(pool.queue) != 0 {
|
2018-03-07 11:55:59 +03:00
|
|
|
t.Errorf("queued accounts mismatch: have %d, want %d", len(pool.queue), 0)
|
2016-06-27 18:28:34 +03:00
|
|
|
}
|
2018-05-23 15:55:42 +03:00
|
|
|
if pool.all.Count() != len(txs) {
|
|
|
|
t.Errorf("total transaction mismatch: have %d, want %d", pool.all.Count(), len(txs))
|
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
|
2018-03-07 11:55:59 +03:00
|
|
|
for _, addr := range accs {
|
2021-05-28 11:28:07 +03:00
|
|
|
testAddBalance(pool, addr, big.NewInt(-1))
|
2018-03-07 11:55:59 +03:00
|
|
|
}
|
2019-06-21 11:29:14 +03:00
|
|
|
<-pool.requestReset(nil, nil)
|
2015-12-30 18:31:37 +02:00
|
|
|
|
2018-03-07 11:55:59 +03:00
|
|
|
// The first account's first transaction remains valid, check that subsequent
|
|
|
|
// ones are either filtered out, or queued up for later.
|
|
|
|
if _, ok := pool.pending[accs[0]].txs.items[txs[0].Nonce()]; !ok {
|
|
|
|
t.Errorf("tx %d: valid and funded transaction missing from pending pool: %v", 0, txs[0])
|
2015-12-30 18:31:37 +02:00
|
|
|
}
|
2018-03-07 11:55:59 +03:00
|
|
|
if _, ok := pool.queue[accs[0]].txs.items[txs[0].Nonce()]; ok {
|
|
|
|
t.Errorf("tx %d: valid and funded transaction present in future queue: %v", 0, txs[0])
|
2015-12-30 18:31:37 +02:00
|
|
|
}
|
2018-03-07 11:55:59 +03:00
|
|
|
for i, tx := range txs[1:100] {
|
2015-12-30 18:31:37 +02:00
|
|
|
if i%2 == 1 {
|
2018-03-07 11:55:59 +03:00
|
|
|
if _, ok := pool.pending[accs[0]].txs.items[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)
|
|
|
|
}
|
2018-03-07 11:55:59 +03:00
|
|
|
if _, ok := pool.queue[accs[0]].txs.items[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 {
|
2018-03-07 11:55:59 +03:00
|
|
|
if _, ok := pool.pending[accs[0]].txs.items[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)
|
|
|
|
}
|
2018-03-07 11:55:59 +03:00
|
|
|
if _, ok := pool.queue[accs[0]].txs.items[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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-07 11:55:59 +03:00
|
|
|
// The second account's first transaction got invalid, check that all transactions
|
|
|
|
// are either filtered out, or queued up for later.
|
|
|
|
if pool.pending[accs[1]] != nil {
|
|
|
|
t.Errorf("invalidated account still has pending transactions")
|
|
|
|
}
|
|
|
|
for i, tx := range txs[100:] {
|
|
|
|
if i%2 == 1 {
|
|
|
|
if _, ok := pool.queue[accs[1]].txs.items[tx.Nonce()]; !ok {
|
|
|
|
t.Errorf("tx %d: valid but future transaction missing from future queue: %v", 100+i, tx)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if _, ok := pool.queue[accs[1]].txs.items[tx.Nonce()]; ok {
|
|
|
|
t.Errorf("tx %d: out-of-fund transaction present in future queue: %v", 100+i, tx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-05-23 15:55:42 +03:00
|
|
|
if pool.all.Count() != len(txs)/2 {
|
|
|
|
t.Errorf("total transaction mismatch: have %d, want %d", pool.all.Count(), len(txs)/2)
|
2016-06-27 18:28:34 +03:00
|
|
|
}
|
2015-12-30 18:31:37 +02:00
|
|
|
}
|
|
|
|
|
2017-10-20 12:34:43 +03:00
|
|
|
// Tests that if the transaction pool has both executable and non-executable
|
|
|
|
// transactions from an origin account, filling the nonce gap moves all queued
|
|
|
|
// ones into the pending pool.
|
2022-10-24 16:13:55 +03:00
|
|
|
func TestGapFilling(t *testing.T) {
|
2017-10-20 12:34:43 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
// Create a test account and fund it
|
2022-10-24 16:13:55 +03:00
|
|
|
pool, key := setupPool()
|
2023-06-16 15:29:40 +03:00
|
|
|
defer pool.Close()
|
2017-10-20 12:34:43 +03:00
|
|
|
|
2020-01-10 12:40:03 +03:00
|
|
|
account := crypto.PubkeyToAddress(key.PublicKey)
|
2021-05-28 11:28:07 +03:00
|
|
|
testAddBalance(pool, account, big.NewInt(1000000))
|
2017-10-20 12:34:43 +03:00
|
|
|
|
|
|
|
// Keep track of transaction events to ensure all executables get announced
|
2022-10-24 16:13:55 +03:00
|
|
|
events := make(chan core.NewTxsEvent, testTxPoolConfig.AccountQueue+5)
|
2017-10-20 12:34:43 +03:00
|
|
|
sub := pool.txFeed.Subscribe(events)
|
|
|
|
defer sub.Unsubscribe()
|
|
|
|
|
|
|
|
// Create a pending and a queued transaction with a nonce-gap in between
|
2023-06-16 15:29:40 +03:00
|
|
|
pool.addRemotesSync([]*types.Transaction{
|
2019-06-21 11:29:14 +03:00
|
|
|
transaction(0, 100000, key),
|
|
|
|
transaction(2, 100000, key),
|
|
|
|
})
|
2017-10-20 12:34:43 +03:00
|
|
|
pending, queued := pool.Stats()
|
|
|
|
if pending != 1 {
|
|
|
|
t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 1)
|
|
|
|
}
|
|
|
|
if queued != 1 {
|
|
|
|
t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 1)
|
|
|
|
}
|
|
|
|
if err := validateEvents(events, 1); err != nil {
|
|
|
|
t.Fatalf("original event firing failed: %v", err)
|
|
|
|
}
|
2022-10-24 16:13:55 +03:00
|
|
|
if err := validatePoolInternals(pool); err != nil {
|
2017-10-20 12:34:43 +03:00
|
|
|
t.Fatalf("pool internal state corrupted: %v", err)
|
|
|
|
}
|
|
|
|
// Fill the nonce gap and ensure all transactions become pending
|
2019-06-21 11:29:14 +03:00
|
|
|
if err := pool.addRemoteSync(transaction(1, 100000, key)); err != nil {
|
2017-10-20 12:34:43 +03:00
|
|
|
t.Fatalf("failed to add gapped transaction: %v", err)
|
|
|
|
}
|
|
|
|
pending, queued = pool.Stats()
|
|
|
|
if pending != 3 {
|
|
|
|
t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 3)
|
|
|
|
}
|
|
|
|
if queued != 0 {
|
|
|
|
t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 0)
|
|
|
|
}
|
|
|
|
if err := validateEvents(events, 2); err != nil {
|
|
|
|
t.Fatalf("gap-filling event firing failed: %v", err)
|
|
|
|
}
|
2022-10-24 16:13:55 +03:00
|
|
|
if err := validatePoolInternals(pool); err != nil {
|
2017-10-20 12:34:43 +03:00
|
|
|
t.Fatalf("pool internal state corrupted: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
2022-10-24 16:13:55 +03:00
|
|
|
func TestQueueAccountLimiting(t *testing.T) {
|
2017-10-20 12:34:43 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2015-12-30 18:31:37 +02:00
|
|
|
// Create a test account and fund it
|
2022-10-24 16:13:55 +03:00
|
|
|
pool, key := setupPool()
|
2023-06-16 15:29:40 +03:00
|
|
|
defer pool.Close()
|
2017-07-06 11:51:59 +03:00
|
|
|
|
2020-01-10 12:40:03 +03:00
|
|
|
account := crypto.PubkeyToAddress(key.PublicKey)
|
2021-05-28 11:28:07 +03:00
|
|
|
testAddBalance(pool, account, big.NewInt(1000000))
|
2015-12-30 18:31:37 +02:00
|
|
|
|
|
|
|
// Keep queuing up transactions and make sure all above a limit are dropped
|
2017-07-28 16:09:39 +03:00
|
|
|
for i := uint64(1); i <= testTxPoolConfig.AccountQueue+5; i++ {
|
2019-06-21 11:29:14 +03:00
|
|
|
if err := pool.addRemoteSync(transaction(i, 100000, key)); err != nil {
|
2015-12-30 18:31:37 +02: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 16:09:39 +03:00
|
|
|
if i <= testTxPoolConfig.AccountQueue {
|
2016-07-01 18:59:55 +03: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 18:31:37 +02:00
|
|
|
}
|
|
|
|
} else {
|
2017-07-28 16:09:39 +03: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 18:31:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-05-23 15:55:42 +03:00
|
|
|
if pool.all.Count() != int(testTxPoolConfig.AccountQueue) {
|
|
|
|
t.Errorf("total transaction mismatch: have %d, want %d", pool.all.Count(), testTxPoolConfig.AccountQueue)
|
2016-08-17 16:53:15 +03: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 11:51:59 +03:00
|
|
|
//
|
|
|
|
// This logic should not hold for local transactions, unless the local tracking
|
|
|
|
// mechanism is disabled.
|
2022-10-24 16:13:55 +03:00
|
|
|
func TestQueueGlobalLimiting(t *testing.T) {
|
|
|
|
testQueueGlobalLimiting(t, false)
|
2017-07-06 11:51:59 +03:00
|
|
|
}
|
2022-10-24 16:13:55 +03:00
|
|
|
func TestQueueGlobalLimitingNoLocals(t *testing.T) {
|
|
|
|
testQueueGlobalLimiting(t, true)
|
2017-07-06 11:51:59 +03:00
|
|
|
}
|
2016-08-17 16:53:15 +03:00
|
|
|
|
2022-10-24 16:13:55 +03:00
|
|
|
func testQueueGlobalLimiting(t *testing.T, nolocals bool) {
|
2017-10-20 12:34:43 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2016-08-17 16:53:15 +03:00
|
|
|
// Create the pool to test the limit enforcement with
|
2023-05-11 10:19:42 +03:00
|
|
|
statedb, _ := state.New(types.EmptyRootHash, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
|
2023-06-16 15:29:40 +03:00
|
|
|
blockchain := newTestBlockChain(params.TestChainConfig, 1000000, statedb, new(event.Feed))
|
2016-08-17 16:53:15 +03:00
|
|
|
|
2017-07-28 16:09:39 +03:00
|
|
|
config := testTxPoolConfig
|
2017-07-06 11:51:59 +03:00
|
|
|
config.NoLocals = nolocals
|
|
|
|
config.GlobalQueue = config.AccountQueue*3 - 1 // reduce the queue limits to shorten test time (-1 to make it non divisible)
|
|
|
|
|
2023-06-16 15:29:40 +03:00
|
|
|
pool := New(config, blockchain)
|
|
|
|
pool.Init(new(big.Int).SetUint64(testTxPoolConfig.PriceLimit), blockchain.CurrentBlock())
|
|
|
|
defer pool.Close()
|
2016-08-17 16:53:15 +03:00
|
|
|
|
2017-07-06 11:51:59 +03:00
|
|
|
// Create a number of test accounts and fund them (last one will be the local)
|
2016-08-17 16:53:15 +03:00
|
|
|
keys := make([]*ecdsa.PrivateKey, 5)
|
|
|
|
for i := 0; i < len(keys); i++ {
|
|
|
|
keys[i], _ = crypto.GenerateKey()
|
2021-05-28 11:28:07 +03:00
|
|
|
testAddBalance(pool, crypto.PubkeyToAddress(keys[i].PublicKey), big.NewInt(1000000))
|
2016-08-17 16:53:15 +03:00
|
|
|
}
|
2017-07-06 11:51:59 +03:00
|
|
|
local := keys[len(keys)-1]
|
|
|
|
|
2016-08-17 16:53:15 +03:00
|
|
|
// Generate and queue a batch of transactions
|
|
|
|
nonces := make(map[common.Address]uint64)
|
|
|
|
|
2017-07-06 11:51:59 +03:00
|
|
|
txs := make(types.Transactions, 0, 3*config.GlobalQueue)
|
2016-08-17 16:53:15 +03:00
|
|
|
for len(txs) < cap(txs) {
|
2017-07-06 11:51:59 +03:00
|
|
|
key := keys[rand.Intn(len(keys)-1)] // skip adding transactions with the local account
|
2016-08-17 16:53:15 +03:00
|
|
|
addr := crypto.PubkeyToAddress(key.PublicKey)
|
|
|
|
|
2017-11-13 14:47:27 +03:00
|
|
|
txs = append(txs, transaction(nonces[addr]+1, 100000, key))
|
2016-08-17 16:53:15 +03:00
|
|
|
nonces[addr]++
|
|
|
|
}
|
|
|
|
// Import the batch and verify that limits have been enforced
|
2023-06-16 15:29:40 +03:00
|
|
|
pool.addRemotesSync(txs)
|
2016-08-17 16:53:15 +03:00
|
|
|
|
|
|
|
queued := 0
|
|
|
|
for addr, list := range pool.queue {
|
2017-07-06 11:51:59 +03: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 16:53:15 +03:00
|
|
|
}
|
|
|
|
queued += list.Len()
|
|
|
|
}
|
2017-07-06 11:51:59 +03: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++ {
|
2017-11-13 14:47:27 +03:00
|
|
|
txs = append(txs, transaction(i+1, 100000, local))
|
2017-07-06 11:51:59 +03:00
|
|
|
}
|
2023-06-16 15:29:40 +03:00
|
|
|
pool.addLocals(txs)
|
2017-07-06 11:51:59 +03:00
|
|
|
|
|
|
|
// 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 16:53:15 +03: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 11:51:59 +03:00
|
|
|
//
|
|
|
|
// This logic should not hold for local transactions, unless the local tracking
|
|
|
|
// mechanism is disabled.
|
2022-10-24 16:13:55 +03:00
|
|
|
func TestQueueTimeLimiting(t *testing.T) {
|
|
|
|
testQueueTimeLimiting(t, false)
|
2020-04-08 11:07:29 +03:00
|
|
|
}
|
2022-10-24 16:13:55 +03:00
|
|
|
func TestQueueTimeLimitingNoLocals(t *testing.T) {
|
|
|
|
testQueueTimeLimiting(t, true)
|
2020-04-08 11:07:29 +03:00
|
|
|
}
|
2017-07-06 11:51:59 +03:00
|
|
|
|
2022-10-24 16:13:55 +03:00
|
|
|
func testQueueTimeLimiting(t *testing.T, nolocals bool) {
|
2017-07-06 11:51:59 +03:00
|
|
|
// Reduce the eviction interval to a testable amount
|
2016-08-17 16:53:15 +03:00
|
|
|
defer func(old time.Duration) { evictionInterval = old }(evictionInterval)
|
2020-05-23 04:15:01 +03:00
|
|
|
evictionInterval = time.Millisecond * 100
|
2016-08-17 16:53:15 +03:00
|
|
|
|
2017-07-06 11:51:59 +03:00
|
|
|
// Create the pool to test the non-expiration enforcement
|
2023-05-11 10:19:42 +03:00
|
|
|
statedb, _ := state.New(types.EmptyRootHash, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
|
2023-06-16 15:29:40 +03:00
|
|
|
blockchain := newTestBlockChain(params.TestChainConfig, 1000000, statedb, new(event.Feed))
|
2017-07-06 11:51:59 +03:00
|
|
|
|
2017-07-28 16:09:39 +03:00
|
|
|
config := testTxPoolConfig
|
2017-08-07 12:53:32 +03:00
|
|
|
config.Lifetime = time.Second
|
2017-07-06 11:51:59 +03:00
|
|
|
config.NoLocals = nolocals
|
|
|
|
|
2023-06-16 15:29:40 +03:00
|
|
|
pool := New(config, blockchain)
|
|
|
|
pool.Init(new(big.Int).SetUint64(config.PriceLimit), blockchain.CurrentBlock())
|
|
|
|
defer pool.Close()
|
2017-07-06 11:51:59 +03:00
|
|
|
|
|
|
|
// Create two test accounts to ensure remotes expire but locals do not
|
|
|
|
local, _ := crypto.GenerateKey()
|
|
|
|
remote, _ := crypto.GenerateKey()
|
2016-08-17 16:53:15 +03:00
|
|
|
|
2021-05-28 11:28:07 +03:00
|
|
|
testAddBalance(pool, crypto.PubkeyToAddress(local.PublicKey), big.NewInt(1000000000))
|
|
|
|
testAddBalance(pool, crypto.PubkeyToAddress(remote.PublicKey), big.NewInt(1000000000))
|
2016-08-17 16:53:15 +03:00
|
|
|
|
2017-07-06 11:51:59 +03:00
|
|
|
// Add the two transactions and ensure they both are queued up
|
2023-06-16 15:29:40 +03:00
|
|
|
if err := pool.addLocal(pricedTransaction(1, 100000, big.NewInt(1), local)); err != nil {
|
2017-07-06 11:51:59 +03:00
|
|
|
t.Fatalf("failed to add local transaction: %v", err)
|
|
|
|
}
|
2023-06-16 15:29:40 +03:00
|
|
|
if err := pool.addRemote(pricedTransaction(1, 100000, big.NewInt(1), remote)); err != nil {
|
2017-07-06 11:51:59 +03:00
|
|
|
t.Fatalf("failed to add remote transaction: %v", err)
|
|
|
|
}
|
2017-08-08 11:59:34 +03:00
|
|
|
pending, queued := pool.Stats()
|
2017-07-06 11:51:59 +03: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)
|
|
|
|
}
|
2022-10-24 16:13:55 +03:00
|
|
|
if err := validatePoolInternals(pool); err != nil {
|
2017-07-06 11:51:59 +03:00
|
|
|
t.Fatalf("pool internal state corrupted: %v", err)
|
|
|
|
}
|
2020-05-23 04:15:01 +03:00
|
|
|
|
|
|
|
// Allow the eviction interval to run
|
|
|
|
time.Sleep(2 * evictionInterval)
|
|
|
|
|
|
|
|
// Transactions should not be evicted from the queue yet since lifetime duration has not passed
|
|
|
|
pending, queued = pool.Stats()
|
|
|
|
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)
|
|
|
|
}
|
2022-10-24 16:13:55 +03:00
|
|
|
if err := validatePoolInternals(pool); err != nil {
|
2020-05-23 04:15:01 +03:00
|
|
|
t.Fatalf("pool internal state corrupted: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-07-06 11:51:59 +03:00
|
|
|
// 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 11:59:34 +03:00
|
|
|
pending, queued = pool.Stats()
|
2017-07-06 11:51:59 +03: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 16:53:15 +03:00
|
|
|
}
|
|
|
|
}
|
2022-10-24 16:13:55 +03:00
|
|
|
if err := validatePoolInternals(pool); err != nil {
|
2017-07-06 11:51:59 +03:00
|
|
|
t.Fatalf("pool internal state corrupted: %v", err)
|
2016-06-27 18:28:34 +03:00
|
|
|
}
|
2020-05-23 04:15:01 +03:00
|
|
|
|
|
|
|
// remove current transactions and increase nonce to prepare for a reset and cleanup
|
|
|
|
statedb.SetNonce(crypto.PubkeyToAddress(remote.PublicKey), 2)
|
|
|
|
statedb.SetNonce(crypto.PubkeyToAddress(local.PublicKey), 2)
|
|
|
|
<-pool.requestReset(nil, nil)
|
|
|
|
|
|
|
|
// make sure queue, pending are cleared
|
|
|
|
pending, queued = pool.Stats()
|
|
|
|
if pending != 0 {
|
|
|
|
t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 0)
|
|
|
|
}
|
|
|
|
if queued != 0 {
|
|
|
|
t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 0)
|
|
|
|
}
|
2022-10-24 16:13:55 +03:00
|
|
|
if err := validatePoolInternals(pool); err != nil {
|
2020-05-23 04:15:01 +03:00
|
|
|
t.Fatalf("pool internal state corrupted: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-07-08 11:27:04 +03:00
|
|
|
// Queue gapped transactions
|
2023-06-16 15:29:40 +03:00
|
|
|
if err := pool.addLocal(pricedTransaction(4, 100000, big.NewInt(1), local)); err != nil {
|
2020-05-23 04:15:01 +03:00
|
|
|
t.Fatalf("failed to add remote transaction: %v", err)
|
|
|
|
}
|
|
|
|
if err := pool.addRemoteSync(pricedTransaction(4, 100000, big.NewInt(1), remote)); err != nil {
|
|
|
|
t.Fatalf("failed to add remote transaction: %v", err)
|
|
|
|
}
|
2020-07-08 11:27:04 +03:00
|
|
|
time.Sleep(5 * evictionInterval) // A half lifetime pass
|
2020-05-23 04:15:01 +03:00
|
|
|
|
2020-07-08 11:27:04 +03:00
|
|
|
// Queue executable transactions, the life cycle should be restarted.
|
2023-06-16 15:29:40 +03:00
|
|
|
if err := pool.addLocal(pricedTransaction(2, 100000, big.NewInt(1), local)); err != nil {
|
2020-07-08 11:27:04 +03:00
|
|
|
t.Fatalf("failed to add remote transaction: %v", err)
|
|
|
|
}
|
|
|
|
if err := pool.addRemoteSync(pricedTransaction(2, 100000, big.NewInt(1), remote)); err != nil {
|
2020-05-23 04:15:01 +03:00
|
|
|
t.Fatalf("failed to add remote transaction: %v", err)
|
|
|
|
}
|
2020-07-08 11:27:04 +03:00
|
|
|
time.Sleep(6 * evictionInterval)
|
2020-05-23 04:15:01 +03:00
|
|
|
|
2020-07-08 11:27:04 +03:00
|
|
|
// All gapped transactions shouldn't be kicked out
|
2020-05-23 04:15:01 +03:00
|
|
|
pending, queued = pool.Stats()
|
|
|
|
if pending != 2 {
|
|
|
|
t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 2)
|
|
|
|
}
|
2020-07-08 11:27:04 +03:00
|
|
|
if queued != 2 {
|
2023-07-06 19:05:34 +03:00
|
|
|
t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 2)
|
2020-05-23 04:15:01 +03:00
|
|
|
}
|
2022-10-24 16:13:55 +03:00
|
|
|
if err := validatePoolInternals(pool); err != nil {
|
2020-05-23 04:15:01 +03:00
|
|
|
t.Fatalf("pool internal state corrupted: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-07-08 11:27:04 +03:00
|
|
|
// The whole life time pass after last promotion, kick out stale transactions
|
|
|
|
time.Sleep(2 * config.Lifetime)
|
2020-05-23 04:15:01 +03:00
|
|
|
pending, queued = pool.Stats()
|
2020-07-08 11:27:04 +03:00
|
|
|
if pending != 2 {
|
|
|
|
t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 2)
|
2020-05-23 04:15:01 +03:00
|
|
|
}
|
|
|
|
if nolocals {
|
|
|
|
if queued != 0 {
|
2020-07-08 11:27:04 +03:00
|
|
|
t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 0)
|
2020-05-23 04:15:01 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if queued != 1 {
|
2020-07-08 11:27:04 +03:00
|
|
|
t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 1)
|
2020-05-23 04:15:01 +03:00
|
|
|
}
|
|
|
|
}
|
2022-10-24 16:13:55 +03:00
|
|
|
if err := validatePoolInternals(pool); err != nil {
|
2020-05-23 04:15:01 +03:00
|
|
|
t.Fatalf("pool internal state corrupted: %v", err)
|
|
|
|
}
|
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.
|
2022-10-24 16:13:55 +03:00
|
|
|
func TestPendingLimiting(t *testing.T) {
|
2017-10-20 12:34:43 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2015-12-30 18:31:37 +02:00
|
|
|
// Create a test account and fund it
|
2022-10-24 16:13:55 +03:00
|
|
|
pool, key := setupPool()
|
2023-06-16 15:29:40 +03:00
|
|
|
defer pool.Close()
|
2017-07-06 11:51:59 +03:00
|
|
|
|
2020-01-10 12:40:03 +03:00
|
|
|
account := crypto.PubkeyToAddress(key.PublicKey)
|
2023-03-10 20:30:26 +03:00
|
|
|
testAddBalance(pool, account, big.NewInt(1000000000000))
|
2015-12-30 18:31:37 +02:00
|
|
|
|
2017-10-20 12:34:43 +03:00
|
|
|
// Keep track of transaction events to ensure all executables get announced
|
2022-10-24 16:13:55 +03:00
|
|
|
events := make(chan core.NewTxsEvent, testTxPoolConfig.AccountQueue+5)
|
2017-10-20 12:34:43 +03:00
|
|
|
sub := pool.txFeed.Subscribe(events)
|
|
|
|
defer sub.Unsubscribe()
|
|
|
|
|
2015-12-30 18:31:37 +02:00
|
|
|
// Keep queuing up transactions and make sure all above a limit are dropped
|
2017-07-28 16:09:39 +03:00
|
|
|
for i := uint64(0); i < testTxPoolConfig.AccountQueue+5; i++ {
|
2019-06-21 11:29:14 +03:00
|
|
|
if err := pool.addRemoteSync(transaction(i, 100000, key)); err != nil {
|
2015-12-30 18:31:37 +02:00
|
|
|
t.Fatalf("tx %d: failed to add transaction: %v", i, err)
|
|
|
|
}
|
2016-07-01 18:59:55 +03: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 18:31:37 +02:00
|
|
|
}
|
2016-07-01 18:59:55 +03: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 18:31:37 +02:00
|
|
|
}
|
|
|
|
}
|
2018-05-23 15:55:42 +03:00
|
|
|
if pool.all.Count() != int(testTxPoolConfig.AccountQueue+5) {
|
|
|
|
t.Errorf("total transaction mismatch: have %d, want %d", pool.all.Count(), testTxPoolConfig.AccountQueue+5)
|
2016-06-27 18:28:34 +03:00
|
|
|
}
|
2017-10-20 12:34:43 +03:00
|
|
|
if err := validateEvents(events, int(testTxPoolConfig.AccountQueue+5)); err != nil {
|
|
|
|
t.Fatalf("event firing failed: %v", err)
|
|
|
|
}
|
2022-10-24 16:13:55 +03:00
|
|
|
if err := validatePoolInternals(pool); err != nil {
|
2017-10-20 12:34:43 +03:00
|
|
|
t.Fatalf("pool internal state corrupted: %v", err)
|
|
|
|
}
|
2015-12-30 18:31:37 +02:00
|
|
|
}
|
|
|
|
|
2016-10-14 15:32:06 +03: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.
|
2022-10-24 16:13:55 +03:00
|
|
|
func TestPendingGlobalLimiting(t *testing.T) {
|
2017-10-20 12:34:43 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2016-10-14 15:32:06 +03:00
|
|
|
// Create the pool to test the limit enforcement with
|
2023-05-11 10:19:42 +03:00
|
|
|
statedb, _ := state.New(types.EmptyRootHash, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
|
2023-06-16 15:29:40 +03:00
|
|
|
blockchain := newTestBlockChain(params.TestChainConfig, 1000000, statedb, new(event.Feed))
|
2016-10-14 15:32:06 +03:00
|
|
|
|
2017-07-28 16:09:39 +03:00
|
|
|
config := testTxPoolConfig
|
2017-07-06 11:51:59 +03:00
|
|
|
config.GlobalSlots = config.AccountSlots * 10
|
|
|
|
|
2023-06-16 15:29:40 +03:00
|
|
|
pool := New(config, blockchain)
|
|
|
|
pool.Init(new(big.Int).SetUint64(config.PriceLimit), blockchain.CurrentBlock())
|
|
|
|
defer pool.Close()
|
2016-10-14 15:32:06 +03:00
|
|
|
|
|
|
|
// Create a number of test accounts and fund them
|
|
|
|
keys := make([]*ecdsa.PrivateKey, 5)
|
|
|
|
for i := 0; i < len(keys); i++ {
|
|
|
|
keys[i], _ = crypto.GenerateKey()
|
2021-05-28 11:28:07 +03:00
|
|
|
testAddBalance(pool, crypto.PubkeyToAddress(keys[i].PublicKey), big.NewInt(1000000))
|
2016-10-14 15:32:06 +03:00
|
|
|
}
|
|
|
|
// 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 11:51:59 +03:00
|
|
|
for j := 0; j < int(config.GlobalSlots)/len(keys)*2; j++ {
|
2017-11-13 14:47:27 +03:00
|
|
|
txs = append(txs, transaction(nonces[addr], 100000, key))
|
2016-10-14 15:32:06 +03:00
|
|
|
nonces[addr]++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Import the batch and verify that limits have been enforced
|
2023-06-16 15:29:40 +03:00
|
|
|
pool.addRemotesSync(txs)
|
2016-10-14 15:32:06 +03:00
|
|
|
|
|
|
|
pending := 0
|
|
|
|
for _, list := range pool.pending {
|
|
|
|
pending += list.Len()
|
|
|
|
}
|
2017-07-06 11:51:59 +03:00
|
|
|
if pending > int(config.GlobalSlots) {
|
|
|
|
t.Fatalf("total pending transactions overflow allowance: %d > %d", pending, config.GlobalSlots)
|
2016-10-14 15:32:06 +03:00
|
|
|
}
|
2022-10-24 16:13:55 +03:00
|
|
|
if err := validatePoolInternals(pool); err != nil {
|
2017-06-22 17:01:49 +03:00
|
|
|
t.Fatalf("pool internal state corrupted: %v", err)
|
|
|
|
}
|
2016-10-14 15:32:06 +03:00
|
|
|
}
|
|
|
|
|
2020-01-10 12:40:03 +03:00
|
|
|
// Test the limit on transaction size is enforced correctly.
|
|
|
|
// This test verifies every transaction having allowed size
|
|
|
|
// is added to the pool, and longer transactions are rejected.
|
2022-10-24 16:13:55 +03:00
|
|
|
func TestAllowedTxSize(t *testing.T) {
|
2020-01-10 12:40:03 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
// Create a test account and fund it
|
2022-10-24 16:13:55 +03:00
|
|
|
pool, key := setupPool()
|
2023-06-16 15:29:40 +03:00
|
|
|
defer pool.Close()
|
2020-01-10 12:40:03 +03:00
|
|
|
|
|
|
|
account := crypto.PubkeyToAddress(key.PublicKey)
|
2021-05-28 11:28:07 +03:00
|
|
|
testAddBalance(pool, account, big.NewInt(1000000000))
|
2020-01-10 12:40:03 +03:00
|
|
|
|
|
|
|
// Compute maximal data size for transactions (lower bound).
|
|
|
|
//
|
|
|
|
// It is assumed the fields in the transaction (except of the data) are:
|
|
|
|
// - nonce <= 32 bytes
|
2023-06-06 12:53:29 +03:00
|
|
|
// - gasTip <= 32 bytes
|
2020-01-10 12:40:03 +03:00
|
|
|
// - gasLimit <= 32 bytes
|
|
|
|
// - recipient == 20 bytes
|
|
|
|
// - value <= 32 bytes
|
|
|
|
// - signature == 65 bytes
|
|
|
|
// All those fields are summed up to at most 213 bytes.
|
|
|
|
baseSize := uint64(213)
|
|
|
|
dataSize := txMaxSize - baseSize
|
|
|
|
// Try adding a transaction with maximal allowed size
|
2023-06-06 12:53:29 +03:00
|
|
|
tx := pricedDataTransaction(0, pool.currentHead.Load().GasLimit, big.NewInt(1), key, dataSize)
|
2020-01-10 12:40:03 +03:00
|
|
|
if err := pool.addRemoteSync(tx); err != nil {
|
|
|
|
t.Fatalf("failed to add transaction of size %d, close to maximal: %v", int(tx.Size()), err)
|
|
|
|
}
|
|
|
|
// Try adding a transaction with random allowed size
|
2023-06-06 12:53:29 +03:00
|
|
|
if err := pool.addRemoteSync(pricedDataTransaction(1, pool.currentHead.Load().GasLimit, big.NewInt(1), key, uint64(rand.Intn(int(dataSize))))); err != nil {
|
2020-01-10 12:40:03 +03:00
|
|
|
t.Fatalf("failed to add transaction of random allowed size: %v", err)
|
|
|
|
}
|
|
|
|
// Try adding a transaction of minimal not allowed size
|
2023-06-06 12:53:29 +03:00
|
|
|
if err := pool.addRemoteSync(pricedDataTransaction(2, pool.currentHead.Load().GasLimit, big.NewInt(1), key, txMaxSize)); err == nil {
|
2020-01-10 12:40:03 +03:00
|
|
|
t.Fatalf("expected rejection on slightly oversize transaction")
|
|
|
|
}
|
|
|
|
// Try adding a transaction of random not allowed size
|
2023-06-06 12:53:29 +03:00
|
|
|
if err := pool.addRemoteSync(pricedDataTransaction(2, pool.currentHead.Load().GasLimit, big.NewInt(1), key, dataSize+1+uint64(rand.Intn(10*txMaxSize)))); err == nil {
|
2020-01-10 12:40:03 +03:00
|
|
|
t.Fatalf("expected rejection on oversize transaction")
|
|
|
|
}
|
|
|
|
// Run some sanity checks on the pool internals
|
|
|
|
pending, queued := pool.Stats()
|
|
|
|
if pending != 2 {
|
|
|
|
t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 2)
|
|
|
|
}
|
|
|
|
if queued != 0 {
|
|
|
|
t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 0)
|
|
|
|
}
|
2022-10-24 16:13:55 +03:00
|
|
|
if err := validatePoolInternals(pool); err != nil {
|
2020-01-10 12:40:03 +03:00
|
|
|
t.Fatalf("pool internal state corrupted: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-04 12:48:36 +03:00
|
|
|
// Tests that if transactions start being capped, transactions are also removed from 'all'
|
2022-10-24 16:13:55 +03:00
|
|
|
func TestCapClearsFromAll(t *testing.T) {
|
2017-10-20 12:34:43 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2017-06-22 11:14:31 +03:00
|
|
|
// Create the pool to test the limit enforcement with
|
2023-05-11 10:19:42 +03:00
|
|
|
statedb, _ := state.New(types.EmptyRootHash, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
|
2023-06-16 15:29:40 +03:00
|
|
|
blockchain := newTestBlockChain(params.TestChainConfig, 1000000, statedb, new(event.Feed))
|
2017-06-22 11:14:31 +03:00
|
|
|
|
2017-07-28 16:09:39 +03:00
|
|
|
config := testTxPoolConfig
|
2017-07-06 11:51:59 +03:00
|
|
|
config.AccountSlots = 2
|
|
|
|
config.AccountQueue = 2
|
|
|
|
config.GlobalSlots = 8
|
|
|
|
|
2023-06-16 15:29:40 +03:00
|
|
|
pool := New(config, blockchain)
|
|
|
|
pool.Init(new(big.Int).SetUint64(config.PriceLimit), blockchain.CurrentBlock())
|
|
|
|
defer pool.Close()
|
2017-06-22 11:14:31 +03:00
|
|
|
|
|
|
|
// Create a number of test accounts and fund them
|
|
|
|
key, _ := crypto.GenerateKey()
|
|
|
|
addr := crypto.PubkeyToAddress(key.PublicKey)
|
2021-05-28 11:28:07 +03:00
|
|
|
testAddBalance(pool, addr, big.NewInt(1000000))
|
2017-06-22 11:14:31 +03:00
|
|
|
|
|
|
|
txs := types.Transactions{}
|
2017-07-06 11:51:59 +03:00
|
|
|
for j := 0; j < int(config.GlobalSlots)*2; j++ {
|
2017-11-13 14:47:27 +03:00
|
|
|
txs = append(txs, transaction(uint64(j), 100000, key))
|
2017-06-22 11:14:31 +03:00
|
|
|
}
|
|
|
|
// Import the batch and verify that limits have been enforced
|
2023-06-16 15:29:40 +03:00
|
|
|
pool.addRemotes(txs)
|
2022-10-24 16:13:55 +03:00
|
|
|
if err := validatePoolInternals(pool); err != nil {
|
2017-06-22 17:01:49 +03:00
|
|
|
t.Fatalf("pool internal state corrupted: %v", err)
|
2017-06-22 11:14:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-14 15:32:06 +03: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.
|
2022-10-24 16:13:55 +03:00
|
|
|
func TestPendingMinimumAllowance(t *testing.T) {
|
2017-10-20 12:34:43 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2016-10-14 15:32:06 +03:00
|
|
|
// Create the pool to test the limit enforcement with
|
2023-05-11 10:19:42 +03:00
|
|
|
statedb, _ := state.New(types.EmptyRootHash, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
|
2023-06-16 15:29:40 +03:00
|
|
|
blockchain := newTestBlockChain(params.TestChainConfig, 1000000, statedb, new(event.Feed))
|
2016-10-14 15:32:06 +03:00
|
|
|
|
2017-07-28 16:09:39 +03:00
|
|
|
config := testTxPoolConfig
|
2018-12-20 16:00:58 +03:00
|
|
|
config.GlobalSlots = 1
|
2017-07-06 11:51:59 +03:00
|
|
|
|
2023-06-16 15:29:40 +03:00
|
|
|
pool := New(config, blockchain)
|
|
|
|
pool.Init(new(big.Int).SetUint64(config.PriceLimit), blockchain.CurrentBlock())
|
|
|
|
defer pool.Close()
|
2016-10-14 15:32:06 +03:00
|
|
|
|
|
|
|
// Create a number of test accounts and fund them
|
|
|
|
keys := make([]*ecdsa.PrivateKey, 5)
|
|
|
|
for i := 0; i < len(keys); i++ {
|
|
|
|
keys[i], _ = crypto.GenerateKey()
|
2021-05-28 11:28:07 +03:00
|
|
|
testAddBalance(pool, crypto.PubkeyToAddress(keys[i].PublicKey), big.NewInt(1000000))
|
2016-10-14 15:32:06 +03:00
|
|
|
}
|
|
|
|
// 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 11:51:59 +03:00
|
|
|
for j := 0; j < int(config.AccountSlots)*2; j++ {
|
2017-11-13 14:47:27 +03:00
|
|
|
txs = append(txs, transaction(nonces[addr], 100000, key))
|
2016-10-14 15:32:06 +03:00
|
|
|
nonces[addr]++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Import the batch and verify that limits have been enforced
|
2023-06-16 15:29:40 +03:00
|
|
|
pool.addRemotesSync(txs)
|
2016-10-14 15:32:06 +03:00
|
|
|
|
|
|
|
for addr, list := range pool.pending {
|
2017-07-06 11:51:59 +03: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 15:32:06 +03:00
|
|
|
}
|
|
|
|
}
|
2022-10-24 16:13:55 +03:00
|
|
|
if err := validatePoolInternals(pool); err != nil {
|
2017-06-22 17:01:49 +03:00
|
|
|
t.Fatalf("pool internal state corrupted: %v", err)
|
|
|
|
}
|
2016-10-14 15:32:06 +03:00
|
|
|
}
|
|
|
|
|
2017-05-16 22:07:27 +03: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.
|
2022-10-24 16:13:55 +03:00
|
|
|
func TestRepricing(t *testing.T) {
|
2017-10-20 12:34:43 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2017-05-16 22:07:27 +03:00
|
|
|
// Create the pool to test the pricing enforcement with
|
2023-05-11 10:19:42 +03:00
|
|
|
statedb, _ := state.New(types.EmptyRootHash, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
|
2023-06-16 15:29:40 +03:00
|
|
|
blockchain := newTestBlockChain(params.TestChainConfig, 1000000, statedb, new(event.Feed))
|
2017-05-16 22:07:27 +03:00
|
|
|
|
2023-06-16 15:29:40 +03:00
|
|
|
pool := New(testTxPoolConfig, blockchain)
|
|
|
|
pool.Init(new(big.Int).SetUint64(testTxPoolConfig.PriceLimit), blockchain.CurrentBlock())
|
|
|
|
defer pool.Close()
|
2017-05-16 22:07:27 +03:00
|
|
|
|
2017-10-20 12:34:43 +03:00
|
|
|
// Keep track of transaction events to ensure all executables get announced
|
2022-10-24 16:13:55 +03:00
|
|
|
events := make(chan core.NewTxsEvent, 32)
|
2017-10-20 12:34:43 +03:00
|
|
|
sub := pool.txFeed.Subscribe(events)
|
|
|
|
defer sub.Unsubscribe()
|
|
|
|
|
2017-05-16 22:07:27 +03:00
|
|
|
// Create a number of test accounts and fund them
|
2018-03-07 11:55:59 +03:00
|
|
|
keys := make([]*ecdsa.PrivateKey, 4)
|
2017-05-16 22:07:27 +03:00
|
|
|
for i := 0; i < len(keys); i++ {
|
|
|
|
keys[i], _ = crypto.GenerateKey()
|
2021-05-28 11:28:07 +03:00
|
|
|
testAddBalance(pool, crypto.PubkeyToAddress(keys[i].PublicKey), big.NewInt(1000000))
|
2017-05-16 22:07:27 +03:00
|
|
|
}
|
|
|
|
// Generate and queue a batch of transactions, both pending and queued
|
|
|
|
txs := types.Transactions{}
|
|
|
|
|
2017-11-13 14:47:27 +03:00
|
|
|
txs = append(txs, pricedTransaction(0, 100000, big.NewInt(2), keys[0]))
|
|
|
|
txs = append(txs, pricedTransaction(1, 100000, big.NewInt(1), keys[0]))
|
|
|
|
txs = append(txs, pricedTransaction(2, 100000, big.NewInt(2), keys[0]))
|
2017-05-16 22:07:27 +03:00
|
|
|
|
2018-03-07 11:55:59 +03:00
|
|
|
txs = append(txs, pricedTransaction(0, 100000, big.NewInt(1), keys[1]))
|
2017-11-13 14:47:27 +03:00
|
|
|
txs = append(txs, pricedTransaction(1, 100000, big.NewInt(2), keys[1]))
|
2018-03-07 11:55:59 +03:00
|
|
|
txs = append(txs, pricedTransaction(2, 100000, big.NewInt(2), keys[1]))
|
|
|
|
|
|
|
|
txs = append(txs, pricedTransaction(1, 100000, big.NewInt(2), keys[2]))
|
|
|
|
txs = append(txs, pricedTransaction(2, 100000, big.NewInt(1), keys[2]))
|
|
|
|
txs = append(txs, pricedTransaction(3, 100000, big.NewInt(2), keys[2]))
|
2017-05-16 22:07:27 +03:00
|
|
|
|
2018-03-07 11:55:59 +03:00
|
|
|
ltx := pricedTransaction(0, 100000, big.NewInt(1), keys[3])
|
2017-05-16 22:07:27 +03:00
|
|
|
|
|
|
|
// Import the batch and that both pending and queued transactions match up
|
2023-06-16 15:29:40 +03:00
|
|
|
pool.addRemotesSync(txs)
|
|
|
|
pool.addLocal(ltx)
|
2017-05-16 22:07:27 +03:00
|
|
|
|
2017-08-08 11:59:34 +03:00
|
|
|
pending, queued := pool.Stats()
|
2018-03-07 11:55:59 +03:00
|
|
|
if pending != 7 {
|
|
|
|
t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 7)
|
2017-05-16 22:07:27 +03:00
|
|
|
}
|
|
|
|
if queued != 3 {
|
|
|
|
t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 3)
|
|
|
|
}
|
2018-03-07 11:55:59 +03:00
|
|
|
if err := validateEvents(events, 7); err != nil {
|
2017-10-20 12:34:43 +03:00
|
|
|
t.Fatalf("original event firing failed: %v", err)
|
|
|
|
}
|
2022-10-24 16:13:55 +03:00
|
|
|
if err := validatePoolInternals(pool); err != nil {
|
2017-06-22 17:01:49 +03:00
|
|
|
t.Fatalf("pool internal state corrupted: %v", err)
|
|
|
|
}
|
2017-05-16 22:07:27 +03:00
|
|
|
// Reprice the pool and check that underpriced transactions get dropped
|
2023-06-06 12:53:29 +03:00
|
|
|
pool.SetGasTip(big.NewInt(2))
|
2017-05-16 22:07:27 +03:00
|
|
|
|
2017-08-08 11:59:34 +03:00
|
|
|
pending, queued = pool.Stats()
|
2017-05-16 22:07:27 +03:00
|
|
|
if pending != 2 {
|
|
|
|
t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 2)
|
|
|
|
}
|
2018-03-07 11:55:59 +03:00
|
|
|
if queued != 5 {
|
|
|
|
t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 5)
|
2017-05-16 22:07:27 +03:00
|
|
|
}
|
2017-10-20 12:34:43 +03:00
|
|
|
if err := validateEvents(events, 0); err != nil {
|
|
|
|
t.Fatalf("reprice event firing failed: %v", err)
|
|
|
|
}
|
2022-10-24 16:13:55 +03:00
|
|
|
if err := validatePoolInternals(pool); err != nil {
|
2017-06-22 17:01:49 +03:00
|
|
|
t.Fatalf("pool internal state corrupted: %v", err)
|
|
|
|
}
|
2017-05-16 22:07:27 +03:00
|
|
|
// Check that we can't add the old transactions back
|
2023-06-16 15:29:40 +03:00
|
|
|
if err := pool.addRemote(pricedTransaction(1, 100000, big.NewInt(1), keys[0])); !errors.Is(err, txpool.ErrUnderpriced) {
|
|
|
|
t.Fatalf("adding underpriced pending transaction error mismatch: have %v, want %v", err, txpool.ErrUnderpriced)
|
2017-05-16 22:07:27 +03:00
|
|
|
}
|
2023-06-16 15:29:40 +03:00
|
|
|
if err := pool.addRemote(pricedTransaction(0, 100000, big.NewInt(1), keys[1])); !errors.Is(err, txpool.ErrUnderpriced) {
|
|
|
|
t.Fatalf("adding underpriced pending transaction error mismatch: have %v, want %v", err, txpool.ErrUnderpriced)
|
2018-03-07 11:55:59 +03:00
|
|
|
}
|
2023-06-16 15:29:40 +03:00
|
|
|
if err := pool.addRemote(pricedTransaction(2, 100000, big.NewInt(1), keys[2])); !errors.Is(err, txpool.ErrUnderpriced) {
|
|
|
|
t.Fatalf("adding underpriced queued transaction error mismatch: have %v, want %v", err, txpool.ErrUnderpriced)
|
2017-05-16 22:07:27 +03:00
|
|
|
}
|
2017-10-20 12:34:43 +03:00
|
|
|
if err := validateEvents(events, 0); err != nil {
|
|
|
|
t.Fatalf("post-reprice event firing failed: %v", err)
|
|
|
|
}
|
2022-10-24 16:13:55 +03:00
|
|
|
if err := validatePoolInternals(pool); err != nil {
|
2017-06-22 17:01:49 +03:00
|
|
|
t.Fatalf("pool internal state corrupted: %v", err)
|
|
|
|
}
|
2017-05-16 22:07:27 +03:00
|
|
|
// However we can add local underpriced transactions
|
2018-03-07 11:55:59 +03:00
|
|
|
tx := pricedTransaction(1, 100000, big.NewInt(1), keys[3])
|
2023-06-16 15:29:40 +03:00
|
|
|
if err := pool.addLocal(tx); err != nil {
|
2017-05-16 22:07:27 +03:00
|
|
|
t.Fatalf("failed to add underpriced local transaction: %v", err)
|
|
|
|
}
|
2017-08-08 11:59:34 +03:00
|
|
|
if pending, _ = pool.Stats(); pending != 3 {
|
2017-05-16 22:07:27 +03:00
|
|
|
t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 3)
|
|
|
|
}
|
2017-10-20 12:34:43 +03:00
|
|
|
if err := validateEvents(events, 1); err != nil {
|
|
|
|
t.Fatalf("post-reprice local event firing failed: %v", err)
|
|
|
|
}
|
2022-10-24 16:13:55 +03:00
|
|
|
if err := validatePoolInternals(pool); err != nil {
|
2017-06-22 17:01:49 +03:00
|
|
|
t.Fatalf("pool internal state corrupted: %v", err)
|
2017-06-22 11:14:31 +03:00
|
|
|
}
|
2018-03-07 11:55:59 +03:00
|
|
|
// And we can fill gaps with properly priced transactions
|
2023-06-16 15:29:40 +03:00
|
|
|
if err := pool.addRemote(pricedTransaction(1, 100000, big.NewInt(2), keys[0])); err != nil {
|
2018-03-07 11:55:59 +03:00
|
|
|
t.Fatalf("failed to add pending transaction: %v", err)
|
|
|
|
}
|
2023-06-16 15:29:40 +03:00
|
|
|
if err := pool.addRemote(pricedTransaction(0, 100000, big.NewInt(2), keys[1])); err != nil {
|
2018-03-07 11:55:59 +03:00
|
|
|
t.Fatalf("failed to add pending transaction: %v", err)
|
|
|
|
}
|
2023-07-06 19:05:34 +03:00
|
|
|
if err := pool.addRemoteSync(pricedTransaction(2, 100000, big.NewInt(2), keys[2])); err != nil {
|
2018-03-07 11:55:59 +03:00
|
|
|
t.Fatalf("failed to add queued transaction: %v", err)
|
|
|
|
}
|
|
|
|
if err := validateEvents(events, 5); err != nil {
|
|
|
|
t.Fatalf("post-reprice event firing failed: %v", err)
|
|
|
|
}
|
2022-10-24 16:13:55 +03:00
|
|
|
if err := validatePoolInternals(pool); err != nil {
|
2018-03-07 11:55:59 +03:00
|
|
|
t.Fatalf("pool internal state corrupted: %v", err)
|
|
|
|
}
|
2017-05-16 22:07:27 +03:00
|
|
|
}
|
|
|
|
|
2021-05-28 11:28:07 +03:00
|
|
|
// Tests that setting the transaction pool gas price to a higher value correctly
|
|
|
|
// discards everything cheaper (legacy & dynamic fee) than that and moves any
|
|
|
|
// gapped transactions back from the pending pool to the queue.
|
|
|
|
//
|
|
|
|
// Note, local transactions are never allowed to be dropped.
|
2022-10-24 16:13:55 +03:00
|
|
|
func TestRepricingDynamicFee(t *testing.T) {
|
2021-05-28 11:28:07 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
// Create the pool to test the pricing enforcement with
|
2022-10-24 16:13:55 +03:00
|
|
|
pool, _ := setupPoolWithConfig(eip1559Config)
|
2023-06-16 15:29:40 +03:00
|
|
|
defer pool.Close()
|
2021-05-28 11:28:07 +03:00
|
|
|
|
|
|
|
// Keep track of transaction events to ensure all executables get announced
|
2022-10-24 16:13:55 +03:00
|
|
|
events := make(chan core.NewTxsEvent, 32)
|
2021-05-28 11:28:07 +03:00
|
|
|
sub := pool.txFeed.Subscribe(events)
|
|
|
|
defer sub.Unsubscribe()
|
|
|
|
|
|
|
|
// Create a number of test accounts and fund them
|
|
|
|
keys := make([]*ecdsa.PrivateKey, 4)
|
|
|
|
for i := 0; i < len(keys); i++ {
|
|
|
|
keys[i], _ = crypto.GenerateKey()
|
|
|
|
testAddBalance(pool, 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, 100000, big.NewInt(2), keys[0]))
|
|
|
|
txs = append(txs, pricedTransaction(1, 100000, big.NewInt(1), keys[0]))
|
|
|
|
txs = append(txs, pricedTransaction(2, 100000, big.NewInt(2), keys[0]))
|
|
|
|
|
|
|
|
txs = append(txs, dynamicFeeTx(0, 100000, big.NewInt(2), big.NewInt(1), keys[1]))
|
|
|
|
txs = append(txs, dynamicFeeTx(1, 100000, big.NewInt(3), big.NewInt(2), keys[1]))
|
|
|
|
txs = append(txs, dynamicFeeTx(2, 100000, big.NewInt(3), big.NewInt(2), keys[1]))
|
|
|
|
|
|
|
|
txs = append(txs, dynamicFeeTx(1, 100000, big.NewInt(2), big.NewInt(2), keys[2]))
|
|
|
|
txs = append(txs, dynamicFeeTx(2, 100000, big.NewInt(1), big.NewInt(1), keys[2]))
|
|
|
|
txs = append(txs, dynamicFeeTx(3, 100000, big.NewInt(2), big.NewInt(2), keys[2]))
|
|
|
|
|
|
|
|
ltx := dynamicFeeTx(0, 100000, big.NewInt(2), big.NewInt(1), keys[3])
|
|
|
|
|
|
|
|
// Import the batch and that both pending and queued transactions match up
|
2023-06-16 15:29:40 +03:00
|
|
|
pool.addRemotesSync(txs)
|
|
|
|
pool.addLocal(ltx)
|
2021-05-28 11:28:07 +03:00
|
|
|
|
|
|
|
pending, queued := pool.Stats()
|
|
|
|
if pending != 7 {
|
|
|
|
t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 7)
|
|
|
|
}
|
|
|
|
if queued != 3 {
|
|
|
|
t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 3)
|
|
|
|
}
|
|
|
|
if err := validateEvents(events, 7); err != nil {
|
|
|
|
t.Fatalf("original event firing failed: %v", err)
|
|
|
|
}
|
2022-10-24 16:13:55 +03:00
|
|
|
if err := validatePoolInternals(pool); err != nil {
|
2021-05-28 11:28:07 +03:00
|
|
|
t.Fatalf("pool internal state corrupted: %v", err)
|
|
|
|
}
|
|
|
|
// Reprice the pool and check that underpriced transactions get dropped
|
2023-06-06 12:53:29 +03:00
|
|
|
pool.SetGasTip(big.NewInt(2))
|
2021-05-28 11:28:07 +03:00
|
|
|
|
|
|
|
pending, queued = pool.Stats()
|
|
|
|
if pending != 2 {
|
|
|
|
t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 2)
|
|
|
|
}
|
|
|
|
if queued != 5 {
|
|
|
|
t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 5)
|
|
|
|
}
|
|
|
|
if err := validateEvents(events, 0); err != nil {
|
|
|
|
t.Fatalf("reprice event firing failed: %v", err)
|
|
|
|
}
|
2022-10-24 16:13:55 +03:00
|
|
|
if err := validatePoolInternals(pool); err != nil {
|
2021-05-28 11:28:07 +03:00
|
|
|
t.Fatalf("pool internal state corrupted: %v", err)
|
|
|
|
}
|
|
|
|
// Check that we can't add the old transactions back
|
|
|
|
tx := pricedTransaction(1, 100000, big.NewInt(1), keys[0])
|
2023-06-16 15:29:40 +03:00
|
|
|
if err := pool.addRemote(tx); !errors.Is(err, txpool.ErrUnderpriced) {
|
|
|
|
t.Fatalf("adding underpriced pending transaction error mismatch: have %v, want %v", err, txpool.ErrUnderpriced)
|
2021-05-28 11:28:07 +03:00
|
|
|
}
|
|
|
|
tx = dynamicFeeTx(0, 100000, big.NewInt(2), big.NewInt(1), keys[1])
|
2023-06-16 15:29:40 +03:00
|
|
|
if err := pool.addRemote(tx); !errors.Is(err, txpool.ErrUnderpriced) {
|
|
|
|
t.Fatalf("adding underpriced pending transaction error mismatch: have %v, want %v", err, txpool.ErrUnderpriced)
|
2021-05-28 11:28:07 +03:00
|
|
|
}
|
|
|
|
tx = dynamicFeeTx(2, 100000, big.NewInt(1), big.NewInt(1), keys[2])
|
2023-06-16 15:29:40 +03:00
|
|
|
if err := pool.addRemote(tx); !errors.Is(err, txpool.ErrUnderpriced) {
|
|
|
|
t.Fatalf("adding underpriced queued transaction error mismatch: have %v, want %v", err, txpool.ErrUnderpriced)
|
2021-05-28 11:28:07 +03:00
|
|
|
}
|
|
|
|
if err := validateEvents(events, 0); err != nil {
|
|
|
|
t.Fatalf("post-reprice event firing failed: %v", err)
|
|
|
|
}
|
2022-10-24 16:13:55 +03:00
|
|
|
if err := validatePoolInternals(pool); err != nil {
|
2021-05-28 11:28:07 +03:00
|
|
|
t.Fatalf("pool internal state corrupted: %v", err)
|
|
|
|
}
|
|
|
|
// However we can add local underpriced transactions
|
|
|
|
tx = dynamicFeeTx(1, 100000, big.NewInt(1), big.NewInt(1), keys[3])
|
2023-06-16 15:29:40 +03:00
|
|
|
if err := pool.addLocal(tx); err != nil {
|
2021-05-28 11:28:07 +03:00
|
|
|
t.Fatalf("failed to add underpriced local transaction: %v", err)
|
|
|
|
}
|
|
|
|
if pending, _ = pool.Stats(); pending != 3 {
|
|
|
|
t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 3)
|
|
|
|
}
|
|
|
|
if err := validateEvents(events, 1); err != nil {
|
|
|
|
t.Fatalf("post-reprice local event firing failed: %v", err)
|
|
|
|
}
|
2022-10-24 16:13:55 +03:00
|
|
|
if err := validatePoolInternals(pool); err != nil {
|
2021-05-28 11:28:07 +03:00
|
|
|
t.Fatalf("pool internal state corrupted: %v", err)
|
|
|
|
}
|
|
|
|
// And we can fill gaps with properly priced transactions
|
|
|
|
tx = pricedTransaction(1, 100000, big.NewInt(2), keys[0])
|
2023-06-16 15:29:40 +03:00
|
|
|
if err := pool.addRemote(tx); err != nil {
|
2021-05-28 11:28:07 +03:00
|
|
|
t.Fatalf("failed to add pending transaction: %v", err)
|
|
|
|
}
|
|
|
|
tx = dynamicFeeTx(0, 100000, big.NewInt(3), big.NewInt(2), keys[1])
|
2023-06-16 15:29:40 +03:00
|
|
|
if err := pool.addRemote(tx); err != nil {
|
2021-05-28 11:28:07 +03:00
|
|
|
t.Fatalf("failed to add pending transaction: %v", err)
|
|
|
|
}
|
|
|
|
tx = dynamicFeeTx(2, 100000, big.NewInt(2), big.NewInt(2), keys[2])
|
2023-07-06 19:05:34 +03:00
|
|
|
if err := pool.addRemoteSync(tx); err != nil {
|
2021-05-28 11:28:07 +03:00
|
|
|
t.Fatalf("failed to add queued transaction: %v", err)
|
|
|
|
}
|
|
|
|
if err := validateEvents(events, 5); err != nil {
|
|
|
|
t.Fatalf("post-reprice event firing failed: %v", err)
|
|
|
|
}
|
2022-10-24 16:13:55 +03:00
|
|
|
if err := validatePoolInternals(pool); err != nil {
|
2021-05-28 11:28:07 +03:00
|
|
|
t.Fatalf("pool internal state corrupted: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-04 12:48:36 +03:00
|
|
|
// Tests that setting the transaction pool gas price to a higher value does not
|
2021-05-28 11:28:07 +03:00
|
|
|
// remove local transactions (legacy & dynamic fee).
|
2022-10-24 16:13:55 +03:00
|
|
|
func TestRepricingKeepsLocals(t *testing.T) {
|
2017-10-20 12:34:43 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2017-09-04 12:48:36 +03:00
|
|
|
// Create the pool to test the pricing enforcement with
|
2023-05-11 10:19:42 +03:00
|
|
|
statedb, _ := state.New(types.EmptyRootHash, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
|
2023-06-16 15:29:40 +03:00
|
|
|
blockchain := newTestBlockChain(eip1559Config, 1000000, statedb, new(event.Feed))
|
2017-09-04 12:48:36 +03:00
|
|
|
|
2023-06-16 15:29:40 +03:00
|
|
|
pool := New(testTxPoolConfig, blockchain)
|
|
|
|
pool.Init(new(big.Int).SetUint64(testTxPoolConfig.PriceLimit), blockchain.CurrentBlock())
|
|
|
|
defer pool.Close()
|
2017-09-04 12:48:36 +03:00
|
|
|
|
|
|
|
// Create a number of test accounts and fund them
|
|
|
|
keys := make([]*ecdsa.PrivateKey, 3)
|
|
|
|
for i := 0; i < len(keys); i++ {
|
|
|
|
keys[i], _ = crypto.GenerateKey()
|
2023-03-10 20:30:26 +03:00
|
|
|
testAddBalance(pool, crypto.PubkeyToAddress(keys[i].PublicKey), big.NewInt(100000*1000000))
|
2017-09-04 12:48:36 +03:00
|
|
|
}
|
|
|
|
// Create transaction (both pending and queued) with a linearly growing gasprice
|
|
|
|
for i := uint64(0); i < 500; i++ {
|
2019-02-14 22:14:05 +03:00
|
|
|
// Add pending transaction.
|
|
|
|
pendingTx := pricedTransaction(i, 100000, big.NewInt(int64(i)), keys[2])
|
2023-06-16 15:29:40 +03:00
|
|
|
if err := pool.addLocal(pendingTx); err != nil {
|
2017-09-04 12:48:36 +03:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-02-14 22:14:05 +03:00
|
|
|
// Add queued transaction.
|
|
|
|
queuedTx := pricedTransaction(i+501, 100000, big.NewInt(int64(i)), keys[2])
|
2023-06-16 15:29:40 +03:00
|
|
|
if err := pool.addLocal(queuedTx); err != nil {
|
2017-09-04 12:48:36 +03:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-05-28 11:28:07 +03:00
|
|
|
|
|
|
|
// Add pending dynamic fee transaction.
|
|
|
|
pendingTx = dynamicFeeTx(i, 100000, big.NewInt(int64(i)+1), big.NewInt(int64(i)), keys[1])
|
2023-06-16 15:29:40 +03:00
|
|
|
if err := pool.addLocal(pendingTx); err != nil {
|
2021-05-28 11:28:07 +03:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// Add queued dynamic fee transaction.
|
|
|
|
queuedTx = dynamicFeeTx(i+501, 100000, big.NewInt(int64(i)+1), big.NewInt(int64(i)), keys[1])
|
2023-06-16 15:29:40 +03:00
|
|
|
if err := pool.addLocal(queuedTx); err != nil {
|
2021-05-28 11:28:07 +03:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-09-04 12:48:36 +03:00
|
|
|
}
|
|
|
|
pending, queued := pool.Stats()
|
2021-05-28 11:28:07 +03:00
|
|
|
expPending, expQueued := 1000, 1000
|
2017-09-04 12:48:36 +03:00
|
|
|
validate := func() {
|
|
|
|
pending, queued = pool.Stats()
|
|
|
|
if pending != expPending {
|
|
|
|
t.Fatalf("pending transactions mismatched: have %d, want %d", pending, expPending)
|
|
|
|
}
|
|
|
|
if queued != expQueued {
|
|
|
|
t.Fatalf("queued transactions mismatched: have %d, want %d", queued, expQueued)
|
|
|
|
}
|
|
|
|
|
2022-10-24 16:13:55 +03:00
|
|
|
if err := validatePoolInternals(pool); err != nil {
|
2017-09-04 12:48:36 +03:00
|
|
|
t.Fatalf("pool internal state corrupted: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
validate()
|
2017-09-04 22:35:00 +03:00
|
|
|
|
2017-09-04 12:48:36 +03:00
|
|
|
// Reprice the pool and check that nothing is dropped
|
2023-06-06 12:53:29 +03:00
|
|
|
pool.SetGasTip(big.NewInt(2))
|
2017-09-04 12:48:36 +03:00
|
|
|
validate()
|
2017-09-04 22:35:00 +03:00
|
|
|
|
2023-06-06 12:53:29 +03:00
|
|
|
pool.SetGasTip(big.NewInt(2))
|
|
|
|
pool.SetGasTip(big.NewInt(4))
|
|
|
|
pool.SetGasTip(big.NewInt(8))
|
|
|
|
pool.SetGasTip(big.NewInt(100))
|
2017-09-04 12:48:36 +03:00
|
|
|
validate()
|
|
|
|
}
|
|
|
|
|
2017-05-16 22:07:27 +03:00
|
|
|
// Tests that when the pool reaches its global transaction limit, underpriced
|
|
|
|
// transactions are gradually shifted out for more expensive ones and any gapped
|
2017-11-24 12:20:01 +03:00
|
|
|
// pending transactions are moved into the queue.
|
2017-05-16 22:07:27 +03:00
|
|
|
//
|
|
|
|
// Note, local transactions are never allowed to be dropped.
|
2022-10-24 16:13:55 +03:00
|
|
|
func TestUnderpricing(t *testing.T) {
|
2017-10-20 12:34:43 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2017-05-16 22:07:27 +03:00
|
|
|
// Create the pool to test the pricing enforcement with
|
2023-05-11 10:19:42 +03:00
|
|
|
statedb, _ := state.New(types.EmptyRootHash, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
|
2023-06-16 15:29:40 +03:00
|
|
|
blockchain := newTestBlockChain(params.TestChainConfig, 1000000, statedb, new(event.Feed))
|
2017-05-16 22:07:27 +03:00
|
|
|
|
2017-07-28 16:09:39 +03:00
|
|
|
config := testTxPoolConfig
|
2017-07-06 11:51:59 +03:00
|
|
|
config.GlobalSlots = 2
|
|
|
|
config.GlobalQueue = 2
|
|
|
|
|
2023-06-16 15:29:40 +03:00
|
|
|
pool := New(config, blockchain)
|
|
|
|
pool.Init(new(big.Int).SetUint64(config.PriceLimit), blockchain.CurrentBlock())
|
|
|
|
defer pool.Close()
|
2017-05-16 22:07:27 +03:00
|
|
|
|
2017-10-20 12:34:43 +03:00
|
|
|
// Keep track of transaction events to ensure all executables get announced
|
2022-10-24 16:13:55 +03:00
|
|
|
events := make(chan core.NewTxsEvent, 32)
|
2017-10-20 12:34:43 +03:00
|
|
|
sub := pool.txFeed.Subscribe(events)
|
|
|
|
defer sub.Unsubscribe()
|
|
|
|
|
2017-05-16 22:07:27 +03:00
|
|
|
// Create a number of test accounts and fund them
|
2023-03-10 20:30:26 +03:00
|
|
|
keys := make([]*ecdsa.PrivateKey, 5)
|
2017-05-16 22:07:27 +03:00
|
|
|
for i := 0; i < len(keys); i++ {
|
|
|
|
keys[i], _ = crypto.GenerateKey()
|
2021-05-28 11:28:07 +03:00
|
|
|
testAddBalance(pool, crypto.PubkeyToAddress(keys[i].PublicKey), big.NewInt(1000000))
|
2017-05-16 22:07:27 +03:00
|
|
|
}
|
|
|
|
// Generate and queue a batch of transactions, both pending and queued
|
|
|
|
txs := types.Transactions{}
|
|
|
|
|
2017-11-13 14:47:27 +03:00
|
|
|
txs = append(txs, pricedTransaction(0, 100000, big.NewInt(1), keys[0]))
|
|
|
|
txs = append(txs, pricedTransaction(1, 100000, big.NewInt(2), keys[0]))
|
2017-05-16 22:07:27 +03:00
|
|
|
|
2017-11-13 14:47:27 +03:00
|
|
|
txs = append(txs, pricedTransaction(1, 100000, big.NewInt(1), keys[1]))
|
2017-05-16 22:07:27 +03:00
|
|
|
|
2017-11-13 14:47:27 +03:00
|
|
|
ltx := pricedTransaction(0, 100000, big.NewInt(1), keys[2])
|
2017-05-16 22:07:27 +03:00
|
|
|
|
|
|
|
// Import the batch and that both pending and queued transactions match up
|
2023-06-16 15:29:40 +03:00
|
|
|
pool.addRemotes(txs)
|
|
|
|
pool.addLocal(ltx)
|
2017-05-16 22:07:27 +03:00
|
|
|
|
2017-08-08 11:59:34 +03:00
|
|
|
pending, queued := pool.Stats()
|
2017-05-16 22:07:27 +03: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-10-20 12:34:43 +03:00
|
|
|
if err := validateEvents(events, 3); err != nil {
|
|
|
|
t.Fatalf("original event firing failed: %v", err)
|
|
|
|
}
|
2022-10-24 16:13:55 +03:00
|
|
|
if err := validatePoolInternals(pool); err != nil {
|
2017-06-22 17:01:49 +03:00
|
|
|
t.Fatalf("pool internal state corrupted: %v", err)
|
|
|
|
}
|
2017-05-16 22:07:27 +03:00
|
|
|
// Ensure that adding an underpriced transaction on block limit fails
|
2023-07-06 19:05:34 +03:00
|
|
|
if err := pool.addRemoteSync(pricedTransaction(0, 100000, big.NewInt(1), keys[1])); !errors.Is(err, txpool.ErrUnderpriced) {
|
2023-06-16 15:29:40 +03:00
|
|
|
t.Fatalf("adding underpriced pending transaction error mismatch: have %v, want %v", err, txpool.ErrUnderpriced)
|
2017-05-16 22:07:27 +03:00
|
|
|
}
|
2023-03-10 20:30:26 +03:00
|
|
|
// Replace a future transaction with a future transaction
|
2023-07-06 19:05:34 +03:00
|
|
|
if err := pool.addRemoteSync(pricedTransaction(1, 100000, big.NewInt(2), keys[1])); err != nil { // +K1:1 => -K1:1 => Pend K0:0, K0:1, K2:0; Que K1:1
|
2023-03-10 20:30:26 +03:00
|
|
|
t.Fatalf("failed to add well priced transaction: %v", err)
|
|
|
|
}
|
2017-05-16 22:07:27 +03:00
|
|
|
// Ensure that adding high priced transactions drops cheap ones, but not own
|
2023-07-06 19:05:34 +03:00
|
|
|
if err := pool.addRemoteSync(pricedTransaction(0, 100000, big.NewInt(3), keys[1])); err != nil { // +K1:0 => -K1:1 => Pend K0:0, K0:1, K1:0, K2:0; Que -
|
2017-05-16 22:07:27 +03:00
|
|
|
t.Fatalf("failed to add well priced transaction: %v", err)
|
|
|
|
}
|
2023-07-06 19:05:34 +03:00
|
|
|
if err := pool.addRemoteSync(pricedTransaction(2, 100000, big.NewInt(4), keys[1])); err != nil { // +K1:2 => -K0:0 => Pend K1:0, K2:0; Que K0:1 K1:2
|
2017-05-16 22:07:27 +03:00
|
|
|
t.Fatalf("failed to add well priced transaction: %v", err)
|
|
|
|
}
|
2023-06-16 15:29:40 +03:00
|
|
|
if err := pool.addRemote(pricedTransaction(3, 100000, big.NewInt(5), keys[1])); err != nil { // +K1:3 => -K0:1 => Pend K1:0, K2:0; Que K1:2 K1:3
|
2017-05-16 22:07:27 +03:00
|
|
|
t.Fatalf("failed to add well priced transaction: %v", err)
|
|
|
|
}
|
2023-03-10 20:30:26 +03:00
|
|
|
// Ensure that replacing a pending transaction with a future transaction fails
|
2023-06-16 15:29:40 +03:00
|
|
|
if err := pool.addRemote(pricedTransaction(5, 100000, big.NewInt(6), keys[1])); err != txpool.ErrFutureReplacePending {
|
|
|
|
t.Fatalf("adding future replace transaction error mismatch: have %v, want %v", err, txpool.ErrFutureReplacePending)
|
2023-03-10 20:30:26 +03:00
|
|
|
}
|
2017-08-08 11:59:34 +03:00
|
|
|
pending, queued = pool.Stats()
|
2017-05-16 22:07:27 +03: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)
|
|
|
|
}
|
2023-03-10 20:30:26 +03:00
|
|
|
if err := validateEvents(events, 2); err != nil {
|
2017-10-20 12:34:43 +03:00
|
|
|
t.Fatalf("additional event firing failed: %v", err)
|
|
|
|
}
|
2022-10-24 16:13:55 +03:00
|
|
|
if err := validatePoolInternals(pool); err != nil {
|
2017-06-22 17:01:49 +03:00
|
|
|
t.Fatalf("pool internal state corrupted: %v", err)
|
|
|
|
}
|
2017-05-16 22:07:27 +03:00
|
|
|
// Ensure that adding local transactions can push out even higher priced ones
|
2018-04-27 02:43:18 +03:00
|
|
|
ltx = pricedTransaction(1, 100000, big.NewInt(0), keys[2])
|
2023-06-16 15:29:40 +03:00
|
|
|
if err := pool.addLocal(ltx); err != nil {
|
2018-04-27 02:43:18 +03:00
|
|
|
t.Fatalf("failed to append underpriced local transaction: %v", err)
|
|
|
|
}
|
|
|
|
ltx = pricedTransaction(0, 100000, big.NewInt(0), keys[3])
|
2023-06-16 15:29:40 +03:00
|
|
|
if err := pool.addLocal(ltx); err != nil {
|
2018-04-27 02:43:18 +03:00
|
|
|
t.Fatalf("failed to add new underpriced local transaction: %v", err)
|
2017-05-16 22:07:27 +03:00
|
|
|
}
|
2017-08-08 11:59:34 +03:00
|
|
|
pending, queued = pool.Stats()
|
2018-04-27 02:43:18 +03:00
|
|
|
if pending != 3 {
|
|
|
|
t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 3)
|
2017-05-16 22:07:27 +03:00
|
|
|
}
|
2018-04-27 02:43:18 +03:00
|
|
|
if queued != 1 {
|
|
|
|
t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 1)
|
2017-05-16 22:07:27 +03:00
|
|
|
}
|
2018-04-27 02:43:18 +03:00
|
|
|
if err := validateEvents(events, 2); err != nil {
|
2017-10-20 12:34:43 +03:00
|
|
|
t.Fatalf("local event firing failed: %v", err)
|
|
|
|
}
|
2022-10-24 16:13:55 +03:00
|
|
|
if err := validatePoolInternals(pool); err != nil {
|
2017-06-22 17:01:49 +03:00
|
|
|
t.Fatalf("pool internal state corrupted: %v", err)
|
2017-06-22 11:14:31 +03:00
|
|
|
}
|
2017-05-16 22:07:27 +03:00
|
|
|
}
|
|
|
|
|
2018-04-12 12:17:52 +03:00
|
|
|
// Tests that more expensive transactions push out cheap ones from the pool, but
|
|
|
|
// without producing instability by creating gaps that start jumping transactions
|
|
|
|
// back and forth between queued/pending.
|
2022-10-24 16:13:55 +03:00
|
|
|
func TestStableUnderpricing(t *testing.T) {
|
2018-04-12 12:17:52 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
// Create the pool to test the pricing enforcement with
|
2023-05-11 10:19:42 +03:00
|
|
|
statedb, _ := state.New(types.EmptyRootHash, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
|
2023-06-16 15:29:40 +03:00
|
|
|
blockchain := newTestBlockChain(params.TestChainConfig, 1000000, statedb, new(event.Feed))
|
2018-04-12 12:17:52 +03:00
|
|
|
|
|
|
|
config := testTxPoolConfig
|
|
|
|
config.GlobalSlots = 128
|
|
|
|
config.GlobalQueue = 0
|
|
|
|
|
2023-06-16 15:29:40 +03:00
|
|
|
pool := New(config, blockchain)
|
|
|
|
pool.Init(new(big.Int).SetUint64(config.PriceLimit), blockchain.CurrentBlock())
|
|
|
|
defer pool.Close()
|
2018-04-12 12:17:52 +03:00
|
|
|
|
|
|
|
// Keep track of transaction events to ensure all executables get announced
|
2022-10-24 16:13:55 +03:00
|
|
|
events := make(chan core.NewTxsEvent, 32)
|
2018-04-12 12:17:52 +03:00
|
|
|
sub := pool.txFeed.Subscribe(events)
|
|
|
|
defer sub.Unsubscribe()
|
|
|
|
|
|
|
|
// Create a number of test accounts and fund them
|
|
|
|
keys := make([]*ecdsa.PrivateKey, 2)
|
|
|
|
for i := 0; i < len(keys); i++ {
|
|
|
|
keys[i], _ = crypto.GenerateKey()
|
2021-05-28 11:28:07 +03:00
|
|
|
testAddBalance(pool, crypto.PubkeyToAddress(keys[i].PublicKey), big.NewInt(1000000))
|
2018-04-12 12:17:52 +03:00
|
|
|
}
|
|
|
|
// Fill up the entire queue with the same transaction price points
|
|
|
|
txs := types.Transactions{}
|
|
|
|
for i := uint64(0); i < config.GlobalSlots; i++ {
|
|
|
|
txs = append(txs, pricedTransaction(i, 100000, big.NewInt(1), keys[0]))
|
|
|
|
}
|
2023-06-16 15:29:40 +03:00
|
|
|
pool.addRemotesSync(txs)
|
2018-04-12 12:17:52 +03:00
|
|
|
|
|
|
|
pending, queued := pool.Stats()
|
|
|
|
if pending != int(config.GlobalSlots) {
|
|
|
|
t.Fatalf("pending transactions mismatched: have %d, want %d", pending, config.GlobalSlots)
|
|
|
|
}
|
|
|
|
if queued != 0 {
|
|
|
|
t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 0)
|
|
|
|
}
|
|
|
|
if err := validateEvents(events, int(config.GlobalSlots)); err != nil {
|
|
|
|
t.Fatalf("original event firing failed: %v", err)
|
|
|
|
}
|
2022-10-24 16:13:55 +03:00
|
|
|
if err := validatePoolInternals(pool); err != nil {
|
2018-04-12 12:17:52 +03:00
|
|
|
t.Fatalf("pool internal state corrupted: %v", err)
|
|
|
|
}
|
|
|
|
// Ensure that adding high priced transactions drops a cheap, but doesn't produce a gap
|
2019-06-21 11:29:14 +03:00
|
|
|
if err := pool.addRemoteSync(pricedTransaction(0, 100000, big.NewInt(3), keys[1])); err != nil {
|
2018-04-12 12:17:52 +03:00
|
|
|
t.Fatalf("failed to add well priced transaction: %v", err)
|
|
|
|
}
|
|
|
|
pending, queued = pool.Stats()
|
|
|
|
if pending != int(config.GlobalSlots) {
|
|
|
|
t.Fatalf("pending transactions mismatched: have %d, want %d", pending, config.GlobalSlots)
|
|
|
|
}
|
|
|
|
if queued != 0 {
|
|
|
|
t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 0)
|
|
|
|
}
|
|
|
|
if err := validateEvents(events, 1); err != nil {
|
|
|
|
t.Fatalf("additional event firing failed: %v", err)
|
|
|
|
}
|
2022-10-24 16:13:55 +03:00
|
|
|
if err := validatePoolInternals(pool); err != nil {
|
2018-04-12 12:17:52 +03:00
|
|
|
t.Fatalf("pool internal state corrupted: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-28 11:28:07 +03:00
|
|
|
// Tests that when the pool reaches its global transaction limit, underpriced
|
|
|
|
// transactions (legacy & dynamic fee) are gradually shifted out for more
|
|
|
|
// expensive ones and any gapped pending transactions are moved into the queue.
|
|
|
|
//
|
|
|
|
// Note, local transactions are never allowed to be dropped.
|
2022-10-24 16:13:55 +03:00
|
|
|
func TestUnderpricingDynamicFee(t *testing.T) {
|
2021-05-28 11:28:07 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2022-10-24 16:13:55 +03:00
|
|
|
pool, _ := setupPoolWithConfig(eip1559Config)
|
2023-06-16 15:29:40 +03:00
|
|
|
defer pool.Close()
|
2021-05-28 11:28:07 +03:00
|
|
|
|
|
|
|
pool.config.GlobalSlots = 2
|
|
|
|
pool.config.GlobalQueue = 2
|
|
|
|
|
|
|
|
// Keep track of transaction events to ensure all executables get announced
|
2022-10-24 16:13:55 +03:00
|
|
|
events := make(chan core.NewTxsEvent, 32)
|
2021-05-28 11:28:07 +03:00
|
|
|
sub := pool.txFeed.Subscribe(events)
|
|
|
|
defer sub.Unsubscribe()
|
|
|
|
|
|
|
|
// Create a number of test accounts and fund them
|
|
|
|
keys := make([]*ecdsa.PrivateKey, 4)
|
|
|
|
for i := 0; i < len(keys); i++ {
|
|
|
|
keys[i], _ = crypto.GenerateKey()
|
|
|
|
testAddBalance(pool, 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, dynamicFeeTx(0, 100000, big.NewInt(3), big.NewInt(2), keys[0]))
|
|
|
|
txs = append(txs, pricedTransaction(1, 100000, big.NewInt(2), keys[0]))
|
|
|
|
txs = append(txs, dynamicFeeTx(1, 100000, big.NewInt(2), big.NewInt(1), keys[1]))
|
|
|
|
|
|
|
|
ltx := dynamicFeeTx(0, 100000, big.NewInt(2), big.NewInt(1), keys[2])
|
|
|
|
|
|
|
|
// Import the batch and that both pending and queued transactions match up
|
2023-06-16 15:29:40 +03:00
|
|
|
pool.addRemotes(txs) // Pend K0:0, K0:1; Que K1:1
|
|
|
|
pool.addLocal(ltx) // +K2:0 => Pend K0:0, K0:1, K2:0; Que K1:1
|
2021-05-28 11:28:07 +03:00
|
|
|
|
|
|
|
pending, queued := pool.Stats()
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
if err := validateEvents(events, 3); err != nil {
|
|
|
|
t.Fatalf("original event firing failed: %v", err)
|
|
|
|
}
|
2022-10-24 16:13:55 +03:00
|
|
|
if err := validatePoolInternals(pool); err != nil {
|
2021-05-28 11:28:07 +03:00
|
|
|
t.Fatalf("pool internal state corrupted: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that adding an underpriced transaction fails
|
|
|
|
tx := dynamicFeeTx(0, 100000, big.NewInt(2), big.NewInt(1), keys[1])
|
2023-06-16 15:29:40 +03:00
|
|
|
if err := pool.addRemote(tx); !errors.Is(err, txpool.ErrUnderpriced) { // Pend K0:0, K0:1, K2:0; Que K1:1
|
|
|
|
t.Fatalf("adding underpriced pending transaction error mismatch: have %v, want %v", err, txpool.ErrUnderpriced)
|
2021-05-28 11:28:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that adding high priced transactions drops cheap ones, but not own
|
|
|
|
tx = pricedTransaction(0, 100000, big.NewInt(2), keys[1])
|
2023-06-16 15:29:40 +03:00
|
|
|
if err := pool.addRemote(tx); err != nil { // +K1:0, -K1:1 => Pend K0:0, K0:1, K1:0, K2:0; Que -
|
2021-05-28 11:28:07 +03:00
|
|
|
t.Fatalf("failed to add well priced transaction: %v", err)
|
|
|
|
}
|
|
|
|
|
2023-03-10 20:30:26 +03:00
|
|
|
tx = pricedTransaction(1, 100000, big.NewInt(3), keys[1])
|
2023-07-06 19:05:34 +03:00
|
|
|
if err := pool.addRemoteSync(tx); err != nil { // +K1:2, -K0:1 => Pend K0:0 K1:0, K2:0; Que K1:2
|
2021-05-28 11:28:07 +03:00
|
|
|
t.Fatalf("failed to add well priced transaction: %v", err)
|
|
|
|
}
|
2023-03-10 20:30:26 +03:00
|
|
|
tx = dynamicFeeTx(2, 100000, big.NewInt(4), big.NewInt(1), keys[1])
|
2023-07-06 19:05:34 +03:00
|
|
|
if err := pool.addRemoteSync(tx); err != nil { // +K1:3, -K1:0 => Pend K0:0 K2:0; Que K1:2 K1:3
|
2021-05-28 11:28:07 +03:00
|
|
|
t.Fatalf("failed to add well priced transaction: %v", err)
|
|
|
|
}
|
|
|
|
pending, queued = pool.Stats()
|
|
|
|
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)
|
|
|
|
}
|
2023-03-10 20:30:26 +03:00
|
|
|
if err := validateEvents(events, 2); err != nil {
|
2021-05-28 11:28:07 +03:00
|
|
|
t.Fatalf("additional event firing failed: %v", err)
|
|
|
|
}
|
2022-10-24 16:13:55 +03:00
|
|
|
if err := validatePoolInternals(pool); err != nil {
|
2021-05-28 11:28:07 +03:00
|
|
|
t.Fatalf("pool internal state corrupted: %v", err)
|
|
|
|
}
|
|
|
|
// Ensure that adding local transactions can push out even higher priced ones
|
|
|
|
ltx = dynamicFeeTx(1, 100000, big.NewInt(0), big.NewInt(0), keys[2])
|
2023-06-16 15:29:40 +03:00
|
|
|
if err := pool.addLocal(ltx); err != nil {
|
2021-05-28 11:28:07 +03:00
|
|
|
t.Fatalf("failed to append underpriced local transaction: %v", err)
|
|
|
|
}
|
|
|
|
ltx = dynamicFeeTx(0, 100000, big.NewInt(0), big.NewInt(0), keys[3])
|
2023-06-16 15:29:40 +03:00
|
|
|
if err := pool.addLocal(ltx); err != nil {
|
2021-05-28 11:28:07 +03:00
|
|
|
t.Fatalf("failed to add new underpriced local transaction: %v", err)
|
|
|
|
}
|
|
|
|
pending, queued = pool.Stats()
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
if err := validateEvents(events, 2); err != nil {
|
|
|
|
t.Fatalf("local event firing failed: %v", err)
|
|
|
|
}
|
2022-10-24 16:13:55 +03:00
|
|
|
if err := validatePoolInternals(pool); err != nil {
|
2021-05-28 11:28:07 +03:00
|
|
|
t.Fatalf("pool internal state corrupted: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests whether highest fee cap transaction is retained after a batch of high effective
|
|
|
|
// tip transactions are added and vice versa
|
|
|
|
func TestDualHeapEviction(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2022-10-24 16:13:55 +03:00
|
|
|
pool, _ := setupPoolWithConfig(eip1559Config)
|
2023-06-16 15:29:40 +03:00
|
|
|
defer pool.Close()
|
2021-05-28 11:28:07 +03:00
|
|
|
|
|
|
|
pool.config.GlobalSlots = 10
|
|
|
|
pool.config.GlobalQueue = 10
|
|
|
|
|
|
|
|
var (
|
|
|
|
highTip, highCap *types.Transaction
|
|
|
|
baseFee int
|
|
|
|
)
|
|
|
|
|
|
|
|
check := func(tx *types.Transaction, name string) {
|
|
|
|
if pool.all.GetRemote(tx.Hash()) == nil {
|
|
|
|
t.Fatalf("highest %s transaction evicted from the pool", name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
add := func(urgent bool) {
|
2021-08-24 21:48:36 +03:00
|
|
|
for i := 0; i < 20; i++ {
|
|
|
|
var tx *types.Transaction
|
2021-05-28 11:28:07 +03:00
|
|
|
// Create a test accounts and fund it
|
|
|
|
key, _ := crypto.GenerateKey()
|
|
|
|
testAddBalance(pool, crypto.PubkeyToAddress(key.PublicKey), big.NewInt(1000000000000))
|
|
|
|
if urgent {
|
2021-08-24 21:48:36 +03:00
|
|
|
tx = dynamicFeeTx(0, 100000, big.NewInt(int64(baseFee+1+i)), big.NewInt(int64(1+i)), key)
|
|
|
|
highTip = tx
|
2021-05-28 11:28:07 +03:00
|
|
|
} else {
|
2021-08-24 21:48:36 +03:00
|
|
|
tx = dynamicFeeTx(0, 100000, big.NewInt(int64(baseFee+200+i)), big.NewInt(1), key)
|
|
|
|
highCap = tx
|
2021-05-28 11:28:07 +03:00
|
|
|
}
|
2023-06-16 15:29:40 +03:00
|
|
|
pool.addRemotesSync([]*types.Transaction{tx})
|
2021-05-28 11:28:07 +03:00
|
|
|
}
|
|
|
|
pending, queued := pool.Stats()
|
|
|
|
if pending+queued != 20 {
|
|
|
|
t.Fatalf("transaction count mismatch: have %d, want %d", pending+queued, 10)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
add(false)
|
|
|
|
for baseFee = 0; baseFee <= 1000; baseFee += 100 {
|
|
|
|
pool.priced.SetBaseFee(big.NewInt(int64(baseFee)))
|
|
|
|
add(true)
|
|
|
|
check(highCap, "fee cap")
|
|
|
|
add(false)
|
|
|
|
check(highTip, "effective tip")
|
|
|
|
}
|
|
|
|
|
2022-10-24 16:13:55 +03:00
|
|
|
if err := validatePoolInternals(pool); err != nil {
|
2021-05-28 11:28:07 +03:00
|
|
|
t.Fatalf("pool internal state corrupted: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-18 10:53:01 +03:00
|
|
|
// Tests that the pool rejects duplicate transactions.
|
2022-10-24 16:13:55 +03:00
|
|
|
func TestDeduplication(t *testing.T) {
|
2019-09-18 10:53:01 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
// Create the pool to test the pricing enforcement with
|
2023-05-11 10:19:42 +03:00
|
|
|
statedb, _ := state.New(types.EmptyRootHash, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
|
2023-06-16 15:29:40 +03:00
|
|
|
blockchain := newTestBlockChain(params.TestChainConfig, 1000000, statedb, new(event.Feed))
|
2019-09-18 10:53:01 +03:00
|
|
|
|
2023-06-16 15:29:40 +03:00
|
|
|
pool := New(testTxPoolConfig, blockchain)
|
|
|
|
pool.Init(new(big.Int).SetUint64(testTxPoolConfig.PriceLimit), blockchain.CurrentBlock())
|
|
|
|
defer pool.Close()
|
2019-09-18 10:53:01 +03:00
|
|
|
|
|
|
|
// Create a test account to add transactions with
|
|
|
|
key, _ := crypto.GenerateKey()
|
2021-05-28 11:28:07 +03:00
|
|
|
testAddBalance(pool, crypto.PubkeyToAddress(key.PublicKey), big.NewInt(1000000000))
|
2019-09-18 10:53:01 +03:00
|
|
|
|
|
|
|
// Create a batch of transactions and add a few of them
|
|
|
|
txs := make([]*types.Transaction, 16)
|
|
|
|
for i := 0; i < len(txs); i++ {
|
|
|
|
txs[i] = pricedTransaction(uint64(i), 100000, big.NewInt(1), key)
|
|
|
|
}
|
|
|
|
var firsts []*types.Transaction
|
|
|
|
for i := 0; i < len(txs); i += 2 {
|
|
|
|
firsts = append(firsts, txs[i])
|
|
|
|
}
|
2023-06-16 15:29:40 +03:00
|
|
|
errs := pool.addRemotesSync(firsts)
|
2019-09-18 10:53:01 +03:00
|
|
|
if len(errs) != len(firsts) {
|
|
|
|
t.Fatalf("first add mismatching result count: have %d, want %d", len(errs), len(firsts))
|
|
|
|
}
|
|
|
|
for i, err := range errs {
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("add %d failed: %v", i, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pending, queued := pool.Stats()
|
|
|
|
if pending != 1 {
|
|
|
|
t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 1)
|
|
|
|
}
|
|
|
|
if queued != len(txs)/2-1 {
|
|
|
|
t.Fatalf("queued transactions mismatched: have %d, want %d", queued, len(txs)/2-1)
|
|
|
|
}
|
|
|
|
// Try to add all of them now and ensure previous ones error out as knowns
|
2023-06-16 15:29:40 +03:00
|
|
|
errs = pool.addRemotesSync(txs)
|
2019-09-18 10:53:01 +03:00
|
|
|
if len(errs) != len(txs) {
|
|
|
|
t.Fatalf("all add mismatching result count: have %d, want %d", len(errs), len(txs))
|
|
|
|
}
|
|
|
|
for i, err := range errs {
|
|
|
|
if i%2 == 0 && err == nil {
|
|
|
|
t.Errorf("add %d succeeded, should have failed as known", i)
|
|
|
|
}
|
|
|
|
if i%2 == 1 && err != nil {
|
|
|
|
t.Errorf("add %d failed: %v", i, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pending, queued = pool.Stats()
|
|
|
|
if pending != len(txs) {
|
|
|
|
t.Fatalf("pending transactions mismatched: have %d, want %d", pending, len(txs))
|
|
|
|
}
|
|
|
|
if queued != 0 {
|
|
|
|
t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 0)
|
|
|
|
}
|
2022-10-24 16:13:55 +03:00
|
|
|
if err := validatePoolInternals(pool); err != nil {
|
2019-09-18 10:53:01 +03:00
|
|
|
t.Fatalf("pool internal state corrupted: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-16 22:07:27 +03:00
|
|
|
// Tests that the pool rejects replacement transactions that don't meet the minimum
|
|
|
|
// price bump required.
|
2022-10-24 16:13:55 +03:00
|
|
|
func TestReplacement(t *testing.T) {
|
2017-10-20 12:34:43 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2017-05-16 22:07:27 +03:00
|
|
|
// Create the pool to test the pricing enforcement with
|
2023-05-11 10:19:42 +03:00
|
|
|
statedb, _ := state.New(types.EmptyRootHash, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
|
2023-06-16 15:29:40 +03:00
|
|
|
blockchain := newTestBlockChain(params.TestChainConfig, 1000000, statedb, new(event.Feed))
|
2017-05-16 22:07:27 +03:00
|
|
|
|
2023-06-16 15:29:40 +03:00
|
|
|
pool := New(testTxPoolConfig, blockchain)
|
|
|
|
pool.Init(new(big.Int).SetUint64(testTxPoolConfig.PriceLimit), blockchain.CurrentBlock())
|
|
|
|
defer pool.Close()
|
2017-05-16 22:07:27 +03:00
|
|
|
|
2017-10-20 12:34:43 +03:00
|
|
|
// Keep track of transaction events to ensure all executables get announced
|
2022-10-24 16:13:55 +03:00
|
|
|
events := make(chan core.NewTxsEvent, 32)
|
2017-10-20 12:34:43 +03:00
|
|
|
sub := pool.txFeed.Subscribe(events)
|
|
|
|
defer sub.Unsubscribe()
|
|
|
|
|
2017-07-06 11:51:59 +03:00
|
|
|
// Create a test account to add transactions with
|
2017-05-16 22:07:27 +03:00
|
|
|
key, _ := crypto.GenerateKey()
|
2021-05-28 11:28:07 +03:00
|
|
|
testAddBalance(pool, crypto.PubkeyToAddress(key.PublicKey), big.NewInt(1000000000))
|
2017-05-16 22:07:27 +03:00
|
|
|
|
|
|
|
// Add pending transactions, ensuring the minimum price bump is enforced for replacement (for ultra low prices too)
|
|
|
|
price := int64(100)
|
2017-07-28 16:09:39 +03:00
|
|
|
threshold := (price * (100 + int64(testTxPoolConfig.PriceBump))) / 100
|
2017-05-16 22:07:27 +03:00
|
|
|
|
2019-06-21 11:29:14 +03:00
|
|
|
if err := pool.addRemoteSync(pricedTransaction(0, 100000, big.NewInt(1), key)); err != nil {
|
2017-05-16 22:07:27 +03:00
|
|
|
t.Fatalf("failed to add original cheap pending transaction: %v", err)
|
|
|
|
}
|
2023-06-16 15:29:40 +03:00
|
|
|
if err := pool.addRemote(pricedTransaction(0, 100001, big.NewInt(1), key)); err != txpool.ErrReplaceUnderpriced {
|
|
|
|
t.Fatalf("original cheap pending transaction replacement error mismatch: have %v, want %v", err, txpool.ErrReplaceUnderpriced)
|
2017-05-16 22:07:27 +03:00
|
|
|
}
|
2023-06-16 15:29:40 +03:00
|
|
|
if err := pool.addRemote(pricedTransaction(0, 100000, big.NewInt(2), key)); err != nil {
|
2017-05-16 22:07:27 +03:00
|
|
|
t.Fatalf("failed to replace original cheap pending transaction: %v", err)
|
|
|
|
}
|
2017-10-20 12:34:43 +03:00
|
|
|
if err := validateEvents(events, 2); err != nil {
|
|
|
|
t.Fatalf("cheap replacement event firing failed: %v", err)
|
|
|
|
}
|
2017-05-16 22:07:27 +03:00
|
|
|
|
2019-06-21 11:29:14 +03:00
|
|
|
if err := pool.addRemoteSync(pricedTransaction(0, 100000, big.NewInt(price), key)); err != nil {
|
2017-05-16 22:07:27 +03:00
|
|
|
t.Fatalf("failed to add original proper pending transaction: %v", err)
|
|
|
|
}
|
2023-06-16 15:29:40 +03:00
|
|
|
if err := pool.addRemote(pricedTransaction(0, 100001, big.NewInt(threshold-1), key)); err != txpool.ErrReplaceUnderpriced {
|
|
|
|
t.Fatalf("original proper pending transaction replacement error mismatch: have %v, want %v", err, txpool.ErrReplaceUnderpriced)
|
2017-05-16 22:07:27 +03:00
|
|
|
}
|
2023-06-16 15:29:40 +03:00
|
|
|
if err := pool.addRemote(pricedTransaction(0, 100000, big.NewInt(threshold), key)); err != nil {
|
2017-05-16 22:07:27 +03:00
|
|
|
t.Fatalf("failed to replace original proper pending transaction: %v", err)
|
|
|
|
}
|
2017-10-20 12:34:43 +03:00
|
|
|
if err := validateEvents(events, 2); err != nil {
|
|
|
|
t.Fatalf("proper replacement event firing failed: %v", err)
|
|
|
|
}
|
2019-06-21 11:29:14 +03:00
|
|
|
|
2017-05-16 22:07:27 +03:00
|
|
|
// Add queued transactions, ensuring the minimum price bump is enforced for replacement (for ultra low prices too)
|
2023-06-16 15:29:40 +03:00
|
|
|
if err := pool.addRemote(pricedTransaction(2, 100000, big.NewInt(1), key)); err != nil {
|
2017-10-30 14:05:00 +03:00
|
|
|
t.Fatalf("failed to add original cheap queued transaction: %v", err)
|
2017-05-16 22:07:27 +03:00
|
|
|
}
|
2023-06-16 15:29:40 +03:00
|
|
|
if err := pool.addRemote(pricedTransaction(2, 100001, big.NewInt(1), key)); err != txpool.ErrReplaceUnderpriced {
|
|
|
|
t.Fatalf("original cheap queued transaction replacement error mismatch: have %v, want %v", err, txpool.ErrReplaceUnderpriced)
|
2017-05-16 22:07:27 +03:00
|
|
|
}
|
2023-06-16 15:29:40 +03:00
|
|
|
if err := pool.addRemote(pricedTransaction(2, 100000, big.NewInt(2), key)); err != nil {
|
2017-10-30 14:05:00 +03:00
|
|
|
t.Fatalf("failed to replace original cheap queued transaction: %v", err)
|
2017-05-16 22:07:27 +03:00
|
|
|
}
|
|
|
|
|
2023-06-16 15:29:40 +03:00
|
|
|
if err := pool.addRemote(pricedTransaction(2, 100000, big.NewInt(price), key)); err != nil {
|
2017-10-30 14:05:00 +03:00
|
|
|
t.Fatalf("failed to add original proper queued transaction: %v", err)
|
2017-05-16 22:07:27 +03:00
|
|
|
}
|
2023-06-16 15:29:40 +03:00
|
|
|
if err := pool.addRemote(pricedTransaction(2, 100001, big.NewInt(threshold-1), key)); err != txpool.ErrReplaceUnderpriced {
|
|
|
|
t.Fatalf("original proper queued transaction replacement error mismatch: have %v, want %v", err, txpool.ErrReplaceUnderpriced)
|
2017-05-16 22:07:27 +03:00
|
|
|
}
|
2023-06-16 15:29:40 +03:00
|
|
|
if err := pool.addRemote(pricedTransaction(2, 100000, big.NewInt(threshold), key)); err != nil {
|
2017-10-30 14:05:00 +03:00
|
|
|
t.Fatalf("failed to replace original proper queued transaction: %v", err)
|
2017-05-16 22:07:27 +03:00
|
|
|
}
|
2017-10-20 12:34:43 +03:00
|
|
|
|
|
|
|
if err := validateEvents(events, 0); err != nil {
|
|
|
|
t.Fatalf("queued replacement event firing failed: %v", err)
|
|
|
|
}
|
2022-10-24 16:13:55 +03:00
|
|
|
if err := validatePoolInternals(pool); err != nil {
|
2017-06-22 17:01:49 +03:00
|
|
|
t.Fatalf("pool internal state corrupted: %v", err)
|
2017-06-22 11:14:31 +03:00
|
|
|
}
|
2017-05-16 22:07:27 +03:00
|
|
|
}
|
|
|
|
|
2021-05-28 11:28:07 +03:00
|
|
|
// Tests that the pool rejects replacement dynamic fee transactions that don't
|
|
|
|
// meet the minimum price bump required.
|
2022-10-24 16:13:55 +03:00
|
|
|
func TestReplacementDynamicFee(t *testing.T) {
|
2021-05-28 11:28:07 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
// Create the pool to test the pricing enforcement with
|
2022-10-24 16:13:55 +03:00
|
|
|
pool, key := setupPoolWithConfig(eip1559Config)
|
2023-06-16 15:29:40 +03:00
|
|
|
defer pool.Close()
|
2021-05-28 11:28:07 +03:00
|
|
|
testAddBalance(pool, crypto.PubkeyToAddress(key.PublicKey), big.NewInt(1000000000))
|
|
|
|
|
|
|
|
// Keep track of transaction events to ensure all executables get announced
|
2022-10-24 16:13:55 +03:00
|
|
|
events := make(chan core.NewTxsEvent, 32)
|
2021-05-28 11:28:07 +03:00
|
|
|
sub := pool.txFeed.Subscribe(events)
|
|
|
|
defer sub.Unsubscribe()
|
|
|
|
|
|
|
|
// Add pending transactions, ensuring the minimum price bump is enforced for replacement (for ultra low prices too)
|
2021-06-08 13:05:41 +03:00
|
|
|
gasFeeCap := int64(100)
|
|
|
|
feeCapThreshold := (gasFeeCap * (100 + int64(testTxPoolConfig.PriceBump))) / 100
|
|
|
|
gasTipCap := int64(60)
|
|
|
|
tipThreshold := (gasTipCap * (100 + int64(testTxPoolConfig.PriceBump))) / 100
|
2021-05-28 11:28:07 +03:00
|
|
|
|
|
|
|
// Run the following identical checks for both the pending and queue pools:
|
|
|
|
// 1. Send initial tx => accept
|
|
|
|
// 2. Don't bump tip or fee cap => discard
|
|
|
|
// 3. Bump both more than min => accept
|
|
|
|
// 4. Check events match expected (2 new executable txs during pending, 0 during queue)
|
2021-06-08 13:05:41 +03:00
|
|
|
// 5. Send new tx with larger tip and gasFeeCap => accept
|
2021-05-28 11:28:07 +03:00
|
|
|
// 6. Bump tip max allowed so it's still underpriced => discard
|
|
|
|
// 7. Bump fee cap max allowed so it's still underpriced => discard
|
|
|
|
// 8. Bump tip min for acceptance => discard
|
|
|
|
// 9. Bump feecap min for acceptance => discard
|
|
|
|
// 10. Bump feecap and tip min for acceptance => accept
|
|
|
|
// 11. Check events match expected (2 new executable txs during pending, 0 during queue)
|
|
|
|
stages := []string{"pending", "queued"}
|
|
|
|
for _, stage := range stages {
|
|
|
|
// Since state is empty, 0 nonce txs are "executable" and can go
|
2022-10-11 10:37:00 +03:00
|
|
|
// into pending immediately. 2 nonce txs are "gapped"
|
2021-05-28 11:28:07 +03:00
|
|
|
nonce := uint64(0)
|
|
|
|
if stage == "queued" {
|
|
|
|
nonce = 2
|
|
|
|
}
|
|
|
|
|
|
|
|
// 1. Send initial tx => accept
|
|
|
|
tx := dynamicFeeTx(nonce, 100000, big.NewInt(2), big.NewInt(1), key)
|
|
|
|
if err := pool.addRemoteSync(tx); err != nil {
|
|
|
|
t.Fatalf("failed to add original cheap %s transaction: %v", stage, err)
|
|
|
|
}
|
|
|
|
// 2. Don't bump tip or feecap => discard
|
|
|
|
tx = dynamicFeeTx(nonce, 100001, big.NewInt(2), big.NewInt(1), key)
|
2023-06-16 15:29:40 +03:00
|
|
|
if err := pool.addRemote(tx); err != txpool.ErrReplaceUnderpriced {
|
|
|
|
t.Fatalf("original cheap %s transaction replacement error mismatch: have %v, want %v", stage, err, txpool.ErrReplaceUnderpriced)
|
2021-05-28 11:28:07 +03:00
|
|
|
}
|
|
|
|
// 3. Bump both more than min => accept
|
|
|
|
tx = dynamicFeeTx(nonce, 100000, big.NewInt(3), big.NewInt(2), key)
|
2023-06-16 15:29:40 +03:00
|
|
|
if err := pool.addRemote(tx); err != nil {
|
2021-05-28 11:28:07 +03:00
|
|
|
t.Fatalf("failed to replace original cheap %s transaction: %v", stage, err)
|
|
|
|
}
|
|
|
|
// 4. Check events match expected (2 new executable txs during pending, 0 during queue)
|
|
|
|
count := 2
|
|
|
|
if stage == "queued" {
|
|
|
|
count = 0
|
|
|
|
}
|
|
|
|
if err := validateEvents(events, count); err != nil {
|
|
|
|
t.Fatalf("cheap %s replacement event firing failed: %v", stage, err)
|
|
|
|
}
|
|
|
|
// 5. Send new tx with larger tip and feeCap => accept
|
2021-06-08 13:05:41 +03:00
|
|
|
tx = dynamicFeeTx(nonce, 100000, big.NewInt(gasFeeCap), big.NewInt(gasTipCap), key)
|
2021-05-28 11:28:07 +03:00
|
|
|
if err := pool.addRemoteSync(tx); err != nil {
|
|
|
|
t.Fatalf("failed to add original proper %s transaction: %v", stage, err)
|
|
|
|
}
|
|
|
|
// 6. Bump tip max allowed so it's still underpriced => discard
|
2021-06-08 13:05:41 +03:00
|
|
|
tx = dynamicFeeTx(nonce, 100000, big.NewInt(gasFeeCap), big.NewInt(tipThreshold-1), key)
|
2023-06-16 15:29:40 +03:00
|
|
|
if err := pool.addRemote(tx); err != txpool.ErrReplaceUnderpriced {
|
|
|
|
t.Fatalf("original proper %s transaction replacement error mismatch: have %v, want %v", stage, err, txpool.ErrReplaceUnderpriced)
|
2021-05-28 11:28:07 +03:00
|
|
|
}
|
|
|
|
// 7. Bump fee cap max allowed so it's still underpriced => discard
|
2021-06-08 13:05:41 +03:00
|
|
|
tx = dynamicFeeTx(nonce, 100000, big.NewInt(feeCapThreshold-1), big.NewInt(gasTipCap), key)
|
2023-06-16 15:29:40 +03:00
|
|
|
if err := pool.addRemote(tx); err != txpool.ErrReplaceUnderpriced {
|
|
|
|
t.Fatalf("original proper %s transaction replacement error mismatch: have %v, want %v", stage, err, txpool.ErrReplaceUnderpriced)
|
2021-05-28 11:28:07 +03:00
|
|
|
}
|
|
|
|
// 8. Bump tip min for acceptance => accept
|
2021-06-08 13:05:41 +03:00
|
|
|
tx = dynamicFeeTx(nonce, 100000, big.NewInt(gasFeeCap), big.NewInt(tipThreshold), key)
|
2023-06-16 15:29:40 +03:00
|
|
|
if err := pool.addRemote(tx); err != txpool.ErrReplaceUnderpriced {
|
|
|
|
t.Fatalf("original proper %s transaction replacement error mismatch: have %v, want %v", stage, err, txpool.ErrReplaceUnderpriced)
|
2021-05-28 11:28:07 +03:00
|
|
|
}
|
|
|
|
// 9. Bump fee cap min for acceptance => accept
|
2021-06-08 13:05:41 +03:00
|
|
|
tx = dynamicFeeTx(nonce, 100000, big.NewInt(feeCapThreshold), big.NewInt(gasTipCap), key)
|
2023-06-16 15:29:40 +03:00
|
|
|
if err := pool.addRemote(tx); err != txpool.ErrReplaceUnderpriced {
|
|
|
|
t.Fatalf("original proper %s transaction replacement error mismatch: have %v, want %v", stage, err, txpool.ErrReplaceUnderpriced)
|
2021-05-28 11:28:07 +03:00
|
|
|
}
|
|
|
|
// 10. Check events match expected (3 new executable txs during pending, 0 during queue)
|
|
|
|
tx = dynamicFeeTx(nonce, 100000, big.NewInt(feeCapThreshold), big.NewInt(tipThreshold), key)
|
2023-06-16 15:29:40 +03:00
|
|
|
if err := pool.addRemote(tx); err != nil {
|
2021-05-28 11:28:07 +03:00
|
|
|
t.Fatalf("failed to replace original cheap %s transaction: %v", stage, err)
|
|
|
|
}
|
|
|
|
// 11. Check events match expected (3 new executable txs during pending, 0 during queue)
|
|
|
|
count = 2
|
|
|
|
if stage == "queued" {
|
|
|
|
count = 0
|
|
|
|
}
|
|
|
|
if err := validateEvents(events, count); err != nil {
|
|
|
|
t.Fatalf("replacement %s event firing failed: %v", stage, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-24 16:13:55 +03:00
|
|
|
if err := validatePoolInternals(pool); err != nil {
|
2021-05-28 11:28:07 +03:00
|
|
|
t.Fatalf("pool internal state corrupted: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-28 16:09:39 +03:00
|
|
|
// Tests that local transactions are journaled to disk, but remote transactions
|
|
|
|
// get discarded between restarts.
|
2022-10-24 16:13:55 +03:00
|
|
|
func TestJournaling(t *testing.T) { testJournaling(t, false) }
|
|
|
|
func TestJournalingNoLocals(t *testing.T) { testJournaling(t, true) }
|
2017-07-28 16:09:39 +03:00
|
|
|
|
2022-10-24 16:13:55 +03:00
|
|
|
func testJournaling(t *testing.T, nolocals bool) {
|
2017-10-20 12:34:43 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2017-07-28 16:09:39 +03:00
|
|
|
// Create a temporary file for the journal
|
2022-05-16 12:59:35 +03:00
|
|
|
file, err := os.CreateTemp("", "")
|
2017-07-28 16:09:39 +03:00
|
|
|
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
|
2023-05-11 10:19:42 +03:00
|
|
|
statedb, _ := state.New(types.EmptyRootHash, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
|
2023-06-16 15:29:40 +03:00
|
|
|
blockchain := newTestBlockChain(params.TestChainConfig, 1000000, statedb, new(event.Feed))
|
2017-07-28 16:09:39 +03:00
|
|
|
|
|
|
|
config := testTxPoolConfig
|
|
|
|
config.NoLocals = nolocals
|
|
|
|
config.Journal = journal
|
|
|
|
config.Rejournal = time.Second
|
|
|
|
|
2023-06-16 15:29:40 +03:00
|
|
|
pool := New(config, blockchain)
|
|
|
|
pool.Init(new(big.Int).SetUint64(config.PriceLimit), blockchain.CurrentBlock())
|
2017-07-28 16:09:39 +03:00
|
|
|
|
|
|
|
// Create two test accounts to ensure remotes expire but locals do not
|
|
|
|
local, _ := crypto.GenerateKey()
|
|
|
|
remote, _ := crypto.GenerateKey()
|
|
|
|
|
2021-05-28 11:28:07 +03:00
|
|
|
testAddBalance(pool, crypto.PubkeyToAddress(local.PublicKey), big.NewInt(1000000000))
|
|
|
|
testAddBalance(pool, crypto.PubkeyToAddress(remote.PublicKey), big.NewInt(1000000000))
|
2017-07-28 16:09:39 +03:00
|
|
|
|
|
|
|
// Add three local and a remote transactions and ensure they are queued up
|
2023-06-16 15:29:40 +03:00
|
|
|
if err := pool.addLocal(pricedTransaction(0, 100000, big.NewInt(1), local)); err != nil {
|
2017-07-28 16:09:39 +03:00
|
|
|
t.Fatalf("failed to add local transaction: %v", err)
|
|
|
|
}
|
2023-06-16 15:29:40 +03:00
|
|
|
if err := pool.addLocal(pricedTransaction(1, 100000, big.NewInt(1), local)); err != nil {
|
2017-07-28 16:09:39 +03:00
|
|
|
t.Fatalf("failed to add local transaction: %v", err)
|
|
|
|
}
|
2023-06-16 15:29:40 +03:00
|
|
|
if err := pool.addLocal(pricedTransaction(2, 100000, big.NewInt(1), local)); err != nil {
|
2017-07-28 16:09:39 +03:00
|
|
|
t.Fatalf("failed to add local transaction: %v", err)
|
|
|
|
}
|
2019-06-21 11:29:14 +03:00
|
|
|
if err := pool.addRemoteSync(pricedTransaction(0, 100000, big.NewInt(1), remote)); err != nil {
|
2017-07-28 16:09:39 +03:00
|
|
|
t.Fatalf("failed to add remote transaction: %v", err)
|
|
|
|
}
|
2017-08-08 11:59:34 +03:00
|
|
|
pending, queued := pool.Stats()
|
2017-07-28 16:09:39 +03: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)
|
|
|
|
}
|
2022-10-24 16:13:55 +03:00
|
|
|
if err := validatePoolInternals(pool); err != nil {
|
2017-07-28 16:09:39 +03:00
|
|
|
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
|
2023-06-16 15:29:40 +03:00
|
|
|
pool.Close()
|
2017-07-28 16:09:39 +03:00
|
|
|
statedb.SetNonce(crypto.PubkeyToAddress(local.PublicKey), 1)
|
2023-06-16 15:29:40 +03:00
|
|
|
blockchain = newTestBlockChain(params.TestChainConfig, 1000000, statedb, new(event.Feed))
|
2017-10-20 12:34:43 +03:00
|
|
|
|
2023-06-16 15:29:40 +03:00
|
|
|
pool = New(config, blockchain)
|
|
|
|
pool.Init(new(big.Int).SetUint64(config.PriceLimit), blockchain.CurrentBlock())
|
2017-07-28 16:09:39 +03:00
|
|
|
|
2017-08-08 11:59:34 +03:00
|
|
|
pending, queued = pool.Stats()
|
2017-07-28 16:09:39 +03: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)
|
|
|
|
}
|
|
|
|
}
|
2022-10-24 16:13:55 +03:00
|
|
|
if err := validatePoolInternals(pool); err != nil {
|
2017-07-28 16:09:39 +03:00
|
|
|
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)
|
2019-06-21 11:29:14 +03:00
|
|
|
<-pool.requestReset(nil, nil)
|
2017-07-28 16:09:39 +03:00
|
|
|
time.Sleep(2 * config.Rejournal)
|
2023-06-16 15:29:40 +03:00
|
|
|
pool.Close()
|
2017-10-20 12:34:43 +03:00
|
|
|
|
2017-07-28 16:09:39 +03:00
|
|
|
statedb.SetNonce(crypto.PubkeyToAddress(local.PublicKey), 1)
|
2023-06-16 15:29:40 +03:00
|
|
|
blockchain = newTestBlockChain(params.TestChainConfig, 1000000, statedb, new(event.Feed))
|
|
|
|
pool = New(config, blockchain)
|
|
|
|
pool.Init(new(big.Int).SetUint64(config.PriceLimit), blockchain.CurrentBlock())
|
2017-07-28 16:09:39 +03:00
|
|
|
|
2017-08-08 11:59:34 +03:00
|
|
|
pending, queued = pool.Stats()
|
2017-07-28 16:09:39 +03: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)
|
|
|
|
}
|
|
|
|
}
|
2022-10-24 16:13:55 +03:00
|
|
|
if err := validatePoolInternals(pool); err != nil {
|
2017-07-28 16:09:39 +03:00
|
|
|
t.Fatalf("pool internal state corrupted: %v", err)
|
|
|
|
}
|
2023-06-16 15:29:40 +03:00
|
|
|
pool.Close()
|
2017-07-28 16:09:39 +03:00
|
|
|
}
|
|
|
|
|
2022-10-24 16:13:55 +03:00
|
|
|
// TestStatusCheck tests that the pool can correctly retrieve the
|
2017-12-20 13:34:43 +03:00
|
|
|
// pending status of individual transactions.
|
2022-10-24 16:13:55 +03:00
|
|
|
func TestStatusCheck(t *testing.T) {
|
2017-12-20 13:34:43 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
// Create the pool to test the status retrievals with
|
2023-05-11 10:19:42 +03:00
|
|
|
statedb, _ := state.New(types.EmptyRootHash, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
|
2023-06-16 15:29:40 +03:00
|
|
|
blockchain := newTestBlockChain(params.TestChainConfig, 1000000, statedb, new(event.Feed))
|
2017-12-20 13:34:43 +03:00
|
|
|
|
2023-06-16 15:29:40 +03:00
|
|
|
pool := New(testTxPoolConfig, blockchain)
|
|
|
|
pool.Init(new(big.Int).SetUint64(testTxPoolConfig.PriceLimit), blockchain.CurrentBlock())
|
|
|
|
defer pool.Close()
|
2017-12-20 13:34:43 +03:00
|
|
|
|
|
|
|
// Create the test accounts to check various transaction statuses with
|
|
|
|
keys := make([]*ecdsa.PrivateKey, 3)
|
|
|
|
for i := 0; i < len(keys); i++ {
|
|
|
|
keys[i], _ = crypto.GenerateKey()
|
2021-05-28 11:28:07 +03:00
|
|
|
testAddBalance(pool, crypto.PubkeyToAddress(keys[i].PublicKey), big.NewInt(1000000))
|
2017-12-20 13:34:43 +03:00
|
|
|
}
|
|
|
|
// Generate and queue a batch of transactions, both pending and queued
|
|
|
|
txs := types.Transactions{}
|
|
|
|
|
2017-11-13 14:47:27 +03:00
|
|
|
txs = append(txs, pricedTransaction(0, 100000, big.NewInt(1), keys[0])) // Pending only
|
|
|
|
txs = append(txs, pricedTransaction(0, 100000, big.NewInt(1), keys[1])) // Pending and queued
|
|
|
|
txs = append(txs, pricedTransaction(2, 100000, big.NewInt(1), keys[1]))
|
|
|
|
txs = append(txs, pricedTransaction(2, 100000, big.NewInt(1), keys[2])) // Queued only
|
2017-12-20 13:34:43 +03:00
|
|
|
|
|
|
|
// Import the transaction and ensure they are correctly added
|
2023-06-16 15:29:40 +03:00
|
|
|
pool.addRemotesSync(txs)
|
2017-12-20 13:34:43 +03:00
|
|
|
|
|
|
|
pending, queued := pool.Stats()
|
|
|
|
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)
|
|
|
|
}
|
2022-10-24 16:13:55 +03:00
|
|
|
if err := validatePoolInternals(pool); err != nil {
|
2017-12-20 13:34:43 +03:00
|
|
|
t.Fatalf("pool internal state corrupted: %v", err)
|
|
|
|
}
|
|
|
|
// Retrieve the status of each transaction and validate them
|
|
|
|
hashes := make([]common.Hash, len(txs))
|
|
|
|
for i, tx := range txs {
|
|
|
|
hashes[i] = tx.Hash()
|
|
|
|
}
|
|
|
|
hashes = append(hashes, common.Hash{})
|
2023-06-16 15:29:40 +03:00
|
|
|
expect := []txpool.TxStatus{txpool.TxStatusPending, txpool.TxStatusPending, txpool.TxStatusQueued, txpool.TxStatusQueued, txpool.TxStatusUnknown}
|
2017-12-20 13:34:43 +03:00
|
|
|
|
2023-06-16 15:29:40 +03:00
|
|
|
for i := 0; i < len(hashes); i++ {
|
|
|
|
if status := pool.Status(hashes[i]); status != expect[i] {
|
|
|
|
t.Errorf("transaction %d: status mismatch: have %v, want %v", i, status, expect[i])
|
2017-12-20 13:34:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-10 12:40:03 +03:00
|
|
|
// Test the transaction slots consumption is computed correctly
|
2022-10-24 16:13:55 +03:00
|
|
|
func TestSlotCount(t *testing.T) {
|
2020-01-10 12:40:03 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
key, _ := crypto.GenerateKey()
|
|
|
|
|
|
|
|
// Check that an empty transaction consumes a single slot
|
|
|
|
smallTx := pricedDataTransaction(0, 0, big.NewInt(0), key, 0)
|
|
|
|
if slots := numSlots(smallTx); slots != 1 {
|
|
|
|
t.Fatalf("small transactions slot count mismatch: have %d want %d", slots, 1)
|
|
|
|
}
|
|
|
|
// Check that a large transaction consumes the correct number of slots
|
|
|
|
bigTx := pricedDataTransaction(0, 0, big.NewInt(0), key, uint64(10*txSlotSize))
|
|
|
|
if slots := numSlots(bigTx); slots != 11 {
|
|
|
|
t.Fatalf("big transactions slot count mismatch: have %d want %d", slots, 11)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-30 18:31:37 +02:00
|
|
|
// Benchmarks the speed of validating the contents of the pending queue of the
|
|
|
|
// transaction pool.
|
2016-07-01 18:59:55 +03: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 18:31:37 +02:00
|
|
|
|
2016-07-01 18:59:55 +03:00
|
|
|
func benchmarkPendingDemotion(b *testing.B, size int) {
|
2015-12-30 18:31:37 +02:00
|
|
|
// Add a batch of transactions to a pool one by one
|
2022-10-24 16:13:55 +03:00
|
|
|
pool, key := setupPool()
|
2023-06-16 15:29:40 +03:00
|
|
|
defer pool.Close()
|
2017-07-06 11:51:59 +03:00
|
|
|
|
2020-01-10 12:40:03 +03:00
|
|
|
account := crypto.PubkeyToAddress(key.PublicKey)
|
2021-05-28 11:28:07 +03:00
|
|
|
testAddBalance(pool, account, big.NewInt(1000000))
|
2015-12-30 18:31:37 +02:00
|
|
|
|
|
|
|
for i := 0; i < size; i++ {
|
2017-11-13 14:47:27 +03:00
|
|
|
tx := transaction(uint64(i), 100000, key)
|
2016-07-01 18:59:55 +03:00
|
|
|
pool.promoteTx(account, tx.Hash(), tx)
|
2015-12-30 18:31:37 +02:00
|
|
|
}
|
|
|
|
// Benchmark the speed of pool validation
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2017-09-04 22:35:00 +03:00
|
|
|
pool.demoteUnexecutables()
|
2015-12-30 18:31:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Benchmarks the speed of scheduling the contents of the future queue of the
|
|
|
|
// transaction pool.
|
2016-07-01 18:59:55 +03: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 18:31:37 +02:00
|
|
|
|
2016-07-01 18:59:55 +03:00
|
|
|
func benchmarkFuturePromotion(b *testing.B, size int) {
|
2015-12-30 18:31:37 +02:00
|
|
|
// Add a batch of transactions to a pool one by one
|
2022-10-24 16:13:55 +03:00
|
|
|
pool, key := setupPool()
|
2023-06-16 15:29:40 +03:00
|
|
|
defer pool.Close()
|
2017-07-06 11:51:59 +03:00
|
|
|
|
2020-01-10 12:40:03 +03:00
|
|
|
account := crypto.PubkeyToAddress(key.PublicKey)
|
2021-05-28 11:28:07 +03:00
|
|
|
testAddBalance(pool, account, big.NewInt(1000000))
|
2015-12-30 18:31:37 +02:00
|
|
|
|
|
|
|
for i := 0; i < size; i++ {
|
2017-11-13 14:47:27 +03:00
|
|
|
tx := transaction(uint64(1+i), 100000, key)
|
2020-12-11 12:44:57 +03:00
|
|
|
pool.enqueueTx(tx.Hash(), tx, false, true)
|
2015-12-30 18:31:37 +02:00
|
|
|
}
|
|
|
|
// Benchmark the speed of pool validation
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2017-09-04 22:35:00 +03:00
|
|
|
pool.promoteExecutables(nil)
|
2016-07-01 18:59:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Benchmarks the speed of batched transaction insertion.
|
2022-10-24 16:13:55 +03:00
|
|
|
func BenchmarkBatchInsert100(b *testing.B) { benchmarkBatchInsert(b, 100, false) }
|
|
|
|
func BenchmarkBatchInsert1000(b *testing.B) { benchmarkBatchInsert(b, 1000, false) }
|
|
|
|
func BenchmarkBatchInsert10000(b *testing.B) { benchmarkBatchInsert(b, 10000, false) }
|
2016-07-01 18:59:55 +03:00
|
|
|
|
2022-10-24 16:13:55 +03:00
|
|
|
func BenchmarkBatchLocalInsert100(b *testing.B) { benchmarkBatchInsert(b, 100, true) }
|
|
|
|
func BenchmarkBatchLocalInsert1000(b *testing.B) { benchmarkBatchInsert(b, 1000, true) }
|
|
|
|
func BenchmarkBatchLocalInsert10000(b *testing.B) { benchmarkBatchInsert(b, 10000, true) }
|
2020-07-01 20:35:26 +03:00
|
|
|
|
2022-10-24 16:13:55 +03:00
|
|
|
func benchmarkBatchInsert(b *testing.B, size int, local bool) {
|
2016-07-01 18:59:55 +03:00
|
|
|
// Generate a batch of transactions to enqueue into the pool
|
2022-10-24 16:13:55 +03:00
|
|
|
pool, key := setupPool()
|
2023-06-16 15:29:40 +03:00
|
|
|
defer pool.Close()
|
2017-07-06 11:51:59 +03:00
|
|
|
|
2020-01-10 12:40:03 +03:00
|
|
|
account := crypto.PubkeyToAddress(key.PublicKey)
|
2023-03-10 20:30:26 +03:00
|
|
|
testAddBalance(pool, account, big.NewInt(1000000000000000000))
|
2016-07-01 18:59:55 +03:00
|
|
|
|
|
|
|
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++ {
|
2017-11-13 14:47:27 +03:00
|
|
|
batches[i][j] = transaction(uint64(size*i+j), 100000, key)
|
2016-07-01 18:59:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Benchmark importing the transactions into the queue
|
|
|
|
b.ResetTimer()
|
|
|
|
for _, batch := range batches {
|
2020-07-01 20:35:26 +03:00
|
|
|
if local {
|
2023-06-16 15:29:40 +03:00
|
|
|
pool.addLocals(batch)
|
2020-07-01 20:35:26 +03:00
|
|
|
} else {
|
2023-06-16 15:29:40 +03:00
|
|
|
pool.addRemotes(batch)
|
2020-07-01 20:35:26 +03:00
|
|
|
}
|
2015-12-30 18:31:37 +02:00
|
|
|
}
|
|
|
|
}
|
2020-12-11 12:44:57 +03:00
|
|
|
|
|
|
|
func BenchmarkInsertRemoteWithAllLocals(b *testing.B) {
|
|
|
|
// Allocate keys for testing
|
|
|
|
key, _ := crypto.GenerateKey()
|
|
|
|
account := crypto.PubkeyToAddress(key.PublicKey)
|
|
|
|
|
|
|
|
remoteKey, _ := crypto.GenerateKey()
|
|
|
|
remoteAddr := crypto.PubkeyToAddress(remoteKey.PublicKey)
|
|
|
|
|
|
|
|
locals := make([]*types.Transaction, 4096+1024) // Occupy all slots
|
|
|
|
for i := 0; i < len(locals); i++ {
|
|
|
|
locals[i] = transaction(uint64(i), 100000, key)
|
|
|
|
}
|
|
|
|
remotes := make([]*types.Transaction, 1000)
|
|
|
|
for i := 0; i < len(remotes); i++ {
|
|
|
|
remotes[i] = pricedTransaction(uint64(i), 100000, big.NewInt(2), remoteKey) // Higher gasprice
|
|
|
|
}
|
|
|
|
// Benchmark importing the transactions into the queue
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
b.StopTimer()
|
2022-10-24 16:13:55 +03:00
|
|
|
pool, _ := setupPool()
|
2021-05-28 11:28:07 +03:00
|
|
|
testAddBalance(pool, account, big.NewInt(100000000))
|
2020-12-11 12:44:57 +03:00
|
|
|
for _, local := range locals {
|
2023-06-16 15:29:40 +03:00
|
|
|
pool.addLocal(local)
|
2020-12-11 12:44:57 +03:00
|
|
|
}
|
|
|
|
b.StartTimer()
|
|
|
|
// Assign a high enough balance for testing
|
2021-05-28 11:28:07 +03:00
|
|
|
testAddBalance(pool, remoteAddr, big.NewInt(100000000))
|
2020-12-11 12:44:57 +03:00
|
|
|
for i := 0; i < len(remotes); i++ {
|
2023-06-16 15:29:40 +03:00
|
|
|
pool.addRemotes([]*types.Transaction{remotes[i]})
|
2020-12-11 12:44:57 +03:00
|
|
|
}
|
2023-06-16 15:29:40 +03:00
|
|
|
pool.Close()
|
2020-12-11 12:44:57 +03:00
|
|
|
}
|
|
|
|
}
|
2021-11-02 20:32:23 +03:00
|
|
|
|
|
|
|
// Benchmarks the speed of batch transaction insertion in case of multiple accounts.
|
2022-10-24 16:13:55 +03:00
|
|
|
func BenchmarkMultiAccountBatchInsert(b *testing.B) {
|
2021-11-02 20:32:23 +03:00
|
|
|
// Generate a batch of transactions to enqueue into the pool
|
2022-10-24 16:13:55 +03:00
|
|
|
pool, _ := setupPool()
|
2023-06-16 15:29:40 +03:00
|
|
|
defer pool.Close()
|
2021-11-02 20:32:23 +03:00
|
|
|
b.ReportAllocs()
|
|
|
|
batches := make(types.Transactions, b.N)
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
key, _ := crypto.GenerateKey()
|
|
|
|
account := crypto.PubkeyToAddress(key.PublicKey)
|
|
|
|
pool.currentState.AddBalance(account, big.NewInt(1000000))
|
|
|
|
tx := transaction(uint64(0), 100000, key)
|
|
|
|
batches[i] = tx
|
|
|
|
}
|
|
|
|
// Benchmark importing the transactions into the queue
|
|
|
|
b.ResetTimer()
|
|
|
|
for _, tx := range batches {
|
2023-06-16 15:29:40 +03:00
|
|
|
pool.addRemotesSync([]*types.Transaction{tx})
|
2021-11-02 20:32:23 +03:00
|
|
|
}
|
|
|
|
}
|