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