1717package filtermaps
1818
1919import (
20+ "fmt"
2021 "math"
2122 "sync"
2223
@@ -75,6 +76,7 @@ type Config struct {
7576// TODO blockId vs blockHash?
7677// TODO disable, export, history, finalized
7778func NewIndexer (db ethdb.KeyValueStore , params * Params , config Config ) * Indexer {
79+ params .sanitize ()
7880 mapDb := newMapDatabase (params , db , config .HashScheme )
7981 ix := & Indexer {
8082 config : config ,
@@ -88,17 +90,25 @@ func NewIndexer(db ethdb.KeyValueStore, params *Params, config Config) *Indexer
8890 ix .updateTargetTailEpoch ()
8991 ix .updateActiveViewTailEpoch ()
9092 ix .updateTailState ()
93+ fmt .Println ("init tail epoch" , ix .tailEpoch , "tail target" , ix .targetTailEpoch , "head number" , ix .headNumber )
9194 return ix
9295}
9396
9497func (ix * Indexer ) initMapBoundary (nextMap , limitMap uint32 ) * renderState {
98+ fmt .Println ("initMapBoundary" , nextMap , limitMap )
9599 rs := & renderState {
96100 params : ix .storage .params ,
97101 renderRange : common .NewRange [uint32 ](nextMap , limitMap - nextMap ),
98102 currentMap : ix .storage .params .newMemoryMap (),
99103 }
100- for nextMap > 0 {
104+ for {
101105 nextMap = ix .storage .lastBoundaryBefore (nextMap )
106+ fmt .Println (" lbb" , nextMap )
107+ if nextMap == 0 {
108+ // initialize at genesis
109+ fmt .Println (" genesis" )
110+ return rs
111+ }
102112 lastNumber , lastHash , err := ix .storage .getLastBlockOfMap (nextMap - 1 )
103113 if err != nil {
104114 log .Error ("Last block of map not found, reverting database" , "mapIndex" , nextMap )
@@ -118,10 +128,9 @@ func (ix *Indexer) initMapBoundary(nextMap, limitMap uint32) *renderState {
118128 rs .nextBlock = lastNumber
119129 rs .partialBlock = true
120130 rs .partialBlockHash = lastHash
131+ fmt .Println (" nextBlock" , rs .nextBlock , "mapIndex" , rs .mapIndex )
121132 return rs
122133 }
123- // initialize at genesis
124- return rs
125134}
126135
127136func (ix * Indexer ) initSnapshot (snapshot * IndexView ) * renderState {
@@ -132,6 +141,7 @@ func (ix *Indexer) initSnapshot(snapshot *IndexView) *renderState {
132141 return nil
133142 }
134143
144+ fmt .Println ("initSnapshot" , snapshot .headBlockHash )
135145 return & renderState {
136146 params : ix .storage .params ,
137147 renderRange : common .NewRange [uint32 ](snapshot .headMapIndex , math .MaxUint32 - snapshot .headMapIndex ),
@@ -142,6 +152,7 @@ func (ix *Indexer) initSnapshot(snapshot *IndexView) *renderState {
142152}
143153
144154func (ix * Indexer ) revertMaps (mapIndex uint32 ) {
155+ fmt .Println ("revertMaps" , mapIndex )
145156 if mapIndex < ix .storage .lastBoundaryBefore (math .MaxUint32 ) {
146157 for hash , iv := range ix .snapshots {
147158 if iv .firstMemoryMap > mapIndex {
@@ -157,7 +168,7 @@ func (ix *Indexer) revertMaps(mapIndex uint32) {
157168 if mapIndex <= ix .headRenderer .mapIndex {
158169 ix .headRenderer = nil
159170 }
160- if mapIndex <= ix .tailRenderer .mapIndex {
171+ if ix . tailRenderer != nil && mapIndex <= ix .tailRenderer .mapIndex {
161172 ix .tailRenderer = nil
162173 }
163174}
@@ -276,26 +287,38 @@ func (ix *Indexer) AddBlockData(headers []*types.Header, receipts []types.Receip
276287 return ix .Status ()
277288}
278289
290+ // epochsUntilBlock returns the numer of epochs in the checkpoint list whose
291+ // last block number is less than or equal to the specified number.
279292func (cpList checkpointList ) epochsUntilBlock (number uint64 ) uint32 {
280- first , afterLast := uint32 (0 ), uint32 (len (cpList ))
281- for first + 1 < afterLast {
282- mid := (first + afterLast ) / 2
293+ fmt .Println ("epochsUntilBlock" , number )
294+ first , last := uint32 (0 ), uint32 (len (cpList ))
295+ for first < last {
296+ fmt .Println (" *" , first , last )
297+ mid := (first + last ) / 2
283298 if cpList [mid ].BlockNumber > number {
284- afterLast = mid
299+ last = mid
285300 } else {
286- first = mid
301+ first = mid + 1
287302 }
288303 }
304+ fmt .Println (" **" , first )
289305 return first
290306}
291307
292308func (ix * Indexer ) tryCheckpointInit (number uint64 , id common.Hash ) {
309+ fmt .Println ("tryCheckpointInit" , number , id )
293310 var ci int
294311 for ci < len (ix .checkpoints ) {
295312 cpList := ix .checkpoints [ci ]
296313 epochs := cpList .epochsUntilBlock (number )
314+ fmt .Println (" cpList" , len (cpList ), epochs )
297315 if epochs == 0 || cpList [epochs - 1 ].BlockNumber != number {
298- // no matching block number, skip list (a relevant block might match later)
316+ if epochs == 0 {
317+ fmt .Println (" skip *" , number )
318+ } else {
319+ fmt .Println (" skip" , cpList [epochs - 1 ].BlockNumber , number )
320+ }
321+ // block number does not match, skip list (a relevant block might match later)
299322 ci ++
300323 continue
301324 }
@@ -304,6 +327,7 @@ func (ix *Indexer) tryCheckpointInit(number uint64, id common.Hash) {
304327 if err := ix .storage .addKnownEpochs (cpList [:epochs ]); err == nil {
305328 ix .checkpoints = []checkpointList {cpList }
306329 ix .headRenderer = ix .initMapBoundary (epochs * ix .storage .params .mapsPerEpoch , math .MaxUint32 )
330+ fmt .Println (" success" )
307331 return
308332 } else {
309333 log .Error ("Error initializing epoch boundaries" , "error" , err )
@@ -313,6 +337,7 @@ func (ix *Indexer) tryCheckpointInit(number uint64, id common.Hash) {
313337 ix .checkpoints [ci ] = ix .checkpoints [len (ix .checkpoints )- 1 ]
314338 ix .checkpoints = ix .checkpoints [:len (ix .checkpoints )- 1 ]
315339 }
340+ fmt .Println (" no match" )
316341}
317342
318343func (ix * Indexer ) SetFinalized (blockNumber uint64 ) {
@@ -361,23 +386,29 @@ func (ix *Indexer) Status() (bool, common.Range[uint64]) {
361386}
362387
363388func (ix * Indexer ) needBlocks () common.Range [uint64 ] {
389+ fmt .Println ("needBlocks" , ix .finalized , ix .headRenderer .nextBlock , ix .tailRenderer != nil )
364390 if ix .finalized > ix .headRenderer .nextBlock {
365391 // request potential checkpoint in this range if available
366392 for _ , cpList := range ix .checkpoints {
393+ fmt .Println ("cpList" , len (cpList ))
367394 if epochs := cpList .epochsUntilBlock (ix .headNumber ); epochs > 0 {
368395 blockNumber := cpList [epochs - 1 ].BlockNumber
396+ fmt .Println ("epochs" , epochs , "blockNumber" , blockNumber )
369397 if ix .storage .lastBoundaryBefore (math .MaxUint32 ) >= epochs * ix .storage .params .mapsPerEpoch ||
370398 blockNumber <= ix .headRenderer .nextBlock || blockNumber < ix .historyCutoff {
399+ fmt .Println (" cont" , ix .storage .lastBoundaryBefore (math .MaxUint32 ), ix .historyCutoff , ix .storage .params .mapsPerEpoch )
371400 continue
372401 }
402+ fmt .Println (" chk" , blockNumber )
373403 return common .NewRange [uint64 ](blockNumber , 1 )
374404 }
375405 }
376406 }
377407 if ix .headRenderer .nextBlock <= ix .headNumber && ix .headRenderer .nextBlock > ix .historyCutoff {
378408 return common .NewRange [uint64 ](ix .headRenderer .nextBlock , ix .headNumber + 1 - ix .headRenderer .nextBlock )
379409 }
380- if ix .tailRenderer .nextBlock <= ix .tailRenderLast && ix .tailRenderer .nextBlock > ix .historyCutoff {
410+ if ix .tailRenderer != nil &&
411+ ix .tailRenderer .nextBlock <= ix .tailRenderLast && ix .tailRenderer .nextBlock > ix .historyCutoff {
381412 return common .NewRange [uint64 ](ix .tailRenderer .nextBlock , ix .tailRenderLast + 1 - ix .tailRenderer .nextBlock )
382413 }
383414 return common.Range [uint64 ]{}
0 commit comments