diff --git a/blockdag/blocknode.go b/blockdag/blocknode.go index a3a031585..a20ced3c3 100644 --- a/blockdag/blocknode.go +++ b/blockdag/blocknode.go @@ -78,9 +78,6 @@ type blockNode struct { // hash is the double sha 256 of the block. hash *daghash.Hash - // height is the position in the block DAG. - height uint64 - // chainHeight is the number of hops you need to go down the selected parent chain in order to get to the genesis block. chainHeight uint64 @@ -132,18 +129,10 @@ func initBlockNode(node *blockNode, blockHeader *wire.BlockHeader, parents block if len(parents) > 0 { node.blues, node.selectedParent, node.blueScore = phantom(node, phantomK) - node.height = calculateNodeHeight(node) node.chainHeight = calculateChainHeight(node) } } -func calculateNodeHeight(node *blockNode) uint64 { - if node.isGenesis() { - return 0 - } - return node.parents.maxHeight() + 1 -} - func calculateChainHeight(node *blockNode) uint64 { if node.isGenesis() { return 0 diff --git a/blockdag/blockset.go b/blockdag/blockset.go index 5fd6a739a..b6d7342c3 100644 --- a/blockdag/blockset.go +++ b/blockdag/blockset.go @@ -23,17 +23,6 @@ func setFromSlice(blocks ...*blockNode) blockSet { return set } -// maxHeight returns the height of the highest block in the block set -func (bs blockSet) maxHeight() uint64 { - var maxHeight uint64 - for _, node := range bs { - if maxHeight < node.height { - maxHeight = node.height - } - } - return maxHeight -} - // add adds a block to this BlockSet func (bs blockSet) add(block *blockNode) { bs[*block.hash] = block diff --git a/blockdag/coinbase.go b/blockdag/coinbase.go index 81421b6be..390865f96 100644 --- a/blockdag/coinbase.go +++ b/blockdag/coinbase.go @@ -257,7 +257,7 @@ func coinbaseInputAndOutputForBlueBlock(dag *BlockDAG, blueBlock *blockNode, } } - totalReward := CalcBlockSubsidy(blueBlock.height, dag.dagParams) + totalFees + totalReward := CalcBlockSubsidy(blueBlock.blueScore, dag.dagParams) + totalFees if totalReward == 0 { return txIn, nil, nil diff --git a/blockdag/dag.go b/blockdag/dag.go index f9c865dac..689f74e1e 100644 --- a/blockdag/dag.go +++ b/blockdag/dag.go @@ -1598,13 +1598,13 @@ func (dag *BlockDAG) ChildHashesByHash(hash *daghash.Hash) ([]*daghash.Hash, err return node.children.hashes(), nil } -// HeightToHashRange returns a range of block hashes for the given start height -// and end hash, inclusive on both ends. The hashes are for all blocks that are -// ancestors of endHash with height greater than or equal to startHeight. The -// end hash must belong to a block that is known to be valid. +// ChainHeightToHashRange returns a range of block hashes for the given start chain +// height and end hash, inclusive on both ends. The hashes are for all blocks that +// are ancestors of endHash with height greater than or equal to startChainHeight. +// The end hash must belong to a block that is known to be valid. // // This function is safe for concurrent access. -func (dag *BlockDAG) HeightToHashRange(startHeight uint64, +func (dag *BlockDAG) ChainHeightToHashRange(startChainHeight uint64, endHash *daghash.Hash, maxResults int) ([]*daghash.Hash, error) { endNode := dag.index.LookupNode(endHash) @@ -1614,23 +1614,23 @@ func (dag *BlockDAG) HeightToHashRange(startHeight uint64, if !dag.index.NodeStatus(endNode).KnownValid() { return nil, errors.Errorf("block %s is not yet validated", endHash) } - endHeight := endNode.height + endChainHeight := endNode.chainHeight - if startHeight < 0 { - return nil, errors.Errorf("start height (%d) is below 0", startHeight) + if startChainHeight < 0 { + return nil, errors.Errorf("start chain height (%d) is below 0", startChainHeight) } - if startHeight > endHeight { - return nil, errors.Errorf("start height (%d) is past end height (%d)", - startHeight, endHeight) + if startChainHeight > endChainHeight { + return nil, errors.Errorf("start chain height (%d) is past end chain height (%d)", + startChainHeight, endChainHeight) } - resultsLength := int(endHeight - startHeight + 1) + resultsLength := int(endChainHeight - startChainHeight + 1) if resultsLength > maxResults { return nil, errors.Errorf("number of results (%d) would exceed max (%d)", resultsLength, maxResults) } - // Walk backwards from endHeight to startHeight, collecting block hashes. + // Walk backwards from endChainHeight to startChainHeight, collecting block hashes. node := endNode hashes := make([]*daghash.Hash, resultsLength) for i := resultsLength - 1; i >= 0; i-- { @@ -1654,16 +1654,16 @@ func (dag *BlockDAG) IntervalBlockHashes(endHash *daghash.Hash, interval uint64, if !dag.index.NodeStatus(endNode).KnownValid() { return nil, errors.Errorf("block %s is not yet validated", endHash) } - endHeight := endNode.height + endChainHeight := endNode.chainHeight - resultsLength := endHeight / interval + resultsLength := endChainHeight / interval hashes := make([]*daghash.Hash, resultsLength) dag.virtual.mtx.Lock() defer dag.virtual.mtx.Unlock() blockNode := endNode - for index := endHeight / interval; index > 0; index-- { + for index := endChainHeight / interval; index > 0; index-- { blockHeight := index * interval blockNode = blockNode.SelectedAncestor(blockHeight) diff --git a/blockdag/dag_test.go b/blockdag/dag_test.go index 3d497dd58..acf3f5830 100644 --- a/blockdag/dag_test.go +++ b/blockdag/dag_test.go @@ -594,10 +594,10 @@ func testTip(nodes []*blockNode) *blockNode { return nodes[len(nodes)-1] } -// TestHeightToHashRange ensures that fetching a range of block hashes by start -// height and end hash works as expected. -func TestHeightToHashRange(t *testing.T) { - // Construct a synthetic block chain with a block index consisting of +// TestChainHeightToHashRange ensures that fetching a range of block hashes by start +// chain height and end hash works as expected. +func TestChainHeightToHashRange(t *testing.T) { + // Construct a synthetic block DAG with a block index consisting of // the following structure. // genesis -> 1 -> 2 -> ... -> 15 -> 16 -> 17 -> 18 // \-> 16a -> 17a -> 18a (unvalidated) @@ -610,7 +610,7 @@ func TestHeightToHashRange(t *testing.T) { blockDAG.index.AddNode(node) } for _, node := range branch1Nodes { - if node.height < 18 { + if node.chainHeight < 18 { blockDAG.index.SetStatusFlags(node, statusValid) } blockDAG.index.AddNode(node) @@ -618,59 +618,59 @@ func TestHeightToHashRange(t *testing.T) { blockDAG.virtual.SetTips(setFromSlice(tip(branch0Nodes))) tests := []struct { - name string - startHeight uint64 // locator for requested inventory - endHash *daghash.Hash // stop hash for locator - maxResults int // max to locate, 0 = wire const - hashes []*daghash.Hash // expected located hashes - expectError bool + name string + startChainHeight uint64 // locator for requested inventory + endHash *daghash.Hash // stop hash for locator + maxResults int // max to locate, 0 = wire const + hashes []*daghash.Hash // expected located hashes + expectError bool }{ { - name: "blocks below tip", - startHeight: 11, - endHash: branch0Nodes[14].hash, - maxResults: 10, - hashes: nodeHashes(branch0Nodes, 10, 11, 12, 13, 14), + name: "blocks below tip", + startChainHeight: 11, + endHash: branch0Nodes[14].hash, + maxResults: 10, + hashes: nodeHashes(branch0Nodes, 10, 11, 12, 13, 14), }, { - name: "blocks on main chain", - startHeight: 15, - endHash: branch0Nodes[17].hash, - maxResults: 10, - hashes: nodeHashes(branch0Nodes, 14, 15, 16, 17), + name: "blocks on main chain", + startChainHeight: 15, + endHash: branch0Nodes[17].hash, + maxResults: 10, + hashes: nodeHashes(branch0Nodes, 14, 15, 16, 17), }, { - name: "blocks on stale chain", - startHeight: 15, - endHash: branch1Nodes[1].hash, - maxResults: 10, + name: "blocks on stale chain", + startChainHeight: 15, + endHash: branch1Nodes[1].hash, + maxResults: 10, hashes: append(nodeHashes(branch0Nodes, 14), nodeHashes(branch1Nodes, 0, 1)...), }, { - name: "invalid start height", - startHeight: 19, - endHash: branch0Nodes[17].hash, - maxResults: 10, - expectError: true, + name: "invalid start chain height", + startChainHeight: 19, + endHash: branch0Nodes[17].hash, + maxResults: 10, + expectError: true, }, { - name: "too many results", - startHeight: 1, - endHash: branch0Nodes[17].hash, - maxResults: 10, - expectError: true, + name: "too many results", + startChainHeight: 1, + endHash: branch0Nodes[17].hash, + maxResults: 10, + expectError: true, }, { - name: "unvalidated block", - startHeight: 15, - endHash: branch1Nodes[2].hash, - maxResults: 10, - expectError: true, + name: "unvalidated block", + startChainHeight: 15, + endHash: branch1Nodes[2].hash, + maxResults: 10, + expectError: true, }, } for _, test := range tests { - hashes, err := blockDAG.HeightToHashRange(test.startHeight, test.endHash, + hashes, err := blockDAG.ChainHeightToHashRange(test.startChainHeight, test.endHash, test.maxResults) if err != nil { if !test.expectError { @@ -702,7 +702,7 @@ func TestIntervalBlockHashes(t *testing.T) { dag.index.AddNode(node) } for _, node := range branch1Nodes { - if node.height < 18 { + if node.chainHeight < 18 { dag.index.SetStatusFlags(node, statusValid) } dag.index.AddNode(node) diff --git a/blockdag/dagio.go b/blockdag/dagio.go index 20f79e093..0a0d3a1b9 100644 --- a/blockdag/dagio.go +++ b/blockdag/dagio.go @@ -684,7 +684,6 @@ func (dag *BlockDAG) deserializeBlockNode(blockRow []byte) (*blockNode, error) { node.blues[i] = dag.index.LookupNode(hash) } - node.height = calculateNodeHeight(node) node.chainHeight = calculateChainHeight(node) return node, nil diff --git a/blockdag/validate.go b/blockdag/validate.go index cc792e172..c100516e3 100644 --- a/blockdag/validate.go +++ b/blockdag/validate.go @@ -612,7 +612,7 @@ func (dag *BlockDAG) validateDifficulty(header *wire.BlockHeader, bluestParent * // validateParents validates that no parent is an ancestor of another parent, and no parent is finalized func validateParents(blockHeader *wire.BlockHeader, parents blockSet) error { - minHeight := uint64(math.MaxUint64) + minBlueScore := uint64(math.MaxUint64) queue := newDownHeap() visited := newSet() for _, parent := range parents { @@ -622,8 +622,8 @@ func validateParents(blockHeader *wire.BlockHeader, parents blockSet) error { if parent.isFinalized { return ruleError(ErrFinality, fmt.Sprintf("block %s is a finalized parent of block %s", parent.hash, blockHeader.BlockHash())) } - if parent.height < minHeight { - minHeight = parent.height + if parent.blueScore < minBlueScore { + minBlueScore = parent.blueScore } for _, grandParent := range parent.parents { if !visited.contains(grandParent) { @@ -640,7 +640,7 @@ func validateParents(blockHeader *wire.BlockHeader, parents blockSet) error { current.hash, blockHeader.BlockHash())) } - if current.height > minHeight { + if current.blueScore > minBlueScore { for _, parent := range current.parents { if !visited.contains(parent) { queue.Push(parent) diff --git a/server/p2p/on_get_c_filters.go b/server/p2p/on_get_c_filters.go index 1c9c00bf2..4eeb6cf98 100644 --- a/server/p2p/on_get_c_filters.go +++ b/server/p2p/on_get_c_filters.go @@ -12,7 +12,7 @@ func (sp *Peer) OnGetCFilters(_ *peer.Peer, msg *wire.MsgGetCFilters) { return } - hashes, err := sp.server.DAG.HeightToHashRange(msg.StartHeight, + hashes, err := sp.server.DAG.ChainHeightToHashRange(msg.StartHeight, msg.StopHash, wire.MaxGetCFiltersReqRange) if err != nil { peerLog.Debugf("Invalid getcfilters request: %s", err) diff --git a/server/p2p/on_get_cf_headers.go b/server/p2p/on_get_cf_headers.go index e3f8dc8fd..104b3b12d 100644 --- a/server/p2p/on_get_cf_headers.go +++ b/server/p2p/on_get_cf_headers.go @@ -24,7 +24,7 @@ func (sp *Peer) OnGetCFHeaders(_ *peer.Peer, msg *wire.MsgGetCFHeaders) { } // Fetch the hashes from the block index. - hashList, err := sp.server.DAG.HeightToHashRange(startHeight, + hashList, err := sp.server.DAG.ChainHeightToHashRange(startHeight, msg.StopHash, maxResults) if err != nil { peerLog.Debugf("Invalid getcfheaders request: %s", err)