Line data Source code
1 : // Copyright (c) 2009-2010 Satoshi Nakamoto
2 : // Copyright (c) 2009-2021 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 <hash.h>
10 : #include <script/script.h>
11 : #include <serialize.h>
12 : #include <tinyformat.h>
13 : #include <uint256.h>
14 : #include <util/strencodings.h>
15 : #include <version.h>
16 :
17 : #include <cassert>
18 : #include <stdexcept>
19 :
20 73555 : std::string COutPoint::ToString() const
21 : {
22 73555 : return strprintf("COutPoint(%s, %u)", hash.ToString()/*.substr(0,10)*/, n);
23 0 : }
24 :
25 75 : std::string COutPoint::ToStringShort() const
26 : {
27 75 : return strprintf("%s-%u", hash.ToString().substr(0,64), n);
28 0 : }
29 :
30 226826 : CTxIn::CTxIn(COutPoint prevoutIn, CScript scriptSigIn, uint32_t nSequenceIn)
31 113413 : {
32 113413 : prevout = prevoutIn;
33 113413 : scriptSig = scriptSigIn;
34 113413 : nSequence = nSequenceIn;
35 226826 : }
36 :
37 64 : CTxIn::CTxIn(uint256 hashPrevTx, uint32_t nOut, CScript scriptSigIn, uint32_t nSequenceIn)
38 32 : {
39 32 : prevout = COutPoint(hashPrevTx, nOut);
40 32 : scriptSig = scriptSigIn;
41 32 : nSequence = nSequenceIn;
42 64 : }
43 :
44 73555 : std::string CTxIn::ToString() const
45 : {
46 73555 : std::string str;
47 73555 : str += "CTxIn(";
48 73555 : str += prevout.ToString();
49 73555 : if (prevout.IsNull())
50 73526 : str += strprintf(", coinbase %s", HexStr(scriptSig));
51 : else
52 29 : str += strprintf(", scriptSig=%s", HexStr(scriptSig).substr(0, 24));
53 73555 : if (nSequence != SEQUENCE_FINAL)
54 29 : str += strprintf(", nSequence=%u", nSequence);
55 73555 : str += ")";
56 73555 : return str;
57 73555 : }
58 :
59 70562 : CTxOut::CTxOut(const CAmount& nValueIn, CScript scriptPubKeyIn)
60 35281 : {
61 35281 : nValue = nValueIn;
62 35281 : scriptPubKey = scriptPubKeyIn;
63 70562 : }
64 :
65 109025 : std::string CTxOut::ToString() const
66 : {
67 109025 : return strprintf("CTxOut(nValue=%d.%08d, scriptPubKey=%s)", nValue / COIN, nValue % COIN, HexStr(scriptPubKey).substr(0, 30));
68 0 : }
69 :
70 1088502 : CMutableTransaction::CMutableTransaction() : nVersion(CTransaction::CURRENT_VERSION), nType(TRANSACTION_NORMAL), nLockTime(0) {}
71 172574 : CMutableTransaction::CMutableTransaction(const CTransaction& tx) : vin(tx.vin), vout(tx.vout), nVersion(tx.nVersion), nType(tx.nType), nLockTime(tx.nLockTime), vExtraPayload(tx.vExtraPayload) {}
72 :
73 258234 : uint256 CMutableTransaction::GetHash() const
74 : {
75 258234 : return SerializeHash(*this);
76 : }
77 :
78 24517 : std::string CMutableTransaction::ToString() const
79 : {
80 24517 : std::string str;
81 24517 : str += strprintf("CMutableTransaction(hash=%s, ver=%d, type=%d, vin.size=%u, vout.size=%u, nLockTime=%u, vExtraPayload.size=%d)\n",
82 24517 : GetHash().ToString().substr(0,10),
83 24517 : nVersion,
84 24517 : nType,
85 24517 : vin.size(),
86 24517 : vout.size(),
87 24517 : nLockTime,
88 24517 : vExtraPayload.size());
89 49034 : for (unsigned int i = 0; i < vin.size(); i++)
90 24517 : str += " " + vin[i].ToString() + "\n";
91 57694 : for (unsigned int i = 0; i < vout.size(); i++)
92 33177 : str += " " + vout[i].ToString() + "\n";
93 24517 : return str;
94 24517 : }
95 :
96 650213 : uint256 CTransaction::ComputeHash() const
97 : {
98 650213 : return SerializeHash(*this);
99 : }
100 :
101 428612 : CTransaction::CTransaction(const CMutableTransaction& tx) : vin(tx.vin), vout(tx.vout), nVersion(tx.nVersion), nType(tx.nType), nLockTime(tx.nLockTime), vExtraPayload(tx.vExtraPayload), hash{ComputeHash()} {}
102 871814 : CTransaction::CTransaction(CMutableTransaction&& tx) : vin(std::move(tx.vin)), vout(std::move(tx.vout)), nVersion(tx.nVersion), nType(tx.nType), nLockTime(tx.nLockTime), vExtraPayload(tx.vExtraPayload), hash{ComputeHash()} {}
103 :
104 89429 : CAmount CTransaction::GetValueOut() const
105 : {
106 89429 : CAmount nValueOut = 0;
107 179205 : for (const auto& tx_out : vout) {
108 89776 : if (!MoneyRange(tx_out.nValue) || !MoneyRange(nValueOut + tx_out.nValue))
109 0 : throw std::runtime_error(std::string(__func__) + ": value out of range");
110 89776 : nValueOut += tx_out.nValue;
111 : }
112 89429 : assert(MoneyRange(nValueOut));
113 89429 : return nValueOut;
114 0 : }
115 :
116 41775 : unsigned int CTransaction::GetTotalSize() const
117 : {
118 41775 : return ::GetSerializeSize(*this, PROTOCOL_VERSION);
119 : }
120 :
121 49026 : std::string CTransaction::ToString() const
122 : {
123 49026 : std::string str;
124 49026 : str += strprintf("CTransaction(hash=%s, ver=%d, type=%d, vin.size=%u, vout.size=%u, nLockTime=%u, vExtraPayload.size=%d)\n",
125 49026 : GetHash().ToString().substr(0,10),
126 49026 : nVersion,
127 49026 : nType,
128 49026 : vin.size(),
129 49026 : vout.size(),
130 49026 : nLockTime,
131 49026 : vExtraPayload.size());
132 98064 : for (const auto& tx_in : vin)
133 49038 : str += " " + tx_in.ToString() + "\n";
134 116214 : for (const auto& tx_out : vout)
135 67188 : str += " " + tx_out.ToString() + "\n";
136 49026 : return str;
137 49026 : }
|