[DEV-34] Added selectedParent to blockNode.

This commit is contained in:
Stas Boutenko 2018-06-26 12:19:05 +03:00
parent 14287a1dd1
commit a289b72980
8 changed files with 29 additions and 26 deletions

View File

@ -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

View File

@ -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
}

View File

@ -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

View File

@ -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) {

View File

@ -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

View File

@ -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]

View File

@ -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

View File

@ -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 " +