Skip to content

Commit

Permalink
merge ethereum bug fix : core: fix chain indexer reorg bug #19748
Browse files Browse the repository at this point in the history
  • Loading branch information
rjgeek committed Jul 11, 2020
1 parent edc47b4 commit f3d261b
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions blockchain/chain_indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ func (c *ChainIndexer) updateLoop() {
updated = time.Now()
}
// Cache the current section count and head to allow unlocking the mutex
c.verifyLastHead()
section := c.storedSections
var oldHead common.Hash
if section > 0 {
Expand All @@ -270,7 +271,7 @@ func (c *ChainIndexer) updateLoop() {
c.lock.Lock()

// If processing succeeded and no reorgs occcurred, mark the section completed
if err == nil && oldHead == c.sectionHead(section-1) {
if err == nil && (section == 0||oldHead == c.sectionHead(section-1)) {
c.setSectionHead(section, newHead)
c.setValidSections(section + 1)
if c.storedSections == c.knownSections && updating {
Expand All @@ -286,6 +287,7 @@ func (c *ChainIndexer) updateLoop() {
} else {
// If processing failed, don't retry until further notification
c.log.Debug("Chain index processing failed", "section", section, "err", err)
c.verifyLastHead()
c.knownSections = c.storedSections
}
}
Expand Down Expand Up @@ -334,13 +336,27 @@ func (c *ChainIndexer) processSection(section uint64, lastHead common.Hash) (com
return lastHead, nil
}



// verifyLastHead compares last stored section head with the corresponding block hash in the
// actual canonical chain and rolls back reorged sections if necessary to ensure that stored
// sections are all valid
func (c *ChainIndexer) verifyLastHead() {
for c.storedSections > 0 {
if c.sectionHead(c.storedSections-1) == GetCanonicalHash(c.chainDb, c.storedSections*c.sectionSize-1) {
return
}
c.setValidSections(c.storedSections - 1)
}
}

// Sections returns the number of processed sections maintained by the indexer
// and also the information about the last header indexed for potential canonical
// verifications.
func (c *ChainIndexer) Sections() (uint64, uint64, common.Hash) {
c.lock.Lock()
defer c.lock.Unlock()

c.verifyLastHead()
return c.storedSections, c.storedSections*c.sectionSize - 1, c.sectionHead(c.storedSections - 1)
}

Expand Down

0 comments on commit f3d261b

Please sign in to comment.