Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calculate pruning point utxo set from acceptance data #2123

Merged
merged 2 commits into from
Aug 17, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Calc new pruning point utxo diff through chain acceptance data
  • Loading branch information
michaelsutton committed Aug 15, 2022
commit 92a2c3739449f8100814a72efaef5b46491d725c
154 changes: 94 additions & 60 deletions domain/consensus/processes/pruningmanager/pruningmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -772,78 +772,112 @@ func (pm *pruningManager) calculateDiffBetweenPreviousAndCurrentPruningPoints(st
if err != nil {
return nil, err
}
currentPruningGhostDAG, err := pm.ghostdagDataStore.Get(pm.databaseContext, stagingArea, currentPruningHash, false)
if err != nil {
return nil, err
}
previousPruningGhostDAG, err := pm.ghostdagDataStore.Get(pm.databaseContext, stagingArea, previousPruningHash, false)
//currentPruningGhostDAG, err := pm.ghostdagDataStore.Get(pm.databaseContext, stagingArea, currentPruningHash, false)
//if err != nil {
// return nil, err
//}
//previousPruningGhostDAG, err := pm.ghostdagDataStore.Get(pm.databaseContext, stagingArea, previousPruningHash, false)
//if err != nil {
// return nil, err
//}

utxoDiff := utxo.NewMutableUTXODiff()

iterator, err := pm.dagTraversalManager.SelectedChildIterator(stagingArea, currentPruningHash, previousPruningHash, false)
if err != nil {
return nil, err
}
defer iterator.Close()

currentPruningCurrentDiffChild := currentPruningHash
previousPruningCurrentDiffChild := previousPruningHash
// We need to use BlueWork because it's the only thing that's monotonic in the whole DAG
// We use the BlueWork to know which point is currently lower on the DAG so we can keep climbing its children,
// that way we keep climbing on the lowest point until they both reach the exact same descendant
currentPruningCurrentDiffChildBlueWork := currentPruningGhostDAG.BlueWork()
previousPruningCurrentDiffChildBlueWork := previousPruningGhostDAG.BlueWork()

var diffHashesFromPrevious []*externalapi.DomainHash
var diffHashesFromCurrent []*externalapi.DomainHash
for {
// if currentPruningCurrentDiffChildBlueWork > previousPruningCurrentDiffChildBlueWork
if currentPruningCurrentDiffChildBlueWork.Cmp(previousPruningCurrentDiffChildBlueWork) == 1 {
diffHashesFromPrevious = append(diffHashesFromPrevious, previousPruningCurrentDiffChild)
previousPruningCurrentDiffChild, err = pm.utxoDiffStore.UTXODiffChild(pm.databaseContext, stagingArea, previousPruningCurrentDiffChild)
if err != nil {
return nil, err
}
diffChildGhostDag, err := pm.ghostdagDataStore.Get(pm.databaseContext, stagingArea, previousPruningCurrentDiffChild, false)
if err != nil {
return nil, err
}
previousPruningCurrentDiffChildBlueWork = diffChildGhostDag.BlueWork()
} else if currentPruningCurrentDiffChild.Equal(previousPruningCurrentDiffChild) {
break
} else {
diffHashesFromCurrent = append(diffHashesFromCurrent, currentPruningCurrentDiffChild)
currentPruningCurrentDiffChild, err = pm.utxoDiffStore.UTXODiffChild(pm.databaseContext, stagingArea, currentPruningCurrentDiffChild)
if err != nil {
return nil, err
}
diffChildGhostDag, err := pm.ghostdagDataStore.Get(pm.databaseContext, stagingArea, currentPruningCurrentDiffChild, false)
if err != nil {
return nil, err
}
currentPruningCurrentDiffChildBlueWork = diffChildGhostDag.BlueWork()
}
}
// The order in which we apply the diffs should be from top to bottom, but we traversed from bottom to top
// so we apply the diffs in reverse order.
oldDiff := utxo.NewMutableUTXODiff()
for i := len(diffHashesFromPrevious) - 1; i >= 0; i-- {
utxoDiff, err := pm.utxoDiffStore.UTXODiff(pm.databaseContext, stagingArea, diffHashesFromPrevious[i])
if err != nil {
return nil, err
}
err = oldDiff.WithDiffInPlace(utxoDiff)
for ok := iterator.First(); ok; ok = iterator.Next() {
child, err := iterator.Get()
if err != nil {
return nil, err
}
}
newDiff := utxo.NewMutableUTXODiff()
for i := len(diffHashesFromCurrent) - 1; i >= 0; i-- {
utxoDiff, err := pm.utxoDiffStore.UTXODiff(pm.databaseContext, stagingArea, diffHashesFromCurrent[i])
chainBlockAcceptanceData, err := pm.acceptanceDataStore.Get(pm.databaseContext, stagingArea, child)
if err != nil {
return nil, err
}
err = newDiff.WithDiffInPlace(utxoDiff)
if err != nil {
return nil, err
for _, blockAcceptanceData := range chainBlockAcceptanceData {
blockHeader, err := pm.blockHeaderStore.BlockHeader(pm.databaseContext, stagingArea, blockAcceptanceData.BlockHash)
if err != nil {
return nil, err
}
for _, transactionAcceptanceData := range blockAcceptanceData.TransactionAcceptanceData {
if transactionAcceptanceData.IsAccepted {
err = utxoDiff.AddTransaction(transactionAcceptanceData.Transaction, blockHeader.DAAScore())
if err != nil {
return nil, err
}
}
}
}
}
return oldDiff.DiffFrom(newDiff.ToImmutable())
return utxoDiff.ToImmutable(), nil

//currentPruningCurrentDiffChild := currentPruningHash
//previousPruningCurrentDiffChild := previousPruningHash
//// We need to use BlueWork because it's the only thing that's monotonic in the whole DAG
//// We use the BlueWork to know which point is currently lower on the DAG so we can keep climbing its children,
//// that way we keep climbing on the lowest point until they both reach the exact same descendant
//currentPruningCurrentDiffChildBlueWork := currentPruningGhostDAG.BlueWork()
//previousPruningCurrentDiffChildBlueWork := previousPruningGhostDAG.BlueWork()
//
//var diffHashesFromPrevious []*externalapi.DomainHash
//var diffHashesFromCurrent []*externalapi.DomainHash
//for {
// // if currentPruningCurrentDiffChildBlueWork > previousPruningCurrentDiffChildBlueWork
// if currentPruningCurrentDiffChildBlueWork.Cmp(previousPruningCurrentDiffChildBlueWork) == 1 {
// diffHashesFromPrevious = append(diffHashesFromPrevious, previousPruningCurrentDiffChild)
// previousPruningCurrentDiffChild, err = pm.utxoDiffStore.UTXODiffChild(pm.databaseContext, stagingArea, previousPruningCurrentDiffChild)
// if err != nil {
// return nil, err
// }
// diffChildGhostDag, err := pm.ghostdagDataStore.Get(pm.databaseContext, stagingArea, previousPruningCurrentDiffChild, false)
// if err != nil {
// return nil, err
// }
// previousPruningCurrentDiffChildBlueWork = diffChildGhostDag.BlueWork()
// } else if currentPruningCurrentDiffChild.Equal(previousPruningCurrentDiffChild) {
// break
// } else {
// diffHashesFromCurrent = append(diffHashesFromCurrent, currentPruningCurrentDiffChild)
// currentPruningCurrentDiffChild, err = pm.utxoDiffStore.UTXODiffChild(pm.databaseContext, stagingArea, currentPruningCurrentDiffChild)
// if err != nil {
// return nil, err
// }
// diffChildGhostDag, err := pm.ghostdagDataStore.Get(pm.databaseContext, stagingArea, currentPruningCurrentDiffChild, false)
// if err != nil {
// return nil, err
// }
// currentPruningCurrentDiffChildBlueWork = diffChildGhostDag.BlueWork()
// }
//}
//// The order in which we apply the diffs should be from top to bottom, but we traversed from bottom to top
//// so we apply the diffs in reverse order.
//oldDiff := utxo.NewMutableUTXODiff()
//for i := len(diffHashesFromPrevious) - 1; i >= 0; i-- {
// utxoDiff, err := pm.utxoDiffStore.UTXODiff(pm.databaseContext, stagingArea, diffHashesFromPrevious[i])
// if err != nil {
// return nil, err
// }
// err = oldDiff.WithDiffInPlace(utxoDiff)
// if err != nil {
// return nil, err
// }
//}
//newDiff := utxo.NewMutableUTXODiff()
//for i := len(diffHashesFromCurrent) - 1; i >= 0; i-- {
// utxoDiff, err := pm.utxoDiffStore.UTXODiff(pm.databaseContext, stagingArea, diffHashesFromCurrent[i])
// if err != nil {
// return nil, err
// }
// err = newDiff.WithDiffInPlace(utxoDiff)
// if err != nil {
// return nil, err
// }
//}
//return oldDiff.DiffFrom(newDiff.ToImmutable())
}

// finalityScore is the number of finality intervals passed since
Expand Down