Line data Source code
1 : // Copyright (c) 2009-2010 Satoshi Nakamoto
2 : // Copyright (c) 2009-2014 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 "hash.h"
9 : #include "tinyformat.h"
10 : #include "utilstrencodings.h"
11 :
12 210 : std::string COutPoint::ToString() const
13 : {
14 630 : return strprintf("COutPoint(%s, %u)", hash.ToString().substr(0,10), n);
15 : }
16 :
17 43 : CTxIn::CTxIn(COutPoint prevoutIn, CScript scriptSigIn, uint32_t nSequenceIn)
18 : {
19 43 : prevout = prevoutIn;
20 43 : scriptSig = scriptSigIn;
21 43 : nSequence = nSequenceIn;
22 43 : }
23 :
24 774 : CTxIn::CTxIn(uint256 hashPrevTx, uint32_t nOut, CScript scriptSigIn, uint32_t nSequenceIn)
25 : {
26 774 : prevout = COutPoint(hashPrevTx, nOut);
27 774 : scriptSig = scriptSigIn;
28 774 : nSequence = nSequenceIn;
29 774 : }
30 :
31 210 : std::string CTxIn::ToString() const
32 : {
33 : std::string str;
34 : str += "CTxIn(";
35 420 : str += prevout.ToString();
36 420 : if (prevout.IsNull())
37 0 : str += strprintf(", coinbase %s", HexStr(scriptSig));
38 : else
39 1050 : str += strprintf(", scriptSig=%s", HexStr(scriptSig).substr(0, 24));
40 210 : if (nSequence != std::numeric_limits<unsigned int>::max())
41 420 : str += strprintf(", nSequence=%u", nSequence);
42 : str += ")";
43 210 : return str;
44 : }
45 :
46 766 : CTxOut::CTxOut(const CAmount& nValueIn, CScript scriptPubKeyIn)
47 : {
48 766 : nValue = nValueIn;
49 766 : scriptPubKey = scriptPubKeyIn;
50 766 : }
51 :
52 0 : uint256 CTxOut::GetHash() const
53 : {
54 0 : return SerializeHash(*this);
55 : }
56 :
57 254 : std::string CTxOut::ToString() const
58 : {
59 1016 : return strprintf("CTxOut(nValue=%d.%08d, scriptPubKey=%s)", nValue / COIN, nValue % COIN, HexStr(scriptPubKey).substr(0, 30));
60 : }
61 :
62 231219 : CMutableTransaction::CMutableTransaction() : nVersion(CTransaction::CURRENT_VERSION), nLockTime(0) {}
63 51580 : CMutableTransaction::CMutableTransaction(const CTransaction& tx) : nVersion(tx.nVersion), vin(tx.vin), vout(tx.vout), nLockTime(tx.nLockTime) {}
64 :
65 18188 : uint256 CMutableTransaction::GetHash() const
66 : {
67 18188 : return SerializeHash(*this);
68 : }
69 :
70 196130 : void CTransaction::UpdateHash() const
71 : {
72 196130 : *const_cast<uint256*>(&hash) = SerializeHash(*this);
73 196130 : }
74 :
75 1494848 : CTransaction::CTransaction() : nVersion(CTransaction::CURRENT_VERSION), vin(), vout(), nLockTime(0) { }
76 :
77 322124 : CTransaction::CTransaction(const CMutableTransaction &tx) : nVersion(tx.nVersion), vin(tx.vin), vout(tx.vout), nLockTime(tx.nLockTime) {
78 161062 : UpdateHash();
79 161062 : }
80 :
81 78457 : CTransaction& CTransaction::operator=(const CTransaction &tx) {
82 78457 : *const_cast<int*>(&nVersion) = tx.nVersion;
83 78457 : *const_cast<std::vector<CTxIn>*>(&vin) = tx.vin;
84 78457 : *const_cast<std::vector<CTxOut>*>(&vout) = tx.vout;
85 78457 : *const_cast<unsigned int*>(&nLockTime) = tx.nLockTime;
86 78457 : *const_cast<uint256*>(&hash) = tx.hash;
87 78457 : return *this;
88 : }
89 :
90 56587 : CAmount CTransaction::GetValueOut() const
91 : {
92 56587 : CAmount nValueOut = 0;
93 486704 : for (std::vector<CTxOut>::const_iterator it(vout.begin()); it != vout.end(); ++it)
94 : {
95 67923 : nValueOut += it->nValue;
96 203769 : if (!MoneyRange(it->nValue) || !MoneyRange(nValueOut))
97 0 : throw std::runtime_error("CTransaction::GetValueOut(): value out of range");
98 : }
99 56587 : return nValueOut;
100 : }
101 :
102 2179 : double CTransaction::ComputePriority(double dPriorityInputs, unsigned int nTxSize) const
103 : {
104 2179 : nTxSize = CalculateModifiedSize(nTxSize);
105 2179 : if (nTxSize == 0) return 0.0;
106 :
107 2179 : return dPriorityInputs / nTxSize;
108 : }
109 :
110 19624 : 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 19624 : if (nTxSize == 0)
118 557 : nTxSize = ::GetSerializeSize(*this, SER_NETWORK, PROTOCOL_VERSION);
119 161516 : for (std::vector<CTxIn>::const_iterator it(vin.begin()); it != vin.end(); ++it)
120 : {
121 63396 : unsigned int offset = 41U + std::min(110U, (unsigned int)it->scriptSig.size());
122 21132 : if (nTxSize > offset)
123 21132 : nTxSize -= offset;
124 : }
125 19624 : return nTxSize;
126 : }
127 :
128 124 : std::string CTransaction::ToString() const
129 : {
130 : std::string str;
131 744 : 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 124 : vin.size(),
135 124 : vout.size(),
136 : nLockTime);
137 668 : for (unsigned int i = 0; i < vin.size(); i++)
138 1050 : str += " " + vin[i].ToString() + "\n";
139 632 : for (unsigned int i = 0; i < vout.size(); i++)
140 1270 : str += " " + vout[i].ToString() + "\n";
141 124 : return str;
142 330 : }
|