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