mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-03-30 15:08:33 +00:00
[NOD-1502] Implement RestorePastUTXOSetIterator (#993)
* [NOD-1502] Implement RestorePastUTXOSetIterator * [NOD-1502] Rename newUtxoSetIterator->newUTXOSetIterator
This commit is contained in:
parent
3f979399b1
commit
c7f2de73df
@ -53,3 +53,7 @@ func (c consensusStateStore) Tips(dbContext model.DBReader) ([]*externalapi.Doma
|
||||
func (c consensusStateStore) StageTips(tipHashes []*externalapi.DomainHash) error {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (c consensusStateStore) VirtualUTXOSetIterator(dbContext model.DBReader) (model.ReadOnlyUTXOSetIterator, error) {
|
||||
panic("implement me")
|
||||
}
|
||||
|
@ -10,6 +10,7 @@ type ConsensusStateStore interface {
|
||||
StageVirtualUTXODiff(virtualUTXODiff *UTXODiff)
|
||||
UTXOByOutpoint(dbContext DBReader, outpoint *externalapi.DomainOutpoint) (*externalapi.UTXOEntry, error)
|
||||
HasUTXOByOutpoint(dbContext DBReader, outpoint *externalapi.DomainOutpoint) (bool, error)
|
||||
VirtualUTXOSetIterator(dbContext DBReader) (ReadOnlyUTXOSetIterator, error)
|
||||
|
||||
StageVirtualDiffParents(virtualDiffParents []*externalapi.DomainHash) error
|
||||
VirtualDiffParents(dbContext DBReader) ([]*externalapi.DomainHash, error)
|
||||
|
@ -7,4 +7,5 @@ type ConsensusStateManager interface {
|
||||
AddBlockToVirtual(blockHash *externalapi.DomainHash) error
|
||||
PopulateTransactionWithUTXOEntries(transaction *externalapi.DomainTransaction) error
|
||||
SetPruningPointUTXOSet(pruningPoint *externalapi.DomainHash, serializedUTXOSet []byte) error
|
||||
RestorePastUTXOSetIterator(blockHash *externalapi.DomainHash) (ReadOnlyUTXOSetIterator, error)
|
||||
}
|
||||
|
@ -165,3 +165,65 @@ func (csm *consensusStateManager) checkTransactionMass(
|
||||
|
||||
return true, accumulatedMassAfter
|
||||
}
|
||||
|
||||
func (csm *consensusStateManager) RestorePastUTXOSetIterator(blockHash *externalapi.DomainHash) (model.ReadOnlyUTXOSetIterator, error) {
|
||||
diff, _, _, err := csm.calculatePastUTXOAndAcceptanceData(blockHash)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
virtualUTXOSetIterator, err := csm.consensusStateStore.VirtualUTXOSetIterator(csm.databaseContext)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pastUTXO := model.NewUTXODiff()
|
||||
for virtualUTXOSetIterator.Next() {
|
||||
outpoint, utxoEntry := virtualUTXOSetIterator.Get()
|
||||
pastUTXO.ToAdd[*outpoint] = utxoEntry
|
||||
}
|
||||
|
||||
diff, err = utxoalgebra.WithDiff(pastUTXO, diff)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(diff.ToRemove) > 0 {
|
||||
return nil, errors.New("diff.ToRemove is expected to be empty")
|
||||
}
|
||||
|
||||
return newUTXOSetIterator(diff.ToAdd), nil
|
||||
}
|
||||
|
||||
type utxoOutpointEntryPair struct {
|
||||
outpoint externalapi.DomainOutpoint
|
||||
entry *externalapi.UTXOEntry
|
||||
}
|
||||
|
||||
type utxoSetIterator struct {
|
||||
index int
|
||||
pairs []utxoOutpointEntryPair
|
||||
}
|
||||
|
||||
func newUTXOSetIterator(collection model.UTXOCollection) *utxoSetIterator {
|
||||
pairs := make([]utxoOutpointEntryPair, len(collection))
|
||||
i := 0
|
||||
for outpoint, entry := range collection {
|
||||
pairs[i] = utxoOutpointEntryPair{
|
||||
outpoint: outpoint,
|
||||
entry: entry,
|
||||
}
|
||||
i++
|
||||
}
|
||||
return &utxoSetIterator{index: 0, pairs: pairs}
|
||||
}
|
||||
|
||||
func (u utxoSetIterator) Next() bool {
|
||||
u.index++
|
||||
return u.index != len(u.pairs)
|
||||
}
|
||||
|
||||
func (u utxoSetIterator) Get() (outpoint *externalapi.DomainOutpoint, utxoEntry *externalapi.UTXOEntry) {
|
||||
pair := u.pairs[u.index]
|
||||
return &pair.outpoint, pair.entry
|
||||
}
|
||||
|
@ -137,8 +137,12 @@ func (csm *consensusStateManager) mergeSetIncrease(
|
||||
|
||||
func (csm *consensusStateManager) boundedMergeBreakingParents(parents []*externalapi.DomainHash) (hashset.HashSet, error) {
|
||||
// Temporarily set virtual to all parents, so that we can run ghostdag on it
|
||||
csm.dagTopologyManager.SetParents(model.VirtualBlockHash, parents)
|
||||
err := csm.ghostdagManager.GHOSTDAG(model.VirtualBlockHash)
|
||||
err := csm.dagTopologyManager.SetParents(model.VirtualBlockHash, parents)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = csm.ghostdagManager.GHOSTDAG(model.VirtualBlockHash)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -13,7 +13,10 @@ func (csm *consensusStateManager) updateVirtual(newBlockHash *externalapi.Domain
|
||||
return err
|
||||
}
|
||||
|
||||
csm.dagTopologyManager.SetParents(model.VirtualBlockHash, virtualParents)
|
||||
err = csm.dagTopologyManager.SetParents(model.VirtualBlockHash, virtualParents)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = csm.ghostdagManager.GHOSTDAG(model.VirtualBlockHash)
|
||||
if err != nil {
|
||||
|
Loading…
x
Reference in New Issue
Block a user