Dash Core  0.12.2.1
P2P Digital Currency
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 class COutPoint
16 {
17 public:
19  uint32_t n;
20 
21  COutPoint() { SetNull(); }
22  COutPoint(uint256 hashIn, uint32_t nIn) { hash = hashIn; n = nIn; }
23 
25 
26  template <typename Stream, typename Operation>
27  inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
28  READWRITE(hash);
29  READWRITE(n);
30  }
31 
32  void SetNull() { hash.SetNull(); n = (uint32_t) -1; }
33  bool IsNull() const { return (hash.IsNull() && n == (uint32_t) -1); }
34 
35  friend bool operator<(const COutPoint& a, const COutPoint& b)
36  {
37  return (a.hash < b.hash || (a.hash == b.hash && a.n < b.n));
38  }
39 
40  friend bool operator==(const COutPoint& a, const COutPoint& b)
41  {
42  return (a.hash == b.hash && a.n == b.n);
43  }
44 
45  friend bool operator!=(const COutPoint& a, const COutPoint& b)
46  {
47  return !(a == b);
48  }
49 
50  std::string ToString() const;
51  std::string ToStringShort() const;
52 };
53 
58 class CTxIn
59 {
60 public:
63  uint32_t nSequence;
65 
66  /* Setting nSequence to this value for every input in a transaction
67  * disables nLockTime. */
68  static const uint32_t SEQUENCE_FINAL = 0xffffffff;
69 
70  /* Below flags apply in the context of BIP 68*/
71  /* If this flag set, CTxIn::nSequence is NOT interpreted as a
72  * relative lock-time. */
73  static const uint32_t SEQUENCE_LOCKTIME_DISABLE_FLAG = (1 << 31);
74 
75  /* If CTxIn::nSequence encodes a relative lock-time and this flag
76  * is set, the relative lock-time has units of 512 seconds,
77  * otherwise it specifies blocks with a granularity of 1. */
78  static const uint32_t SEQUENCE_LOCKTIME_TYPE_FLAG = (1 << 22);
79 
80  /* If CTxIn::nSequence encodes a relative lock-time, this mask is
81  * applied to extract that lock-time from the sequence field. */
82  static const uint32_t SEQUENCE_LOCKTIME_MASK = 0x0000ffff;
83 
84  /* In order to use the same number of bits to encode roughly the
85  * same wall-clock duration, and because blocks are naturally
86  * limited to occur every 600s on average, the minimum granularity
87  * for time-based relative lock-time is fixed at 512 seconds.
88  * Converting from CTxIn::nSequence to seconds is performed by
89  * multiplying by 512 = 2^9, or equivalently shifting up by
90  * 9 bits. */
91  static const int SEQUENCE_LOCKTIME_GRANULARITY = 9;
92 
94  {
96  }
97 
98  explicit CTxIn(COutPoint prevoutIn, CScript scriptSigIn=CScript(), uint32_t nSequenceIn=SEQUENCE_FINAL);
99  CTxIn(uint256 hashPrevTx, uint32_t nOut, CScript scriptSigIn=CScript(), uint32_t nSequenceIn=SEQUENCE_FINAL);
100 
102 
103  template <typename Stream, typename Operation>
104  inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
108  }
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  friend bool operator<(const CTxIn& a, const CTxIn& b)
123  {
124  return a.prevout<b.prevout;
125  }
126 
127  std::string ToString() const;
128 };
129 
133 class CTxOut
134 {
135 public:
138  int nRounds;
139 
141  {
142  SetNull();
143  }
144 
145  CTxOut(const CAmount& nValueIn, CScript scriptPubKeyIn);
146 
148 
149  template <typename Stream, typename Operation>
150  inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
151  READWRITE(nValue);
153  }
154 
155  void SetNull()
156  {
157  nValue = -1;
159  nRounds = -10; // an initial value, should be no way to get this by calculations
160  }
161 
162  bool IsNull() const
163  {
164  return (nValue == -1);
165  }
166 
167  uint256 GetHash() const;
168 
170  {
171  // "Dust" is defined in terms of CTransaction::minRelayTxFee, which has units duffs-per-kilobyte.
172  // If you'd pay more than 1/3 in fees to spend something, then we consider it dust.
173  // A typical spendable txout is 34 bytes big, and will need a CTxIn of at least 148 bytes to spend
174  // i.e. total is 148 + 32 = 182 bytes. Default -minrelaytxfee is 10000 duffs per kB
175  // and that means that fee per spendable txout is 182 * 10000 / 1000 = 1820 duffs.
176  // So dust is a spendable txout less than 546 * minRelayTxFee / 1000 (in duffs)
177  // i.e. 1820 * 3 = 5460 duffs with default -minrelaytxfee = minRelayTxFee = 10000 duffs per kB.
179  return 0;
180 
181  size_t nSize = GetSerializeSize(SER_DISK,0)+148u;
182  return 3*minRelayTxFee.GetFee(nSize);
183  }
184 
185  bool IsDust(const CFeeRate &minRelayTxFee) const
186  {
188  }
189 
190  friend bool operator==(const CTxOut& a, const CTxOut& b)
191  {
192  return (a.nValue == b.nValue &&
193  a.scriptPubKey == b.scriptPubKey &&
194  a.nRounds == b.nRounds);
195  }
196 
197  friend bool operator!=(const CTxOut& a, const CTxOut& b)
198  {
199  return !(a == b);
200  }
201 
202  std::string ToString() const;
203 };
204 
205 struct CMutableTransaction;
206 
211 {
212 private:
214  const uint256 hash;
215  void UpdateHash() const;
216 
217 public:
218  // Default transaction version.
219  static const int32_t CURRENT_VERSION=1;
220 
221  // Changing the default transaction version requires a two step process: first
222  // adapting relay policy by bumping MAX_STANDARD_VERSION, and then later date
223  // bumping the default CURRENT_VERSION at which point both CURRENT_VERSION and
224  // MAX_STANDARD_VERSION will be equal.
225  static const int32_t MAX_STANDARD_VERSION=2;
226 
227  // The local variables are made const to prevent unintended modification
228  // without updating the cached hash value. However, CTransaction is not
229  // actually immutable; deserialization and assignment are implemented,
230  // and bypass the constness. This is safe, as they update the entire
231  // structure, including the hash.
232  const int32_t nVersion;
233  const std::vector<CTxIn> vin;
234  const std::vector<CTxOut> vout;
235  const uint32_t nLockTime;
236 
238  CTransaction();
239 
242 
243  CTransaction& operator=(const CTransaction& tx);
244 
246 
247  template <typename Stream, typename Operation>
248  inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
249  READWRITE(*const_cast<int32_t*>(&this->nVersion));
250  nVersion = this->nVersion;
251  READWRITE(*const_cast<std::vector<CTxIn>*>(&vin));
252  READWRITE(*const_cast<std::vector<CTxOut>*>(&vout));
253  READWRITE(*const_cast<uint32_t*>(&nLockTime));
254  if (ser_action.ForRead())
255  UpdateHash();
256  }
257 
258  bool IsNull() const {
259  return vin.empty() && vout.empty();
260  }
261 
262  const uint256& GetHash() const {
263  return hash;
264  }
265 
266  // Return sum of txouts.
267  CAmount GetValueOut() const;
268  // GetValueIn() is a method on CCoinsViewCache, because
269  // inputs must be known to compute value in.
270 
271  // Compute priority, given priority of inputs and (optionally) tx size
272  double ComputePriority(double dPriorityInputs, unsigned int nTxSize=0) const;
273 
274  // Compute modified tx size for priority calculation (optionally given tx size)
275  unsigned int CalculateModifiedSize(unsigned int nTxSize=0) const;
276 
282  unsigned int GetTotalSize() const;
283 
284  bool IsCoinBase() const
285  {
286  return (vin.size() == 1 && vin[0].prevout.IsNull());
287  }
288 
289  friend bool operator==(const CTransaction& a, const CTransaction& b)
290  {
291  return a.hash == b.hash;
292  }
293 
294  friend bool operator!=(const CTransaction& a, const CTransaction& b)
295  {
296  return a.hash != b.hash;
297  }
298 
299  std::string ToString() const;
300 };
301 
304 {
305  int32_t nVersion;
306  std::vector<CTxIn> vin;
307  std::vector<CTxOut> vout;
308  uint32_t nLockTime;
309 
312 
314 
315  template <typename Stream, typename Operation>
316  inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
317  READWRITE(this->nVersion);
318  nVersion = this->nVersion;
319  READWRITE(vin);
320  READWRITE(vout);
322  }
323 
327  uint256 GetHash() const;
328 
329  std::string ToString() const;
330 
331  friend bool operator==(const CMutableTransaction& a, const CMutableTransaction& b)
332  {
333  return a.GetHash() == b.GetHash();
334  }
335 
336  friend bool operator!=(const CMutableTransaction& a, const CMutableTransaction& b)
337  {
338  return !(a == b);
339  }
340 
341 };
342 
347 {
348  inline bool operator()(const CTxIn& a, const CTxIn& b) const
349  {
350  if (a.prevout.hash == b.prevout.hash) return a.prevout.n < b.prevout.n;
351 
352  uint256 hasha = a.prevout.hash;
353  uint256 hashb = b.prevout.hash;
354 
355  typedef std::reverse_iterator<const unsigned char*> rev_it;
356  rev_it rita = rev_it(hasha.end());
357  rev_it ritb = rev_it(hashb.end());
358 
359  return std::lexicographical_compare(rita, rita + hasha.size(), ritb, ritb + hashb.size());
360  }
361 };
362 
364 {
365  inline bool operator()(const CTxOut& a, const CTxOut& b) const
366  {
367  return a.nValue < b.nValue || (a.nValue == b.nValue && a.scriptPubKey < b.scriptPubKey);
368  }
369 };
370 
371 #endif // BITCOIN_PRIMITIVES_TRANSACTION_H
uint32_t n
Definition: transaction.h:19
bool IsNull() const
Definition: transaction.h:258
uint256 GetHash() const
Definition: transaction.cpp:58
bool IsNull() const
Definition: transaction.h:33
const uint32_t nLockTime
Definition: transaction.h:235
void SetNull()
Definition: uint256.h:41
friend bool operator!=(const COutPoint &a, const COutPoint &b)
Definition: transaction.h:45
static const int32_t CURRENT_VERSION
Definition: transaction.h:219
#define READWRITE(obj)
Definition: serialize.h:175
ADD_SERIALIZE_METHODS
Definition: transaction.h:147
uint32_t nSequence
Definition: transaction.h:63
void UpdateHash() const
Definition: transaction.cpp:92
double ComputePriority(double dPriorityInputs, unsigned int nTxSize=0) const
friend bool operator==(const CTxOut &a, const CTxOut &b)
Definition: transaction.h:190
bool IsNull() const
Definition: transaction.h:162
CAmount nValue
Definition: transaction.h:136
static const uint32_t SEQUENCE_FINAL
Definition: transaction.h:68
std::string ToString() const
Definition: transaction.cpp:76
std::vector< CTxIn > vin
Definition: transaction.h:306
CAmount GetFee(size_t size) const
Definition: amount.cpp:20
CAmount GetValueOut() const
ADD_SERIALIZE_METHODS
Definition: transaction.h:24
bool IsNull() const
Definition: uint256.h:33
unsigned char * end()
Definition: uint256.h:60
void SerializationOp(Stream &s, Operation ser_action, int nType, int nVersion)
Definition: transaction.h:248
static const uint32_t SEQUENCE_LOCKTIME_MASK
Definition: transaction.h:82
friend bool operator<(const COutPoint &a, const COutPoint &b)
Definition: transaction.h:35
CScript scriptSig
Definition: transaction.h:62
int64_t CAmount
Definition: amount.h:14
bool IsUnspendable() const
Definition: script.h:634
void SerializationOp(Stream &s, Operation ser_action, int nType, int nVersion)
Definition: transaction.h:150
unsigned int GetTotalSize() const
bool operator()(const CTxOut &a, const CTxOut &b) const
Definition: transaction.h:365
const int32_t nVersion
Definition: transaction.h:232
COutPoint prevout
Definition: transaction.h:61
unsigned int GetSerializeSize(char a, int, int=0)
Definition: serialize.h:202
CFeeRate minRelayTxFee
Definition: validation.cpp:94
void SerializationOp(Stream &s, Operation ser_action, int nType, int nVersion)
Definition: transaction.h:27
std::string ToString() const
Definition: transaction.cpp:36
void SetNull()
Definition: transaction.h:32
const uint256 hash
Definition: transaction.h:214
friend bool operator!=(const CMutableTransaction &a, const CMutableTransaction &b)
Definition: transaction.h:336
friend bool operator==(const CTransaction &a, const CTransaction &b)
Definition: transaction.h:289
friend bool operator!=(const CTxOut &a, const CTxOut &b)
Definition: transaction.h:197
unsigned int size() const
Definition: uint256.h:75
friend bool operator==(const CTxIn &a, const CTxIn &b)
Definition: transaction.h:110
friend bool operator<(const CTxIn &a, const CTxIn &b)
Definition: transaction.h:122
CScript scriptPubKey
Definition: transaction.h:137
static const int SEQUENCE_LOCKTIME_GRANULARITY
Definition: transaction.h:91
std::vector< CTxOut > vout
Definition: transaction.h:307
unsigned int CalculateModifiedSize(unsigned int nTxSize=0) const
const std::vector< CTxIn > vin
Definition: transaction.h:233
static const int32_t MAX_STANDARD_VERSION
Definition: transaction.h:225
bool IsDust(const CFeeRate &minRelayTxFee) const
Definition: transaction.h:185
std::string ToString() const
Definition: transaction.cpp:12
friend bool operator==(const CMutableTransaction &a, const CMutableTransaction &b)
Definition: transaction.h:331
uint256 GetHash() const
Definition: transaction.cpp:71
std::string ToString() const
Definition: transaction.cpp:63
const uint256 & GetHash() const
Definition: transaction.h:262
std::string ToString() const
bool IsCoinBase() const
Definition: transaction.h:284
friend bool operator!=(const CTxIn &a, const CTxIn &b)
Definition: transaction.h:117
const std::vector< CTxOut > vout
Definition: transaction.h:234
ADD_SERIALIZE_METHODS
Definition: transaction.h:101
int nRounds
Definition: transaction.h:138
void SerializationOp(Stream &s, Operation ser_action, int nType, int nVersion)
Definition: transaction.h:104
CTransaction & operator=(const CTransaction &tx)
static const uint32_t SEQUENCE_LOCKTIME_TYPE_FLAG
Definition: transaction.h:78
void clear()
Definition: script.h:639
COutPoint(uint256 hashIn, uint32_t nIn)
Definition: transaction.h:22
static const uint32_t SEQUENCE_LOCKTIME_DISABLE_FLAG
Definition: transaction.h:73
friend bool operator!=(const CTransaction &a, const CTransaction &b)
Definition: transaction.h:294
void SetNull()
Definition: transaction.h:155
std::string ToStringShort() const
Definition: transaction.cpp:17
uint256 hash
Definition: transaction.h:18
bool operator()(const CTxIn &a, const CTxIn &b) const
Definition: transaction.h:348
friend bool operator==(const COutPoint &a, const COutPoint &b)
Definition: transaction.h:40
CScript prevPubKey
Definition: transaction.h:64
CAmount GetDustThreshold(const CFeeRate &minRelayTxFee) const
Definition: transaction.h:169
CTxIn()
Definition: transaction.h:93
void SerializationOp(Stream &s, Operation ser_action, int nType, int nVersion)
Definition: transaction.h:316