Bitcoin Core  0.20.99
P2P Digital Currency
tx_check.cpp
Go to the documentation of this file.
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 
8 #include <consensus/validation.h>
9 
11 {
12  // Basic checks that don't depend on any context
13  if (tx.vin.empty())
14  return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-vin-empty");
15  if (tx.vout.empty())
16  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)
19  return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-oversize");
20 
21  // Check for negative or overflow output values (see CVE-2010-5139)
22  CAmount nValueOut = 0;
23  for (const auto& txout : tx.vout)
24  {
25  if (txout.nValue < 0)
26  return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-vout-negative");
27  if (txout.nValue > MAX_MONEY)
28  return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-vout-toolarge");
29  nValueOut += txout.nValue;
30  if (!MoneyRange(nValueOut))
31  return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-txouttotal-toolarge");
32  }
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  std::set<COutPoint> vInOutPoints;
40  for (const auto& txin : tx.vin) {
41  if (!vInOutPoints.insert(txin.prevout).second)
42  return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-inputs-duplicate");
43  }
44 
45  if (tx.IsCoinBase())
46  {
47  if (tx.vin[0].scriptSig.size() < 2 || tx.vin[0].scriptSig.size() > 100)
48  return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-cb-length");
49  }
50  else
51  {
52  for (const auto& txin : tx.vin)
53  if (txin.prevout.IsNull())
54  return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-prevout-null");
55  }
56 
57  return true;
58 }
static const int SERIALIZE_TRANSACTION_NO_WITNESS
A flag that is ORed into the protocol version to designate that a transaction should be (un)serialize...
Definition: transaction.h:23
invalid by consensus rules
static const int WITNESS_SCALE_FACTOR
Definition: consensus.h:21
static const CAmount MAX_MONEY
No amount larger than this (in satoshi) is valid.
Definition: amount.h:25
bool MoneyRange(const CAmount &nValue)
Definition: amount.h:26
bool IsCoinBase() const
Definition: transaction.h:324
const std::vector< CTxIn > vin
Definition: transaction.h:276
size_t GetSerializeSize(const T &t, int nVersion=0)
Definition: serialize.h:1116
static const unsigned int MAX_BLOCK_WEIGHT
The maximum allowed weight for a block, see BIP 141 (network rule)
Definition: consensus.h:15
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
bool Invalid(Result result, const std::string &reject_reason="", const std::string &debug_message="")
Definition: validation.h:102
const std::vector< CTxOut > vout
Definition: transaction.h:277
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:12
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:259
bool CheckTransaction(const CTransaction &tx, TxValidationState &state)
Definition: tx_check.cpp:10