Bitcoin Core  0.20.99
P2P Digital Currency
transaction.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2020 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 #include <assert.h>
13 
14 std::string COutPoint::ToString() const
15 {
16  return strprintf("COutPoint(%s, %u)", hash.ToString().substr(0,10), n);
17 }
18 
19 CTxIn::CTxIn(COutPoint prevoutIn, CScript scriptSigIn, uint32_t nSequenceIn)
20 {
21  prevout = prevoutIn;
22  scriptSig = scriptSigIn;
23  nSequence = nSequenceIn;
24 }
25 
26 CTxIn::CTxIn(uint256 hashPrevTx, uint32_t nOut, CScript scriptSigIn, uint32_t nSequenceIn)
27 {
28  prevout = COutPoint(hashPrevTx, nOut);
29  scriptSig = scriptSigIn;
30  nSequence = nSequenceIn;
31 }
32 
33 std::string CTxIn::ToString() const
34 {
35  std::string str;
36  str += "CTxIn(";
37  str += prevout.ToString();
38  if (prevout.IsNull())
39  str += strprintf(", coinbase %s", HexStr(scriptSig));
40  else
41  str += strprintf(", scriptSig=%s", HexStr(scriptSig).substr(0, 24));
42  if (nSequence != SEQUENCE_FINAL)
43  str += strprintf(", nSequence=%u", nSequence);
44  str += ")";
45  return str;
46 }
47 
48 CTxOut::CTxOut(const CAmount& nValueIn, CScript scriptPubKeyIn)
49 {
50  nValue = nValueIn;
51  scriptPubKey = scriptPubKeyIn;
52 }
53 
54 std::string CTxOut::ToString() const
55 {
56  return strprintf("CTxOut(nValue=%d.%08d, scriptPubKey=%s)", nValue / COIN, nValue % COIN, HexStr(scriptPubKey).substr(0, 30));
57 }
58 
59 CMutableTransaction::CMutableTransaction() : nVersion(CTransaction::CURRENT_VERSION), nLockTime(0) {}
61 
63 {
65 }
66 
68 {
70 }
71 
73 {
74  if (!HasWitness()) {
75  return hash;
76  }
77  return SerializeHash(*this, SER_GETHASH, 0);
78 }
79 
80 /* For backward compatibility, the hash is initialized to 0. TODO: remove the need for this default constructor entirely. */
81 CTransaction::CTransaction() : vin(), vout(), nVersion(CTransaction::CURRENT_VERSION), nLockTime(0), hash{}, m_witness_hash{} {}
84 
86 {
87  CAmount nValueOut = 0;
88  for (const auto& tx_out : vout) {
89  if (!MoneyRange(tx_out.nValue) || !MoneyRange(nValueOut + tx_out.nValue))
90  throw std::runtime_error(std::string(__func__) + ": value out of range");
91  nValueOut += tx_out.nValue;
92  }
93  assert(MoneyRange(nValueOut));
94  return nValueOut;
95 }
96 
97 unsigned int CTransaction::GetTotalSize() const
98 {
100 }
101 
102 std::string CTransaction::ToString() const
103 {
104  std::string str;
105  str += strprintf("CTransaction(hash=%s, ver=%d, vin.size=%u, vout.size=%u, nLockTime=%u)\n",
106  GetHash().ToString().substr(0,10),
107  nVersion,
108  vin.size(),
109  vout.size(),
110  nLockTime);
111  for (const auto& tx_in : vin)
112  str += " " + tx_in.ToString() + "\n";
113  for (const auto& tx_in : vin)
114  str += " " + tx_in.scriptWitness.ToString() + "\n";
115  for (const auto& tx_out : vout)
116  str += " " + tx_out.ToString() + "\n";
117  return str;
118 }
static const int SERIALIZE_TRANSACTION_NO_WITNESS
A flag that is ORed into the protocol version to designate that a transaction should be (un)serialize...
Definition: transaction.h:23
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1164
std::vector< CTxIn > vin
Definition: transaction.h:355
static const CAmount COIN
Definition: amount.h:14
bool MoneyRange(const CAmount &nValue)
Definition: amount.h:26
std::string ToString() const
Definition: transaction.cpp:33
std::string ToString() const
Definition: transaction.cpp:14
std::string ToString() const
Definition: transaction.cpp:54
const uint256 hash
Memory only.
Definition: transaction.h:283
const std::vector< CTxIn > vin
Definition: transaction.h:276
CAmount GetValueOut() const
Definition: transaction.cpp:85
size_t GetSerializeSize(const T &t, int nVersion=0)
Definition: serialize.h:1116
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:192
const uint256 & GetHash() const
Definition: transaction.h:311
uint32_t n
Definition: transaction.h:30
const std::vector< CTxOut > vout
Definition: transaction.h:277
std::string ToString() const
Definition: uint256.cpp:64
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:26
std::vector< CTxOut > vout
Definition: transaction.h:356
bool HasWitness() const
Definition: transaction.h:384
const uint256 m_witness_hash
Definition: transaction.h:284
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
uint256 ComputeHash() const
Definition: transaction.cpp:67
uint256 ComputeWitnessHash() const
Definition: transaction.cpp:72
256-bit opaque blob.
Definition: uint256.h:124
const int32_t nVersion
Definition: transaction.h:278
uint256 GetHash() const
Compute the hash of this CMutableTransaction.
Definition: transaction.cpp:62
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:404
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:12
CTransaction()
Construct a CTransaction that qualifies as IsNull()
Definition: transaction.cpp:81
std::string ToString() const
A mutable version of CTransaction.
Definition: transaction.h:353
unsigned int GetTotalSize() const
Get the total transaction size in bytes, including witness data.
Definition: transaction.cpp:97
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:259
const uint32_t nLockTime
Definition: transaction.h:279
uint256 hash
Definition: transaction.h:29