From ba89df28da9daf02475eef4a537b9c05fd62e5f7 Mon Sep 17 00:00:00 2001 From: rjl493456442 Date: Thu, 19 Apr 2018 13:36:51 +0800 Subject: [PATCH] core: add additional judgement when repair the blockchain --- core/blockchain.go | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index b33eb85a44..9800f84ea5 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -48,7 +48,9 @@ import ( var ( blockInsertTimer = metrics.NewRegisteredTimer("chain/inserts", nil) - ErrNoGenesis = errors.New("Genesis not found in chain") + ErrNoGenesis = errors.New("Genesis not found in chain") + errGenesisStateIncomplete = errors.New("genesis state is incomplete") + errMissingBlock = errors.New("missing block") ) const ( @@ -432,8 +434,18 @@ func (bc *BlockChain) repair(head **types.Block) error { log.Info("Rewound blockchain to past state", "number", (*head).Number(), "hash", (*head).Hash()) return nil } - // Otherwise rewind one block and recheck state availability there - (*head) = bc.GetBlock((*head).ParentHash(), (*head).NumberU64()-1) + // Abort if the genesis block's state is still incomplete. + if (*head).NumberU64() == 0 { + log.Info("The state of genesis block is incomplete") + return errGenesisStateIncomplete + } + // Rewind one block and recheck state availability there + block := bc.GetBlock((*head).ParentHash(), (*head).NumberU64()-1) + if block == nil { + log.Info("Rewound to a missing block", "number", (*head).NumberU64()-1) + return errMissingBlock + } + *head = block } }