tx_list_test.go 2.3 KB

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