Bitcoin Core  21.99.0
P2P Digital Currency
transaction.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2020 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 
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  {
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;
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  // 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  explicit CTransaction(const CMutableTransaction& tx);
287 
288  template <typename Stream>
289  inline void Serialize(Stream& s) const {
290  SerializeTransaction(*this, s);
291  }
292 
295  template <typename Stream>
297 
298  bool IsNull() const {
299  return vin.empty() && vout.empty();
300  }
301 
302  const uint256& GetHash() const { return hash; }
303  const uint256& GetWitnessHash() const { return m_witness_hash; };
304 
305  // Return sum of txouts.
306  CAmount GetValueOut() const;
307 
313  unsigned int GetTotalSize() const;
314 
315  bool IsCoinBase() const
316  {
317  return (vin.size() == 1 && vin[0].prevout.IsNull());
318  }
319 
320  friend bool operator==(const CTransaction& a, const CTransaction& b)
321  {
322  return a.hash == b.hash;
323  }
324 
325  friend bool operator!=(const CTransaction& a, const CTransaction& b)
326  {
327  return a.hash != b.hash;
328  }
329 
330  std::string ToString() const;
331 
332  bool HasWitness() const
333  {
334  for (size_t i = 0; i < vin.size(); i++) {
335  if (!vin[i].scriptWitness.IsNull()) {
336  return true;
337  }
338  }
339  return false;
340  }
341 };
342 
345 {
346  std::vector<CTxIn> vin;
347  std::vector<CTxOut> vout;
348  int32_t nVersion;
349  uint32_t nLockTime;
350 
352  explicit CMutableTransaction(const CTransaction& tx);
353 
354  template <typename Stream>
355  inline void Serialize(Stream& s) const {
356  SerializeTransaction(*this, s);
357  }
358 
359 
360  template <typename Stream>
361  inline void Unserialize(Stream& s) {
362  UnserializeTransaction(*this, s);
363  }
364 
365  template <typename Stream>
367  Unserialize(s);
368  }
369 
373  uint256 GetHash() const;
374 
375  bool HasWitness() const
376  {
377  for (size_t i = 0; i < vin.size(); i++) {
378  if (!vin[i].scriptWitness.IsNull()) {
379  return true;
380  }
381  }
382  return false;
383  }
384 };
385 
386 typedef std::shared_ptr<const CTransaction> CTransactionRef;
387 template <typename Tx> static inline CTransactionRef MakeTransactionRef(Tx&& txIn) { return std::make_shared<const CTransaction>(std::forward<Tx>(txIn)); }
388 
390 class GenTxid
391 {
394 public:
395  GenTxid(bool is_wtxid, const uint256& hash) : m_is_wtxid(is_wtxid), m_hash(hash) {}
396  bool IsWtxid() const { return m_is_wtxid; }
397  const uint256& GetHash() const { return m_hash; }
398  friend bool operator==(const GenTxid& a, const GenTxid& b) { return a.m_is_wtxid == b.m_is_wtxid && a.m_hash == b.m_hash; }
399  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); }
400 };
401 
402 #endif // BITCOIN_PRIMITIVES_TRANSACTION_H
CTxIn
An input of a transaction.
Definition: transaction.h:65
COutPoint::operator<
friend bool operator<(const COutPoint &a, const COutPoint &b)
Definition: transaction.h:42
CMutableTransaction::vin
std::vector< CTxIn > vin
Definition: transaction.h:346
CTransaction::vin
const std::vector< CTxIn > vin
Definition: transaction.h:270
deserialize_type
Dummy data type to identify deserializing constructors.
Definition: serialize.h:47
CScriptWitness::IsNull
bool IsNull() const
Definition: script.h:565
GenTxid::GenTxid
GenTxid(bool is_wtxid, const uint256 &hash)
Definition: transaction.h:395
GenTxid
A generic txid reference (txid or wtxid).
Definition: transaction.h:390
CTransaction::Serialize
void Serialize(Stream &s) const
Definition: transaction.h:289
CTxOut::operator!=
friend bool operator!=(const CTxOut &a, const CTxOut &b)
Definition: transaction.h:160
COutPoint::SetNull
void SetNull()
Definition: transaction.h:39
flags
int flags
Definition: bitcoin-tx.cpp:512
COutPoint::operator==
friend bool operator==(const COutPoint &a, const COutPoint &b)
Definition: transaction.h:48
CTxOut::SERIALIZE_METHODS
SERIALIZE_METHODS(CTxOut, obj)
Definition: transaction.h:141
COutPoint::hash
uint256 hash
Definition: transaction.h:29
CMutableTransaction::nVersion
int32_t nVersion
Definition: transaction.h:348
uint256.h
CTransaction::nLockTime
const uint32_t nLockTime
Definition: transaction.h:273
base_blob::SetNull
void SetNull()
Definition: uint256.h:39
CTransaction::operator!=
friend bool operator!=(const CTransaction &a, const CTransaction &b)
Definition: transaction.h:325
MakeTransactionRef
static CTransactionRef MakeTransactionRef(Tx &&txIn)
Definition: transaction.h:387
CTransaction::GetValueOut
CAmount GetValueOut() const
Definition: transaction.cpp:83
CTransactionRef
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:386
CScript::clear
void clear()
Definition: script.h:548
CTxIn::SEQUENCE_LOCKTIME_MASK
static const uint32_t SEQUENCE_LOCKTIME_MASK
Definition: transaction.h:89
CTransaction
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:259
CTxIn::SEQUENCE_FINAL
static const uint32_t SEQUENCE_FINAL
Definition: transaction.h:75
GenTxid::m_hash
uint256 m_hash
Definition: transaction.h:393
CScriptWitness
Definition: script.h:556
COutPoint::IsNull
bool IsNull() const
Definition: transaction.h:40
CTxIn::nSequence
uint32_t nSequence
Definition: transaction.h:70
CTxIn::scriptWitness
CScriptWitness scriptWitness
Only serialized through CTransaction.
Definition: transaction.h:71
GenTxid::IsWtxid
bool IsWtxid() const
Definition: transaction.h:396
CTxOut::nValue
CAmount nValue
Definition: transaction.h:131
CTxIn::operator!=
friend bool operator!=(const CTxIn &a, const CTxIn &b)
Definition: transaction.h:117
CTransaction::IsCoinBase
bool IsCoinBase() const
Definition: transaction.h:315
CTxOut::IsNull
bool IsNull() const
Definition: transaction.h:149
base_blob::Compare
int Compare(const base_blob &other) const
Definition: uint256.h:44
CMutableTransaction::nLockTime
uint32_t nLockTime
Definition: transaction.h:349
CTxOut
An output of a transaction.
Definition: transaction.h:128
COutPoint::NULL_INDEX
static constexpr uint32_t NULL_INDEX
Definition: transaction.h:32
CTransaction::CTransaction
CTransaction(const CMutableTransaction &tx)
Convert a CMutableTransaction into a CTransaction.
Definition: transaction.cpp:80
CTxOut::CTxOut
CTxOut()
Definition: transaction.h:134
GenTxid::operator==
friend bool operator==(const GenTxid &a, const GenTxid &b)
Definition: transaction.h:398
CTransaction::vout
const std::vector< CTxOut > vout
Definition: transaction.h:271
CTxOut::scriptPubKey
CScript scriptPubKey
Definition: transaction.h:132
UnserializeTransaction
void UnserializeTransaction(TxType &tx, Stream &s)
Basic transaction serialization format:
Definition: transaction.h:188
CTxIn::operator==
friend bool operator==(const CTxIn &a, const CTxIn &b)
Definition: transaction.h:110
CAmount
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
CTxIn::SEQUENCE_LOCKTIME_GRANULARITY
static const int SEQUENCE_LOCKTIME_GRANULARITY
Definition: transaction.h:98
SERIALIZE_TRANSACTION_NO_WITNESS
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
COutPoint::operator!=
friend bool operator!=(const COutPoint &a, const COutPoint &b)
Definition: transaction.h:53
CTransaction::GetTotalSize
unsigned int GetTotalSize() const
Get the total transaction size in bytes, including witness data.
Definition: transaction.cpp:95
CTxIn::CTxIn
CTxIn()
Definition: transaction.h:100
CMutableTransaction::CMutableTransaction
CMutableTransaction(deserialize_type, Stream &s)
Definition: transaction.h:366
CTransaction::m_witness_hash
const uint256 m_witness_hash
Definition: transaction.h:278
CTxIn::SERIALIZE_METHODS
SERIALIZE_METHODS(CTxIn, obj)
Definition: transaction.h:108
COutPoint::COutPoint
COutPoint()
Definition: transaction.h:34
uint256
256-bit opaque blob.
Definition: uint256.h:124
READWRITE
#define READWRITE(...)
Definition: serialize.h:175
CMutableTransaction::HasWitness
bool HasWitness() const
Definition: transaction.h:375
COutPoint::SERIALIZE_METHODS
SERIALIZE_METHODS(COutPoint, obj)
Definition: transaction.h:37
GenTxid::operator<
friend bool operator<(const GenTxid &a, const GenTxid &b)
Definition: transaction.h:399
CScript
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:404
CTransaction::HasWitness
bool HasWitness() const
Definition: transaction.h:332
CTransaction::CURRENT_VERSION
static const int32_t CURRENT_VERSION
Definition: transaction.h:263
CMutableTransaction::Unserialize
void Unserialize(Stream &s)
Definition: transaction.h:361
CMutableTransaction::vout
std::vector< CTxOut > vout
Definition: transaction.h:347
CTransaction::operator==
friend bool operator==(const CTransaction &a, const CTransaction &b)
Definition: transaction.h:320
CTransaction::ToString
std::string ToString() const
Definition: transaction.cpp:100
base_blob::IsNull
bool IsNull() const
Definition: uint256.h:31
GenTxid::GetHash
const uint256 & GetHash() const
Definition: transaction.h:397
CTransaction::ComputeWitnessHash
uint256 ComputeWitnessHash() const
Definition: transaction.cpp:72
CTxIn::ToString
std::string ToString() const
Definition: transaction.cpp:33
COutPoint::n
uint32_t n
Definition: transaction.h:30
CTxOut::operator==
friend bool operator==(const CTxOut &a, const CTxOut &b)
Definition: transaction.h:154
CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG
static const uint32_t SEQUENCE_LOCKTIME_TYPE_FLAG
Definition: transaction.h:85
CTxIn::prevout
COutPoint prevout
Definition: transaction.h:68
CTxIn::scriptSig
CScript scriptSig
Definition: transaction.h:69
CTxOut::ToString
std::string ToString() const
Definition: transaction.cpp:54
CTransaction::IsNull
bool IsNull() const
Definition: transaction.h:298
serialize.h
CTransaction::CTransaction
CTransaction(deserialize_type, Stream &s)
This deserializing constructor is provided instead of an Unserialize method.
Definition: transaction.h:296
GenTxid::m_is_wtxid
bool m_is_wtxid
Definition: transaction.h:392
CTransaction::GetWitnessHash
const uint256 & GetWitnessHash() const
Definition: transaction.h:303
script.h
COutPoint::ToString
std::string ToString() const
Definition: transaction.cpp:14
CTransaction::GetHash
const uint256 & GetHash() const
Definition: transaction.h:302
COutPoint
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:26
CMutableTransaction::GetHash
uint256 GetHash() const
Compute the hash of this CMutableTransaction.
Definition: transaction.cpp:62
CTransaction::nVersion
const int32_t nVersion
Definition: transaction.h:272
CMutableTransaction::CMutableTransaction
CMutableTransaction()
Definition: transaction.cpp:59
CMutableTransaction
A mutable version of CTransaction.
Definition: transaction.h:344
SerializeTransaction
void SerializeTransaction(const TxType &tx, Stream &s)
Definition: transaction.h:227
amount.h
CTxOut::SetNull
void SetNull()
Definition: transaction.h:143
CTransaction::ComputeHash
uint256 ComputeHash() const
Definition: transaction.cpp:67
deserialize
constexpr deserialize_type deserialize
Definition: serialize.h:48
CTransaction::hash
const uint256 hash
Memory only.
Definition: transaction.h:277
CMutableTransaction::Serialize
void Serialize(Stream &s) const
Definition: transaction.h:355
CTxIn::SEQUENCE_LOCKTIME_DISABLE_FLAG
static const uint32_t SEQUENCE_LOCKTIME_DISABLE_FLAG
Definition: transaction.h:80
COutPoint::COutPoint
COutPoint(const uint256 &hashIn, uint32_t nIn)
Definition: transaction.h:35