mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-05-25 08:16:46 +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
161 lines
4.1 KiB
Go
161 lines
4.1 KiB
Go
package utxo
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/kaspanet/kaspad/domain/consensus/model"
|
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
|
"github.com/kaspanet/kaspad/domain/consensus/utils/consensushashing"
|
|
"github.com/kaspanet/kaspad/domain/consensus/utils/transactionhelper"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
type mutableUTXODiff struct {
|
|
toAdd utxoCollection
|
|
toRemove utxoCollection
|
|
|
|
immutableReferences []*immutableUTXODiff
|
|
}
|
|
|
|
// NewMutableUTXODiff creates an empty mutable UTXO-Diff
|
|
func NewMutableUTXODiff() model.MutableUTXODiff {
|
|
return newMutableUTXODiff()
|
|
}
|
|
|
|
func newMutableUTXODiff() *mutableUTXODiff {
|
|
return &mutableUTXODiff{
|
|
toAdd: utxoCollection{},
|
|
toRemove: utxoCollection{},
|
|
}
|
|
}
|
|
|
|
func (mud *mutableUTXODiff) ToImmutable() model.UTXODiff {
|
|
immutableReference := &immutableUTXODiff{
|
|
mutableUTXODiff: mud,
|
|
isInvalidated: false,
|
|
}
|
|
|
|
mud.immutableReferences = append(mud.immutableReferences, immutableReference)
|
|
|
|
return immutableReference
|
|
}
|
|
|
|
func (mud *mutableUTXODiff) invalidateImmutableReferences() {
|
|
for _, immutableReference := range mud.immutableReferences {
|
|
immutableReference.isInvalidated = true
|
|
}
|
|
|
|
mud.immutableReferences = nil
|
|
}
|
|
|
|
func (mud *mutableUTXODiff) WithDiff(other model.UTXODiff) (model.UTXODiff, error) {
|
|
o, ok := other.(*immutableUTXODiff)
|
|
if !ok {
|
|
return nil, errors.New("other is not of type *immutableUTXODiff")
|
|
}
|
|
|
|
result, err := withDiff(mud, o.mutableUTXODiff)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return result.ToImmutable(), nil
|
|
}
|
|
|
|
func (mud *mutableUTXODiff) WithDiffInPlace(other model.UTXODiff) error {
|
|
o, ok := other.(*immutableUTXODiff)
|
|
if !ok {
|
|
return errors.New("other is not of type *immutableUTXODiff")
|
|
}
|
|
|
|
mud.invalidateImmutableReferences()
|
|
|
|
return withDiffInPlace(mud, o.mutableUTXODiff)
|
|
}
|
|
|
|
func (mud *mutableUTXODiff) DiffFrom(other model.UTXODiff) (model.UTXODiff, error) {
|
|
o, ok := other.(*immutableUTXODiff)
|
|
if !ok {
|
|
return nil, errors.New("other is not of type *immutableUTXODiff")
|
|
}
|
|
|
|
result, err := diffFrom(mud, o.mutableUTXODiff)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return result.ToImmutable(), nil
|
|
}
|
|
|
|
func (mud *mutableUTXODiff) ToAdd() model.UTXOCollection {
|
|
return mud.toAdd
|
|
}
|
|
|
|
func (mud *mutableUTXODiff) ToRemove() model.UTXOCollection {
|
|
return mud.toRemove
|
|
}
|
|
func (mud *mutableUTXODiff) AddTransaction(transaction *externalapi.DomainTransaction, blockBlueScore uint64) error {
|
|
mud.invalidateImmutableReferences()
|
|
|
|
for _, input := range transaction.Inputs {
|
|
err := mud.removeEntry(&input.PreviousOutpoint, input.UTXOEntry)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
isCoinbase := transactionhelper.IsCoinBase(transaction)
|
|
transactionID := *consensushashing.TransactionID(transaction)
|
|
for i, output := range transaction.Outputs {
|
|
outpoint := &externalapi.DomainOutpoint{
|
|
TransactionID: transactionID,
|
|
Index: uint32(i),
|
|
}
|
|
entry := NewUTXOEntry(output.Value, output.ScriptPublicKey, isCoinbase, blockBlueScore)
|
|
|
|
err := mud.addEntry(outpoint, entry)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (mud *mutableUTXODiff) addEntry(outpoint *externalapi.DomainOutpoint, entry externalapi.UTXOEntry) error {
|
|
if mud.toRemove.containsWithBlueScore(outpoint, entry.BlockBlueScore()) {
|
|
mud.toRemove.remove(outpoint)
|
|
} else if mud.toAdd.Contains(outpoint) {
|
|
return errors.Errorf("AddEntry: Cannot add outpoint %s twice", outpoint)
|
|
} else {
|
|
mud.toAdd.add(outpoint, entry)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (mud *mutableUTXODiff) removeEntry(outpoint *externalapi.DomainOutpoint, entry externalapi.UTXOEntry) error {
|
|
if mud.toAdd.containsWithBlueScore(outpoint, entry.BlockBlueScore()) {
|
|
mud.toAdd.remove(outpoint)
|
|
} else if mud.toRemove.Contains(outpoint) {
|
|
return errors.Errorf("removeEntry: Cannot remove outpoint %s twice", outpoint)
|
|
} else {
|
|
mud.toRemove.add(outpoint, entry)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (mud *mutableUTXODiff) clone() *mutableUTXODiff {
|
|
if mud == nil {
|
|
return nil
|
|
}
|
|
|
|
return &mutableUTXODiff{
|
|
toAdd: mud.toAdd.Clone(),
|
|
toRemove: mud.toRemove.Clone(),
|
|
}
|
|
}
|
|
|
|
func (mud *mutableUTXODiff) String() string {
|
|
return fmt.Sprintf("toAdd: %s; toRemove: %s", mud.toAdd, mud.toRemove)
|
|
}
|