From d8f72e2b27bab86e8e6a7face03dced556cbbd1f Mon Sep 17 00:00:00 2001 From: Mike Zak Date: Mon, 16 Nov 2020 15:58:05 +0200 Subject: [PATCH] [NOD-1532] Update VirtualUTXODiffParents diffs even if list didn't change --- .../calculate_past_utxo.go | 17 +++++----- .../consensusstatemanager/update_virtual.go | 32 +++++++++---------- 2 files changed, 24 insertions(+), 25 deletions(-) diff --git a/domain/consensus/processes/consensusstatemanager/calculate_past_utxo.go b/domain/consensus/processes/consensusstatemanager/calculate_past_utxo.go index f2b8d48ea..7cbef09cb 100644 --- a/domain/consensus/processes/consensusstatemanager/calculate_past_utxo.go +++ b/domain/consensus/processes/consensusstatemanager/calculate_past_utxo.go @@ -1,10 +1,9 @@ package consensusstatemanager import ( - "errors" - "github.com/kaspanet/kaspad/domain/consensus/utils/constants" "github.com/kaspanet/kaspad/domain/consensus/utils/multiset" + "github.com/pkg/errors" "github.com/kaspanet/kaspad/domain/consensus/model" "github.com/kaspanet/kaspad/domain/consensus/model/externalapi" @@ -186,7 +185,7 @@ func (csm *consensusStateManager) checkTransactionMass( } func (csm *consensusStateManager) RestorePastUTXOSetIterator(blockHash *externalapi.DomainHash) (model.ReadOnlyUTXOSetIterator, error) { - diff, _, _, err := csm.CalculatePastUTXOAndAcceptanceData(blockHash) + blockDiff, _, _, err := csm.CalculatePastUTXOAndAcceptanceData(blockHash) if err != nil { return nil, err } @@ -196,25 +195,25 @@ func (csm *consensusStateManager) RestorePastUTXOSetIterator(blockHash *external return nil, err } - pastUTXO := model.NewUTXODiff() + virtualUTXO := model.NewUTXODiff() for virtualUTXOSetIterator.Next() { outpoint, utxoEntry, err := virtualUTXOSetIterator.Get() if err != nil { return nil, err } - pastUTXO.ToAdd[*outpoint] = utxoEntry + virtualUTXO.ToAdd[*outpoint] = utxoEntry } - diff, err = utxoalgebra.WithDiff(pastUTXO, diff) + blockUTXO, err := utxoalgebra.WithDiff(virtualUTXO, blockDiff) if err != nil { return nil, err } - if len(diff.ToRemove) > 0 { - return nil, errors.New("diff.ToRemove is expected to be empty") + if len(blockUTXO.ToRemove) > 0 { + return nil, errors.New("blockUTXO.ToRemove is expected to be empty") } - return newUTXOSetIterator(diff.ToAdd), nil + return newUTXOSetIterator(blockUTXO.ToAdd), nil } type utxoOutpointEntryPair struct { diff --git a/domain/consensus/processes/consensusstatemanager/update_virtual.go b/domain/consensus/processes/consensusstatemanager/update_virtual.go index 10c113ab2..fb0df7df4 100644 --- a/domain/consensus/processes/consensusstatemanager/update_virtual.go +++ b/domain/consensus/processes/consensusstatemanager/update_virtual.go @@ -51,34 +51,34 @@ func (csm *consensusStateManager) updateVirtual(newBlockHash *externalapi.Domain func (csm *consensusStateManager) updateVirtualDiffParents( newBlockHash *externalapi.DomainHash, virtualUTXODiff *model.UTXODiff) error { - // If the status of the new block is not `Valid` - virtualDiffParents didn't change - status, err := csm.blockStatusStore.Get(csm.databaseContext, newBlockHash) - if err != nil { - return err - } - if status != externalapi.StatusValid { - return nil - } - var newVirtualDiffParents []*externalapi.DomainHash if *newBlockHash == *csm.genesisHash { newVirtualDiffParents = []*externalapi.DomainHash{newBlockHash} } else { - virtualDiffParents, err := csm.consensusStateStore.VirtualDiffParents(csm.databaseContext) + oldVirtualDiffParents, err := csm.consensusStateStore.VirtualDiffParents(csm.databaseContext) if err != nil { return err } - newBlockParentsSlice, err := csm.dagTopologyManager.Parents(newBlockHash) + // If the status of the new block is not `Valid` - virtualDiffParents didn't change + status, err := csm.blockStatusStore.Get(csm.databaseContext, newBlockHash) if err != nil { return err } - newBlockParents := hashset.NewFromSlice(newBlockParentsSlice...) + if status != externalapi.StatusValid { + newVirtualDiffParents = oldVirtualDiffParents + } else { + newBlockParentsSlice, err := csm.dagTopologyManager.Parents(newBlockHash) + if err != nil { + return err + } + newBlockParents := hashset.NewFromSlice(newBlockParentsSlice...) - newVirtualDiffParents = []*externalapi.DomainHash{newBlockHash} - for _, virtualDiffParent := range virtualDiffParents { - if !newBlockParents.Contains(virtualDiffParent) { - newVirtualDiffParents = append(newVirtualDiffParents, virtualDiffParent) + newVirtualDiffParents = []*externalapi.DomainHash{newBlockHash} + for _, virtualDiffParent := range oldVirtualDiffParents { + if !newBlockParents.Contains(virtualDiffParent) { + newVirtualDiffParents = append(newVirtualDiffParents, virtualDiffParent) + } } } }