Bitcoin ABC  0.28.12
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>
7 #include <consensus/consensus.h>
8 #include <consensus/validation.h>
10 #include <version.h>
11 
12 #include <unordered_set>
13 
14 static bool CheckTransactionCommon(const CTransaction &tx,
15  TxValidationState &state) {
16  // Basic checks that don't depend on any context
17  if (tx.vin.empty()) {
19  "bad-txns-vin-empty");
20  }
21 
22  if (tx.vout.empty()) {
24  "bad-txns-vout-empty");
25  }
26 
27  // Size limit
30  "bad-txns-oversize");
31  }
32 
33  // Check for negative or overflow output values (see CVE-2010-5139)
34  Amount nValueOut = Amount::zero();
35  for (const auto &txout : tx.vout) {
36  if (txout.nValue < Amount::zero()) {
38  "bad-txns-vout-negative");
39  }
40 
41  if (txout.nValue > MAX_MONEY) {
43  "bad-txns-vout-toolarge");
44  }
45 
46  nValueOut += txout.nValue;
47  if (!MoneyRange(nValueOut)) {
49  "bad-txns-txouttotal-toolarge");
50  }
51  }
52 
53  return true;
54 }
55 
56 bool CheckCoinbase(const CTransaction &tx, TxValidationState &state) {
57  if (!tx.IsCoinBase()) {
58  return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-cb-missing",
59  "first tx is not coinbase");
60  }
61 
62  if (!CheckTransactionCommon(tx, state)) {
63  // CheckTransactionCommon fill in the state.
64  return false;
65  }
66 
67  if (tx.vin[0].scriptSig.size() < 2 ||
68  tx.vin[0].scriptSig.size() > MAX_COINBASE_SCRIPTSIG_SIZE) {
69  return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-cb-length");
70  }
71 
72  return true;
73 }
74 
76  if (tx.IsCoinBase()) {
78  "bad-tx-coinbase");
79  }
80 
81  if (!CheckTransactionCommon(tx, state)) {
82  // CheckTransactionCommon fill in the state.
83  return false;
84  }
85 
86  std::unordered_set<COutPoint, SaltedOutpointHasher> vInOutPoints;
87  for (const auto &txin : tx.vin) {
88  if (txin.prevout.IsNull()) {
90  "bad-txns-prevout-null");
91  }
92 
93  // Check for duplicate inputs (see CVE-2018-17144)
94  // While Consensus::CheckTxInputs does check if all inputs of a tx are
95  // available, and UpdateCoins marks all inputs of a tx as spent, it does
96  // not check if the tx has duplicate inputs. Failure to run this check
97  // will result in either a crash or an inflation bug, depending on the
98  // implementation of the underlying coins database.
99  if (!vInOutPoints.insert(txin.prevout).second) {
101  "bad-txns-inputs-duplicate");
102  }
103  }
104 
105  return true;
106 }
bool MoneyRange(const Amount nValue)
Definition: amount.h:166
static constexpr Amount MAX_MONEY
No amount larger than this (in satoshi) is valid.
Definition: amount.h:165
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:94
@ 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, int nVersion=0)
Definition: serialize.h:1276
Definition: amount.h:19
static constexpr Amount zero() noexcept
Definition: amount.h:32
static bool CheckTransactionCommon(const CTransaction &tx, TxValidationState &state)
Definition: tx_check.cpp:14
bool CheckRegularTransaction(const CTransaction &tx, TxValidationState &state)
Context-independent validity checks for coinbase and non-coinbase transactions.
Definition: tx_check.cpp:75
bool CheckCoinbase(const CTransaction &tx, TxValidationState &state)
Definition: tx_check.cpp:56
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:11