diff --git a/blockdag/blockindex.go b/blockdag/blockindex.go index 1b6c07c1c..fd35c54fe 100644 --- a/blockdag/blockindex.go +++ b/blockdag/blockindex.go @@ -74,6 +74,9 @@ type blockNode struct { // parents is the parent blocks for this node. parents []*blockNode + // selectedParent is the selected parent for this node. + selectedParent *blockNode + // hash is the double sha 256 of the block. hash daghash.Hash @@ -117,9 +120,9 @@ func initBlockNode(node *blockNode, blockHeader *wire.BlockHeader, parents []*bl merkleRoot: blockHeader.MerkleRoot, } if len(parents) > 0 { - parent := parents[0] // TODO: (Stas) This is wrong. Modified only to satisfy compilation. - node.height = parent.height + 1 - node.workSum = node.workSum.Add(parent.workSum, node.workSum) + node.selectedParent = parents[0] + node.height = node.selectedParent.height + 1 + node.workSum = node.workSum.Add(node.selectedParent.workSum, node.workSum) } } @@ -160,7 +163,7 @@ func (node *blockNode) Ancestor(height int32) *blockNode { } n := node - for ; n != nil && n.height != height; n = n.parents[0] { + for ; n != nil && n.height != height; n = n.selectedParent { // Intentionally left blank } @@ -190,7 +193,7 @@ func (node *blockNode) CalcPastMedianTime() time.Time { timestamps[i] = iterNode.timestamp numNodes++ - iterNode = iterNode.parents[0] + iterNode = iterNode.selectedParent } // Prune the slice to the actual number of available timestamps which diff --git a/blockdag/chain.go b/blockdag/chain.go index 545776a10..1b6cc8950 100644 --- a/blockdag/chain.go +++ b/blockdag/chain.go @@ -373,7 +373,7 @@ func (b *BlockChain) calcSequenceLock(node *blockNode, tx *btcutil.Tx, utxoView // Obtain the latest BIP9 version bits state for the // CSV-package soft-fork deployment. The adherence of sequence // locks depends on the current soft-fork state. - csvState, err := b.deploymentState(node.parents[0], dagconfig.DeploymentCSV) // TODO: (Stas) This is wrong. Modified only to satisfy compilation. + csvState, err := b.deploymentState(node.selectedParent, dagconfig.DeploymentCSV) if err != nil { return nil, err } @@ -502,7 +502,7 @@ func (b *BlockChain) getReorganizeNodes(node *blockNode) (*list.List, *list.List // Do not reorganize to a known invalid chain. Ancestors deeper than the // direct parent are checked below but this is a quick check before doing // more unnecessary work. - if b.index.NodeStatus(node.parents[0]).KnownInvalid() { // TODO: (Stas) This is wrong. Modified only to satisfy compilation. + if b.index.NodeStatus(node.selectedParent).KnownInvalid() { b.index.SetStatusFlags(node, statusInvalidAncestor) return detachNodes, attachNodes } @@ -513,7 +513,7 @@ func (b *BlockChain) getReorganizeNodes(node *blockNode) (*list.List, *list.List // later. forkNode := b.bestChain.FindFork(node) invalidChain := false - for n := node; n != nil && n != forkNode; n = n.parents[0] { // TODO: (Stas) This is wrong. Modified only to satisfy compilation. + for n := node; n != nil && n != forkNode; n = n.selectedParent { if b.index.NodeStatus(n).KnownInvalid() { invalidChain = true break @@ -536,7 +536,7 @@ func (b *BlockChain) getReorganizeNodes(node *blockNode) (*list.List, *list.List // Start from the end of the main chain and work backwards until the // common ancestor adding each block to the list of nodes to detach from // the main chain. - for n := b.bestChain.Tips()[0]; n != nil && n != forkNode; n = n.parents[0] { // TODO: (Stas) This is wrong. Modified only to satisfy compilation. + for n := b.bestChain.Tips()[0]; n != nil && n != forkNode; n = n.selectedParent { detachNodes.PushBack(n) } @@ -684,7 +684,7 @@ func (b *BlockChain) disconnectBlock(node *blockNode, block *btcutil.Block, view } // Load the previous block since some details for it are needed below. - prevNode := node.parents[0] // TODO: (Stas) This is wrong. Modified only to satisfy compilation. + prevNode := node.selectedParent var prevBlock *btcutil.Block err := b.db.View(func(dbTx database.Tx) error { var err error @@ -762,7 +762,7 @@ func (b *BlockChain) disconnectBlock(node *blockNode, block *btcutil.Block, view view.commit() // This node's parent is now the end of the best chain. - b.bestChain.SetTip(node.parents[0]) // TODO: (Stas) This is wrong. Modified only to satisfy compilation. + b.bestChain.SetTip(node.selectedParent) // Update the state for the best block. Notice how this replaces the // entire struct instead of updating the existing one. This effectively @@ -1003,7 +1003,7 @@ func (b *BlockChain) reorganizeChain(detachNodes, attachNodes *list.List) error firstAttachNode := attachNodes.Front().Value.(*blockNode) firstDetachNode := detachNodes.Front().Value.(*blockNode) lastAttachNode := attachNodes.Back().Value.(*blockNode) - log.Infof("REORGANIZE: Chain forks at %v", firstAttachNode.parents[0].hash) // TODO: (Stas) This is wrong. Modified only to satisfy compilation. + log.Infof("REORGANIZE: Chain forks at %v", firstAttachNode.selectedParent.hash) log.Infof("REORGANIZE: Old best chain head was %v", firstDetachNode.hash) log.Infof("REORGANIZE: New best chain head is %v", lastAttachNode.hash) @@ -1355,7 +1355,7 @@ func (b *BlockChain) HeightToHashRange(startHeight int32, hashes := make([]daghash.Hash, resultsLength) for i := resultsLength - 1; i >= 0; i-- { hashes[i] = node.hash - node = node.parents[0] // TODO: (Stas) This is wrong. Modified only to satisfy compilation. + node = node.selectedParent } return hashes, nil } diff --git a/blockdag/chainview.go b/blockdag/chainview.go index 4e97be2d3..855f4d9f5 100644 --- a/blockdag/chainview.go +++ b/blockdag/chainview.go @@ -141,7 +141,7 @@ func (c *chainView) setTip(node *blockNode) { for node != nil && c.nodes[node.height] != node { c.nodes[node.height] = node - node = node.parents[0] // TODO: (Stas) This is wrong. Modified only to satisfy compilation. + node = node.selectedParent } } @@ -311,7 +311,7 @@ func (c *chainView) findFork(node *blockNode) *blockNode { // contain the node or there are no more nodes in which case there is no // common node between the two. for node != nil && !c.contains(node) { - node = node.parents[0] // TODO: (Stas) This is wrong. Modified only to satisfy compilation. + node = node.selectedParent } return node diff --git a/blockdag/checkpoints.go b/blockdag/checkpoints.go index 3f90bdfd4..a478c443f 100644 --- a/blockdag/checkpoints.go +++ b/blockdag/checkpoints.go @@ -234,14 +234,14 @@ func (b *BlockChain) IsCheckpointCandidate(block *btcutil.Block) (bool, error) { } // A checkpoint must be have at least one block before it. - if &node.parents[0] == nil { // TODO: (Stas) This is wrong. Modified only to satisfy compilation. + if &node.selectedParent == nil { return false, nil } // A checkpoint must have timestamps for the block and the blocks on // either side of it in order (due to the median time allowance this is // not always the case). - prevTime := time.Unix(node.parents[0].timestamp, 0) // TODO: (Stas) This is wrong. Modified only to satisfy compilation. + prevTime := time.Unix(node.selectedParent.timestamp, 0) curTime := block.MsgBlock().Header.Timestamp nextTime := time.Unix(nextNode.timestamp, 0) if prevTime.After(curTime) || nextTime.Before(curTime) { diff --git a/blockdag/difficulty.go b/blockdag/difficulty.go index e063ee463..dbb483d22 100644 --- a/blockdag/difficulty.go +++ b/blockdag/difficulty.go @@ -201,7 +201,7 @@ func (b *BlockChain) findPrevTestNetDifficulty(startNode *blockNode) uint32 { for iterNode != nil && iterNode.height%b.blocksPerRetarget != 0 && iterNode.bits == b.chainParams.PowLimitBits { - iterNode = iterNode.parents[0] // TODO: (Stas) This is wrong. Modified only to satisfy compilation. + iterNode = iterNode.selectedParent } // Return the found difficulty or the minimum difficulty if no diff --git a/blockdag/thresholdstate.go b/blockdag/thresholdstate.go index b8d9cace6..605b5adb3 100644 --- a/blockdag/thresholdstate.go +++ b/blockdag/thresholdstate.go @@ -230,7 +230,7 @@ func (b *BlockChain) thresholdState(prevNode *blockNode, checker thresholdCondit } // Get the previous block node. - countNode = countNode.parents[0] // TODO: (Stas) This is wrong. Modified only to satisfy compilation. + countNode = countNode.selectedParent } // The state is locked in if the number of blocks in the @@ -316,7 +316,7 @@ func (b *BlockChain) initThresholdCaches() error { // threshold state for each of them. This will ensure the caches are // populated and any states that needed to be recalculated due to // definition changes is done now. - prevNode := b.bestChain.Tips()[0].parents[0] // TODO: (Stas) This is wrong. Modified only to satisfy compilation. + prevNode := b.bestChain.Tips()[0].selectedParent // TODO: (Stas) This is wrong. Modified only to satisfy compilation. for bit := uint32(0); bit < vbNumBits; bit++ { checker := bitConditionChecker{bit: bit, chain: b} cache := &b.warningCaches[bit] diff --git a/blockdag/validate.go b/blockdag/validate.go index 65a14a087..193eb831b 100644 --- a/blockdag/validate.go +++ b/blockdag/validate.go @@ -1141,7 +1141,7 @@ func (b *BlockChain) checkConnectBlock(node *blockNode, block *btcutil.Block, vi // Enforce CHECKSEQUENCEVERIFY during all block validation checks once // the soft-fork deployment is fully active. - csvState, err := b.deploymentState(node.parents[0], dagconfig.DeploymentCSV) // TODO: (Stas) This is wrong. Modified only to satisfy compilation. + csvState, err := b.deploymentState(node.selectedParent, dagconfig.DeploymentCSV) if err != nil { return err } @@ -1153,7 +1153,7 @@ func (b *BlockChain) checkConnectBlock(node *blockNode, block *btcutil.Block, vi // We obtain the MTP of the *previous* block in order to // determine if transactions in the current block are final. - medianTime := node.parents[0].CalcPastMedianTime() // TODO: (Stas) This is wrong. Modified only to satisfy compilation. + medianTime := node.selectedParent.CalcPastMedianTime() // Additionally, if the CSV soft-fork package is now active, // then we also enforce the relative sequence number based diff --git a/blockdag/versionbits.go b/blockdag/versionbits.go index d33164169..bab9f593c 100644 --- a/blockdag/versionbits.go +++ b/blockdag/versionbits.go @@ -113,7 +113,7 @@ func (c bitConditionChecker) Condition(node *blockNode) (bool, error) { return false, nil } - expectedVersion, err := c.chain.calcNextBlockVersion(node.parents[0]) // TODO: (Stas) This is wrong. Modified only to satisfy compilation. + expectedVersion, err := c.chain.calcNextBlockVersion(node.selectedParent) if err != nil { return false, err } @@ -241,7 +241,7 @@ func (b *BlockChain) warnUnknownRuleActivations(node *blockNode) error { for bit := uint32(0); bit < vbNumBits; bit++ { checker := bitConditionChecker{bit: bit, chain: b} cache := &b.warningCaches[bit] - state, err := b.thresholdState(node.parents[0], checker, cache) // TODO: (Stas) This is wrong. Modified only to satisfy compilation. + state, err := b.thresholdState(node.selectedParent, checker, cache) if err != nil { return err } @@ -278,7 +278,7 @@ func (b *BlockChain) warnUnknownVersions(node *blockNode) error { // Warn if enough previous blocks have unexpected versions. numUpgraded := uint32(0) for i := uint32(0); i < unknownVerNumToCheck && node != nil; i++ { - expectedVersion, err := b.calcNextBlockVersion(node.parents[0]) // TODO: (Stas) This is wrong. Modified only to satisfy compilation. + expectedVersion, err := b.calcNextBlockVersion(node.selectedParent) if err != nil { return err } @@ -288,7 +288,7 @@ func (b *BlockChain) warnUnknownVersions(node *blockNode) error { numUpgraded++ } - node = node.parents[0] // TODO: (Stas) This is wrong. Modified only to satisfy compilation. + node = node.selectedParent } if numUpgraded > unknownVerWarnNum { log.Warn("Unknown block versions are being mined, so new " +