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