Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove broken blocks #1068

Merged
merged 1 commit into from
Jul 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions consensus/istanbul/backend/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,10 @@ func (sb *Backend) snapshot(chain consensus.ChainReader, number uint64, hash com
}

genesis := chain.GetHeaderByNumber(0)
if genesis == nil {
log.Error("Cannot load genesis")
return nil, errors.New("Cannot load genesis")
}

istanbulExtra, err := types.ExtractIstanbulExtra(genesis)
if err != nil {
Expand Down
48 changes: 37 additions & 11 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -569,17 +569,25 @@ func (bc *BlockChain) ResetWithGenesisBlock(genesis *types.Block) error {
// This method only rolls back the current block. The current header and current
// fast block are left intact.
func (bc *BlockChain) repair(head **types.Block) error {
batch := bc.db.NewBatch()

for {
// Abort if we've rewound to a head block that does have associated state
if _, err := state.New((*head).Root(), bc.stateCache); err == nil {
log.Info("Rewound blockchain to past state", "number", (*head).Number(), "hash", (*head).Hash())
if err := batch.Write(); err != nil {
log.Error("Error when removing sidechain", "err", err)
}
bc.purge()
return nil
}
// Otherwise rewind one block and recheck state availability there
block := bc.GetBlock((*head).ParentHash(), (*head).NumberU64()-1)
if block == nil {
return fmt.Errorf("missing block %d [%x]", (*head).NumberU64()-1, (*head).ParentHash())
}
// It's safest to remove these blocks
rawdb.DeleteBlock(batch, (*head).Hash(), (*head).NumberU64())
*head = block
}
}
Expand Down Expand Up @@ -922,17 +930,7 @@ func (bc *BlockChain) truncateAncient(head uint64) error {
if err := bc.db.TruncateAncients(head + 1); err != nil {
return err
}
// Clear out any stale content from the caches
bc.hc.headerCache.Purge()
bc.hc.tdCache.Purge()
bc.hc.numberCache.Purge()
// Clear out any stale content from the caches
bc.bodyCache.Purge()
bc.bodyRLPCache.Purge()
bc.receiptsCache.Purge()
bc.blockCache.Purge()
bc.txLookupCache.Purge()
bc.futureBlocks.Purge()
bc.purge()

log.Info("Rewind ancient data", "number", head)
return nil
Expand Down Expand Up @@ -1906,6 +1904,20 @@ func (bc *BlockChain) insertSideChain(block *types.Block, it *insertIterator) (i
parent = bc.GetHeader(parent.ParentHash, parent.Number.Uint64()-1)
}
if parent == nil {
log.Info("Found orphaned sidechain, removing", "length", len(hashes))
batch := bc.db.NewBatch()

for i := 0; i < len(hashes); i++ {
rawdb.DeleteBlock(batch, hashes[i], numbers[i])
}

if err := batch.Write(); err != nil {
log.Error("Error when removing sidechain", "err", err)
}

// Clear out any stale content from the caches
bc.purge()

return it.index, nil, nil, errors.New("missing parent")
}
// Import all the pruned blocks to make the state available
Expand Down Expand Up @@ -1944,6 +1956,20 @@ func (bc *BlockChain) insertSideChain(block *types.Block, it *insertIterator) (i
return 0, nil, nil, nil
}

func (bc *BlockChain) purge() {
// Clear out any stale content from the caches
bc.hc.headerCache.Purge()
bc.hc.tdCache.Purge()
bc.hc.numberCache.Purge()
// Clear out any stale content from the caches
bc.bodyCache.Purge()
bc.bodyRLPCache.Purge()
bc.receiptsCache.Purge()
bc.blockCache.Purge()
bc.txLookupCache.Purge()
bc.futureBlocks.Purge()
}

// reorg takes two blocks, an old chain and a new chain and will reconstruct the
// blocks and inserts them to be part of the new canonical chain and accumulates
// potential missing transactions and post an event about them.
Expand Down