github.com/ethereum/go-ethereum@v1.14.4-0.20240516095835-473ee8fc07a3/core/txpool/legacypool/list_test.go (about)

     1  // Copyright 2016 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package legacypool
    18  
    19  import (
    20  	"math/big"
    21  	"math/rand"
    22  	"testing"
    23  
    24  	"github.com/ethereum/go-ethereum/common"
    25  	"github.com/ethereum/go-ethereum/core/types"
    26  	"github.com/ethereum/go-ethereum/crypto"
    27  	"github.com/holiman/uint256"
    28  )
    29  
    30  // Tests that transactions can be added to strict lists and list contents and
    31  // nonce boundaries are correctly maintained.
    32  func TestStrictListAdd(t *testing.T) {
    33  	// Generate a list of transactions to insert
    34  	key, _ := crypto.GenerateKey()
    35  
    36  	txs := make(types.Transactions, 1024)
    37  	for i := 0; i < len(txs); i++ {
    38  		txs[i] = transaction(uint64(i), 0, key)
    39  	}
    40  	// Insert the transactions in a random order
    41  	list := newList(true)
    42  	for _, v := range rand.Perm(len(txs)) {
    43  		list.Add(txs[v], DefaultConfig.PriceBump)
    44  	}
    45  	// Verify internal state
    46  	if len(list.txs.items) != len(txs) {
    47  		t.Errorf("transaction count mismatch: have %d, want %d", len(list.txs.items), len(txs))
    48  	}
    49  	for i, tx := range txs {
    50  		if list.txs.items[tx.Nonce()] != tx {
    51  			t.Errorf("item %d: transaction mismatch: have %v, want %v", i, list.txs.items[tx.Nonce()], tx)
    52  		}
    53  	}
    54  }
    55  
    56  // TestListAddVeryExpensive tests adding txs which exceed 256 bits in cost. It is
    57  // expected that the list does not panic.
    58  func TestListAddVeryExpensive(t *testing.T) {
    59  	key, _ := crypto.GenerateKey()
    60  	list := newList(true)
    61  	for i := 0; i < 3; i++ {
    62  		value := big.NewInt(100)
    63  		gasprice, _ := new(big.Int).SetString("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 0)
    64  		gaslimit := uint64(i)
    65  		tx, _ := types.SignTx(types.NewTransaction(uint64(i), common.Address{}, value, gaslimit, gasprice, nil), types.HomesteadSigner{}, key)
    66  		t.Logf("cost: %x bitlen: %d\n", tx.Cost(), tx.Cost().BitLen())
    67  		list.Add(tx, DefaultConfig.PriceBump)
    68  	}
    69  }
    70  
    71  func BenchmarkListAdd(b *testing.B) {
    72  	// Generate a list of transactions to insert
    73  	key, _ := crypto.GenerateKey()
    74  
    75  	txs := make(types.Transactions, 100000)
    76  	for i := 0; i < len(txs); i++ {
    77  		txs[i] = transaction(uint64(i), 0, key)
    78  	}
    79  	// Insert the transactions in a random order
    80  	priceLimit := uint256.NewInt(DefaultConfig.PriceLimit)
    81  	b.ResetTimer()
    82  	for i := 0; i < b.N; i++ {
    83  		list := newList(true)
    84  		for _, v := range rand.Perm(len(txs)) {
    85  			list.Add(txs[v], DefaultConfig.PriceBump)
    86  			list.Filter(priceLimit, DefaultConfig.PriceBump)
    87  		}
    88  	}
    89  }
    90  
    91  func BenchmarkListCapOneTx(b *testing.B) {
    92  	// Generate a list of transactions to insert
    93  	key, _ := crypto.GenerateKey()
    94  
    95  	txs := make(types.Transactions, 32)
    96  	for i := 0; i < len(txs); i++ {
    97  		txs[i] = transaction(uint64(i), 0, key)
    98  	}
    99  
   100  	b.ResetTimer()
   101  	for i := 0; i < b.N; i++ {
   102  		list := newList(true)
   103  		// Insert the transactions in a random order
   104  		for _, v := range rand.Perm(len(txs)) {
   105  			list.Add(txs[v], DefaultConfig.PriceBump)
   106  		}
   107  		b.StartTimer()
   108  		list.Cap(list.Len() - 1)
   109  		b.StopTimer()
   110  	}
   111  }