Bitcoin ABC  0.22.13
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 std::string COutPoint::ToString() const {
13  return strprintf("COutPoint(%s, %u)", txid.ToString().substr(0, 10), n);
14 }
15 
16 std::string CTxIn::ToString() const {
17  std::string str;
18  str += "CTxIn(";
19  str += prevout.ToString();
20  if (prevout.IsNull()) {
21  str += strprintf(", coinbase %s", HexStr(scriptSig));
22  } else {
23  str += strprintf(", scriptSig=%s", HexStr(scriptSig).substr(0, 24));
24  }
25  if (nSequence != SEQUENCE_FINAL) {
26  str += strprintf(", nSequence=%u", nSequence);
27  }
28  str += ")";
29  return str;
30 }
31 
32 std::string CTxOut::ToString() const {
33  return strprintf("CTxOut(nValue=%d.%08d, scriptPubKey=%s)", nValue / COIN,
34  (nValue % COIN) / SATOSHI,
35  HexStr(scriptPubKey).substr(0, 30));
36 }
37 
39  : nVersion(CTransaction::CURRENT_VERSION), nLockTime(0) {}
41  : vin(tx.vin), vout(tx.vout), nVersion(tx.nVersion),
42  nLockTime(tx.nLockTime) {}
43 
45  return SerializeHash(tx, SER_GETHASH, 0);
46 }
47 
49  return TxId(ComputeCMutableTransactionHash(*this));
50 }
51 
54 }
55 
57  return SerializeHash(*this, SER_GETHASH, 0);
58 }
59 
65  : vin(), vout(), nVersion(CTransaction::CURRENT_VERSION), nLockTime(0),
66  hash() {}
68  : vin(tx.vin), vout(tx.vout), nVersion(tx.nVersion),
71  : vin(std::move(tx.vin)), vout(std::move(tx.vout)), nVersion(tx.nVersion),
73 
75  Amount nValueOut = Amount::zero();
76  for (const auto &tx_out : vout) {
77  nValueOut += tx_out.nValue;
78  if (!MoneyRange(tx_out.nValue) || !MoneyRange(nValueOut)) {
79  throw std::runtime_error(std::string(__func__) +
80  ": value out of range");
81  }
82  }
83  return nValueOut;
84 }
85 
86 unsigned int CTransaction::GetTotalSize() const {
88 }
89 
90 std::string CTransaction::ToString() const {
91  std::string str;
92  str += strprintf("CTransaction(txid=%s, ver=%d, vin.size=%u, vout.size=%u, "
93  "nLockTime=%u)\n",
94  GetId().ToString().substr(0, 10), nVersion, vin.size(),
95  vout.size(), nLockTime);
96  for (const auto &nVin : vin) {
97  str += " " + nVin.ToString() + "\n";
98  }
99  for (const auto &nVout : vout) {
100  str += " " + nVout.ToString() + "\n";
101  }
102  return str;
103 }
static constexpr Amount zero()
Definition: amount.h:35
TxId GetId() const
Compute the id and hash of this CMutableTransaction.
Definition: transaction.cpp:48
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1201
static uint256 ComputeCMutableTransactionHash(const CMutableTransaction &tx)
Definition: transaction.cpp:44
Amount GetValueOut() const
Definition: transaction.cpp:74
Definition: amount.h:17
TxId txid
Definition: transaction.h:24
static constexpr Amount SATOSHI
Definition: amount.h:151
std::vector< CTxOut > vout
Definition: transaction.h:298
std::string ToString() const
Definition: transaction.cpp:16
std::string ToString() const
Definition: transaction.cpp:12
std::string ToString() const
Definition: transaction.cpp:32
TxHash GetHash() const
Definition: transaction.cpp:52
const uint256 hash
Memory only.
Definition: transaction.h:234
const std::vector< CTxIn > vin
Definition: transaction.h:227
size_t GetSerializeSize(const T &t, int nVersion=0)
Definition: serialize.h:1193
std::vector< CTxIn > vin
Definition: transaction.h:297
bool MoneyRange(const Amount nValue)
Definition: amount.h:166
uint256 SerializeHash(const T &obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
Compute the 256-bit hash of an object&#39;s serialization.
Definition: hash.h:196
static constexpr Amount COIN
Definition: amount.h:153
A TxHash is the double sha256 hash of the full transaction data.
Definition: txid.h:22
uint32_t n
Definition: transaction.h:25
const std::vector< CTxOut > vout
Definition: transaction.h:228
std::string ToString() const
Definition: uint256.h:74
uint256 ComputeHash() const
Definition: transaction.cpp:56
256-bit opaque blob.
Definition: uint256.h:120
const int32_t nVersion
Definition: transaction.h:229
A TxId is the identifier of a transaction.
Definition: txid.h:14
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:11
CTransaction()
Construct a CTransaction that qualifies as IsNull()
Definition: transaction.cpp:64
std::string ToString() const
Definition: transaction.cpp:90
A mutable version of CTransaction.
Definition: transaction.h:295
std::string HexStr(const T itbegin, const T itend)
Definition: strencodings.h:132
unsigned int GetTotalSize() const
Get the total transaction size in bytes.
Definition: transaction.cpp:86
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:211
const TxId GetId() const
Definition: transaction.h:261
const uint32_t nLockTime
Definition: transaction.h:230