Bitcoin ABC  0.24.11
P2P Digital Currency
transaction.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 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 <cassert>
13 
14 std::string COutPoint::ToString() const {
15  return strprintf("COutPoint(%s, %u)", txid.ToString().substr(0, 10), n);
16 }
17 
18 std::string CTxIn::ToString() const {
19  std::string str;
20  str += "CTxIn(";
21  str += prevout.ToString();
22  if (prevout.IsNull()) {
23  str += strprintf(", coinbase %s", HexStr(scriptSig));
24  } else {
25  str += strprintf(", scriptSig=%s", HexStr(scriptSig).substr(0, 24));
26  }
27  if (nSequence != SEQUENCE_FINAL) {
28  str += strprintf(", nSequence=%u", nSequence);
29  }
30  str += ")";
31  return str;
32 }
33 
34 std::string CTxOut::ToString() const {
35  return strprintf("CTxOut(nValue=%d.%08d, scriptPubKey=%s)", nValue / COIN,
36  (nValue % COIN) / SATOSHI,
37  HexStr(scriptPubKey).substr(0, 30));
38 }
39 
41  : nVersion(CTransaction::CURRENT_VERSION), nLockTime(0) {}
43  : vin(tx.vin), vout(tx.vout), nVersion(tx.nVersion),
44  nLockTime(tx.nLockTime) {}
45 
47  return SerializeHash(tx, SER_GETHASH, 0);
48 }
49 
51  return TxId(ComputeCMutableTransactionHash(*this));
52 }
53 
56 }
57 
59  return SerializeHash(*this, SER_GETHASH, 0);
60 }
61 
67  : vin(), vout(), nVersion(CTransaction::CURRENT_VERSION), nLockTime(0),
68  hash() {}
70  : vin(tx.vin), vout(tx.vout), nVersion(tx.nVersion),
71  nLockTime(tx.nLockTime), hash(ComputeHash()) {}
73  : vin(std::move(tx.vin)), vout(std::move(tx.vout)), nVersion(tx.nVersion),
74  nLockTime(tx.nLockTime), hash(ComputeHash()) {}
75 
77  Amount nValueOut = Amount::zero();
78  for (const auto &tx_out : vout) {
79  if (!MoneyRange(tx_out.nValue) ||
80  !MoneyRange(nValueOut + tx_out.nValue)) {
81  throw std::runtime_error(std::string(__func__) +
82  ": value out of range");
83  }
84  nValueOut += tx_out.nValue;
85  }
86  assert(MoneyRange(nValueOut));
87  return nValueOut;
88 }
89 
90 unsigned int CTransaction::GetTotalSize() const {
92 }
93 
94 std::string CTransaction::ToString() const {
95  std::string str;
96  str += strprintf("CTransaction(txid=%s, ver=%d, vin.size=%u, vout.size=%u, "
97  "nLockTime=%u)\n",
98  GetId().ToString().substr(0, 10), nVersion, vin.size(),
99  vout.size(), nLockTime);
100  for (const auto &nVin : vin) {
101  str += " " + nVin.ToString() + "\n";
102  }
103  for (const auto &nVout : vout) {
104  str += " " + nVout.ToString() + "\n";
105  }
106  return str;
107 }
GetSerializeSize
size_t GetSerializeSize(const T &t, int nVersion=0)
Definition: serialize.h:1182
CTransaction::vin
const std::vector< CTxIn > vin
Definition: transaction.h:210
MoneyRange
bool MoneyRange(const Amount nValue)
Definition: amount.h:176
CTxOut::nValue
Amount nValue
Definition: transaction.h:132
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:175
transaction.h
ComputeCMutableTransactionHash
static uint256 ComputeCMutableTransactionHash(const CMutableTransaction &tx)
Definition: transaction.cpp:46
CTransaction::nLockTime
const uint32_t nLockTime
Definition: transaction.h:213
CMutableTransaction::CMutableTransaction
CMutableTransaction()
Definition: transaction.cpp:40
COIN
static constexpr Amount COIN
Definition: amount.h:154
CTransaction
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:194
tinyformat.h
CTxIn::SEQUENCE_FINAL
static const uint32_t SEQUENCE_FINAL
Setting nSequence to this value for every input in a transaction disables nLockTime.
Definition: transaction.h:71
Amount::zero
static constexpr Amount zero()
Definition: amount.h:42
COutPoint::IsNull
bool IsNull() const
Definition: transaction.h:35
CTxIn::nSequence
uint32_t nSequence
Definition: transaction.h:65
SATOSHI
static constexpr Amount SATOSHI
Definition: amount.h:153
strencodings.h
TxHash
A TxHash is the double sha256 hash of the full transaction data.
Definition: txid.h:22
CTransaction::GetId
const TxId GetId() const
Definition: transaction.h:244
COutPoint::txid
TxId txid
Definition: transaction.h:24
CTransaction::vout
const std::vector< CTxOut > vout
Definition: transaction.h:211
CTxOut::scriptPubKey
CScript scriptPubKey
Definition: transaction.h:133
CTransaction::GetTotalSize
unsigned int GetTotalSize() const
Get the total transaction size in bytes.
Definition: transaction.cpp:90
base_blob::ToString
std::string ToString() const
Definition: uint256.h:78
uint256
256-bit opaque blob.
Definition: uint256.h:127
TxId
A TxId is the identifier of a transaction.
Definition: txid.h:14
CTransaction::CTransaction
CTransaction()
Construct a CTransaction that qualifies as IsNull()
Definition: transaction.cpp:66
CMutableTransaction::GetId
TxId GetId() const
Compute the id and hash of this CMutableTransaction.
Definition: transaction.cpp:50
Amount
Definition: amount.h:19
CTransaction::GetValueOut
Amount GetValueOut() const
Definition: transaction.cpp:76
strprintf
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1201
SER_GETHASH
@ SER_GETHASH
Definition: serialize.h:167
CTransaction::ToString
std::string ToString() const
Definition: transaction.cpp:94
CTxIn::ToString
std::string ToString() const
Definition: transaction.cpp:18
COutPoint::n
uint32_t n
Definition: transaction.h:25
CMutableTransaction::GetHash
TxHash GetHash() const
Definition: transaction.cpp:54
CTxIn::prevout
COutPoint prevout
Definition: transaction.h:63
CTxIn::scriptSig
CScript scriptSig
Definition: transaction.h:64
CTxOut::ToString
std::string ToString() const
Definition: transaction.cpp:34
COutPoint::ToString
std::string ToString() const
Definition: transaction.cpp:14
CTransaction::nVersion
const int32_t nVersion
Definition: transaction.h:212
HexStr
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
Definition: strencodings.cpp:656
CTransaction::ComputeHash
uint256 ComputeHash() const
Definition: transaction.cpp:58
CMutableTransaction
A mutable version of CTransaction.
Definition: transaction.h:278
PROTOCOL_VERSION
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:11