chain_iterator_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. to := common.BytesToAddress([]byte{0x11})
  32. block = types.NewBlock(&types.Header{Number: big.NewInt(int64(0))}, nil, nil, nil, newHasher()) // Empty genesis block
  33. WriteBlock(chainDb, block)
  34. WriteCanonicalHash(chainDb, block.Hash(), block.NumberU64())
  35. for i := uint64(1); i <= 10; i++ {
  36. var tx *types.Transaction
  37. if i%2 == 0 {
  38. tx = types.NewTx(&types.LegacyTx{
  39. Nonce: i,
  40. GasPrice: big.NewInt(11111),
  41. Gas: 1111,
  42. To: &to,
  43. Value: big.NewInt(111),
  44. Data: []byte{0x11, 0x11, 0x11},
  45. })
  46. } else {
  47. tx = types.NewTx(&types.AccessListTx{
  48. ChainID: big.NewInt(1337),
  49. Nonce: i,
  50. GasPrice: big.NewInt(11111),
  51. Gas: 1111,
  52. To: &to,
  53. Value: big.NewInt(111),
  54. Data: []byte{0x11, 0x11, 0x11},
  55. })
  56. }
  57. txs = append(txs, tx)
  58. block = types.NewBlock(&types.Header{Number: big.NewInt(int64(i))}, []*types.Transaction{tx}, nil, nil, newHasher())
  59. WriteBlock(chainDb, block)
  60. WriteCanonicalHash(chainDb, block.Hash(), block.NumberU64())
  61. }
  62. var cases = []struct {
  63. from, to uint64
  64. reverse bool
  65. expect []int
  66. }{
  67. {0, 11, true, []int{10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0}},
  68. {0, 0, true, nil},
  69. {0, 5, true, []int{4, 3, 2, 1, 0}},
  70. {10, 11, true, []int{10}},
  71. {0, 11, false, []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}},
  72. {0, 0, false, nil},
  73. {10, 11, false, []int{10}},
  74. }
  75. for i, c := range cases {
  76. var numbers []int
  77. hashCh := iterateTransactions(chainDb, c.from, c.to, c.reverse, nil)
  78. if hashCh != nil {
  79. for h := range hashCh {
  80. numbers = append(numbers, int(h.number))
  81. if len(h.hashes) > 0 {
  82. if got, exp := h.hashes[0], txs[h.number-1].Hash(); got != exp {
  83. t.Fatalf("block %d: hash wrong, got %x exp %x", h.number, got, exp)
  84. }
  85. }
  86. }
  87. }
  88. if !c.reverse {
  89. sort.Ints(numbers)
  90. } else {
  91. sort.Sort(sort.Reverse(sort.IntSlice(numbers)))
  92. }
  93. if !reflect.DeepEqual(numbers, c.expect) {
  94. t.Fatalf("Case %d failed, visit element mismatch, want %v, got %v", i, c.expect, numbers)
  95. }
  96. }
  97. }
  98. func TestIndexTransactions(t *testing.T) {
  99. // Construct test chain db
  100. chainDb := NewMemoryDatabase()
  101. var block *types.Block
  102. var txs []*types.Transaction
  103. to := common.BytesToAddress([]byte{0x11})
  104. // Write empty genesis block
  105. block = types.NewBlock(&types.Header{Number: big.NewInt(int64(0))}, nil, nil, nil, newHasher())
  106. WriteBlock(chainDb, block)
  107. WriteCanonicalHash(chainDb, block.Hash(), block.NumberU64())
  108. for i := uint64(1); i <= 10; i++ {
  109. var tx *types.Transaction
  110. if i%2 == 0 {
  111. tx = types.NewTx(&types.LegacyTx{
  112. Nonce: i,
  113. GasPrice: big.NewInt(11111),
  114. Gas: 1111,
  115. To: &to,
  116. Value: big.NewInt(111),
  117. Data: []byte{0x11, 0x11, 0x11},
  118. })
  119. } else {
  120. tx = types.NewTx(&types.AccessListTx{
  121. ChainID: big.NewInt(1337),
  122. Nonce: i,
  123. GasPrice: big.NewInt(11111),
  124. Gas: 1111,
  125. To: &to,
  126. Value: big.NewInt(111),
  127. Data: []byte{0x11, 0x11, 0x11},
  128. })
  129. }
  130. txs = append(txs, tx)
  131. block = types.NewBlock(&types.Header{Number: big.NewInt(int64(i))}, []*types.Transaction{tx}, nil, nil, newHasher())
  132. WriteBlock(chainDb, block)
  133. WriteCanonicalHash(chainDb, block.Hash(), block.NumberU64())
  134. }
  135. // verify checks whether the tx indices in the range [from, to)
  136. // is expected.
  137. verify := func(from, to int, exist bool, tail uint64) {
  138. for i := from; i < to; i++ {
  139. if i == 0 {
  140. continue
  141. }
  142. number := ReadTxLookupEntry(chainDb, txs[i-1].Hash())
  143. if exist && number == nil {
  144. t.Fatalf("Transaction index %d missing", i)
  145. }
  146. if !exist && number != nil {
  147. t.Fatalf("Transaction index %d is not deleted", i)
  148. }
  149. }
  150. number := ReadTxIndexTail(chainDb)
  151. if number == nil || *number != tail {
  152. t.Fatalf("Transaction tail mismatch")
  153. }
  154. }
  155. IndexTransactions(chainDb, 5, 11, nil)
  156. verify(5, 11, true, 5)
  157. verify(0, 5, false, 5)
  158. IndexTransactions(chainDb, 0, 5, nil)
  159. verify(0, 11, true, 0)
  160. UnindexTransactions(chainDb, 0, 5, nil)
  161. verify(5, 11, true, 5)
  162. verify(0, 5, false, 5)
  163. UnindexTransactions(chainDb, 5, 11, nil)
  164. verify(0, 11, false, 11)
  165. // Testing corner cases
  166. signal := make(chan struct{})
  167. var once sync.Once
  168. indexTransactionsForTesting(chainDb, 5, 11, signal, func(n uint64) bool {
  169. if n <= 8 {
  170. once.Do(func() {
  171. close(signal)
  172. })
  173. return false
  174. }
  175. return true
  176. })
  177. verify(9, 11, true, 9)
  178. verify(0, 9, false, 9)
  179. IndexTransactions(chainDb, 0, 9, nil)
  180. signal = make(chan struct{})
  181. var once2 sync.Once
  182. unindexTransactionsForTesting(chainDb, 0, 11, signal, func(n uint64) bool {
  183. if n >= 8 {
  184. once2.Do(func() {
  185. close(signal)
  186. })
  187. return false
  188. }
  189. return true
  190. })
  191. verify(8, 11, true, 8)
  192. verify(0, 8, false, 8)
  193. }