Line data Source code
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.
5 :
6 : #ifndef BITCOIN_PRIMITIVES_TRANSACTION_H
7 : #define BITCOIN_PRIMITIVES_TRANSACTION_H
8 :
9 : #include "amount.h"
10 : #include "script/script.h"
11 : #include "serialize.h"
12 : #include "uint256.h"
13 :
14 : /** An outpoint - a combination of a transaction hash and an index n into its vout */
15 : class COutPoint
16 : {
17 : public:
18 : uint256 hash;
19 : uint32_t n;
20 :
21 1197650 : COutPoint() { SetNull(); }
22 49298 : COutPoint(uint256 hashIn, uint32_t nIn) { hash = hashIn; n = nIn; }
23 :
24 755139 : ADD_SERIALIZE_METHODS;
25 :
26 : template <typename Stream, typename Operation>
27 648899 : inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
28 755157 : READWRITE(hash);
29 755130 : READWRITE(n);
30 648843 : }
31 :
32 1202774 : void SetNull() { hash.SetNull(); n = (uint32_t) -1; }
33 203675 : bool IsNull() const { return (hash.IsNull() && n == (uint32_t) -1); }
34 :
35 610633 : friend bool operator<(const COutPoint& a, const COutPoint& b)
36 : {
37 1221266 : return (a.hash < b.hash || (a.hash == b.hash && a.n < b.n));
38 : }
39 :
40 3466 : friend bool operator==(const COutPoint& a, const COutPoint& b)
41 : {
42 6932 : return (a.hash == b.hash && a.n == b.n);
43 : }
44 :
45 : friend bool operator!=(const COutPoint& a, const COutPoint& b)
46 : {
47 : return !(a == b);
48 : }
49 :
50 : std::string ToString() const;
51 : };
52 :
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.
56 : */
57 4632640 : class CTxIn
58 : {
59 : public:
60 : COutPoint prevout;
61 : CScript scriptSig;
62 : uint32_t nSequence;
63 :
64 0 : CTxIn()
65 598004 : {
66 598004 : nSequence = std::numeric_limits<unsigned int>::max();
67 0 : }
68 :
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());
71 :
72 602802 : ADD_SERIALIZE_METHODS;
73 :
74 : template <typename Stream, typename Operation>
75 602802 : inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
76 602802 : READWRITE(prevout);
77 602802 : READWRITE(scriptSig);
78 602802 : READWRITE(nSequence);
79 602802 : }
80 :
81 0 : bool IsFinal() const
82 : {
83 0 : return (nSequence == std::numeric_limits<uint32_t>::max());
84 : }
85 :
86 : friend bool operator==(const CTxIn& a, const CTxIn& b)
87 : {
88 : return (a.prevout == b.prevout &&
89 : a.scriptSig == b.scriptSig &&
90 : a.nSequence == b.nSequence);
91 : }
92 :
93 : friend bool operator!=(const CTxIn& a, const CTxIn& b)
94 : {
95 : return !(a == b);
96 : }
97 :
98 : std::string ToString() const;
99 : };
100 :
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.
103 : */
104 6733373 : class CTxOut
105 : {
106 : public:
107 : CAmount nValue;
108 : CScript scriptPubKey;
109 :
110 0 : CTxOut()
111 1236715 : {
112 : SetNull();
113 0 : }
114 :
115 : CTxOut(const CAmount& nValueIn, CScript scriptPubKeyIn);
116 :
117 774876 : ADD_SERIALIZE_METHODS;
118 :
119 : template <typename Stream, typename Operation>
120 670600 : inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
121 774159 : READWRITE(nValue);
122 774158 : READWRITE(scriptPubKey);
123 670601 : }
124 :
125 : void SetNull()
126 : {
127 1245134 : nValue = -1;
128 1245134 : scriptPubKey.clear();
129 : }
130 :
131 0 : bool IsNull() const
132 : {
133 0 : return (nValue == -1);
134 : }
135 :
136 : uint256 GetHash() const;
137 :
138 720 : CAmount GetDustThreshold(const CFeeRate &minRelayTxFee) const
139 : {
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 1440 : if (scriptPubKey.IsUnspendable())
149 : return 0;
150 :
151 717 : size_t nSize = GetSerializeSize(SER_DISK,0)+148u;
152 717 : return 3*minRelayTxFee.GetFee(nSize);
153 : }
154 :
155 : bool IsDust(const CFeeRate &minRelayTxFee) const
156 : {
157 720 : return (nValue < GetDustThreshold(minRelayTxFee));
158 : }
159 :
160 : friend bool operator==(const CTxOut& a, const CTxOut& b)
161 : {
162 303074 : return (a.nValue == b.nValue &&
163 151537 : a.scriptPubKey == b.scriptPubKey);
164 : }
165 :
166 : friend bool operator!=(const CTxOut& a, const CTxOut& b)
167 : {
168 : return !(a == b);
169 : }
170 :
171 : std::string ToString() const;
172 : };
173 :
174 : struct CMutableTransaction;
175 :
176 : /** The basic transaction that is broadcasted on the network and contained in
177 : * blocks. A transaction can contain multiple inputs and outputs.
178 : */
179 813598 : class CTransaction
180 : {
181 : private:
182 : /** Memory only. */
183 : const uint256 hash;
184 : void UpdateHash() const;
185 :
186 : public:
187 : static const int32_t CURRENT_VERSION=1;
188 :
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;
198 :
199 : /** Construct a CTransaction that qualifies as IsNull() */
200 : CTransaction();
201 :
202 : /** Convert a CMutableTransaction into a CTransaction. */
203 : CTransaction(const CMutableTransaction &tx);
204 :
205 : CTransaction& operator=(const CTransaction& tx);
206 :
207 338793 : ADD_SERIALIZE_METHODS;
208 :
209 : template <typename Stream, typename Operation>
210 338793 : inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
211 338793 : READWRITE(*const_cast<int32_t*>(&this->nVersion));
212 338793 : nVersion = this->nVersion;
213 338793 : READWRITE(*const_cast<std::vector<CTxIn>*>(&vin));
214 338791 : READWRITE(*const_cast<std::vector<CTxOut>*>(&vout));
215 338791 : READWRITE(*const_cast<uint32_t*>(&nLockTime));
216 338791 : if (ser_action.ForRead())
217 35068 : UpdateHash();
218 338791 : }
219 :
220 : bool IsNull() const {
221 : return vin.empty() && vout.empty();
222 : }
223 :
224 : const uint256& GetHash() const {
225 117490 : return hash;
226 : }
227 :
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.
232 :
233 : // Compute priority, given priority of inputs and (optionally) tx size
234 : double ComputePriority(double dPriorityInputs, unsigned int nTxSize=0) const;
235 :
236 : // Compute modified tx size for priority calculation (optionally given tx size)
237 : unsigned int CalculateModifiedSize(unsigned int nTxSize=0) const;
238 :
239 199555 : bool IsCoinBase() const
240 : {
241 595505 : return (vin.size() == 1 && vin[0].prevout.IsNull());
242 : }
243 :
244 : friend bool operator==(const CTransaction& a, const CTransaction& b)
245 : {
246 5324 : return a.hash == b.hash;
247 : }
248 :
249 : friend bool operator!=(const CTransaction& a, const CTransaction& b)
250 : {
251 13 : return a.hash != b.hash;
252 : }
253 :
254 : std::string ToString() const;
255 : };
256 :
257 : /** A mutable version of CTransaction. */
258 130644 : struct CMutableTransaction
259 : {
260 : int32_t nVersion;
261 : std::vector<CTxIn> vin;
262 : std::vector<CTxOut> vout;
263 : uint32_t nLockTime;
264 :
265 : CMutableTransaction();
266 : CMutableTransaction(const CTransaction& tx);
267 :
268 69688 : ADD_SERIALIZE_METHODS;
269 :
270 : template <typename Stream, typename Operation>
271 69688 : inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
272 69688 : READWRITE(this->nVersion);
273 69687 : nVersion = this->nVersion;
274 69687 : READWRITE(vin);
275 69687 : READWRITE(vout);
276 69687 : READWRITE(nLockTime);
277 69687 : }
278 :
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.
281 : */
282 : uint256 GetHash() const;
283 : };
284 :
285 : #endif // BITCOIN_PRIMITIVES_TRANSACTION_H
|