Bitcoin ABC 0.30.5
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 <consensus/amount.h>
9#include <hash.h>
10#include <tinyformat.h>
11#include <util/strencodings.h>
12
13#include <cassert>
14
15std::string COutPoint::ToString() const {
16 return strprintf("COutPoint(%s, %u)", txid.ToString().substr(0, 10), n);
17}
18
19std::string CTxIn::ToString() const {
20 std::string str;
21 str += "CTxIn(";
22 str += prevout.ToString();
23 if (prevout.IsNull()) {
24 str += strprintf(", coinbase %s", HexStr(scriptSig));
25 } else {
26 str += strprintf(", scriptSig=%s", HexStr(scriptSig).substr(0, 24));
27 }
29 str += strprintf(", nSequence=%u", nSequence);
30 }
31 str += ")";
32 return str;
33}
34
35std::string CTxOut::ToString() const {
36 const Amount xec = 100 * SATOSHI;
37 return strprintf("CTxOut(nValue=%d.%02d, scriptPubKey=%s)", nValue / xec,
38 (nValue % xec) / SATOSHI,
39 HexStr(scriptPubKey).substr(0, 30));
40}
41
43 : nVersion(CTransaction::CURRENT_VERSION), nLockTime(0) {}
45 : vin(tx.vin), vout(tx.vout), nVersion(tx.nVersion),
46 nLockTime(tx.nLockTime) {}
47
49 return SerializeHash(tx, SER_GETHASH, 0);
50}
51
54}
55
58}
59
61 return SerializeHash(*this, SER_GETHASH, 0);
62}
63
69 : vin(), vout(), nVersion(CTransaction::CURRENT_VERSION), nLockTime(0),
70 hash() {}
72 : vin(tx.vin), vout(tx.vout), nVersion(tx.nVersion),
73 nLockTime(tx.nLockTime), hash(ComputeHash()) {}
75 : vin(std::move(tx.vin)), vout(std::move(tx.vout)), nVersion(tx.nVersion),
76 nLockTime(tx.nLockTime), hash(ComputeHash()) {}
77
79 Amount nValueOut = Amount::zero();
80 for (const auto &tx_out : vout) {
81 if (!MoneyRange(tx_out.nValue) ||
82 !MoneyRange(nValueOut + tx_out.nValue)) {
83 throw std::runtime_error(std::string(__func__) +
84 ": value out of range");
85 }
86 nValueOut += tx_out.nValue;
87 }
88 assert(MoneyRange(nValueOut));
89 return nValueOut;
90}
91
92unsigned int CTransaction::GetTotalSize() const {
94}
95
96std::string CTransaction::ToString() const {
97 std::string str;
98 str += strprintf("CTransaction(txid=%s, ver=%d, vin.size=%u, vout.size=%u, "
99 "nLockTime=%u)\n",
100 GetId().ToString().substr(0, 10), nVersion, vin.size(),
101 vout.size(), nLockTime);
102 for (const auto &nVin : vin) {
103 str += " " + nVin.ToString() + "\n";
104 }
105 for (const auto &nVout : vout) {
106 str += " " + nVout.ToString() + "\n";
107 }
108 return str;
109}
bool MoneyRange(const Amount nValue)
Definition: amount.h:166
static constexpr Amount SATOSHI
Definition: amount.h:143
A mutable version of CTransaction.
Definition: transaction.h:274
TxHash GetHash() const
Definition: transaction.cpp:56
TxId GetId() const
Compute the id and hash of this CMutableTransaction.
Definition: transaction.cpp:52
uint32_t n
Definition: transaction.h:23
TxId txid
Definition: transaction.h:22
std::string ToString() const
Definition: transaction.cpp:15
bool IsNull() const
Definition: transaction.h:33
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:192
CTransaction()
Construct a CTransaction that qualifies as IsNull()
Definition: transaction.cpp:68
const uint32_t nLockTime
Definition: transaction.h:209
const std::vector< CTxOut > vout
Definition: transaction.h:207
uint256 ComputeHash() const
Definition: transaction.cpp:60
std::string ToString() const
Definition: transaction.cpp:96
unsigned int GetTotalSize() const
Get the total transaction size in bytes.
Definition: transaction.cpp:92
Amount GetValueOut() const
Definition: transaction.cpp:78
const int32_t nVersion
Definition: transaction.h:208
const std::vector< CTxIn > vin
Definition: transaction.h:206
const TxId GetId() const
Definition: transaction.h:240
uint32_t nSequence
Definition: transaction.h:63
static const uint32_t SEQUENCE_FINAL
Setting nSequence to this value for every input in a transaction disables nLockTime.
Definition: transaction.h:69
std::string ToString() const
Definition: transaction.cpp:19
CScript scriptSig
Definition: transaction.h:62
COutPoint prevout
Definition: transaction.h:61
CScript scriptPubKey
Definition: transaction.h:131
Amount nValue
Definition: transaction.h:130
std::string ToString() const
Definition: transaction.cpp:35
std::string ToString() const
Definition: uint256.h:80
256-bit opaque blob.
Definition: uint256.h:129
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:201
Implement std::hash so RCUPtr can be used as a key for maps or sets.
Definition: rcu.h:259
static uint256 ComputeCMutableTransactionHash(const CMutableTransaction &tx)
Definition: transaction.cpp:48
@ SER_GETHASH
Definition: serialize.h:154
size_t GetSerializeSize(const T &t, int nVersion=0)
Definition: serialize.h:1258
Definition: amount.h:19
static constexpr Amount zero() noexcept
Definition: amount.h:32
A TxHash is the double sha256 hash of the full transaction data.
Definition: txid.h:22
A TxId is the identifier of a transaction.
Definition: txid.h:14
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1202
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
assert(!tx.IsCoinBase())
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:11