Bitcoin ABC  0.22.12
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 <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 
30  COutPoint() : txid(), n(NULL_INDEX) {}
31  COutPoint(TxId txidIn, uint32_t nIn) : txid(txidIn), n(nIn) {}
32 
34 
35  template <typename Stream, typename Operation>
36  inline void SerializationOp(Stream &s, Operation ser_action) {
37  READWRITE(txid);
38  READWRITE(n);
39  }
40 
41  bool IsNull() const { return txid.IsNull() && n == NULL_INDEX; }
42 
43  const TxId &GetTxId() const { return txid; }
44  uint32_t GetN() const { return n; }
45 
46  friend bool operator<(const COutPoint &a, const COutPoint &b) {
47  int cmp = a.txid.Compare(b.txid);
48  return cmp < 0 || (cmp == 0 && a.n < b.n);
49  }
50 
51  friend bool operator==(const COutPoint &a, const COutPoint &b) {
52  return (a.txid == b.txid && a.n == b.n);
53  }
54 
55  friend bool operator!=(const COutPoint &a, const COutPoint &b) {
56  return !(a == b);
57  }
58 
59  std::string ToString() const;
60 };
61 
67 class CTxIn {
68 public:
71  uint32_t nSequence;
72 
77  static const uint32_t SEQUENCE_FINAL = 0xffffffff;
78 
79  /* Below flags apply in the context of BIP 68*/
84  static const uint32_t SEQUENCE_LOCKTIME_DISABLE_FLAG = (1U << 31);
85 
91  static const uint32_t SEQUENCE_LOCKTIME_TYPE_FLAG = (1 << 22);
92 
97  static const uint32_t SEQUENCE_LOCKTIME_MASK = 0x0000ffff;
98 
107  static const int SEQUENCE_LOCKTIME_GRANULARITY = 9;
108 
109  CTxIn() { nSequence = SEQUENCE_FINAL; }
110 
111  explicit CTxIn(COutPoint prevoutIn, CScript scriptSigIn = CScript(),
112  uint32_t nSequenceIn = SEQUENCE_FINAL)
113  : prevout(prevoutIn), scriptSig(scriptSigIn), nSequence(nSequenceIn) {}
114  CTxIn(TxId prevTxId, uint32_t nOut, CScript scriptSigIn = CScript(),
115  uint32_t nSequenceIn = SEQUENCE_FINAL)
116  : CTxIn(COutPoint(prevTxId, nOut), scriptSigIn, nSequenceIn) {}
117 
119 
120  template <typename Stream, typename Operation>
121  inline void SerializationOp(Stream &s, Operation ser_action) {
122  READWRITE(prevout);
123  READWRITE(scriptSig);
124  READWRITE(nSequence);
125  }
126 
127  friend bool operator==(const CTxIn &a, const CTxIn &b) {
128  return (a.prevout == b.prevout && a.scriptSig == b.scriptSig &&
129  a.nSequence == b.nSequence);
130  }
131 
132  friend bool operator!=(const CTxIn &a, const CTxIn &b) { return !(a == b); }
133 
134  std::string ToString() const;
135 };
136 
141 class CTxOut {
142 public:
145 
146  CTxOut() { SetNull(); }
147 
148  CTxOut(Amount nValueIn, CScript scriptPubKeyIn)
149  : nValue(nValueIn), scriptPubKey(scriptPubKeyIn) {}
150 
152 
153  template <typename Stream, typename Operation>
154  inline void SerializationOp(Stream &s, Operation ser_action) {
155  READWRITE(nValue);
156  READWRITE(scriptPubKey);
157  }
158 
159  void SetNull() {
160  nValue = -SATOSHI;
161  scriptPubKey.clear();
162  }
163 
164  bool IsNull() const { return nValue == -SATOSHI; }
165 
166  friend bool operator==(const CTxOut &a, const CTxOut &b) {
167  return (a.nValue == b.nValue && a.scriptPubKey == b.scriptPubKey);
168  }
169 
170  friend bool operator!=(const CTxOut &a, const CTxOut &b) {
171  return !(a == b);
172  }
173 
174  std::string ToString() const;
175 };
176 
177 class CMutableTransaction;
178 
186 template <typename Stream, typename TxType>
187 inline void UnserializeTransaction(TxType &tx, Stream &s) {
188  s >> tx.nVersion;
189  tx.vin.clear();
190  tx.vout.clear();
191  /* Try to read the vin. In case the dummy is there, this will be read as an
192  * empty vector. */
193  s >> tx.vin;
194  /* We read a non-empty vin. Assume a normal vout follows. */
195  s >> tx.vout;
196  s >> tx.nLockTime;
197 }
198 
199 template <typename Stream, typename TxType>
200 inline void SerializeTransaction(const TxType &tx, Stream &s) {
201  s << tx.nVersion;
202  s << tx.vin;
203  s << tx.vout;
204  s << tx.nLockTime;
205 }
206 
212 public:
213  // Default transaction version.
214  static const int32_t CURRENT_VERSION = 2;
215 
216  // Changing the default transaction version requires a two step process:
217  // first adapting relay policy by bumping MAX_STANDARD_VERSION, and then
218  // later date bumping the default CURRENT_VERSION at which point both
219  // CURRENT_VERSION and MAX_STANDARD_VERSION will be equal.
220  static const int32_t MAX_STANDARD_VERSION = 2;
221 
222  // The local variables are made const to prevent unintended modification
223  // without updating the cached hash value. However, CTransaction is not
224  // actually immutable; deserialization and assignment are implemented,
225  // and bypass the constness. This is safe, as they update the entire
226  // structure, including the hash.
227  const std::vector<CTxIn> vin;
228  const std::vector<CTxOut> vout;
229  const int32_t nVersion;
230  const uint32_t nLockTime;
231 
232 private:
234  const uint256 hash;
235 
236  uint256 ComputeHash() const;
237 
238 public:
240  CTransaction();
241 
243  explicit CTransaction(const CMutableTransaction &tx);
244  explicit CTransaction(CMutableTransaction &&tx);
245 
246  template <typename Stream> inline void Serialize(Stream &s) const {
247  SerializeTransaction(*this, s);
248  }
249 
255  template <typename Stream>
258 
259  bool IsNull() const { return vin.empty() && vout.empty(); }
260 
261  const TxId GetId() const { return TxId(hash); }
262  const TxHash GetHash() const { return TxHash(hash); }
263 
264  // Return sum of txouts.
265  Amount GetValueOut() const;
266  // GetValueIn() is a method on CCoinsViewCache, because
267  // inputs must be known to compute value in.
268 
273  unsigned int GetTotalSize() const;
274 
275  bool IsCoinBase() const {
276  return (vin.size() == 1 && vin[0].prevout.IsNull());
277  }
278 
279  friend bool operator==(const CTransaction &a, const CTransaction &b) {
280  return a.GetHash() == b.GetHash();
281  }
282 
283  friend bool operator!=(const CTransaction &a, const CTransaction &b) {
284  return !(a == b);
285  }
286 
287  std::string ToString() const;
288 };
289 #if defined(__x86_64__)
290 static_assert(sizeof(CTransaction) == 88,
291  "sizeof CTransaction is expected to be 88 bytes");
292 #endif
293 
298 public:
299  std::vector<CTxIn> vin;
300  std::vector<CTxOut> vout;
301  int32_t nVersion;
302  uint32_t nLockTime;
303 
305  explicit CMutableTransaction(const CTransaction &tx);
306 
307  template <typename Stream> inline void Serialize(Stream &s) const {
308  SerializeTransaction(*this, s);
309  }
310 
311  template <typename Stream> inline void Unserialize(Stream &s) {
312  UnserializeTransaction(*this, s);
313  }
314 
315  template <typename Stream>
317  Unserialize(s);
318  }
319 
325  TxId GetId() const;
326  TxHash GetHash() const;
327 
328  friend bool operator==(const CMutableTransaction &a,
329  const CMutableTransaction &b) {
330  return a.GetHash() == b.GetHash();
331  }
332 };
333 #if defined(__x86_64__)
334 static_assert(sizeof(CMutableTransaction) == 56,
335  "sizeof CMutableTransaction is expected to be 56 bytes");
336 #endif
337 
338 typedef std::shared_ptr<const CTransaction> CTransactionRef;
339 static inline CTransactionRef MakeTransactionRef() {
340  return std::make_shared<const CTransaction>();
341 }
342 template <typename Tx>
343 static inline CTransactionRef MakeTransactionRef(Tx &&txIn) {
344  return std::make_shared<const CTransaction>(std::forward<Tx>(txIn));
345 }
346 
349  uint256 hashPrevouts, hashSequence, hashOutputs;
350 
352  : hashPrevouts(), hashSequence(), hashOutputs() {}
353 
355  default;
356 
357  template <class T> explicit PrecomputedTransactionData(const T &tx);
358 };
359 
360 #endif // BITCOIN_PRIMITIVES_TRANSACTION_H
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:338
uint32_t GetN() const
Definition: transaction.h:44
void SetNull()
Definition: transaction.h:159
friend bool operator!=(const COutPoint &a, const COutPoint &b)
Definition: transaction.h:55
CScript scriptPubKey
Definition: transaction.h:144
ADD_SERIALIZE_METHODS
Definition: transaction.h:33
void Unserialize(Stream &s)
Definition: transaction.h:311
CTxIn(TxId prevTxId, uint32_t nOut, CScript scriptSigIn=CScript(), uint32_t nSequenceIn=SEQUENCE_FINAL)
Definition: transaction.h:114
Precompute sighash midstate to avoid quadratic hashing.
Definition: transaction.h:348
constexpr deserialize_type deserialize
Definition: serialize.h:45
ADD_SERIALIZE_METHODS
Definition: transaction.h:118
static CTransactionRef MakeTransactionRef()
Definition: transaction.h:339
friend bool operator==(const CTxOut &a, const CTxOut &b)
Definition: transaction.h:166
Definition: amount.h:17
TxId txid
Definition: transaction.h:24
const TxId & GetTxId() const
Definition: transaction.h:43
const TxHash GetHash() const
Definition: transaction.h:262
static constexpr Amount SATOSHI
Definition: amount.h:151
void SerializationOp(Stream &s, Operation ser_action)
Definition: transaction.h:154
std::vector< CTxOut > vout
Definition: transaction.h:300
std::string ToString() const
Definition: transaction.cpp:12
friend bool operator!=(const CTxOut &a, const CTxOut &b)
Definition: transaction.h:170
TxHash GetHash() const
Definition: transaction.cpp:52
const uint256 hash
Memory only.
Definition: transaction.h:234
bool IsNull() const
Definition: uint256.h:26
Dummy data type to identify deserializing constructors.
Definition: serialize.h:44
bool IsCoinBase() const
Definition: transaction.h:275
const std::vector< CTxIn > vin
Definition: transaction.h:227
friend bool operator==(const COutPoint &a, const COutPoint &b)
Definition: transaction.h:51
CMutableTransaction(deserialize_type, Stream &s)
Definition: transaction.h:316
CTxOut(Amount nValueIn, CScript scriptPubKeyIn)
Definition: transaction.h:148
int Compare(const base_blob &other) const
Definition: uint256.h:37
bool IsNull() const
Definition: transaction.h:164
std::vector< CTxIn > vin
Definition: transaction.h:299
static const int SERIALIZE_TRANSACTION
Definition: transaction.h:16
An input of a transaction.
Definition: transaction.h:67
CTxIn(COutPoint prevoutIn, CScript scriptSigIn=CScript(), uint32_t nSequenceIn=SEQUENCE_FINAL)
Definition: transaction.h:111
bool IsNull() const
Definition: transaction.h:259
void SerializationOp(Stream &s, Operation ser_action)
Definition: transaction.h:36
A TxHash is the double sha256 hash of the full transaction data.
Definition: txid.h:22
uint32_t n
Definition: transaction.h:25
const std::vector< CTxOut > vout
Definition: transaction.h:228
friend bool operator==(const CMutableTransaction &a, const CMutableTransaction &b)
Definition: transaction.h:328
void SerializeTransaction(const TxType &tx, Stream &s)
Definition: transaction.h:200
An output of a transaction.
Definition: transaction.h:141
static constexpr uint32_t NULL_INDEX
Definition: transaction.h:28
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:22
COutPoint(TxId txidIn, uint32_t nIn)
Definition: transaction.h:31
CTransaction(deserialize_type, Stream &s)
This deserializing constructor is provided instead of an Unserialize method.
Definition: transaction.h:256
void SerializationOp(Stream &s, Operation ser_action)
Definition: transaction.h:121
CScript scriptSig
Definition: transaction.h:70
256-bit opaque blob.
Definition: uint256.h:120
const int32_t nVersion
Definition: transaction.h:229
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:429
uint32_t nSequence
Definition: transaction.h:71
A TxId is the identifier of a transaction.
Definition: txid.h:14
void Serialize(Stream &s) const
Definition: transaction.h:307
void Serialize(Stream &s) const
Definition: transaction.h:246
A mutable version of CTransaction.
Definition: transaction.h:297
void Unserialize(Stream &s, char &a)
Definition: serialize.h:313
ADD_SERIALIZE_METHODS
Definition: transaction.h:151
bool IsNull() const
Definition: transaction.h:41
friend bool operator<(const COutPoint &a, const COutPoint &b)
Definition: transaction.h:46
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:211
friend bool operator==(const CTxIn &a, const CTxIn &b)
Definition: transaction.h:127
void UnserializeTransaction(TxType &tx, Stream &s)
Basic transaction serialization format:
Definition: transaction.h:187
#define READWRITE(...)
Definition: serialize.h:191
COutPoint prevout
Definition: transaction.h:69
friend bool operator!=(const CTxIn &a, const CTxIn &b)
Definition: transaction.h:132
void clear()
Definition: script.h:557
Amount nValue
Definition: transaction.h:143
const TxId GetId() const
Definition: transaction.h:261
const uint32_t nLockTime
Definition: transaction.h:230
friend bool operator==(const CTransaction &a, const CTransaction &b)
Definition: transaction.h:279
friend bool operator!=(const CTransaction &a, const CTransaction &b)
Definition: transaction.h:283