|
9 | 9 | #include "tinyformat.h" |
10 | 10 | #include "utilstrencodings.h" |
11 | 11 |
|
12 | | -std::string COutPoint::ToString() const |
13 | | -{ |
14 | | - return strprintf("COutPoint(%s, %u)", hash.ToString().substr(0,10), n); |
15 | | -} |
16 | | - |
17 | | -CTxIn::CTxIn(COutPoint prevoutIn, CScript scriptSigIn, uint32_t nSequenceIn) |
18 | | -{ |
19 | | - prevout = prevoutIn; |
20 | | - scriptSig = scriptSigIn; |
21 | | - nSequence = nSequenceIn; |
22 | | -} |
23 | | - |
24 | | -CTxIn::CTxIn(uint256 hashPrevTx, uint32_t nOut, CScript scriptSigIn, uint32_t nSequenceIn) |
25 | | -{ |
26 | | - prevout = COutPoint(hashPrevTx, nOut); |
27 | | - scriptSig = scriptSigIn; |
28 | | - nSequence = nSequenceIn; |
29 | | -} |
30 | | - |
31 | | -std::string CTxIn::ToString() const |
32 | | -{ |
33 | | - std::string str; |
34 | | - str += "CTxIn("; |
35 | | - str += prevout.ToString(); |
36 | | - if (prevout.IsNull()) |
37 | | - str += strprintf(", coinbase %s", HexStr(scriptSig)); |
38 | | - else |
39 | | - str += strprintf(", scriptSig=%s", scriptSig.ToString().substr(0,24)); |
40 | | - if (nSequence != std::numeric_limits<unsigned int>::max()) |
41 | | - str += strprintf(", nSequence=%u", nSequence); |
42 | | - str += ")"; |
43 | | - return str; |
44 | | -} |
45 | | - |
46 | | -CTxOut::CTxOut(const CAmount& nValueIn, CScript scriptPubKeyIn) |
47 | | -{ |
48 | | - nValue = nValueIn; |
49 | | - scriptPubKey = scriptPubKeyIn; |
50 | | -} |
51 | | - |
52 | | -uint256 CTxOut::GetHash() const |
53 | | -{ |
54 | | - return SerializeHash(*this); |
55 | | -} |
56 | | - |
57 | | -std::string CTxOut::ToString() const |
58 | | -{ |
59 | | - return strprintf("CTxOut(nValue=%d.%08d, scriptPubKey=%s)", nValue / COIN, nValue % COIN, scriptPubKey.ToString().substr(0,30)); |
60 | | -} |
61 | | - |
62 | | -CMutableTransaction::CMutableTransaction() : nVersion(CTransaction::CURRENT_VERSION), nLockTime(0) {} |
63 | | -CMutableTransaction::CMutableTransaction(const CTransaction& tx) : nVersion(tx.nVersion), vin(tx.vin), vout(tx.vout), nLockTime(tx.nLockTime) {} |
64 | | - |
65 | | -uint256 CMutableTransaction::GetHash() const |
66 | | -{ |
67 | | - return SerializeHash(*this); |
68 | | -} |
69 | | - |
70 | | -void CTransaction::UpdateHash() const |
71 | | -{ |
72 | | - *const_cast<uint256*>(&hash) = SerializeHash(*this); |
73 | | -} |
74 | | - |
75 | | -CTransaction::CTransaction() : hash(0), nVersion(CTransaction::CURRENT_VERSION), vin(), vout(), nLockTime(0) { } |
76 | | - |
77 | | -CTransaction::CTransaction(const CMutableTransaction &tx) : nVersion(tx.nVersion), vin(tx.vin), vout(tx.vout), nLockTime(tx.nLockTime) { |
78 | | - UpdateHash(); |
79 | | -} |
80 | | - |
81 | | -CTransaction& CTransaction::operator=(const CTransaction &tx) { |
82 | | - *const_cast<int*>(&nVersion) = tx.nVersion; |
83 | | - *const_cast<std::vector<CTxIn>*>(&vin) = tx.vin; |
84 | | - *const_cast<std::vector<CTxOut>*>(&vout) = tx.vout; |
85 | | - *const_cast<unsigned int*>(&nLockTime) = tx.nLockTime; |
86 | | - *const_cast<uint256*>(&hash) = tx.hash; |
87 | | - return *this; |
88 | | -} |
89 | | - |
90 | | -CAmount CTransaction::GetValueOut() const |
91 | | -{ |
92 | | - CAmount nValueOut = 0; |
93 | | - for (std::vector<CTxOut>::const_iterator it(vout.begin()); it != vout.end(); ++it) |
94 | | - { |
95 | | - nValueOut += it->nValue; |
96 | | - if (!MoneyRange(it->nValue) || !MoneyRange(nValueOut)) |
97 | | - throw std::runtime_error("CTransaction::GetValueOut() : value out of range"); |
98 | | - } |
99 | | - return nValueOut; |
100 | | -} |
101 | | - |
102 | | -double CTransaction::ComputePriority(double dPriorityInputs, unsigned int nTxSize) const |
103 | | -{ |
104 | | - nTxSize = CalculateModifiedSize(nTxSize); |
105 | | - if (nTxSize == 0) return 0.0; |
106 | | - |
107 | | - return dPriorityInputs / nTxSize; |
108 | | -} |
109 | | - |
110 | | -unsigned int CTransaction::CalculateModifiedSize(unsigned int nTxSize) const |
111 | | -{ |
112 | | - // In order to avoid disincentivizing cleaning up the UTXO set we don't count |
113 | | - // the constant overhead for each txin and up to 110 bytes of scriptSig (which |
114 | | - // is enough to cover a compressed pubkey p2sh redemption) for priority. |
115 | | - // Providing any more cleanup incentive than making additional inputs free would |
116 | | - // risk encouraging people to create junk outputs to redeem later. |
117 | | - if (nTxSize == 0) |
118 | | - nTxSize = ::GetSerializeSize(*this, SER_NETWORK, PROTOCOL_VERSION); |
119 | | - for (std::vector<CTxIn>::const_iterator it(vin.begin()); it != vin.end(); ++it) |
120 | | - { |
121 | | - unsigned int offset = 41U + std::min(110U, (unsigned int)it->scriptSig.size()); |
122 | | - if (nTxSize > offset) |
123 | | - nTxSize -= offset; |
124 | | - } |
125 | | - return nTxSize; |
126 | | -} |
127 | | - |
128 | | -std::string CTransaction::ToString() const |
129 | | -{ |
130 | | - std::string str; |
131 | | - str += strprintf("CTransaction(hash=%s, ver=%d, vin.size=%u, vout.size=%u, nLockTime=%u)\n", |
132 | | - GetHash().ToString().substr(0,10), |
133 | | - nVersion, |
134 | | - vin.size(), |
135 | | - vout.size(), |
136 | | - nLockTime); |
137 | | - for (unsigned int i = 0; i < vin.size(); i++) |
138 | | - str += " " + vin[i].ToString() + "\n"; |
139 | | - for (unsigned int i = 0; i < vout.size(); i++) |
140 | | - str += " " + vout[i].ToString() + "\n"; |
141 | | - return str; |
142 | | -} |
143 | | - |
144 | 12 | // Amount compression: |
145 | 13 | // * If the amount is 0, output 0 |
146 | 14 | // * first, divide the amount (in base units) by the largest power of 10 possible; call the exponent e (e is max 9) |
|
0 commit comments