Bitcoin Core 31.99.0
P2P Digital Currency
tx_check.cpp
Go to the documentation of this file.
1// Copyright (c) 2017-present 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
6
7#include <consensus/amount.h>
11#include <script/script.h>
12#include <serialize.h>
13
14#include <set>
15#include <string>
16#include <utility>
17#include <vector>
18
20{
21 // Basic checks that don't depend on any context
22 if (tx.vin.empty())
23 return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-vin-empty");
24 if (tx.vout.empty())
25 return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-vout-empty");
26 // Size limits (this doesn't take the witness into account, as that hasn't been checked for malleability)
28 return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-oversize");
29 }
30
31 // Check for negative or overflow output values (see CVE-2010-5139)
32 CAmount nValueOut = 0;
33 for (const auto& txout : tx.vout)
34 {
35 if (txout.nValue < 0)
36 return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-vout-negative");
37 if (txout.nValue > MAX_MONEY)
38 return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-vout-toolarge");
39 nValueOut += txout.nValue;
40 if (!MoneyRange(nValueOut))
41 return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-txouttotal-toolarge");
42 }
43
44 // Check for duplicate inputs (see CVE-2018-17144)
45 // While Consensus::CheckTxInputs does check if all inputs of a tx are available, and UpdateCoins marks all inputs
46 // of a tx as spent, it does not check if the tx has duplicate inputs.
47 // Failure to run this check will result in either a crash or an inflation bug, depending on the implementation of
48 // the underlying coins database.
49 std::set<COutPoint> vInOutPoints;
50 for (const auto& txin : tx.vin) {
51 if (!vInOutPoints.insert(txin.prevout).second)
52 return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-inputs-duplicate");
53 }
54
55 if (tx.IsCoinBase())
56 {
57 if (tx.vin[0].scriptSig.size() < 2 || tx.vin[0].scriptSig.size() > 100)
58 return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-cb-length");
59 }
60 else
61 {
62 for (const auto& txin : tx.vin)
63 if (txin.prevout.IsNull())
64 return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-prevout-null");
65 }
66
67 return true;
68}
static constexpr CAmount MAX_MONEY
No amount larger than this (in satoshi) is valid.
Definition: amount.h:26
bool MoneyRange(const CAmount &nValue)
Definition: amount.h:27
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:281
const std::vector< CTxOut > vout
Definition: transaction.h:292
bool IsCoinBase() const
Definition: transaction.h:341
const std::vector< CTxIn > vin
Definition: transaction.h:291
bool Invalid(Result result, const std::string &reject_reason="", const std::string &debug_message="")
Definition: validation.h:96
@ TX_CONSENSUS
invalid by consensus rules
static const unsigned int MAX_BLOCK_WEIGHT
The maximum allowed weight for a block, see BIP 141 (network rule)
Definition: consensus.h:15
static const int WITNESS_SCALE_FACTOR
Definition: consensus.h:21
static constexpr TransactionSerParams TX_NO_WITNESS
Definition: transaction.h:181
uint64_t GetSerializeSize(const T &t)
Definition: serialize.h:1157
bool CheckTransaction(const CTransaction &tx, TxValidationState &state)
Definition: tx_check.cpp:19