Raven Core  3.0.0
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 Raven Core 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 RAVEN_PRIMITIVES_TRANSACTION_H
8 #define RAVEN_PRIMITIVES_TRANSACTION_H
9 
10 #include <stdint.h>
11 #include "amount.h"
12 #include "script/script.h"
13 #include "serialize.h"
14 #include "uint256.h"
15 
16 static const int SERIALIZE_TRANSACTION_NO_WITNESS = 0x40000000;
17 
18 class CCoinsViewCache;
20 
22 class COutPoint
23 {
24 public:
26  uint32_t n;
27 
28  COutPoint(): n((uint32_t) -1) { }
29  COutPoint(const uint256& hashIn, uint32_t nIn): hash(hashIn), n(nIn) { }
30 
32 
33  template <typename Stream, typename Operation>
34  inline void SerializationOp(Stream& s, Operation ser_action) {
35  READWRITE(hash);
36  READWRITE(n);
37  }
38 
39  void SetNull() { hash.SetNull(); n = (uint32_t) -1; }
40  bool IsNull() const { return (hash.IsNull() && n == (uint32_t) -1); }
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  std::string ToSerializedString() const;
61 };
62 
67 class CTxIn
68 {
69 public:
72  uint32_t nSequence;
74 
75  /* Setting nSequence to this value for every input in a transaction
76  * disables nLockTime. */
77  static const uint32_t SEQUENCE_FINAL = 0xffffffff;
78 
79  /* Below flags apply in the context of BIP 68*/
80  /* If this flag set, CTxIn::nSequence is NOT interpreted as a
81  * relative lock-time. */
82  static const uint32_t SEQUENCE_LOCKTIME_DISABLE_FLAG = (1 << 31);
83 
84  /* If CTxIn::nSequence encodes a relative lock-time and this flag
85  * is set, the relative lock-time has units of 512 seconds,
86  * otherwise it specifies blocks with a granularity of 1. */
87  static const uint32_t SEQUENCE_LOCKTIME_TYPE_FLAG = (1 << 22);
88 
89  /* If CTxIn::nSequence encodes a relative lock-time, this mask is
90  * applied to extract that lock-time from the sequence field. */
91  static const uint32_t SEQUENCE_LOCKTIME_MASK = 0x0000ffff;
92 
93  /* In order to use the same number of bits to encode roughly the
94  * same wall-clock duration, and because blocks are naturally
95  * limited to occur every 600s on average, the minimum granularity
96  * for time-based relative lock-time is fixed at 512 seconds.
97  * Converting from CTxIn::nSequence to seconds is performed by
98  * multiplying by 512 = 2^9, or equivalently shifting up by
99  * 9 bits. */
100  static const int SEQUENCE_LOCKTIME_GRANULARITY = 9;
101 
103  {
104  nSequence = SEQUENCE_FINAL;
105  }
106 
107  explicit CTxIn(COutPoint prevoutIn, CScript scriptSigIn=CScript(), uint32_t nSequenceIn=SEQUENCE_FINAL);
108  CTxIn(uint256 hashPrevTx, uint32_t nOut, CScript scriptSigIn=CScript(), uint32_t nSequenceIn=SEQUENCE_FINAL);
109 
111 
112  template <typename Stream, typename Operation>
113  inline void SerializationOp(Stream& s, Operation ser_action) {
114  READWRITE(prevout);
115  READWRITE(scriptSig);
116  READWRITE(nSequence);
117  }
118 
119  friend bool operator==(const CTxIn& a, const CTxIn& b)
120  {
121  return (a.prevout == b.prevout &&
122  a.scriptSig == b.scriptSig &&
123  a.nSequence == b.nSequence);
124  }
125 
126  friend bool operator!=(const CTxIn& a, const CTxIn& b)
127  {
128  return !(a == b);
129  }
130 
131  std::string ToString() const;
132 };
133 
137 class CTxOut
138 {
139 public:
142 
144  {
145  SetNull();
146  }
147 
148  CTxOut(const CAmount& nValueIn, CScript scriptPubKeyIn);
149 
151 
152  template <typename Stream, typename Operation>
153  inline void SerializationOp(Stream& s, Operation ser_action) {
154  READWRITE(nValue);
155  READWRITE(scriptPubKey);
156  }
157 
158  void SetNull()
159  {
160  nValue = -1;
161  scriptPubKey.clear();
162  }
163 
164  bool IsNull() const
165  {
166  return (nValue == -1);
167  }
168 
169  friend bool operator==(const CTxOut& a, const CTxOut& b)
170  {
171  return (a.nValue == b.nValue &&
172  a.scriptPubKey == b.scriptPubKey);
173  }
174 
175  friend bool operator!=(const CTxOut& a, const CTxOut& b)
176  {
177  return !(a == b);
178  }
179 
180  std::string ToString() const;
181 };
182 
183 struct CMutableTransaction;
184 
202 template<typename Stream, typename TxType>
203 inline void UnserializeTransaction(TxType& tx, Stream& s) {
204  const bool fAllowWitness = !(s.GetVersion() & SERIALIZE_TRANSACTION_NO_WITNESS);
205 
206  s >> tx.nVersion;
207  unsigned char flags = 0;
208  tx.vin.clear();
209  tx.vout.clear();
210  /* Try to read the vin. In case the dummy is there, this will be read as an empty vector. */
211  s >> tx.vin;
212  if (tx.vin.size() == 0 && fAllowWitness) {
213  /* We read a dummy or an empty vin. */
214  s >> flags;
215  if (flags != 0) {
216  s >> tx.vin;
217  s >> tx.vout;
218  }
219  } else {
220  /* We read a non-empty vin. Assume a normal vout follows. */
221  s >> tx.vout;
222  }
223  if ((flags & 1) && fAllowWitness) {
224  /* The witness flag is present, and we support witnesses. */
225  flags ^= 1;
226  for (size_t i = 0; i < tx.vin.size(); i++) {
227  s >> tx.vin[i].scriptWitness.stack;
228  }
229  }
230  if (flags) {
231  /* Unknown flag in the serialization */
232  throw std::ios_base::failure("Unknown transaction optional data");
233  }
234  s >> tx.nLockTime;
235 }
236 
237 template<typename Stream, typename TxType>
238 inline void SerializeTransaction(const TxType& tx, Stream& s) {
239  const bool fAllowWitness = !(s.GetVersion() & SERIALIZE_TRANSACTION_NO_WITNESS);
240 
241  s << tx.nVersion;
242  unsigned char flags = 0;
243  // Consistency check
244  if (fAllowWitness) {
245  /* Check whether witnesses need to be serialized. */
246  if (tx.HasWitness()) {
247  flags |= 1;
248  }
249  }
250  if (flags) {
251  /* Use extended format in case witnesses are to be serialized. */
252  std::vector<CTxIn> vinDummy;
253  s << vinDummy;
254  s << flags;
255  }
256  s << tx.vin;
257  s << tx.vout;
258  if (flags & 1) {
259  for (size_t i = 0; i < tx.vin.size(); i++) {
260  s << tx.vin[i].scriptWitness.stack;
261  }
262  }
263  s << tx.nLockTime;
264 }
265 
266 
271 {
272 public:
273  // Default transaction version.
274  static const int32_t CURRENT_VERSION=2;
275 
276  // Changing the default transaction version requires a two step process: first
277  // adapting relay policy by bumping MAX_STANDARD_VERSION, and then later date
278  // bumping the default CURRENT_VERSION at which point both CURRENT_VERSION and
279  // MAX_STANDARD_VERSION will be equal.
280  static const int32_t MAX_STANDARD_VERSION=2;
281 
282  // The local variables are made const to prevent unintended modification
283  // without updating the cached hash value. However, CTransaction is not
284  // actually immutable; deserialization and assignment are implemented,
285  // and bypass the constness. This is safe, as they update the entire
286  // structure, including the hash.
287  const std::vector<CTxIn> vin;
288  const std::vector<CTxOut> vout;
289  const int32_t nVersion;
290  const uint32_t nLockTime;
291 
292 private:
294  const uint256 hash;
295 
296  uint256 ComputeHash() const;
297 
298 public:
300  CTransaction();
301 
305 
306  template <typename Stream>
307  inline void Serialize(Stream& s) const {
308  SerializeTransaction(*this, s);
309  }
310 
313  template <typename Stream>
315 
316  bool IsNull() const {
317  return vin.empty() && vout.empty();
318  }
319 
320  const uint256& GetHash() const {
321  return hash;
322  }
323 
324  // Compute a hash that includes both transaction and witness data
325  uint256 GetWitnessHash() const;
326 
327  // Return sum of txouts.
328  CAmount GetValueOut() const;
329  // GetValueIn() is a method on CCoinsViewCache, because
330  // inputs must be known to compute value in.
331 
333  bool IsNewAsset() const;
334  bool VerifyNewAsset(std::string& strError) const;
335  bool IsNewUniqueAsset() const;
336  bool VerifyNewUniqueAsset(std::string& strError) const;
337  bool IsReissueAsset() const;
338  bool VerifyReissueAsset(std::string& strError) const;
339  bool IsNewMsgChannelAsset() const;
340  bool VerifyNewMsgChannelAsset(std::string& strError) const;
341  bool IsNewQualifierAsset() const;
342  bool VerifyNewQualfierAsset(std::string &strError) const;
343  bool IsNewRestrictedAsset() const;
344  bool VerifyNewRestrictedAsset(std::string& strError) const;
345 
346  bool CheckAddingTagBurnFee(const int& count) const;
347 
348  bool GetVerifierStringFromTx(CNullAssetTxVerifierString& verifier, std::string& strError) const;
349  bool GetVerifierStringFromTx(CNullAssetTxVerifierString& verifier, std::string& strError, bool& fNotFound) const;
350 
358  unsigned int GetTotalSize() const;
359 
360  bool IsCoinBase() const
361  {
362  return (vin.size() == 1 && vin[0].prevout.IsNull());
363  }
364 
365  friend bool operator==(const CTransaction& a, const CTransaction& b)
366  {
367  return a.hash == b.hash;
368  }
369 
370  friend bool operator!=(const CTransaction& a, const CTransaction& b)
371  {
372  return a.hash != b.hash;
373  }
374 
375  std::string ToString() const;
376 
377  bool HasWitness() const
378  {
379  for (size_t i = 0; i < vin.size(); i++) {
380  if (!vin[i].scriptWitness.IsNull()) {
381  return true;
382  }
383  }
384  return false;
385  }
386 };
387 
390 {
391  std::vector<CTxIn> vin;
392  std::vector<CTxOut> vout;
393  int32_t nVersion;
394  uint32_t nLockTime;
395 
398 
399  template <typename Stream>
400  inline void Serialize(Stream& s) const {
401  SerializeTransaction(*this, s);
402  }
403 
404 
405  template <typename Stream>
406  inline void Unserialize(Stream& s) {
407  UnserializeTransaction(*this, s);
408  }
409 
410  template <typename Stream>
412  Unserialize(s);
413  }
414 
418  uint256 GetHash() const;
419 
420  friend bool operator==(const CMutableTransaction& a, const CMutableTransaction& b)
421  {
422  return a.GetHash() == b.GetHash();
423  }
424 
425  bool HasWitness() const
426  {
427  for (size_t i = 0; i < vin.size(); i++) {
428  if (!vin[i].scriptWitness.IsNull()) {
429  return true;
430  }
431  }
432  return false;
433  }
434 };
435 
436 typedef std::shared_ptr<const CTransaction> CTransactionRef;
437 static inline CTransactionRef MakeTransactionRef() { return std::make_shared<const CTransaction>(); }
438 template <typename Tx> static inline CTransactionRef MakeTransactionRef(Tx&& txIn) { return std::make_shared<const CTransaction>(std::forward<Tx>(txIn)); }
439 
440 #endif // RAVEN_PRIMITIVES_TRANSACTION_H
CAmount nValue
Definition: transaction.h:140
void SetNull()
Definition: transaction.h:158
void SetNull()
Definition: uint256.h:41
friend bool operator!=(const COutPoint &a, const COutPoint &b)
Definition: transaction.h:53
CScript scriptPubKey
Definition: transaction.h:141
#define READWRITE(obj)
Definition: serialize.h:163
int flags
Definition: raven-tx.cpp:500
ADD_SERIALIZE_METHODS
Definition: transaction.h:31
COutPoint(const uint256 &hashIn, uint32_t nIn)
Definition: transaction.h:29
std::vector< CTxIn > vin
Definition: transaction.h:391
CScriptWitness scriptWitness
Definition: transaction.h:73
std::string ToSerializedString() const
Definition: transaction.cpp:19
constexpr deserialize_type deserialize
Definition: serialize.h:41
ADD_SERIALIZE_METHODS
Definition: transaction.h:110
friend bool operator==(const CTxOut &a, const CTxOut &b)
Definition: transaction.h:169
void SerializationOp(Stream &s, Operation ser_action)
Definition: transaction.h:153
std::string ToString() const
Definition: transaction.cpp:14
friend bool operator!=(const CTxOut &a, const CTxOut &b)
Definition: transaction.h:175
void UnserializeTransaction(TxType &tx, Stream &s)
Basic transaction serialization format:
Definition: transaction.h:203
const uint256 hash
Memory only.
Definition: transaction.h:294
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:436
bool IsNull() const
Definition: uint256.h:33
Dummy data type to identify deserializing constructors.
Definition: serialize.h:40
bool IsCoinBase() const
Definition: transaction.h:360
const std::vector< CTxIn > vin
Definition: transaction.h:287
friend bool operator==(const COutPoint &a, const COutPoint &b)
Definition: transaction.h:48
void SerializeTransaction(const TxType &tx, Stream &s)
Definition: transaction.h:238
int Compare(const base_blob &other) const
Definition: uint256.h:46
bool IsNull() const
Definition: transaction.h:164
int64_t CAmount
Amount in corbies (Can be negative)
Definition: amount.h:13
An input of a transaction.
Definition: transaction.h:67
const uint256 & GetHash() const
Definition: transaction.h:320
bool IsNull() const
Definition: transaction.h:316
void Unserialize(Stream &s)
Definition: transaction.h:406
void SerializationOp(Stream &s, Operation ser_action)
Definition: transaction.h:34
uint32_t n
Definition: transaction.h:26
const std::vector< CTxOut > vout
Definition: transaction.h:288
An output of a transaction.
Definition: transaction.h:137
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:22
std::vector< CTxOut > vout
Definition: transaction.h:392
bool HasWitness() const
Definition: transaction.h:425
CTransaction(deserialize_type, Stream &s)
This deserializing constructor is provided instead of an Unserialize method.
Definition: transaction.h:314
friend bool operator==(const CMutableTransaction &a, const CMutableTransaction &b)
Definition: transaction.h:420
void SetNull()
Definition: transaction.h:39
void SerializationOp(Stream &s, Operation ser_action)
Definition: transaction.h:113
CScript scriptSig
Definition: transaction.h:71
256-bit opaque blob.
Definition: uint256.h:123
CMutableTransaction(deserialize_type, Stream &s)
Definition: transaction.h:411
const int32_t nVersion
Definition: transaction.h:289
bool HasWitness() const
Definition: transaction.h:377
uint256 GetHash() const
Compute the hash of this CMutableTransaction.
Definition: transaction.cpp:69
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:396
uint32_t nSequence
Definition: transaction.h:72
void Serialize(Stream &s) const
Definition: transaction.h:307
void Unserialize(Stream &s, char &a)
Definition: serialize.h:194
void Serialize(Stream &s) const
Definition: transaction.h:400
A mutable version of CTransaction.
Definition: transaction.h:389
ADD_SERIALIZE_METHODS
Definition: transaction.h:150
bool IsNull() const
Definition: transaction.h:40
friend bool operator<(const COutPoint &a, const COutPoint &b)
Definition: transaction.h:42
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:270
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:208
friend bool operator==(const CTxIn &a, const CTxIn &b)
Definition: transaction.h:119
COutPoint prevout
Definition: transaction.h:70
friend bool operator!=(const CTxIn &a, const CTxIn &b)
Definition: transaction.h:126
void clear()
Definition: script.h:689
const uint32_t nLockTime
Definition: transaction.h:290
friend bool operator==(const CTransaction &a, const CTransaction &b)
Definition: transaction.h:365
uint256 hash
Definition: transaction.h:25
friend bool operator!=(const CTransaction &a, const CTransaction &b)
Definition: transaction.h:370