core: add additional judgement when repair the blockchain

This commit is contained in:
rjl493456442 2018-04-19 13:36:51 +08:00
parent 92c6d13083
commit ba89df28da

View file

@ -49,6 +49,8 @@ var (
blockInsertTimer = metrics.NewRegisteredTimer("chain/inserts", nil)
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
}
}