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 438843 : std::string COutPoint::ToString() const
21 : {
22 438843 : return strprintf("COutPoint(%s, %u)", hash.ToString()/*.substr(0,10)*/, n);
23 0 : }
24 :
25 55950 : std::string COutPoint::ToStringShort() const
26 : {
27 55950 : return strprintf("%s-%u", hash.ToString().substr(0,64), n);
28 0 : }
29 :
30 1526010 : CTxIn::CTxIn(COutPoint prevoutIn, CScript scriptSigIn, uint32_t nSequenceIn)
31 763005 : {
32 763005 : prevout = prevoutIn;
33 763005 : scriptSig = scriptSigIn;
34 763005 : nSequence = nSequenceIn;
35 1526010 : }
36 :
37 13608 : CTxIn::CTxIn(uint256 hashPrevTx, uint32_t nOut, CScript scriptSigIn, uint32_t nSequenceIn)
38 6804 : {
39 6804 : prevout = COutPoint(hashPrevTx, nOut);
40 6804 : scriptSig = scriptSigIn;
41 6804 : nSequence = nSequenceIn;
42 13608 : }
43 :
44 438843 : std::string CTxIn::ToString() const
45 : {
46 438843 : std::string str;
47 438843 : str += "CTxIn(";
48 438843 : str += prevout.ToString();
49 438843 : if (prevout.IsNull())
50 428566 : str += strprintf(", coinbase %s", HexStr(scriptSig));
51 : else
52 10277 : str += strprintf(", scriptSig=%s", HexStr(scriptSig).substr(0, 24));
53 438843 : if (nSequence != SEQUENCE_FINAL)
54 9967 : str += strprintf(", nSequence=%u", nSequence);
55 438843 : str += ")";
56 438843 : return str;
57 438843 : }
58 :
59 1185972 : CTxOut::CTxOut(const CAmount& nValueIn, CScript scriptPubKeyIn)
60 592986 : {
61 592986 : nValue = nValueIn;
62 592986 : scriptPubKey = scriptPubKeyIn;
63 1185972 : }
64 :
65 824584 : std::string CTxOut::ToString() const
66 : {
67 824584 : return strprintf("CTxOut(nValue=%d.%08d, scriptPubKey=%s)", nValue / COIN, nValue % COIN, HexStr(scriptPubKey).substr(0, 30));
68 0 : }
69 :
70 3179788 : CMutableTransaction::CMutableTransaction() : nVersion(CTransaction::CURRENT_VERSION), nType(TRANSACTION_NORMAL), nLockTime(0) {}
71 206230 : 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 327586 : uint256 CMutableTransaction::GetHash() const
74 : {
75 327586 : return SerializeHash(*this);
76 : }
77 :
78 87814 : std::string CMutableTransaction::ToString() const
79 : {
80 87814 : std::string str;
81 87814 : str += strprintf("CMutableTransaction(hash=%s, ver=%d, type=%d, vin.size=%u, vout.size=%u, nLockTime=%u, vExtraPayload.size=%d)\n",
82 87814 : GetHash().ToString().substr(0,10),
83 87814 : nVersion,
84 87814 : nType,
85 87814 : vin.size(),
86 87814 : vout.size(),
87 87814 : nLockTime,
88 87814 : vExtraPayload.size());
89 175415 : for (unsigned int i = 0; i < vin.size(); i++)
90 87601 : str += " " + vin[i].ToString() + "\n";
91 223171 : for (unsigned int i = 0; i < vout.size(); i++)
92 135357 : str += " " + vout[i].ToString() + "\n";
93 87814 : return str;
94 87814 : }
95 :
96 3027225 : uint256 CTransaction::ComputeHash() const
97 : {
98 3027225 : return SerializeHash(*this);
99 : }
100 :
101 593564 : 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 5460873 : 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 8900063 : CAmount CTransaction::GetValueOut() const
105 : {
106 8900063 : CAmount nValueOut = 0;
107 26936898 : for (const auto& tx_out : vout) {
108 18036835 : if (!MoneyRange(tx_out.nValue) || !MoneyRange(nValueOut + tx_out.nValue))
109 0 : throw std::runtime_error(std::string(__func__) + ": value out of range");
110 18036835 : nValueOut += tx_out.nValue;
111 : }
112 8900063 : assert(MoneyRange(nValueOut));
113 8900063 : return nValueOut;
114 0 : }
115 :
116 158906 : unsigned int CTransaction::GetTotalSize() const
117 : {
118 158906 : return ::GetSerializeSize(*this, PROTOCOL_VERSION);
119 : }
120 :
121 346120 : std::string CTransaction::ToString() const
122 : {
123 346120 : std::string str;
124 346120 : str += strprintf("CTransaction(hash=%s, ver=%d, type=%d, vin.size=%u, vout.size=%u, nLockTime=%u, vExtraPayload.size=%d)\n",
125 346120 : GetHash().ToString().substr(0,10),
126 346120 : nVersion,
127 346120 : nType,
128 346120 : vin.size(),
129 346120 : vout.size(),
130 346120 : nLockTime,
131 346120 : vExtraPayload.size());
132 697362 : for (const auto& tx_in : vin)
133 351242 : str += " " + tx_in.ToString() + "\n";
134 987177 : for (const auto& tx_out : vout)
135 641057 : str += " " + tx_out.ToString() + "\n";
136 346120 : return str;
137 346120 : }
|