github.com/cgcardona/r-subnet-evm@v0.1.5/core/tx_list_test.go (about) 1 // (c) 2019-2020, Ava Labs, Inc. 2 // 3 // This file is a derived work, based on the go-ethereum library whose original 4 // notices appear below. 5 // 6 // It is distributed under a license compatible with the licensing terms of the 7 // original code from which it is derived. 8 // 9 // Much love to the original authors for their work. 10 // ********** 11 // Copyright 2016 The go-ethereum Authors 12 // This file is part of the go-ethereum library. 13 // 14 // The go-ethereum library is free software: you can redistribute it and/or modify 15 // it under the terms of the GNU Lesser General Public License as published by 16 // the Free Software Foundation, either version 3 of the License, or 17 // (at your option) any later version. 18 // 19 // The go-ethereum library is distributed in the hope that it will be useful, 20 // but WITHOUT ANY WARRANTY; without even the implied warranty of 21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22 // GNU Lesser General Public License for more details. 23 // 24 // You should have received a copy of the GNU Lesser General Public License 25 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 26 27 package core 28 29 import ( 30 "math/big" 31 "math/rand" 32 "testing" 33 34 "github.com/cgcardona/r-subnet-evm/core/types" 35 "github.com/ethereum/go-ethereum/crypto" 36 ) 37 38 // Tests that transactions can be added to strict lists and list contents and 39 // nonce boundaries are correctly maintained. 40 func TestStrictTxListAdd(t *testing.T) { 41 // Generate a list of transactions to insert 42 key, _ := crypto.GenerateKey() 43 44 txs := make(types.Transactions, 1024) 45 for i := 0; i < len(txs); i++ { 46 txs[i] = transaction(uint64(i), 0, key) 47 } 48 // Insert the transactions in a random order 49 list := newTxList(true) 50 for _, v := range rand.Perm(len(txs)) { 51 list.Add(txs[v], DefaultTxPoolConfig.PriceBump) 52 } 53 // Verify internal state 54 if len(list.txs.items) != len(txs) { 55 t.Errorf("transaction count mismatch: have %d, want %d", len(list.txs.items), len(txs)) 56 } 57 for i, tx := range txs { 58 if list.txs.items[tx.Nonce()] != tx { 59 t.Errorf("item %d: transaction mismatch: have %v, want %v", i, list.txs.items[tx.Nonce()], tx) 60 } 61 } 62 } 63 64 func BenchmarkTxListAdd(b *testing.B) { 65 // Generate a list of transactions to insert 66 key, _ := crypto.GenerateKey() 67 68 txs := make(types.Transactions, 100000) 69 for i := 0; i < len(txs); i++ { 70 txs[i] = transaction(uint64(i), 0, key) 71 } 72 // Insert the transactions in a random order 73 priceLimit := big.NewInt(int64(DefaultTxPoolConfig.PriceLimit)) 74 b.ResetTimer() 75 for i := 0; i < b.N; i++ { 76 list := newTxList(true) 77 for _, v := range rand.Perm(len(txs)) { 78 list.Add(txs[v], DefaultTxPoolConfig.PriceBump) 79 list.Filter(priceLimit, DefaultTxPoolConfig.PriceBump) 80 } 81 } 82 }