mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-05-24 07:46:45 +00:00

* [NOD-1551] Make UTXO-Diff implemented fully in utils/utxo * [NOD-1551] Fixes everywhere except database * [NOD-1551] Fix database * [NOD-1551] Add comments * [NOD-1551] Partial commit * [NOD-1551] Comlete making UTXOEntry immutable + don't clone it in UTXOCollectionClone * [NOD-1551] Rename ToUnmutable -> ToImmutable * [NOD-1551] Track immutable references generated from mutable UTXODiff, and invalidate them if the mutable one changed * [NOD-1551] Clone scriptPubKey in NewUTXOEntry * [NOD-1551] Remove redundant code * [NOD-1551] Remove redundant call for .CloneMutable and then .ToImmutable * [NOD-1551] Make utxoEntry pointert-receiver + clone ScriptPubKey in getter
84 lines
2.9 KiB
Go
84 lines
2.9 KiB
Go
package consensusstatemanager
|
|
|
|
import (
|
|
"github.com/kaspanet/kaspad/domain/consensus/model"
|
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
func (csm *consensusStateManager) stageDiff(blockHash *externalapi.DomainHash,
|
|
utxoDiff model.UTXODiff, utxoDiffChild *externalapi.DomainHash) error {
|
|
|
|
log.Tracef("stageDiff start for block %s", blockHash)
|
|
defer log.Tracef("stageDiff end for block %s", blockHash)
|
|
|
|
log.Tracef("Staging block %s as the diff child of %s", utxoDiffChild, blockHash)
|
|
csm.utxoDiffStore.Stage(blockHash, utxoDiff, utxoDiffChild)
|
|
|
|
if utxoDiffChild == nil {
|
|
log.Tracef("Adding block %s to the virtual diff parents", blockHash)
|
|
return csm.addToVirtualDiffParents(blockHash)
|
|
}
|
|
|
|
log.Tracef("Removing block %s from the virtual diff parents", blockHash)
|
|
return csm.removeFromVirtualDiffParents(blockHash)
|
|
}
|
|
|
|
func (csm *consensusStateManager) addToVirtualDiffParents(blockHash *externalapi.DomainHash) error {
|
|
log.Tracef("addToVirtualDiffParents start for block %s", blockHash)
|
|
defer log.Tracef("addToVirtualDiffParents end for block %s", blockHash)
|
|
|
|
var oldVirtualDiffParents []*externalapi.DomainHash
|
|
if *blockHash != *csm.genesisHash {
|
|
var err error
|
|
oldVirtualDiffParents, err = csm.consensusStateStore.VirtualDiffParents(csm.databaseContext)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
isInVirtualDiffParents := false
|
|
for _, diffParent := range oldVirtualDiffParents {
|
|
if *diffParent == *blockHash {
|
|
isInVirtualDiffParents = true
|
|
break
|
|
}
|
|
}
|
|
|
|
if isInVirtualDiffParents {
|
|
log.Tracef("Block %s is already a virtual diff parent, so there's no need to add it", blockHash)
|
|
return nil
|
|
}
|
|
|
|
newVirtualDiffParents := append([]*externalapi.DomainHash{blockHash}, oldVirtualDiffParents...)
|
|
log.Tracef("Staging virtual diff parents after adding %s to it", blockHash)
|
|
csm.consensusStateStore.StageVirtualDiffParents(newVirtualDiffParents)
|
|
return nil
|
|
}
|
|
|
|
func (csm *consensusStateManager) removeFromVirtualDiffParents(blockHash *externalapi.DomainHash) error {
|
|
log.Tracef("removeFromVirtualDiffParents start for block %s", blockHash)
|
|
defer log.Tracef("removeFromVirtualDiffParents end for block %s", blockHash)
|
|
|
|
oldVirtualDiffParents, err := csm.consensusStateStore.VirtualDiffParents(csm.databaseContext)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
newVirtualDiffParents := make([]*externalapi.DomainHash, 0, len(oldVirtualDiffParents)-1)
|
|
for _, diffParent := range oldVirtualDiffParents {
|
|
if *diffParent != *blockHash {
|
|
newVirtualDiffParents = append(newVirtualDiffParents, diffParent)
|
|
}
|
|
}
|
|
|
|
if len(newVirtualDiffParents) != len(oldVirtualDiffParents)-1 {
|
|
return errors.Errorf("expected to remove one member from virtual diff parents and "+
|
|
"have a length of %d but got length of %d", len(oldVirtualDiffParents)-1, len(newVirtualDiffParents))
|
|
}
|
|
|
|
log.Tracef("Staging virtual diff parents after removing %s from it", blockHash)
|
|
csm.consensusStateStore.StageVirtualDiffParents(newVirtualDiffParents)
|
|
return nil
|
|
}
|