chain_iterator_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. "sync"
  22. "testing"
  23. "github.com/ethereum/go-ethereum/common"
  24. "github.com/ethereum/go-ethereum/core/types"
  25. )
  26. func TestChainIterator(t *testing.T) {
  27. // Construct test chain db
  28. chainDb := NewMemoryDatabase()
  29. var block *types.Block
  30. var txs []*types.Transaction
  31. for i := uint64(0); i <= 10; i++ {
  32. if i == 0 {
  33. block = types.NewBlock(&types.Header{Number: big.NewInt(int64(i))}, nil, nil, nil, newHasher()) // Empty genesis block
  34. } else {
  35. tx := types.NewTransaction(i, common.BytesToAddress([]byte{0x11}), big.NewInt(111), 1111, big.NewInt(11111), []byte{0x11, 0x11, 0x11})
  36. txs = append(txs, tx)
  37. block = types.NewBlock(&types.Header{Number: big.NewInt(int64(i))}, []*types.Transaction{tx}, nil, nil, newHasher())
  38. }
  39. WriteBlock(chainDb, block)
  40. WriteCanonicalHash(chainDb, block.Hash(), block.NumberU64())
  41. }
  42. var cases = []struct {
  43. from, to uint64
  44. reverse bool
  45. expect []int
  46. }{
  47. {0, 11, true, []int{10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0}},
  48. {0, 0, true, nil},
  49. {0, 5, true, []int{4, 3, 2, 1, 0}},
  50. {10, 11, true, []int{10}},
  51. {0, 11, false, []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}},
  52. {0, 0, false, nil},
  53. {10, 11, false, []int{10}},
  54. }
  55. for i, c := range cases {
  56. var numbers []int
  57. hashCh := iterateTransactions(chainDb, c.from, c.to, c.reverse, nil)
  58. if hashCh != nil {
  59. for h := range hashCh {
  60. numbers = append(numbers, int(h.number))
  61. if len(h.hashes) > 0 {
  62. if got, exp := h.hashes[0], txs[h.number-1].Hash(); got != exp {
  63. t.Fatalf("hash wrong, got %x exp %x", got, exp)
  64. }
  65. }
  66. }
  67. }
  68. if !c.reverse {
  69. sort.Ints(numbers)
  70. } else {
  71. sort.Sort(sort.Reverse(sort.IntSlice(numbers)))
  72. }
  73. if !reflect.DeepEqual(numbers, c.expect) {
  74. t.Fatalf("Case %d failed, visit element mismatch, want %v, got %v", i, c.expect, numbers)
  75. }
  76. }
  77. }
  78. func TestIndexTransactions(t *testing.T) {
  79. // Construct test chain db
  80. chainDb := NewMemoryDatabase()
  81. var block *types.Block
  82. var txs []*types.Transaction
  83. for i := uint64(0); i <= 10; i++ {
  84. if i == 0 {
  85. block = types.NewBlock(&types.Header{Number: big.NewInt(int64(i))}, nil, nil, nil, newHasher()) // Empty genesis block
  86. } else {
  87. tx := types.NewTransaction(i, common.BytesToAddress([]byte{0x11}), big.NewInt(111), 1111, big.NewInt(11111), []byte{0x11, 0x11, 0x11})
  88. txs = append(txs, tx)
  89. block = types.NewBlock(&types.Header{Number: big.NewInt(int64(i))}, []*types.Transaction{tx}, nil, nil, newHasher())
  90. }
  91. WriteBlock(chainDb, block)
  92. WriteCanonicalHash(chainDb, block.Hash(), block.NumberU64())
  93. }
  94. // verify checks whether the tx indices in the range [from, to)
  95. // is expected.
  96. verify := func(from, to int, exist bool, tail uint64) {
  97. for i := from; i < to; i++ {
  98. if i == 0 {
  99. continue
  100. }
  101. number := ReadTxLookupEntry(chainDb, txs[i-1].Hash())
  102. if exist && number == nil {
  103. t.Fatalf("Transaction indice missing")
  104. }
  105. if !exist && number != nil {
  106. t.Fatalf("Transaction indice is not deleted")
  107. }
  108. }
  109. number := ReadTxIndexTail(chainDb)
  110. if number == nil || *number != tail {
  111. t.Fatalf("Transaction tail mismatch")
  112. }
  113. }
  114. IndexTransactions(chainDb, 5, 11, nil)
  115. verify(5, 11, true, 5)
  116. verify(0, 5, false, 5)
  117. IndexTransactions(chainDb, 0, 5, nil)
  118. verify(0, 11, true, 0)
  119. UnindexTransactions(chainDb, 0, 5, nil)
  120. verify(5, 11, true, 5)
  121. verify(0, 5, false, 5)
  122. UnindexTransactions(chainDb, 5, 11, nil)
  123. verify(0, 11, false, 11)
  124. // Testing corner cases
  125. signal := make(chan struct{})
  126. var once sync.Once
  127. indexTransactionsForTesting(chainDb, 5, 11, signal, func(n uint64) bool {
  128. if n <= 8 {
  129. once.Do(func() {
  130. close(signal)
  131. })
  132. return false
  133. }
  134. return true
  135. })
  136. verify(9, 11, true, 9)
  137. verify(0, 9, false, 9)
  138. IndexTransactions(chainDb, 0, 9, nil)
  139. signal = make(chan struct{})
  140. var once2 sync.Once
  141. unindexTransactionsForTesting(chainDb, 0, 11, signal, func(n uint64) bool {
  142. if n >= 8 {
  143. once2.Do(func() {
  144. close(signal)
  145. })
  146. return false
  147. }
  148. return true
  149. })
  150. verify(8, 11, true, 8)
  151. verify(0, 8, false, 8)
  152. }