mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-05-24 15:56:42 +00:00

* [NOD-1420] Start working on ConsensusStateManager. Might be redundant due to recent changes * [NOD-1420] Convert model to externalapi in utxo_algerbra helpers * [NOD-1420] Add UTXO-diff algebra * [NOD-1420] Prepare skeleton of calculateAcceptanceDataAndMultiset * [NOD-1420] Added skeleton for AddBlockToVirtual * [NOD-1420] Implement PopulateTransactionWithUTXOEntries * [NOD-1420] Implement restorePastUTXO * [NOD-1420] Implement finality check * [NOD-1420] Move handling of tips to consensusStateManager * [NOD-1420] Implement calculateAcceptanceDataAndMultiset * [NOD-1420] Start implementing resolveBlockStatus * [NOD-1420] Implement resolveBlockStatus * [NOD-1420] Update related fields in end of resolveSingleBlockStatus * [NOD-1420] Start working on selectVirtualParents * [NOD-1420] Implemented BlockHeap * [NOD-1420] Implement selectVirtualParents * [NOD-1420] Implement updateVirtual * [NOD-1420] Added comments where they were missing * [NOD-1420] Place all consensusStateManager functions in correct files * [NOD-1420] Return the missing outpoints from populateTransactionWithUTXOEntriesFromVirtualOrDiff * [NOD-1420] Outpoint.ID -> TransactionID * [NOD-1420] Fix Stringer tests * [NOD-1420] Copy hash.FromString into utils * [NOD-1420] SetParents should return an error * [NOD-1420] Remove all reachabilityManager references from consensusStateManager * [NOD-1420] Remove VirtualData. Get the info from the stores where needed * [NOD-1420] Invert parameters to IsAncestorOf * [NOD-1420] Use model.AcceptanceData * [NOD-1420] Don't return accumulatedMassBefore in error cases * [NOD-1420] Don't expect store functions to return nil when the requested data was found - instead add HasXXX functions * [NOD-1420] addTransactionToMultiset sets isCoinbase properly * [NOD-1420] expected hash string length is externalapi.DomainHashSize * 2 * [NOD-1420] Rename reachabilityTree -> reachabilityManager + updateReindexRoot if isNextVirtualSelectedParent * [NOD-1420] ValidateCoinbaseTransaction in csm.verifyAndBuildUTXO * [NOD-1420] Re-write HAsUTXODiffChild * [NOD-1420] delete past_utxo.go.bak * [NOD-1420] Implement validateCoinbaseTransaction in CSM * [NOD-1420] Imlemented missing functionality in ValidateTransactionAndPopulateWithConsensusData * [NOD-1420] Moved merge depth logic to MergeDepthManager * [NOD-1420] Add logs
184 lines
5.6 KiB
Go
184 lines
5.6 KiB
Go
package utxodiffstore
|
|
|
|
import (
|
|
"github.com/golang/protobuf/proto"
|
|
"github.com/kaspanet/kaspad/domain/consensus/database/serialization"
|
|
"github.com/kaspanet/kaspad/domain/consensus/model"
|
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
|
"github.com/kaspanet/kaspad/domain/consensus/utils/dbkeys"
|
|
)
|
|
|
|
var utxoDiffBucket = dbkeys.MakeBucket([]byte("utxo-diffs"))
|
|
var utxoDiffChildBucket = dbkeys.MakeBucket([]byte("utxo-diff-children"))
|
|
|
|
// utxoDiffStore represents a store of UTXODiffs
|
|
type utxoDiffStore struct {
|
|
utxoDiffStaging map[externalapi.DomainHash]*model.UTXODiff
|
|
utxoDiffChildStaging map[externalapi.DomainHash]*externalapi.DomainHash
|
|
toDelete map[externalapi.DomainHash]struct{}
|
|
}
|
|
|
|
// New instantiates a new UTXODiffStore
|
|
func New() model.UTXODiffStore {
|
|
return &utxoDiffStore{
|
|
utxoDiffStaging: make(map[externalapi.DomainHash]*model.UTXODiff),
|
|
utxoDiffChildStaging: make(map[externalapi.DomainHash]*externalapi.DomainHash),
|
|
toDelete: make(map[externalapi.DomainHash]struct{}),
|
|
}
|
|
}
|
|
|
|
// Stage stages the given utxoDiff for the given blockHash
|
|
func (uds *utxoDiffStore) Stage(blockHash *externalapi.DomainHash, utxoDiff *model.UTXODiff, utxoDiffChild *externalapi.DomainHash) {
|
|
uds.utxoDiffStaging[*blockHash] = utxoDiff
|
|
uds.utxoDiffChildStaging[*blockHash] = utxoDiffChild
|
|
}
|
|
|
|
func (uds *utxoDiffStore) IsStaged() bool {
|
|
return len(uds.utxoDiffStaging) != 0 || len(uds.utxoDiffChildStaging) != 0 || len(uds.toDelete) != 0
|
|
}
|
|
|
|
func (uds *utxoDiffStore) IsBlockHashStaged(blockHash *externalapi.DomainHash) bool {
|
|
if _, ok := uds.utxoDiffStaging[*blockHash]; ok {
|
|
return true
|
|
}
|
|
_, ok := uds.utxoDiffChildStaging[*blockHash]
|
|
return ok
|
|
}
|
|
|
|
func (uds *utxoDiffStore) Discard() {
|
|
uds.utxoDiffStaging = make(map[externalapi.DomainHash]*model.UTXODiff)
|
|
uds.utxoDiffChildStaging = make(map[externalapi.DomainHash]*externalapi.DomainHash)
|
|
uds.toDelete = make(map[externalapi.DomainHash]struct{})
|
|
}
|
|
|
|
func (uds *utxoDiffStore) Commit(dbTx model.DBTransaction) error {
|
|
for hash, utxoDiff := range uds.utxoDiffStaging {
|
|
utxoDiffBytes, err := uds.serializeUTXODiff(utxoDiff)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = dbTx.Put(uds.utxoDiffHashAsKey(&hash), utxoDiffBytes)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
for hash, utxoDiffChild := range uds.utxoDiffChildStaging {
|
|
utxoDiffChildBytes, err := uds.serializeUTXODiffChild(utxoDiffChild)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = dbTx.Put(uds.utxoDiffHashAsKey(&hash), utxoDiffChildBytes)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
for hash := range uds.toDelete {
|
|
err := dbTx.Delete(uds.utxoDiffHashAsKey(&hash))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = dbTx.Delete(uds.utxoDiffChildHashAsKey(&hash))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
uds.Discard()
|
|
return nil
|
|
}
|
|
|
|
// UTXODiff gets the utxoDiff associated with the given blockHash
|
|
func (uds *utxoDiffStore) UTXODiff(dbContext model.DBReader, blockHash *externalapi.DomainHash) (*model.UTXODiff, error) {
|
|
if utxoDiff, ok := uds.utxoDiffStaging[*blockHash]; ok {
|
|
return utxoDiff, nil
|
|
}
|
|
|
|
utxoDiffBytes, err := dbContext.Get(uds.utxoDiffHashAsKey(blockHash))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return uds.deserializeUTXODiff(utxoDiffBytes)
|
|
}
|
|
|
|
// UTXODiffChild gets the utxoDiff child associated with the given blockHash
|
|
func (uds *utxoDiffStore) UTXODiffChild(dbContext model.DBReader, blockHash *externalapi.DomainHash) (*externalapi.DomainHash, error) {
|
|
if utxoDiffChild, ok := uds.utxoDiffChildStaging[*blockHash]; ok {
|
|
return utxoDiffChild, nil
|
|
}
|
|
|
|
utxoDiffChildBytes, err := dbContext.Get(uds.utxoDiffChildHashAsKey(blockHash))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
utxoDiffChild, err := uds.deserializeUTXODiffChild(utxoDiffChildBytes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return utxoDiffChild, nil
|
|
}
|
|
|
|
// HasUTXODiffChild returns true if the given blockHash has a UTXODiffChild
|
|
func (uds *utxoDiffStore) HasUTXODiffChild(dbContext model.DBReader, blockHash *externalapi.DomainHash) (bool, error) {
|
|
if _, ok := uds.utxoDiffChildStaging[*blockHash]; ok {
|
|
return true, nil
|
|
}
|
|
|
|
return dbContext.Has(uds.utxoDiffChildHashAsKey(blockHash))
|
|
}
|
|
|
|
// Delete deletes the utxoDiff associated with the given blockHash
|
|
func (uds *utxoDiffStore) Delete(blockHash *externalapi.DomainHash) {
|
|
if uds.IsBlockHashStaged(blockHash) {
|
|
if _, ok := uds.utxoDiffStaging[*blockHash]; ok {
|
|
delete(uds.utxoDiffStaging, *blockHash)
|
|
}
|
|
if _, ok := uds.utxoDiffChildStaging[*blockHash]; ok {
|
|
delete(uds.utxoDiffChildStaging, *blockHash)
|
|
}
|
|
return
|
|
}
|
|
uds.toDelete[*blockHash] = struct{}{}
|
|
}
|
|
|
|
func (uds *utxoDiffStore) utxoDiffHashAsKey(hash *externalapi.DomainHash) model.DBKey {
|
|
return utxoDiffBucket.Key(hash[:])
|
|
}
|
|
|
|
func (uds *utxoDiffStore) utxoDiffChildHashAsKey(hash *externalapi.DomainHash) model.DBKey {
|
|
return utxoDiffChildBucket.Key(hash[:])
|
|
}
|
|
|
|
func (uds *utxoDiffStore) serializeUTXODiff(utxoDiff *model.UTXODiff) ([]byte, error) {
|
|
return proto.Marshal(serialization.UTXODiffToDBUTXODiff(utxoDiff))
|
|
}
|
|
|
|
func (uds *utxoDiffStore) deserializeUTXODiff(utxoDiffBytes []byte) (*model.UTXODiff, error) {
|
|
dbUTXODiff := &serialization.DbUtxoDiff{}
|
|
err := proto.Unmarshal(utxoDiffBytes, dbUTXODiff)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return serialization.DBUTXODiffToUTXODiff(dbUTXODiff)
|
|
}
|
|
|
|
func (uds *utxoDiffStore) serializeUTXODiffChild(utxoDiffChild *externalapi.DomainHash) ([]byte, error) {
|
|
return proto.Marshal(serialization.DomainHashToDbHash(utxoDiffChild))
|
|
}
|
|
|
|
func (uds *utxoDiffStore) deserializeUTXODiffChild(utxoDiffChildBytes []byte) (*externalapi.DomainHash, error) {
|
|
dbHash := &serialization.DbHash{}
|
|
err := proto.Unmarshal(utxoDiffChildBytes, dbHash)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return serialization.DbHashToDomainHash(dbHash)
|
|
}
|