[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:
Ori Newman 2019-04-11 16:05:55 +03:00 committed by Evgeny Khirin
parent 8630b65bb2
commit 9276494820

View File

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