]>
Commit | Line | Data |
---|---|---|
effc2770 EL |
1 | // Copyright (c) 2009-2010 Satoshi Nakamoto |
2 | // Copyright (c) 2009-2013 The Bitcoin developers | |
3 | // Distributed under the MIT/X11 software license, see the accompanying | |
4 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | |
51ed9ec9 | 5 | |
effc2770 EL |
6 | #ifndef BITCOIN_CORE_H |
7 | #define BITCOIN_CORE_H | |
8 | ||
05df3fc6 | 9 | #include "script.h" |
51ed9ec9 BD |
10 | #include "serialize.h" |
11 | #include "uint256.h" | |
12 | ||
13 | #include <stdint.h> | |
effc2770 | 14 | |
51ed9ec9 | 15 | #include <boost/foreach.hpp> |
effc2770 | 16 | |
788536f1 EL |
17 | class CTransaction; |
18 | ||
effc2770 EL |
19 | /** An outpoint - a combination of a transaction hash and an index n into its vout */ |
20 | class COutPoint | |
21 | { | |
22 | public: | |
23 | uint256 hash; | |
24 | unsigned int n; | |
25 | ||
26 | COutPoint() { SetNull(); } | |
27 | COutPoint(uint256 hashIn, unsigned int nIn) { hash = hashIn; n = nIn; } | |
28 | IMPLEMENT_SERIALIZE( READWRITE(FLATDATA(*this)); ) | |
29 | void SetNull() { hash = 0; n = (unsigned int) -1; } | |
30 | bool IsNull() const { return (hash == 0 && n == (unsigned int) -1); } | |
31 | ||
32 | friend bool operator<(const COutPoint& a, const COutPoint& b) | |
33 | { | |
34 | return (a.hash < b.hash || (a.hash == b.hash && a.n < b.n)); | |
35 | } | |
36 | ||
37 | friend bool operator==(const COutPoint& a, const COutPoint& b) | |
38 | { | |
39 | return (a.hash == b.hash && a.n == b.n); | |
40 | } | |
41 | ||
42 | friend bool operator!=(const COutPoint& a, const COutPoint& b) | |
43 | { | |
44 | return !(a == b); | |
45 | } | |
46 | ||
f121db58 PW |
47 | std::string ToString() const; |
48 | void print() const; | |
effc2770 EL |
49 | }; |
50 | ||
788536f1 EL |
51 | /** An inpoint - a combination of a transaction and an index n into its vin */ |
52 | class CInPoint | |
53 | { | |
54 | public: | |
55 | CTransaction* ptx; | |
56 | unsigned int n; | |
57 | ||
58 | CInPoint() { SetNull(); } | |
59 | CInPoint(CTransaction* ptxIn, unsigned int nIn) { ptx = ptxIn; n = nIn; } | |
60 | void SetNull() { ptx = NULL; n = (unsigned int) -1; } | |
61 | bool IsNull() const { return (ptx == NULL && n == (unsigned int) -1); } | |
62 | }; | |
63 | ||
05df3fc6 EL |
64 | /** An input of a transaction. It contains the location of the previous |
65 | * transaction's output that it claims and a signature that matches the | |
66 | * output's public key. | |
67 | */ | |
68 | class CTxIn | |
69 | { | |
70 | public: | |
71 | COutPoint prevout; | |
72 | CScript scriptSig; | |
73 | unsigned int nSequence; | |
74 | ||
75 | CTxIn() | |
76 | { | |
77 | nSequence = std::numeric_limits<unsigned int>::max(); | |
78 | } | |
79 | ||
f121db58 PW |
80 | explicit CTxIn(COutPoint prevoutIn, CScript scriptSigIn=CScript(), unsigned int nSequenceIn=std::numeric_limits<unsigned int>::max()); |
81 | CTxIn(uint256 hashPrevTx, unsigned int nOut, CScript scriptSigIn=CScript(), unsigned int nSequenceIn=std::numeric_limits<unsigned int>::max()); | |
05df3fc6 EL |
82 | |
83 | IMPLEMENT_SERIALIZE | |
84 | ( | |
85 | READWRITE(prevout); | |
86 | READWRITE(scriptSig); | |
87 | READWRITE(nSequence); | |
88 | ) | |
89 | ||
90 | bool IsFinal() const | |
91 | { | |
92 | return (nSequence == std::numeric_limits<unsigned int>::max()); | |
93 | } | |
94 | ||
95 | friend bool operator==(const CTxIn& a, const CTxIn& b) | |
96 | { | |
97 | return (a.prevout == b.prevout && | |
98 | a.scriptSig == b.scriptSig && | |
99 | a.nSequence == b.nSequence); | |
100 | } | |
101 | ||
102 | friend bool operator!=(const CTxIn& a, const CTxIn& b) | |
103 | { | |
104 | return !(a == b); | |
105 | } | |
106 | ||
f121db58 PW |
107 | std::string ToString() const; |
108 | void print() const; | |
05df3fc6 EL |
109 | }; |
110 | ||
111 | ||
112 | ||
113 | ||
114 | /** An output of a transaction. It contains the public key that the next input | |
115 | * must be able to sign with to claim it. | |
116 | */ | |
117 | class CTxOut | |
118 | { | |
119 | public: | |
51ed9ec9 | 120 | int64_t nValue; |
05df3fc6 EL |
121 | CScript scriptPubKey; |
122 | ||
123 | CTxOut() | |
124 | { | |
125 | SetNull(); | |
126 | } | |
127 | ||
51ed9ec9 | 128 | CTxOut(int64_t nValueIn, CScript scriptPubKeyIn); |
05df3fc6 EL |
129 | |
130 | IMPLEMENT_SERIALIZE | |
131 | ( | |
132 | READWRITE(nValue); | |
133 | READWRITE(scriptPubKey); | |
134 | ) | |
135 | ||
136 | void SetNull() | |
137 | { | |
138 | nValue = -1; | |
139 | scriptPubKey.clear(); | |
140 | } | |
141 | ||
142 | bool IsNull() const | |
143 | { | |
144 | return (nValue == -1); | |
145 | } | |
146 | ||
f121db58 | 147 | uint256 GetHash() const; |
05df3fc6 | 148 | |
51ed9ec9 | 149 | bool IsDust(int64_t nMinRelayTxFee) const |
05df3fc6 EL |
150 | { |
151 | // "Dust" is defined in terms of CTransaction::nMinRelayTxFee, | |
152 | // which has units satoshis-per-kilobyte. | |
153 | // If you'd pay more than 1/3 in fees | |
154 | // to spend something, then we consider it dust. | |
346427f0 | 155 | // A typical txout is 34 bytes big, and will |
05df3fc6 EL |
156 | // need a CTxIn of at least 148 bytes to spend, |
157 | // so dust is a txout less than 54 uBTC | |
346427f0 | 158 | // (5460 satoshis) with default nMinRelayTxFee |
05df3fc6 EL |
159 | return ((nValue*1000)/(3*((int)GetSerializeSize(SER_DISK,0)+148)) < nMinRelayTxFee); |
160 | } | |
161 | ||
162 | friend bool operator==(const CTxOut& a, const CTxOut& b) | |
163 | { | |
164 | return (a.nValue == b.nValue && | |
165 | a.scriptPubKey == b.scriptPubKey); | |
166 | } | |
167 | ||
168 | friend bool operator!=(const CTxOut& a, const CTxOut& b) | |
169 | { | |
170 | return !(a == b); | |
171 | } | |
172 | ||
f121db58 PW |
173 | std::string ToString() const; |
174 | void print() const; | |
05df3fc6 EL |
175 | }; |
176 | ||
177 | ||
178 | /** The basic transaction that is broadcasted on the network and contained in | |
179 | * blocks. A transaction can contain multiple inputs and outputs. | |
180 | */ | |
181 | class CTransaction | |
182 | { | |
183 | public: | |
51ed9ec9 BD |
184 | static int64_t nMinTxFee; |
185 | static int64_t nMinRelayTxFee; | |
05df3fc6 EL |
186 | static const int CURRENT_VERSION=1; |
187 | int nVersion; | |
188 | std::vector<CTxIn> vin; | |
189 | std::vector<CTxOut> vout; | |
190 | unsigned int nLockTime; | |
191 | ||
192 | CTransaction() | |
193 | { | |
194 | SetNull(); | |
195 | } | |
196 | ||
197 | IMPLEMENT_SERIALIZE | |
198 | ( | |
199 | READWRITE(this->nVersion); | |
200 | nVersion = this->nVersion; | |
201 | READWRITE(vin); | |
202 | READWRITE(vout); | |
203 | READWRITE(nLockTime); | |
204 | ) | |
205 | ||
206 | void SetNull() | |
207 | { | |
208 | nVersion = CTransaction::CURRENT_VERSION; | |
209 | vin.clear(); | |
210 | vout.clear(); | |
211 | nLockTime = 0; | |
212 | } | |
213 | ||
214 | bool IsNull() const | |
215 | { | |
216 | return (vin.empty() && vout.empty()); | |
217 | } | |
218 | ||
f121db58 PW |
219 | uint256 GetHash() const; |
220 | bool IsNewerThan(const CTransaction& old) const; | |
05df3fc6 EL |
221 | |
222 | bool IsCoinBase() const | |
223 | { | |
224 | return (vin.size() == 1 && vin[0].prevout.IsNull()); | |
225 | } | |
226 | ||
227 | friend bool operator==(const CTransaction& a, const CTransaction& b) | |
228 | { | |
229 | return (a.nVersion == b.nVersion && | |
230 | a.vin == b.vin && | |
231 | a.vout == b.vout && | |
232 | a.nLockTime == b.nLockTime); | |
233 | } | |
234 | ||
235 | friend bool operator!=(const CTransaction& a, const CTransaction& b) | |
236 | { | |
237 | return !(a == b); | |
238 | } | |
239 | ||
240 | ||
f121db58 PW |
241 | std::string ToString() const; |
242 | void print() const; | |
05df3fc6 EL |
243 | }; |
244 | ||
65e7bbef EL |
245 | /** wrapper for CTxOut that provides a more compact serialization */ |
246 | class CTxOutCompressor | |
247 | { | |
248 | private: | |
249 | CTxOut &txout; | |
250 | ||
251 | public: | |
51ed9ec9 BD |
252 | static uint64_t CompressAmount(uint64_t nAmount); |
253 | static uint64_t DecompressAmount(uint64_t nAmount); | |
65e7bbef EL |
254 | |
255 | CTxOutCompressor(CTxOut &txoutIn) : txout(txoutIn) { } | |
256 | ||
257 | IMPLEMENT_SERIALIZE(({ | |
258 | if (!fRead) { | |
51ed9ec9 | 259 | uint64_t nVal = CompressAmount(txout.nValue); |
65e7bbef EL |
260 | READWRITE(VARINT(nVal)); |
261 | } else { | |
51ed9ec9 | 262 | uint64_t nVal = 0; |
65e7bbef EL |
263 | READWRITE(VARINT(nVal)); |
264 | txout.nValue = DecompressAmount(nVal); | |
265 | } | |
266 | CScriptCompressor cscript(REF(txout.scriptPubKey)); | |
267 | READWRITE(cscript); | |
268 | });) | |
269 | }; | |
270 | ||
271 | /** Undo information for a CTxIn | |
272 | * | |
273 | * Contains the prevout's CTxOut being spent, and if this was the | |
274 | * last output of the affected transaction, its metadata as well | |
275 | * (coinbase or not, height, transaction version) | |
276 | */ | |
277 | class CTxInUndo | |
278 | { | |
279 | public: | |
280 | CTxOut txout; // the txout data before being spent | |
281 | bool fCoinBase; // if the outpoint was the last unspent: whether it belonged to a coinbase | |
282 | unsigned int nHeight; // if the outpoint was the last unspent: its height | |
283 | int nVersion; // if the outpoint was the last unspent: its version | |
284 | ||
285 | CTxInUndo() : txout(), fCoinBase(false), nHeight(0), nVersion(0) {} | |
286 | CTxInUndo(const CTxOut &txoutIn, bool fCoinBaseIn = false, unsigned int nHeightIn = 0, int nVersionIn = 0) : txout(txoutIn), fCoinBase(fCoinBaseIn), nHeight(nHeightIn), nVersion(nVersionIn) { } | |
287 | ||
288 | unsigned int GetSerializeSize(int nType, int nVersion) const { | |
289 | return ::GetSerializeSize(VARINT(nHeight*2+(fCoinBase ? 1 : 0)), nType, nVersion) + | |
290 | (nHeight > 0 ? ::GetSerializeSize(VARINT(this->nVersion), nType, nVersion) : 0) + | |
291 | ::GetSerializeSize(CTxOutCompressor(REF(txout)), nType, nVersion); | |
292 | } | |
293 | ||
294 | template<typename Stream> | |
295 | void Serialize(Stream &s, int nType, int nVersion) const { | |
296 | ::Serialize(s, VARINT(nHeight*2+(fCoinBase ? 1 : 0)), nType, nVersion); | |
297 | if (nHeight > 0) | |
298 | ::Serialize(s, VARINT(this->nVersion), nType, nVersion); | |
299 | ::Serialize(s, CTxOutCompressor(REF(txout)), nType, nVersion); | |
300 | } | |
301 | ||
302 | template<typename Stream> | |
303 | void Unserialize(Stream &s, int nType, int nVersion) { | |
304 | unsigned int nCode = 0; | |
305 | ::Unserialize(s, VARINT(nCode), nType, nVersion); | |
306 | nHeight = nCode / 2; | |
307 | fCoinBase = nCode & 1; | |
308 | if (nHeight > 0) | |
309 | ::Unserialize(s, VARINT(this->nVersion), nType, nVersion); | |
310 | ::Unserialize(s, REF(CTxOutCompressor(REF(txout))), nType, nVersion); | |
311 | } | |
312 | }; | |
313 | ||
314 | /** Undo information for a CTransaction */ | |
315 | class CTxUndo | |
316 | { | |
317 | public: | |
318 | // undo information for all txins | |
319 | std::vector<CTxInUndo> vprevout; | |
320 | ||
321 | IMPLEMENT_SERIALIZE( | |
322 | READWRITE(vprevout); | |
323 | ) | |
324 | }; | |
325 | ||
326 | ||
327 | /** pruned version of CTransaction: only retains metadata and unspent transaction outputs | |
328 | * | |
329 | * Serialized format: | |
330 | * - VARINT(nVersion) | |
331 | * - VARINT(nCode) | |
332 | * - unspentness bitvector, for vout[2] and further; least significant byte first | |
333 | * - the non-spent CTxOuts (via CTxOutCompressor) | |
334 | * - VARINT(nHeight) | |
335 | * | |
336 | * The nCode value consists of: | |
337 | * - bit 1: IsCoinBase() | |
338 | * - bit 2: vout[0] is not spent | |
339 | * - bit 4: vout[1] is not spent | |
340 | * - The higher bits encode N, the number of non-zero bytes in the following bitvector. | |
341 | * - In case both bit 2 and bit 4 are unset, they encode N-1, as there must be at | |
342 | * least one non-spent output). | |
343 | * | |
344 | * Example: 0104835800816115944e077fe7c803cfa57f29b36bf87c1d358bb85e | |
345 | * <><><--------------------------------------------><----> | |
346 | * | \ | / | |
347 | * version code vout[1] height | |
348 | * | |
349 | * - version = 1 | |
350 | * - code = 4 (vout[1] is not spent, and 0 non-zero bytes of bitvector follow) | |
351 | * - unspentness bitvector: as 0 non-zero bytes follow, it has length 0 | |
352 | * - vout[1]: 835800816115944e077fe7c803cfa57f29b36bf87c1d35 | |
353 | * * 8358: compact amount representation for 60000000000 (600 BTC) | |
354 | * * 00: special txout type pay-to-pubkey-hash | |
355 | * * 816115944e077fe7c803cfa57f29b36bf87c1d35: address uint160 | |
356 | * - height = 203998 | |
357 | * | |
358 | * | |
359 | * Example: 0109044086ef97d5790061b01caab50f1b8e9c50a5057eb43c2d9563a4eebbd123008c988f1a4a4de2161e0f50aac7f17e7f9555caa486af3b | |
360 | * <><><--><--------------------------------------------------><----------------------------------------------><----> | |
361 | * / \ \ | | / | |
362 | * version code unspentness vout[4] vout[16] height | |
363 | * | |
364 | * - version = 1 | |
365 | * - code = 9 (coinbase, neither vout[0] or vout[1] are unspent, | |
366 | * 2 (1, +1 because both bit 2 and bit 4 are unset) non-zero bitvector bytes follow) | |
367 | * - unspentness bitvector: bits 2 (0x04) and 14 (0x4000) are set, so vout[2+2] and vout[14+2] are unspent | |
368 | * - vout[4]: 86ef97d5790061b01caab50f1b8e9c50a5057eb43c2d9563a4ee | |
369 | * * 86ef97d579: compact amount representation for 234925952 (2.35 BTC) | |
370 | * * 00: special txout type pay-to-pubkey-hash | |
371 | * * 61b01caab50f1b8e9c50a5057eb43c2d9563a4ee: address uint160 | |
372 | * - vout[16]: bbd123008c988f1a4a4de2161e0f50aac7f17e7f9555caa4 | |
373 | * * bbd123: compact amount representation for 110397 (0.001 BTC) | |
374 | * * 00: special txout type pay-to-pubkey-hash | |
375 | * * 8c988f1a4a4de2161e0f50aac7f17e7f9555caa4: address uint160 | |
376 | * - height = 120891 | |
377 | */ | |
378 | class CCoins | |
379 | { | |
380 | public: | |
381 | // whether transaction is a coinbase | |
382 | bool fCoinBase; | |
383 | ||
384 | // unspent transaction outputs; spent outputs are .IsNull(); spent outputs at the end of the array are dropped | |
385 | std::vector<CTxOut> vout; | |
386 | ||
387 | // at which height this transaction was included in the active block chain | |
388 | int nHeight; | |
389 | ||
390 | // version of the CTransaction; accesses to this value should probably check for nHeight as well, | |
391 | // as new tx version will probably only be introduced at certain heights | |
392 | int nVersion; | |
393 | ||
394 | // construct a CCoins from a CTransaction, at a given height | |
ec84e81e | 395 | CCoins(const CTransaction &tx, int nHeightIn) : fCoinBase(tx.IsCoinBase()), vout(tx.vout), nHeight(nHeightIn), nVersion(tx.nVersion) { |
99740bab | 396 | ClearUnspendable(); |
ec84e81e | 397 | } |
65e7bbef EL |
398 | |
399 | // empty constructor | |
400 | CCoins() : fCoinBase(false), vout(0), nHeight(0), nVersion(0) { } | |
401 | ||
402 | // remove spent outputs at the end of vout | |
403 | void Cleanup() { | |
404 | while (vout.size() > 0 && vout.back().IsNull()) | |
405 | vout.pop_back(); | |
406 | if (vout.empty()) | |
407 | std::vector<CTxOut>().swap(vout); | |
408 | } | |
409 | ||
99740bab PW |
410 | void ClearUnspendable() { |
411 | BOOST_FOREACH(CTxOut &txout, vout) { | |
412 | if (txout.scriptPubKey.IsUnspendable()) | |
413 | txout.SetNull(); | |
414 | } | |
415 | Cleanup(); | |
416 | } | |
417 | ||
65e7bbef EL |
418 | void swap(CCoins &to) { |
419 | std::swap(to.fCoinBase, fCoinBase); | |
420 | to.vout.swap(vout); | |
421 | std::swap(to.nHeight, nHeight); | |
422 | std::swap(to.nVersion, nVersion); | |
423 | } | |
424 | ||
425 | // equality test | |
426 | friend bool operator==(const CCoins &a, const CCoins &b) { | |
170e02de PW |
427 | // Empty CCoins objects are always equal. |
428 | if (a.IsPruned() && b.IsPruned()) | |
429 | return true; | |
65e7bbef EL |
430 | return a.fCoinBase == b.fCoinBase && |
431 | a.nHeight == b.nHeight && | |
432 | a.nVersion == b.nVersion && | |
433 | a.vout == b.vout; | |
434 | } | |
435 | friend bool operator!=(const CCoins &a, const CCoins &b) { | |
436 | return !(a == b); | |
437 | } | |
438 | ||
f121db58 | 439 | void CalcMaskSize(unsigned int &nBytes, unsigned int &nNonzeroBytes) const; |
65e7bbef EL |
440 | |
441 | bool IsCoinBase() const { | |
442 | return fCoinBase; | |
443 | } | |
444 | ||
445 | unsigned int GetSerializeSize(int nType, int nVersion) const { | |
446 | unsigned int nSize = 0; | |
447 | unsigned int nMaskSize = 0, nMaskCode = 0; | |
448 | CalcMaskSize(nMaskSize, nMaskCode); | |
449 | bool fFirst = vout.size() > 0 && !vout[0].IsNull(); | |
450 | bool fSecond = vout.size() > 1 && !vout[1].IsNull(); | |
451 | assert(fFirst || fSecond || nMaskCode); | |
452 | unsigned int nCode = 8*(nMaskCode - (fFirst || fSecond ? 0 : 1)) + (fCoinBase ? 1 : 0) + (fFirst ? 2 : 0) + (fSecond ? 4 : 0); | |
453 | // version | |
454 | nSize += ::GetSerializeSize(VARINT(this->nVersion), nType, nVersion); | |
455 | // size of header code | |
456 | nSize += ::GetSerializeSize(VARINT(nCode), nType, nVersion); | |
457 | // spentness bitmask | |
458 | nSize += nMaskSize; | |
459 | // txouts themself | |
460 | for (unsigned int i = 0; i < vout.size(); i++) | |
461 | if (!vout[i].IsNull()) | |
462 | nSize += ::GetSerializeSize(CTxOutCompressor(REF(vout[i])), nType, nVersion); | |
463 | // height | |
464 | nSize += ::GetSerializeSize(VARINT(nHeight), nType, nVersion); | |
465 | return nSize; | |
466 | } | |
467 | ||
468 | template<typename Stream> | |
469 | void Serialize(Stream &s, int nType, int nVersion) const { | |
470 | unsigned int nMaskSize = 0, nMaskCode = 0; | |
471 | CalcMaskSize(nMaskSize, nMaskCode); | |
472 | bool fFirst = vout.size() > 0 && !vout[0].IsNull(); | |
473 | bool fSecond = vout.size() > 1 && !vout[1].IsNull(); | |
474 | assert(fFirst || fSecond || nMaskCode); | |
475 | unsigned int nCode = 8*(nMaskCode - (fFirst || fSecond ? 0 : 1)) + (fCoinBase ? 1 : 0) + (fFirst ? 2 : 0) + (fSecond ? 4 : 0); | |
476 | // version | |
477 | ::Serialize(s, VARINT(this->nVersion), nType, nVersion); | |
478 | // header code | |
479 | ::Serialize(s, VARINT(nCode), nType, nVersion); | |
480 | // spentness bitmask | |
481 | for (unsigned int b = 0; b<nMaskSize; b++) { | |
482 | unsigned char chAvail = 0; | |
483 | for (unsigned int i = 0; i < 8 && 2+b*8+i < vout.size(); i++) | |
484 | if (!vout[2+b*8+i].IsNull()) | |
485 | chAvail |= (1 << i); | |
486 | ::Serialize(s, chAvail, nType, nVersion); | |
487 | } | |
488 | // txouts themself | |
489 | for (unsigned int i = 0; i < vout.size(); i++) { | |
490 | if (!vout[i].IsNull()) | |
491 | ::Serialize(s, CTxOutCompressor(REF(vout[i])), nType, nVersion); | |
492 | } | |
493 | // coinbase height | |
494 | ::Serialize(s, VARINT(nHeight), nType, nVersion); | |
495 | } | |
496 | ||
497 | template<typename Stream> | |
498 | void Unserialize(Stream &s, int nType, int nVersion) { | |
499 | unsigned int nCode = 0; | |
500 | // version | |
501 | ::Unserialize(s, VARINT(this->nVersion), nType, nVersion); | |
502 | // header code | |
503 | ::Unserialize(s, VARINT(nCode), nType, nVersion); | |
504 | fCoinBase = nCode & 1; | |
505 | std::vector<bool> vAvail(2, false); | |
506 | vAvail[0] = nCode & 2; | |
507 | vAvail[1] = nCode & 4; | |
508 | unsigned int nMaskCode = (nCode / 8) + ((nCode & 6) != 0 ? 0 : 1); | |
509 | // spentness bitmask | |
510 | while (nMaskCode > 0) { | |
511 | unsigned char chAvail = 0; | |
512 | ::Unserialize(s, chAvail, nType, nVersion); | |
513 | for (unsigned int p = 0; p < 8; p++) { | |
514 | bool f = (chAvail & (1 << p)) != 0; | |
515 | vAvail.push_back(f); | |
516 | } | |
517 | if (chAvail != 0) | |
518 | nMaskCode--; | |
519 | } | |
520 | // txouts themself | |
521 | vout.assign(vAvail.size(), CTxOut()); | |
522 | for (unsigned int i = 0; i < vAvail.size(); i++) { | |
523 | if (vAvail[i]) | |
524 | ::Unserialize(s, REF(CTxOutCompressor(vout[i])), nType, nVersion); | |
525 | } | |
526 | // coinbase height | |
527 | ::Unserialize(s, VARINT(nHeight), nType, nVersion); | |
528 | Cleanup(); | |
529 | } | |
530 | ||
531 | // mark an outpoint spent, and construct undo information | |
f121db58 | 532 | bool Spend(const COutPoint &out, CTxInUndo &undo); |
65e7bbef EL |
533 | |
534 | // mark a vout spent | |
f121db58 | 535 | bool Spend(int nPos); |
65e7bbef EL |
536 | |
537 | // check whether a particular output is still available | |
538 | bool IsAvailable(unsigned int nPos) const { | |
539 | return (nPos < vout.size() && !vout[nPos].IsNull()); | |
540 | } | |
541 | ||
542 | // check whether the entire CCoins is spent | |
543 | // note that only !IsPruned() CCoins can be serialized | |
544 | bool IsPruned() const { | |
545 | BOOST_FOREACH(const CTxOut &out, vout) | |
546 | if (!out.IsNull()) | |
547 | return false; | |
548 | return true; | |
549 | } | |
550 | }; | |
551 | ||
552 | ||
aabdf9e8 EL |
553 | /** Nodes collect new transactions into a block, hash them into a hash tree, |
554 | * and scan through nonce values to make the block's hash satisfy proof-of-work | |
555 | * requirements. When they solve the proof-of-work, they broadcast the block | |
556 | * to everyone and the block is added to the block chain. The first transaction | |
557 | * in the block is a special one that creates a new coin owned by the creator | |
558 | * of the block. | |
559 | */ | |
560 | class CBlockHeader | |
561 | { | |
562 | public: | |
563 | // header | |
564 | static const int CURRENT_VERSION=2; | |
565 | int nVersion; | |
566 | uint256 hashPrevBlock; | |
567 | uint256 hashMerkleRoot; | |
568 | unsigned int nTime; | |
569 | unsigned int nBits; | |
570 | unsigned int nNonce; | |
571 | ||
572 | CBlockHeader() | |
573 | { | |
574 | SetNull(); | |
575 | } | |
576 | ||
577 | IMPLEMENT_SERIALIZE | |
578 | ( | |
579 | READWRITE(this->nVersion); | |
580 | nVersion = this->nVersion; | |
581 | READWRITE(hashPrevBlock); | |
582 | READWRITE(hashMerkleRoot); | |
583 | READWRITE(nTime); | |
584 | READWRITE(nBits); | |
585 | READWRITE(nNonce); | |
586 | ) | |
587 | ||
588 | void SetNull() | |
589 | { | |
590 | nVersion = CBlockHeader::CURRENT_VERSION; | |
591 | hashPrevBlock = 0; | |
592 | hashMerkleRoot = 0; | |
593 | nTime = 0; | |
594 | nBits = 0; | |
595 | nNonce = 0; | |
596 | } | |
597 | ||
598 | bool IsNull() const | |
599 | { | |
600 | return (nBits == 0); | |
601 | } | |
602 | ||
f121db58 | 603 | uint256 GetHash() const; |
aabdf9e8 | 604 | |
51ed9ec9 | 605 | int64_t GetBlockTime() const |
aabdf9e8 | 606 | { |
51ed9ec9 | 607 | return (int64_t)nTime; |
aabdf9e8 EL |
608 | } |
609 | }; | |
610 | ||
33944573 EL |
611 | |
612 | class CBlock : public CBlockHeader | |
613 | { | |
614 | public: | |
615 | // network and disk | |
616 | std::vector<CTransaction> vtx; | |
617 | ||
618 | // memory only | |
619 | mutable std::vector<uint256> vMerkleTree; | |
620 | ||
621 | CBlock() | |
622 | { | |
623 | SetNull(); | |
624 | } | |
625 | ||
626 | CBlock(const CBlockHeader &header) | |
627 | { | |
628 | SetNull(); | |
629 | *((CBlockHeader*)this) = header; | |
630 | } | |
631 | ||
632 | IMPLEMENT_SERIALIZE | |
633 | ( | |
634 | READWRITE(*(CBlockHeader*)this); | |
635 | READWRITE(vtx); | |
636 | ) | |
637 | ||
638 | void SetNull() | |
639 | { | |
640 | CBlockHeader::SetNull(); | |
641 | vtx.clear(); | |
642 | vMerkleTree.clear(); | |
643 | } | |
644 | ||
645 | CBlockHeader GetBlockHeader() const | |
646 | { | |
647 | CBlockHeader block; | |
648 | block.nVersion = nVersion; | |
649 | block.hashPrevBlock = hashPrevBlock; | |
650 | block.hashMerkleRoot = hashMerkleRoot; | |
651 | block.nTime = nTime; | |
652 | block.nBits = nBits; | |
653 | block.nNonce = nNonce; | |
654 | return block; | |
655 | } | |
656 | ||
f121db58 | 657 | uint256 BuildMerkleTree() const; |
33944573 EL |
658 | |
659 | const uint256 &GetTxHash(unsigned int nIndex) const { | |
660 | assert(vMerkleTree.size() > 0); // BuildMerkleTree must have been called first | |
661 | assert(nIndex < vtx.size()); | |
662 | return vMerkleTree[nIndex]; | |
663 | } | |
664 | ||
f121db58 PW |
665 | std::vector<uint256> GetMerkleBranch(int nIndex) const; |
666 | static uint256 CheckMerkleBranch(uint256 hash, const std::vector<uint256>& vMerkleBranch, int nIndex); | |
667 | void print() const; | |
33944573 EL |
668 | }; |
669 | ||
f9b15a4f PW |
670 | |
671 | /** Describes a place in the block chain to another node such that if the | |
672 | * other node doesn't have the same branch, it can find a recent common trunk. | |
673 | * The further back it is, the further before the fork it may be. | |
674 | */ | |
675 | struct CBlockLocator | |
676 | { | |
677 | std::vector<uint256> vHave; | |
678 | ||
679 | CBlockLocator() {} | |
680 | ||
681 | CBlockLocator(const std::vector<uint256>& vHaveIn) | |
682 | { | |
683 | vHave = vHaveIn; | |
684 | } | |
685 | ||
686 | IMPLEMENT_SERIALIZE | |
687 | ( | |
688 | if (!(nType & SER_GETHASH)) | |
689 | READWRITE(nVersion); | |
690 | READWRITE(vHave); | |
691 | ) | |
692 | ||
693 | void SetNull() | |
694 | { | |
695 | vHave.clear(); | |
696 | } | |
697 | ||
698 | bool IsNull() | |
699 | { | |
700 | return vHave.empty(); | |
701 | } | |
702 | }; | |
703 | ||
05df3fc6 | 704 | #endif |