mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-10-14 00:59:33 +00:00
[NOD-85] Fix dag.NewFinalityPoint (#246)
* [NOD-85] Fix dag.NewFinalityPoint * [NOD-85] change newFinalityPoint to updateFinalityPoint * [NOD-85] Fix comment in updateFinalityPoint
This commit is contained in:
parent
8630b65bb2
commit
9276494820
@ -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
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user