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