BSHA3  0.17.99
P2P Blockchain, based on Bitcoin
transaction.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2018 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 <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 = (1U << 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  {
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) {
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);
150  }
151 
152  void SetNull()
153  {
154  nValue = -1;
156  }
157 
158  bool IsNull() const
159  {
160  return (nValue == -1);
161  }
162 
163  friend bool operator==(const CTxOut& a, const CTxOut& b)
164  {
165  return (a.nValue == b.nValue &&
166  a.scriptPubKey == b.scriptPubKey);
167  }
168 
169  friend bool operator!=(const CTxOut& a, const CTxOut& b)
170  {
171  return !(a == b);
172  }
173 
174  std::string ToString() const;
175 };
176 
177 struct CMutableTransaction;
178 
196 template<typename Stream, typename TxType>
197 inline void UnserializeTransaction(TxType& tx, Stream& s) {
198  const bool fAllowWitness = !(s.GetVersion() & SERIALIZE_TRANSACTION_NO_WITNESS);
199 
200  s >> tx.nVersion;
201  unsigned char flags = 0;
202  tx.vin.clear();
203  tx.vout.clear();
204  /* Try to read the vin. In case the dummy is there, this will be read as an empty vector. */
205  s >> tx.vin;
206  if (tx.vin.size() == 0 && fAllowWitness) {
207  /* We read a dummy or an empty vin. */
208  s >> flags;
209  if (flags != 0) {
210  s >> tx.vin;
211  s >> tx.vout;
212  }
213  } else {
214  /* We read a non-empty vin. Assume a normal vout follows. */
215  s >> tx.vout;
216  }
217  if ((flags & 1) && fAllowWitness) {
218  /* The witness flag is present, and we support witnesses. */
219  flags ^= 1;
220  for (size_t i = 0; i < tx.vin.size(); i++) {
221  s >> tx.vin[i].scriptWitness.stack;
222  }
223  }
224  if (flags) {
225  /* Unknown flag in the serialization */
226  throw std::ios_base::failure("Unknown transaction optional data");
227  }
228  s >> tx.nLockTime;
229 }
230 
231 template<typename Stream, typename TxType>
232 inline void SerializeTransaction(const TxType& tx, Stream& s) {
233  const bool fAllowWitness = !(s.GetVersion() & SERIALIZE_TRANSACTION_NO_WITNESS);
234 
235  s << tx.nVersion;
236  unsigned char flags = 0;
237  // Consistency check
238  if (fAllowWitness) {
239  /* Check whether witnesses need to be serialized. */
240  if (tx.HasWitness()) {
241  flags |= 1;
242  }
243  }
244  if (flags) {
245  /* Use extended format in case witnesses are to be serialized. */
246  std::vector<CTxIn> vinDummy;
247  s << vinDummy;
248  s << flags;
249  }
250  s << tx.vin;
251  s << tx.vout;
252  if (flags & 1) {
253  for (size_t i = 0; i < tx.vin.size(); i++) {
254  s << tx.vin[i].scriptWitness.stack;
255  }
256  }
257  s << tx.nLockTime;
258 }
259 
260 
265 {
266 public:
267  // Default transaction version.
268  static const int32_t CURRENT_VERSION=2;
269 
270  // Changing the default transaction version requires a two step process: first
271  // adapting relay policy by bumping MAX_STANDARD_VERSION, and then later date
272  // bumping the default CURRENT_VERSION at which point both CURRENT_VERSION and
273  // MAX_STANDARD_VERSION will be equal.
274  static const int32_t MAX_STANDARD_VERSION=2;
275 
276  // The local variables are made const to prevent unintended modification
277  // without updating the cached hash value. However, CTransaction is not
278  // actually immutable; deserialization and assignment are implemented,
279  // and bypass the constness. This is safe, as they update the entire
280  // structure, including the hash.
281  const std::vector<CTxIn> vin;
282  const std::vector<CTxOut> vout;
283  const int32_t nVersion;
284  const uint32_t nLockTime;
285 
286 private:
288  const uint256 hash;
290 
291  uint256 ComputeHash() const;
292  uint256 ComputeWitnessHash() const;
293 
294 public:
296  CTransaction();
297 
301 
302  template <typename Stream>
303  inline void Serialize(Stream& s) const {
304  SerializeTransaction(*this, s);
305  }
306 
309  template <typename Stream>
311 
312  bool IsNull() const {
313  return vin.empty() && vout.empty();
314  }
315 
316  const uint256& GetHash() const { return hash; }
317  const uint256& GetWitnessHash() const { return m_witness_hash; };
318 
319  // Return sum of txouts.
320  CAmount GetValueOut() const;
321  // GetValueIn() is a method on CCoinsViewCache, because
322  // inputs must be known to compute value in.
323 
329  unsigned int GetTotalSize() const;
330 
331  bool IsCoinBase() const
332  {
333  return (vin.size() == 1 && vin[0].prevout.IsNull());
334  }
335 
336  friend bool operator==(const CTransaction& a, const CTransaction& b)
337  {
338  return a.hash == b.hash;
339  }
340 
341  friend bool operator!=(const CTransaction& a, const CTransaction& b)
342  {
343  return a.hash != b.hash;
344  }
345 
346  std::string ToString() const;
347 
348  bool HasWitness() const
349  {
350  for (size_t i = 0; i < vin.size(); i++) {
351  if (!vin[i].scriptWitness.IsNull()) {
352  return true;
353  }
354  }
355  return false;
356  }
357 };
358 
361 {
362  std::vector<CTxIn> vin;
363  std::vector<CTxOut> vout;
364  int32_t nVersion;
365  uint32_t nLockTime;
366 
368  explicit CMutableTransaction(const CTransaction& tx);
369 
370  template <typename Stream>
371  inline void Serialize(Stream& s) const {
372  SerializeTransaction(*this, s);
373  }
374 
375 
376  template <typename Stream>
377  inline void Unserialize(Stream& s) {
378  UnserializeTransaction(*this, s);
379  }
380 
381  template <typename Stream>
383  Unserialize(s);
384  }
385 
389  uint256 GetHash() const;
390 
391  bool HasWitness() const
392  {
393  for (size_t i = 0; i < vin.size(); i++) {
394  if (!vin[i].scriptWitness.IsNull()) {
395  return true;
396  }
397  }
398  return false;
399  }
400 };
401 
402 typedef std::shared_ptr<const CTransaction> CTransactionRef;
403 static inline CTransactionRef MakeTransactionRef() { return std::make_shared<const CTransaction>(); }
404 template <typename Tx> static inline CTransactionRef MakeTransactionRef(Tx&& txIn) { return std::make_shared<const CTransaction>(std::forward<Tx>(txIn)); }
405 
406 #endif // BITCOIN_PRIMITIVES_TRANSACTION_H
CAmount nValue
Definition: transaction.h:134
void SetNull()
Definition: transaction.h:152
static const int32_t MAX_STANDARD_VERSION
Definition: transaction.h:274
void SetNull()
Definition: uint256.h:40
friend bool operator!=(const COutPoint &a, const COutPoint &b)
Definition: transaction.h:49
CScript scriptPubKey
Definition: transaction.h:135
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:362
CScriptWitness scriptWitness
Only serialized through CTransaction.
Definition: transaction.h:67
static const uint32_t SEQUENCE_FINAL
Definition: transaction.h:71
static const uint32_t SEQUENCE_LOCKTIME_DISABLE_FLAG
Definition: transaction.h:76
constexpr deserialize_type deserialize
Definition: serialize.h:41
ADD_SERIALIZE_METHODS
Definition: transaction.h:104
friend bool operator==(const CTxOut &a, const CTxOut &b)
Definition: transaction.h:163
void SerializationOp(Stream &s, Operation ser_action)
Definition: transaction.h:147
static const int SEQUENCE_LOCKTIME_GRANULARITY
Definition: transaction.h:94
std::string ToString() const
Definition: transaction.cpp:31
std::string ToString() const
Definition: transaction.cpp:12
std::string ToString() const
Definition: transaction.cpp:52
friend bool operator!=(const CTxOut &a, const CTxOut &b)
Definition: transaction.h:169
bool IsNull() const
Definition: script.h:571
void UnserializeTransaction(TxType &tx, Stream &s)
Basic transaction serialization format:
Definition: transaction.h:197
const uint256 hash
Memory only.
Definition: transaction.h:288
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:402
bool IsNull() const
Definition: uint256.h:32
Dummy data type to identify deserializing constructors.
Definition: serialize.h:40
bool IsCoinBase() const
Definition: transaction.h:331
const std::vector< CTxIn > vin
Definition: transaction.h:281
friend bool operator==(const COutPoint &a, const COutPoint &b)
Definition: transaction.h:44
CAmount GetValueOut() const
Definition: transaction.cpp:83
void SerializeTransaction(const TxType &tx, Stream &s)
Definition: transaction.h:232
int Compare(const base_blob &other) const
Definition: uint256.h:45
bool IsNull() const
Definition: transaction.h:158
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
An input of a transaction.
Definition: transaction.h:61
const uint256 & GetWitnessHash() const
Definition: transaction.h:317
const uint256 & GetHash() const
Definition: transaction.h:316
bool IsNull() const
Definition: transaction.h:312
void Unserialize(Stream &s)
Definition: transaction.h:377
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:282
An output of a transaction.
Definition: transaction.h:131
static const uint32_t SEQUENCE_LOCKTIME_TYPE_FLAG
Definition: transaction.h:81
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:363
bool HasWitness() const
Definition: transaction.h:391
CTransaction(deserialize_type, Stream &s)
This deserializing constructor is provided instead of an Unserialize method.
Definition: transaction.h:310
const uint256 m_witness_hash
Definition: transaction.h:289
uint256 ComputeHash() const
Definition: transaction.cpp:65
uint256 ComputeWitnessHash() const
Definition: transaction.cpp:70
void SetNull()
Definition: transaction.h:35
void SerializationOp(Stream &s, Operation ser_action)
Definition: transaction.h:107
CScript scriptSig
Definition: transaction.h:65
256-bit opaque blob.
Definition: uint256.h:122
CMutableTransaction(deserialize_type, Stream &s)
Definition: transaction.h:382
const int32_t nVersion
Definition: transaction.h:283
bool HasWitness() const
Definition: transaction.h:348
uint256 GetHash() const
Compute the hash of this CMutableTransaction.
Definition: transaction.cpp:60
static const uint32_t SEQUENCE_LOCKTIME_MASK
Definition: transaction.h:85
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:384
CTxIn()
Definition: transaction.h:96
uint32_t nSequence
Definition: transaction.h:66
CTransaction()
Construct a CTransaction that qualifies as IsNull()
Definition: transaction.cpp:79
void Serialize(Stream &s) const
Definition: transaction.h:303
std::string ToString() const
Definition: transaction.cpp:99
void Serialize(Stream &s) const
Definition: transaction.h:371
A mutable version of CTransaction.
Definition: transaction.h:360
unsigned int GetTotalSize() const
Get the total transaction size in bytes, including witness data.
Definition: transaction.cpp:94
int flags
Definition: bsha3-tx.cpp:509
ADD_SERIALIZE_METHODS
Definition: transaction.h:144
bool IsNull() const
Definition: transaction.h:36
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:264
static const int32_t CURRENT_VERSION
Definition: transaction.h:268
friend bool operator==(const CTxIn &a, const CTxIn &b)
Definition: transaction.h:113
#define READWRITE(...)
Definition: serialize.h:173
COutPoint prevout
Definition: transaction.h:64
friend bool operator!=(const CTxIn &a, const CTxIn &b)
Definition: transaction.h:120
void clear()
Definition: script.h:554
const uint32_t nLockTime
Definition: transaction.h:284
friend bool operator==(const CTransaction &a, const CTransaction &b)
Definition: transaction.h:336
uint256 hash
Definition: transaction.h:21
friend bool operator!=(const CTransaction &a, const CTransaction &b)
Definition: transaction.h:341