1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 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.
6 #ifndef BITCOIN_PRIMITIVES_TRANSACTION_H
7 #define BITCOIN_PRIMITIVES_TRANSACTION_H
11 #include "script/script.h"
12 #include "serialize.h"
15 static const int SERIALIZE_TRANSACTION_NO_WITNESS
= 0x40000000;
17 /** An outpoint - a combination of a transaction hash and an index n into its vout */
24 COutPoint(): n((uint32_t) -1) { }
25 COutPoint(const uint256
& hashIn
, uint32_t nIn
): hash(hashIn
), n(nIn
) { }
27 ADD_SERIALIZE_METHODS
;
29 template <typename Stream
, typename Operation
>
30 inline void SerializationOp(Stream
& s
, Operation ser_action
) {
35 void SetNull() { hash
.SetNull(); n
= (uint32_t) -1; }
36 bool IsNull() const { return (hash
.IsNull() && n
== (uint32_t) -1); }
38 friend bool operator<(const COutPoint
& a
, const COutPoint
& b
)
40 int cmp
= a
.hash
.Compare(b
.hash
);
41 return cmp
< 0 || (cmp
== 0 && a
.n
< b
.n
);
44 friend bool operator==(const COutPoint
& a
, const COutPoint
& b
)
46 return (a
.hash
== b
.hash
&& a
.n
== b
.n
);
49 friend bool operator!=(const COutPoint
& a
, const COutPoint
& b
)
54 std::string
ToString() const;
57 /** An input of a transaction. It contains the location of the previous
58 * transaction's output that it claims and a signature that matches the
59 * output's public key.
67 CScriptWitness scriptWitness
; //! Only serialized through CTransaction
69 /* Setting nSequence to this value for every input in a transaction
70 * disables nLockTime. */
71 static const uint32_t SEQUENCE_FINAL
= 0xffffffff;
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);
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);
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;
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
94 static const int SEQUENCE_LOCKTIME_GRANULARITY
= 9;
98 nSequence
= SEQUENCE_FINAL
;
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
);
104 ADD_SERIALIZE_METHODS
;
106 template <typename Stream
, typename Operation
>
107 inline void SerializationOp(Stream
& s
, Operation ser_action
) {
109 READWRITE(scriptSig
);
110 READWRITE(nSequence
);
113 friend bool operator==(const CTxIn
& a
, const CTxIn
& b
)
115 return (a
.prevout
== b
.prevout
&&
116 a
.scriptSig
== b
.scriptSig
&&
117 a
.nSequence
== b
.nSequence
);
120 friend bool operator!=(const CTxIn
& a
, const CTxIn
& b
)
125 std::string
ToString() const;
128 /** An output of a transaction. It contains the public key that the next input
129 * must be able to sign with to claim it.
135 CScript scriptPubKey
;
142 CTxOut(const CAmount
& nValueIn
, CScript scriptPubKeyIn
);
144 ADD_SERIALIZE_METHODS
;
146 template <typename Stream
, typename Operation
>
147 inline void SerializationOp(Stream
& s
, Operation ser_action
) {
149 READWRITE(scriptPubKey
);
155 scriptPubKey
.clear();
160 return (nValue
== -1);
163 friend bool operator==(const CTxOut
& a
, const CTxOut
& b
)
165 return (a
.nValue
== b
.nValue
&&
166 a
.scriptPubKey
== b
.scriptPubKey
);
169 friend bool operator!=(const CTxOut
& a
, const CTxOut
& b
)
174 std::string
ToString() const;
177 struct CMutableTransaction
;
180 * Basic transaction serialization format:
182 * - std::vector<CTxIn> vin
183 * - std::vector<CTxOut> vout
184 * - uint32_t nLockTime
186 * Extended transaction serialization format:
188 * - unsigned char dummy = 0x00
189 * - unsigned char flags (!= 0)
190 * - std::vector<CTxIn> vin
191 * - std::vector<CTxOut> vout
194 * - uint32_t nLockTime
196 template<typename Stream
, typename TxType
>
197 inline void UnserializeTransaction(TxType
& tx
, Stream
& s
) {
198 const bool fAllowWitness
= !(s
.GetVersion() & SERIALIZE_TRANSACTION_NO_WITNESS
);
201 unsigned char flags
= 0;
204 /* Try to read the vin. In case the dummy is there, this will be read as an empty vector. */
206 if (tx
.vin
.size() == 0 && fAllowWitness
) {
207 /* We read a dummy or an empty vin. */
214 /* We read a non-empty vin. Assume a normal vout follows. */
217 if ((flags
& 1) && fAllowWitness
) {
218 /* The witness flag is present, and we support witnesses. */
220 for (size_t i
= 0; i
< tx
.vin
.size(); i
++) {
221 s
>> tx
.vin
[i
].scriptWitness
.stack
;
225 /* Unknown flag in the serialization */
226 throw std::ios_base::failure("Unknown transaction optional data");
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
);
236 unsigned char flags
= 0;
239 /* Check whether witnesses need to be serialized. */
240 if (tx
.HasWitness()) {
245 /* Use extended format in case witnesses are to be serialized. */
246 std::vector
<CTxIn
> vinDummy
;
253 for (size_t i
= 0; i
< tx
.vin
.size(); i
++) {
254 s
<< tx
.vin
[i
].scriptWitness
.stack
;
261 /** The basic transaction that is broadcasted on the network and contained in
262 * blocks. A transaction can contain multiple inputs and outputs.
267 // Default transaction version.
268 static const int32_t CURRENT_VERSION
=2;
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;
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 int32_t nVersion
;
282 const std::vector
<CTxIn
> vin
;
283 const std::vector
<CTxOut
> vout
;
284 const uint32_t nLockTime
;
290 uint256
ComputeHash() const;
293 /** Construct a CTransaction that qualifies as IsNull() */
296 /** Convert a CMutableTransaction into a CTransaction. */
297 CTransaction(const CMutableTransaction
&tx
);
298 CTransaction(CMutableTransaction
&&tx
);
300 template <typename Stream
>
301 inline void Serialize(Stream
& s
) const {
302 SerializeTransaction(*this, s
);
305 /** This deserializing constructor is provided instead of an Unserialize method.
306 * Unserialize is not possible, since it would require overwriting const fields. */
307 template <typename Stream
>
308 CTransaction(deserialize_type
, Stream
& s
) : CTransaction(CMutableTransaction(deserialize
, s
)) {}
310 bool IsNull() const {
311 return vin
.empty() && vout
.empty();
314 const uint256
& GetHash() const {
318 // Compute a hash that includes both transaction and witness data
319 uint256
GetWitnessHash() const;
321 // Return sum of txouts.
322 CAmount
GetValueOut() const;
323 // GetValueIn() is a method on CCoinsViewCache, because
324 // inputs must be known to compute value in.
327 * Get the total transaction size in bytes, including witness data.
328 * "Total Size" defined in BIP141 and BIP144.
329 * @return Total transaction size in bytes
331 unsigned int GetTotalSize() const;
333 bool IsCoinBase() const
335 return (vin
.size() == 1 && vin
[0].prevout
.IsNull());
338 friend bool operator==(const CTransaction
& a
, const CTransaction
& b
)
340 return a
.hash
== b
.hash
;
343 friend bool operator!=(const CTransaction
& a
, const CTransaction
& b
)
345 return a
.hash
!= b
.hash
;
348 std::string
ToString() const;
350 bool HasWitness() const
352 for (size_t i
= 0; i
< vin
.size(); i
++) {
353 if (!vin
[i
].scriptWitness
.IsNull()) {
361 /** A mutable version of CTransaction. */
362 struct CMutableTransaction
365 std::vector
<CTxIn
> vin
;
366 std::vector
<CTxOut
> vout
;
369 CMutableTransaction();
370 CMutableTransaction(const CTransaction
& tx
);
372 template <typename Stream
>
373 inline void Serialize(Stream
& s
) const {
374 SerializeTransaction(*this, s
);
378 template <typename Stream
>
379 inline void Unserialize(Stream
& s
) {
380 UnserializeTransaction(*this, s
);
383 template <typename Stream
>
384 CMutableTransaction(deserialize_type
, Stream
& s
) {
388 /** Compute the hash of this CMutableTransaction. This is computed on the
389 * fly, as opposed to GetHash() in CTransaction, which uses a cached result.
391 uint256
GetHash() const;
393 friend bool operator==(const CMutableTransaction
& a
, const CMutableTransaction
& b
)
395 return a
.GetHash() == b
.GetHash();
398 bool HasWitness() const
400 for (size_t i
= 0; i
< vin
.size(); i
++) {
401 if (!vin
[i
].scriptWitness
.IsNull()) {
409 typedef std::shared_ptr
<const CTransaction
> CTransactionRef
;
410 static inline CTransactionRef
MakeTransactionRef() { return std::make_shared
<const CTransaction
>(); }
411 template <typename Tx
> static inline CTransactionRef
MakeTransactionRef(Tx
&& txIn
) { return std::make_shared
<const CTransaction
>(std::forward
<Tx
>(txIn
)); }
413 #endif // BITCOIN_PRIMITIVES_TRANSACTION_H