mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-03-30 15:08:33 +00:00

* Add Close() function to all the iterators * Add defer iterator.Close() whenever we open an iterator * Add isClosed to all iterators and panic/return error if used after closing Co-authored-by: Svarog <feanorr@gmail.com>
316 lines
9.8 KiB
Go
316 lines
9.8 KiB
Go
package utxoindex
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
|
"github.com/kaspanet/kaspad/infrastructure/db/database"
|
|
"github.com/kaspanet/kaspad/infrastructure/logger"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
var utxoIndexBucket = database.MakeBucket([]byte("utxo-index"))
|
|
var virtualParentsKey = database.MakeBucket([]byte("")).Key([]byte("utxo-index-virtual-parents"))
|
|
|
|
type utxoIndexStore struct {
|
|
database database.Database
|
|
toAdd map[ScriptPublicKeyString]UTXOOutpointEntryPairs
|
|
toRemove map[ScriptPublicKeyString]UTXOOutpoints
|
|
virtualParents []*externalapi.DomainHash
|
|
}
|
|
|
|
func newUTXOIndexStore(database database.Database) *utxoIndexStore {
|
|
return &utxoIndexStore{
|
|
database: database,
|
|
toAdd: make(map[ScriptPublicKeyString]UTXOOutpointEntryPairs),
|
|
toRemove: make(map[ScriptPublicKeyString]UTXOOutpoints),
|
|
}
|
|
}
|
|
|
|
func (uis *utxoIndexStore) add(scriptPublicKey *externalapi.ScriptPublicKey, outpoint *externalapi.DomainOutpoint, utxoEntry externalapi.UTXOEntry) error {
|
|
|
|
key := ConvertScriptPublicKeyToString(scriptPublicKey)
|
|
log.Tracef("Adding outpoint %s:%d to scriptPublicKey %s",
|
|
outpoint.TransactionID, outpoint.Index, key)
|
|
|
|
// If the outpoint exists in `toRemove` simply remove it from there and return
|
|
if toRemoveOutpointsOfKey, ok := uis.toRemove[key]; ok {
|
|
if _, ok := toRemoveOutpointsOfKey[*outpoint]; ok {
|
|
log.Tracef("Outpoint %s:%d exists in `toRemove`. Deleting it from there",
|
|
outpoint.TransactionID, outpoint.Index)
|
|
delete(toRemoveOutpointsOfKey, *outpoint)
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// Create a UTXOOutpointEntryPairs entry in `toAdd` if it doesn't exist
|
|
if _, ok := uis.toAdd[key]; !ok {
|
|
log.Tracef("Creating key %s in `toAdd`", key)
|
|
uis.toAdd[key] = make(UTXOOutpointEntryPairs)
|
|
}
|
|
|
|
// Return an error if the outpoint already exists in `toAdd`
|
|
toAddPairsOfKey := uis.toAdd[key]
|
|
if _, ok := toAddPairsOfKey[*outpoint]; ok {
|
|
return errors.Errorf("cannot add outpoint %s because it's being added already", outpoint)
|
|
}
|
|
|
|
toAddPairsOfKey[*outpoint] = utxoEntry
|
|
|
|
log.Tracef("Added outpoint %s:%d to scriptPublicKey %s",
|
|
outpoint.TransactionID, outpoint.Index, key)
|
|
return nil
|
|
}
|
|
|
|
func (uis *utxoIndexStore) remove(scriptPublicKey *externalapi.ScriptPublicKey, outpoint *externalapi.DomainOutpoint) error {
|
|
key := ConvertScriptPublicKeyToString(scriptPublicKey)
|
|
log.Tracef("Removing outpoint %s:%d from scriptPublicKey %s",
|
|
outpoint.TransactionID, outpoint.Index, key)
|
|
|
|
// If the outpoint exists in `toAdd` simply remove it from there and return
|
|
if toAddPairsOfKey, ok := uis.toAdd[key]; ok {
|
|
if _, ok := toAddPairsOfKey[*outpoint]; ok {
|
|
log.Tracef("Outpoint %s:%d exists in `toAdd`. Deleting it from there",
|
|
outpoint.TransactionID, outpoint.Index)
|
|
delete(toAddPairsOfKey, *outpoint)
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// Create a UTXOOutpoints entry in `toRemove` if it doesn't exist
|
|
if _, ok := uis.toRemove[key]; !ok {
|
|
log.Tracef("Creating key %s in `toRemove`", key)
|
|
uis.toRemove[key] = make(UTXOOutpoints)
|
|
}
|
|
|
|
// Return an error if the outpoint already exists in `toRemove`
|
|
toRemoveOutpointsOfKey := uis.toRemove[key]
|
|
if _, ok := toRemoveOutpointsOfKey[*outpoint]; ok {
|
|
return errors.Errorf("cannot remove outpoint %s because it's being removed already", outpoint)
|
|
}
|
|
|
|
toRemoveOutpointsOfKey[*outpoint] = struct{}{}
|
|
|
|
log.Tracef("Removed outpoint %s:%d from scriptPublicKey %s",
|
|
outpoint.TransactionID, outpoint.Index, key)
|
|
return nil
|
|
}
|
|
|
|
func (uis *utxoIndexStore) updateVirtualParents(virtualParents []*externalapi.DomainHash) {
|
|
uis.virtualParents = virtualParents
|
|
}
|
|
|
|
func (uis *utxoIndexStore) discard() {
|
|
uis.toAdd = make(map[ScriptPublicKeyString]UTXOOutpointEntryPairs)
|
|
uis.toRemove = make(map[ScriptPublicKeyString]UTXOOutpoints)
|
|
uis.virtualParents = nil
|
|
}
|
|
|
|
func (uis *utxoIndexStore) commit() error {
|
|
onEnd := logger.LogAndMeasureExecutionTime(log, "utxoIndexStore.commit")
|
|
defer onEnd()
|
|
|
|
dbTransaction, err := uis.database.Begin()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer dbTransaction.RollbackUnlessClosed()
|
|
|
|
for scriptPublicKeyString, toRemoveOutpointsOfKey := range uis.toRemove {
|
|
scriptPublicKey := ConvertStringToScriptPublicKey(scriptPublicKeyString)
|
|
bucket := uis.bucketForScriptPublicKey(scriptPublicKey)
|
|
for outpointToRemove := range toRemoveOutpointsOfKey {
|
|
key, err := uis.convertOutpointToKey(bucket, &outpointToRemove)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = dbTransaction.Delete(key)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
|
|
for scriptPublicKeyString, toAddUTXOOutpointEntryPairs := range uis.toAdd {
|
|
scriptPublicKey := ConvertStringToScriptPublicKey(scriptPublicKeyString)
|
|
bucket := uis.bucketForScriptPublicKey(scriptPublicKey)
|
|
for outpointToAdd, utxoEntryToAdd := range toAddUTXOOutpointEntryPairs {
|
|
key, err := uis.convertOutpointToKey(bucket, &outpointToAdd)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
serializedUTXOEntry, err := serializeUTXOEntry(utxoEntryToAdd)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = dbTransaction.Put(key, serializedUTXOEntry)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
|
|
serializeParentHashes := serializeHashes(uis.virtualParents)
|
|
err = dbTransaction.Put(virtualParentsKey, serializeParentHashes)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = dbTransaction.Commit()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
uis.discard()
|
|
return nil
|
|
}
|
|
|
|
func (uis *utxoIndexStore) addAndCommitOutpointsWithoutTransaction(utxoPairs []*externalapi.OutpointAndUTXOEntryPair) error {
|
|
for _, pair := range utxoPairs {
|
|
bucket := uis.bucketForScriptPublicKey(pair.UTXOEntry.ScriptPublicKey())
|
|
key, err := uis.convertOutpointToKey(bucket, pair.Outpoint)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
serializedUTXOEntry, err := serializeUTXOEntry(pair.UTXOEntry)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = uis.database.Put(key, serializedUTXOEntry)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (uis *utxoIndexStore) updateAndCommitVirtualParentsWithoutTransaction(virtualParents []*externalapi.DomainHash) error {
|
|
serializeParentHashes := serializeHashes(virtualParents)
|
|
return uis.database.Put(virtualParentsKey, serializeParentHashes)
|
|
}
|
|
|
|
func (uis *utxoIndexStore) bucketForScriptPublicKey(scriptPublicKey *externalapi.ScriptPublicKey) *database.Bucket {
|
|
var scriptPublicKeyBytes = make([]byte, 2+len(scriptPublicKey.Script)) // uint16
|
|
binary.LittleEndian.PutUint16(scriptPublicKeyBytes[:2], scriptPublicKey.Version)
|
|
copy(scriptPublicKeyBytes[2:], scriptPublicKey.Script)
|
|
return utxoIndexBucket.Bucket(scriptPublicKeyBytes)
|
|
}
|
|
|
|
func (uis *utxoIndexStore) convertOutpointToKey(bucket *database.Bucket, outpoint *externalapi.DomainOutpoint) (*database.Key, error) {
|
|
serializedOutpoint, err := serializeOutpoint(outpoint)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return bucket.Key(serializedOutpoint), nil
|
|
}
|
|
|
|
func (uis *utxoIndexStore) convertKeyToOutpoint(key *database.Key) (*externalapi.DomainOutpoint, error) {
|
|
serializedOutpoint := key.Suffix()
|
|
return deserializeOutpoint(serializedOutpoint)
|
|
}
|
|
|
|
func (uis *utxoIndexStore) stagedData() (
|
|
toAdd map[ScriptPublicKeyString]UTXOOutpointEntryPairs,
|
|
toRemove map[ScriptPublicKeyString]UTXOOutpoints,
|
|
virtualParents []*externalapi.DomainHash) {
|
|
|
|
toAddClone := make(map[ScriptPublicKeyString]UTXOOutpointEntryPairs, len(uis.toAdd))
|
|
for scriptPublicKeyString, toAddUTXOOutpointEntryPairs := range uis.toAdd {
|
|
toAddUTXOOutpointEntryPairsClone := make(UTXOOutpointEntryPairs, len(toAddUTXOOutpointEntryPairs))
|
|
for outpoint, utxoEntry := range toAddUTXOOutpointEntryPairs {
|
|
toAddUTXOOutpointEntryPairsClone[outpoint] = utxoEntry
|
|
}
|
|
toAddClone[scriptPublicKeyString] = toAddUTXOOutpointEntryPairsClone
|
|
}
|
|
|
|
toRemoveClone := make(map[ScriptPublicKeyString]UTXOOutpoints, len(uis.toRemove))
|
|
for scriptPublicKeyString, toRemoveOutpoints := range uis.toRemove {
|
|
toRemoveOutpointsClone := make(UTXOOutpoints, len(toRemoveOutpoints))
|
|
for outpoint := range toRemoveOutpoints {
|
|
toRemoveOutpointsClone[outpoint] = struct{}{}
|
|
}
|
|
toRemoveClone[scriptPublicKeyString] = toRemoveOutpointsClone
|
|
}
|
|
|
|
return toAddClone, toRemoveClone, uis.virtualParents
|
|
}
|
|
|
|
func (uis *utxoIndexStore) isAnythingStaged() bool {
|
|
return len(uis.toAdd) > 0 || len(uis.toRemove) > 0
|
|
}
|
|
|
|
func (uis *utxoIndexStore) getUTXOOutpointEntryPairs(scriptPublicKey *externalapi.ScriptPublicKey) (UTXOOutpointEntryPairs, error) {
|
|
if uis.isAnythingStaged() {
|
|
return nil, errors.Errorf("cannot get utxo outpoint entry pairs while staging isn't empty")
|
|
}
|
|
|
|
bucket := uis.bucketForScriptPublicKey(scriptPublicKey)
|
|
cursor, err := uis.database.Cursor(bucket)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer cursor.Close()
|
|
utxoOutpointEntryPairs := make(UTXOOutpointEntryPairs)
|
|
for cursor.Next() {
|
|
key, err := cursor.Key()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
outpoint, err := uis.convertKeyToOutpoint(key)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
serializedUTXOEntry, err := cursor.Value()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
utxoEntry, err := deserializeUTXOEntry(serializedUTXOEntry)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
utxoOutpointEntryPairs[*outpoint] = utxoEntry
|
|
}
|
|
return utxoOutpointEntryPairs, nil
|
|
}
|
|
|
|
func (uis *utxoIndexStore) getVirtualParents() ([]*externalapi.DomainHash, error) {
|
|
if uis.isAnythingStaged() {
|
|
return nil, errors.Errorf("cannot get the virtual parents while staging isn't empty")
|
|
}
|
|
|
|
serializedHashes, err := uis.database.Get(virtualParentsKey)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return deserializeHashes(serializedHashes)
|
|
}
|
|
|
|
func (uis *utxoIndexStore) deleteAll() error {
|
|
// First we delete the virtual parents, so if anything goes wrong, the UTXO index will be marked as "not synced"
|
|
// and will be reset.
|
|
err := uis.database.Delete(virtualParentsKey)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
cursor, err := uis.database.Cursor(utxoIndexBucket)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer cursor.Close()
|
|
for cursor.Next() {
|
|
key, err := cursor.Key()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = uis.database.Delete(key)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|