Bitcoin ABC  0.24.7
P2P Digital Currency
transaction.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin Core developers
3 // Copyright (c) 2017-2019 The Bitcoin developers
4 // Distributed under the MIT software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
7 #ifndef BITCOIN_PRIMITIVES_TRANSACTION_H
8 #define BITCOIN_PRIMITIVES_TRANSACTION_H
9 
10 #include <amount.h>
11 #include <feerate.h>
12 #include <primitives/txid.h>
13 #include <script/script.h>
14 #include <serialize.h>
15 
16 static const int SERIALIZE_TRANSACTION = 0x00;
17 
22 class COutPoint {
23 private:
25  uint32_t n;
26 
27 public:
28  static constexpr uint32_t NULL_INDEX = std::numeric_limits<uint32_t>::max();
29 
31  COutPoint(TxId txidIn, uint32_t nIn) : txid(txidIn), n(nIn) {}
32 
33  SERIALIZE_METHODS(COutPoint, obj) { READWRITE(obj.txid, obj.n); }
34 
35  bool IsNull() const { return txid.IsNull() && n == NULL_INDEX; }
36 
37  const TxId &GetTxId() const { return txid; }
38  uint32_t GetN() const { return n; }
39 
40  friend bool operator<(const COutPoint &a, const COutPoint &b) {
41  int cmp = a.txid.Compare(b.txid);
42  return cmp < 0 || (cmp == 0 && a.n < b.n);
43  }
44 
45  friend bool operator==(const COutPoint &a, const COutPoint &b) {
46  return (a.txid == b.txid && a.n == b.n);
47  }
48 
49  friend bool operator!=(const COutPoint &a, const COutPoint &b) {
50  return !(a == b);
51  }
52 
53  std::string ToString() const;
54 };
55 
61 class CTxIn {
62 public:
65  uint32_t nSequence;
66 
71  static const uint32_t SEQUENCE_FINAL = 0xffffffff;
72 
73  /* Below flags apply in the context of BIP 68*/
78  static const uint32_t SEQUENCE_LOCKTIME_DISABLE_FLAG = (1U << 31);
79 
85  static const uint32_t SEQUENCE_LOCKTIME_TYPE_FLAG = (1 << 22);
86 
91  static const uint32_t SEQUENCE_LOCKTIME_MASK = 0x0000ffff;
92 
101  static const int SEQUENCE_LOCKTIME_GRANULARITY = 9;
102 
104 
105  explicit CTxIn(COutPoint prevoutIn, CScript scriptSigIn = CScript(),
106  uint32_t nSequenceIn = SEQUENCE_FINAL)
107  : prevout(prevoutIn), scriptSig(scriptSigIn), nSequence(nSequenceIn) {}
108  CTxIn(TxId prevTxId, uint32_t nOut, CScript scriptSigIn = CScript(),
109  uint32_t nSequenceIn = SEQUENCE_FINAL)
110  : CTxIn(COutPoint(prevTxId, nOut), scriptSigIn, nSequenceIn) {}
111 
113  READWRITE(obj.prevout, obj.scriptSig, obj.nSequence);
114  }
115 
116  friend bool operator==(const CTxIn &a, const CTxIn &b) {
117  return (a.prevout == b.prevout && a.scriptSig == b.scriptSig &&
118  a.nSequence == b.nSequence);
119  }
120 
121  friend bool operator!=(const CTxIn &a, const CTxIn &b) { return !(a == b); }
122 
123  std::string ToString() const;
124 };
125 
130 class CTxOut {
131 public:
134 
135  CTxOut() { SetNull(); }
136 
137  CTxOut(Amount nValueIn, CScript scriptPubKeyIn)
138  : nValue(nValueIn), scriptPubKey(scriptPubKeyIn) {}
139 
140  SERIALIZE_METHODS(CTxOut, obj) { READWRITE(obj.nValue, obj.scriptPubKey); }
141 
142  void SetNull() {
143  nValue = -SATOSHI;
145  }
146 
147  bool IsNull() const { return nValue == -SATOSHI; }
148 
149  friend bool operator==(const CTxOut &a, const CTxOut &b) {
150  return (a.nValue == b.nValue && a.scriptPubKey == b.scriptPubKey);
151  }
152 
153  friend bool operator!=(const CTxOut &a, const CTxOut &b) {
154  return !(a == b);
155  }
156 
157  std::string ToString() const;
158 };
159 
160 class CMutableTransaction;
161 
169 template <typename Stream, typename TxType>
170 inline void UnserializeTransaction(TxType &tx, Stream &s) {
171  s >> tx.nVersion;
172  tx.vin.clear();
173  tx.vout.clear();
174  /* Try to read the vin. In case the dummy is there, this will be read as an
175  * empty vector. */
176  s >> tx.vin;
177  /* We read a non-empty vin. Assume a normal vout follows. */
178  s >> tx.vout;
179  s >> tx.nLockTime;
180 }
181 
182 template <typename Stream, typename TxType>
183 inline void SerializeTransaction(const TxType &tx, Stream &s) {
184  s << tx.nVersion;
185  s << tx.vin;
186  s << tx.vout;
187  s << tx.nLockTime;
188 }
189 
195 public:
196  // Default transaction version.
197  static const int32_t CURRENT_VERSION = 2;
198 
199  // Changing the default transaction version requires a two step process:
200  // first adapting relay policy by bumping MAX_STANDARD_VERSION, and then
201  // later date bumping the default CURRENT_VERSION at which point both
202  // CURRENT_VERSION and MAX_STANDARD_VERSION will be equal.
203  static const int32_t MAX_STANDARD_VERSION = 2;
204 
205  // The local variables are made const to prevent unintended modification
206  // without updating the cached hash value. However, CTransaction is not
207  // actually immutable; deserialization and assignment are implemented,
208  // and bypass the constness. This is safe, as they update the entire
209  // structure, including the hash.
210  const std::vector<CTxIn> vin;
211  const std::vector<CTxOut> vout;
212  const int32_t nVersion;
213  const uint32_t nLockTime;
214 
215 private:
217  const uint256 hash;
218 
219  uint256 ComputeHash() const;
220 
221 public:
223  CTransaction();
224 
226  explicit CTransaction(const CMutableTransaction &tx);
227  explicit CTransaction(CMutableTransaction &&tx);
228 
229  template <typename Stream> inline void Serialize(Stream &s) const {
230  SerializeTransaction(*this, s);
231  }
232 
238  template <typename Stream>
241 
242  bool IsNull() const { return vin.empty() && vout.empty(); }
243 
244  const TxId GetId() const { return TxId(hash); }
245  const TxHash GetHash() const { return TxHash(hash); }
246 
247  // Return sum of txouts.
248  Amount GetValueOut() const;
249 
254  unsigned int GetTotalSize() const;
255 
256  bool IsCoinBase() const {
257  return (vin.size() == 1 && vin[0].prevout.IsNull());
258  }
259 
260  friend bool operator==(const CTransaction &a, const CTransaction &b) {
261  return a.GetHash() == b.GetHash();
262  }
263 
264  friend bool operator!=(const CTransaction &a, const CTransaction &b) {
265  return !(a == b);
266  }
267 
268  std::string ToString() const;
269 };
270 #if defined(__x86_64__)
271 static_assert(sizeof(CTransaction) == 88,
272  "sizeof CTransaction is expected to be 88 bytes");
273 #endif
274 
279 public:
280  std::vector<CTxIn> vin;
281  std::vector<CTxOut> vout;
282  int32_t nVersion;
283  uint32_t nLockTime;
284 
286  explicit CMutableTransaction(const CTransaction &tx);
287 
288  template <typename Stream> inline void Serialize(Stream &s) const {
289  SerializeTransaction(*this, s);
290  }
291 
292  template <typename Stream> inline void Unserialize(Stream &s) {
293  UnserializeTransaction(*this, s);
294  }
295 
296  template <typename Stream>
298  Unserialize(s);
299  }
300 
306  TxId GetId() const;
307  TxHash GetHash() const;
308 
309  friend bool operator==(const CMutableTransaction &a,
310  const CMutableTransaction &b) {
311  return a.GetHash() == b.GetHash();
312  }
313 };
314 #if defined(__x86_64__)
315 static_assert(sizeof(CMutableTransaction) == 56,
316  "sizeof CMutableTransaction is expected to be 56 bytes");
317 #endif
318 
319 using CTransactionRef = std::shared_ptr<const CTransaction>;
321  return std::make_shared<const CTransaction>();
322 }
323 template <typename Tx>
324 static inline CTransactionRef MakeTransactionRef(Tx &&txIn) {
325  return std::make_shared<const CTransaction>(std::forward<Tx>(txIn));
326 }
327 
331 
334 
336  default;
338  operator=(const PrecomputedTransactionData &txdata) = default;
339 
340  template <class T> explicit PrecomputedTransactionData(const T &tx);
341 };
342 
343 #endif // BITCOIN_PRIMITIVES_TRANSACTION_H
CTxIn
An input of a transaction.
Definition: transaction.h:61
COutPoint::operator<
friend bool operator<(const COutPoint &a, const COutPoint &b)
Definition: transaction.h:40
CTransaction::vin
const std::vector< CTxIn > vin
Definition: transaction.h:210
deserialize_type
Dummy data type to identify deserializing constructors.
Definition: serialize.h:48
feerate.h
CTxOut::nValue
Amount nValue
Definition: transaction.h:132
MakeTransactionRef
static CTransactionRef MakeTransactionRef()
Definition: transaction.h:320
COutPoint::COutPoint
COutPoint(TxId txidIn, uint32_t nIn)
Definition: transaction.h:31
PrecomputedTransactionData::PrecomputedTransactionData
PrecomputedTransactionData()
Definition: transaction.h:332
CTransaction::Serialize
void Serialize(Stream &s) const
Definition: transaction.h:229
CTxOut::operator!=
friend bool operator!=(const CTxOut &a, const CTxOut &b)
Definition: transaction.h:153
COutPoint::operator==
friend bool operator==(const COutPoint &a, const COutPoint &b)
Definition: transaction.h:45
CTxOut::SERIALIZE_METHODS
SERIALIZE_METHODS(CTxOut, obj)
Definition: transaction.h:140
CTxIn::CTxIn
CTxIn(TxId prevTxId, uint32_t nOut, CScript scriptSigIn=CScript(), uint32_t nSequenceIn=SEQUENCE_FINAL)
Definition: transaction.h:108
CTransaction::nLockTime
const uint32_t nLockTime
Definition: transaction.h:213
CMutableTransaction::operator==
friend bool operator==(const CMutableTransaction &a, const CMutableTransaction &b)
Definition: transaction.h:309
CTransaction::operator!=
friend bool operator!=(const CTransaction &a, const CTransaction &b)
Definition: transaction.h:264
CMutableTransaction::CMutableTransaction
CMutableTransaction()
Definition: transaction.cpp:40
CTransaction::GetHash
const TxHash GetHash() const
Definition: transaction.h:245
CScript::clear
void clear()
Definition: script.h:553
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:91
CTransaction
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:194
CTxIn::SEQUENCE_FINAL
static const uint32_t SEQUENCE_FINAL
Setting nSequence to this value for every input in a transaction disables nLockTime.
Definition: transaction.h:71
COutPoint::IsNull
bool IsNull() const
Definition: transaction.h:35
CTxIn::nSequence
uint32_t nSequence
Definition: transaction.h:65
CTxIn::operator!=
friend bool operator!=(const CTxIn &a, const CTxIn &b)
Definition: transaction.h:121
SATOSHI
static constexpr Amount SATOSHI
Definition: amount.h:153
CTransaction::IsCoinBase
bool IsCoinBase() const
Definition: transaction.h:256
CTxOut::IsNull
bool IsNull() const
Definition: transaction.h:147
TxHash
A TxHash is the double sha256 hash of the full transaction data.
Definition: txid.h:22
base_blob::Compare
int Compare(const base_blob &other) const
Definition: uint256.h:41
CMutableTransaction::Serialize
void Serialize(Stream &s) const
Definition: transaction.h:288
COutPoint::GetTxId
const TxId & GetTxId() const
Definition: transaction.h:37
CTxOut
An output of a transaction.
Definition: transaction.h:130
CTransaction::GetId
const TxId GetId() const
Definition: transaction.h:244
COutPoint::txid
TxId txid
Definition: transaction.h:24
CMutableTransaction::nVersion
int32_t nVersion
Definition: transaction.h:282
COutPoint::NULL_INDEX
static constexpr uint32_t NULL_INDEX
Definition: transaction.h:28
CTxOut::CTxOut
CTxOut()
Definition: transaction.h:135
CTransaction::vout
const std::vector< CTxOut > vout
Definition: transaction.h:211
CTxOut::scriptPubKey
CScript scriptPubKey
Definition: transaction.h:133
CTxIn::CTxIn
CTxIn(COutPoint prevoutIn, CScript scriptSigIn=CScript(), uint32_t nSequenceIn=SEQUENCE_FINAL)
Definition: transaction.h:105
UnserializeTransaction
void UnserializeTransaction(TxType &tx, Stream &s)
Basic transaction serialization format:
Definition: transaction.h:170
CTxIn::operator==
friend bool operator==(const CTxIn &a, const CTxIn &b)
Definition: transaction.h:116
CTxOut::CTxOut
CTxOut(Amount nValueIn, CScript scriptPubKeyIn)
Definition: transaction.h:137
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:101
COutPoint::operator!=
friend bool operator!=(const COutPoint &a, const COutPoint &b)
Definition: transaction.h:49
CTransaction::GetTotalSize
unsigned int GetTotalSize() const
Get the total transaction size in bytes.
Definition: transaction.cpp:90
CTxIn::CTxIn
CTxIn()
Definition: transaction.h:103
CTxIn::SERIALIZE_METHODS
SERIALIZE_METHODS(CTxIn, obj)
Definition: transaction.h:112
PrecomputedTransactionData
Precompute sighash midstate to avoid quadratic hashing.
Definition: transaction.h:329
COutPoint::COutPoint
COutPoint()
Definition: transaction.h:30
uint256
256-bit opaque blob.
Definition: uint256.h:127
READWRITE
#define READWRITE(...)
Definition: serialize.h:179
PrecomputedTransactionData::hashPrevouts
uint256 hashPrevouts
Definition: transaction.h:330
TxId
A TxId is the identifier of a transaction.
Definition: txid.h:14
CTransaction::CTransaction
CTransaction()
Construct a CTransaction that qualifies as IsNull()
Definition: transaction.cpp:66
COutPoint::SERIALIZE_METHODS
SERIALIZE_METHODS(COutPoint, obj)
Definition: transaction.h:33
CMutableTransaction::GetId
TxId GetId() const
Compute the id and hash of this CMutableTransaction.
Definition: transaction.cpp:50
Amount
Definition: amount.h:19
CScript
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:430
CTransaction::MAX_STANDARD_VERSION
static const int32_t MAX_STANDARD_VERSION
Definition: transaction.h:203
PrecomputedTransactionData::hashOutputs
uint256 hashOutputs
Definition: transaction.h:330
CTransaction::GetValueOut
Amount GetValueOut() const
Definition: transaction.cpp:76
CTransaction::CURRENT_VERSION
static const int32_t CURRENT_VERSION
Definition: transaction.h:197
CTransaction::operator==
friend bool operator==(const CTransaction &a, const CTransaction &b)
Definition: transaction.h:260
CMutableTransaction::Unserialize
void Unserialize(Stream &s)
Definition: transaction.h:292
CTransaction::ToString
std::string ToString() const
Definition: transaction.cpp:94
base_blob::IsNull
bool IsNull() const
Definition: uint256.h:30
CMutableTransaction::vout
std::vector< CTxOut > vout
Definition: transaction.h:281
CTxIn::ToString
std::string ToString() const
Definition: transaction.cpp:18
COutPoint::n
uint32_t n
Definition: transaction.h:25
CTxOut::operator==
friend bool operator==(const CTxOut &a, const CTxOut &b)
Definition: transaction.h:149
CMutableTransaction::GetHash
TxHash GetHash() const
Definition: transaction.cpp:54
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:85
CTxIn::prevout
COutPoint prevout
Definition: transaction.h:63
CTxIn::scriptSig
CScript scriptSig
Definition: transaction.h:64
CMutableTransaction::nLockTime
uint32_t nLockTime
Definition: transaction.h:283
CTxOut::ToString
std::string ToString() const
Definition: transaction.cpp:34
CTransactionRef
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:319
CTransaction::IsNull
bool IsNull() const
Definition: transaction.h:242
serialize.h
CTransaction::CTransaction
CTransaction(deserialize_type, Stream &s)
This deserializing constructor is provided instead of an Unserialize method.
Definition: transaction.h:239
script.h
COutPoint::ToString
std::string ToString() const
Definition: transaction.cpp:14
COutPoint::GetN
uint32_t GetN() const
Definition: transaction.h:38
PrecomputedTransactionData::operator=
PrecomputedTransactionData & operator=(const PrecomputedTransactionData &txdata)=default
COutPoint
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:22
txid.h
CTransaction::nVersion
const int32_t nVersion
Definition: transaction.h:212
CMutableTransaction::CMutableTransaction
CMutableTransaction(deserialize_type, Stream &s)
Definition: transaction.h:297
SerializeTransaction
void SerializeTransaction(const TxType &tx, Stream &s)
Definition: transaction.h:183
amount.h
CMutableTransaction::vin
std::vector< CTxIn > vin
Definition: transaction.h:280
PrecomputedTransactionData::hashSequence
uint256 hashSequence
Definition: transaction.h:330
CTxOut::SetNull
void SetNull()
Definition: transaction.h:142
CTransaction::ComputeHash
uint256 ComputeHash() const
Definition: transaction.cpp:58
deserialize
constexpr deserialize_type deserialize
Definition: serialize.h:49
CTransaction::hash
const uint256 hash
Memory only.
Definition: transaction.h:217
SERIALIZE_TRANSACTION
static const int SERIALIZE_TRANSACTION
Definition: transaction.h:16
CMutableTransaction
A mutable version of CTransaction.
Definition: transaction.h:278
CTxIn::SEQUENCE_LOCKTIME_DISABLE_FLAG
static const uint32_t SEQUENCE_LOCKTIME_DISABLE_FLAG
If this flag set, CTxIn::nSequence is NOT interpreted as a relative lock-time.
Definition: transaction.h:78