tx_list_test.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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/rand"
  19. "testing"
  20. "github.com/ethereum/go-ethereum/core/types"
  21. "github.com/ethereum/go-ethereum/crypto"
  22. )
  23. // Tests that transactions can be added to strict lists and list contents and
  24. // nonce boundaries are correctly maintained.
  25. func TestStrictTxListAdd(t *testing.T) {
  26. // Generate a list of transactions to insert
  27. key, _ := crypto.GenerateKey()
  28. txs := make(types.Transactions, 1024)
  29. for i := 0; i < len(txs); i++ {
  30. txs[i] = transaction(uint64(i), 0, key)
  31. }
  32. // Insert the transactions in a random order
  33. list := newTxList(true)
  34. for _, v := range rand.Perm(len(txs)) {
  35. list.Add(txs[v], DefaultTxPoolConfig.PriceBump)
  36. }
  37. // Verify internal state
  38. if len(list.txs.items) != len(txs) {
  39. t.Errorf("transaction count mismatch: have %d, want %d", len(list.txs.items), len(txs))
  40. }
  41. for i, tx := range txs {
  42. if list.txs.items[tx.Nonce()] != tx {
  43. t.Errorf("item %d: transaction mismatch: have %v, want %v", i, list.txs.items[tx.Nonce()], tx)
  44. }
  45. }
  46. }