Branch data Line data Source code
# 1 : : // Copyright (c) 2017-2019 The Bitcoin Core developers # 2 : : // Distributed under the MIT software license, see the accompanying # 3 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php. # 4 : : # 5 : : #include <consensus/tx_check.h> # 6 : : # 7 : : #include <primitives/transaction.h> # 8 : : #include <consensus/validation.h> # 9 : : # 10 : : bool CheckTransaction(const CTransaction& tx, TxValidationState& state) # 11 : 259443 : { # 12 : : // Basic checks that don't depend on any context # 13 [ + + ]: 259443 : if (tx.vin.empty()) # 14 : 2 : return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-vin-empty"); # 15 [ + + ]: 259441 : if (tx.vout.empty()) # 16 : 5 : return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-vout-empty"); # 17 : : // Size limits (this doesn't take the witness into account, as that hasn't been checked for malleability) # 18 [ + + ]: 259436 : if (::GetSerializeSize(tx, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS) * WITNESS_SCALE_FACTOR > MAX_BLOCK_WEIGHT) # 19 : 1 : return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-oversize"); # 20 : : # 21 : : // Check for negative or overflow output values (see CVE-2010-5139) # 22 : 259435 : CAmount nValueOut = 0; # 23 [ + + ]: 259435 : for (const auto& txout : tx.vout) # 24 : 856766 : { # 25 [ + + ]: 856766 : if (txout.nValue < 0) # 26 : 5 : return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-vout-negative"); # 27 [ + + ]: 856761 : if (txout.nValue > MAX_MONEY) # 28 : 5 : return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-vout-toolarge"); # 29 : 856756 : nValueOut += txout.nValue; # 30 [ + + ]: 856756 : if (!MoneyRange(nValueOut)) # 31 : 5 : return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-txouttotal-toolarge"); # 32 : 856756 : } # 33 : : # 34 : : // Check for duplicate inputs (see CVE-2018-17144) # 35 : : // While Consensus::CheckTxInputs does check if all inputs of a tx are available, and UpdateCoins marks all inputs # 36 : : // of a tx as spent, it does not check if the tx has duplicate inputs. # 37 : : // Failure to run this check will result in either a crash or an inflation bug, depending on the implementation of # 38 : : // the underlying coins database. # 39 : 259435 : std::set<COutPoint> vInOutPoints; # 40 [ + + ]: 368610 : for (const auto& txin : tx.vin) { # 41 [ + + ]: 368610 : if (!vInOutPoints.insert(txin.prevout).second) # 42 : 256 : return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-inputs-duplicate"); # 43 : 368610 : } # 44 : : # 45 [ + + ]: 259420 : if (tx.IsCoinBase()) # 46 : 140013 : { # 47 [ + + ][ + + ]: 140013 : if (tx.vin[0].scriptSig.size() < 2 || tx.vin[0].scriptSig.size() > 100) # 48 : 6 : return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-cb-length"); # 49 : 119151 : } # 50 : 119151 : else # 51 : 119151 : { # 52 [ + + ]: 119151 : for (const auto& txin : tx.vin) # 53 [ + + ]: 228083 : if (txin.prevout.IsNull()) # 54 : 4 : return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-prevout-null"); # 55 : 119151 : } # 56 : : # 57 : 259164 : return true; # 58 : 259164 : }