Bitcoin Core 22.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-2020 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 <stdint.h>
10#include <consensus/amount.h>
11#include <script/script.h>
12#include <serialize.h>
13#include <uint256.h>
14
15#include <tuple>
16
23static const int SERIALIZE_TRANSACTION_NO_WITNESS = 0x40000000;
24
27{
28public:
30 uint32_t n;
31
32 static constexpr uint32_t NULL_INDEX = std::numeric_limits<uint32_t>::max();
33
35 COutPoint(const uint256& hashIn, uint32_t nIn): hash(hashIn), n(nIn) { }
36
37 SERIALIZE_METHODS(COutPoint, obj) { READWRITE(obj.hash, obj.n); }
38
39 void SetNull() { hash.SetNull(); n = NULL_INDEX; }
40 bool IsNull() const { return (hash.IsNull() && n == NULL_INDEX); }
41
42 friend bool operator<(const COutPoint& a, const COutPoint& b)
43 {
44 int cmp = a.hash.Compare(b.hash);
45 return cmp < 0 || (cmp == 0 && a.n < b.n);
46 }
47
48 friend bool operator==(const COutPoint& a, const COutPoint& b)
49 {
50 return (a.hash == b.hash && a.n == b.n);
51 }
52
53 friend bool operator!=(const COutPoint& a, const COutPoint& b)
54 {
55 return !(a == b);
56 }
57
58 std::string ToString() const;
59};
60
65class CTxIn
66{
67public:
70 uint32_t nSequence;
72
73 /* Setting nSequence to this value for every input in a transaction
74 * disables nLockTime. */
75 static const uint32_t SEQUENCE_FINAL = 0xffffffff;
76
77 /* Below flags apply in the context of BIP 68*/
78 /* If this flag set, CTxIn::nSequence is NOT interpreted as a
79 * relative lock-time. */
80 static const uint32_t SEQUENCE_LOCKTIME_DISABLE_FLAG = (1U << 31);
81
82 /* If CTxIn::nSequence encodes a relative lock-time and this flag
83 * is set, the relative lock-time has units of 512 seconds,
84 * otherwise it specifies blocks with a granularity of 1. */
85 static const uint32_t SEQUENCE_LOCKTIME_TYPE_FLAG = (1 << 22);
86
87 /* If CTxIn::nSequence encodes a relative lock-time, this mask is
88 * applied to extract that lock-time from the sequence field. */
89 static const uint32_t SEQUENCE_LOCKTIME_MASK = 0x0000ffff;
90
91 /* In order to use the same number of bits to encode roughly the
92 * same wall-clock duration, and because blocks are naturally
93 * limited to occur every 600s on average, the minimum granularity
94 * for time-based relative lock-time is fixed at 512 seconds.
95 * Converting from CTxIn::nSequence to seconds is performed by
96 * multiplying by 512 = 2^9, or equivalently shifting up by
97 * 9 bits. */
98 static const int SEQUENCE_LOCKTIME_GRANULARITY = 9;
99
101 {
103 }
104
105 explicit CTxIn(COutPoint prevoutIn, CScript scriptSigIn=CScript(), uint32_t nSequenceIn=SEQUENCE_FINAL);
106 CTxIn(uint256 hashPrevTx, uint32_t nOut, CScript scriptSigIn=CScript(), uint32_t nSequenceIn=SEQUENCE_FINAL);
107
108 SERIALIZE_METHODS(CTxIn, obj) { READWRITE(obj.prevout, obj.scriptSig, obj.nSequence); }
109
110 friend bool operator==(const CTxIn& a, const CTxIn& b)
111 {
112 return (a.prevout == b.prevout &&
113 a.scriptSig == b.scriptSig &&
114 a.nSequence == b.nSequence);
115 }
116
117 friend bool operator!=(const CTxIn& a, const CTxIn& b)
118 {
119 return !(a == b);
120 }
121
122 std::string ToString() const;
123};
124
129{
130public:
133
135 {
136 SetNull();
137 }
138
139 CTxOut(const CAmount& nValueIn, CScript scriptPubKeyIn);
140
141 SERIALIZE_METHODS(CTxOut, obj) { READWRITE(obj.nValue, obj.scriptPubKey); }
142
143 void SetNull()
144 {
145 nValue = -1;
147 }
148
149 bool IsNull() const
150 {
151 return (nValue == -1);
152 }
153
154 friend bool operator==(const CTxOut& a, const CTxOut& b)
155 {
156 return (a.nValue == b.nValue &&
158 }
159
160 friend bool operator!=(const CTxOut& a, const CTxOut& b)
161 {
162 return !(a == b);
163 }
164
165 std::string ToString() const;
166};
167
169
187template<typename Stream, typename TxType>
188inline void UnserializeTransaction(TxType& tx, Stream& s) {
189 const bool fAllowWitness = !(s.GetVersion() & SERIALIZE_TRANSACTION_NO_WITNESS);
190
191 s >> tx.nVersion;
192 unsigned char flags = 0;
193 tx.vin.clear();
194 tx.vout.clear();
195 /* Try to read the vin. In case the dummy is there, this will be read as an empty vector. */
196 s >> tx.vin;
197 if (tx.vin.size() == 0 && fAllowWitness) {
198 /* We read a dummy or an empty vin. */
199 s >> flags;
200 if (flags != 0) {
201 s >> tx.vin;
202 s >> tx.vout;
203 }
204 } else {
205 /* We read a non-empty vin. Assume a normal vout follows. */
206 s >> tx.vout;
207 }
208 if ((flags & 1) && fAllowWitness) {
209 /* The witness flag is present, and we support witnesses. */
210 flags ^= 1;
211 for (size_t i = 0; i < tx.vin.size(); i++) {
212 s >> tx.vin[i].scriptWitness.stack;
213 }
214 if (!tx.HasWitness()) {
215 /* It's illegal to encode witnesses when all witness stacks are empty. */
216 throw std::ios_base::failure("Superfluous witness record");
217 }
218 }
219 if (flags) {
220 /* Unknown flag in the serialization */
221 throw std::ios_base::failure("Unknown transaction optional data");
222 }
223 s >> tx.nLockTime;
224}
225
226template<typename Stream, typename TxType>
227inline void SerializeTransaction(const TxType& tx, Stream& s) {
228 const bool fAllowWitness = !(s.GetVersion() & SERIALIZE_TRANSACTION_NO_WITNESS);
229
230 s << tx.nVersion;
231 unsigned char flags = 0;
232 // Consistency check
233 if (fAllowWitness) {
234 /* Check whether witnesses need to be serialized. */
235 if (tx.HasWitness()) {
236 flags |= 1;
237 }
238 }
239 if (flags) {
240 /* Use extended format in case witnesses are to be serialized. */
241 std::vector<CTxIn> vinDummy;
242 s << vinDummy;
243 s << flags;
244 }
245 s << tx.vin;
246 s << tx.vout;
247 if (flags & 1) {
248 for (size_t i = 0; i < tx.vin.size(); i++) {
249 s << tx.vin[i].scriptWitness.stack;
250 }
251 }
252 s << tx.nLockTime;
253}
254
255
260{
261public:
262 // Default transaction version.
263 static const int32_t CURRENT_VERSION=2;
264
265 // The local variables are made const to prevent unintended modification
266 // without updating the cached hash value. However, CTransaction is not
267 // actually immutable; deserialization and assignment are implemented,
268 // and bypass the constness. This is safe, as they update the entire
269 // structure, including the hash.
270 const std::vector<CTxIn> vin;
271 const std::vector<CTxOut> vout;
272 const int32_t nVersion;
273 const uint32_t nLockTime;
274
275private:
279
280 uint256 ComputeHash() const;
282
283public:
285 explicit CTransaction(const CMutableTransaction& tx);
287
288 template <typename Stream>
289 inline void Serialize(Stream& s) const {
290 SerializeTransaction(*this, s);
291 }
292
295 template <typename Stream>
297
298 bool IsNull() const {
299 return vin.empty() && vout.empty();
300 }
301
302 const uint256& GetHash() const { return hash; }
303 const uint256& GetWitnessHash() const { return m_witness_hash; };
304
305 // Return sum of txouts.
306 CAmount GetValueOut() const;
307
313 unsigned int GetTotalSize() const;
314
315 bool IsCoinBase() const
316 {
317 return (vin.size() == 1 && vin[0].prevout.IsNull());
318 }
319
320 friend bool operator==(const CTransaction& a, const CTransaction& b)
321 {
322 return a.hash == b.hash;
323 }
324
325 friend bool operator!=(const CTransaction& a, const CTransaction& b)
326 {
327 return a.hash != b.hash;
328 }
329
330 std::string ToString() const;
331
332 bool HasWitness() const
333 {
334 for (size_t i = 0; i < vin.size(); i++) {
335 if (!vin[i].scriptWitness.IsNull()) {
336 return true;
337 }
338 }
339 return false;
340 }
341};
342
345{
346 std::vector<CTxIn> vin;
347 std::vector<CTxOut> vout;
348 int32_t nVersion;
349 uint32_t nLockTime;
350
352 explicit CMutableTransaction(const CTransaction& tx);
353
354 template <typename Stream>
355 inline void Serialize(Stream& s) const {
356 SerializeTransaction(*this, s);
357 }
358
359
360 template <typename Stream>
361 inline void Unserialize(Stream& s) {
362 UnserializeTransaction(*this, s);
363 }
364
365 template <typename Stream>
367 Unserialize(s);
368 }
369
373 uint256 GetHash() const;
374
375 bool HasWitness() const
376 {
377 for (size_t i = 0; i < vin.size(); i++) {
378 if (!vin[i].scriptWitness.IsNull()) {
379 return true;
380 }
381 }
382 return false;
383 }
384};
385
386typedef std::shared_ptr<const CTransaction> CTransactionRef;
387template <typename Tx> static inline CTransactionRef MakeTransactionRef(Tx&& txIn) { return std::make_shared<const CTransaction>(std::forward<Tx>(txIn)); }
388
391{
394 GenTxid(bool is_wtxid, const uint256& hash) : m_is_wtxid(is_wtxid), m_hash(hash) {}
395
396public:
397 static GenTxid Txid(const uint256& hash) { return GenTxid{false, hash}; }
398 static GenTxid Wtxid(const uint256& hash) { return GenTxid{true, hash}; }
399 bool IsWtxid() const { return m_is_wtxid; }
400 const uint256& GetHash() const { return m_hash; }
401 friend bool operator==(const GenTxid& a, const GenTxid& b) { return a.m_is_wtxid == b.m_is_wtxid && a.m_hash == b.m_hash; }
402 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); }
403};
404
405#endif // BITCOIN_PRIMITIVES_TRANSACTION_H
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
int flags
Definition: bitcoin-tx.cpp:525
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:27
COutPoint(const uint256 &hashIn, uint32_t nIn)
Definition: transaction.h:35
uint32_t n
Definition: transaction.h:30
friend bool operator!=(const COutPoint &a, const COutPoint &b)
Definition: transaction.h:53
friend bool operator==(const COutPoint &a, const COutPoint &b)
Definition: transaction.h:48
SERIALIZE_METHODS(COutPoint, obj)
Definition: transaction.h:37
friend bool operator<(const COutPoint &a, const COutPoint &b)
Definition: transaction.h:42
void SetNull()
Definition: transaction.h:39
std::string ToString() const
Definition: transaction.cpp:15
bool IsNull() const
Definition: transaction.h:40
uint256 hash
Definition: transaction.h:29
static constexpr uint32_t NULL_INDEX
Definition: transaction.h:32
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:406
void clear()
Definition: script.h:549
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:260
const uint256 & GetWitnessHash() const
Definition: transaction.h:303
uint256 ComputeWitnessHash() const
Definition: transaction.cpp:73
friend bool operator==(const CTransaction &a, const CTransaction &b)
Definition: transaction.h:320
bool HasWitness() const
Definition: transaction.h:332
const uint256 & GetHash() const
Definition: transaction.h:302
bool IsNull() const
Definition: transaction.h:298
const uint32_t nLockTime
Definition: transaction.h:273
CTransaction(deserialize_type, Stream &s)
This deserializing constructor is provided instead of an Unserialize method.
Definition: transaction.h:296
CTransaction(const CMutableTransaction &tx)
Convert a CMutableTransaction into a CTransaction.
Definition: transaction.cpp:81
void Serialize(Stream &s) const
Definition: transaction.h:289
const std::vector< CTxOut > vout
Definition: transaction.h:271
uint256 ComputeHash() const
Definition: transaction.cpp:68
std::string ToString() const
unsigned int GetTotalSize() const
Get the total transaction size in bytes, including witness data.
Definition: transaction.cpp:96
bool IsCoinBase() const
Definition: transaction.h:315
CAmount GetValueOut() const
Definition: transaction.cpp:84
friend bool operator!=(const CTransaction &a, const CTransaction &b)
Definition: transaction.h:325
const int32_t nVersion
Definition: transaction.h:272
const std::vector< CTxIn > vin
Definition: transaction.h:270
const uint256 hash
Memory only.
Definition: transaction.h:277
const uint256 m_witness_hash
Definition: transaction.h:278
static const int32_t CURRENT_VERSION
Definition: transaction.h:263
An input of a transaction.
Definition: transaction.h:66
friend bool operator==(const CTxIn &a, const CTxIn &b)
Definition: transaction.h:110
static const uint32_t SEQUENCE_LOCKTIME_DISABLE_FLAG
Definition: transaction.h:80
friend bool operator!=(const CTxIn &a, const CTxIn &b)
Definition: transaction.h:117
uint32_t nSequence
Definition: transaction.h:70
static const uint32_t SEQUENCE_LOCKTIME_MASK
Definition: transaction.h:89
static const uint32_t SEQUENCE_FINAL
Definition: transaction.h:75
std::string ToString() const
Definition: transaction.cpp:34
CScript scriptSig
Definition: transaction.h:69
static const uint32_t SEQUENCE_LOCKTIME_TYPE_FLAG
Definition: transaction.h:85
CScriptWitness scriptWitness
Only serialized through CTransaction.
Definition: transaction.h:71
COutPoint prevout
Definition: transaction.h:68
static const int SEQUENCE_LOCKTIME_GRANULARITY
Definition: transaction.h:98
SERIALIZE_METHODS(CTxIn, obj)
Definition: transaction.h:108
An output of a transaction.
Definition: transaction.h:129
CScript scriptPubKey
Definition: transaction.h:132
SERIALIZE_METHODS(CTxOut, obj)
Definition: transaction.h:141
friend bool operator==(const CTxOut &a, const CTxOut &b)
Definition: transaction.h:154
friend bool operator!=(const CTxOut &a, const CTxOut &b)
Definition: transaction.h:160
void SetNull()
Definition: transaction.h:143
CAmount nValue
Definition: transaction.h:131
bool IsNull() const
Definition: transaction.h:149
std::string ToString() const
Definition: transaction.cpp:55
A generic txid reference (txid or wtxid).
Definition: transaction.h:391
bool IsWtxid() const
Definition: transaction.h:399
bool m_is_wtxid
Definition: transaction.h:392
const uint256 & GetHash() const
Definition: transaction.h:400
friend bool operator<(const GenTxid &a, const GenTxid &b)
Definition: transaction.h:402
static GenTxid Wtxid(const uint256 &hash)
Definition: transaction.h:398
GenTxid(bool is_wtxid, const uint256 &hash)
Definition: transaction.h:394
uint256 m_hash
Definition: transaction.h:393
friend bool operator==(const GenTxid &a, const GenTxid &b)
Definition: transaction.h:401
static GenTxid Txid(const uint256 &hash)
Definition: transaction.h:397
int Compare(const base_blob &other) const
Definition: uint256.h:44
void SetNull()
Definition: uint256.h:39
bool IsNull() const
Definition: uint256.h:31
256-bit opaque blob.
Definition: uint256.h:124
static const int SERIALIZE_TRANSACTION_NO_WITNESS
A flag that is ORed into the protocol version to designate that a transaction should be (un)serialize...
Definition: transaction.h:23
void UnserializeTransaction(TxType &tx, Stream &s)
Basic transaction serialization format:
Definition: transaction.h:188
void SerializeTransaction(const TxType &tx, Stream &s)
Definition: transaction.h:227
static CTransactionRef MakeTransactionRef(Tx &&txIn)
Definition: transaction.h:387
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:386
constexpr deserialize_type deserialize
Definition: serialize.h:48
#define READWRITE(...)
Definition: serialize.h:147
A mutable version of CTransaction.
Definition: transaction.h:345
bool HasWitness() const
Definition: transaction.h:375
uint256 GetHash() const
Compute the hash of this CMutableTransaction.
Definition: transaction.cpp:63
void Unserialize(Stream &s)
Definition: transaction.h:361
void Serialize(Stream &s) const
Definition: transaction.h:355
CMutableTransaction(deserialize_type, Stream &s)
Definition: transaction.h:366
std::vector< CTxOut > vout
Definition: transaction.h:347
std::vector< CTxIn > vin
Definition: transaction.h:346
bool IsNull() const
Definition: script.h:566
Dummy data type to identify deserializing constructors.
Definition: serialize.h:47