kaspad/domain/consensus/utils/utxo/immutable_utxo_diff.go
Svarog dfd8b3423d
Implement new mechanism for updating UTXO Diffs (#1671)
* Use selectedParent instead of selectedTip for non-selectedTip blocks in restoreSingleBlockStatus

* Cache the selectedParent for re-use in a resolveSingleBlockStatus chain

* Implement and use reverseUTXOSet

* Reverse blocks in correct order

* Support resolveBlockStatus without separate stagingAreas for usage of testConsensus

* Handle the case where the tip of the resolved block is not the next selectedTip

* Unify isResolveTip

* Some minor fixes and cleanup

* Add full finality window re-org test to stability-slow

* rename: useSeparateStagingAreasPerBlock -> useSeparateStagingAreaPerBlock

* Better logs in resolveSingleBlockStatus

* A few retouches to reverseUTXODiffs

* TEMPORARY COMMIT: EXTRAT ALL DIFFFROMS TO SEPARATE METHODS

* TEMPORARY COMMIT: REMOVE DIFFICULTY CHECKS IN DEVNET

* Don't pre-allocate in utxo-algebra, since the numbers are not known ahead-of-time

* Add some logs to reverseUTXODiffs

* Revert "TEMPORARY COMMIT: REMOVE DIFFICULTY CHECKS IN DEVNET"

This reverts commit c0af9dc6ade78a914c970e11bc63c34605565f57.

* Revert "TEMPORARY COMMIT: EXTRAT ALL DIFFFROMS TO SEPARATE METHODS"

This reverts commit 4fcca1b48c3a1183598833a355b9bfaf169edba1.

* Remove redundant paranthesis

* Revise some logs messages

* Rename:oneBlockBeforeCurrentUTXOSet -> lastResolvedBlockUTXOSet

* Don't break if the block was resolved as invalid

* rename unverifiedBlocks to recentlyVerifiedBlcks in reverseUTXODiffs

* Add errors.New to the panic, for a stack trace

* Reverse the UTXODiffs after the main block has been commited

* Use the correct value for previousUTXODiff

* Add test for ReverseUTXODiff

* Fix some names and comments

* Update TestReverseUTXODiffs to use consensus.Config

* Fix comments mentioning 'oneBlockBeforeTip'
2021-04-20 10:26:55 +03:00

104 lines
2.5 KiB
Go

package utxo
import (
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
"github.com/pkg/errors"
)
type immutableUTXODiff struct {
mutableUTXODiff *mutableUTXODiff
isInvalidated bool
}
func (iud *immutableUTXODiff) ToAdd() externalapi.UTXOCollection {
if iud.isInvalidated {
panic(errors.New("Attempt to read from an invalidated UTXODiff"))
}
return iud.mutableUTXODiff.ToAdd()
}
func (iud *immutableUTXODiff) ToRemove() externalapi.UTXOCollection {
if iud.isInvalidated {
panic(errors.New("Attempt to read from an invalidated UTXODiff"))
}
return iud.mutableUTXODiff.ToRemove()
}
func (iud *immutableUTXODiff) WithDiff(other externalapi.UTXODiff) (externalapi.UTXODiff, error) {
if iud.isInvalidated {
panic(errors.New("Attempt to read from an invalidated UTXODiff"))
}
return iud.mutableUTXODiff.WithDiff(other)
}
func (iud *immutableUTXODiff) DiffFrom(other externalapi.UTXODiff) (externalapi.UTXODiff, error) {
if iud.isInvalidated {
panic(errors.New("Attempt to read from an invalidated UTXODiff"))
}
return iud.mutableUTXODiff.DiffFrom(other)
}
// NewUTXODiff creates an empty UTXODiff
func NewUTXODiff() externalapi.UTXODiff {
return newUTXODiff()
}
func newUTXODiff() *immutableUTXODiff {
return &immutableUTXODiff{
mutableUTXODiff: newMutableUTXODiff(),
isInvalidated: false,
}
}
// NewUTXODiffFromCollections returns a new UTXODiff with the given toAdd and toRemove collections
func NewUTXODiffFromCollections(toAdd, toRemove externalapi.UTXOCollection) (externalapi.UTXODiff, error) {
add, ok := toAdd.(utxoCollection)
if !ok {
return nil, errors.New("toAdd is not of type utxoCollection")
}
remove, ok := toRemove.(utxoCollection)
if !ok {
return nil, errors.New("toRemove is not of type utxoCollection")
}
return &immutableUTXODiff{
mutableUTXODiff: &mutableUTXODiff{
toAdd: add,
toRemove: remove,
},
}, nil
}
func (iud *immutableUTXODiff) CloneMutable() externalapi.MutableUTXODiff {
if iud.isInvalidated {
panic(errors.New("Attempt to read from an invalidated UTXODiff"))
}
return iud.cloneMutable()
}
func (iud *immutableUTXODiff) Reversed() externalapi.UTXODiff {
if iud.isInvalidated {
panic(errors.New("Attempt to read from an invalidated UTXODiff"))
}
return &immutableUTXODiff{
mutableUTXODiff: iud.mutableUTXODiff.Reversed(),
isInvalidated: false,
}
}
func (iud *immutableUTXODiff) cloneMutable() *mutableUTXODiff {
if iud == nil {
return nil
}
return iud.mutableUTXODiff.clone()
}
func (iud immutableUTXODiff) String() string {
return iud.mutableUTXODiff.String()
}