diff --git a/blockdag/dag.go b/blockdag/dag.go index 20827ba3d..80f36fcb7 100644 --- a/blockdag/dag.go +++ b/blockdag/dag.go @@ -672,29 +672,30 @@ func (dag *BlockDAG) checkFinalityRules(newNode *blockNode) error { return nil } -// newFinalityPoint return the (potentially) new finality point after the the introduction of a new block -func (dag *BlockDAG) newFinalityPoint(newNode *blockNode) *blockNode { - // if the new node is the genesis block - it should be the new finality point - if newNode.isGenesis() { - return newNode - } - - // We are looking for a new finality point only if the new block's finality score is higher - // than the existing finality point's - if newNode.finalityScore() <= dag.lastFinalityPoint.finalityScore() { - return dag.lastFinalityPoint - } - - var currentNode *blockNode - for currentNode = newNode.selectedParent; ; currentNode = currentNode.selectedParent { - // If current node's finality score is higher than it's selectedParent's - - // current node is the new finalityPoint - if currentNode.isGenesis() || currentNode.finalityScore() > currentNode.selectedParent.finalityScore() { - break +// updateFinalityPoint updates the dag's last finality point if necessary. +func (dag *BlockDAG) updateFinalityPoint() { + selectedTip := dag.selectedTip() + var newFinalityPoint *blockNode + // if the selected tip is the genesis block - it should be the new finality point + if selectedTip.isGenesis() { + newFinalityPoint = selectedTip + } else { + // We are looking for a new finality point only if the new block's finality score is higher + // by 2 than the existing finality point's + if selectedTip.finalityScore() < dag.lastFinalityPoint.finalityScore()+2 { + return } - } - return currentNode + var currentNode *blockNode + for currentNode = selectedTip.selectedParent; ; currentNode = currentNode.selectedParent { + // We look for the first node in the selected parent chain that has a higher finality score than the last finality point. + if currentNode.selectedParent.finalityScore() == dag.lastFinalityPoint.finalityScore() { + break + } + } + newFinalityPoint = currentNode + } + dag.lastFinalityPoint = newFinalityPoint } // NextBlockFeeTransactionWithLock prepares the fee transaction for the next mined block @@ -763,7 +764,7 @@ func (dag *BlockDAG) applyDAGChanges(node *blockNode, block *util.Block, newBloc dag.index.SetStatusFlags(node, statusValid) // And now we can update the finality point of the DAG (if required) - dag.lastFinalityPoint = dag.newFinalityPoint(node) + dag.updateFinalityPoint() return virtualUTXODiff, nil }