Bitcoin Core 28.99.0
P2P Digital Currency
transaction.h
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-2022 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
6#ifndef BITCOIN_PRIMITIVES_TRANSACTION_H
7#define BITCOIN_PRIMITIVES_TRANSACTION_H
8
9#include <attributes.h>
10#include <consensus/amount.h>
11#include <script/script.h>
12#include <serialize.h>
13#include <uint256.h>
14#include <util/transaction_identifier.h> // IWYU pragma: export
15
16#include <cstddef>
17#include <cstdint>
18#include <ios>
19#include <limits>
20#include <memory>
21#include <numeric>
22#include <string>
23#include <tuple>
24#include <utility>
25#include <vector>
26
29{
30public:
32 uint32_t n;
33
34 static constexpr uint32_t NULL_INDEX = std::numeric_limits<uint32_t>::max();
35
37 COutPoint(const Txid& hashIn, uint32_t nIn): hash(hashIn), n(nIn) { }
38
39 SERIALIZE_METHODS(COutPoint, obj) { READWRITE(obj.hash, obj.n); }
40
41 void SetNull() { hash.SetNull(); n = NULL_INDEX; }
42 bool IsNull() const { return (hash.IsNull() && n == NULL_INDEX); }
43
44 friend bool operator<(const COutPoint& a, const COutPoint& b)
45 {
46 return std::tie(a.hash, a.n) < std::tie(b.hash, b.n);
47 }
48
49 friend bool operator==(const COutPoint& a, const COutPoint& b)
50 {
51 return (a.hash == b.hash && a.n == b.n);
52 }
53
54 friend bool operator!=(const COutPoint& a, const COutPoint& b)
55 {
56 return !(a == b);
57 }
58
59 std::string ToString() const;
60};
61
66class CTxIn
67{
68public:
71 uint32_t nSequence;
73
81 static const uint32_t SEQUENCE_FINAL = 0xffffffff;
87 static const uint32_t MAX_SEQUENCE_NONFINAL{SEQUENCE_FINAL - 1};
88
89 // Below flags apply in the context of BIP 68. BIP 68 requires the tx
90 // version to be set to 2, or higher.
98 static const uint32_t SEQUENCE_LOCKTIME_DISABLE_FLAG = (1U << 31);
99
104 static const uint32_t SEQUENCE_LOCKTIME_TYPE_FLAG = (1 << 22);
105
109 static const uint32_t SEQUENCE_LOCKTIME_MASK = 0x0000ffff;
110
119 static const int SEQUENCE_LOCKTIME_GRANULARITY = 9;
120
122 {
124 }
125
126 explicit CTxIn(COutPoint prevoutIn, CScript scriptSigIn=CScript(), uint32_t nSequenceIn=SEQUENCE_FINAL);
127 CTxIn(Txid hashPrevTx, uint32_t nOut, CScript scriptSigIn=CScript(), uint32_t nSequenceIn=SEQUENCE_FINAL);
128
129 SERIALIZE_METHODS(CTxIn, obj) { READWRITE(obj.prevout, obj.scriptSig, obj.nSequence); }
130
131 friend bool operator==(const CTxIn& a, const CTxIn& b)
132 {
133 return (a.prevout == b.prevout &&
134 a.scriptSig == b.scriptSig &&
135 a.nSequence == b.nSequence);
136 }
137
138 friend bool operator!=(const CTxIn& a, const CTxIn& b)
139 {
140 return !(a == b);
141 }
142
143 std::string ToString() const;
144};
145
150{
151public:
154
156 {
157 SetNull();
158 }
159
160 CTxOut(const CAmount& nValueIn, CScript scriptPubKeyIn);
161
162 SERIALIZE_METHODS(CTxOut, obj) { READWRITE(obj.nValue, obj.scriptPubKey); }
163
164 void SetNull()
165 {
166 nValue = -1;
168 }
169
170 bool IsNull() const
171 {
172 return (nValue == -1);
173 }
174
175 friend bool operator==(const CTxOut& a, const CTxOut& b)
176 {
177 return (a.nValue == b.nValue &&
179 }
180
181 friend bool operator!=(const CTxOut& a, const CTxOut& b)
182 {
183 return !(a == b);
184 }
185
186 std::string ToString() const;
187};
188
190
192 const bool allow_witness;
194};
197
215template<typename Stream, typename TxType>
216void UnserializeTransaction(TxType& tx, Stream& s, const TransactionSerParams& params)
217{
218 const bool fAllowWitness = params.allow_witness;
219
220 s >> tx.version;
221 unsigned char flags = 0;
222 tx.vin.clear();
223 tx.vout.clear();
224 /* Try to read the vin. In case the dummy is there, this will be read as an empty vector. */
225 s >> tx.vin;
226 if (tx.vin.size() == 0 && fAllowWitness) {
227 /* We read a dummy or an empty vin. */
228 s >> flags;
229 if (flags != 0) {
230 s >> tx.vin;
231 s >> tx.vout;
232 }
233 } else {
234 /* We read a non-empty vin. Assume a normal vout follows. */
235 s >> tx.vout;
236 }
237 if ((flags & 1) && fAllowWitness) {
238 /* The witness flag is present, and we support witnesses. */
239 flags ^= 1;
240 for (size_t i = 0; i < tx.vin.size(); i++) {
241 s >> tx.vin[i].scriptWitness.stack;
242 }
243 if (!tx.HasWitness()) {
244 /* It's illegal to encode witnesses when all witness stacks are empty. */
245 throw std::ios_base::failure("Superfluous witness record");
246 }
247 }
248 if (flags) {
249 /* Unknown flag in the serialization */
250 throw std::ios_base::failure("Unknown transaction optional data");
251 }
252 s >> tx.nLockTime;
253}
254
255template<typename Stream, typename TxType>
256void SerializeTransaction(const TxType& tx, Stream& s, const TransactionSerParams& params)
257{
258 const bool fAllowWitness = params.allow_witness;
259
260 s << tx.version;
261 unsigned char flags = 0;
262 // Consistency check
263 if (fAllowWitness) {
264 /* Check whether witnesses need to be serialized. */
265 if (tx.HasWitness()) {
266 flags |= 1;
267 }
268 }
269 if (flags) {
270 /* Use extended format in case witnesses are to be serialized. */
271 std::vector<CTxIn> vinDummy;
272 s << vinDummy;
273 s << flags;
274 }
275 s << tx.vin;
276 s << tx.vout;
277 if (flags & 1) {
278 for (size_t i = 0; i < tx.vin.size(); i++) {
279 s << tx.vin[i].scriptWitness.stack;
280 }
281 }
282 s << tx.nLockTime;
283}
284
285template<typename TxType>
286inline CAmount CalculateOutputValue(const TxType& tx)
287{
288 return std::accumulate(tx.vout.cbegin(), tx.vout.cend(), CAmount{0}, [](CAmount sum, const auto& txout) { return sum + txout.nValue; });
289}
290
291
296{
297public:
298 // Default transaction version.
299 static const uint32_t CURRENT_VERSION{2};
300
301 // The local variables are made const to prevent unintended modification
302 // without updating the cached hash value. However, CTransaction is not
303 // actually immutable; deserialization and assignment are implemented,
304 // and bypass the constness. This is safe, as they update the entire
305 // structure, including the hash.
306 const std::vector<CTxIn> vin;
307 const std::vector<CTxOut> vout;
308 const uint32_t version;
309 const uint32_t nLockTime;
310
311private:
313 const bool m_has_witness;
314 const Txid hash;
316
317 Txid ComputeHash() const;
319
320 bool ComputeHasWitness() const;
321
322public:
324 explicit CTransaction(const CMutableTransaction& tx);
325 explicit CTransaction(CMutableTransaction&& tx);
326
327 template <typename Stream>
328 inline void Serialize(Stream& s) const {
329 SerializeTransaction(*this, s, s.template GetParams<TransactionSerParams>());
330 }
331
334 template <typename Stream>
336 template <typename Stream>
338
339 bool IsNull() const {
340 return vin.empty() && vout.empty();
341 }
342
343 const Txid& GetHash() const LIFETIMEBOUND { return hash; }
345
346 // Return sum of txouts.
347 CAmount GetValueOut() const;
348
354 unsigned int GetTotalSize() const;
355
356 bool IsCoinBase() const
357 {
358 return (vin.size() == 1 && vin[0].prevout.IsNull());
359 }
360
361 friend bool operator==(const CTransaction& a, const CTransaction& b)
362 {
363 return a.hash == b.hash;
364 }
365
366 friend bool operator!=(const CTransaction& a, const CTransaction& b)
367 {
368 return a.hash != b.hash;
369 }
370
371 std::string ToString() const;
372
373 bool HasWitness() const { return m_has_witness; }
374};
375
378{
379 std::vector<CTxIn> vin;
380 std::vector<CTxOut> vout;
381 uint32_t version;
382 uint32_t nLockTime;
383
384 explicit CMutableTransaction();
385 explicit CMutableTransaction(const CTransaction& tx);
386
387 template <typename Stream>
388 inline void Serialize(Stream& s) const {
389 SerializeTransaction(*this, s, s.template GetParams<TransactionSerParams>());
390 }
391
392 template <typename Stream>
393 inline void Unserialize(Stream& s) {
394 UnserializeTransaction(*this, s, s.template GetParams<TransactionSerParams>());
395 }
396
397 template <typename Stream>
399 UnserializeTransaction(*this, s, params);
400 }
401
402 template <typename Stream>
404 Unserialize(s);
405 }
406
410 Txid GetHash() const;
411
412 bool HasWitness() const
413 {
414 for (size_t i = 0; i < vin.size(); i++) {
415 if (!vin[i].scriptWitness.IsNull()) {
416 return true;
417 }
418 }
419 return false;
420 }
421};
422
423typedef std::shared_ptr<const CTransaction> CTransactionRef;
424template <typename Tx> static inline CTransactionRef MakeTransactionRef(Tx&& txIn) { return std::make_shared<const CTransaction>(std::forward<Tx>(txIn)); }
425
428{
431 GenTxid(bool is_wtxid, const uint256& hash) : m_is_wtxid(is_wtxid), m_hash(hash) {}
432
433public:
434 static GenTxid Txid(const uint256& hash) { return GenTxid{false, hash}; }
435 static GenTxid Wtxid(const uint256& hash) { return GenTxid{true, hash}; }
436 bool IsWtxid() const { return m_is_wtxid; }
437 const uint256& GetHash() const LIFETIMEBOUND { return m_hash; }
438 friend bool operator==(const GenTxid& a, const GenTxid& b) { return a.m_is_wtxid == b.m_is_wtxid && a.m_hash == b.m_hash; }
439 friend bool operator<(const GenTxid& a, const GenTxid& b) { return std::tie(a.m_is_wtxid, a.m_hash) < std::tie(b.m_is_wtxid, b.m_hash); }
440};
441
442#endif // BITCOIN_PRIMITIVES_TRANSACTION_H
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
#define LIFETIMEBOUND
Definition: attributes.h:16
int flags
Definition: bitcoin-tx.cpp:536
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:29
uint32_t n
Definition: transaction.h:32
friend bool operator!=(const COutPoint &a, const COutPoint &b)
Definition: transaction.h:54
friend bool operator==(const COutPoint &a, const COutPoint &b)
Definition: transaction.h:49
Txid hash
Definition: transaction.h:31
SERIALIZE_METHODS(COutPoint, obj)
Definition: transaction.h:39
COutPoint(const Txid &hashIn, uint32_t nIn)
Definition: transaction.h:37
friend bool operator<(const COutPoint &a, const COutPoint &b)
Definition: transaction.h:44
void SetNull()
Definition: transaction.h:41
std::string ToString() const
Definition: transaction.cpp:21
bool IsNull() const
Definition: transaction.h:42
static constexpr uint32_t NULL_INDEX
Definition: transaction.h:34
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:415
void clear()
Definition: script.h:576
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:296
Txid ComputeHash() const
Definition: transaction.cpp:81
friend bool operator==(const CTransaction &a, const CTransaction &b)
Definition: transaction.h:361
CTransaction(deserialize_type, const TransactionSerParams &params, Stream &s)
This deserializing constructor is provided instead of an Unserialize method.
Definition: transaction.h:335
bool HasWitness() const
Definition: transaction.h:373
bool IsNull() const
Definition: transaction.h:339
const uint32_t nLockTime
Definition: transaction.h:309
CTransaction(deserialize_type, Stream &s)
Definition: transaction.h:337
bool ComputeHasWitness() const
Definition: transaction.cpp:74
CTransaction(const CMutableTransaction &tx)
Convert a CMutableTransaction into a CTransaction.
Definition: transaction.cpp:95
const Wtxid m_witness_hash
Definition: transaction.h:315
void Serialize(Stream &s) const
Definition: transaction.h:328
const std::vector< CTxOut > vout
Definition: transaction.h:307
std::string ToString() const
const bool m_has_witness
Memory only.
Definition: transaction.h:313
const Wtxid & GetWitnessHash() const LIFETIMEBOUND
Definition: transaction.h:344
unsigned int GetTotalSize() const
Get the total transaction size in bytes, including witness data.
Wtxid ComputeWitnessHash() const
Definition: transaction.cpp:86
bool IsCoinBase() const
Definition: transaction.h:356
CAmount GetValueOut() const
Definition: transaction.cpp:98
const Txid & GetHash() const LIFETIMEBOUND
Definition: transaction.h:343
static const uint32_t CURRENT_VERSION
Definition: transaction.h:299
friend bool operator!=(const CTransaction &a, const CTransaction &b)
Definition: transaction.h:366
const uint32_t version
Definition: transaction.h:308
const std::vector< CTxIn > vin
Definition: transaction.h:306
const Txid hash
Definition: transaction.h:314
An input of a transaction.
Definition: transaction.h:67
static const uint32_t MAX_SEQUENCE_NONFINAL
This is the maximum sequence number that enables both nLockTime and OP_CHECKLOCKTIMEVERIFY (BIP 65).
Definition: transaction.h:87
friend bool operator==(const CTxIn &a, const CTxIn &b)
Definition: transaction.h:131
static const uint32_t SEQUENCE_LOCKTIME_DISABLE_FLAG
If this flag is set, CTxIn::nSequence is NOT interpreted as a relative lock-time.
Definition: transaction.h:98
friend bool operator!=(const CTxIn &a, const CTxIn &b)
Definition: transaction.h:138
uint32_t nSequence
Definition: transaction.h:71
static const uint32_t SEQUENCE_LOCKTIME_MASK
If CTxIn::nSequence encodes a relative lock-time, this mask is applied to extract that lock-time from...
Definition: transaction.h:109
static const uint32_t SEQUENCE_FINAL
Setting nSequence to this value for every input in a transaction disables nLockTime/IsFinalTx().
Definition: transaction.h:81
std::string ToString() const
Definition: transaction.cpp:40
CScript scriptSig
Definition: transaction.h:70
static const uint32_t SEQUENCE_LOCKTIME_TYPE_FLAG
If CTxIn::nSequence encodes a relative lock-time and this flag is set, the relative lock-time has uni...
Definition: transaction.h:104
CScriptWitness scriptWitness
Only serialized through CTransaction.
Definition: transaction.h:72
COutPoint prevout
Definition: transaction.h:69
static const int SEQUENCE_LOCKTIME_GRANULARITY
In order to use the same number of bits to encode roughly the same wall-clock duration,...
Definition: transaction.h:119
SERIALIZE_METHODS(CTxIn, obj)
Definition: transaction.h:129
An output of a transaction.
Definition: transaction.h:150
CScript scriptPubKey
Definition: transaction.h:153
SERIALIZE_METHODS(CTxOut, obj)
Definition: transaction.h:162
friend bool operator==(const CTxOut &a, const CTxOut &b)
Definition: transaction.h:175
friend bool operator!=(const CTxOut &a, const CTxOut &b)
Definition: transaction.h:181
void SetNull()
Definition: transaction.h:164
CAmount nValue
Definition: transaction.h:152
bool IsNull() const
Definition: transaction.h:170
std::string ToString() const
Definition: transaction.cpp:61
A generic txid reference (txid or wtxid).
Definition: transaction.h:428
bool IsWtxid() const
Definition: transaction.h:436
bool m_is_wtxid
Definition: transaction.h:429
friend bool operator<(const GenTxid &a, const GenTxid &b)
Definition: transaction.h:439
static GenTxid Wtxid(const uint256 &hash)
Definition: transaction.h:435
GenTxid(bool is_wtxid, const uint256 &hash)
Definition: transaction.h:431
uint256 m_hash
Definition: transaction.h:430
friend bool operator==(const GenTxid &a, const GenTxid &b)
Definition: transaction.h:438
const uint256 & GetHash() const LIFETIMEBOUND
Definition: transaction.h:437
static GenTxid Txid(const uint256 &hash)
Definition: transaction.h:434
constexpr bool IsNull() const
Wrapped uint256 methods.
256-bit opaque blob.
Definition: uint256.h:190
volatile double sum
Definition: examples.cpp:10
static constexpr TransactionSerParams TX_NO_WITNESS
Definition: transaction.h:196
void UnserializeTransaction(TxType &tx, Stream &s, const TransactionSerParams &params)
Basic transaction serialization format:
Definition: transaction.h:216
static constexpr TransactionSerParams TX_WITH_WITNESS
Definition: transaction.h:195
void SerializeTransaction(const TxType &tx, Stream &s, const TransactionSerParams &params)
Definition: transaction.h:256
CAmount CalculateOutputValue(const TxType &tx)
Definition: transaction.h:286
static CTransactionRef MakeTransactionRef(Tx &&txIn)
Definition: transaction.h:424
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:423
constexpr deserialize_type deserialize
Definition: serialize.h:49
#define SER_PARAMS_OPFUNC
Helper macro for SerParams structs.
Definition: serialize.h:1218
#define READWRITE(...)
Definition: serialize.h:156
A mutable version of CTransaction.
Definition: transaction.h:378
CMutableTransaction(deserialize_type, const TransactionSerParams &params, Stream &s)
Definition: transaction.h:398
bool HasWitness() const
Definition: transaction.h:412
void Unserialize(Stream &s)
Definition: transaction.h:393
void Serialize(Stream &s) const
Definition: transaction.h:388
CMutableTransaction(deserialize_type, Stream &s)
Definition: transaction.h:403
std::vector< CTxOut > vout
Definition: transaction.h:380
Txid GetHash() const
Compute the hash of this CMutableTransaction.
Definition: transaction.cpp:69
std::vector< CTxIn > vin
Definition: transaction.h:379
bool IsNull() const
Definition: script.h:593
const bool allow_witness
Definition: transaction.h:192
Dummy data type to identify deserializing constructors.
Definition: serialize.h:48