Bitcoin ABC  0.24.7
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/consensus.h>
7 #include <consensus/validation.h>
9 #include <version.h>
10 
11 #include <unordered_set>
12 
13 static bool CheckTransactionCommon(const CTransaction &tx,
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 
55 bool CheckCoinbase(const CTransaction &tx, TxValidationState &state) {
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 }
GetSerializeSize
size_t GetSerializeSize(const T &t, int nVersion=0)
Definition: serialize.h:1182
CTransaction::vin
const std::vector< CTxIn > vin
Definition: transaction.h:210
MoneyRange
bool MoneyRange(const Amount nValue)
Definition: amount.h:176
transaction.h
validation.h
CheckRegularTransaction
bool CheckRegularTransaction(const CTransaction &tx, TxValidationState &state)
Context-independent validity checks for coinbase and non-coinbase transactions.
Definition: tx_check.cpp:74
MAX_COINBASE_SCRIPTSIG_SIZE
static const int MAX_COINBASE_SCRIPTSIG_SIZE
Coinbase scripts have their own script size limit.
Definition: consensus.h:34
version.h
CTransaction
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:194
Amount::zero
static constexpr Amount zero()
Definition: amount.h:42
TxValidationState
Definition: validation.h:137
CTransaction::IsCoinBase
bool IsCoinBase() const
Definition: transaction.h:256
MAX_TX_SIZE
static const uint64_t MAX_TX_SIZE
The maximum allowed size for a transaction, in bytes.
Definition: consensus.h:14
CTransaction::vout
const std::vector< CTxOut > vout
Definition: transaction.h:211
consensus.h
Amount
Definition: amount.h:19
ValidationState::Invalid
bool Invalid(Result result, const std::string &reject_reason="", const std::string &debug_message="")
Definition: validation.h:100
coins.h
TxValidationResult::TX_CONSENSUS
@ TX_CONSENSUS
invalid by consensus rules
CheckTransactionCommon
static bool CheckTransactionCommon(const CTransaction &tx, TxValidationState &state)
Definition: tx_check.cpp:13
MAX_MONEY
static const Amount MAX_MONEY
No amount larger than this (in satoshi) is valid.
Definition: amount.h:175
CheckCoinbase
bool CheckCoinbase(const CTransaction &tx, TxValidationState &state)
Definition: tx_check.cpp:55
PROTOCOL_VERSION
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:11