Skip to content

Commit

Permalink
blockchain: Do not accept orphans/genesis block.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
davecgh committed Feb 19, 2018
1 parent 39e5af8 commit cc6f0cb
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions blockchain/accept.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit cc6f0cb

Please sign in to comment.