mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-09-14 13:30:11 +00:00

* 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>
83 lines
2.3 KiB
Go
83 lines
2.3 KiB
Go
package consensusstatestore
|
|
|
|
import (
|
|
"github.com/kaspanet/kaspad/domain/consensus/database"
|
|
"github.com/kaspanet/kaspad/domain/consensus/model"
|
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
var importingPruningPointUTXOSetKey = database.MakeBucket(nil).Key([]byte("importing-pruning-point-utxo-set"))
|
|
|
|
func (css *consensusStateStore) StartImportingPruningPointUTXOSet(dbContext model.DBWriter) error {
|
|
return dbContext.Put(importingPruningPointUTXOSetKey, []byte{0})
|
|
}
|
|
|
|
func (css *consensusStateStore) HadStartedImportingPruningPointUTXOSet(dbContext model.DBWriter) (bool, error) {
|
|
return dbContext.Has(importingPruningPointUTXOSetKey)
|
|
}
|
|
|
|
func (css *consensusStateStore) FinishImportingPruningPointUTXOSet(dbContext model.DBWriter) error {
|
|
return dbContext.Delete(importingPruningPointUTXOSetKey)
|
|
}
|
|
|
|
func (css *consensusStateStore) ImportPruningPointUTXOSetIntoVirtualUTXOSet(dbContext model.DBWriter,
|
|
pruningPointUTXOSetIterator externalapi.ReadOnlyUTXOSetIterator) error {
|
|
|
|
if css.virtualUTXODiffStaging != nil {
|
|
return errors.New("cannot import virtual UTXO set while virtual UTXO diff is staged")
|
|
}
|
|
|
|
hadStartedImportingPruningPointUTXOSet, err := css.HadStartedImportingPruningPointUTXOSet(dbContext)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !hadStartedImportingPruningPointUTXOSet {
|
|
return errors.New("cannot import pruning point UTXO set " +
|
|
"without calling StartImportingPruningPointUTXOSet first")
|
|
}
|
|
|
|
// Clear the cache
|
|
css.virtualUTXOSetCache.Clear()
|
|
|
|
// Delete all the old UTXOs from the database
|
|
deleteCursor, err := dbContext.Cursor(utxoSetBucket)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for ok := deleteCursor.First(); ok; ok = deleteCursor.Next() {
|
|
key, err := deleteCursor.Key()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = dbContext.Delete(key)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
// Insert all the new UTXOs into the database
|
|
for ok := pruningPointUTXOSetIterator.First(); ok; ok = pruningPointUTXOSetIterator.Next() {
|
|
outpoint, entry, err := pruningPointUTXOSetIterator.Get()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
key, err := utxoKey(outpoint)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
serializedUTXOEntry, err := serializeUTXOEntry(entry)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = dbContext.Put(key, serializedUTXOEntry)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|