Fabcoin Core  0.16.2
P2P Digital Currency
transaction.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2017 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 FABCOIN_PRIMITIVES_TRANSACTION_H
7 #define FABCOIN_PRIMITIVES_TRANSACTION_H
8 
9 #include <stdint.h>
10 #include <amount.h>
11 #include <script/script.h>
12 #include <serialize.h>
13 #include <uint256.h>
14 
15 static const int SERIALIZE_TRANSACTION_NO_WITNESS = 0x40000000;
16 
18 class COutPoint
19 {
20 public:
22  uint32_t n;
23 
24  COutPoint(): n((uint32_t) -1) { }
25  COutPoint(const uint256& hashIn, uint32_t nIn): hash(hashIn), n(nIn) { }
26 
28 
29  template <typename Stream, typename Operation>
30  inline void SerializationOp(Stream& s, Operation ser_action) {
31  READWRITE(hash);
32  READWRITE(n);
33  }
34 
35  void SetNull() { hash.SetNull(); n = (uint32_t) -1; }
36  bool IsNull() const { return (hash.IsNull() && n == (uint32_t) -1); }
37 
38  friend bool operator<(const COutPoint& a, const COutPoint& b)
39  {
40  int cmp = a.hash.Compare(b.hash);
41  return cmp < 0 || (cmp == 0 && a.n < b.n);
42  }
43 
44  friend bool operator==(const COutPoint& a, const COutPoint& b)
45  {
46  return (a.hash == b.hash && a.n == b.n);
47  }
48 
49  friend bool operator!=(const COutPoint& a, const COutPoint& b)
50  {
51  return !(a == b);
52  }
53 
54  std::string ToString() const;
55 };
56 
61 class CTxIn
62 {
63 public:
66  uint32_t nSequence;
68 
69  /* Setting nSequence to this value for every input in a transaction
70  * disables nLockTime. */
71  static const uint32_t SEQUENCE_FINAL = 0xffffffff;
72 
73  /* Below flags apply in the context of BIP 68*/
74  /* If this flag set, CTxIn::nSequence is NOT interpreted as a
75  * relative lock-time. */
76  static const uint32_t SEQUENCE_LOCKTIME_DISABLE_FLAG = (1 << 31);
77 
78  /* If CTxIn::nSequence encodes a relative lock-time and this flag
79  * is set, the relative lock-time has units of 512 seconds,
80  * otherwise it specifies blocks with a granularity of 1. */
81  static const uint32_t SEQUENCE_LOCKTIME_TYPE_FLAG = (1 << 22);
82 
83  /* If CTxIn::nSequence encodes a relative lock-time, this mask is
84  * applied to extract that lock-time from the sequence field. */
85  static const uint32_t SEQUENCE_LOCKTIME_MASK = 0x0000ffff;
86 
87  /* In order to use the same number of bits to encode roughly the
88  * same wall-clock duration, and because blocks are naturally
89  * limited to occur every 600s on average, the minimum granularity
90  * for time-based relative lock-time is fixed at 512 seconds.
91  * Converting from CTxIn::nSequence to seconds is performed by
92  * multiplying by 512 = 2^9, or equivalently shifting up by
93  * 9 bits. */
94  static const int SEQUENCE_LOCKTIME_GRANULARITY = 9;
95 
97  {
98  nSequence = SEQUENCE_FINAL;
99  }
100 
101  explicit CTxIn(COutPoint prevoutIn, CScript scriptSigIn=CScript(), uint32_t nSequenceIn=SEQUENCE_FINAL);
102  CTxIn(uint256 hashPrevTx, uint32_t nOut, CScript scriptSigIn=CScript(), uint32_t nSequenceIn=SEQUENCE_FINAL);
103 
105 
106  template <typename Stream, typename Operation>
107  inline void SerializationOp(Stream& s, Operation ser_action) {
108  READWRITE(prevout);
109  READWRITE(scriptSig);
110  READWRITE(nSequence);
111  }
112 
113  friend bool operator==(const CTxIn& a, const CTxIn& b)
114  {
115  return (a.prevout == b.prevout &&
116  a.scriptSig == b.scriptSig &&
117  a.nSequence == b.nSequence);
118  }
119 
120  friend bool operator!=(const CTxIn& a, const CTxIn& b)
121  {
122  return !(a == b);
123  }
124 
125  std::string ToString() const;
126 };
127 
131 class CTxOut
132 {
133 public:
136 
138  {
139  SetNull();
140  }
141 
142  CTxOut(const CAmount& nValueIn, CScript scriptPubKeyIn);
143 
145 
146  template <typename Stream, typename Operation>
147  inline void SerializationOp(Stream& s, Operation ser_action) {
148  READWRITE(nValue);
149  READWRITE(scriptPubKey);
150  }
151 
152  void SetNull()
153  {
154  nValue = -1;
155  scriptPubKey.clear();
156  }
157 
158  bool IsNull() const
159  {
160  return (nValue == -1);
161  }
162 
163  void SetEmpty()
164  {
165  nValue = 0;
166  scriptPubKey.clear();
167  }
168 
169  bool IsEmpty() const
170  {
171  return (nValue == 0 && scriptPubKey.empty());
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  }
235  if (flags) {
236  /* Unknown flag in the serialization */
237  throw std::ios_base::failure("Unknown transaction optional data");
238  }
239  s >> tx.nLockTime;
240 }
241 
242 template<typename Stream, typename TxType>
243 inline void SerializeTransaction(const TxType& tx, Stream& s) {
244  const bool fAllowWitness = !(s.GetVersion() & SERIALIZE_TRANSACTION_NO_WITNESS);
245 
246  s << tx.nVersion;
247  unsigned char flags = 0;
248  // Consistency check
249  if (fAllowWitness) {
250  /* Check whether witnesses need to be serialized. */
251  if (tx.HasWitness()) {
252  flags |= 1;
253  }
254  }
255  if (flags) {
256  /* Use extended format in case witnesses are to be serialized. */
257  std::vector<CTxIn> vinDummy;
258  s << vinDummy;
259  s << flags;
260  }
261  s << tx.vin;
262  s << tx.vout;
263  if (flags & 1) {
264  for (size_t i = 0; i < tx.vin.size(); i++) {
265  s << tx.vin[i].scriptWitness.stack;
266  }
267  }
268  s << tx.nLockTime;
269 }
270 
271 
276 {
277 public:
278  // Default transaction version.
279  static const int32_t CURRENT_VERSION=2;
280 
281  // Changing the default transaction version requires a two step process: first
282  // adapting relay policy by bumping MAX_STANDARD_VERSION, and then later date
283  // bumping the default CURRENT_VERSION at which point both CURRENT_VERSION and
284  // MAX_STANDARD_VERSION will be equal.
285  static const int32_t MAX_STANDARD_VERSION=2;
286 
287  // The local variables are made const to prevent unintended modification
288  // without updating the cached hash value. However, CTransaction is not
289  // actually immutable; deserialization and assignment are implemented,
290  // and bypass the constness. This is safe, as they update the entire
291  // structure, including the hash.
292  const std::vector<CTxIn> vin;
293  const std::vector<CTxOut> vout;
294  const int32_t nVersion;
295  const uint32_t nLockTime;
296 
297 private:
299  const uint256 hash;
300 
301  uint256 ComputeHash() const;
302 
303 public:
305  CTransaction();
306 
310 
311  template <typename Stream>
312  inline void Serialize(Stream& s) const {
313  SerializeTransaction(*this, s);
314  }
315 
318  template <typename Stream>
320 
321  bool IsNull() const {
322  return vin.empty() && vout.empty();
323  }
324 
325  const uint256& GetHash() const {
326  return hash;
327  }
328 
329  // Compute a hash that includes both transaction and witness data
330  uint256 GetWitnessHash() const;
331 
332  // Return sum of txouts.
333  CAmount GetValueOut() const;
334  // GetValueIn() is a method on CCoinsViewCache, because
335  // inputs must be known to compute value in.
336 
342  unsigned int GetTotalSize() const;
343 
345  bool HasCreateOrCall() const;
346  bool HasOpSpend() const;
348 
349  bool IsCoinBase() const
350  {
351  return (vin.size() == 1 && vin[0].prevout.IsNull() && vout.size() >= 1);
352  }
353 
354  bool IsCoinStake() const
355  {
356  // ppcoin: the coin stake transaction is marked with the first output empty
357  return (vin.size() > 0 && (!vin[0].prevout.IsNull()) && vout.size() >= 2 && vout[0].IsEmpty());
358  }
359 
360  bool IsNormalTx() const
361  {
362  // not coin base or coin stake transaction
363  return !IsCoinBase() && !IsCoinStake();
364  }
365 
366  friend bool operator==(const CTransaction& a, const CTransaction& b)
367  {
368  return a.hash == b.hash;
369  }
370 
371  friend bool operator!=(const CTransaction& a, const CTransaction& b)
372  {
373  return a.hash != b.hash;
374  }
375 
376  std::string ToString() const;
377 
378  bool HasWitness() const
379  {
380  for (size_t i = 0; i < vin.size(); i++) {
381  if (!vin[i].scriptWitness.IsNull()) {
382  return true;
383  }
384  }
385  return false;
386  }
387 };
388 
391 {
392  std::vector<CTxIn> vin;
393  std::vector<CTxOut> vout;
394  int32_t nVersion;
395  uint32_t nLockTime;
396 
399 
400  template <typename Stream>
401  inline void Serialize(Stream& s) const {
402  SerializeTransaction(*this, s);
403  }
404 
405 
406  template <typename Stream>
407  inline void Unserialize(Stream& s) {
408  UnserializeTransaction(*this, s);
409  }
410 
411  template <typename Stream>
413  Unserialize(s);
414  }
415 
419  uint256 GetHash() const;
420 
422  {
423  return a.GetHash() == b.GetHash();
424  }
425 
426  bool HasWitness() const
427  {
428  for (size_t i = 0; i < vin.size(); i++) {
429  if (!vin[i].scriptWitness.IsNull()) {
430  return true;
431  }
432  }
433  return false;
434  }
435 };
436 
437 typedef std::shared_ptr<const CTransaction> CTransactionRef;
438 static inline CTransactionRef MakeTransactionRef() { return std::make_shared<const CTransaction>(); }
439 template <typename Tx> static inline CTransactionRef MakeTransactionRef(Tx&& txIn) { return std::make_shared<const CTransaction>(std::forward<Tx>(txIn)); }
440 
441 int64_t GetTransactionWeight(const CTransaction &tx);
442 #endif // FABCOIN_PRIMITIVES_TRANSACTION_H
CAmount nValue
Definition: transaction.h:134
void SetEmpty()
Definition: transaction.h:163
void SetNull()
Definition: transaction.h:152
void SetNull()
Definition: uint256.h:46
friend bool operator!=(const COutPoint &a, const COutPoint &b)
Definition: transaction.h:49
CScript scriptPubKey
Definition: transaction.h:135
#define READWRITE(obj)
Definition: serialize.h:179
bool IsNull() const
Definition: transaction.h:158
ADD_SERIALIZE_METHODS
Definition: transaction.h:27
COutPoint(const uint256 &hashIn, uint32_t nIn)
Definition: transaction.h:25
std::vector< CTxIn > vin
Definition: transaction.h:392
CScriptWitness scriptWitness
Definition: transaction.h:67
constexpr deserialize_type deserialize
Definition: serialize.h:42
ADD_SERIALIZE_METHODS
Definition: transaction.h:104
friend bool operator==(const CTxOut &a, const CTxOut &b)
Definition: transaction.h:174
bool HasWitness() const
Definition: transaction.h:378
bool empty() const
Definition: prevector.h:286
void SerializationOp(Stream &s, Operation ser_action)
Definition: transaction.h:147
void Serialize(Stream &s) const
Definition: transaction.h:401
friend bool operator!=(const CTxOut &a, const CTxOut &b)
Definition: transaction.h:180
void UnserializeTransaction(TxType &tx, Stream &s)
Basic transaction serialization format:
Definition: transaction.h:208
const uint256 hash
Memory only.
Definition: transaction.h:299
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:437
Dummy data type to identify deserializing constructors.
Definition: serialize.h:41
const std::vector< CTxIn > vin
Definition: transaction.h:292
friend bool operator==(const COutPoint &a, const COutPoint &b)
Definition: transaction.h:44
void SerializeTransaction(const TxType &tx, Stream &s)
Definition: transaction.h:243
int64_t CAmount
Amount in lius (Can be negative)
Definition: amount.h:15
#define a(i)
An input of a transaction.
Definition: transaction.h:61
bool IsNull() const
Definition: uint256.h:38
void Unserialize(Stream &s)
Definition: transaction.h:407
void SerializationOp(Stream &s, Operation ser_action)
Definition: transaction.h:30
uint32_t n
Definition: transaction.h:22
const std::vector< CTxOut > vout
Definition: transaction.h:293
An output of a transaction.
Definition: transaction.h:131
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:18
std::vector< CTxOut > vout
Definition: transaction.h:393
CTransaction(deserialize_type, Stream &s)
This deserializing constructor is provided instead of an Unserialize method.
Definition: transaction.h:319
friend bool operator==(const CMutableTransaction &a, const CMutableTransaction &b)
Definition: transaction.h:421
bool IsCoinStake() const
Definition: transaction.h:354
uint256 GetHash() const
Compute the hash of this CMutableTransaction.
Definition: transaction.cpp:61
#define b(i, j)
void SetNull()
Definition: transaction.h:35
void SerializationOp(Stream &s, Operation ser_action)
Definition: transaction.h:107
CScript scriptSig
Definition: transaction.h:65
std::string ToString() const
Definition: transaction.cpp:13
256-bit opaque blob.
Definition: uint256.h:132
CMutableTransaction(deserialize_type, Stream &s)
Definition: transaction.h:412
const int32_t nVersion
Definition: transaction.h:294
int64_t GetTransactionWeight(const CTransaction &tx)
bool IsNormalTx() const
Definition: transaction.h:360
bool IsEmpty() const
Definition: transaction.h:169
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:417
CTxIn()
Definition: transaction.h:96
uint32_t nSequence
Definition: transaction.h:66
void Unserialize(Stream &s, char &a)
Definition: serialize.h:210
int Compare(const base_blob &other) const
Definition: uint256.h:51
bool IsCoinBase() const
Definition: transaction.h:349
void Serialize(Stream &s) const
Definition: transaction.h:312
A mutable version of CTransaction.
Definition: transaction.h:390
const uint256 & GetHash() const
Definition: transaction.h:325
bool IsNull() const
Definition: transaction.h:36
ADD_SERIALIZE_METHODS
Definition: transaction.h:144
friend bool operator<(const COutPoint &a, const COutPoint &b)
Definition: transaction.h:38
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:275
friend bool operator==(const CTxIn &a, const CTxIn &b)
Definition: transaction.h:113
COutPoint prevout
Definition: transaction.h:64
friend bool operator!=(const CTxIn &a, const CTxIn &b)
Definition: transaction.h:120
void clear()
Definition: script.h:712
bool IsNull() const
Definition: transaction.h:321
const uint32_t nLockTime
Definition: transaction.h:295
friend bool operator==(const CTransaction &a, const CTransaction &b)
Definition: transaction.h:366
bool HasWitness() const
Definition: transaction.h:426
uint256 hash
Definition: transaction.h:21
friend bool operator!=(const CTransaction &a, const CTransaction &b)
Definition: transaction.h:371