/Users/eugenesiegel/btc/bitcoin/src/primitives/transaction.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) 2009-2010 Satoshi Nakamoto |
2 | | // Copyright (c) 2009-2022 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 <script/script.h> |
12 | | #include <serialize.h> |
13 | | #include <tinyformat.h> |
14 | | #include <uint256.h> |
15 | | #include <util/transaction_identifier.h> |
16 | | |
17 | | #include <algorithm> |
18 | | #include <cassert> |
19 | | #include <stdexcept> |
20 | | |
21 | | std::string COutPoint::ToString() const |
22 | 0 | { |
23 | 0 | return strprintf("COutPoint(%s, %u)", hash.ToString().substr(0,10), n); Line | Count | Source | 1172 | 0 | #define strprintf tfm::format |
|
24 | 0 | } |
25 | | |
26 | | CTxIn::CTxIn(COutPoint prevoutIn, CScript scriptSigIn, uint32_t nSequenceIn) |
27 | 0 | { |
28 | 0 | prevout = prevoutIn; |
29 | 0 | scriptSig = scriptSigIn; |
30 | 0 | nSequence = nSequenceIn; |
31 | 0 | } |
32 | | |
33 | | CTxIn::CTxIn(Txid hashPrevTx, uint32_t nOut, CScript scriptSigIn, uint32_t nSequenceIn) |
34 | 0 | { |
35 | 0 | prevout = COutPoint(hashPrevTx, nOut); |
36 | 0 | scriptSig = scriptSigIn; |
37 | 0 | nSequence = nSequenceIn; |
38 | 0 | } |
39 | | |
40 | | std::string CTxIn::ToString() const |
41 | 0 | { |
42 | 0 | std::string str; |
43 | 0 | str += "CTxIn("; |
44 | 0 | str += prevout.ToString(); |
45 | 0 | if (prevout.IsNull()) |
46 | 0 | str += strprintf(", coinbase %s", HexStr(scriptSig)); Line | Count | Source | 1172 | 0 | #define strprintf tfm::format |
|
47 | 0 | else |
48 | 0 | str += strprintf(", scriptSig=%s", HexStr(scriptSig).substr(0, 24)); Line | Count | Source | 1172 | 0 | #define strprintf tfm::format |
|
49 | 0 | if (nSequence != SEQUENCE_FINAL) |
50 | 0 | str += strprintf(", nSequence=%u", nSequence); Line | Count | Source | 1172 | 0 | #define strprintf tfm::format |
|
51 | 0 | str += ")"; |
52 | 0 | return str; |
53 | 0 | } |
54 | | |
55 | | CTxOut::CTxOut(const CAmount& nValueIn, CScript scriptPubKeyIn) |
56 | 935k | { |
57 | 935k | nValue = nValueIn; |
58 | 935k | scriptPubKey = scriptPubKeyIn; |
59 | 935k | } |
60 | | |
61 | | std::string CTxOut::ToString() const |
62 | 0 | { |
63 | 0 | return strprintf("CTxOut(nValue=%d.%08d, scriptPubKey=%s)", nValue / COIN, nValue % COIN, HexStr(scriptPubKey).substr(0, 30)); Line | Count | Source | 1172 | 0 | #define strprintf tfm::format |
|
64 | 0 | } |
65 | | |
66 | 10.6M | CMutableTransaction::CMutableTransaction() : version{CTransaction::CURRENT_VERSION}, nLockTime{0} {} |
67 | 19.9M | CMutableTransaction::CMutableTransaction(const CTransaction& tx) : vin(tx.vin), vout(tx.vout), version{tx.version}, nLockTime{tx.nLockTime} {} |
68 | | |
69 | | Txid CMutableTransaction::GetHash() const |
70 | 0 | { |
71 | 0 | return Txid::FromUint256((HashWriter{} << TX_NO_WITNESS(*this)).GetHash()); |
72 | 0 | } |
73 | | |
74 | | bool CTransaction::ComputeHasWitness() const |
75 | 31.0M | { |
76 | 31.0M | return std::any_of(vin.begin(), vin.end(), [](const auto& input) { |
77 | 30.8M | return !input.scriptWitness.IsNull(); |
78 | 30.8M | }); |
79 | 31.0M | } |
80 | | |
81 | | Txid CTransaction::ComputeHash() const |
82 | 31.0M | { |
83 | 31.0M | return Txid::FromUint256((HashWriter{} << TX_NO_WITNESS(*this)).GetHash()); |
84 | 31.0M | } |
85 | | |
86 | | Wtxid CTransaction::ComputeWitnessHash() const |
87 | 31.0M | { |
88 | 31.0M | if (!HasWitness()) { |
89 | 20.7M | return Wtxid::FromUint256(hash.ToUint256()); |
90 | 20.7M | } |
91 | | |
92 | 10.2M | return Wtxid::FromUint256((HashWriter{} << TX_WITH_WITNESS(*this)).GetHash()); |
93 | 31.0M | } |
94 | | |
95 | 341k | 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 | 30.6M | 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 | 19.9M | { |
100 | 19.9M | CAmount nValueOut = 0; |
101 | 39.9M | for (const auto& tx_out : vout) { |
102 | 39.9M | if (!MoneyRange(tx_out.nValue) || !MoneyRange(nValueOut + tx_out.nValue)) |
103 | 0 | throw std::runtime_error(std::string(__func__) + ": value out of range"); |
104 | 39.9M | nValueOut += tx_out.nValue; |
105 | 39.9M | } |
106 | 19.9M | assert(MoneyRange(nValueOut)); |
107 | 19.9M | return nValueOut; |
108 | 19.9M | } |
109 | | |
110 | | unsigned int CTransaction::GetTotalSize() const |
111 | 0 | { |
112 | 0 | return ::GetSerializeSize(TX_WITH_WITNESS(*this)); |
113 | 0 | } |
114 | | |
115 | | std::string CTransaction::ToString() const |
116 | 0 | { |
117 | 0 | std::string str; |
118 | 0 | str += strprintf("CTransaction(hash=%s, ver=%u, vin.size=%u, vout.size=%u, nLockTime=%u)\n", Line | Count | Source | 1172 | 0 | #define strprintf tfm::format |
|
119 | 0 | GetHash().ToString().substr(0,10), |
120 | 0 | version, |
121 | 0 | vin.size(), |
122 | 0 | vout.size(), |
123 | 0 | nLockTime); |
124 | 0 | for (const auto& tx_in : vin) |
125 | 0 | str += " " + tx_in.ToString() + "\n"; |
126 | 0 | for (const auto& tx_in : vin) |
127 | 0 | str += " " + tx_in.scriptWitness.ToString() + "\n"; |
128 | 0 | for (const auto& tx_out : vout) |
129 | 0 | str += " " + tx_out.ToString() + "\n"; |
130 | 0 | return str; |
131 | 0 | } |