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