Skip to content

Commit 63352bf

Browse files
zsfelfoldikaralabe
authored andcommitted
core: safe indexer operation when syncing starts before the checkpoint (#17511)
1 parent b69476b commit 63352bf

2 files changed

Lines changed: 48 additions & 14 deletions

File tree

‎core/chain_indexer.go‎

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ type ChainIndexer struct {
8585
knownSections uint64 // Number of sections known to be complete (block wise)
8686
cascadedHead uint64 // Block number of the last completed section cascaded to subindexers
8787

88+
checkpointSections uint64 // Number of sections covered by the checkpoint
89+
checkpointHead common.Hash // Section head belonging to the checkpoint
90+
8891
throttling time.Duration // Disk throttling to prevent a heavy upgrade from hogging resources
8992

9093
log log.Logger
@@ -115,12 +118,19 @@ func NewChainIndexer(chainDb, indexDb ethdb.Database, backend ChainIndexerBacken
115118
return c
116119
}
117120

118-
// AddKnownSectionHead marks a new section head as known/processed if it is newer
119-
// than the already known best section head
120-
func (c *ChainIndexer) AddKnownSectionHead(section uint64, shead common.Hash) {
121+
// AddCheckpoint adds a checkpoint. Sections are never processed and the chain
122+
// is not expected to be available before this point. The indexer assumes that
123+
// the backend has sufficient information available to process subsequent sections.
124+
//
125+
// Note: knownSections == 0 and storedSections == checkpointSections until
126+
// syncing reaches the checkpoint
127+
func (c *ChainIndexer) AddCheckpoint(section uint64, shead common.Hash) {
121128
c.lock.Lock()
122129
defer c.lock.Unlock()
123130

131+
c.checkpointSections = section + 1
132+
c.checkpointHead = shead
133+
124134
if section < c.storedSections {
125135
return
126136
}
@@ -233,16 +243,23 @@ func (c *ChainIndexer) newHead(head uint64, reorg bool) {
233243
// If a reorg happened, invalidate all sections until that point
234244
if reorg {
235245
// Revert the known section number to the reorg point
236-
changed := head / c.sectionSize
237-
if changed < c.knownSections {
238-
c.knownSections = changed
246+
known := head / c.sectionSize
247+
stored := known
248+
if known < c.checkpointSections {
249+
known = 0
250+
}
251+
if stored < c.checkpointSections {
252+
stored = c.checkpointSections
253+
}
254+
if known < c.knownSections {
255+
c.knownSections = known
239256
}
240257
// Revert the stored sections from the database to the reorg point
241-
if changed < c.storedSections {
242-
c.setValidSections(changed)
258+
if stored < c.storedSections {
259+
c.setValidSections(stored)
243260
}
244261
// Update the new head number to the finalized section end and notify children
245-
head = changed * c.sectionSize
262+
head = known * c.sectionSize
246263

247264
if head < c.cascadedHead {
248265
c.cascadedHead = head
@@ -256,7 +273,18 @@ func (c *ChainIndexer) newHead(head uint64, reorg bool) {
256273
var sections uint64
257274
if head >= c.confirmsReq {
258275
sections = (head + 1 - c.confirmsReq) / c.sectionSize
276+
if sections < c.checkpointSections {
277+
sections = 0
278+
}
259279
if sections > c.knownSections {
280+
if c.knownSections < c.checkpointSections {
281+
// syncing reached the checkpoint, verify section head
282+
syncedHead := rawdb.ReadCanonicalHash(c.chainDb, c.checkpointSections*c.sectionSize-1)
283+
if syncedHead != c.checkpointHead {
284+
c.log.Error("Synced chain does not match checkpoint", "number", c.checkpointSections*c.sectionSize-1, "expected", c.checkpointHead, "synced", syncedHead)
285+
return
286+
}
287+
}
260288
c.knownSections = sections
261289

262290
select {
@@ -401,8 +429,14 @@ func (c *ChainIndexer) AddChildIndexer(indexer *ChainIndexer) {
401429
c.children = append(c.children, indexer)
402430

403431
// Cascade any pending updates to new children too
404-
if c.storedSections > 0 {
405-
indexer.newHead(c.storedSections*c.sectionSize-1, false)
432+
sections := c.storedSections
433+
if c.knownSections < sections {
434+
// if a section is "stored" but not "known" then it is a checkpoint without
435+
// available chain data so we should not cascade it yet
436+
sections = c.knownSections
437+
}
438+
if sections > 0 {
439+
indexer.newHead(sections*c.sectionSize-1, false)
406440
}
407441
}
408442

‎light/lightchain.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,14 @@ func NewLightChain(odr OdrBackend, config *params.ChainConfig, engine consensus.
121121
func (self *LightChain) addTrustedCheckpoint(cp TrustedCheckpoint) {
122122
if self.odr.ChtIndexer() != nil {
123123
StoreChtRoot(self.chainDb, cp.SectionIdx, cp.SectionHead, cp.CHTRoot)
124-
self.odr.ChtIndexer().AddKnownSectionHead(cp.SectionIdx, cp.SectionHead)
124+
self.odr.ChtIndexer().AddCheckpoint(cp.SectionIdx, cp.SectionHead)
125125
}
126126
if self.odr.BloomTrieIndexer() != nil {
127127
StoreBloomTrieRoot(self.chainDb, cp.SectionIdx, cp.SectionHead, cp.BloomRoot)
128-
self.odr.BloomTrieIndexer().AddKnownSectionHead(cp.SectionIdx, cp.SectionHead)
128+
self.odr.BloomTrieIndexer().AddCheckpoint(cp.SectionIdx, cp.SectionHead)
129129
}
130130
if self.odr.BloomIndexer() != nil {
131-
self.odr.BloomIndexer().AddKnownSectionHead(cp.SectionIdx, cp.SectionHead)
131+
self.odr.BloomIndexer().AddCheckpoint(cp.SectionIdx, cp.SectionHead)
132132
}
133133
log.Info("Added trusted checkpoint", "chain", cp.name, "block", (cp.SectionIdx+1)*self.indexerConfig.ChtSize-1, "hash", cp.SectionHead)
134134
}

0 commit comments

Comments
 (0)