Bitcoin Core  24.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-2022 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 <consensus/amount.h>
9 #include <hash.h>
10 #include <script/script.h>
11 #include <serialize.h>
12 #include <tinyformat.h>
13 #include <uint256.h>
14 #include <util/strencodings.h>
15 #include <version.h>
16 
17 #include <cassert>
18 #include <stdexcept>
19 
20 std::string COutPoint::ToString() const
21 {
22  return strprintf("COutPoint(%s, %u)", hash.ToString().substr(0,10), n);
23 }
24 
25 CTxIn::CTxIn(COutPoint prevoutIn, CScript scriptSigIn, uint32_t nSequenceIn)
26 {
27  prevout = prevoutIn;
28  scriptSig = scriptSigIn;
29  nSequence = nSequenceIn;
30 }
31 
32 CTxIn::CTxIn(uint256 hashPrevTx, uint32_t nOut, CScript scriptSigIn, uint32_t nSequenceIn)
33 {
34  prevout = COutPoint(hashPrevTx, nOut);
35  scriptSig = scriptSigIn;
36  nSequence = nSequenceIn;
37 }
38 
39 std::string CTxIn::ToString() const
40 {
41  std::string str;
42  str += "CTxIn(";
43  str += prevout.ToString();
44  if (prevout.IsNull())
45  str += strprintf(", coinbase %s", HexStr(scriptSig));
46  else
47  str += strprintf(", scriptSig=%s", HexStr(scriptSig).substr(0, 24));
49  str += strprintf(", nSequence=%u", nSequence);
50  str += ")";
51  return str;
52 }
53 
54 CTxOut::CTxOut(const CAmount& nValueIn, CScript scriptPubKeyIn)
55 {
56  nValue = nValueIn;
57  scriptPubKey = scriptPubKeyIn;
58 }
59 
60 std::string CTxOut::ToString() const
61 {
62  return strprintf("CTxOut(nValue=%d.%08d, scriptPubKey=%s)", nValue / COIN, nValue % COIN, HexStr(scriptPubKey).substr(0, 30));
63 }
64 
65 CMutableTransaction::CMutableTransaction() : nVersion(CTransaction::CURRENT_VERSION), nLockTime(0) {}
66 CMutableTransaction::CMutableTransaction(const CTransaction& tx) : vin(tx.vin), vout(tx.vout), nVersion(tx.nVersion), nLockTime(tx.nLockTime) {}
67 
69 {
71 }
72 
74 {
76 }
77 
79 {
80  if (!HasWitness()) {
81  return hash;
82  }
83  return SerializeHash(*this, SER_GETHASH, 0);
84 }
85 
86 CTransaction::CTransaction(const CMutableTransaction& tx) : vin(tx.vin), vout(tx.vout), nVersion(tx.nVersion), nLockTime(tx.nLockTime), hash{ComputeHash()}, m_witness_hash{ComputeWitnessHash()} {}
87 CTransaction::CTransaction(CMutableTransaction&& tx) : vin(std::move(tx.vin)), vout(std::move(tx.vout)), nVersion(tx.nVersion), nLockTime(tx.nLockTime), hash{ComputeHash()}, m_witness_hash{ComputeWitnessHash()} {}
88 
90 {
91  CAmount nValueOut = 0;
92  for (const auto& tx_out : vout) {
93  if (!MoneyRange(tx_out.nValue) || !MoneyRange(nValueOut + tx_out.nValue))
94  throw std::runtime_error(std::string(__func__) + ": value out of range");
95  nValueOut += tx_out.nValue;
96  }
97  assert(MoneyRange(nValueOut));
98  return nValueOut;
99 }
100 
101 unsigned int CTransaction::GetTotalSize() const
102 {
104 }
105 
106 std::string CTransaction::ToString() const
107 {
108  std::string str;
109  str += strprintf("CTransaction(hash=%s, ver=%d, vin.size=%u, vout.size=%u, nLockTime=%u)\n",
110  GetHash().ToString().substr(0,10),
111  nVersion,
112  vin.size(),
113  vout.size(),
114  nLockTime);
115  for (const auto& tx_in : vin)
116  str += " " + tx_in.ToString() + "\n";
117  for (const auto& tx_in : vin)
118  str += " " + tx_in.scriptWitness.ToString() + "\n";
119  for (const auto& tx_out : vout)
120  str += " " + tx_out.ToString() + "\n";
121  return str;
122 }
bool MoneyRange(const CAmount &nValue)
Definition: amount.h:27
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
static constexpr CAmount COIN
The amount of satoshis in one BTC.
Definition: amount.h:15
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:36
uint32_t n
Definition: transaction.h:39
std::string ToString() const
Definition: transaction.cpp:20
bool IsNull() const
Definition: transaction.h:49
uint256 hash
Definition: transaction.h:38
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:411
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:295
uint256 ComputeWitnessHash() const
Definition: transaction.cpp:78
bool HasWitness() const
Definition: transaction.h:367
const uint32_t nLockTime
Definition: transaction.h:308
CTransaction(const CMutableTransaction &tx)
Convert a CMutableTransaction into a CTransaction.
Definition: transaction.cpp:86
const std::vector< CTxOut > vout
Definition: transaction.h:306
uint256 ComputeHash() const
Definition: transaction.cpp:73
const uint256 & GetHash() const
Definition: transaction.h:337
std::string ToString() const
unsigned int GetTotalSize() const
Get the total transaction size in bytes, including witness data.
CAmount GetValueOut() const
Definition: transaction.cpp:89
const int32_t nVersion
Definition: transaction.h:307
const std::vector< CTxIn > vin
Definition: transaction.h:305
const uint256 hash
Memory only.
Definition: transaction.h:312
uint32_t nSequence
Definition: transaction.h:79
static const uint32_t SEQUENCE_FINAL
Setting nSequence to this value for every input in a transaction disables nLockTime/IsFinalTx().
Definition: transaction.h:89
std::string ToString() const
Definition: transaction.cpp:39
CScript scriptSig
Definition: transaction.h:78
COutPoint prevout
Definition: transaction.h:77
CScript scriptPubKey
Definition: transaction.h:161
CAmount nValue
Definition: transaction.h:160
std::string ToString() const
Definition: transaction.cpp:60
std::string ToString() const
Definition: uint256.cpp:55
256-bit opaque blob.
Definition: uint256.h:105
uint256 SerializeHash(const T &obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
Compute the 256-bit hash of an object's serialization.
Definition: hash.h:263
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:32
@ SER_GETHASH
Definition: serialize.h:133
size_t GetSerializeSize(const T &t, int nVersion=0)
Definition: serialize.h:1109
A mutable version of CTransaction.
Definition: transaction.h:380
uint256 GetHash() const
Compute the hash of this CMutableTransaction.
Definition: transaction.cpp:68
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1162
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
assert(!tx.IsCoinBase())
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:12