Coverage Report

Created: 2026-04-29 19:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/tmp/bitcoin/src/primitives/transaction.cpp
Line
Count
Source
1
// Copyright (c) 2009-2010 Satoshi Nakamoto
2
// Copyright (c) 2009-present The Bitcoin Core developers
3
// Distributed under the MIT software license, see the accompanying
4
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6
#include <primitives/transaction.h>
7
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>
15
16
#include <algorithm>
17
#include <cassert>
18
#include <span>
19
#include <stdexcept>
20
21
std::string COutPoint::ToString() const
22
4.04k
{
23
4.04k
    return strprintf("COutPoint(%s, %u)", hash.ToString().substr(0,10), n);
24
4.04k
}
25
26
CTxIn::CTxIn(COutPoint prevoutIn, CScript scriptSigIn, uint32_t nSequenceIn)
27
18.9k
{
28
18.9k
    prevout = prevoutIn;
29
18.9k
    scriptSig = scriptSigIn;
30
18.9k
    nSequence = nSequenceIn;
31
18.9k
}
32
33
CTxIn::CTxIn(Txid hashPrevTx, uint32_t nOut, CScript scriptSigIn, uint32_t nSequenceIn)
34
3.58k
{
35
3.58k
    prevout = COutPoint(hashPrevTx, nOut);
36
3.58k
    scriptSig = scriptSigIn;
37
3.58k
    nSequence = nSequenceIn;
38
3.58k
}
39
40
std::string CTxIn::ToString() const
41
3.93k
{
42
3.93k
    std::string str;
43
3.93k
    str += "CTxIn(";
44
3.93k
    str += prevout.ToString();
45
3.93k
    if (prevout.IsNull())
46
0
        str += strprintf(", coinbase %s", HexStr(scriptSig));
47
3.93k
    else
48
3.93k
        str += strprintf(", scriptSig=%s", HexStr(scriptSig).substr(0, 24));
49
3.93k
    if (nSequence != SEQUENCE_FINAL)
50
3.92k
        str += strprintf(", nSequence=%u", nSequence);
51
3.93k
    str += ")";
52
3.93k
    return str;
53
3.93k
}
54
55
CTxOut::CTxOut(const CAmount& nValueIn, CScript scriptPubKeyIn)
56
189k
{
57
189k
    nValue = nValueIn;
58
189k
    scriptPubKey = scriptPubKeyIn;
59
189k
}
60
61
std::string CTxOut::ToString() const
62
9.62k
{
63
9.62k
    return strprintf("CTxOut(nValue=%d.%08d, scriptPubKey=%s)", nValue / COIN, nValue % COIN, HexStr(scriptPubKey).substr(0, 30));
64
9.62k
}
65
66
998k
CMutableTransaction::CMutableTransaction() : version{CTransaction::CURRENT_VERSION}, nLockTime{0} {}
67
166k
CMutableTransaction::CMutableTransaction(const CTransaction& tx) : vin(tx.vin), vout(tx.vout), version{tx.version}, nLockTime{tx.nLockTime} {}
68
69
Txid CMutableTransaction::GetHash() const
70
684k
{
71
684k
    return Txid::FromUint256((HashWriter{} << TX_NO_WITNESS(*this)).GetHash());
72
684k
}
73
74
bool CTransaction::ComputeHasWitness() const
75
1.07M
{
76
1.07M
    return std::any_of(vin.begin(), vin.end(), [](const auto& input) {
77
1.02M
        return !input.scriptWitness.IsNull();
78
1.02M
    });
79
1.07M
}
80
81
Txid CTransaction::ComputeHash() const
82
1.07M
{
83
1.07M
    return Txid::FromUint256((HashWriter{} << TX_NO_WITNESS(*this)).GetHash());
84
1.07M
}
85
86
Wtxid CTransaction::ComputeWitnessHash() const
87
1.07M
{
88
1.07M
    if (!HasWitness()) {
89
714k
        return Wtxid::FromUint256(hash.ToUint256());
90
714k
    }
91
92
362k
    return Wtxid::FromUint256((HashWriter{} << TX_WITH_WITNESS(*this)).GetHash());
93
1.07M
}
94
95
314k
CTransaction::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()} {}
96
762k
CTransaction::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()} {}
97
98
CAmount CTransaction::GetValueOut() const
99
10.9M
{
100
10.9M
    CAmount nValueOut = 0;
101
21.7M
    for (const auto& tx_out : vout) {
102
21.7M
        if (!MoneyRange(tx_out.nValue) || !MoneyRange(nValueOut + tx_out.nValue))
103
1
            throw std::runtime_error(std::string(__func__) + ": value out of range");
104
21.7M
        nValueOut += tx_out.nValue;
105
21.7M
    }
106
10.9M
    assert(MoneyRange(nValueOut));
107
10.9M
    return nValueOut;
108
10.9M
}
109
110
unsigned int CTransaction::ComputeTotalSize() const
111
11.4k
{
112
11.4k
    return ::GetSerializeSize(TX_WITH_WITNESS(*this));
113
11.4k
}
114
115
std::string CTransaction::ToString() const
116
1.53k
{
117
1.53k
    std::string str;
118
1.53k
    str += strprintf("CTransaction(hash=%s, ver=%u, vin.size=%u, vout.size=%u, nLockTime=%u)\n",
119
1.53k
        GetHash().ToString().substr(0,10),
120
1.53k
        version,
121
1.53k
        vin.size(),
122
1.53k
        vout.size(),
123
1.53k
        nLockTime);
124
1.53k
    for (const auto& tx_in : vin)
125
3.93k
        str += "    " + tx_in.ToString() + "\n";
126
1.53k
    for (const auto& tx_in : vin)
127
3.93k
        str += "    " + tx_in.scriptWitness.ToString() + "\n";
128
1.53k
    for (const auto& tx_out : vout)
129
9.62k
        str += "    " + tx_out.ToString() + "\n";
130
1.53k
    return str;
131
1.53k
}