Bitcoin ABC 0.30.7
P2P Digital Currency
transaction.h
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-2016 The Bitcoin Core developers
3// Copyright (c) 2017-2019 The Bitcoin developers
4// Distributed under the MIT software license, see the accompanying
5// file COPYING or http://www.opensource.org/licenses/mit-license.php.
6
7#ifndef BITCOIN_PRIMITIVES_TRANSACTION_H
8#define BITCOIN_PRIMITIVES_TRANSACTION_H
9
10#include <consensus/amount.h>
11#include <feerate.h>
12#include <primitives/txid.h>
13#include <script/script.h>
14#include <serialize.h>
15
20class COutPoint {
21private:
23 uint32_t n;
24
25public:
26 static constexpr uint32_t NULL_INDEX = std::numeric_limits<uint32_t>::max();
27
29 COutPoint(TxId txidIn, uint32_t nIn) : txid(txidIn), n(nIn) {}
30
31 SERIALIZE_METHODS(COutPoint, obj) { READWRITE(obj.txid, obj.n); }
32
33 bool IsNull() const { return txid.IsNull() && n == NULL_INDEX; }
34
35 const TxId &GetTxId() const { return txid; }
36 uint32_t GetN() const { return n; }
37
38 friend bool operator<(const COutPoint &a, const COutPoint &b) {
39 int cmp = a.txid.Compare(b.txid);
40 return cmp < 0 || (cmp == 0 && a.n < b.n);
41 }
42
43 friend bool operator==(const COutPoint &a, const COutPoint &b) {
44 return (a.txid == b.txid && a.n == b.n);
45 }
46
47 friend bool operator!=(const COutPoint &a, const COutPoint &b) {
48 return !(a == b);
49 }
50
51 std::string ToString() const;
52};
53
59class CTxIn {
60public:
63 uint32_t nSequence;
64
69 static const uint32_t SEQUENCE_FINAL = 0xffffffff;
70
71 /* Below flags apply in the context of BIP 68*/
76 static const uint32_t SEQUENCE_LOCKTIME_DISABLE_FLAG = (1U << 31);
77
83 static const uint32_t SEQUENCE_LOCKTIME_TYPE_FLAG = (1 << 22);
84
89 static const uint32_t SEQUENCE_LOCKTIME_MASK = 0x0000ffff;
90
99 static const int SEQUENCE_LOCKTIME_GRANULARITY = 9;
100
102
103 explicit CTxIn(COutPoint prevoutIn, CScript scriptSigIn = CScript(),
104 uint32_t nSequenceIn = SEQUENCE_FINAL)
105 : prevout(prevoutIn), scriptSig(scriptSigIn), nSequence(nSequenceIn) {}
106 CTxIn(TxId prevTxId, uint32_t nOut, CScript scriptSigIn = CScript(),
107 uint32_t nSequenceIn = SEQUENCE_FINAL)
108 : CTxIn(COutPoint(prevTxId, nOut), scriptSigIn, nSequenceIn) {}
109
111 READWRITE(obj.prevout, obj.scriptSig, obj.nSequence);
112 }
113
114 friend bool operator==(const CTxIn &a, const CTxIn &b) {
115 return (a.prevout == b.prevout && a.scriptSig == b.scriptSig &&
116 a.nSequence == b.nSequence);
117 }
118
119 friend bool operator!=(const CTxIn &a, const CTxIn &b) { return !(a == b); }
120
121 std::string ToString() const;
122};
123
128class CTxOut {
129public:
132
134
135 CTxOut(Amount nValueIn, CScript scriptPubKeyIn)
136 : nValue(nValueIn), scriptPubKey(scriptPubKeyIn) {}
137
138 SERIALIZE_METHODS(CTxOut, obj) { READWRITE(obj.nValue, obj.scriptPubKey); }
139
140 void SetNull() {
141 nValue = -SATOSHI;
143 }
144
145 bool IsNull() const { return nValue == -SATOSHI; }
146
147 friend bool operator==(const CTxOut &a, const CTxOut &b) {
148 return (a.nValue == b.nValue && a.scriptPubKey == b.scriptPubKey);
149 }
150
151 friend bool operator!=(const CTxOut &a, const CTxOut &b) {
152 return !(a == b);
153 }
154
155 std::string ToString() const;
156};
157
159
167template <typename Stream, typename TxType>
168inline void UnserializeTransaction(TxType &tx, Stream &s) {
169 s >> tx.nVersion;
170 tx.vin.clear();
171 tx.vout.clear();
172 /* Try to read the vin. In case the dummy is there, this will be read as an
173 * empty vector. */
174 s >> tx.vin;
175 /* We read a non-empty vin. Assume a normal vout follows. */
176 s >> tx.vout;
177 s >> tx.nLockTime;
178}
179
180template <typename Stream, typename TxType>
181inline void SerializeTransaction(const TxType &tx, Stream &s) {
182 s << tx.nVersion;
183 s << tx.vin;
184 s << tx.vout;
185 s << tx.nLockTime;
186}
187
193public:
194 // Default transaction version.
195 static constexpr int32_t CURRENT_VERSION = 2;
196
197 // Consensus: Valid min/max for nVersion, enforced as a consensus rule after
198 // Wellington.
199 static constexpr int32_t MIN_VERSION = 1, MAX_VERSION = 2;
200
201 // The local variables are made const to prevent unintended modification
202 // without updating the cached hash value. However, CTransaction is not
203 // actually immutable; deserialization and assignment are implemented,
204 // and bypass the constness. This is safe, as they update the entire
205 // structure, including the hash.
206 const std::vector<CTxIn> vin;
207 const std::vector<CTxOut> vout;
208 const int32_t nVersion;
209 const uint32_t nLockTime;
210
211private:
214
215 uint256 ComputeHash() const;
216
217public:
219 CTransaction();
220
222 explicit CTransaction(const CMutableTransaction &tx);
223 explicit CTransaction(CMutableTransaction &&tx);
224
225 template <typename Stream> inline void Serialize(Stream &s) const {
226 SerializeTransaction(*this, s);
227 }
228
234 template <typename Stream>
237
238 bool IsNull() const { return vin.empty() && vout.empty(); }
239
240 const TxId GetId() const { return TxId(hash); }
241 const TxHash GetHash() const { return TxHash(hash); }
242
243 // Return sum of txouts.
244 Amount GetValueOut() const;
245
250 unsigned int GetTotalSize() const;
251
252 bool IsCoinBase() const {
253 return (vin.size() == 1 && vin[0].prevout.IsNull());
254 }
255
256 friend bool operator==(const CTransaction &a, const CTransaction &b) {
257 return a.GetHash() == b.GetHash();
258 }
259
260 friend bool operator!=(const CTransaction &a, const CTransaction &b) {
261 return !(a == b);
262 }
263
264 std::string ToString() const;
265};
266#if defined(__x86_64__)
267static_assert(sizeof(CTransaction) == 88,
268 "sizeof CTransaction is expected to be 88 bytes");
269#endif
270
275public:
276 std::vector<CTxIn> vin;
277 std::vector<CTxOut> vout;
278 int32_t nVersion;
279 uint32_t nLockTime;
280
282 explicit CMutableTransaction(const CTransaction &tx);
283
284 template <typename Stream> inline void Serialize(Stream &s) const {
285 SerializeTransaction(*this, s);
286 }
287
288 template <typename Stream> inline void Unserialize(Stream &s) {
289 UnserializeTransaction(*this, s);
290 }
291
292 template <typename Stream>
294 Unserialize(s);
295 }
296
302 TxId GetId() const;
303 TxHash GetHash() const;
304
305 friend bool operator==(const CMutableTransaction &a,
306 const CMutableTransaction &b) {
307 return a.GetHash() == b.GetHash();
308 }
309};
310#if defined(__x86_64__)
311static_assert(sizeof(CMutableTransaction) == 56,
312 "sizeof CMutableTransaction is expected to be 56 bytes");
313#endif
314
315using CTransactionRef = std::shared_ptr<const CTransaction>;
317 return std::make_shared<const CTransaction>();
318}
319template <typename Tx>
320static inline CTransactionRef MakeTransactionRef(Tx &&txIn) {
321 return std::make_shared<const CTransaction>(std::forward<Tx>(txIn));
322}
323
327
330
332 default;
334 operator=(const PrecomputedTransactionData &txdata) = default;
335
336 template <class T> explicit PrecomputedTransactionData(const T &tx);
337};
338
339#endif // BITCOIN_PRIMITIVES_TRANSACTION_H
static constexpr Amount SATOSHI
Definition: amount.h:143
A mutable version of CTransaction.
Definition: transaction.h:274
friend bool operator==(const CMutableTransaction &a, const CMutableTransaction &b)
Definition: transaction.h:305
void Unserialize(Stream &s)
Definition: transaction.h:288
void Serialize(Stream &s) const
Definition: transaction.h:284
TxHash GetHash() const
Definition: transaction.cpp:56
TxId GetId() const
Compute the id and hash of this CMutableTransaction.
Definition: transaction.cpp:52
CMutableTransaction(deserialize_type, Stream &s)
Definition: transaction.h:293
std::vector< CTxOut > vout
Definition: transaction.h:277
std::vector< CTxIn > vin
Definition: transaction.h:276
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:20
uint32_t n
Definition: transaction.h:23
TxId txid
Definition: transaction.h:22
friend bool operator!=(const COutPoint &a, const COutPoint &b)
Definition: transaction.h:47
friend bool operator==(const COutPoint &a, const COutPoint &b)
Definition: transaction.h:43
SERIALIZE_METHODS(COutPoint, obj)
Definition: transaction.h:31
const TxId & GetTxId() const
Definition: transaction.h:35
uint32_t GetN() const
Definition: transaction.h:36
friend bool operator<(const COutPoint &a, const COutPoint &b)
Definition: transaction.h:38
COutPoint(TxId txidIn, uint32_t nIn)
Definition: transaction.h:29
std::string ToString() const
Definition: transaction.cpp:15
bool IsNull() const
Definition: transaction.h:33
static constexpr uint32_t NULL_INDEX
Definition: transaction.h:26
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:424
void clear()
Definition: script.h:546
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:192
friend bool operator==(const CTransaction &a, const CTransaction &b)
Definition: transaction.h:256
static constexpr int32_t MAX_VERSION
Definition: transaction.h:199
CTransaction()
Construct a CTransaction that qualifies as IsNull()
Definition: transaction.cpp:68
bool IsNull() const
Definition: transaction.h:238
const uint32_t nLockTime
Definition: transaction.h:209
CTransaction(deserialize_type, Stream &s)
This deserializing constructor is provided instead of an Unserialize method.
Definition: transaction.h:235
void Serialize(Stream &s) const
Definition: transaction.h:225
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
bool IsCoinBase() const
Definition: transaction.h:252
Amount GetValueOut() const
Definition: transaction.cpp:78
friend bool operator!=(const CTransaction &a, const CTransaction &b)
Definition: transaction.h:260
const int32_t nVersion
Definition: transaction.h:208
const TxHash GetHash() const
Definition: transaction.h:241
const std::vector< CTxIn > vin
Definition: transaction.h:206
const TxId GetId() const
Definition: transaction.h:240
static constexpr int32_t MIN_VERSION
Definition: transaction.h:199
const uint256 hash
Memory only.
Definition: transaction.h:213
static constexpr int32_t CURRENT_VERSION
Definition: transaction.h:195
An input of a transaction.
Definition: transaction.h:59
friend bool operator==(const CTxIn &a, const CTxIn &b)
Definition: transaction.h:114
static const uint32_t SEQUENCE_LOCKTIME_DISABLE_FLAG
If this flag set, CTxIn::nSequence is NOT interpreted as a relative lock-time.
Definition: transaction.h:76
friend bool operator!=(const CTxIn &a, const CTxIn &b)
Definition: transaction.h:119
uint32_t nSequence
Definition: transaction.h:63
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:89
static const uint32_t SEQUENCE_FINAL
Setting nSequence to this value for every input in a transaction disables nLockTime.
Definition: transaction.h:69
CTxIn(COutPoint prevoutIn, CScript scriptSigIn=CScript(), uint32_t nSequenceIn=SEQUENCE_FINAL)
Definition: transaction.h:103
CTxIn(TxId prevTxId, uint32_t nOut, CScript scriptSigIn=CScript(), uint32_t nSequenceIn=SEQUENCE_FINAL)
Definition: transaction.h:106
std::string ToString() const
Definition: transaction.cpp:19
CScript scriptSig
Definition: transaction.h:62
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:83
COutPoint prevout
Definition: transaction.h:61
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:99
SERIALIZE_METHODS(CTxIn, obj)
Definition: transaction.h:110
An output of a transaction.
Definition: transaction.h:128
CScript scriptPubKey
Definition: transaction.h:131
SERIALIZE_METHODS(CTxOut, obj)
Definition: transaction.h:138
Amount nValue
Definition: transaction.h:130
CTxOut(Amount nValueIn, CScript scriptPubKeyIn)
Definition: transaction.h:135
friend bool operator==(const CTxOut &a, const CTxOut &b)
Definition: transaction.h:147
friend bool operator!=(const CTxOut &a, const CTxOut &b)
Definition: transaction.h:151
void SetNull()
Definition: transaction.h:140
bool IsNull() const
Definition: transaction.h:145
std::string ToString() const
Definition: transaction.cpp:35
int Compare(const base_blob &other) const
Definition: uint256.h:43
bool IsNull() const
Definition: uint256.h:32
256-bit opaque blob.
Definition: uint256.h:129
void UnserializeTransaction(TxType &tx, Stream &s)
Basic transaction serialization format:
Definition: transaction.h:168
void SerializeTransaction(const TxType &tx, Stream &s)
Definition: transaction.h:181
static CTransactionRef MakeTransactionRef()
Definition: transaction.h:316
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:315
constexpr deserialize_type deserialize
Definition: serialize.h:50
#define READWRITE(...)
Definition: serialize.h:166
Definition: amount.h:19
Precompute sighash midstate to avoid quadratic hashing.
Definition: transaction.h:325
PrecomputedTransactionData & operator=(const PrecomputedTransactionData &txdata)=default
PrecomputedTransactionData(const PrecomputedTransactionData &txdata)=default
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
Dummy data type to identify deserializing constructors.
Definition: serialize.h:49