Bitcoin Core  21.99.0
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 
82 
84 {
85  CAmount nValueOut = 0;
86  for (const auto& tx_out : vout) {
87  if (!MoneyRange(tx_out.nValue) || !MoneyRange(nValueOut + tx_out.nValue))
88  throw std::runtime_error(std::string(__func__) + ": value out of range");
89  nValueOut += tx_out.nValue;
90  }
91  assert(MoneyRange(nValueOut));
92  return nValueOut;
93 }
94 
95 unsigned int CTransaction::GetTotalSize() const
96 {
98 }
99 
100 std::string CTransaction::ToString() const
101 {
102  std::string str;
103  str += strprintf("CTransaction(hash=%s, ver=%d, vin.size=%u, vout.size=%u, nLockTime=%u)\n",
104  GetHash().ToString().substr(0,10),
105  nVersion,
106  vin.size(),
107  vout.size(),
108  nLockTime);
109  for (const auto& tx_in : vin)
110  str += " " + tx_in.ToString() + "\n";
111  for (const auto& tx_in : vin)
112  str += " " + tx_in.scriptWitness.ToString() + "\n";
113  for (const auto& tx_out : vout)
114  str += " " + tx_out.ToString() + "\n";
115  return str;
116 }
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
assert(!tx.IsCoinBase())
#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:346
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:277
const std::vector< CTxIn > vin
Definition: transaction.h:270
CAmount GetValueOut() const
Definition: transaction.cpp:83
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:302
uint32_t n
Definition: transaction.h:30
const std::vector< CTxOut > vout
Definition: transaction.h:271
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:347
bool HasWitness() const
Definition: transaction.h:375
const uint256 m_witness_hash
Definition: transaction.h:278
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:272
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
std::string ToString() const
A mutable version of CTransaction.
Definition: transaction.h:344
unsigned int GetTotalSize() const
Get the total transaction size in bytes, including witness data.
Definition: transaction.cpp:95
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:259
const uint32_t nLockTime
Definition: transaction.h:273
CTransaction(const CMutableTransaction &tx)
Convert a CMutableTransaction into a CTransaction.
Definition: transaction.cpp:80
uint256 hash
Definition: transaction.h:29