Bitcoin Core  22.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 <stdint.h>
10 #include <consensus/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 
80  static const uint32_t SEQUENCE_FINAL = 0xffffffff;
86  static const uint32_t MAX_SEQUENCE_NONFINAL{SEQUENCE_FINAL - 1};
87 
88  // Below flags apply in the context of BIP 68. BIP 68 requires the tx
89  // version to be set to 2, or higher.
97  static const uint32_t SEQUENCE_LOCKTIME_DISABLE_FLAG = (1U << 31);
98 
103  static const uint32_t SEQUENCE_LOCKTIME_TYPE_FLAG = (1 << 22);
104 
108  static const uint32_t SEQUENCE_LOCKTIME_MASK = 0x0000ffff;
109 
118  static const int SEQUENCE_LOCKTIME_GRANULARITY = 9;
119 
121  {
123  }
124 
125  explicit CTxIn(COutPoint prevoutIn, CScript scriptSigIn=CScript(), uint32_t nSequenceIn=SEQUENCE_FINAL);
126  CTxIn(uint256 hashPrevTx, uint32_t nOut, CScript scriptSigIn=CScript(), uint32_t nSequenceIn=SEQUENCE_FINAL);
127 
128  SERIALIZE_METHODS(CTxIn, obj) { READWRITE(obj.prevout, obj.scriptSig, obj.nSequence); }
129 
130  friend bool operator==(const CTxIn& a, const CTxIn& b)
131  {
132  return (a.prevout == b.prevout &&
133  a.scriptSig == b.scriptSig &&
134  a.nSequence == b.nSequence);
135  }
136 
137  friend bool operator!=(const CTxIn& a, const CTxIn& b)
138  {
139  return !(a == b);
140  }
141 
142  std::string ToString() const;
143 };
144 
148 class CTxOut
149 {
150 public:
153 
155  {
156  SetNull();
157  }
158 
159  CTxOut(const CAmount& nValueIn, CScript scriptPubKeyIn);
160 
161  SERIALIZE_METHODS(CTxOut, obj) { READWRITE(obj.nValue, obj.scriptPubKey); }
162 
163  void SetNull()
164  {
165  nValue = -1;
167  }
168 
169  bool IsNull() const
170  {
171  return (nValue == -1);
172  }
173 
174  friend bool operator==(const CTxOut& a, const CTxOut& b)
175  {
176  return (a.nValue == b.nValue &&
177  a.scriptPubKey == b.scriptPubKey);
178  }
179 
180  friend bool operator!=(const CTxOut& a, const CTxOut& b)
181  {
182  return !(a == b);
183  }
184 
185  std::string ToString() const;
186 };
187 
188 struct CMutableTransaction;
189 
207 template<typename Stream, typename TxType>
208 inline void UnserializeTransaction(TxType& tx, Stream& s) {
209  const bool fAllowWitness = !(s.GetVersion() & SERIALIZE_TRANSACTION_NO_WITNESS);
210 
211  s >> tx.nVersion;
212  unsigned char flags = 0;
213  tx.vin.clear();
214  tx.vout.clear();
215  /* Try to read the vin. In case the dummy is there, this will be read as an empty vector. */
216  s >> tx.vin;
217  if (tx.vin.size() == 0 && fAllowWitness) {
218  /* We read a dummy or an empty vin. */
219  s >> flags;
220  if (flags != 0) {
221  s >> tx.vin;
222  s >> tx.vout;
223  }
224  } else {
225  /* We read a non-empty vin. Assume a normal vout follows. */
226  s >> tx.vout;
227  }
228  if ((flags & 1) && fAllowWitness) {
229  /* The witness flag is present, and we support witnesses. */
230  flags ^= 1;
231  for (size_t i = 0; i < tx.vin.size(); i++) {
232  s >> tx.vin[i].scriptWitness.stack;
233  }
234  if (!tx.HasWitness()) {
235  /* It's illegal to encode witnesses when all witness stacks are empty. */
236  throw std::ios_base::failure("Superfluous witness record");
237  }
238  }
239  if (flags) {
240  /* Unknown flag in the serialization */
241  throw std::ios_base::failure("Unknown transaction optional data");
242  }
243  s >> tx.nLockTime;
244 }
245 
246 template<typename Stream, typename TxType>
247 inline void SerializeTransaction(const TxType& tx, Stream& s) {
248  const bool fAllowWitness = !(s.GetVersion() & SERIALIZE_TRANSACTION_NO_WITNESS);
249 
250  s << tx.nVersion;
251  unsigned char flags = 0;
252  // Consistency check
253  if (fAllowWitness) {
254  /* Check whether witnesses need to be serialized. */
255  if (tx.HasWitness()) {
256  flags |= 1;
257  }
258  }
259  if (flags) {
260  /* Use extended format in case witnesses are to be serialized. */
261  std::vector<CTxIn> vinDummy;
262  s << vinDummy;
263  s << flags;
264  }
265  s << tx.vin;
266  s << tx.vout;
267  if (flags & 1) {
268  for (size_t i = 0; i < tx.vin.size(); i++) {
269  s << tx.vin[i].scriptWitness.stack;
270  }
271  }
272  s << tx.nLockTime;
273 }
274 
275 
280 {
281 public:
282  // Default transaction version.
283  static const int32_t CURRENT_VERSION=2;
284 
285  // The local variables are made const to prevent unintended modification
286  // without updating the cached hash value. However, CTransaction is not
287  // actually immutable; deserialization and assignment are implemented,
288  // and bypass the constness. This is safe, as they update the entire
289  // structure, including the hash.
290  const std::vector<CTxIn> vin;
291  const std::vector<CTxOut> vout;
292  const int32_t nVersion;
293  const uint32_t nLockTime;
294 
295 private:
297  const uint256 hash;
299 
300  uint256 ComputeHash() const;
301  uint256 ComputeWitnessHash() const;
302 
303 public:
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 
333  unsigned int GetTotalSize() const;
334 
335  bool IsCoinBase() const
336  {
337  return (vin.size() == 1 && vin[0].prevout.IsNull());
338  }
339 
340  friend bool operator==(const CTransaction& a, const CTransaction& b)
341  {
342  return a.hash == b.hash;
343  }
344 
345  friend bool operator!=(const CTransaction& a, const CTransaction& b)
346  {
347  return a.hash != b.hash;
348  }
349 
350  std::string ToString() const;
351 
352  bool HasWitness() const
353  {
354  for (size_t i = 0; i < vin.size(); i++) {
355  if (!vin[i].scriptWitness.IsNull()) {
356  return true;
357  }
358  }
359  return false;
360  }
361 };
362 
365 {
366  std::vector<CTxIn> vin;
367  std::vector<CTxOut> vout;
368  int32_t nVersion;
369  uint32_t nLockTime;
370 
372  explicit CMutableTransaction(const CTransaction& tx);
373 
374  template <typename Stream>
375  inline void Serialize(Stream& s) const {
376  SerializeTransaction(*this, s);
377  }
378 
379 
380  template <typename Stream>
381  inline void Unserialize(Stream& s) {
382  UnserializeTransaction(*this, s);
383  }
384 
385  template <typename Stream>
387  Unserialize(s);
388  }
389 
393  uint256 GetHash() const;
394 
395  bool HasWitness() const
396  {
397  for (size_t i = 0; i < vin.size(); i++) {
398  if (!vin[i].scriptWitness.IsNull()) {
399  return true;
400  }
401  }
402  return false;
403  }
404 };
405 
406 typedef std::shared_ptr<const CTransaction> CTransactionRef;
407 template <typename Tx> static inline CTransactionRef MakeTransactionRef(Tx&& txIn) { return std::make_shared<const CTransaction>(std::forward<Tx>(txIn)); }
408 
410 class GenTxid
411 {
414  GenTxid(bool is_wtxid, const uint256& hash) : m_is_wtxid(is_wtxid), m_hash(hash) {}
415 
416 public:
417  static GenTxid Txid(const uint256& hash) { return GenTxid{false, hash}; }
418  static GenTxid Wtxid(const uint256& hash) { return GenTxid{true, hash}; }
419  bool IsWtxid() const { return m_is_wtxid; }
420  const uint256& GetHash() const { return m_hash; }
421  friend bool operator==(const GenTxid& a, const GenTxid& b) { return a.m_is_wtxid == b.m_is_wtxid && a.m_hash == b.m_hash; }
422  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); }
423 };
424 
425 #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:366
CTransaction::vin
const std::vector< CTxIn > vin
Definition: transaction.h:290
deserialize_type
Dummy data type to identify deserializing constructors.
Definition: serialize.h:47
CScriptWitness::IsNull
bool IsNull() const
Definition: script.h:566
GenTxid::GenTxid
GenTxid(bool is_wtxid, const uint256 &hash)
Definition: transaction.h:414
GenTxid
A generic txid reference (txid or wtxid).
Definition: transaction.h:410
CTransaction::Serialize
void Serialize(Stream &s) const
Definition: transaction.h:309
CTxOut::operator!=
friend bool operator!=(const CTxOut &a, const CTxOut &b)
Definition: transaction.h:180
COutPoint::SetNull
void SetNull()
Definition: transaction.h:39
flags
int flags
Definition: bitcoin-tx.cpp:529
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:161
COutPoint::hash
uint256 hash
Definition: transaction.h:29
CMutableTransaction::nVersion
int32_t nVersion
Definition: transaction.h:368
uint256.h
CTransaction::nLockTime
const uint32_t nLockTime
Definition: transaction.h:293
GenTxid::Wtxid
static GenTxid Wtxid(const uint256 &hash)
Definition: transaction.h:418
base_blob::SetNull
void SetNull()
Definition: uint256.h:41
CTransaction::operator!=
friend bool operator!=(const CTransaction &a, const CTransaction &b)
Definition: transaction.h:345
MakeTransactionRef
static CTransactionRef MakeTransactionRef(Tx &&txIn)
Definition: transaction.h:407
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:86
CTransaction::GetValueOut
CAmount GetValueOut() const
Definition: transaction.cpp:84
GenTxid::Txid
static GenTxid Txid(const uint256 &hash)
Definition: transaction.h:417
CTransactionRef
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:406
CScript::clear
void clear()
Definition: script.h:549
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:108
CTransaction
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:279
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:80
GenTxid::m_hash
uint256 m_hash
Definition: transaction.h:413
CScriptWitness
Definition: script.h:557
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:419
CTxOut::nValue
CAmount nValue
Definition: transaction.h:151
CTxIn::operator!=
friend bool operator!=(const CTxIn &a, const CTxIn &b)
Definition: transaction.h:137
CTransaction::IsCoinBase
bool IsCoinBase() const
Definition: transaction.h:335
CTxOut::IsNull
bool IsNull() const
Definition: transaction.h:169
base_blob::Compare
int Compare(const base_blob &other) const
Definition: uint256.h:46
CMutableTransaction::nLockTime
uint32_t nLockTime
Definition: transaction.h:369
CTxOut
An output of a transaction.
Definition: transaction.h:148
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:81
CTxOut::CTxOut
CTxOut()
Definition: transaction.h:154
GenTxid::operator==
friend bool operator==(const GenTxid &a, const GenTxid &b)
Definition: transaction.h:421
CTransaction::vout
const std::vector< CTxOut > vout
Definition: transaction.h:291
CTxOut::scriptPubKey
CScript scriptPubKey
Definition: transaction.h:152
UnserializeTransaction
void UnserializeTransaction(TxType &tx, Stream &s)
Basic transaction serialization format:
Definition: transaction.h:208
CTxIn::operator==
friend bool operator==(const CTxIn &a, const CTxIn &b)
Definition: transaction.h:130
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:118
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:96
CTxIn::CTxIn
CTxIn()
Definition: transaction.h:120
CMutableTransaction::CMutableTransaction
CMutableTransaction(deserialize_type, Stream &s)
Definition: transaction.h:386
CTransaction::m_witness_hash
const uint256 m_witness_hash
Definition: transaction.h:298
CTxIn::SERIALIZE_METHODS
SERIALIZE_METHODS(CTxIn, obj)
Definition: transaction.h:128
COutPoint::COutPoint
COutPoint()
Definition: transaction.h:34
uint256
256-bit opaque blob.
Definition: uint256.h:126
READWRITE
#define READWRITE(...)
Definition: serialize.h:140
CMutableTransaction::HasWitness
bool HasWitness() const
Definition: transaction.h:395
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:422
CScript
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:405
script.h
CTransaction::HasWitness
bool HasWitness() const
Definition: transaction.h:352
CTransaction::CURRENT_VERSION
static const int32_t CURRENT_VERSION
Definition: transaction.h:283
CMutableTransaction::Unserialize
void Unserialize(Stream &s)
Definition: transaction.h:381
CMutableTransaction::vout
std::vector< CTxOut > vout
Definition: transaction.h:367
CTransaction::operator==
friend bool operator==(const CTransaction &a, const CTransaction &b)
Definition: transaction.h:340
CTransaction::ToString
std::string ToString() const
Definition: transaction.cpp:101
base_blob::IsNull
bool IsNull() const
Definition: uint256.h:33
GenTxid::GetHash
const uint256 & GetHash() const
Definition: transaction.h:420
CTransaction::ComputeWitnessHash
uint256 ComputeWitnessHash() const
Definition: transaction.cpp:73
CTxIn::ToString
std::string ToString() const
Definition: transaction.cpp:34
COutPoint::n
uint32_t n
Definition: transaction.h:30
CTxOut::operator==
friend bool operator==(const CTxOut &a, const CTxOut &b)
Definition: transaction.h:174
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:103
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:55
CTransaction::IsNull
bool IsNull() const
Definition: transaction.h:318
serialize.h
CTransaction::CTransaction
CTransaction(deserialize_type, Stream &s)
This deserializing constructor is provided instead of an Unserialize method.
Definition: transaction.h:316
GenTxid::m_is_wtxid
bool m_is_wtxid
Definition: transaction.h:412
CTransaction::GetWitnessHash
const uint256 & GetWitnessHash() const
Definition: transaction.h:323
COutPoint::ToString
std::string ToString() const
Definition: transaction.cpp:15
CTransaction::GetHash
const uint256 & GetHash() const
Definition: transaction.h:322
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:63
CTransaction::nVersion
const int32_t nVersion
Definition: transaction.h:292
CMutableTransaction::CMutableTransaction
CMutableTransaction()
Definition: transaction.cpp:60
CMutableTransaction
A mutable version of CTransaction.
Definition: transaction.h:364
SerializeTransaction
void SerializeTransaction(const TxType &tx, Stream &s)
Definition: transaction.h:247
amount.h
CTxOut::SetNull
void SetNull()
Definition: transaction.h:163
CTransaction::ComputeHash
uint256 ComputeHash() const
Definition: transaction.cpp:68
deserialize
constexpr deserialize_type deserialize
Definition: serialize.h:48
CTransaction::hash
const uint256 hash
Memory only.
Definition: transaction.h:297
CMutableTransaction::Serialize
void Serialize(Stream &s) const
Definition: transaction.h:375
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:97
COutPoint::COutPoint
COutPoint(const uint256 &hashIn, uint32_t nIn)
Definition: transaction.h:35