mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-05-25 00:06:49 +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
90 lines
2.3 KiB
Go
90 lines
2.3 KiB
Go
package consensusstatemanager
|
|
|
|
import (
|
|
"github.com/kaspanet/kaspad/domain/consensus/model"
|
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
|
"github.com/kaspanet/kaspad/domain/consensus/utils/hashserialization"
|
|
)
|
|
|
|
func (csm *consensusStateManager) calculateMultiset(
|
|
acceptanceData model.AcceptanceData, blockGHOSTDAGData *model.BlockGHOSTDAGData) (model.Multiset, error) {
|
|
|
|
multiset, err := csm.multisetStore.Get(csm.databaseContext, blockGHOSTDAGData.SelectedParent)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
for _, blockAcceptanceData := range acceptanceData {
|
|
for i, transactionAcceptanceData := range blockAcceptanceData.TransactionAcceptanceData {
|
|
if !transactionAcceptanceData.IsAccepted {
|
|
continue
|
|
}
|
|
|
|
transaction := transactionAcceptanceData.Transaction
|
|
|
|
isCoinbase := i == 0
|
|
var err error
|
|
err = addTransactionToMultiset(multiset, transaction, blockGHOSTDAGData.BlueScore, isCoinbase)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
}
|
|
|
|
return multiset, nil
|
|
}
|
|
|
|
func addTransactionToMultiset(multiset model.Multiset, transaction *externalapi.DomainTransaction,
|
|
blockBlueScore uint64, isCoinbase bool) error {
|
|
|
|
for _, input := range transaction.Inputs {
|
|
err := removeUTXOFromMultiset(multiset, input.UTXOEntry, &input.PreviousOutpoint)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
for i, output := range transaction.Outputs {
|
|
outpoint := &externalapi.DomainOutpoint{
|
|
TransactionID: *hashserialization.TransactionID(transaction),
|
|
Index: uint32(i),
|
|
}
|
|
utxoEntry := &externalapi.UTXOEntry{
|
|
Amount: output.Value,
|
|
ScriptPublicKey: output.ScriptPublicKey,
|
|
BlockBlueScore: blockBlueScore,
|
|
IsCoinbase: isCoinbase,
|
|
}
|
|
err := addUTXOToMultiset(multiset, utxoEntry, outpoint)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func addUTXOToMultiset(multiset model.Multiset, entry *externalapi.UTXOEntry,
|
|
outpoint *externalapi.DomainOutpoint) error {
|
|
|
|
serializedUTXO, err := hashserialization.SerializeUTXO(entry, outpoint)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
multiset.Add(serializedUTXO)
|
|
|
|
return nil
|
|
}
|
|
|
|
func removeUTXOFromMultiset(multiset model.Multiset, entry *externalapi.UTXOEntry,
|
|
outpoint *externalapi.DomainOutpoint) error {
|
|
|
|
serializedUTXO, err := hashserialization.SerializeUTXO(entry, outpoint)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
multiset.Remove(serializedUTXO)
|
|
|
|
return nil
|
|
}
|