Bitcoin Core  27.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>
16 
17 #include <algorithm>
18 #include <cassert>
19 #include <stdexcept>
20 
21 std::string COutPoint::ToString() const
22 {
23  return strprintf("COutPoint(%s, %u)", hash.ToString().substr(0,10), n);
24 }
25 
26 CTxIn::CTxIn(COutPoint prevoutIn, CScript scriptSigIn, uint32_t nSequenceIn)
27 {
28  prevout = prevoutIn;
29  scriptSig = scriptSigIn;
30  nSequence = nSequenceIn;
31 }
32 
33 CTxIn::CTxIn(Txid hashPrevTx, uint32_t nOut, CScript scriptSigIn, uint32_t nSequenceIn)
34 {
35  prevout = COutPoint(hashPrevTx, nOut);
36  scriptSig = scriptSigIn;
37  nSequence = nSequenceIn;
38 }
39 
40 std::string CTxIn::ToString() const
41 {
42  std::string str;
43  str += "CTxIn(";
44  str += prevout.ToString();
45  if (prevout.IsNull())
46  str += strprintf(", coinbase %s", HexStr(scriptSig));
47  else
48  str += strprintf(", scriptSig=%s", HexStr(scriptSig).substr(0, 24));
50  str += strprintf(", nSequence=%u", nSequence);
51  str += ")";
52  return str;
53 }
54 
55 CTxOut::CTxOut(const CAmount& nValueIn, CScript scriptPubKeyIn)
56 {
57  nValue = nValueIn;
58  scriptPubKey = scriptPubKeyIn;
59 }
60 
61 std::string CTxOut::ToString() const
62 {
63  return strprintf("CTxOut(nValue=%d.%08d, scriptPubKey=%s)", nValue / COIN, nValue % COIN, HexStr(scriptPubKey).substr(0, 30));
64 }
65 
66 CMutableTransaction::CMutableTransaction() : nVersion(CTransaction::CURRENT_VERSION), nLockTime(0) {}
67 CMutableTransaction::CMutableTransaction(const CTransaction& tx) : vin(tx.vin), vout(tx.vout), nVersion(tx.nVersion), nLockTime(tx.nLockTime) {}
68 
70 {
71  return Txid::FromUint256((HashWriter{} << TX_NO_WITNESS(*this)).GetHash());
72 }
73 
75 {
76  return std::any_of(vin.begin(), vin.end(), [](const auto& input) {
77  return !input.scriptWitness.IsNull();
78  });
79 }
80 
82 {
83  return Txid::FromUint256((HashWriter{} << TX_NO_WITNESS(*this)).GetHash());
84 }
85 
87 {
88  if (!HasWitness()) {
90  }
91 
92  return Wtxid::FromUint256((HashWriter{} << TX_WITH_WITNESS(*this)).GetHash());
93 }
94 
95 CTransaction::CTransaction(const CMutableTransaction& tx) : vin(tx.vin), vout(tx.vout), nVersion(tx.nVersion), nLockTime(tx.nLockTime), m_has_witness{ComputeHasWitness()}, hash{ComputeHash()}, m_witness_hash{ComputeWitnessHash()} {}
96 CTransaction::CTransaction(CMutableTransaction&& tx) : vin(std::move(tx.vin)), vout(std::move(tx.vout)), nVersion(tx.nVersion), nLockTime(tx.nLockTime), m_has_witness{ComputeHasWitness()}, hash{ComputeHash()}, m_witness_hash{ComputeWitnessHash()} {}
97 
99 {
100  CAmount nValueOut = 0;
101  for (const auto& tx_out : vout) {
102  if (!MoneyRange(tx_out.nValue) || !MoneyRange(nValueOut + tx_out.nValue))
103  throw std::runtime_error(std::string(__func__) + ": value out of range");
104  nValueOut += tx_out.nValue;
105  }
106  assert(MoneyRange(nValueOut));
107  return nValueOut;
108 }
109 
110 unsigned int CTransaction::GetTotalSize() const
111 {
113 }
114 
115 std::string CTransaction::ToString() const
116 {
117  std::string str;
118  str += strprintf("CTransaction(hash=%s, ver=%d, vin.size=%u, vout.size=%u, nLockTime=%u)\n",
119  GetHash().ToString().substr(0,10),
120  nVersion,
121  vin.size(),
122  vout.size(),
123  nLockTime);
124  for (const auto& tx_in : vin)
125  str += " " + tx_in.ToString() + "\n";
126  for (const auto& tx_in : vin)
127  str += " " + tx_in.scriptWitness.ToString() + "\n";
128  for (const auto& tx_out : vout)
129  str += " " + tx_out.ToString() + "\n";
130  return str;
131 }
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:29
uint32_t n
Definition: transaction.h:32
Txid hash
Definition: transaction.h:31
std::string ToString() const
Definition: transaction.cpp:21
bool IsNull() const
Definition: transaction.h:42
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:414
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:296
Txid ComputeHash() const
Definition: transaction.cpp:81
bool HasWitness() const
Definition: transaction.h:373
const Txid & GetHash() const LIFETIMEBOUND
Definition: transaction.h:343
const uint32_t nLockTime
Definition: transaction.h:309
bool ComputeHasWitness() const
Definition: transaction.cpp:74
CTransaction(const CMutableTransaction &tx)
Convert a CMutableTransaction into a CTransaction.
Definition: transaction.cpp:95
const std::vector< CTxOut > vout
Definition: transaction.h:307
std::string ToString() const
unsigned int GetTotalSize() const
Get the total transaction size in bytes, including witness data.
Wtxid ComputeWitnessHash() const
Definition: transaction.cpp:86
CAmount GetValueOut() const
Definition: transaction.cpp:98
const int32_t nVersion
Definition: transaction.h:308
const std::vector< CTxIn > vin
Definition: transaction.h:306
const Txid hash
Definition: transaction.h:314
uint32_t nSequence
Definition: transaction.h:71
static const uint32_t SEQUENCE_FINAL
Setting nSequence to this value for every input in a transaction disables nLockTime/IsFinalTx().
Definition: transaction.h:81
std::string ToString() const
Definition: transaction.cpp:40
CScript scriptSig
Definition: transaction.h:70
COutPoint prevout
Definition: transaction.h:69
CScript scriptPubKey
Definition: transaction.h:153
CAmount nValue
Definition: transaction.h:152
std::string ToString() const
Definition: transaction.cpp:61
A writer stream (for serialization) that computes a 256-bit hash.
Definition: hash.h:101
std::string ToString() const
const uint256 & ToUint256() const LIFETIMEBOUND
static transaction_identifier FromUint256(const uint256 &id)
static constexpr TransactionSerParams TX_NO_WITNESS
Definition: transaction.h:196
static constexpr TransactionSerParams TX_WITH_WITNESS
Definition: transaction.h:195
size_t GetSerializeSize(const T &t)
Definition: serialize.h:1116
A mutable version of CTransaction.
Definition: transaction.h:378
Txid GetHash() const
Compute the hash of this CMutableTransaction.
Definition: transaction.cpp:69
#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())