Bitcoin Core  23.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-2021 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 <consensus/amount.h>
10 #include <prevector.h>
11 #include <script/script.h>
12 #include <serialize.h>
13 #include <uint256.h>
14 
15 #include <cstddef>
16 #include <cstdint>
17 #include <ios>
18 #include <limits>
19 #include <memory>
20 #include <string>
21 #include <tuple>
22 #include <utility>
23 #include <vector>
24 
31 static const int SERIALIZE_TRANSACTION_NO_WITNESS = 0x40000000;
32 
34 class COutPoint
35 {
36 public:
38  uint32_t n;
39 
40  static constexpr uint32_t NULL_INDEX = std::numeric_limits<uint32_t>::max();
41 
43  COutPoint(const uint256& hashIn, uint32_t nIn): hash(hashIn), n(nIn) { }
44 
45  SERIALIZE_METHODS(COutPoint, obj) { READWRITE(obj.hash, obj.n); }
46 
47  void SetNull() { hash.SetNull(); n = NULL_INDEX; }
48  bool IsNull() const { return (hash.IsNull() && n == NULL_INDEX); }
49 
50  friend bool operator<(const COutPoint& a, const COutPoint& b)
51  {
52  int cmp = a.hash.Compare(b.hash);
53  return cmp < 0 || (cmp == 0 && a.n < b.n);
54  }
55 
56  friend bool operator==(const COutPoint& a, const COutPoint& b)
57  {
58  return (a.hash == b.hash && a.n == b.n);
59  }
60 
61  friend bool operator!=(const COutPoint& a, const COutPoint& b)
62  {
63  return !(a == b);
64  }
65 
66  std::string ToString() const;
67 };
68 
73 class CTxIn
74 {
75 public:
78  uint32_t nSequence;
80 
88  static const uint32_t SEQUENCE_FINAL = 0xffffffff;
94  static const uint32_t MAX_SEQUENCE_NONFINAL{SEQUENCE_FINAL - 1};
95 
96  // Below flags apply in the context of BIP 68. BIP 68 requires the tx
97  // version to be set to 2, or higher.
105  static const uint32_t SEQUENCE_LOCKTIME_DISABLE_FLAG = (1U << 31);
106 
111  static const uint32_t SEQUENCE_LOCKTIME_TYPE_FLAG = (1 << 22);
112 
116  static const uint32_t SEQUENCE_LOCKTIME_MASK = 0x0000ffff;
117 
126  static const int SEQUENCE_LOCKTIME_GRANULARITY = 9;
127 
129  {
131  }
132 
133  explicit CTxIn(COutPoint prevoutIn, CScript scriptSigIn=CScript(), uint32_t nSequenceIn=SEQUENCE_FINAL);
134  CTxIn(uint256 hashPrevTx, uint32_t nOut, CScript scriptSigIn=CScript(), uint32_t nSequenceIn=SEQUENCE_FINAL);
135 
136  SERIALIZE_METHODS(CTxIn, obj) { READWRITE(obj.prevout, obj.scriptSig, obj.nSequence); }
137 
138  friend bool operator==(const CTxIn& a, const CTxIn& b)
139  {
140  return (a.prevout == b.prevout &&
141  a.scriptSig == b.scriptSig &&
142  a.nSequence == b.nSequence);
143  }
144 
145  friend bool operator!=(const CTxIn& a, const CTxIn& b)
146  {
147  return !(a == b);
148  }
149 
150  std::string ToString() const;
151 };
152 
156 class CTxOut
157 {
158 public:
161 
163  {
164  SetNull();
165  }
166 
167  CTxOut(const CAmount& nValueIn, CScript scriptPubKeyIn);
168 
169  SERIALIZE_METHODS(CTxOut, obj) { READWRITE(obj.nValue, obj.scriptPubKey); }
170 
171  void SetNull()
172  {
173  nValue = -1;
175  }
176 
177  bool IsNull() const
178  {
179  return (nValue == -1);
180  }
181 
182  friend bool operator==(const CTxOut& a, const CTxOut& b)
183  {
184  return (a.nValue == b.nValue &&
185  a.scriptPubKey == b.scriptPubKey);
186  }
187 
188  friend bool operator!=(const CTxOut& a, const CTxOut& b)
189  {
190  return !(a == b);
191  }
192 
193  std::string ToString() const;
194 };
195 
196 struct CMutableTransaction;
197 
215 template<typename Stream, typename TxType>
216 inline void UnserializeTransaction(TxType& tx, Stream& s) {
217  const bool fAllowWitness = !(s.GetVersion() & SERIALIZE_TRANSACTION_NO_WITNESS);
218 
219  s >> tx.nVersion;
220  unsigned char flags = 0;
221  tx.vin.clear();
222  tx.vout.clear();
223  /* Try to read the vin. In case the dummy is there, this will be read as an empty vector. */
224  s >> tx.vin;
225  if (tx.vin.size() == 0 && fAllowWitness) {
226  /* We read a dummy or an empty vin. */
227  s >> flags;
228  if (flags != 0) {
229  s >> tx.vin;
230  s >> tx.vout;
231  }
232  } else {
233  /* We read a non-empty vin. Assume a normal vout follows. */
234  s >> tx.vout;
235  }
236  if ((flags & 1) && fAllowWitness) {
237  /* The witness flag is present, and we support witnesses. */
238  flags ^= 1;
239  for (size_t i = 0; i < tx.vin.size(); i++) {
240  s >> tx.vin[i].scriptWitness.stack;
241  }
242  if (!tx.HasWitness()) {
243  /* It's illegal to encode witnesses when all witness stacks are empty. */
244  throw std::ios_base::failure("Superfluous witness record");
245  }
246  }
247  if (flags) {
248  /* Unknown flag in the serialization */
249  throw std::ios_base::failure("Unknown transaction optional data");
250  }
251  s >> tx.nLockTime;
252 }
253 
254 template<typename Stream, typename TxType>
255 inline void SerializeTransaction(const TxType& tx, Stream& s) {
256  const bool fAllowWitness = !(s.GetVersion() & SERIALIZE_TRANSACTION_NO_WITNESS);
257 
258  s << tx.nVersion;
259  unsigned char flags = 0;
260  // Consistency check
261  if (fAllowWitness) {
262  /* Check whether witnesses need to be serialized. */
263  if (tx.HasWitness()) {
264  flags |= 1;
265  }
266  }
267  if (flags) {
268  /* Use extended format in case witnesses are to be serialized. */
269  std::vector<CTxIn> vinDummy;
270  s << vinDummy;
271  s << flags;
272  }
273  s << tx.vin;
274  s << tx.vout;
275  if (flags & 1) {
276  for (size_t i = 0; i < tx.vin.size(); i++) {
277  s << tx.vin[i].scriptWitness.stack;
278  }
279  }
280  s << tx.nLockTime;
281 }
282 
283 
288 {
289 public:
290  // Default transaction version.
291  static const int32_t CURRENT_VERSION=2;
292 
293  // The local variables are made const to prevent unintended modification
294  // without updating the cached hash value. However, CTransaction is not
295  // actually immutable; deserialization and assignment are implemented,
296  // and bypass the constness. This is safe, as they update the entire
297  // structure, including the hash.
298  const std::vector<CTxIn> vin;
299  const std::vector<CTxOut> vout;
300  const int32_t nVersion;
301  const uint32_t nLockTime;
302 
303 private:
305  const uint256 hash;
307 
308  uint256 ComputeHash() const;
309  uint256 ComputeWitnessHash() const;
310 
311 public:
313  explicit CTransaction(const CMutableTransaction& tx);
314  explicit CTransaction(CMutableTransaction&& tx);
315 
316  template <typename Stream>
317  inline void Serialize(Stream& s) const {
318  SerializeTransaction(*this, s);
319  }
320 
323  template <typename Stream>
325 
326  bool IsNull() const {
327  return vin.empty() && vout.empty();
328  }
329 
330  const uint256& GetHash() const { return hash; }
331  const uint256& GetWitnessHash() const { return m_witness_hash; };
332 
333  // Return sum of txouts.
334  CAmount GetValueOut() const;
335 
341  unsigned int GetTotalSize() const;
342 
343  bool IsCoinBase() const
344  {
345  return (vin.size() == 1 && vin[0].prevout.IsNull());
346  }
347 
348  friend bool operator==(const CTransaction& a, const CTransaction& b)
349  {
350  return a.hash == b.hash;
351  }
352 
353  friend bool operator!=(const CTransaction& a, const CTransaction& b)
354  {
355  return a.hash != b.hash;
356  }
357 
358  std::string ToString() const;
359 
360  bool HasWitness() const
361  {
362  for (size_t i = 0; i < vin.size(); i++) {
363  if (!vin[i].scriptWitness.IsNull()) {
364  return true;
365  }
366  }
367  return false;
368  }
369 };
370 
373 {
374  std::vector<CTxIn> vin;
375  std::vector<CTxOut> vout;
376  int32_t nVersion;
377  uint32_t nLockTime;
378 
379  explicit CMutableTransaction();
380  explicit CMutableTransaction(const CTransaction& tx);
381 
382  template <typename Stream>
383  inline void Serialize(Stream& s) const {
384  SerializeTransaction(*this, s);
385  }
386 
387 
388  template <typename Stream>
389  inline void Unserialize(Stream& s) {
390  UnserializeTransaction(*this, s);
391  }
392 
393  template <typename Stream>
395  Unserialize(s);
396  }
397 
401  uint256 GetHash() const;
402 
403  bool HasWitness() const
404  {
405  for (size_t i = 0; i < vin.size(); i++) {
406  if (!vin[i].scriptWitness.IsNull()) {
407  return true;
408  }
409  }
410  return false;
411  }
412 };
413 
414 typedef std::shared_ptr<const CTransaction> CTransactionRef;
415 template <typename Tx> static inline CTransactionRef MakeTransactionRef(Tx&& txIn) { return std::make_shared<const CTransaction>(std::forward<Tx>(txIn)); }
416 
418 class GenTxid
419 {
422  GenTxid(bool is_wtxid, const uint256& hash) : m_is_wtxid(is_wtxid), m_hash(hash) {}
423 
424 public:
425  static GenTxid Txid(const uint256& hash) { return GenTxid{false, hash}; }
426  static GenTxid Wtxid(const uint256& hash) { return GenTxid{true, hash}; }
427  bool IsWtxid() const { return m_is_wtxid; }
428  const uint256& GetHash() const { return m_hash; }
429  friend bool operator==(const GenTxid& a, const GenTxid& b) { return a.m_is_wtxid == b.m_is_wtxid && a.m_hash == b.m_hash; }
430  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); }
431 };
432 
433 #endif // BITCOIN_PRIMITIVES_TRANSACTION_H
CTxIn
An input of a transaction.
Definition: transaction.h:73
COutPoint::operator<
friend bool operator<(const COutPoint &a, const COutPoint &b)
Definition: transaction.h:50
CMutableTransaction::vin
std::vector< CTxIn > vin
Definition: transaction.h:374
CTransaction::vin
const std::vector< CTxIn > vin
Definition: transaction.h:298
deserialize_type
Dummy data type to identify deserializing constructors.
Definition: serialize.h:47
CScriptWitness::IsNull
bool IsNull() const
Definition: script.h:571
GenTxid::GenTxid
GenTxid(bool is_wtxid, const uint256 &hash)
Definition: transaction.h:422
GenTxid
A generic txid reference (txid or wtxid).
Definition: transaction.h:418
CTransaction::Serialize
void Serialize(Stream &s) const
Definition: transaction.h:317
CTxOut::operator!=
friend bool operator!=(const CTxOut &a, const CTxOut &b)
Definition: transaction.h:188
COutPoint::SetNull
void SetNull()
Definition: transaction.h:47
flags
int flags
Definition: bitcoin-tx.cpp:526
COutPoint::operator==
friend bool operator==(const COutPoint &a, const COutPoint &b)
Definition: transaction.h:56
CTxOut::SERIALIZE_METHODS
SERIALIZE_METHODS(CTxOut, obj)
Definition: transaction.h:169
COutPoint::hash
uint256 hash
Definition: transaction.h:37
CMutableTransaction::nVersion
int32_t nVersion
Definition: transaction.h:376
uint256.h
CTransaction::nLockTime
const uint32_t nLockTime
Definition: transaction.h:301
GenTxid::Wtxid
static GenTxid Wtxid(const uint256 &hash)
Definition: transaction.h:426
base_blob::SetNull
void SetNull()
Definition: uint256.h:41
CTransaction::operator!=
friend bool operator!=(const CTransaction &a, const CTransaction &b)
Definition: transaction.h:353
MakeTransactionRef
static CTransactionRef MakeTransactionRef(Tx &&txIn)
Definition: transaction.h:415
CTxIn::MAX_SEQUENCE_NONFINAL
static const uint32_t MAX_SEQUENCE_NONFINAL
This is the maximum sequence number that enables both nLockTime and OP_CHECKLOCKTIMEVERIFY (BIP 65).
Definition: transaction.h:94
CTransaction::GetValueOut
CAmount GetValueOut() const
Definition: transaction.cpp:89
GenTxid::Txid
static GenTxid Txid(const uint256 &hash)
Definition: transaction.h:425
CTransactionRef
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:414
CScript::clear
void clear()
Definition: script.h:554
CTxIn::SEQUENCE_LOCKTIME_MASK
static const uint32_t SEQUENCE_LOCKTIME_MASK
If CTxIn::nSequence encodes a relative lock-time, this mask is applied to extract that lock-time from...
Definition: transaction.h:116
CTransaction
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:287
CTxIn::SEQUENCE_FINAL
static const uint32_t SEQUENCE_FINAL
Setting nSequence to this value for every input in a transaction disables nLockTime/IsFinalTx().
Definition: transaction.h:88
GenTxid::m_hash
uint256 m_hash
Definition: transaction.h:421
CScriptWitness
Definition: script.h:562
COutPoint::IsNull
bool IsNull() const
Definition: transaction.h:48
CTxIn::nSequence
uint32_t nSequence
Definition: transaction.h:78
CTxIn::scriptWitness
CScriptWitness scriptWitness
Only serialized through CTransaction.
Definition: transaction.h:79
GenTxid::IsWtxid
bool IsWtxid() const
Definition: transaction.h:427
CTxOut::nValue
CAmount nValue
Definition: transaction.h:159
CTxIn::operator!=
friend bool operator!=(const CTxIn &a, const CTxIn &b)
Definition: transaction.h:145
CTransaction::IsCoinBase
bool IsCoinBase() const
Definition: transaction.h:343
CTxOut::IsNull
bool IsNull() const
Definition: transaction.h:177
base_blob::Compare
int Compare(const base_blob &other) const
Definition: uint256.h:46
CMutableTransaction::nLockTime
uint32_t nLockTime
Definition: transaction.h:377
CTxOut
An output of a transaction.
Definition: transaction.h:156
COutPoint::NULL_INDEX
static constexpr uint32_t NULL_INDEX
Definition: transaction.h:40
CTransaction::CTransaction
CTransaction(const CMutableTransaction &tx)
Convert a CMutableTransaction into a CTransaction.
Definition: transaction.cpp:86
CTxOut::CTxOut
CTxOut()
Definition: transaction.h:162
GenTxid::operator==
friend bool operator==(const GenTxid &a, const GenTxid &b)
Definition: transaction.h:429
CTransaction::vout
const std::vector< CTxOut > vout
Definition: transaction.h:299
CTxOut::scriptPubKey
CScript scriptPubKey
Definition: transaction.h:160
UnserializeTransaction
void UnserializeTransaction(TxType &tx, Stream &s)
Basic transaction serialization format:
Definition: transaction.h:216
CTxIn::operator==
friend bool operator==(const CTxIn &a, const CTxIn &b)
Definition: transaction.h:138
CAmount
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
CTxIn::SEQUENCE_LOCKTIME_GRANULARITY
static const int SEQUENCE_LOCKTIME_GRANULARITY
In order to use the same number of bits to encode roughly the same wall-clock duration,...
Definition: transaction.h:126
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:31
COutPoint::operator!=
friend bool operator!=(const COutPoint &a, const COutPoint &b)
Definition: transaction.h:61
CTransaction::GetTotalSize
unsigned int GetTotalSize() const
Get the total transaction size in bytes, including witness data.
Definition: transaction.cpp:101
CTxIn::CTxIn
CTxIn()
Definition: transaction.h:128
CMutableTransaction::CMutableTransaction
CMutableTransaction(deserialize_type, Stream &s)
Definition: transaction.h:394
CTransaction::m_witness_hash
const uint256 m_witness_hash
Definition: transaction.h:306
CTxIn::SERIALIZE_METHODS
SERIALIZE_METHODS(CTxIn, obj)
Definition: transaction.h:136
COutPoint::COutPoint
COutPoint()
Definition: transaction.h:42
uint256
256-bit opaque blob.
Definition: uint256.h:126
READWRITE
#define READWRITE(...)
Definition: serialize.h:140
CMutableTransaction::HasWitness
bool HasWitness() const
Definition: transaction.h:403
COutPoint::SERIALIZE_METHODS
SERIALIZE_METHODS(COutPoint, obj)
Definition: transaction.h:45
GenTxid::operator<
friend bool operator<(const GenTxid &a, const GenTxid &b)
Definition: transaction.h:430
CScript
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:410
script.h
CTransaction::HasWitness
bool HasWitness() const
Definition: transaction.h:360
CTransaction::CURRENT_VERSION
static const int32_t CURRENT_VERSION
Definition: transaction.h:291
CMutableTransaction::Unserialize
void Unserialize(Stream &s)
Definition: transaction.h:389
CMutableTransaction::vout
std::vector< CTxOut > vout
Definition: transaction.h:375
CTransaction::operator==
friend bool operator==(const CTransaction &a, const CTransaction &b)
Definition: transaction.h:348
CTransaction::ToString
std::string ToString() const
Definition: transaction.cpp:106
base_blob::IsNull
bool IsNull() const
Definition: uint256.h:33
GenTxid::GetHash
const uint256 & GetHash() const
Definition: transaction.h:428
CTransaction::ComputeWitnessHash
uint256 ComputeWitnessHash() const
Definition: transaction.cpp:78
CTxIn::ToString
std::string ToString() const
Definition: transaction.cpp:39
COutPoint::n
uint32_t n
Definition: transaction.h:38
CTxOut::operator==
friend bool operator==(const CTxOut &a, const CTxOut &b)
Definition: transaction.h:182
CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG
static const uint32_t SEQUENCE_LOCKTIME_TYPE_FLAG
If CTxIn::nSequence encodes a relative lock-time and this flag is set, the relative lock-time has uni...
Definition: transaction.h:111
CTxIn::prevout
COutPoint prevout
Definition: transaction.h:76
CTxIn::scriptSig
CScript scriptSig
Definition: transaction.h:77
CTxOut::ToString
std::string ToString() const
Definition: transaction.cpp:60
CTransaction::IsNull
bool IsNull() const
Definition: transaction.h:326
serialize.h
CTransaction::CTransaction
CTransaction(deserialize_type, Stream &s)
This deserializing constructor is provided instead of an Unserialize method.
Definition: transaction.h:324
GenTxid::m_is_wtxid
bool m_is_wtxid
Definition: transaction.h:420
CTransaction::GetWitnessHash
const uint256 & GetWitnessHash() const
Definition: transaction.h:331
COutPoint::ToString
std::string ToString() const
Definition: transaction.cpp:20
CTransaction::GetHash
const uint256 & GetHash() const
Definition: transaction.h:330
prevector.h
COutPoint
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:34
CMutableTransaction::GetHash
uint256 GetHash() const
Compute the hash of this CMutableTransaction.
Definition: transaction.cpp:68
CTransaction::nVersion
const int32_t nVersion
Definition: transaction.h:300
CMutableTransaction::CMutableTransaction
CMutableTransaction()
Definition: transaction.cpp:65
CMutableTransaction
A mutable version of CTransaction.
Definition: transaction.h:372
SerializeTransaction
void SerializeTransaction(const TxType &tx, Stream &s)
Definition: transaction.h:255
amount.h
CTxOut::SetNull
void SetNull()
Definition: transaction.h:171
CTransaction::ComputeHash
uint256 ComputeHash() const
Definition: transaction.cpp:73
deserialize
constexpr deserialize_type deserialize
Definition: serialize.h:48
CTransaction::hash
const uint256 hash
Memory only.
Definition: transaction.h:305
CMutableTransaction::Serialize
void Serialize(Stream &s) const
Definition: transaction.h:383
CTxIn::SEQUENCE_LOCKTIME_DISABLE_FLAG
static const uint32_t SEQUENCE_LOCKTIME_DISABLE_FLAG
If this flag is set, CTxIn::nSequence is NOT interpreted as a relative lock-time.
Definition: transaction.h:105
COutPoint::COutPoint
COutPoint(const uint256 &hashIn, uint32_t nIn)
Definition: transaction.h:43