@@ -35,17 +35,12 @@ func (f *Fetcher) handlerSync(ctx context.Context) (success bool, err error) {
3535 return success , err
3636 }
3737 if errors .Is (err , ErrEmpy ) {
38- // no chain-cache present yet,
39- // just set the chain-update without
38+ // no chain-cache present yet, just set the chain-update without
4039 // checking for reorgs
41- // FIXME: here we could incorporate a starting block
42- // option, fetch the starting block, and set this in
43- // the cache and sync from there.
4440 removedSegment = nil
4541 updatedSegment = f .chainUpdate
4642 log .Trace ().Msg ("internal chain cache empty, setting updated chain segment" )
4743 } else if err != nil {
48- //TODO: what to do on db error?
4944 success = false
5045 return success , err
5146 } else {
@@ -63,6 +58,11 @@ func (f *Fetcher) handlerSync(ctx context.Context) (success bool, err error) {
6358 return success , fmt .Errorf ("chain-update difference too big: %w" , errUint64Overflow )
6459 }
6560 diff := diffBig .Uint64 ()
61+ queryBlocks := MaxRequestBlockRange
62+ // cap the extend range at the diff to the update to not overshoot
63+ if diff < uint64 (queryBlocks ) {
64+ queryBlocks = int (diff )
65+ }
6666
6767 // we are not synced to the chain-update
6868 // so first construct an update to the right of the synced chain
@@ -86,20 +86,33 @@ func (f *Fetcher) handlerSync(ctx context.Context) (success bool, err error) {
8686 }
8787 removedSegment = nil
8888 success = false
89- result , errr := syncedChain .UpdateLatest (ctx , f .ethClient , f .chainUpdate )
90- result , errr := syncedChain .UpdateLatest (ctx , f .client , f .chainUpdate )
91- if errr != nil {
92- // TODO: for ErrUpdateBlockTooFarInPast this should shut down the
93- // client? Since we can't really play back a reorg that reaches too far in the past.
94- // Now as long as the chain-cache eviction policy is not agressive (easily doable)
95- log .Error ().Err (err ).Msg ("error updating chain" )
89+ } else {
90+ result , updateErr := syncedChain .UpdateLatest (ctx , f .ethClient , f .chainUpdate )
91+ success = true
92+ if updateErr != nil {
93+ log .Error ().Err (err ).Msg ("error updating chain with latest segment" )
94+ if errors .Is (err , chainsegment .ErrUpdateBlockTooFarInPast ) {
95+ //TODO: what should we do on 'ErrUpdateBlockTooFarInPast'?
96+ // We can't provide handler calls with the same accuracy of
97+ // information on the potentially "removed" chain-segment,
98+ // since our chain-cache does not have the full old chain segment
99+ // in it's storage anymore, and especially the block-hash
100+ // of the reorged away chain is not present anymore.
101+ // The client should probably panic with a critical log error.
102+ // In general this is very unlikely when the chain-cache capacity is
103+ // larger than the most unlikely, still realistic reorg-size.
104+ // However the described condition might currently occur during
105+ // initial syncing, when the block-cache is not filled to capacity
106+ // yet.
107+ log .Warn ().Err (err ).Msg ("received a reorg that pre-dates the internal chain-cache." +
108+ " ignoring chain-update for now, but this condition might be irrecoverable" )
109+ }
96110 err = updateErr
97111 // Now as long as the chain-cache eviction policy is not aggressive (easily doable)
98112 removedSegment = result .RemovedSegment
99113 updatedSegment = result .UpdatedSegment
100114 // we will process the whole segment of the chain update
101115 f .chainUpdate = nil
102- success = true
103116 }
104117 }
105118
0 commit comments