kaspad/domain/consensus/utils/utxo/immutable_utxo_diff.go
Ori Newman 2adb4f5d0f
Fix UTXO index (#1548)
* Add VirtualUTXODiff and VirtualParents to block insertion result

* Add GetVirtualUTXOs

* Add OnPruningPointUTXOSetOverrideHandler

* Add recovery to UTXO index

* Add UTXO set override notification

* Fix compilation error

* Fix iterators in UTXO index and fix TestUTXOIndex

* Change Dialing to DEBUG

* Change LogBlock location

* Rename StopNotify to StopNotifying

* Add sanity check

* Add comment

* Remove receiver from serialization functions

Co-authored-by: Elichai Turkel <elichai.turkel@gmail.com>
2021-02-23 16:51:51 +02:00

87 lines
2.1 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("Attempt to read from an invalidated UTXODiff")
}
return iud.mutableUTXODiff.ToAdd()
}
func (iud *immutableUTXODiff) ToRemove() externalapi.UTXOCollection {
if iud.isInvalidated {
panic("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("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("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 {
return iud.cloneMutable()
}
func (iud *immutableUTXODiff) cloneMutable() *mutableUTXODiff {
if iud == nil {
return nil
}
return iud.mutableUTXODiff.clone()
}