Bitcoin ABC  0.26.3
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 
16 static const int SERIALIZE_TRANSACTION = 0x00;
17 
22 class COutPoint {
23 private:
25  uint32_t n;
26 
27 public:
28  static constexpr uint32_t NULL_INDEX = std::numeric_limits<uint32_t>::max();
29 
31  COutPoint(TxId txidIn, uint32_t nIn) : txid(txidIn), n(nIn) {}
32 
33  SERIALIZE_METHODS(COutPoint, obj) { READWRITE(obj.txid, obj.n); }
34 
35  bool IsNull() const { return txid.IsNull() && n == NULL_INDEX; }
36 
37  const TxId &GetTxId() const { return txid; }
38  uint32_t GetN() const { return n; }
39 
40  friend bool operator<(const COutPoint &a, const COutPoint &b) {
41  int cmp = a.txid.Compare(b.txid);
42  return cmp < 0 || (cmp == 0 && a.n < b.n);
43  }
44 
45  friend bool operator==(const COutPoint &a, const COutPoint &b) {
46  return (a.txid == b.txid && a.n == b.n);
47  }
48 
49  friend bool operator!=(const COutPoint &a, const COutPoint &b) {
50  return !(a == b);
51  }
52 
53  std::string ToString() const;
54 };
55 
61 class CTxIn {
62 public:
65  uint32_t nSequence;
66 
71  static const uint32_t SEQUENCE_FINAL = 0xffffffff;
72 
73  /* Below flags apply in the context of BIP 68*/
78  static const uint32_t SEQUENCE_LOCKTIME_DISABLE_FLAG = (1U << 31);
79 
85  static const uint32_t SEQUENCE_LOCKTIME_TYPE_FLAG = (1 << 22);
86 
91  static const uint32_t SEQUENCE_LOCKTIME_MASK = 0x0000ffff;
92 
101  static const int SEQUENCE_LOCKTIME_GRANULARITY = 9;
102 
104 
105  explicit CTxIn(COutPoint prevoutIn, CScript scriptSigIn = CScript(),
106  uint32_t nSequenceIn = SEQUENCE_FINAL)
107  : prevout(prevoutIn), scriptSig(scriptSigIn), nSequence(nSequenceIn) {}
108  CTxIn(TxId prevTxId, uint32_t nOut, CScript scriptSigIn = CScript(),
109  uint32_t nSequenceIn = SEQUENCE_FINAL)
110  : CTxIn(COutPoint(prevTxId, nOut), scriptSigIn, nSequenceIn) {}
111 
113  READWRITE(obj.prevout, obj.scriptSig, obj.nSequence);
114  }
115 
116  friend bool operator==(const CTxIn &a, const CTxIn &b) {
117  return (a.prevout == b.prevout && a.scriptSig == b.scriptSig &&
118  a.nSequence == b.nSequence);
119  }
120 
121  friend bool operator!=(const CTxIn &a, const CTxIn &b) { return !(a == b); }
122 
123  std::string ToString() const;
124 };
125 
130 class CTxOut {
131 public:
134 
135  CTxOut() { SetNull(); }
136 
137  CTxOut(Amount nValueIn, CScript scriptPubKeyIn)
138  : nValue(nValueIn), scriptPubKey(scriptPubKeyIn) {}
139 
140  SERIALIZE_METHODS(CTxOut, obj) { READWRITE(obj.nValue, obj.scriptPubKey); }
141 
142  void SetNull() {
143  nValue = -SATOSHI;
145  }
146 
147  bool IsNull() const { return nValue == -SATOSHI; }
148 
149  friend bool operator==(const CTxOut &a, const CTxOut &b) {
150  return (a.nValue == b.nValue && a.scriptPubKey == b.scriptPubKey);
151  }
152 
153  friend bool operator!=(const CTxOut &a, const CTxOut &b) {
154  return !(a == b);
155  }
156 
157  std::string ToString() const;
158 };
159 
160 class CMutableTransaction;
161 
169 template <typename Stream, typename TxType>
170 inline void UnserializeTransaction(TxType &tx, Stream &s) {
171  s >> tx.nVersion;
172  tx.vin.clear();
173  tx.vout.clear();
174  /* Try to read the vin. In case the dummy is there, this will be read as an
175  * empty vector. */
176  s >> tx.vin;
177  /* We read a non-empty vin. Assume a normal vout follows. */
178  s >> tx.vout;
179  s >> tx.nLockTime;
180 }
181 
182 template <typename Stream, typename TxType>
183 inline void SerializeTransaction(const TxType &tx, Stream &s) {
184  s << tx.nVersion;
185  s << tx.vin;
186  s << tx.vout;
187  s << tx.nLockTime;
188 }
189 
195 public:
196  // Default transaction version.
197  static const int32_t CURRENT_VERSION = 2;
198 
199  // Changing the default transaction version requires a two step process:
200  // first adapting relay policy by bumping MAX_STANDARD_VERSION, and then
201  // later date bumping the default CURRENT_VERSION at which point both
202  // CURRENT_VERSION and MAX_STANDARD_VERSION will be equal.
203  static const int32_t MAX_STANDARD_VERSION = 2;
204 
205  // The local variables are made const to prevent unintended modification
206  // without updating the cached hash value. However, CTransaction is not
207  // actually immutable; deserialization and assignment are implemented,
208  // and bypass the constness. This is safe, as they update the entire
209  // structure, including the hash.
210  const std::vector<CTxIn> vin;
211  const std::vector<CTxOut> vout;
212  const int32_t nVersion;
213  const uint32_t nLockTime;
214 
215 private:
217  const uint256 hash;
218 
219  uint256 ComputeHash() const;
220 
221 public:
223  CTransaction();
224 
226  explicit CTransaction(const CMutableTransaction &tx);
227  explicit CTransaction(CMutableTransaction &&tx);
228 
229  template <typename Stream> inline void Serialize(Stream &s) const {
230  SerializeTransaction(*this, s);
231  }
232 
238  template <typename Stream>
241 
242  bool IsNull() const { return vin.empty() && vout.empty(); }
243 
244  const TxId GetId() const { return TxId(hash); }
245  const TxHash GetHash() const { return TxHash(hash); }
246 
247  // Return sum of txouts.
248  Amount GetValueOut() const;
249 
254  unsigned int GetTotalSize() const;
255 
256  bool IsCoinBase() const {
257  return (vin.size() == 1 && vin[0].prevout.IsNull());
258  }
259 
260  friend bool operator==(const CTransaction &a, const CTransaction &b) {
261  return a.GetHash() == b.GetHash();
262  }
263 
264  friend bool operator!=(const CTransaction &a, const CTransaction &b) {
265  return !(a == b);
266  }
267 
268  std::string ToString() const;
269 };
270 #if defined(__x86_64__)
271 static_assert(sizeof(CTransaction) == 88,
272  "sizeof CTransaction is expected to be 88 bytes");
273 #endif
274 
279 public:
280  std::vector<CTxIn> vin;
281  std::vector<CTxOut> vout;
282  int32_t nVersion;
283  uint32_t nLockTime;
284 
286  explicit CMutableTransaction(const CTransaction &tx);
287 
288  template <typename Stream> inline void Serialize(Stream &s) const {
289  SerializeTransaction(*this, s);
290  }
291 
292  template <typename Stream> inline void Unserialize(Stream &s) {
293  UnserializeTransaction(*this, s);
294  }
295 
296  template <typename Stream>
298  Unserialize(s);
299  }
300 
306  TxId GetId() const;
307  TxHash GetHash() const;
308 
309  friend bool operator==(const CMutableTransaction &a,
310  const CMutableTransaction &b) {
311  return a.GetHash() == b.GetHash();
312  }
313 };
314 #if defined(__x86_64__)
315 static_assert(sizeof(CMutableTransaction) == 56,
316  "sizeof CMutableTransaction is expected to be 56 bytes");
317 #endif
318 
319 using CTransactionRef = std::shared_ptr<const CTransaction>;
321  return std::make_shared<const CTransaction>();
322 }
323 template <typename Tx>
324 static inline CTransactionRef MakeTransactionRef(Tx &&txIn) {
325  return std::make_shared<const CTransaction>(std::forward<Tx>(txIn));
326 }
327 
331 
334 
336  default;
338  operator=(const PrecomputedTransactionData &txdata) = default;
339 
340  template <class T> explicit PrecomputedTransactionData(const T &tx);
341 };
342 
343 #endif // BITCOIN_PRIMITIVES_TRANSACTION_H
static constexpr Amount SATOSHI
Definition: amount.h:153
A mutable version of CTransaction.
Definition: transaction.h:278
friend bool operator==(const CMutableTransaction &a, const CMutableTransaction &b)
Definition: transaction.h:309
void Unserialize(Stream &s)
Definition: transaction.h:292
void Serialize(Stream &s) const
Definition: transaction.h:288
TxHash GetHash() const
Definition: transaction.cpp:55
TxId GetId() const
Compute the id and hash of this CMutableTransaction.
Definition: transaction.cpp:51
CMutableTransaction(deserialize_type, Stream &s)
Definition: transaction.h:297
std::vector< CTxOut > vout
Definition: transaction.h:281
std::vector< CTxIn > vin
Definition: transaction.h:280
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:22
uint32_t n
Definition: transaction.h:25
TxId txid
Definition: transaction.h:24
friend bool operator!=(const COutPoint &a, const COutPoint &b)
Definition: transaction.h:49
friend bool operator==(const COutPoint &a, const COutPoint &b)
Definition: transaction.h:45
SERIALIZE_METHODS(COutPoint, obj)
Definition: transaction.h:33
uint32_t GetN() const
Definition: transaction.h:38
const TxId & GetTxId() const
Definition: transaction.h:37
friend bool operator<(const COutPoint &a, const COutPoint &b)
Definition: transaction.h:40
COutPoint(TxId txidIn, uint32_t nIn)
Definition: transaction.h:31
std::string ToString() const
Definition: transaction.cpp:15
bool IsNull() const
Definition: transaction.h:35
static constexpr uint32_t NULL_INDEX
Definition: transaction.h:28
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:431
void clear()
Definition: script.h:554
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:194
static const int32_t MAX_STANDARD_VERSION
Definition: transaction.h:203
friend bool operator==(const CTransaction &a, const CTransaction &b)
Definition: transaction.h:260
CTransaction()
Construct a CTransaction that qualifies as IsNull()
Definition: transaction.cpp:67
bool IsNull() const
Definition: transaction.h:242
const uint32_t nLockTime
Definition: transaction.h:213
CTransaction(deserialize_type, Stream &s)
This deserializing constructor is provided instead of an Unserialize method.
Definition: transaction.h:239
void Serialize(Stream &s) const
Definition: transaction.h:229
const std::vector< CTxOut > vout
Definition: transaction.h:211
uint256 ComputeHash() const
Definition: transaction.cpp:59
std::string ToString() const
Definition: transaction.cpp:95
unsigned int GetTotalSize() const
Get the total transaction size in bytes.
Definition: transaction.cpp:91
bool IsCoinBase() const
Definition: transaction.h:256
Amount GetValueOut() const
Definition: transaction.cpp:77
friend bool operator!=(const CTransaction &a, const CTransaction &b)
Definition: transaction.h:264
const int32_t nVersion
Definition: transaction.h:212
const TxHash GetHash() const
Definition: transaction.h:245
const std::vector< CTxIn > vin
Definition: transaction.h:210
const TxId GetId() const
Definition: transaction.h:244
const uint256 hash
Memory only.
Definition: transaction.h:217
static const int32_t CURRENT_VERSION
Definition: transaction.h:197
An input of a transaction.
Definition: transaction.h:61
friend bool operator==(const CTxIn &a, const CTxIn &b)
Definition: transaction.h:116
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:78
friend bool operator!=(const CTxIn &a, const CTxIn &b)
Definition: transaction.h:121
uint32_t nSequence
Definition: transaction.h:65
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:91
static const uint32_t SEQUENCE_FINAL
Setting nSequence to this value for every input in a transaction disables nLockTime.
Definition: transaction.h:71
CTxIn(COutPoint prevoutIn, CScript scriptSigIn=CScript(), uint32_t nSequenceIn=SEQUENCE_FINAL)
Definition: transaction.h:105
CTxIn(TxId prevTxId, uint32_t nOut, CScript scriptSigIn=CScript(), uint32_t nSequenceIn=SEQUENCE_FINAL)
Definition: transaction.h:108
std::string ToString() const
Definition: transaction.cpp:19
CScript scriptSig
Definition: transaction.h:64
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:85
COutPoint prevout
Definition: transaction.h:63
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:101
SERIALIZE_METHODS(CTxIn, obj)
Definition: transaction.h:112
An output of a transaction.
Definition: transaction.h:130
CScript scriptPubKey
Definition: transaction.h:133
SERIALIZE_METHODS(CTxOut, obj)
Definition: transaction.h:140
Amount nValue
Definition: transaction.h:132
CTxOut(Amount nValueIn, CScript scriptPubKeyIn)
Definition: transaction.h:137
friend bool operator==(const CTxOut &a, const CTxOut &b)
Definition: transaction.h:149
friend bool operator!=(const CTxOut &a, const CTxOut &b)
Definition: transaction.h:153
void SetNull()
Definition: transaction.h:142
bool IsNull() const
Definition: transaction.h:147
std::string ToString() const
Definition: transaction.cpp:35
int Compare(const base_blob &other) const
Definition: uint256.h:41
bool IsNull() const
Definition: uint256.h:30
256-bit opaque blob.
Definition: uint256.h:127
void UnserializeTransaction(TxType &tx, Stream &s)
Basic transaction serialization format:
Definition: transaction.h:170
static const int SERIALIZE_TRANSACTION
Definition: transaction.h:16
void SerializeTransaction(const TxType &tx, Stream &s)
Definition: transaction.h:183
static CTransactionRef MakeTransactionRef()
Definition: transaction.h:320
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:319
constexpr deserialize_type deserialize
Definition: serialize.h:50
#define READWRITE(...)
Definition: serialize.h:180
Definition: amount.h:19
Precompute sighash midstate to avoid quadratic hashing.
Definition: transaction.h:329
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