Do not remove headers when unwinding (#2122)

Co-authored-by: Alex Sharp <alexsharp@Alexs-MacBook-Pro.local>
This commit is contained in:
ledgerwatch 2021-06-08 16:16:27 +01:00 committed by GitHub
parent 338fc0df26
commit 657a8b13df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 9 deletions

View File

@ -53,6 +53,7 @@ type ChainReader interface {
// GetBlock retrieves a block from the database by hash and number.
GetBlock(hash common.Hash, number uint64) *types.Block
GetHeader(hash common.Hash, number uint64) *types.Header
HasBlock(hash common.Hash, number uint64) bool
}

View File

@ -211,15 +211,23 @@ func getUncles(chain consensus.ChainReader, header *types.Header) (mapset.Set, m
number, parent := header.Number.Uint64()-1, header.ParentHash
for i := 0; i < 7; i++ {
ancestor := chain.GetBlock(parent, number)
if ancestor == nil {
ancestorHeader := chain.GetHeader(parent, number)
if ancestorHeader == nil {
break
}
ancestors[ancestor.Hash()] = ancestor.Header()
for _, uncle := range ancestor.Uncles() {
uncles.Add(uncle.Hash())
ancestors[parent] = ancestorHeader
// If the ancestor doesn't have any uncles, we don't have to iterate them
if ancestorHeader.UncleHash != types.EmptyUncleHash {
// Need to add those uncles to the blacklist too
ancestor := chain.GetBlock(parent, number)
if ancestor == nil {
break
}
for _, uncle := range ancestor.Uncles() {
uncles.Add(uncle.Hash())
}
}
parent, number = ancestor.ParentHash(), number-1
parent, number = ancestorHeader.ParentHash, number-1
}
ancestors[header.Hash()] = header
uncles.Add(header.Hash())

View File

@ -260,9 +260,7 @@ func HeadersUnwind(u *UnwindState, s *StageState, tx ethdb.RwTx, cfg HeadersCfg)
if hash, err = rawdb.ReadCanonicalHash(tx, blockHeight); err != nil {
return err
}
rawdb.DeleteHeader(tx, hash, blockHeight)
rawdb.DeleteTd(tx, hash, blockHeight)
rawdb.DeleteHeaderNumber(tx, hash)
}
if err = rawdb.DeleteCanonicalHash(tx, blockHeight); err != nil {
return err

View File

@ -381,7 +381,9 @@ func (ms *MockSentry) InsertChain(chain *core.ChainPack) error {
}
}
// Check if the latest header was imported or rolled back
if rawdb.ReadHeader(ethdb.NewObjectDatabase(ms.DB), chain.TopBlock.Hash(), chain.TopBlock.NumberU64()) == nil {
if td, tdErr := rawdb.ReadTd(ethdb.NewObjectDatabase(ms.DB), chain.TopBlock.Hash(), chain.TopBlock.NumberU64()); tdErr != nil {
return tdErr
} else if td == nil {
return fmt.Errorf("did not import block %d %x", chain.TopBlock.NumberU64(), chain.TopBlock.Hash())
}
return nil