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 <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));
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) {}
60 CMutableTransaction::CMutableTransaction(const CTransaction& tx) : vin(tx.vin), vout(tx.vout), nVersion(tx.nVersion), nLockTime(tx.nLockTime) {}
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 CTransaction::CTransaction(const CMutableTransaction& tx) : vin(tx.vin), vout(tx.vout), nVersion(tx.nVersion), nLockTime(tx.nLockTime), hash{ComputeHash()}, m_witness_hash{ComputeWitnessHash()} {}
81 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()} {}
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 }
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:83
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:80
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:95
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:404
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:100
CTransaction::ComputeWitnessHash
uint256 ComputeWitnessHash() const
Definition: transaction.cpp:72
CTxIn::ToString
std::string ToString() const
Definition: transaction.cpp:33
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:54
COIN
static const CAmount COIN
Definition: amount.h:14
COutPoint::ToString
std::string ToString() const
Definition: transaction.cpp:14
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:62
CTransaction::nVersion
const int32_t nVersion
Definition: transaction.h:272
CMutableTransaction::CMutableTransaction
CMutableTransaction()
Definition: transaction.cpp:59
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:594
SER_GETHASH
@ SER_GETHASH
Definition: serialize.h:140
CTransaction::ComputeHash
uint256 ComputeHash() const
Definition: transaction.cpp:67
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