mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-05-29 18:26:41 +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>
188 lines
6.4 KiB
Go
188 lines
6.4 KiB
Go
package transactionvalidator
|
|
|
|
import (
|
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
|
"github.com/kaspanet/kaspad/domain/consensus/ruleerrors"
|
|
"github.com/kaspanet/kaspad/domain/consensus/utils/constants"
|
|
"github.com/kaspanet/kaspad/domain/consensus/utils/hashes"
|
|
"github.com/kaspanet/kaspad/domain/consensus/utils/subnetworks"
|
|
"github.com/kaspanet/kaspad/domain/consensus/utils/transactionhelper"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
func (v *transactionValidator) ValidateTransactionInIsolation(tx *externalapi.DomainTransaction) error {
|
|
err := v.checkTransactionInputCount(tx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = v.checkTransactionAmountRanges(tx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = v.checkDuplicateTransactionInputs(tx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = v.checkCoinbaseLength(tx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = v.checkTransactionPayloadHash(tx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = v.checkGasInBuiltInOrNativeTransactions(tx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = v.checkSubnetworkRegistryTransaction(tx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = v.checkNativeTransactionPayload(tx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// TODO: fill it with the right subnetwork id.
|
|
err = v.checkTransactionSubnetwork(tx, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (v *transactionValidator) checkTransactionInputCount(tx *externalapi.DomainTransaction) error {
|
|
// A non-coinbase transaction must have at least one input.
|
|
if !transactionhelper.IsCoinBase(tx) && len(tx.Inputs) == 0 {
|
|
return errors.Wrapf(ruleerrors.ErrNoTxInputs, "transaction has no inputs")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (v *transactionValidator) checkTransactionAmountRanges(tx *externalapi.DomainTransaction) error {
|
|
// Ensure the transaction amounts are in range. Each transaction
|
|
// output must not be negative or more than the max allowed per
|
|
// transaction. Also, the total of all outputs must abide by the same
|
|
// restrictions. All amounts in a transaction are in a unit value known
|
|
// as a sompi. One kaspa is a quantity of sompi as defined by the
|
|
// sompiPerKaspa constant.
|
|
var totalSompi uint64
|
|
for _, txOut := range tx.Outputs {
|
|
sompi := txOut.Value
|
|
if sompi > constants.MaxSompi {
|
|
return errors.Wrapf(ruleerrors.ErrBadTxOutValue, "transaction output value of %d is "+
|
|
"higher than max allowed value of %d", sompi, constants.MaxSompi)
|
|
}
|
|
|
|
// Binary arithmetic guarantees that any overflow is detected and reported.
|
|
// This is impossible for Kaspa, but perhaps possible if an alt increases
|
|
// the total money supply.
|
|
newTotalSompi := totalSompi + sompi
|
|
if newTotalSompi < totalSompi {
|
|
return errors.Wrapf(ruleerrors.ErrBadTxOutValue, "total value of all transaction "+
|
|
"outputs exceeds max allowed value of %d",
|
|
constants.MaxSompi)
|
|
}
|
|
totalSompi = newTotalSompi
|
|
if totalSompi > constants.MaxSompi {
|
|
return errors.Wrapf(ruleerrors.ErrBadTxOutValue, "total value of all transaction "+
|
|
"outputs is %d which is higher than max "+
|
|
"allowed value of %d", totalSompi,
|
|
constants.MaxSompi)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (v *transactionValidator) checkDuplicateTransactionInputs(tx *externalapi.DomainTransaction) error {
|
|
existingTxOut := make(map[externalapi.DomainOutpoint]struct{})
|
|
for _, txIn := range tx.Inputs {
|
|
if _, exists := existingTxOut[txIn.PreviousOutpoint]; exists {
|
|
return errors.Wrapf(ruleerrors.ErrDuplicateTxInputs, "transaction "+
|
|
"contains duplicate inputs")
|
|
}
|
|
existingTxOut[txIn.PreviousOutpoint] = struct{}{}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (v *transactionValidator) checkCoinbaseLength(tx *externalapi.DomainTransaction) error {
|
|
if !transactionhelper.IsCoinBase(tx) {
|
|
return nil
|
|
}
|
|
|
|
// Coinbase payload length must not exceed the max length.
|
|
payloadLen := len(tx.Payload)
|
|
if payloadLen > constants.MaxCoinbasePayloadLength {
|
|
return errors.Wrapf(ruleerrors.ErrBadCoinbasePayloadLen, "coinbase transaction payload length "+
|
|
"of %d is out of range (max: %d)",
|
|
payloadLen, constants.MaxCoinbasePayloadLength)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (v *transactionValidator) checkTransactionPayloadHash(tx *externalapi.DomainTransaction) error {
|
|
if tx.SubnetworkID != subnetworks.SubnetworkIDNative {
|
|
payloadHash := hashes.HashData(tx.Payload)
|
|
if tx.PayloadHash != *payloadHash {
|
|
return errors.Wrapf(ruleerrors.ErrInvalidPayloadHash, "invalid payload hash")
|
|
}
|
|
} else if tx.PayloadHash != (externalapi.DomainHash{}) {
|
|
return errors.Wrapf(ruleerrors.ErrInvalidPayloadHash, "unexpected non-empty payload hash in native subnetwork")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (v *transactionValidator) checkGasInBuiltInOrNativeTransactions(tx *externalapi.DomainTransaction) error {
|
|
// Transactions in native, registry and coinbase subnetworks must have Gas = 0
|
|
if subnetworks.IsBuiltInOrNative(tx.SubnetworkID) && tx.Gas > 0 {
|
|
return errors.Wrapf(ruleerrors.ErrInvalidGas, "transaction in the native or "+
|
|
"registry subnetworks has gas > 0 ")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (v *transactionValidator) checkSubnetworkRegistryTransaction(tx *externalapi.DomainTransaction) error {
|
|
if tx.SubnetworkID != subnetworks.SubnetworkIDRegistry {
|
|
return nil
|
|
}
|
|
|
|
if len(tx.Payload) != 8 {
|
|
return errors.Wrapf(ruleerrors.ErrSubnetworkRegistry, "validation failed: subnetwork registry "+
|
|
"tx has an invalid payload")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (v *transactionValidator) checkNativeTransactionPayload(tx *externalapi.DomainTransaction) error {
|
|
if tx.SubnetworkID == subnetworks.SubnetworkIDNative && len(tx.Payload) > 0 {
|
|
return errors.Wrapf(ruleerrors.ErrInvalidPayload, "transaction in the native subnetwork "+
|
|
"includes a payload")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (v *transactionValidator) checkTransactionSubnetwork(tx *externalapi.DomainTransaction,
|
|
subnetworkID *externalapi.DomainSubnetworkID) error {
|
|
if !v.enableNonNativeSubnetworks && tx.SubnetworkID != subnetworks.SubnetworkIDNative &&
|
|
tx.SubnetworkID != subnetworks.SubnetworkIDCoinbase {
|
|
return errors.Wrapf(ruleerrors.ErrSubnetworksDisabled, "transaction has non native or coinbase "+
|
|
"subnetwork ID")
|
|
}
|
|
|
|
// If we are a partial node, only transactions on built in subnetworks
|
|
// or our own subnetwork may have a payload
|
|
isLocalNodeFull := subnetworkID == nil
|
|
shouldTxBeFull := subnetworks.IsBuiltIn(tx.SubnetworkID) || subnetworks.IsEqual(&tx.SubnetworkID, subnetworkID)
|
|
if !isLocalNodeFull && !shouldTxBeFull && len(tx.Payload) > 0 {
|
|
return errors.Wrapf(ruleerrors.ErrInvalidPayload,
|
|
"transaction that was expected to be partial has a payload "+
|
|
"with length > 0")
|
|
}
|
|
return nil
|
|
}
|