Svarog fb6c9c8f21
Remove virtual diff parents (#1550)
* resolveSingleBlockStatus: If the block being resolved is not going to be the next selectedTip - set it's diffParent to be the old selectedTip

* resolveSingleBlockStatus: If the block being resolved is going to be the next selectedTip - set it as old selectedTip's diffChild

* Remove any mentions of virtualDiffParents

* If block is genesis - don't do all the mumbo-jumbo with oldSelectedTip

* Check an unchecked error

* Write a better log message
2021-02-23 17:19:35 +02:00

113 lines
3.9 KiB
Go

package consensusstatemanager
import (
"github.com/kaspanet/kaspad/domain/consensus/model"
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
"github.com/kaspanet/kaspad/infrastructure/logger"
)
func (csm *consensusStateManager) updateVirtual(newBlockHash *externalapi.DomainHash,
tips []*externalapi.DomainHash) (*externalapi.SelectedChainPath, externalapi.UTXODiff, error) {
onEnd := logger.LogAndMeasureExecutionTime(log, "updateVirtual")
defer onEnd()
log.Debugf("updateVirtual start for block %s", newBlockHash)
log.Debugf("Saving a reference to the GHOSTDAG data of the old virtual")
var oldVirtualSelectedParent *externalapi.DomainHash
if !newBlockHash.Equal(csm.genesisHash) {
oldVirtualGHOSTDAGData, err := csm.ghostdagDataStore.Get(csm.databaseContext, model.VirtualBlockHash)
if err != nil {
return nil, nil, err
}
oldVirtualSelectedParent = oldVirtualGHOSTDAGData.SelectedParent()
}
log.Debugf("Picking virtual parents from tips len: %d", len(tips))
virtualParents, err := csm.pickVirtualParents(tips)
if err != nil {
return nil, nil, err
}
log.Debugf("Picked virtual parents: %s", virtualParents)
err = csm.dagTopologyManager.SetParents(model.VirtualBlockHash, virtualParents)
if err != nil {
return nil, nil, err
}
log.Debugf("Set new parents for the virtual block hash")
err = csm.ghostdagManager.GHOSTDAG(model.VirtualBlockHash)
if err != nil {
return nil, nil, err
}
log.Debugf("Calculating past UTXO, acceptance data, and multiset for the new virtual block")
virtualUTXODiff, virtualAcceptanceData, virtualMultiset, err := csm.CalculatePastUTXOAndAcceptanceData(model.VirtualBlockHash)
if err != nil {
return nil, nil, err
}
log.Debugf("Calculated the past UTXO of the new virtual. "+
"Diff toAdd length: %d, toRemove length: %d",
virtualUTXODiff.ToAdd().Len(), virtualUTXODiff.ToRemove().Len())
log.Debugf("Staging new acceptance data for the virtual block")
csm.acceptanceDataStore.Stage(model.VirtualBlockHash, virtualAcceptanceData)
log.Debugf("Staging new multiset for the virtual block")
csm.multisetStore.Stage(model.VirtualBlockHash, virtualMultiset)
log.Debugf("Staging new UTXO diff for the virtual block")
csm.consensusStateStore.StageVirtualUTXODiff(virtualUTXODiff)
log.Debugf("Updating the selected tip's utxo-diff after adding %s to the DAG", newBlockHash)
err = csm.updateSelectedTipUTXODiff(virtualUTXODiff)
if err != nil {
return nil, nil, err
}
log.Debugf("Calculating selected parent chain changes")
var selectedParentChainChanges *externalapi.SelectedChainPath
if !newBlockHash.Equal(csm.genesisHash) {
newVirtualGHOSTDAGData, err := csm.ghostdagDataStore.Get(csm.databaseContext, model.VirtualBlockHash)
if err != nil {
return nil, nil, err
}
newVirtualSelectedParent := newVirtualGHOSTDAGData.SelectedParent()
selectedParentChainChanges, err = csm.dagTraversalManager.
CalculateChainPath(oldVirtualSelectedParent, newVirtualSelectedParent)
if err != nil {
return nil, nil, err
}
log.Debugf("Selected parent chain changes: %d blocks were removed and %d blocks were added",
len(selectedParentChainChanges.Removed), len(selectedParentChainChanges.Added))
}
return selectedParentChainChanges, virtualUTXODiff, nil
}
func (csm *consensusStateManager) updateSelectedTipUTXODiff(virtualUTXODiff externalapi.UTXODiff) error {
onEnd := logger.LogAndMeasureExecutionTime(log, "updateSelectedTipUTXODiff")
defer onEnd()
selectedTip, err := csm.selectedTip()
if err != nil {
return err
}
log.Debugf("Calculating new UTXO diff for virtual diff parent %s", selectedTip)
selectedTipUTXODiff, err := csm.utxoDiffStore.UTXODiff(csm.databaseContext, selectedTip)
if err != nil {
return err
}
newDiff, err := virtualUTXODiff.DiffFrom(selectedTipUTXODiff)
if err != nil {
return err
}
log.Debugf("Staging new UTXO diff for virtual diff parent %s", selectedTip)
csm.stageDiff(selectedTip, newDiff, nil)
return nil
}