From cc6f0cbff48a2cfb222d78c534e5c4df9a0926e1 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Sun, 18 Feb 2018 19:47:21 -0600 Subject: [PATCH] blockchain: Do not accept orphans/genesis block. This modifies the code in maybeAcceptBlock to return an error if there is no previous entry in the block index for a given block indicating it is either an orphan or the genesis block. The function should never be called in either circumstance since the genesis block is valid by definition and orphans are handled prior to calling the function. --- blockchain/accept.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/blockchain/accept.go b/blockchain/accept.go index 9949aa2f3b..c15bc4c755 100644 --- a/blockchain/accept.go +++ b/blockchain/accept.go @@ -118,6 +118,14 @@ func (b *BlockChain) maybeAcceptBlock(block *dcrutil.Block, flags BehaviorFlags) return false, err } + // This function should never be called with orphan blocks or the + // genesis block. + if prevNode == nil { + prevHash := &block.MsgBlock().Header.PrevBlock + str := fmt.Sprintf("previous block %s is not known", prevHash) + return false, ruleError(ErrMissingParent, str) + } + blockHeight := block.Height() // The block must pass all of the validation rules which depend on the @@ -154,11 +162,9 @@ func (b *BlockChain) maybeAcceptBlock(block *dcrutil.Block, flags BehaviorFlags) blockHeader := &block.MsgBlock().Header newNode := newBlockNode(blockHeader, stake.FindSpentTicketsInBlock(block.MsgBlock())) - if prevNode != nil { - newNode.parent = prevNode - newNode.height = blockHeight - newNode.workSum.Add(prevNode.workSum, newNode.workSum) - } + newNode.parent = prevNode + newNode.height = blockHeight + newNode.workSum.Add(prevNode.workSum, newNode.workSum) // Fetching a stake node could enable a new DoS vector, so restrict // this only to blocks that are recent in history.