1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 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"
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
)
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
35 str
+= prevout
.ToString();
37 str
+= strprintf(", coinbase %s", HexStr(scriptSig
));
39 str
+= strprintf(", scriptSig=%s", HexStr(scriptSig
).substr(0, 24));
40 if (nSequence
!= SEQUENCE_FINAL
)
41 str
+= strprintf(", nSequence=%u", nSequence
);
46 CTxOut::CTxOut(const CAmount
& nValueIn
, CScript scriptPubKeyIn
)
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
), nLockTime(tx
.nLockTime
) {}
60 uint256
CMutableTransaction::GetHash() const
62 return SerializeHash(*this, SER_GETHASH
, SERIALIZE_TRANSACTION_NO_WITNESS
);
65 uint256
CTransaction::ComputeHash() const
67 return SerializeHash(*this, SER_GETHASH
, SERIALIZE_TRANSACTION_NO_WITNESS
);
70 uint256
CTransaction::GetWitnessHash() const
75 return SerializeHash(*this, SER_GETHASH
, 0);
78 /* For backward compatibility, the hash is initialized to 0. TODO: remove the need for this default constructor entirely. */
79 CTransaction::CTransaction() : nVersion(CTransaction::CURRENT_VERSION
), vin(), vout(), nLockTime(0), hash() {}
80 CTransaction::CTransaction(const CMutableTransaction
&tx
) : nVersion(tx
.nVersion
), vin(tx
.vin
), vout(tx
.vout
), nLockTime(tx
.nLockTime
), hash(ComputeHash()) {}
81 CTransaction::CTransaction(CMutableTransaction
&&tx
) : nVersion(tx
.nVersion
), vin(std::move(tx
.vin
)), vout(std::move(tx
.vout
)), nLockTime(tx
.nLockTime
), hash(ComputeHash()) {}
83 CAmount
CTransaction::GetValueOut() const
85 CAmount nValueOut
= 0;
86 for (const auto& tx_out
: vout
) {
87 nValueOut
+= tx_out
.nValue
;
88 if (!MoneyRange(tx_out
.nValue
) || !MoneyRange(nValueOut
))
89 throw std::runtime_error(std::string(__func__
) + ": value out of range");
94 unsigned int CTransaction::GetTotalSize() const
96 return ::GetSerializeSize(*this, SER_NETWORK
, PROTOCOL_VERSION
);
99 std::string
CTransaction::ToString() const
102 str
+= strprintf("CTransaction(hash=%s, ver=%d, vin.size=%u, vout.size=%u, nLockTime=%u)\n",
103 GetHash().ToString().substr(0,10),
108 for (const auto& tx_in
: vin
)
109 str
+= " " + tx_in
.ToString() + "\n";
110 for (const auto& tx_in
: vin
)
111 str
+= " " + tx_in
.scriptWitness
.ToString() + "\n";
112 for (const auto& tx_out
: vout
)
113 str
+= " " + tx_out
.ToString() + "\n";