diff --git a/core/blockchain.go b/core/blockchain.go index a3998306b4..25b9cbd7cb 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -2387,26 +2387,11 @@ func (bc *BlockChain) updateHighestVerifiedHeader(header *types.Header) { if header == nil || header.Number == nil { return } - currentHeader := bc.highestVerifiedHeader.Load() - if currentHeader == nil { - bc.highestVerifiedHeader.Store(types.CopyHeader(header)) - return - } - - newParentTD := bc.GetTd(header.ParentHash, header.Number.Uint64()-1) - if newParentTD == nil { - newParentTD = big.NewInt(0) - } - oldParentTD := bc.GetTd(currentHeader.ParentHash, currentHeader.Number.Uint64()-1) - if oldParentTD == nil { - oldParentTD = big.NewInt(0) - } - newTD := big.NewInt(0).Add(newParentTD, header.Difficulty) - oldTD := big.NewInt(0).Add(oldParentTD, currentHeader.Difficulty) - - if newTD.Cmp(oldTD) > 0 { + currentBlock := bc.CurrentBlock() + reorg, err := bc.forker.ReorgNeededWithFastFinality(currentBlock, header) + if err == nil && reorg { bc.highestVerifiedHeader.Store(types.CopyHeader(header)) - return + log.Trace("updateHighestVerifiedHeader", "number", header.Number.Uint64(), "hash", header.Hash()) } } diff --git a/core/forkchoice.go b/core/forkchoice.go index 3be956068d..c70d5dd64d 100644 --- a/core/forkchoice.go +++ b/core/forkchoice.go @@ -86,9 +86,16 @@ func (f *ForkChoice) ReorgNeeded(current *types.Header, extern *types.Header) (b localTD = f.chain.GetTd(current.Hash(), current.Number.Uint64()) externTd = f.chain.GetTd(extern.Hash(), extern.Number.Uint64()) ) - if localTD == nil || externTd == nil { + if localTD == nil { return false, errors.New("missing td") } + if externTd == nil { + ptd := f.chain.GetTd(extern.ParentHash, extern.Number.Uint64()-1) + if ptd == nil { + return false, consensus.ErrUnknownAncestor + } + externTd = new(big.Int).Add(ptd, extern.Difficulty) + } // Accept the new header as the chain head if the transition // is already triggered. We assume all the headers after the // transition come from the trusted consensus layer.