github.com/popcateum/go-popcateum@v1.1.2/core/tx_list_test.go (about)

     1  // Copyright 2016 The go-popcateum Authors
     2  // This file is part of the go-popcateum library.
     3  //
     4  // The go-popcateum 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-popcateum 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-popcateum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package core
    18  
    19  import (
    20  	"math/big"
    21  	"math/rand"
    22  	"testing"
    23  
    24  	"github.com/popcateum/go-popcateum/core/types"
    25  	"github.com/popcateum/go-popcateum/crypto"
    26  )
    27  
    28  // Tests that transactions can be added to strict lists and list contents and
    29  // nonce boundaries are correctly maintained.
    30  func TestStrictTxListAdd(t *testing.T) {
    31  	// Generate a list of transactions to insert
    32  	key, _ := crypto.GenerateKey()
    33  
    34  	txs := make(types.Transactions, 1024)
    35  	for i := 0; i < len(txs); i++ {
    36  		txs[i] = transaction(uint64(i), 0, key)
    37  	}
    38  	// Insert the transactions in a random order
    39  	list := newTxList(true)
    40  	for _, v := range rand.Perm(len(txs)) {
    41  		list.Add(txs[v], DefaultTxPoolConfig.PriceBump)
    42  	}
    43  	// Verify internal state
    44  	if len(list.txs.items) != len(txs) {
    45  		t.Errorf("transaction count mismatch: have %d, want %d", len(list.txs.items), len(txs))
    46  	}
    47  	for i, tx := range txs {
    48  		if list.txs.items[tx.Nonce()] != tx {
    49  			t.Errorf("item %d: transaction mismatch: have %v, want %v", i, list.txs.items[tx.Nonce()], tx)
    50  		}
    51  	}
    52  }
    53  
    54  func BenchmarkTxListAdd(t *testing.B) {
    55  	// Generate a list of transactions to insert
    56  	key, _ := crypto.GenerateKey()
    57  
    58  	txs := make(types.Transactions, 100000)
    59  	for i := 0; i < len(txs); i++ {
    60  		txs[i] = transaction(uint64(i), 0, key)
    61  	}
    62  	// Insert the transactions in a random order
    63  	list := newTxList(true)
    64  	priceLimit := big.NewInt(int64(DefaultTxPoolConfig.PriceLimit))
    65  	t.ResetTimer()
    66  	for _, v := range rand.Perm(len(txs)) {
    67  		list.Add(txs[v], DefaultTxPoolConfig.PriceBump)
    68  		list.Filter(priceLimit, DefaultTxPoolConfig.PriceBump)
    69  	}
    70  }