mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-11-27 07:48:44 +00:00
* Replace the old blockSubsidy parameters with the new ones. * Return subsidyGenesisReward if blockHash is the genesis hash. * Traverse a block's past for the subsidy calculation. * Partially implement SubsidyStore. * Refer to SubsidyStore from CoinbaseManager. * Wrap calcBlockSubsidy in getBlockSubsidy, which first checks the database. * Fix finalityStore not calling GenerateShardingID. * Implement calculateAveragePastSubsidy. * Implement calculateMergeSetSubsidySum. * Implement calculateSubsidyRandomVariable. * Implement calcBlockSubsidy. * Add a TODO about floats. * Update the calcBlockSubsidy TODO. * Use binary.LittleEndian in calculateSubsidyRandomVariable. * Fix bad range in calculateSubsidyRandomVariable. * Replace float64 with big.Rat everywhere except for subsidyRandomVariable. * Fix a nil dereference. * Use a random walk to approximate the normal distribution. * In order to avoid unsupported fractional results from powInt64, flip the numerator and the denominator manually. * Set standardDeviation to 0.25, MaxSompi to 10_000_000_000 * SompiPerKaspa and defaultSubsidyGenesisReward to 1_000. * Set the standard deviation to 0.2. * Use a binomial distribution instead of trying to estimate the normal distribution. * Change some values around. * Clamp the block subsidy. * Remove the fake duplicate constants in the util package. * Reduce MaxSompi to only 100m Kaspa to avoid hitting the uint64 ceiling. * Lower MaxSompi further to avoid new and exciting ways for the uint64 ceiling to be hit. * Remove debug logs. * Fix a couple of failing tests. * Fix TestBlockWindow. * Fix limitTransactionCount sometimes crashing on index-out-of-bounds. * In TrustedDataDataDAABlock, replace BlockHeader with DomainBlock * In calculateAveragePastSubsidy, use blockWindow instead of doing a BFS manually. * Remove the reference to DAGTopologyManager in coinbaseManager. * Add subsidy to the coinbase payload. * Get rid of the subsidy store and extract subsidies out of coinbase transactions. * Keep a blockWindow amount of blocks under the virtual for IBD purposes. * Manually remove the virtual genesis from the merge set. * Fix simnet genesis. * Fix TestPruning. * Fix TestCheckBlockIsNotPruned. * Fix TestBlockWindow. * Fix TestCalculateSignatureHashSchnorr. * Fix TestCalculateSignatureHashECDSA. * Fix serializing the wrong value into the coinbase payload. * Rename coinbaseOutputForBlueBlock to coinbaseOutputAndSubsidyForBlueBlock. * Add a TODO about optimizing trusted data DAA window blocks. * Expand on a comment in TestCheckBlockIsNotPruned. * In calcBlockSubsidy, divide the big.Int numerator by the big.Int denominator instead of converting to float64. * Clarify a comment. * Rename SubsidyMinGenesisReward to MinSubsidy. * Properly handle trusted data blocks in calculateMergeSetSubsidySum. * Use the first two bytes of the selected parent's hash for randomness instead of math/rand. * Restore maxSompi to what it used to be. * Fix TestPruning. * Fix TestAmountCreation. * Fix TestBlockWindow. * Fix TestAmountUnitConversions. * Increase the timeout in many-tips to 30 minutes. * Check coinbase subsidy for every block * Re-rename functions * Use shift instead of powInt64 to determine subsidyRandom Co-authored-by: Ori Newman <orinewman1@gmail.com>
182 lines
7.1 KiB
Go
182 lines
7.1 KiB
Go
package consensusstatemanager
|
|
|
|
import (
|
|
"sort"
|
|
|
|
"github.com/kaspanet/kaspad/domain/consensus/utils/transactionhelper"
|
|
|
|
"github.com/kaspanet/kaspad/domain/consensus/utils/consensushashing"
|
|
|
|
"github.com/kaspanet/kaspad/domain/consensus/utils/merkle"
|
|
|
|
"github.com/kaspanet/kaspad/domain/consensus/model"
|
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
|
"github.com/kaspanet/kaspad/domain/consensus/ruleerrors"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
func (csm *consensusStateManager) verifyUTXO(stagingArea *model.StagingArea, block *externalapi.DomainBlock,
|
|
blockHash *externalapi.DomainHash, pastUTXODiff externalapi.UTXODiff, acceptanceData externalapi.AcceptanceData,
|
|
multiset model.Multiset) error {
|
|
|
|
log.Debugf("verifyUTXO start for block %s", blockHash)
|
|
defer log.Debugf("verifyUTXO end for block %s", blockHash)
|
|
|
|
log.Debugf("Validating UTXO commitment for block %s", blockHash)
|
|
err := csm.validateUTXOCommitment(block, blockHash, multiset)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
log.Debugf("UTXO commitment validation passed for block %s", blockHash)
|
|
|
|
log.Debugf("Validating acceptedIDMerkleRoot for block %s", blockHash)
|
|
err = csm.validateAcceptedIDMerkleRoot(block, blockHash, acceptanceData)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
log.Debugf("AcceptedIDMerkleRoot validation passed for block %s", blockHash)
|
|
|
|
coinbaseTransaction := block.Transactions[0]
|
|
log.Debugf("Validating coinbase transaction %s for block %s",
|
|
consensushashing.TransactionID(coinbaseTransaction), blockHash)
|
|
err = csm.validateCoinbaseTransaction(stagingArea, blockHash, coinbaseTransaction)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
log.Debugf("Coinbase transaction validation passed for block %s", blockHash)
|
|
|
|
log.Debugf("Validating transactions against past UTXO for block %s", blockHash)
|
|
err = csm.validateBlockTransactionsAgainstPastUTXO(stagingArea, block, pastUTXODiff)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
log.Tracef("Transactions against past UTXO validation passed for block %s", blockHash)
|
|
|
|
return nil
|
|
}
|
|
|
|
func (csm *consensusStateManager) validateBlockTransactionsAgainstPastUTXO(stagingArea *model.StagingArea,
|
|
block *externalapi.DomainBlock, pastUTXODiff externalapi.UTXODiff) error {
|
|
|
|
blockHash := consensushashing.BlockHash(block)
|
|
log.Tracef("validateBlockTransactionsAgainstPastUTXO start for block %s", blockHash)
|
|
defer log.Tracef("validateBlockTransactionsAgainstPastUTXO end for block %s", blockHash)
|
|
|
|
selectedParentMedianTime, err := csm.pastMedianTimeManager.PastMedianTime(stagingArea, blockHash)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
log.Tracef("The past median time of %s is %d", blockHash, selectedParentMedianTime)
|
|
|
|
for i, transaction := range block.Transactions {
|
|
transactionID := consensushashing.TransactionID(transaction)
|
|
log.Tracef("Validating transaction %s in block %s against "+
|
|
"the block's past UTXO", transactionID, blockHash)
|
|
if i == transactionhelper.CoinbaseTransactionIndex {
|
|
log.Tracef("Skipping transaction %s because it is the coinbase", transactionID)
|
|
continue
|
|
}
|
|
|
|
log.Tracef("Populating transaction %s with UTXO entries", transactionID)
|
|
err = csm.populateTransactionWithUTXOEntriesFromVirtualOrDiff(stagingArea, transaction, pastUTXODiff)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
log.Tracef("Validating transaction %s and populating it with fee", transactionID)
|
|
err = csm.transactionValidator.ValidateTransactionInContextAndPopulateFee(
|
|
stagingArea, transaction, blockHash)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
log.Tracef("Validation against the block's past UTXO "+
|
|
"passed for transaction %s in block %s", transactionID, blockHash)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (csm *consensusStateManager) validateAcceptedIDMerkleRoot(block *externalapi.DomainBlock,
|
|
blockHash *externalapi.DomainHash, acceptanceData externalapi.AcceptanceData) error {
|
|
|
|
log.Tracef("validateAcceptedIDMerkleRoot start for block %s", blockHash)
|
|
defer log.Tracef("validateAcceptedIDMerkleRoot end for block %s", blockHash)
|
|
|
|
calculatedAcceptedIDMerkleRoot := calculateAcceptedIDMerkleRoot(acceptanceData)
|
|
if !block.Header.AcceptedIDMerkleRoot().Equal(calculatedAcceptedIDMerkleRoot) {
|
|
return errors.Wrapf(ruleerrors.ErrBadMerkleRoot, "block %s accepted ID merkle root is invalid - block "+
|
|
"header indicates %s, but calculated value is %s",
|
|
blockHash, block.Header.UTXOCommitment(), calculatedAcceptedIDMerkleRoot)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (csm *consensusStateManager) validateUTXOCommitment(
|
|
block *externalapi.DomainBlock, blockHash *externalapi.DomainHash, multiset model.Multiset) error {
|
|
|
|
log.Tracef("validateUTXOCommitment start for block %s", blockHash)
|
|
defer log.Tracef("validateUTXOCommitment end for block %s", blockHash)
|
|
|
|
multisetHash := multiset.Hash()
|
|
if !block.Header.UTXOCommitment().Equal(multisetHash) {
|
|
return errors.Wrapf(ruleerrors.ErrBadUTXOCommitment, "block %s UTXO commitment is invalid - block "+
|
|
"header indicates %s, but calculated value is %s", blockHash, block.Header.UTXOCommitment(), multisetHash)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func calculateAcceptedIDMerkleRoot(multiblockAcceptanceData externalapi.AcceptanceData) *externalapi.DomainHash {
|
|
log.Tracef("calculateAcceptedIDMerkleRoot start")
|
|
defer log.Tracef("calculateAcceptedIDMerkleRoot end")
|
|
|
|
var acceptedTransactions []*externalapi.DomainTransaction
|
|
|
|
for _, blockAcceptanceData := range multiblockAcceptanceData {
|
|
for _, transactionAcceptance := range blockAcceptanceData.TransactionAcceptanceData {
|
|
if !transactionAcceptance.IsAccepted {
|
|
continue
|
|
}
|
|
acceptedTransactions = append(acceptedTransactions, transactionAcceptance.Transaction)
|
|
}
|
|
}
|
|
sort.Slice(acceptedTransactions, func(i, j int) bool {
|
|
return consensushashing.TransactionID(acceptedTransactions[i]).Less(
|
|
consensushashing.TransactionID(acceptedTransactions[j]))
|
|
})
|
|
|
|
return merkle.CalculateIDMerkleRoot(acceptedTransactions)
|
|
}
|
|
|
|
func (csm *consensusStateManager) validateCoinbaseTransaction(stagingArea *model.StagingArea,
|
|
blockHash *externalapi.DomainHash, coinbaseTransaction *externalapi.DomainTransaction) error {
|
|
|
|
log.Tracef("validateCoinbaseTransaction start for block %s", blockHash)
|
|
defer log.Tracef("validateCoinbaseTransaction end for block %s", blockHash)
|
|
|
|
log.Tracef("Extracting coinbase data for coinbase transaction %s in block %s",
|
|
consensushashing.TransactionID(coinbaseTransaction), blockHash)
|
|
_, coinbaseData, _, err := csm.coinbaseManager.ExtractCoinbaseDataBlueScoreAndSubsidy(coinbaseTransaction)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
log.Tracef("Calculating the expected coinbase transaction for the given coinbase data and block %s", blockHash)
|
|
expectedCoinbaseTransaction, err :=
|
|
csm.coinbaseManager.ExpectedCoinbaseTransaction(stagingArea, blockHash, coinbaseData)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
coinbaseTransactionHash := consensushashing.TransactionHash(coinbaseTransaction)
|
|
expectedCoinbaseTransactionHash := consensushashing.TransactionHash(expectedCoinbaseTransaction)
|
|
log.Tracef("given coinbase hash: %s, expected coinbase hash: %s",
|
|
coinbaseTransactionHash, expectedCoinbaseTransactionHash)
|
|
|
|
if !coinbaseTransactionHash.Equal(expectedCoinbaseTransactionHash) {
|
|
return errors.Wrap(ruleerrors.ErrBadCoinbaseTransaction, "coinbase transaction is not built as expected")
|
|
}
|
|
|
|
return nil
|
|
}
|