chain_iterator_test.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright 2019 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 rawdb
  17. import (
  18. "math/big"
  19. "reflect"
  20. "sort"
  21. "testing"
  22. "github.com/ethereum/go-ethereum/common"
  23. "github.com/ethereum/go-ethereum/core/types"
  24. )
  25. func TestChainIterator(t *testing.T) {
  26. // Construct test chain db
  27. chainDb := NewMemoryDatabase()
  28. var block *types.Block
  29. var txs []*types.Transaction
  30. for i := uint64(0); i <= 10; i++ {
  31. if i == 0 {
  32. block = types.NewBlock(&types.Header{Number: big.NewInt(int64(i))}, nil, nil, nil) // Empty genesis block
  33. } else {
  34. tx := types.NewTransaction(i, common.BytesToAddress([]byte{0x11}), big.NewInt(111), 1111, big.NewInt(11111), []byte{0x11, 0x11, 0x11})
  35. txs = append(txs, tx)
  36. block = types.NewBlock(&types.Header{Number: big.NewInt(int64(i))}, []*types.Transaction{tx}, nil, nil)
  37. }
  38. WriteBlock(chainDb, block)
  39. WriteCanonicalHash(chainDb, block.Hash(), block.NumberU64())
  40. }
  41. var cases = []struct {
  42. from, to uint64
  43. reverse bool
  44. expect []int
  45. }{
  46. {0, 11, true, []int{10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0}},
  47. {0, 0, true, nil},
  48. {0, 5, true, []int{4, 3, 2, 1, 0}},
  49. {10, 11, true, []int{10}},
  50. {0, 11, false, []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}},
  51. {0, 0, false, nil},
  52. {10, 11, false, []int{10}},
  53. }
  54. for i, c := range cases {
  55. var numbers []int
  56. hashCh, _ := iterateTransactions(chainDb, c.from, c.to, c.reverse)
  57. if hashCh != nil {
  58. for h := range hashCh {
  59. numbers = append(numbers, int(h.number))
  60. if len(h.hashes) > 0 {
  61. if got, exp := h.hashes[0], txs[h.number-1].Hash(); got != exp {
  62. t.Fatalf("hash wrong, got %x exp %x", got, exp)
  63. }
  64. }
  65. }
  66. }
  67. if !c.reverse {
  68. sort.Ints(numbers)
  69. } else {
  70. sort.Sort(sort.Reverse(sort.IntSlice(numbers)))
  71. }
  72. if !reflect.DeepEqual(numbers, c.expect) {
  73. t.Fatalf("Case %d failed, visit element mismatch, want %v, got %v", i, c.expect, numbers)
  74. }
  75. }
  76. }