github.com/palisadeinc/bor@v0.0.0-20230615125219-ab7196213d15/core/tx_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 core
    18  
    19  import (
    20  	"math/rand"
    21  	"testing"
    22  
    23  	"github.com/holiman/uint256"
    24  
    25  	"github.com/ethereum/go-ethereum/core/types"
    26  	"github.com/ethereum/go-ethereum/crypto"
    27  )
    28  
    29  // Tests that transactions can be added to strict lists and list contents and
    30  // nonce boundaries are correctly maintained.
    31  func TestStrictTxListAdd(t *testing.T) {
    32  	// Generate a list of transactions to insert
    33  	key, _ := crypto.GenerateKey()
    34  
    35  	txs := make(types.Transactions, 1024)
    36  	for i := 0; i < len(txs); i++ {
    37  		txs[i] = transaction(uint64(i), 0, key)
    38  	}
    39  	// Insert the transactions in a random order
    40  	list := newTxList(true)
    41  	for _, v := range rand.Perm(len(txs)) {
    42  		list.Add(txs[v], DefaultTxPoolConfig.PriceBump)
    43  	}
    44  	// Verify internal state
    45  	if len(list.txs.items) != len(txs) {
    46  		t.Errorf("transaction count mismatch: have %d, want %d", len(list.txs.items), len(txs))
    47  	}
    48  	for i, tx := range txs {
    49  		if list.txs.items[tx.Nonce()] != tx {
    50  			t.Errorf("item %d: transaction mismatch: have %v, want %v", i, list.txs.items[tx.Nonce()], tx)
    51  		}
    52  	}
    53  }
    54  
    55  func BenchmarkTxListAdd(b *testing.B) {
    56  	// Generate a list of transactions to insert
    57  	key, _ := crypto.GenerateKey()
    58  
    59  	txs := make(types.Transactions, 100000)
    60  	for i := 0; i < len(txs); i++ {
    61  		txs[i] = transaction(uint64(i), 0, key)
    62  	}
    63  
    64  	// Insert the transactions in a random order
    65  	priceLimit := uint256.NewInt(DefaultTxPoolConfig.PriceLimit)
    66  	b.ResetTimer()
    67  	b.ReportAllocs()
    68  
    69  	for i := 0; i < b.N; i++ {
    70  		list := newTxList(true)
    71  
    72  		for _, v := range rand.Perm(len(txs)) {
    73  			list.Add(txs[v], DefaultTxPoolConfig.PriceBump)
    74  			list.Filter(priceLimit, DefaultTxPoolConfig.PriceBump)
    75  		}
    76  	}
    77  }