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