Skip to content

Commit

Permalink
remove broken blocks (#1068)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrsmkl authored Jul 2, 2020
1 parent 93fa73a commit 81d41a4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 11 deletions.
4 changes: 4 additions & 0 deletions consensus/istanbul/backend/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,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

0 comments on commit 81d41a4

Please sign in to comment.