mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-09-13 21:10:12 +00:00

* Add StagingArea struct * Implemented staging areas in blockStore * Move blockStagingShard to separate folder * Apply staging shard to acceptanceDataStore * Update blockHeaderStore with StagingArea * Add StagingArea to BlockRelationStore * Add StagingArea to blockStatusStore * Add StagingArea to consensusStateStore * Add StagingArea to daaBlocksStore * Add StagingArea to finalityStore * Add StagingArea to ghostdagDataStore * Add StagingArea to headersSelectedChainStore and headersSelectedTipStore * Add StagingArea to multisetStore * Add StagingArea to pruningStore * Add StagingArea to reachabilityDataStore * Add StagingArea to utxoDiffStore * Fix forgotten compilation error * Update reachability manager and some more things with StagingArea * Add StagingArea to dagTopologyManager, and some more * Add StagingArea to GHOSTDAGManager, and some more * Add StagingArea to difficultyManager, and some more * Add StagingArea to dagTraversalManager, and some more * Add StagingArea to headerTipsManager, and some more * Add StagingArea to constnsusStateManager, pastMedianTimeManager * Add StagingArea to transactionValidator * Add StagingArea to finalityManager * Add StagingArea to mergeDepthManager * Add StagingArea to pruningManager * Add StagingArea to rest of ValidateAndInsertBlock * Add StagingArea to blockValidator * Add StagingArea to coinbaseManager * Add StagingArea to syncManager * Add StagingArea to blockBuilder * Update consensus with StagingArea * Add StagingArea to ghostdag2 * Fix remaining compilation errors * Update names of stagingShards * Fix forgotten stagingArea passing * Mark stagingShard.isCommited = true once commited * Move isStaged to stagingShard, so that it's available without going through store * Make blockHeaderStore count be avilable from stagingShard * Fix remaining forgotten stagingArea passing * commitAllChanges should call dbTx.Commit in the end * Fix all tests tests in blockValidator * Fix all tests in consensusStateManager and some more * Fix all tests in pruningManager * Add many missing stagingAreas in tests * Fix many tests * Fix most of all other tests * Fix ghostdag_test.go * Add comment to StagingArea * Make list of StagingShards an array * Add comment to StagingShardID * Make sure all staging shards are pointer-receiver * Undo bucket rename in block_store * Typo: isCommited -> isCommitted * Add comment explaining why stagingArea.shards is an array
108 lines
3.6 KiB
Go
108 lines
3.6 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/consensushashing"
|
|
"github.com/kaspanet/kaspad/domain/consensus/utils/multiset"
|
|
"github.com/kaspanet/kaspad/domain/consensus/utils/utxo"
|
|
)
|
|
|
|
func (csm *consensusStateManager) calculateMultiset(stagingArea *model.StagingArea,
|
|
acceptanceData externalapi.AcceptanceData, blockGHOSTDAGData *model.BlockGHOSTDAGData, daaScore uint64) (model.Multiset, error) {
|
|
|
|
log.Debugf("calculateMultiset start for block with selected parent %s", blockGHOSTDAGData.SelectedParent())
|
|
defer log.Debugf("calculateMultiset end for block with selected parent %s", blockGHOSTDAGData.SelectedParent())
|
|
|
|
if blockGHOSTDAGData.SelectedParent() == nil {
|
|
log.Debugf("Selected parent is nil, which could only happen for the genesis. " +
|
|
"The genesis, by definition, has an empty multiset")
|
|
return multiset.New(), nil
|
|
}
|
|
|
|
ms, err := csm.multisetStore.Get(csm.databaseContext, stagingArea, blockGHOSTDAGData.SelectedParent())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
log.Debugf("The multiset for the selected parent %s is: %s", blockGHOSTDAGData.SelectedParent(), ms.Hash())
|
|
|
|
for _, blockAcceptanceData := range acceptanceData {
|
|
for i, transactionAcceptanceData := range blockAcceptanceData.TransactionAcceptanceData {
|
|
transaction := transactionAcceptanceData.Transaction
|
|
transactionID := consensushashing.TransactionID(transaction)
|
|
if !transactionAcceptanceData.IsAccepted {
|
|
log.Tracef("Skipping transaction %s because it was not accepted", transactionID)
|
|
continue
|
|
}
|
|
|
|
isCoinbase := i == 0
|
|
log.Tracef("Is transaction %s a coinbase transaction: %t", transactionID, isCoinbase)
|
|
|
|
err := addTransactionToMultiset(ms, transaction, daaScore, isCoinbase)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
log.Tracef("Added transaction %s to the multiset", transactionID)
|
|
}
|
|
}
|
|
|
|
return ms, nil
|
|
}
|
|
|
|
func addTransactionToMultiset(multiset model.Multiset, transaction *externalapi.DomainTransaction,
|
|
blockDAAScore uint64, isCoinbase bool) error {
|
|
|
|
transactionID := consensushashing.TransactionID(transaction)
|
|
log.Tracef("addTransactionToMultiset start for transaction %s", transactionID)
|
|
defer log.Tracef("addTransactionToMultiset end for transaction %s", transactionID)
|
|
|
|
for _, input := range transaction.Inputs {
|
|
log.Tracef("Removing input %s at index %d from the multiset",
|
|
input.PreviousOutpoint.TransactionID, input.PreviousOutpoint.Index)
|
|
err := removeUTXOFromMultiset(multiset, input.UTXOEntry, &input.PreviousOutpoint)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
for i, output := range transaction.Outputs {
|
|
outpoint := &externalapi.DomainOutpoint{
|
|
TransactionID: *transactionID,
|
|
Index: uint32(i),
|
|
}
|
|
utxoEntry := utxo.NewUTXOEntry(output.Value, output.ScriptPublicKey, isCoinbase, blockDAAScore)
|
|
|
|
log.Tracef("Adding input %s at index %d from the multiset", transactionID, i)
|
|
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 := utxo.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 := utxo.SerializeUTXO(entry, outpoint)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
multiset.Remove(serializedUTXO)
|
|
|
|
return nil
|
|
}
|