Bitcoin ABC 0.32.6
P2P Digital Currency
tx_check.cpp
Go to the documentation of this file.
1// Copyright (c) 2017-2018 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 <coins.h>
6#include <consensus/amount.h>
10
11#include <unordered_set>
12
14 TxValidationState &state) {
15 // Basic checks that don't depend on any context
16 if (tx.vin.empty()) {
18 "bad-txns-vin-empty");
19 }
20
21 if (tx.vout.empty()) {
23 "bad-txns-vout-empty");
24 }
25
26 // Size limit
29 "bad-txns-oversize");
30 }
31
32 // Check for negative or overflow output values (see CVE-2010-5139)
33 Amount nValueOut = Amount::zero();
34 for (const auto &txout : tx.vout) {
35 if (txout.nValue < Amount::zero()) {
37 "bad-txns-vout-negative");
38 }
39
40 if (txout.nValue > MAX_MONEY) {
42 "bad-txns-vout-toolarge");
43 }
44
45 nValueOut += txout.nValue;
46 if (!MoneyRange(nValueOut)) {
48 "bad-txns-txouttotal-toolarge");
49 }
50 }
51
52 return true;
53}
54
56 if (!tx.IsCoinBase()) {
57 return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-cb-missing",
58 "first tx is not coinbase");
59 }
60
61 if (!CheckTransactionCommon(tx, state)) {
62 // CheckTransactionCommon fill in the state.
63 return false;
64 }
65
66 if (tx.vin[0].scriptSig.size() < 2 ||
67 tx.vin[0].scriptSig.size() > MAX_COINBASE_SCRIPTSIG_SIZE) {
68 return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-cb-length");
69 }
70
71 return true;
72}
73
75 if (tx.IsCoinBase()) {
77 "bad-tx-coinbase");
78 }
79
80 if (!CheckTransactionCommon(tx, state)) {
81 // CheckTransactionCommon fill in the state.
82 return false;
83 }
84
85 std::unordered_set<COutPoint, SaltedOutpointHasher> vInOutPoints;
86 for (const auto &txin : tx.vin) {
87 if (txin.prevout.IsNull()) {
89 "bad-txns-prevout-null");
90 }
91
92 // Check for duplicate inputs (see CVE-2018-17144)
93 // While Consensus::CheckTxInputs does check if all inputs of a tx are
94 // available, and UpdateCoins marks all inputs of a tx as spent, it does
95 // not check if the tx has duplicate inputs. Failure to run this check
96 // will result in either a crash or an inflation bug, depending on the
97 // implementation of the underlying coins database.
98 if (!vInOutPoints.insert(txin.prevout).second) {
100 "bad-txns-inputs-duplicate");
101 }
102 }
103
104 return true;
105}
bool MoneyRange(const Amount nValue)
Definition: amount.h:171
static constexpr Amount MAX_MONEY
No amount larger than this (in satoshi) is valid.
Definition: amount.h:170
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:192
const std::vector< CTxOut > vout
Definition: transaction.h:207
bool IsCoinBase() const
Definition: transaction.h:252
const std::vector< CTxIn > vin
Definition: transaction.h:206
bool Invalid(Result result, const std::string &reject_reason="", const std::string &debug_message="")
Definition: validation.h:101
@ TX_CONSENSUS
invalid by consensus rules
static const int MAX_COINBASE_SCRIPTSIG_SIZE
Coinbase scripts have their own script size limit.
Definition: consensus.h:34
static const uint64_t MAX_TX_SIZE
The maximum allowed size for a transaction, in bytes.
Definition: consensus.h:14
size_t GetSerializeSize(const T &t)
Definition: serialize.h:1262
Definition: amount.h:21
static constexpr Amount zero() noexcept
Definition: amount.h:34
static bool CheckTransactionCommon(const CTransaction &tx, TxValidationState &state)
Definition: tx_check.cpp:13
bool CheckRegularTransaction(const CTransaction &tx, TxValidationState &state)
Context-independent validity checks for coinbase and non-coinbase transactions.
Definition: tx_check.cpp:74
bool CheckCoinbase(const CTransaction &tx, TxValidationState &state)
Definition: tx_check.cpp:55