Bitcoin Core  0.18.99
P2P Digital Currency
transaction.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2018 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 
7 
8 #include <hash.h>
9 #include <tinyformat.h>
10 #include <util/strencodings.h>
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", HexStr(scriptSig).substr(0, 24));
40  if (nSequence != SEQUENCE_FINAL)
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 std::string CTxOut::ToString() const
53 {
54  return strprintf("CTxOut(nValue=%d.%08d, scriptPubKey=%s)", nValue / COIN, nValue % COIN, HexStr(scriptPubKey).substr(0, 30));
55 }
56 
57 CMutableTransaction::CMutableTransaction() : nVersion(CTransaction::CURRENT_VERSION), nLockTime(0) {}
59 
61 {
63 }
64 
66 {
68 }
69 
71 {
72  if (!HasWitness()) {
73  return hash;
74  }
75  return SerializeHash(*this, SER_GETHASH, 0);
76 }
77 
78 /* For backward compatibility, the hash is initialized to 0. TODO: remove the need for this default constructor entirely. */
79 CTransaction::CTransaction() : vin(), vout(), nVersion(CTransaction::CURRENT_VERSION), nLockTime(0), hash{}, m_witness_hash{} {}
82 
84 {
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");
90  }
91  return nValueOut;
92 }
93 
94 unsigned int CTransaction::GetTotalSize() const
95 {
97 }
98 
99 std::string CTransaction::ToString() const
100 {
101  std::string str;
102  str += strprintf("CTransaction(hash=%s, ver=%d, vin.size=%u, vout.size=%u, nLockTime=%u)\n",
103  GetHash().ToString().substr(0,10),
104  nVersion,
105  vin.size(),
106  vout.size(),
107  nLockTime);
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";
114  return str;
115 }
static const int SERIALIZE_TRANSACTION_NO_WITNESS
Definition: transaction.h:15
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1067
std::vector< CTxIn > vin
Definition: transaction.h:368
static const CAmount COIN
Definition: amount.h:14
bool MoneyRange(const CAmount &nValue)
Definition: amount.h:26
std::string ToString() const
Definition: transaction.cpp:31
std::string ToString() const
Definition: transaction.cpp:12
std::string ToString() const
Definition: transaction.cpp:52
const uint256 hash
Memory only.
Definition: transaction.h:294
const std::vector< CTxIn > vin
Definition: transaction.h:287
CAmount GetValueOut() const
Definition: transaction.cpp:83
size_t GetSerializeSize(const T &t, int nVersion=0)
Definition: serialize.h:992
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
uint256 SerializeHash(const T &obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
Compute the 256-bit hash of an object&#39;s serialization.
Definition: hash.h:196
const uint256 & GetHash() const
Definition: transaction.h:322
uint32_t n
Definition: transaction.h:22
const std::vector< CTxOut > vout
Definition: transaction.h:288
std::string ToString() const
Definition: uint256.cpp:61
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:18
std::vector< CTxOut > vout
Definition: transaction.h:369
bool HasWitness() const
Definition: transaction.h:397
const uint256 m_witness_hash
Definition: transaction.h:295
uint256 ComputeHash() const
Definition: transaction.cpp:65
uint256 ComputeWitnessHash() const
Definition: transaction.cpp:70
256-bit opaque blob.
Definition: uint256.h:121
const int32_t nVersion
Definition: transaction.h:289
uint256 GetHash() const
Compute the hash of this CMutableTransaction.
Definition: transaction.cpp:60
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:390
CTxIn()
Definition: transaction.h:98
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:12
CTransaction()
Construct a CTransaction that qualifies as IsNull()
Definition: transaction.cpp:79
std::string ToString() const
Definition: transaction.cpp:99
std::string HexStr(const T itbegin, const T itend)
Definition: strencodings.h:125
A mutable version of CTransaction.
Definition: transaction.h:366
unsigned int GetTotalSize() const
Get the total transaction size in bytes, including witness data.
Definition: transaction.cpp:94
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:270
const uint32_t nLockTime
Definition: transaction.h:290
uint256 hash
Definition: transaction.h:21