Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2017-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#include <consensus/tx_check.h>7#include <consensus/amount.h>8#include <consensus/consensus.h>9#include <consensus/validation.h>10#include <primitives/transaction.h>11#include <script/script.h>12#include <serialize.h>14#include <set>15#include <string>16#include <utility>17#include <vector>19bool CheckTransaction(const CTransaction& tx, TxValidationState& state)20{21// Basic checks that don't depend on any context22if (tx.vin.empty())23return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-vin-empty");24if (tx.vout.empty())25return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-vout-empty");26// Size limits (this doesn't take the witness into account, as that hasn't been checked for malleability)27if (::GetSerializeSize(TX_NO_WITNESS(tx)) * WITNESS_SCALE_FACTOR > MAX_BLOCK_WEIGHT) {28return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-oversize");29}31// Check for negative or overflow output values (see CVE-2010-5139)32CAmount nValueOut = 0;33for (const auto& txout : tx.vout)34{35if (txout.nValue < 0)36return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-vout-negative");37if (txout.nValue > MAX_MONEY)38return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-vout-toolarge");39nValueOut += txout.nValue;40if (!MoneyRange(nValueOut))41return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-txouttotal-toolarge");42}44// Check for duplicate inputs (see CVE-2018-17144)45// While Consensus::CheckTxInputs does check if all inputs of a tx are available, and UpdateCoins marks all inputs46// of a tx as spent, it does not check if the tx has duplicate inputs.47// Failure to run this check will result in either a crash or an inflation bug, depending on the implementation of48// the underlying coins database.49std::set<COutPoint> vInOutPoints;50for (const auto& txin : tx.vin) {51if (!vInOutPoints.insert(txin.prevout).second)52return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-inputs-duplicate");53}55if (tx.IsCoinBase())56{57if (tx.vin[0].scriptSig.size() < 2 || tx.vin[0].scriptSig.size() > 100)58return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-cb-length");59}60else61{62for (const auto& txin : tx.vin)63if (txin.prevout.IsNull())64return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-prevout-null");65}67return true;68}