@@ -35,17 +35,12 @@ func (f *Fetcher) handlerSync(ctx context.Context) (success bool, err error) {
35
35
return success , err
36
36
}
37
37
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
40
39
// 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.
44
40
removedSegment = nil
45
41
updatedSegment = f .chainUpdate
46
42
log .Trace ().Msg ("internal chain cache empty, setting updated chain segment" )
47
43
} else if err != nil {
48
- //TODO: what to do on db error?
49
44
success = false
50
45
return success , err
51
46
} else {
@@ -63,6 +58,11 @@ func (f *Fetcher) handlerSync(ctx context.Context) (success bool, err error) {
63
58
return success , fmt .Errorf ("chain-update difference too big: %w" , errUint64Overflow )
64
59
}
65
60
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
+ }
66
66
67
67
// we are not synced to the chain-update
68
68
// 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) {
86
86
}
87
87
removedSegment = nil
88
88
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
+ }
96
110
err = updateErr
97
111
// Now as long as the chain-cache eviction policy is not aggressive (easily doable)
98
112
removedSegment = result .RemovedSegment
99
113
updatedSegment = result .UpdatedSegment
100
114
// we will process the whole segment of the chain update
101
115
f .chainUpdate = nil
102
- success = true
103
116
}
104
117
}
105
118
0 commit comments