Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

transaction.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 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 <amount.h>
10 #include <script/script.h>
11 #include <serialize.h>
12 #include <uint256.h>
13 
15 enum {
23 };
24 
26 class COutPoint
27 {
28 public:
30  uint32_t n;
31 
32  COutPoint(): n((uint32_t) -1) { }
33  COutPoint(const uint256& hashIn, uint32_t nIn): hash(hashIn), n(nIn) { }
34 
36 
37  template <typename Stream, typename Operation>
38  inline void SerializationOp(Stream& s, Operation ser_action) {
39  READWRITE(hash);
40  READWRITE(n);
41  }
42 
43  void SetNull() { hash.SetNull(); n = (uint32_t) -1; }
44  bool IsNull() const { return (hash.IsNull() && n == (uint32_t) -1); }
45 
46  friend bool operator<(const COutPoint& a, const COutPoint& b)
47  {
48  int cmp = a.hash.Compare(b.hash);
49  return cmp < 0 || (cmp == 0 && a.n < b.n);
50  }
51 
52  friend bool operator==(const COutPoint& a, const COutPoint& b)
53  {
54  return (a.hash == b.hash && a.n == b.n);
55  }
56 
57  friend bool operator!=(const COutPoint& a, const COutPoint& b)
58  {
59  return !(a == b);
60  }
61 
62  std::string ToString() const;
63  std::string ToStringShort() const;
64 };
65 
70 class CTxIn
71 {
72 public:
75  uint32_t nSequence;
76 
77  /* Setting nSequence to this value for every input in a transaction
78  * disables nLockTime. */
79  static const uint32_t SEQUENCE_FINAL = 0xffffffff;
80 
81  /* Below flags apply in the context of BIP 68*/
82  /* If this flag set, CTxIn::nSequence is NOT interpreted as a
83  * relative lock-time. */
84  static const uint32_t SEQUENCE_LOCKTIME_DISABLE_FLAG = (1 << 31);
85 
86  /* If CTxIn::nSequence encodes a relative lock-time and this flag
87  * is set, the relative lock-time has units of 512 seconds,
88  * otherwise it specifies blocks with a granularity of 1. */
89  static const uint32_t SEQUENCE_LOCKTIME_TYPE_FLAG = (1 << 22);
90 
91  /* If CTxIn::nSequence encodes a relative lock-time, this mask is
92  * applied to extract that lock-time from the sequence field. */
93  static const uint32_t SEQUENCE_LOCKTIME_MASK = 0x0000ffff;
94 
95  /* In order to use the same number of bits to encode roughly the
96  * same wall-clock duration, and because blocks are naturally
97  * limited to occur every 600s on average, the minimum granularity
98  * for time-based relative lock-time is fixed at 512 seconds.
99  * Converting from CTxIn::nSequence to seconds is performed by
100  * multiplying by 512 = 2^9, or equivalently shifting up by
101  * 9 bits. */
102  static const int SEQUENCE_LOCKTIME_GRANULARITY = 9;
103 
105  {
107  }
108 
109  explicit CTxIn(COutPoint prevoutIn, CScript scriptSigIn=CScript(), uint32_t nSequenceIn=SEQUENCE_FINAL);
110  CTxIn(uint256 hashPrevTx, uint32_t nOut, CScript scriptSigIn=CScript(), uint32_t nSequenceIn=SEQUENCE_FINAL);
111 
113 
114  template <typename Stream, typename Operation>
115  inline void SerializationOp(Stream& s, Operation ser_action) {
119  }
120 
121  friend bool operator==(const CTxIn& a, const CTxIn& b)
122  {
123  return (a.prevout == b.prevout &&
124  a.scriptSig == b.scriptSig &&
125  a.nSequence == b.nSequence);
126  }
127 
128  friend bool operator!=(const CTxIn& a, const CTxIn& b)
129  {
130  return !(a == b);
131  }
132 
133  friend bool operator<(const CTxIn& a, const CTxIn& b)
134  {
135  return a.prevout<b.prevout;
136  }
137 
138  std::string ToString() const;
139 };
140 
144 class CTxOut
145 {
146 public:
149  int nRounds;
150 
152  {
153  SetNull();
154  }
155 
156  CTxOut(const CAmount& nValueIn, CScript scriptPubKeyIn, int nRoundsIn = -10);
157 
159 
160  template <typename Stream, typename Operation>
161  inline void SerializationOp(Stream& s, Operation ser_action) {
162  READWRITE(nValue);
164  }
165 
166  void SetNull()
167  {
168  nValue = -1;
170  nRounds = -10; // an initial value, should be no way to get this by calculations
171  }
172 
173  bool IsNull() const
174  {
175  return (nValue == -1);
176  }
177 
178  friend bool operator==(const CTxOut& a, const CTxOut& b)
179  {
180  return (a.nValue == b.nValue &&
181  a.scriptPubKey == b.scriptPubKey &&
182  a.nRounds == b.nRounds);
183  }
184 
185  friend bool operator!=(const CTxOut& a, const CTxOut& b)
186  {
187  return !(a == b);
188  }
189 
190  std::string ToString() const;
191 };
192 
193 struct CMutableTransaction;
194 
199 {
200 public:
201  // Default transaction version.
202  static const int32_t CURRENT_VERSION=2;
203 
204  // Changing the default transaction version requires a two step process: first
205  // adapting relay policy by bumping MAX_STANDARD_VERSION, and then later date
206  // bumping the default CURRENT_VERSION at which point both CURRENT_VERSION and
207  // MAX_STANDARD_VERSION will be equal.
208  static const int32_t MAX_STANDARD_VERSION=3;
209 
210  // The local variables are made const to prevent unintended modification
211  // without updating the cached hash value. However, CTransaction is not
212  // actually immutable; deserialization and assignment are implemented,
213  // and bypass the constness. This is safe, as they update the entire
214  // structure, including the hash.
215  const std::vector<CTxIn> vin;
216  const std::vector<CTxOut> vout;
217  const int16_t nVersion;
218  const int16_t nType;
219  const uint32_t nLockTime;
220  const std::vector<uint8_t> vExtraPayload; // only available for special transaction types
221 
222 private:
224  const uint256 hash;
225 
226  uint256 ComputeHash() const;
227 
228 public:
230  CTransaction();
231 
235 
236  template <typename Stream>
237  inline void Serialize(Stream& s) const {
238  int32_t n32bitVersion = this->nVersion | (this->nType << 16);
239  s << n32bitVersion;
240  s << vin;
241  s << vout;
242  s << nLockTime;
243  if (this->nVersion == 3 && this->nType != TRANSACTION_NORMAL)
244  s << vExtraPayload;
245  }
246 
249  template <typename Stream>
251 
252  bool IsNull() const {
253  return vin.empty() && vout.empty();
254  }
255 
256  const uint256& GetHash() const {
257  return hash;
258  }
259 
260  // Return sum of txouts.
261  CAmount GetValueOut() const;
262  // GetValueIn() is a method on CCoinsViewCache, because
263  // inputs must be known to compute value in.
264 
270  unsigned int GetTotalSize() const;
271 
272  bool IsCoinBase() const
273  {
274  return (vin.size() == 1 && vin[0].prevout.IsNull());
275  }
276 
277  friend bool operator==(const CTransaction& a, const CTransaction& b)
278  {
279  return a.hash == b.hash;
280  }
281 
282  friend bool operator!=(const CTransaction& a, const CTransaction& b)
283  {
284  return a.hash != b.hash;
285  }
286 
287  std::string ToString() const;
288 };
289 
292 {
293  std::vector<CTxIn> vin;
294  std::vector<CTxOut> vout;
295  int16_t nVersion;
296  int16_t nType;
297  uint32_t nLockTime;
298  std::vector<uint8_t> vExtraPayload; // only available for special transaction types
299 
302 
304 
305  template <typename Stream, typename Operation>
306  inline void SerializationOp(Stream& s, Operation ser_action) {
307  int32_t n32bitVersion = this->nVersion | (this->nType << 16);
308  READWRITE(n32bitVersion);
309  if (ser_action.ForRead()) {
310  this->nVersion = (int16_t) (n32bitVersion & 0xffff);
311  this->nType = (int16_t) ((n32bitVersion >> 16) & 0xffff);
312  }
313  READWRITE(vin);
314  READWRITE(vout);
316  if (this->nVersion == 3 && this->nType != TRANSACTION_NORMAL) {
318  }
319  }
320 
321  template <typename Stream>
323  Unserialize(s);
324  }
325 
329  uint256 GetHash() const;
330 
331  std::string ToString() const;
332 
333  friend bool operator==(const CMutableTransaction& a, const CMutableTransaction& b)
334  {
335  return a.GetHash() == b.GetHash();
336  }
337 
338  friend bool operator!=(const CMutableTransaction& a, const CMutableTransaction& b)
339  {
340  return !(a == b);
341  }
342 
343 };
344 
345 typedef std::shared_ptr<const CTransaction> CTransactionRef;
346 static inline CTransactionRef MakeTransactionRef() { return std::make_shared<const CTransaction>(); }
347 template <typename Tx> static inline CTransactionRef MakeTransactionRef(Tx&& txIn) { return std::make_shared<const CTransaction>(std::forward<Tx>(txIn)); }
348 
353 {
354  inline bool operator()(const CTxIn& a, const CTxIn& b) const
355  {
356  if (a.prevout.hash == b.prevout.hash) return a.prevout.n < b.prevout.n;
357 
358  uint256 hasha = a.prevout.hash;
359  uint256 hashb = b.prevout.hash;
360 
361  typedef std::reverse_iterator<const unsigned char*> rev_it;
362  rev_it rita = rev_it(hasha.end());
363  rev_it ritb = rev_it(hashb.end());
364 
365  return std::lexicographical_compare(rita, rita + hasha.size(), ritb, ritb + hashb.size());
366  }
367 };
368 
370 {
371  inline bool operator()(const CTxOut& a, const CTxOut& b) const
372  {
373  return a.nValue < b.nValue || (a.nValue == b.nValue && a.scriptPubKey < b.scriptPubKey);
374  }
375 };
376 
377 #endif // BITCOIN_PRIMITIVES_TRANSACTION_H
CAmount nValue
Definition: transaction.h:147
void SetNull()
Definition: transaction.h:166
static const int32_t MAX_STANDARD_VERSION
Definition: transaction.h:208
void SetNull()
Definition: uint256.h:41
friend bool operator!=(const COutPoint &a, const COutPoint &b)
Definition: transaction.h:57
CScript scriptPubKey
Definition: transaction.h:148
#define READWRITE(obj)
Definition: serialize.h:165
ADD_SERIALIZE_METHODS
Definition: transaction.h:35
COutPoint(const uint256 &hashIn, uint32_t nIn)
Definition: transaction.h:33
std::vector< CTxIn > vin
Definition: transaction.h:293
static const uint32_t SEQUENCE_FINAL
Definition: transaction.h:79
Implementation of BIP69 https://github.com/bitcoin/bips/blob/master/bip-0069.mediawiki.
Definition: transaction.h:352
static const uint32_t SEQUENCE_LOCKTIME_DISABLE_FLAG
Definition: transaction.h:84
constexpr deserialize_type deserialize
Definition: serialize.h:43
ADD_SERIALIZE_METHODS
Definition: transaction.h:112
friend bool operator==(const CTxOut &a, const CTxOut &b)
Definition: transaction.h:178
void SerializationOp(Stream &s, Operation ser_action)
Definition: transaction.h:161
static const int SEQUENCE_LOCKTIME_GRANULARITY
Definition: transaction.h:102
std::string ToString() const
Definition: transaction.cpp:36
std::string ToString() const
Definition: transaction.cpp:12
std::string ToString() const
Definition: transaction.cpp:58
friend bool operator!=(const CTxOut &a, const CTxOut &b)
Definition: transaction.h:185
const uint256 hash
Memory only.
Definition: transaction.h:224
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:345
bool IsNull() const
Definition: uint256.h:33
Dummy data type to identify deserializing constructors.
Definition: serialize.h:42
bool IsCoinBase() const
Definition: transaction.h:272
unsigned char * end()
Definition: uint256.h:62
const std::vector< CTxIn > vin
Definition: transaction.h:215
friend bool operator==(const COutPoint &a, const COutPoint &b)
Definition: transaction.h:52
CAmount GetValueOut() const
Definition: transaction.cpp:99
int Compare(const base_blob &other) const
Definition: uint256.h:46
bool IsNull() const
Definition: transaction.h:173
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
static CTransactionRef MakeTransactionRef()
Definition: transaction.h:346
An input of a transaction.
Definition: transaction.h:70
const uint256 & GetHash() const
Definition: transaction.h:256
bool IsNull() const
Definition: transaction.h:252
void SerializationOp(Stream &s, Operation ser_action)
Definition: transaction.h:38
uint32_t n
Definition: transaction.h:30
const std::vector< CTxOut > vout
Definition: transaction.h:216
An output of a transaction.
Definition: transaction.h:144
unsigned int size() const
Definition: uint256.h:77
static const uint32_t SEQUENCE_LOCKTIME_TYPE_FLAG
Definition: transaction.h:89
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:26
std::vector< CTxOut > vout
Definition: transaction.h:294
CTransaction(deserialize_type, Stream &s)
This deserializing constructor is provided instead of an Unserialize method.
Definition: transaction.h:250
friend bool operator==(const CMutableTransaction &a, const CMutableTransaction &b)
Definition: transaction.h:333
uint256 ComputeHash() const
Definition: transaction.cpp:89
void SetNull()
Definition: transaction.h:43
const int16_t nVersion
Definition: transaction.h:217
void SerializationOp(Stream &s, Operation ser_action)
Definition: transaction.h:115
CScript scriptSig
Definition: transaction.h:74
std::vector< uint8_t > vExtraPayload
Definition: transaction.h:298
256-bit opaque blob.
Definition: uint256.h:123
CMutableTransaction(deserialize_type, Stream &s)
Definition: transaction.h:322
void SerializationOp(Stream &s, Operation ser_action)
Definition: transaction.h:306
uint256 GetHash() const
Compute the hash of this CMutableTransaction.
Definition: transaction.cpp:66
friend bool operator<(const CTxIn &a, const CTxIn &b)
Definition: transaction.h:133
static const uint32_t SEQUENCE_LOCKTIME_MASK
Definition: transaction.h:93
std::string ToString() const
Definition: transaction.cpp:71
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:389
uint32_t nSequence
Definition: transaction.h:75
bool operator()(const CTxIn &a, const CTxIn &b) const
Definition: transaction.h:354
CTransaction()
Construct a CTransaction that qualifies as IsNull()
Definition: transaction.cpp:95
void Serialize(Stream &s) const
Definition: transaction.h:237
const std::vector< uint8_t > vExtraPayload
Definition: transaction.h:220
std::string ToString() const
int nRounds
Definition: transaction.h:149
void Unserialize(Stream &s, char &a)
Definition: serialize.h:196
bool operator()(const CTxOut &a, const CTxOut &b) const
Definition: transaction.h:371
A mutable version of CTransaction.
Definition: transaction.h:291
unsigned int GetTotalSize() const
Get the total transaction size in bytes, including witness data.
ADD_SERIALIZE_METHODS
Definition: transaction.h:158
bool IsNull() const
Definition: transaction.h:44
friend bool operator<(const COutPoint &a, const COutPoint &b)
Definition: transaction.h:46
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:198
static const int32_t CURRENT_VERSION
Definition: transaction.h:202
friend bool operator!=(const CMutableTransaction &a, const CMutableTransaction &b)
Definition: transaction.h:338
friend bool operator==(const CTxIn &a, const CTxIn &b)
Definition: transaction.h:121
COutPoint prevout
Definition: transaction.h:73
const int16_t nType
Definition: transaction.h:218
friend bool operator!=(const CTxIn &a, const CTxIn &b)
Definition: transaction.h:128
void clear()
Definition: script.h:664
const uint32_t nLockTime
Definition: transaction.h:219
friend bool operator==(const CTransaction &a, const CTransaction &b)
Definition: transaction.h:277
uint256 hash
Definition: transaction.h:29
friend bool operator!=(const CTransaction &a, const CTransaction &b)
Definition: transaction.h:282
std::string ToStringShort() const
Definition: transaction.cpp:17
Released under the MIT license