1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 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
10 #include "script/script.h"
11 #include "serialize.h"
14 /** An outpoint - a combination of a transaction hash and an index n into its vout */
21 COutPoint() { SetNull(); }
22 COutPoint(uint256 hashIn
, uint32_t nIn
) { hash
= hashIn
; n
= nIn
; }
24 ADD_SERIALIZE_METHODS
;
26 template <typename Stream
, typename Operation
>
27 inline void SerializationOp(Stream
& s
, Operation ser_action
, int nType
, int nVersion
) {
32 void SetNull() { hash
.SetNull(); n
= (uint32_t) -1; }
33 bool IsNull() const { return (hash
.IsNull() && n
== (uint32_t) -1); }
35 friend bool operator<(const COutPoint
& a
, const COutPoint
& b
)
37 return (a
.hash
< b
.hash
|| (a
.hash
== b
.hash
&& a
.n
< b
.n
));
40 friend bool operator==(const COutPoint
& a
, const COutPoint
& b
)
42 return (a
.hash
== b
.hash
&& a
.n
== b
.n
);
45 friend bool operator!=(const COutPoint
& a
, const COutPoint
& b
)
50 std::string
ToString() const;
53 /** An input of a transaction. It contains the location of the previous
54 * transaction's output that it claims and a signature that matches the
55 * output's public key.
66 nSequence
= std::numeric_limits
<unsigned int>::max();
69 explicit CTxIn(COutPoint prevoutIn
, CScript scriptSigIn
=CScript(), uint32_t nSequenceIn
=std::numeric_limits
<unsigned int>::max());
70 CTxIn(uint256 hashPrevTx
, uint32_t nOut
, CScript scriptSigIn
=CScript(), uint32_t nSequenceIn
=std::numeric_limits
<uint32_t>::max());
72 ADD_SERIALIZE_METHODS
;
74 template <typename Stream
, typename Operation
>
75 inline void SerializationOp(Stream
& s
, Operation ser_action
, int nType
, int nVersion
) {
83 return (nSequence
== std::numeric_limits
<uint32_t>::max());
86 friend bool operator==(const CTxIn
& a
, const CTxIn
& b
)
88 return (a
.prevout
== b
.prevout
&&
89 a
.scriptSig
== b
.scriptSig
&&
90 a
.nSequence
== b
.nSequence
);
93 friend bool operator!=(const CTxIn
& a
, const CTxIn
& b
)
98 std::string
ToString() const;
101 /** An output of a transaction. It contains the public key that the next input
102 * must be able to sign with to claim it.
108 CScript scriptPubKey
;
115 CTxOut(const CAmount
& nValueIn
, CScript scriptPubKeyIn
);
117 ADD_SERIALIZE_METHODS
;
119 template <typename Stream
, typename Operation
>
120 inline void SerializationOp(Stream
& s
, Operation ser_action
, int nType
, int nVersion
) {
122 READWRITE(scriptPubKey
);
128 scriptPubKey
.clear();
133 return (nValue
== -1);
136 uint256
GetHash() const;
138 CAmount
GetDustThreshold(const CFeeRate
&minRelayTxFee
) const
140 // "Dust" is defined in terms of CTransaction::minRelayTxFee,
141 // which has units satoshis-per-kilobyte.
142 // If you'd pay more than 1/3 in fees
143 // to spend something, then we consider it dust.
144 // A typical spendable txout is 34 bytes big, and will
145 // need a CTxIn of at least 148 bytes to spend:
146 // so dust is a spendable txout less than 546 satoshis
147 // with default minRelayTxFee.
148 if (scriptPubKey
.IsUnspendable())
151 size_t nSize
= GetSerializeSize(SER_DISK
,0)+148u;
152 return 3*minRelayTxFee
.GetFee(nSize
);
155 bool IsDust(const CFeeRate
&minRelayTxFee
) const
157 return (nValue
< GetDustThreshold(minRelayTxFee
));
160 friend bool operator==(const CTxOut
& a
, const CTxOut
& b
)
162 return (a
.nValue
== b
.nValue
&&
163 a
.scriptPubKey
== b
.scriptPubKey
);
166 friend bool operator!=(const CTxOut
& a
, const CTxOut
& b
)
171 std::string
ToString() const;
174 struct CMutableTransaction
;
176 /** The basic transaction that is broadcasted on the network and contained in
177 * blocks. A transaction can contain multiple inputs and outputs.
184 void UpdateHash() const;
187 static const int32_t CURRENT_VERSION
=1;
189 // The local variables are made const to prevent unintended modification
190 // without updating the cached hash value. However, CTransaction is not
191 // actually immutable; deserialization and assignment are implemented,
192 // and bypass the constness. This is safe, as they update the entire
193 // structure, including the hash.
194 const int32_t nVersion
;
195 const std::vector
<CTxIn
> vin
;
196 const std::vector
<CTxOut
> vout
;
197 const uint32_t nLockTime
;
199 /** Construct a CTransaction that qualifies as IsNull() */
202 /** Convert a CMutableTransaction into a CTransaction. */
203 CTransaction(const CMutableTransaction
&tx
);
205 CTransaction
& operator=(const CTransaction
& tx
);
207 ADD_SERIALIZE_METHODS
;
209 template <typename Stream
, typename Operation
>
210 inline void SerializationOp(Stream
& s
, Operation ser_action
, int nType
, int nVersion
) {
211 READWRITE(*const_cast<int32_t*>(&this->nVersion
));
212 nVersion
= this->nVersion
;
213 READWRITE(*const_cast<std::vector
<CTxIn
>*>(&vin
));
214 READWRITE(*const_cast<std::vector
<CTxOut
>*>(&vout
));
215 READWRITE(*const_cast<uint32_t*>(&nLockTime
));
216 if (ser_action
.ForRead())
220 bool IsNull() const {
221 return vin
.empty() && vout
.empty();
224 const uint256
& GetHash() const {
228 // Return sum of txouts.
229 CAmount
GetValueOut() const;
230 // GetValueIn() is a method on CCoinsViewCache, because
231 // inputs must be known to compute value in.
233 // Compute priority, given priority of inputs and (optionally) tx size
234 double ComputePriority(double dPriorityInputs
, unsigned int nTxSize
=0) const;
236 // Compute modified tx size for priority calculation (optionally given tx size)
237 unsigned int CalculateModifiedSize(unsigned int nTxSize
=0) const;
239 bool IsCoinBase() const
241 return (vin
.size() == 1 && vin
[0].prevout
.IsNull());
244 friend bool operator==(const CTransaction
& a
, const CTransaction
& b
)
246 return a
.hash
== b
.hash
;
249 friend bool operator!=(const CTransaction
& a
, const CTransaction
& b
)
251 return a
.hash
!= b
.hash
;
254 std::string
ToString() const;
257 /** A mutable version of CTransaction. */
258 struct CMutableTransaction
261 std::vector
<CTxIn
> vin
;
262 std::vector
<CTxOut
> vout
;
265 CMutableTransaction();
266 CMutableTransaction(const CTransaction
& tx
);
268 ADD_SERIALIZE_METHODS
;
270 template <typename Stream
, typename Operation
>
271 inline void SerializationOp(Stream
& s
, Operation ser_action
, int nType
, int nVersion
) {
272 READWRITE(this->nVersion
);
273 nVersion
= this->nVersion
;
276 READWRITE(nLockTime
);
279 /** Compute the hash of this CMutableTransaction. This is computed on the
280 * fly, as opposed to GetHash() in CTransaction, which uses a cached result.
282 uint256
GetHash() const;
285 #endif // BITCOIN_PRIMITIVES_TRANSACTION_H