Inside Bitcoin
Opening the selected file…
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) 2009-2010 Satoshi Nakamoto2// Copyright (c) 2009-present The Bitcoin Core developers3// Distributed under the MIT software license, see the accompanying4// file COPYING or http://www.opensource.org/licenses/mit-license.php.6#include <primitives/transaction.h>8#include <consensus/amount.h>9#include <crypto/hex_base.h>10#include <hash.h>11#include <primitives/transaction_identifier.h>12#include <script/script.h>13#include <serialize.h>14#include <tinyformat.h>16#include <algorithm>17#include <cassert>18#include <span>19#include <stdexcept>21std::string COutPoint::ToString() const22{23return strprintf("COutPoint(%s, %u)", hash.ToString().substr(0,10), n);24}26CTxIn::CTxIn(COutPoint prevoutIn, CScript scriptSigIn, uint32_t nSequenceIn)27{28prevout = prevoutIn;29scriptSig = scriptSigIn;30nSequence = nSequenceIn;31}33CTxIn::CTxIn(Txid hashPrevTx, uint32_t nOut, CScript scriptSigIn, uint32_t nSequenceIn)34{35prevout = COutPoint(hashPrevTx, nOut);36scriptSig = scriptSigIn;37nSequence = nSequenceIn;38}40std::string CTxIn::ToString() const41{42std::string str;43str += "CTxIn(";44str += prevout.ToString();45if (prevout.IsNull())46str += strprintf(", coinbase %s", HexStr(scriptSig));47else48str += strprintf(", scriptSig=%s", HexStr(scriptSig).substr(0, 24));49if (nSequence != SEQUENCE_FINAL)50str += strprintf(", nSequence=%u", nSequence);51str += ")";52return str;53}55CTxOut::CTxOut(const CAmount& nValueIn, CScript scriptPubKeyIn)56{57nValue = nValueIn;58scriptPubKey = scriptPubKeyIn;59}61std::string CTxOut::ToString() const62{63return strprintf("CTxOut(nValue=%d.%08d, scriptPubKey=%s)", nValue / COIN, nValue % COIN, HexStr(scriptPubKey).substr(0, 30));64}66CMutableTransaction::CMutableTransaction() : version{CTransaction::CURRENT_VERSION}, nLockTime{0} {}67CMutableTransaction::CMutableTransaction(const CTransaction& tx) : vin(tx.vin), vout(tx.vout), version{tx.version}, nLockTime{tx.nLockTime} {}69Txid CMutableTransaction::GetHash() const70{71return Txid::FromUint256((HashWriter{} << TX_NO_WITNESS(*this)).GetHash());72}74bool CTransaction::ComputeHasWitness() const75{76return std::any_of(vin.begin(), vin.end(), [](const auto& input) {77return !input.scriptWitness.IsNull();78});79}81Txid CTransaction::ComputeHash() const82{83return Txid::FromUint256((HashWriter{} << TX_NO_WITNESS(*this)).GetHash());84}86Wtxid CTransaction::ComputeWitnessHash() const87{88if (!HasWitness()) {89return Wtxid::FromUint256(hash.ToUint256());90}92return Wtxid::FromUint256((HashWriter{} << TX_WITH_WITNESS(*this)).GetHash());93}95CTransaction::CTransaction(const CMutableTransaction& tx) : vin(tx.vin), vout(tx.vout), version{tx.version}, nLockTime{tx.nLockTime}, m_has_witness{ComputeHasWitness()}, hash{ComputeHash()}, m_witness_hash{ComputeWitnessHash()} {}96CTransaction::CTransaction(CMutableTransaction&& tx) : vin(std::move(tx.vin)), vout(std::move(tx.vout)), version{tx.version}, nLockTime{tx.nLockTime}, m_has_witness{ComputeHasWitness()}, hash{ComputeHash()}, m_witness_hash{ComputeWitnessHash()} {}98CAmount CTransaction::GetValueOut() const99{100CAmount nValueOut = 0;101for (const auto& tx_out : vout) {102if (!MoneyRange(tx_out.nValue) || !MoneyRange(nValueOut + tx_out.nValue))103throw std::runtime_error(std::string(__func__) + ": value out of range");104nValueOut += tx_out.nValue;105}106assert(MoneyRange(nValueOut));107return nValueOut;108}110unsigned int CTransaction::ComputeTotalSize() const111{112return ::GetSerializeSize(TX_WITH_WITNESS(*this));113}115std::string CTransaction::ToString() const116{117std::string str;118str += strprintf("CTransaction(hash=%s, ver=%u, vin.size=%u, vout.size=%u, nLockTime=%u)\n",119GetHash().ToString().substr(0,10),120version,121vin.size(),122vout.size(),123nLockTime);124for (const auto& tx_in : vin)125str += " " + tx_in.ToString() + "\n";126for (const auto& tx_in : vin)127str += " " + tx_in.scriptWitness.ToString() + "\n";128for (const auto& tx_out : vout)129str += " " + tx_out.ToString() + "\n";130return str;131}