chain_indexer_test.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 implements the Ethereum consensus protocol.
  17. package core
  18. import (
  19. "encoding/binary"
  20. "math/big"
  21. "math/rand"
  22. "testing"
  23. "time"
  24. "github.com/ethereum/go-ethereum/core/types"
  25. "github.com/ethereum/go-ethereum/ethdb"
  26. )
  27. func TestChainIndexerSingle(t *testing.T) {
  28. // run multiple tests with randomized parameters
  29. for i := 0; i < 10; i++ {
  30. testChainIndexer(t, 1)
  31. }
  32. }
  33. func TestChainIndexerWithChildren(t *testing.T) {
  34. // run multiple tests with randomized parameters and different number of
  35. // chained indexers
  36. for i := 2; i < 8; i++ {
  37. testChainIndexer(t, i)
  38. }
  39. }
  40. // testChainIndexer runs a test with either a single ChainIndexer or a chain of multiple indexers
  41. // sectionSize and confirmReq parameters are randomized
  42. func testChainIndexer(t *testing.T, tciCount int) {
  43. db, _ := ethdb.NewMemDatabase()
  44. stop := make(chan struct{})
  45. tciList := make([]*testChainIndex, tciCount)
  46. var lastIndexer *ChainIndexer
  47. for i, _ := range tciList {
  48. tci := &testChainIndex{t: t, sectionSize: uint64(rand.Intn(100) + 1), confirmReq: uint64(rand.Intn(10)), processCh: make(chan uint64)}
  49. tciList[i] = tci
  50. tci.indexer = NewChainIndexer(db, ethdb.NewTable(db, string([]byte{byte(i)})), tci, tci.sectionSize, tci.confirmReq, 0, stop)
  51. if cs := tci.indexer.CanonicalSections(); cs != 0 {
  52. t.Errorf("Expected 0 canonical sections, got %d", cs)
  53. }
  54. if lastIndexer != nil {
  55. lastIndexer.AddChildIndexer(tci.indexer)
  56. }
  57. lastIndexer = tci.indexer
  58. }
  59. // expectCs expects a certain number of available canonical sections
  60. expectCs := func(indexer *ChainIndexer, expCs uint64) {
  61. cnt := 0
  62. for {
  63. cs := indexer.CanonicalSections()
  64. if cs == expCs {
  65. return
  66. }
  67. // keep trying for 10 seconds if it does not match
  68. cnt++
  69. if cnt == 10000 {
  70. t.Fatalf("Expected %d canonical sections, got %d", expCs, cs)
  71. }
  72. time.Sleep(time.Millisecond)
  73. }
  74. }
  75. // notify the indexer about a new head or rollback, then expect processed blocks if a section is processable
  76. notify := func(headNum, expFailAfter uint64, rollback bool) {
  77. tciList[0].indexer.newHead(headNum, rollback)
  78. if rollback {
  79. for _, tci := range tciList {
  80. headNum = tci.rollback(headNum)
  81. expectCs(tci.indexer, tci.stored)
  82. }
  83. } else {
  84. for _, tci := range tciList {
  85. var more bool
  86. headNum, more = tci.newBlocks(headNum, expFailAfter)
  87. if !more {
  88. break
  89. }
  90. expectCs(tci.indexer, tci.stored)
  91. }
  92. }
  93. }
  94. for i := uint64(0); i <= 100; i++ {
  95. testCanonicalHeader(db, i)
  96. }
  97. // start indexer with an already existing chain
  98. notify(100, 100, false)
  99. // add new blocks one by one
  100. for i := uint64(101); i <= 1000; i++ {
  101. testCanonicalHeader(db, i)
  102. notify(i, i, false)
  103. }
  104. // do a rollback
  105. notify(500, 500, true)
  106. // create new fork
  107. for i := uint64(501); i <= 1000; i++ {
  108. testCanonicalHeader(db, i)
  109. notify(i, i, false)
  110. }
  111. for i := uint64(1001); i <= 1500; i++ {
  112. testCanonicalHeader(db, i)
  113. }
  114. // create a failed processing scenario where less blocks are available at processing time than notified
  115. notify(2000, 1500, false)
  116. // notify about a rollback (which could have caused the missing blocks if happened during processing)
  117. notify(1500, 1500, true)
  118. // create new fork
  119. for i := uint64(1501); i <= 2000; i++ {
  120. testCanonicalHeader(db, i)
  121. notify(i, i, false)
  122. }
  123. close(stop)
  124. db.Close()
  125. }
  126. func testCanonicalHeader(db ethdb.Database, idx uint64) {
  127. var rnd [8]byte
  128. binary.BigEndian.PutUint64(rnd[:], uint64(rand.Int63()))
  129. header := &types.Header{Number: big.NewInt(int64(idx)), Extra: rnd[:]}
  130. if idx > 0 {
  131. header.ParentHash = GetCanonicalHash(db, idx-1)
  132. }
  133. WriteHeader(db, header)
  134. WriteCanonicalHash(db, header.Hash(), idx)
  135. }
  136. // testChainIndex implements ChainIndexerBackend
  137. type testChainIndex struct {
  138. t *testing.T
  139. sectionSize, confirmReq uint64
  140. section, headerCnt, stored uint64
  141. indexer *ChainIndexer
  142. processCh chan uint64
  143. }
  144. // newBlocks expects process calls after new blocks have arrived. If expFailAfter < headNum then
  145. // we are simulating a scenario where a rollback has happened after the processing has started and
  146. // the processing of a section fails.
  147. func (t *testChainIndex) newBlocks(headNum, expFailAfter uint64) (uint64, bool) {
  148. var newCount uint64
  149. if headNum >= t.confirmReq {
  150. newCount = (headNum + 1 - t.confirmReq) / t.sectionSize
  151. if newCount > t.stored {
  152. // expect processed blocks
  153. for exp := t.stored * t.sectionSize; exp < newCount*t.sectionSize; exp++ {
  154. if exp > expFailAfter {
  155. // rolled back after processing started, no more process calls expected
  156. // wait until updating is done to make sure that processing actually fails
  157. for {
  158. t.indexer.lock.Lock()
  159. u := t.indexer.updating
  160. t.indexer.lock.Unlock()
  161. if !u {
  162. break
  163. }
  164. time.Sleep(time.Millisecond)
  165. }
  166. newCount = exp / t.sectionSize
  167. break
  168. }
  169. select {
  170. case <-time.After(10 * time.Second):
  171. t.t.Fatalf("Expected processed block #%d, got nothing", exp)
  172. case proc := <-t.processCh:
  173. if proc != exp {
  174. t.t.Errorf("Expected processed block #%d, got #%d", exp, proc)
  175. }
  176. }
  177. }
  178. t.stored = newCount
  179. }
  180. }
  181. if t.stored == 0 {
  182. return 0, false
  183. }
  184. return t.stored*t.sectionSize - 1, true
  185. }
  186. func (t *testChainIndex) rollback(headNum uint64) uint64 {
  187. firstChanged := headNum / t.sectionSize
  188. if firstChanged < t.stored {
  189. t.stored = firstChanged
  190. }
  191. return t.stored * t.sectionSize
  192. }
  193. func (t *testChainIndex) Reset(section uint64) {
  194. t.section = section
  195. t.headerCnt = 0
  196. }
  197. func (t *testChainIndex) Process(header *types.Header) {
  198. t.headerCnt++
  199. if t.headerCnt > t.sectionSize {
  200. t.t.Error("Processing too many headers")
  201. }
  202. //t.processCh <- header.Number.Uint64()
  203. select {
  204. case <-time.After(10 * time.Second):
  205. t.t.Fatal("Unexpected call to Process")
  206. case t.processCh <- header.Number.Uint64():
  207. }
  208. }
  209. func (t *testChainIndex) Commit(db ethdb.Database) error {
  210. if t.headerCnt != t.sectionSize {
  211. t.t.Error("Not enough headers processed")
  212. }
  213. return nil
  214. }
  215. func (t *testChainIndex) UpdateMsg(done, all uint64) {}