2019-12-31 18:35:41 +01:00
|
|
|
// Copyright (c) 2017-2019 The Bitcoin Core developers
|
2021-11-16 16:19:47 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#include <version.h>
|
|
|
|
|
|
|
|
#include <consensus/consensus.h>
|
|
|
|
#include <consensus/tx_check.h>
|
|
|
|
|
|
|
|
#include <primitives/transaction.h>
|
|
|
|
#include <consensus/validation.h>
|
|
|
|
|
2019-10-30 15:27:22 +01:00
|
|
|
bool CheckTransaction(const CTransaction& tx, TxValidationState& state)
|
2021-11-16 16:19:47 +01:00
|
|
|
{
|
2023-07-24 18:39:38 +02:00
|
|
|
bool allowEmptyTxIn = false;
|
|
|
|
bool allowEmptyTxOut = false;
|
2023-07-25 20:46:55 +02:00
|
|
|
if (tx.nType == TRANSACTION_QUORUM_COMMITMENT || tx.nType == TRANSACTION_MNHF_SIGNAL) {
|
2023-07-24 18:39:38 +02:00
|
|
|
allowEmptyTxIn = true;
|
|
|
|
allowEmptyTxOut = true;
|
|
|
|
}
|
|
|
|
if (tx.nType == TRANSACTION_ASSET_UNLOCK) {
|
|
|
|
allowEmptyTxIn = true;
|
2021-11-16 16:19:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Basic checks that don't depend on any context
|
2023-07-24 18:39:38 +02:00
|
|
|
if (!allowEmptyTxIn && tx.vin.empty())
|
2019-10-30 15:27:22 +01:00
|
|
|
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-vin-empty");
|
2023-07-24 18:39:38 +02:00
|
|
|
if (!allowEmptyTxOut && tx.vout.empty())
|
2019-10-30 15:27:22 +01:00
|
|
|
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-vout-empty");
|
2021-11-16 16:19:47 +01:00
|
|
|
// Size limits
|
2018-09-11 09:09:57 +02:00
|
|
|
if (::GetSerializeSize(tx, PROTOCOL_VERSION) > MAX_LEGACY_BLOCK_SIZE)
|
2019-10-30 15:27:22 +01:00
|
|
|
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-oversize");
|
2021-11-16 16:19:47 +01:00
|
|
|
if (tx.vExtraPayload.size() > MAX_TX_EXTRA_PAYLOAD)
|
2019-10-30 15:27:22 +01:00
|
|
|
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-payload-oversize");
|
2021-11-16 16:19:47 +01:00
|
|
|
|
2019-09-18 15:53:36 +02:00
|
|
|
// Check for negative or overflow output values (see CVE-2010-5139)
|
2021-11-16 16:19:47 +01:00
|
|
|
CAmount nValueOut = 0;
|
|
|
|
for (const auto& txout : tx.vout) {
|
|
|
|
if (txout.nValue < 0)
|
2019-10-30 15:27:22 +01:00
|
|
|
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-vout-negative");
|
2021-11-16 16:19:47 +01:00
|
|
|
if (txout.nValue > MAX_MONEY)
|
2019-10-30 15:27:22 +01:00
|
|
|
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-vout-toolarge");
|
2021-11-16 16:19:47 +01:00
|
|
|
nValueOut += txout.nValue;
|
|
|
|
if (!MoneyRange(nValueOut))
|
2019-10-30 15:27:22 +01:00
|
|
|
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-txouttotal-toolarge");
|
2021-11-16 16:19:47 +01:00
|
|
|
}
|
|
|
|
|
2019-10-25 13:46:41 +02:00
|
|
|
// Check for duplicate inputs (see CVE-2018-17144)
|
|
|
|
// While Consensus::CheckTxInputs does check if all inputs of a tx are available, and UpdateCoins marks all inputs
|
|
|
|
// of a tx as spent, it does not check if the tx has duplicate inputs.
|
|
|
|
// Failure to run this check will result in either a crash or an inflation bug, depending on the implementation of
|
|
|
|
// the underlying coins database.
|
2021-11-16 16:19:47 +01:00
|
|
|
std::set<COutPoint> vInOutPoints;
|
|
|
|
for (const auto& txin : tx.vin) {
|
|
|
|
if (!vInOutPoints.insert(txin.prevout).second)
|
2019-10-30 15:27:22 +01:00
|
|
|
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-inputs-duplicate");
|
2021-11-16 16:19:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tx.IsCoinBase()) {
|
|
|
|
size_t minCbSize = 2;
|
|
|
|
if (tx.nType == TRANSACTION_COINBASE) {
|
|
|
|
// With the introduction of CbTx, coinbase scripts are not required anymore to hold a valid block height
|
|
|
|
minCbSize = 1;
|
|
|
|
}
|
|
|
|
if (tx.vin[0].scriptSig.size() < minCbSize || tx.vin[0].scriptSig.size() > 100)
|
2019-10-30 15:27:22 +01:00
|
|
|
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-cb-length");
|
2021-11-16 16:19:47 +01:00
|
|
|
} else {
|
|
|
|
for (const auto& txin : tx.vin)
|
|
|
|
if (txin.prevout.IsNull())
|
2019-10-30 15:27:22 +01:00
|
|
|
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-prevout-null");
|
2021-11-16 16:19:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|