Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
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
51
55
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
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 }
89 return nValueOut;
90}
91
92unsigned int CTransaction::GetTotalSize() const {
93 return ::GetSerializeSize(*this, PROTOCOL_VERSION);
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.
TxHash GetHash() const
TxId GetId() const
Compute the id and hash of this CMutableTransaction.
uint32_t n
Definition transaction.h:23
std::string ToString() const
bool IsNull() const
Definition transaction.h:33
The basic transaction that is broadcasted on the network and contained in blocks.
CTransaction()
Construct a CTransaction that qualifies as IsNull()
const uint32_t nLockTime
const std::vector< CTxOut > vout
uint256 ComputeHash() const
std::string ToString() const
unsigned int GetTotalSize() const
Get the total transaction size in bytes.
Amount GetValueOut() const
const int32_t nVersion
const std::vector< CTxIn > vin
const TxId GetId() const
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
CScript scriptSig
Definition transaction.h:62
COutPoint prevout
Definition transaction.h:61
CScript scriptPubKey
Amount nValue
std::string ToString() const
unsigned int size() const
Definition uint256.h:93
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)
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Definition random.h:85
@ SER_GETHASH
Definition serialize.h:154
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...
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