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

* Revert "[NOD-1500] Delete integration tests" This reverts commit fcb57a206690a884fa6afb69d5d493282954a8bf. * [NOD-1518] hashserialization -> consenusserialization * [NOD-1518] Fix add genesis to virtual * [NOD-1518] Fix a bug in SerializeCoinbasePayload. * [NOD-1518] Fix a loop error and make pastMedianTime behave correctly everywhere on genesis. * [NOD-1518] Fix another bug and an infinite loop. * [NOD-1518] Fix uninitialized slice. * [NOD-1518] Fix bad should-commit checks and another infinite loop. * [NOD-1518] Fix nil serialization. * [NOD-1518] Rename blockHash to currentBlockHash. * [NOD-1518] Move the check whether stagedVirtualUTXOSet != nil to the top of commitVirtualUTXODiff. * [NOD-1518] Simplify utxoDiffStore.Commit. * [NOD-1518] Unextract resolveBlockStatusAndCheckFinality. * [NOD-1518] Move no-transactions logic into CalculateIDMerkleRoot. * [NOD-1518] Remove redundant is-staged check. * [NOD-1518] Fix merge errors. * [NOD-1518] Don't write anything if utxoDiffChild is nil. * [NOD-1518] Stage virtualAcceptanceData and virtualMultiset. * [NOD-1518] Fix bugs in getBlockTemplate and submitBlock. * [NOD-1518] Fix bad validation order in validateHeaderInContext. * [NOD-1518] Fix bug in Next(). * [NOD-1518] Fix nil dereference of subnetworks in AddressCache. * [NOD-1518] Fix multisetStore.Get returning a pointer to a multiset that is changed in place. * [NOD-1518] Break on genesis in countSubtrees. * [NOD-1518] Fix createBlockLocator. * [NOD-1518] Fix MsgTxToDomainTransaction. * [NOD-1518] Set MaxTxVersion to 1. * [NOD-1518] Fix missing error handling, bug in MsgTxToDomainTransaction, and bad subnetwork equality check. * [NOD-1518] Fix bug in hasUTXOByOutpointFromStagedVirtualUTXODiff. * [NOD-1518] Remove irrelevant comments. * [NOD-1518] Generate transactions with sufficient fee in tx_relay_test. * [NOD-1518] Fix broken RPC handlers. * [NOD-1518] Fix merge errors. * [NOD-1518] Fix bad exists check in restorePastUTXO and missing genesis check in CalculatePastUTXOAndAcceptanceData. * [NOD-1518] Add a comment. * [NOD-1518] Use a regular mutex instead of a read-write mutex in consensus to avoid dealing with sneaky not-actually-read functions. * [NOD-1518] Fix a deadlock in GetVirtualSelectedParent. * [NOD-1518] Fix missing handler registration for CmdHeader. * [NOD-1518] Fix processHeader calling OnNewBlock and LogBlock. Also fix conversion errors in IBDRootUTXOSetAndBlock. * [NOD-1518] Fix bad Command() in MsgIBDRootUTXOSetAndBlock. * [NOD-1518] Fix bad SyncStateMissingUTXOSet logic in resolveSyncState. * [NOD-1518] Rename mode to syncState. * [NOD-1518] Fix headers-only blocks coming in after the consensus thinks it's synced. * [NOD-1518] Fix selectedChildIterator.Next not ignoring virtual, infinite loop in HashSet.Length(). * [NOD-1518] Fix not-properly wrapped IBD blocks. * [NOD-1532] Add TestMultiset * [NOD-1518] Fix bad conversion in RequestIBDBlocks. * [NOD-1518] Fix bad string for CmdRequestHeaders. * [NOD-1518] Fix bad string for CmdDoneHeaders. * [NOD-1518] Fix bad Command() for MsgIBDRootNotFound. * [NOD-1532] Add TestPastUTXOMultiset * [NOD-1518] Fix bad areHeaderTipsSyncedMaxTimeDifference value. * [NOD-1532] Added TestDoubleSpends * [NOD-1518] Add missing string for CmdRequestIBDBlocks. * [NOD-1518] Fix bad check for SyncStateMissingBlockBodies. * [NOD-1518] Fix bad timeout durations in tests. * [NOD-1518] Fix IBD blocks not calling OnNewBlock. * [NOD-1518] Change when IBD finishes. * [NOD-1518] Properly clone utxoDiffChild. * [NOD-1532] Update hashes of blocks * [NOD-1532] Fix genesis blocks and a few more bugs * [NOD-1532] Bugfix: incorrect key passed to dbTx.Put * [NOD-1532] Make sure there's no nil payloads * [NOD-1532] Fix AddBlockToVirtual * [NOD-1532] Update tips and virtualDiffParents properly * [NOD-1532] Allow nil payload * [NOD-1532] Check for actual error and not just some RuleError * [NOD-1532] Get rid of SimpleCoinbaseData and make OpTrueScript P2SH * [NOD-1532] If coinbaseData is nil - fill in with generic coinbaseData Co-authored-by: Ori Newman <orinewman1@gmail.com> Co-authored-by: stasatdaglabs <stas@daglabs.com>
250 lines
7.3 KiB
Go
250 lines
7.3 KiB
Go
package consensusstatemanager
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"github.com/kaspanet/kaspad/domain/consensus/utils/constants"
|
|
"github.com/kaspanet/kaspad/domain/consensus/utils/multiset"
|
|
|
|
"github.com/kaspanet/kaspad/domain/consensus/model"
|
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
|
"github.com/kaspanet/kaspad/domain/consensus/processes/consensusstatemanager/utxoalgebra"
|
|
"github.com/kaspanet/kaspad/domain/consensus/ruleerrors"
|
|
"github.com/kaspanet/kaspad/domain/consensus/utils/transactionhelper"
|
|
)
|
|
|
|
func (csm *consensusStateManager) CalculatePastUTXOAndAcceptanceData(blockHash *externalapi.DomainHash) (
|
|
*model.UTXODiff, model.AcceptanceData, model.Multiset, error) {
|
|
|
|
// The genesis block has an empty UTXO diff, empty acceptance data, and a blank multiset
|
|
if *blockHash == *csm.genesisHash {
|
|
return &model.UTXODiff{}, model.AcceptanceData{}, multiset.New(), nil
|
|
}
|
|
|
|
blockGHOSTDAGData, err := csm.ghostdagDataStore.Get(csm.databaseContext, blockHash)
|
|
if err != nil {
|
|
return nil, nil, nil, err
|
|
}
|
|
selectedParentPastUTXO, err := csm.restorePastUTXO(blockGHOSTDAGData.SelectedParent)
|
|
if err != nil {
|
|
return nil, nil, nil, err
|
|
}
|
|
acceptanceData, utxoDiff, err := csm.applyBlueBlocks(blockHash, selectedParentPastUTXO, blockGHOSTDAGData)
|
|
if err != nil {
|
|
return nil, nil, nil, err
|
|
}
|
|
multiset, err := csm.calculateMultiset(acceptanceData, blockGHOSTDAGData)
|
|
if err != nil {
|
|
return nil, nil, nil, err
|
|
}
|
|
|
|
return utxoDiff, acceptanceData, multiset, nil
|
|
}
|
|
|
|
func (csm *consensusStateManager) restorePastUTXO(blockHash *externalapi.DomainHash) (*model.UTXODiff, error) {
|
|
var err error
|
|
|
|
// collect the UTXO diffs
|
|
var utxoDiffs []*model.UTXODiff
|
|
nextBlockHash := blockHash
|
|
for {
|
|
utxoDiff, err := csm.utxoDiffStore.UTXODiff(csm.databaseContext, nextBlockHash)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
utxoDiffs = append(utxoDiffs, utxoDiff)
|
|
|
|
exists, err := csm.utxoDiffStore.HasUTXODiffChild(csm.databaseContext, nextBlockHash)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !exists {
|
|
break
|
|
}
|
|
|
|
nextBlockHash, err = csm.utxoDiffStore.UTXODiffChild(csm.databaseContext, nextBlockHash)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if nextBlockHash == nil {
|
|
break
|
|
}
|
|
}
|
|
|
|
// apply the diffs in reverse order
|
|
accumulatedDiff := model.NewUTXODiff()
|
|
for i := len(utxoDiffs) - 1; i >= 0; i-- {
|
|
accumulatedDiff, err = utxoalgebra.WithDiff(accumulatedDiff, utxoDiffs[i])
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
return accumulatedDiff, nil
|
|
}
|
|
|
|
func (csm *consensusStateManager) applyBlueBlocks(blockHash *externalapi.DomainHash,
|
|
selectedParentPastUTXODiff *model.UTXODiff, ghostdagData *model.BlockGHOSTDAGData) (
|
|
model.AcceptanceData, *model.UTXODiff, error) {
|
|
|
|
blueBlocks, err := csm.blockStore.Blocks(csm.databaseContext, ghostdagData.MergeSetBlues)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
selectedParentMedianTime, err := csm.pastMedianTimeManager.PastMedianTime(blockHash)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
multiblockAcceptanceData := make(model.AcceptanceData, len(blueBlocks))
|
|
accumulatedUTXODiff := utxoalgebra.DiffClone(selectedParentPastUTXODiff)
|
|
accumulatedMass := uint64(0)
|
|
|
|
for i, blueBlock := range blueBlocks {
|
|
blockAcceptanceData := &model.BlockAcceptanceData{
|
|
TransactionAcceptanceData: make([]*model.TransactionAcceptanceData, len(blueBlock.Transactions)),
|
|
}
|
|
isSelectedParent := i == 0
|
|
|
|
for j, transaction := range blueBlock.Transactions {
|
|
var isAccepted bool
|
|
var fee uint64
|
|
|
|
isAccepted, accumulatedMass, err = csm.maybeAcceptTransaction(transaction, blockHash, isSelectedParent,
|
|
accumulatedUTXODiff, accumulatedMass, selectedParentMedianTime, ghostdagData.BlueScore)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
blockAcceptanceData.TransactionAcceptanceData[j] = &model.TransactionAcceptanceData{
|
|
Transaction: transaction,
|
|
Fee: fee,
|
|
IsAccepted: isAccepted,
|
|
}
|
|
}
|
|
multiblockAcceptanceData[i] = blockAcceptanceData
|
|
}
|
|
|
|
return multiblockAcceptanceData, accumulatedUTXODiff, nil
|
|
}
|
|
|
|
func (csm *consensusStateManager) maybeAcceptTransaction(transaction *externalapi.DomainTransaction,
|
|
blockHash *externalapi.DomainHash, isSelectedParent bool, accumulatedUTXODiff *model.UTXODiff,
|
|
accumulatedMassBefore uint64, selectedParentPastMedianTime int64, blockBlueScore uint64) (
|
|
isAccepted bool, accumulatedMassAfter uint64, err error) {
|
|
|
|
err = csm.populateTransactionWithUTXOEntriesFromVirtualOrDiff(transaction, accumulatedUTXODiff)
|
|
if err != nil {
|
|
return false, 0, err
|
|
}
|
|
|
|
// Coinbase transaction outputs are added to the UTXO-set only if they are in the selected parent chain.
|
|
if transactionhelper.IsCoinBase(transaction) {
|
|
if !isSelectedParent {
|
|
return false, accumulatedMassBefore, nil
|
|
}
|
|
|
|
err := utxoalgebra.DiffAddTransaction(accumulatedUTXODiff, transaction, blockBlueScore)
|
|
if err != nil {
|
|
return false, 0, err
|
|
}
|
|
|
|
return true, accumulatedMassBefore, nil
|
|
}
|
|
|
|
err = csm.transactionValidator.ValidateTransactionInContextAndPopulateMassAndFee(
|
|
transaction, blockHash, selectedParentPastMedianTime)
|
|
if err != nil {
|
|
if !errors.As(err, &(ruleerrors.RuleError{})) {
|
|
return false, 0, err
|
|
}
|
|
|
|
return false, accumulatedMassBefore, nil
|
|
}
|
|
|
|
isAccepted, accumulatedMassAfter = csm.checkTransactionMass(transaction, accumulatedMassBefore)
|
|
|
|
return isAccepted, accumulatedMassAfter, nil
|
|
}
|
|
|
|
func (csm *consensusStateManager) checkTransactionMass(
|
|
transaction *externalapi.DomainTransaction, accumulatedMassBefore uint64) (
|
|
isAccepted bool, accumulatedMassAfter uint64) {
|
|
|
|
accumulatedMassAfter = accumulatedMassBefore + transaction.Mass
|
|
|
|
// We could potentially overflow the accumulator so check for
|
|
// overflow as well.
|
|
if accumulatedMassAfter < transaction.Mass || accumulatedMassAfter > constants.MaxMassAcceptedByBlock {
|
|
return false, 0
|
|
}
|
|
|
|
return true, accumulatedMassAfter
|
|
}
|
|
|
|
func (csm *consensusStateManager) RestorePastUTXOSetIterator(blockHash *externalapi.DomainHash) (model.ReadOnlyUTXOSetIterator, error) {
|
|
diff, _, _, err := csm.CalculatePastUTXOAndAcceptanceData(blockHash)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
virtualUTXOSetIterator, err := csm.consensusStateStore.VirtualUTXOSetIterator(csm.databaseContext)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
pastUTXO := model.NewUTXODiff()
|
|
for virtualUTXOSetIterator.Next() {
|
|
outpoint, utxoEntry, err := virtualUTXOSetIterator.Get()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
pastUTXO.ToAdd[*outpoint] = utxoEntry
|
|
}
|
|
|
|
diff, err = utxoalgebra.WithDiff(pastUTXO, diff)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if len(diff.ToRemove) > 0 {
|
|
return nil, errors.New("diff.ToRemove is expected to be empty")
|
|
}
|
|
|
|
return newUTXOSetIterator(diff.ToAdd), nil
|
|
}
|
|
|
|
type utxoOutpointEntryPair struct {
|
|
outpoint externalapi.DomainOutpoint
|
|
entry *externalapi.UTXOEntry
|
|
}
|
|
|
|
type utxoSetIterator struct {
|
|
index int
|
|
pairs []utxoOutpointEntryPair
|
|
}
|
|
|
|
func newUTXOSetIterator(collection model.UTXOCollection) *utxoSetIterator {
|
|
pairs := make([]utxoOutpointEntryPair, len(collection))
|
|
i := 0
|
|
for outpoint, entry := range collection {
|
|
pairs[i] = utxoOutpointEntryPair{
|
|
outpoint: outpoint,
|
|
entry: entry,
|
|
}
|
|
i++
|
|
}
|
|
return &utxoSetIterator{index: 0, pairs: pairs}
|
|
}
|
|
|
|
func (u utxoSetIterator) Next() bool {
|
|
u.index++
|
|
return u.index < len(u.pairs)
|
|
}
|
|
|
|
func (u utxoSetIterator) Get() (outpoint *externalapi.DomainOutpoint, utxoEntry *externalapi.UTXOEntry, err error) {
|
|
pair := u.pairs[u.index]
|
|
return &pair.outpoint, pair.entry, nil
|
|
}
|