chain_indexer_test.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. // Copyright 2017 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. "fmt"
  19. "math/big"
  20. "math/rand"
  21. "testing"
  22. "time"
  23. "github.com/ethereum/go-ethereum/common"
  24. "github.com/ethereum/go-ethereum/core/types"
  25. "github.com/ethereum/go-ethereum/ethdb"
  26. )
  27. // Runs multiple tests with randomized parameters.
  28. func TestChainIndexerSingle(t *testing.T) {
  29. for i := 0; i < 10; i++ {
  30. testChainIndexer(t, 1)
  31. }
  32. }
  33. // Runs multiple tests with randomized parameters and different number of
  34. // chain backends.
  35. func TestChainIndexerWithChildren(t *testing.T) {
  36. for i := 2; i < 8; i++ {
  37. testChainIndexer(t, i)
  38. }
  39. }
  40. // testChainIndexer runs a test with either a single chain indexer or a chain of
  41. // multiple backends. The section size and required confirmation count parameters
  42. // are randomized.
  43. func testChainIndexer(t *testing.T, count int) {
  44. db, _ := ethdb.NewMemDatabase()
  45. defer db.Close()
  46. // Create a chain of indexers and ensure they all report empty
  47. backends := make([]*testChainIndexBackend, count)
  48. for i := 0; i < count; i++ {
  49. var (
  50. sectionSize = uint64(rand.Intn(100) + 1)
  51. confirmsReq = uint64(rand.Intn(10))
  52. )
  53. backends[i] = &testChainIndexBackend{t: t, processCh: make(chan uint64)}
  54. backends[i].indexer = NewChainIndexer(db, ethdb.NewTable(db, string([]byte{byte(i)})), backends[i], sectionSize, confirmsReq, 0, fmt.Sprintf("indexer-%d", i))
  55. if sections, _, _ := backends[i].indexer.Sections(); sections != 0 {
  56. t.Fatalf("Canonical section count mismatch: have %v, want %v", sections, 0)
  57. }
  58. if i > 0 {
  59. backends[i-1].indexer.AddChildIndexer(backends[i].indexer)
  60. }
  61. }
  62. defer backends[0].indexer.Close() // parent indexer shuts down children
  63. // notify pings the root indexer about a new head or reorg, then expect
  64. // processed blocks if a section is processable
  65. notify := func(headNum, failNum uint64, reorg bool) {
  66. backends[0].indexer.newHead(headNum, reorg)
  67. if reorg {
  68. for _, backend := range backends {
  69. headNum = backend.reorg(headNum)
  70. backend.assertSections()
  71. }
  72. return
  73. }
  74. var cascade bool
  75. for _, backend := range backends {
  76. headNum, cascade = backend.assertBlocks(headNum, failNum)
  77. if !cascade {
  78. break
  79. }
  80. backend.assertSections()
  81. }
  82. }
  83. // inject inserts a new random canonical header into the database directly
  84. inject := func(number uint64) {
  85. header := &types.Header{Number: big.NewInt(int64(number)), Extra: big.NewInt(rand.Int63()).Bytes()}
  86. if number > 0 {
  87. header.ParentHash = GetCanonicalHash(db, number-1)
  88. }
  89. WriteHeader(db, header)
  90. WriteCanonicalHash(db, header.Hash(), number)
  91. }
  92. // Start indexer with an already existing chain
  93. for i := uint64(0); i <= 100; i++ {
  94. inject(i)
  95. }
  96. notify(100, 100, false)
  97. // Add new blocks one by one
  98. for i := uint64(101); i <= 1000; i++ {
  99. inject(i)
  100. notify(i, i, false)
  101. }
  102. // Do a reorg
  103. notify(500, 500, true)
  104. // Create new fork
  105. for i := uint64(501); i <= 1000; i++ {
  106. inject(i)
  107. notify(i, i, false)
  108. }
  109. for i := uint64(1001); i <= 1500; i++ {
  110. inject(i)
  111. }
  112. // Failed processing scenario where less blocks are available than notified
  113. notify(2000, 1500, false)
  114. // Notify about a reorg (which could have caused the missing blocks if happened during processing)
  115. notify(1500, 1500, true)
  116. // Create new fork
  117. for i := uint64(1501); i <= 2000; i++ {
  118. inject(i)
  119. notify(i, i, false)
  120. }
  121. }
  122. // testChainIndexBackend implements ChainIndexerBackend
  123. type testChainIndexBackend struct {
  124. t *testing.T
  125. indexer *ChainIndexer
  126. section, headerCnt, stored uint64
  127. processCh chan uint64
  128. }
  129. // assertSections verifies if a chain indexer has the correct number of section.
  130. func (b *testChainIndexBackend) assertSections() {
  131. // Keep trying for 3 seconds if it does not match
  132. var sections uint64
  133. for i := 0; i < 300; i++ {
  134. sections, _, _ = b.indexer.Sections()
  135. if sections == b.stored {
  136. return
  137. }
  138. time.Sleep(10 * time.Millisecond)
  139. }
  140. b.t.Fatalf("Canonical section count mismatch: have %v, want %v", sections, b.stored)
  141. }
  142. // assertBlocks expects processing calls after new blocks have arrived. If the
  143. // failNum < headNum then we are simulating a scenario where a reorg has happened
  144. // after the processing has started and the processing of a section fails.
  145. func (b *testChainIndexBackend) assertBlocks(headNum, failNum uint64) (uint64, bool) {
  146. var sections uint64
  147. if headNum >= b.indexer.confirmsReq {
  148. sections = (headNum + 1 - b.indexer.confirmsReq) / b.indexer.sectionSize
  149. if sections > b.stored {
  150. // expect processed blocks
  151. for expectd := b.stored * b.indexer.sectionSize; expectd < sections*b.indexer.sectionSize; expectd++ {
  152. if expectd > failNum {
  153. // rolled back after processing started, no more process calls expected
  154. // wait until updating is done to make sure that processing actually fails
  155. var updating bool
  156. for i := 0; i < 300; i++ {
  157. b.indexer.lock.Lock()
  158. updating = b.indexer.knownSections > b.indexer.storedSections
  159. b.indexer.lock.Unlock()
  160. if !updating {
  161. break
  162. }
  163. time.Sleep(10 * time.Millisecond)
  164. }
  165. if updating {
  166. b.t.Fatalf("update did not finish")
  167. }
  168. sections = expectd / b.indexer.sectionSize
  169. break
  170. }
  171. select {
  172. case <-time.After(10 * time.Second):
  173. b.t.Fatalf("Expected processed block #%d, got nothing", expectd)
  174. case processed := <-b.processCh:
  175. if processed != expectd {
  176. b.t.Errorf("Expected processed block #%d, got #%d", expectd, processed)
  177. }
  178. }
  179. }
  180. b.stored = sections
  181. }
  182. }
  183. if b.stored == 0 {
  184. return 0, false
  185. }
  186. return b.stored*b.indexer.sectionSize - 1, true
  187. }
  188. func (b *testChainIndexBackend) reorg(headNum uint64) uint64 {
  189. firstChanged := headNum / b.indexer.sectionSize
  190. if firstChanged < b.stored {
  191. b.stored = firstChanged
  192. }
  193. return b.stored * b.indexer.sectionSize
  194. }
  195. func (b *testChainIndexBackend) Reset(section uint64, prevHead common.Hash) error {
  196. b.section = section
  197. b.headerCnt = 0
  198. return nil
  199. }
  200. func (b *testChainIndexBackend) Process(header *types.Header) {
  201. b.headerCnt++
  202. if b.headerCnt > b.indexer.sectionSize {
  203. b.t.Error("Processing too many headers")
  204. }
  205. //t.processCh <- header.Number.Uint64()
  206. select {
  207. case <-time.After(10 * time.Second):
  208. b.t.Fatal("Unexpected call to Process")
  209. case b.processCh <- header.Number.Uint64():
  210. }
  211. }
  212. func (b *testChainIndexBackend) Commit() error {
  213. if b.headerCnt != b.indexer.sectionSize {
  214. b.t.Error("Not enough headers processed")
  215. }
  216. return nil
  217. }