Add deserializing constructors to CTransaction and CMutableTransaction
[bitcoinplatinum.git] / src / primitives / transaction.cpp
blob91f4d29488b16e4b1457c9a4e23dc8501de4e770
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 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.
6 #include "primitives/transaction.h"
8 #include "hash.h"
9 #include "tinyformat.h"
10 #include "utilstrencodings.h"
12 std::string COutPoint::ToString() const
14 return strprintf("COutPoint(%s, %u)", hash.ToString().substr(0,10), n);
17 CTxIn::CTxIn(COutPoint prevoutIn, CScript scriptSigIn, uint32_t nSequenceIn)
19 prevout = prevoutIn;
20 scriptSig = scriptSigIn;
21 nSequence = nSequenceIn;
24 CTxIn::CTxIn(uint256 hashPrevTx, uint32_t nOut, CScript scriptSigIn, uint32_t nSequenceIn)
26 prevout = COutPoint(hashPrevTx, nOut);
27 scriptSig = scriptSigIn;
28 nSequence = nSequenceIn;
31 std::string CTxIn::ToString() const
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", HexStr(scriptSig).substr(0, 24));
40 if (nSequence != SEQUENCE_FINAL)
41 str += strprintf(", nSequence=%u", nSequence);
42 str += ")";
43 return str;
46 CTxOut::CTxOut(const CAmount& nValueIn, CScript scriptPubKeyIn)
48 nValue = nValueIn;
49 scriptPubKey = scriptPubKeyIn;
52 std::string CTxOut::ToString() const
54 return strprintf("CTxOut(nValue=%d.%08d, scriptPubKey=%s)", nValue / COIN, nValue % COIN, HexStr(scriptPubKey).substr(0, 30));
57 CMutableTransaction::CMutableTransaction() : nVersion(CTransaction::CURRENT_VERSION), nLockTime(0) {}
58 CMutableTransaction::CMutableTransaction(const CTransaction& tx) : nVersion(tx.nVersion), vin(tx.vin), vout(tx.vout), wit(tx.wit), nLockTime(tx.nLockTime) {}
60 uint256 CMutableTransaction::GetHash() const
62 return SerializeHash(*this, SER_GETHASH, SERIALIZE_TRANSACTION_NO_WITNESS);
65 void CTransaction::UpdateHash() const
67 *const_cast<uint256*>(&hash) = SerializeHash(*this, SER_GETHASH, SERIALIZE_TRANSACTION_NO_WITNESS);
70 uint256 CTransaction::GetWitnessHash() const
72 return SerializeHash(*this, SER_GETHASH, 0);
75 CTransaction::CTransaction() : nVersion(CTransaction::CURRENT_VERSION), vin(), vout(), nLockTime(0) { }
77 CTransaction::CTransaction(const CMutableTransaction &tx) : nVersion(tx.nVersion), vin(tx.vin), vout(tx.vout), wit(tx.wit), nLockTime(tx.nLockTime) {
78 UpdateHash();
81 CTransaction::CTransaction(CMutableTransaction &&tx) : nVersion(tx.nVersion), vin(std::move(tx.vin)), vout(std::move(tx.vout)), wit(std::move(tx.wit)), nLockTime(tx.nLockTime) {
82 UpdateHash();
85 CTransaction& CTransaction::operator=(const CTransaction &tx) {
86 *const_cast<int*>(&nVersion) = tx.nVersion;
87 *const_cast<std::vector<CTxIn>*>(&vin) = tx.vin;
88 *const_cast<std::vector<CTxOut>*>(&vout) = tx.vout;
89 *const_cast<CTxWitness*>(&wit) = tx.wit;
90 *const_cast<unsigned int*>(&nLockTime) = tx.nLockTime;
91 *const_cast<uint256*>(&hash) = tx.hash;
92 return *this;
95 CAmount CTransaction::GetValueOut() const
97 CAmount nValueOut = 0;
98 for (std::vector<CTxOut>::const_iterator it(vout.begin()); it != vout.end(); ++it)
100 nValueOut += it->nValue;
101 if (!MoneyRange(it->nValue) || !MoneyRange(nValueOut))
102 throw std::runtime_error(std::string(__func__) + ": value out of range");
104 return nValueOut;
107 double CTransaction::ComputePriority(double dPriorityInputs, unsigned int nTxSize) const
109 nTxSize = CalculateModifiedSize(nTxSize);
110 if (nTxSize == 0) return 0.0;
112 return dPriorityInputs / nTxSize;
115 unsigned int CTransaction::CalculateModifiedSize(unsigned int nTxSize) const
117 // In order to avoid disincentivizing cleaning up the UTXO set we don't count
118 // the constant overhead for each txin and up to 110 bytes of scriptSig (which
119 // is enough to cover a compressed pubkey p2sh redemption) for priority.
120 // Providing any more cleanup incentive than making additional inputs free would
121 // risk encouraging people to create junk outputs to redeem later.
122 if (nTxSize == 0)
123 nTxSize = (GetTransactionWeight(*this) + WITNESS_SCALE_FACTOR - 1) / WITNESS_SCALE_FACTOR;
124 for (std::vector<CTxIn>::const_iterator it(vin.begin()); it != vin.end(); ++it)
126 unsigned int offset = 41U + std::min(110U, (unsigned int)it->scriptSig.size());
127 if (nTxSize > offset)
128 nTxSize -= offset;
130 return nTxSize;
133 unsigned int CTransaction::GetTotalSize() const
135 return ::GetSerializeSize(*this, SER_NETWORK, PROTOCOL_VERSION);
138 std::string CTransaction::ToString() const
140 std::string str;
141 str += strprintf("CTransaction(hash=%s, ver=%d, vin.size=%u, vout.size=%u, nLockTime=%u)\n",
142 GetHash().ToString().substr(0,10),
143 nVersion,
144 vin.size(),
145 vout.size(),
146 nLockTime);
147 for (unsigned int i = 0; i < vin.size(); i++)
148 str += " " + vin[i].ToString() + "\n";
149 for (unsigned int i = 0; i < wit.vtxinwit.size(); i++)
150 str += " " + wit.vtxinwit[i].scriptWitness.ToString() + "\n";
151 for (unsigned int i = 0; i < vout.size(); i++)
152 str += " " + vout[i].ToString() + "\n";
153 return str;
156 int64_t GetTransactionWeight(const CTransaction& tx)
158 return ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS) * (WITNESS_SCALE_FACTOR -1) + ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION);