Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
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:
24
25public:
26 static constexpr uint32_t NULL_INDEX = std::numeric_limits<uint32_t>::max();
27
30
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:
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
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
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>
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;
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;
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>
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>
321 return std::make_shared<const CTransaction>(std::forward<Tx>(txIn));
322}
323
338
339#endif // BITCOIN_PRIMITIVES_TRANSACTION_H
static constexpr Amount SATOSHI
Definition amount.h:143
A mutable version of CTransaction.
friend bool operator==(const CMutableTransaction &a, const CMutableTransaction &b)
void Unserialize(Stream &s)
void Serialize(Stream &s) const
TxHash GetHash() const
TxId GetId() const
Compute the id and hash of this CMutableTransaction.
CMutableTransaction(deserialize_type, Stream &s)
std::vector< CTxOut > vout
std::vector< CTxIn > vin
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
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
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:431
void clear()
Definition script.h:553
The basic transaction that is broadcasted on the network and contained in blocks.
friend bool operator==(const CTransaction &a, const CTransaction &b)
static constexpr int32_t MAX_VERSION
CTransaction()
Construct a CTransaction that qualifies as IsNull()
bool IsNull() const
const uint32_t nLockTime
CTransaction(deserialize_type, Stream &s)
This deserializing constructor is provided instead of an Unserialize method.
void Serialize(Stream &s) const
const std::vector< CTxOut > vout
uint256 ComputeHash() const
std::string ToString() const
unsigned int GetTotalSize() const
Get the total transaction size in bytes.
bool IsCoinBase() const
Amount GetValueOut() const
friend bool operator!=(const CTransaction &a, const CTransaction &b)
const int32_t nVersion
const TxHash GetHash() const
const std::vector< CTxIn > vin
const TxId GetId() const
static constexpr int32_t MIN_VERSION
const uint256 hash
Memory only.
static constexpr int32_t CURRENT_VERSION
An input of a transaction.
Definition transaction.h:59
friend bool operator==(const CTxIn &a, const CTxIn &b)
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)
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)
CTxIn(TxId prevTxId, uint32_t nOut, CScript scriptSigIn=CScript(), uint32_t nSequenceIn=SEQUENCE_FINAL)
std::string ToString() const
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)
An output of a transaction.
CScript scriptPubKey
SERIALIZE_METHODS(CTxOut, obj)
Amount nValue
CTxOut(Amount nValueIn, CScript scriptPubKeyIn)
friend bool operator==(const CTxOut &a, const CTxOut &b)
friend bool operator!=(const CTxOut &a, const CTxOut &b)
void SetNull()
bool IsNull() const
std::string ToString() const
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:
void SerializeTransaction(const TxType &tx, Stream &s)
static CTransactionRef MakeTransactionRef()
std::shared_ptr< const CTransaction > CTransactionRef
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
constexpr deserialize_type deserialize
Definition serialize.h:50
#define READWRITE(...)
Definition serialize.h:166
Precompute sighash midstate to avoid quadratic hashing.
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