diff --git a/blockdag/validate.go b/blockdag/validate.go index 66bb4c83b..c6a1bf4b8 100644 --- a/blockdag/validate.go +++ b/blockdag/validate.go @@ -624,7 +624,7 @@ func checkSerializedHeight(coinbaseTx *util.Tx, wantHeight int32) error { // the checkpoints are not performed. // // This function MUST be called with the chain state lock held (for writes). -func (dag *BlockDAG) checkBlockHeaderContext(header *wire.BlockHeader, selectedParent *blockNode, flags BehaviorFlags) error { +func (dag *BlockDAG) checkBlockHeaderContext(header *wire.BlockHeader, selectedParent *blockNode, blockHeight int32, flags BehaviorFlags) error { fastAdd := flags&BFFastAdd == BFFastAdd if !fastAdd { // Ensure the difficulty specified in the block header matches @@ -652,10 +652,6 @@ func (dag *BlockDAG) checkBlockHeaderContext(header *wire.BlockHeader, selectedP } } - // The height of this block is one more than the referenced previous - // block. - blockHeight := selectedParent.height + 1 - // Ensure chain matches up to predetermined checkpoints. blockHash := header.BlockHash() if !dag.verifyCheckpoint(blockHeight, &blockHash) { @@ -736,7 +732,7 @@ func (dag *BlockDAG) checkBlockContext(block *util.Block, parents blockSet, sele } // Perform all block header related validation checks. header := &block.MsgBlock().Header - err = dag.checkBlockHeaderContext(header, selectedParent, flags) + err = dag.checkBlockHeaderContext(header, selectedParent, block.Height(), flags) if err != nil { return err } @@ -745,13 +741,9 @@ func (dag *BlockDAG) checkBlockContext(block *util.Block, parents blockSet, sele if !fastAdd { blockTime := selectedParent.CalcPastMedianTime() - // The height of this block is one more than the referenced - // previous block. - blockHeight := selectedParent.height + 1 - // Ensure all transactions in the block are finalized. for _, tx := range block.Transactions() { - if !IsFinalizedTransaction(tx, blockHeight, + if !IsFinalizedTransaction(tx, block.Height(), blockTime) { str := fmt.Sprintf("block contains unfinalized "+ @@ -763,7 +755,7 @@ func (dag *BlockDAG) checkBlockContext(block *util.Block, parents blockSet, sele // Ensure coinbase starts with serialized block heights coinbaseTx := block.Transactions()[0] - err := checkSerializedHeight(coinbaseTx, blockHeight) + err := checkSerializedHeight(coinbaseTx, block.Height()) if err != nil { return err } diff --git a/blockdag/virtualblock.go b/blockdag/virtualblock.go index 04bb36f95..ae98400af 100644 --- a/blockdag/virtualblock.go +++ b/blockdag/virtualblock.go @@ -74,7 +74,7 @@ func (v *VirtualBlock) addTip(newTip *blockNode) { v.setTips(updatedTips) } -// addTip adds the given tip to the set of tips in the virtual block. +// AddTip adds the given tip to the set of tips in the virtual block. // All former tips that happen to be the given tip's parents are removed // from the set. // @@ -106,6 +106,12 @@ func (v *VirtualBlock) SelectedTipHeight() int32 { return v.SelectedTip().height } +// Height returns the height of the virtual block. +// In other words: height of highest block + 1 +func (v *VirtualBlock) Height() int32 { + return v.tips().maxHeight() + 1 +} + // TipHashes returns the hashes of the tips of the virtual block. func (v *VirtualBlock) TipHashes() []daghash.Hash { return v.tips().hashes() diff --git a/mining/mining.go b/mining/mining.go index f196b958d..647a9dc87 100644 --- a/mining/mining.go +++ b/mining/mining.go @@ -403,7 +403,7 @@ func NewBlkTmplGenerator(policy *Policy, params *dagconfig.Params, func (g *BlkTmplGenerator) NewBlockTemplate(payToAddress util.Address) (*BlockTemplate, error) { // Extend the most recently known best block. virtualBlock := g.dag.VirtualBlock() - nextBlockHeight := virtualBlock.SelectedTipHeight() + 1 + nextBlockHeight := virtualBlock.Height() // Create a standard coinbase transaction paying to the provided // address. NOTE: The coinbase value will be updated to include the