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_COINS_H
7 : #define BITCOIN_COINS_H
8 :
9 : #include "compressor.h"
10 : #include "core_memusage.h"
11 : #include "memusage.h"
12 : #include "serialize.h"
13 : #include "uint256.h"
14 :
15 : #include <assert.h>
16 : #include <stdint.h>
17 :
18 : #include <boost/foreach.hpp>
19 : #include <boost/unordered_map.hpp>
20 :
21 : /**
22 : * Pruned version of CTransaction: only retains metadata and unspent transaction outputs
23 : *
24 : * Serialized format:
25 : * - VARINT(nVersion)
26 : * - VARINT(nCode)
27 : * - unspentness bitvector, for vout[2] and further; least significant byte first
28 : * - the non-spent CTxOuts (via CTxOutCompressor)
29 : * - VARINT(nHeight)
30 : *
31 : * The nCode value consists of:
32 : * - bit 1: IsCoinBase()
33 : * - bit 2: vout[0] is not spent
34 : * - bit 4: vout[1] is not spent
35 : * - The higher bits encode N, the number of non-zero bytes in the following bitvector.
36 : * - In case both bit 2 and bit 4 are unset, they encode N-1, as there must be at
37 : * least one non-spent output).
38 : *
39 : * Example: 0104835800816115944e077fe7c803cfa57f29b36bf87c1d358bb85e
40 : * <><><--------------------------------------------><---->
41 : * | \ | /
42 : * version code vout[1] height
43 : *
44 : * - version = 1
45 : * - code = 4 (vout[1] is not spent, and 0 non-zero bytes of bitvector follow)
46 : * - unspentness bitvector: as 0 non-zero bytes follow, it has length 0
47 : * - vout[1]: 835800816115944e077fe7c803cfa57f29b36bf87c1d35
48 : * * 8358: compact amount representation for 60000000000 (600 BTC)
49 : * * 00: special txout type pay-to-pubkey-hash
50 : * * 816115944e077fe7c803cfa57f29b36bf87c1d35: address uint160
51 : * - height = 203998
52 : *
53 : *
54 : * Example: 0109044086ef97d5790061b01caab50f1b8e9c50a5057eb43c2d9563a4eebbd123008c988f1a4a4de2161e0f50aac7f17e7f9555caa486af3b
55 : * <><><--><--------------------------------------------------><----------------------------------------------><---->
56 : * / \ \ | | /
57 : * version code unspentness vout[4] vout[16] height
58 : *
59 : * - version = 1
60 : * - code = 9 (coinbase, neither vout[0] or vout[1] are unspent,
61 : * 2 (1, +1 because both bit 2 and bit 4 are unset) non-zero bitvector bytes follow)
62 : * - unspentness bitvector: bits 2 (0x04) and 14 (0x4000) are set, so vout[2+2] and vout[14+2] are unspent
63 : * - vout[4]: 86ef97d5790061b01caab50f1b8e9c50a5057eb43c2d9563a4ee
64 : * * 86ef97d579: compact amount representation for 234925952 (2.35 BTC)
65 : * * 00: special txout type pay-to-pubkey-hash
66 : * * 61b01caab50f1b8e9c50a5057eb43c2d9563a4ee: address uint160
67 : * - vout[16]: bbd123008c988f1a4a4de2161e0f50aac7f17e7f9555caa4
68 : * * bbd123: compact amount representation for 110397 (0.001 BTC)
69 : * * 00: special txout type pay-to-pubkey-hash
70 : * * 8c988f1a4a4de2161e0f50aac7f17e7f9555caa4: address uint160
71 : * - height = 120891
72 : */
73 2480802 : class CCoins
74 : {
75 : public:
76 : //! whether transaction is a coinbase
77 : bool fCoinBase;
78 :
79 : //! unspent transaction outputs; spent outputs are .IsNull(); spent outputs at the end of the array are dropped
80 : std::vector<CTxOut> vout;
81 :
82 : //! at which height this transaction was included in the active block chain
83 : int nHeight;
84 :
85 : //! version of the CTransaction; accesses to this value should probably check for nHeight as well,
86 : //! as new tx version will probably only be introduced at certain heights
87 : int nVersion;
88 :
89 23824 : void FromTx(const CTransaction &tx, int nHeightIn) {
90 23824 : fCoinBase = tx.IsCoinBase();
91 23824 : vout = tx.vout;
92 23824 : nHeight = nHeightIn;
93 23824 : nVersion = tx.nVersion;
94 23824 : ClearUnspendable();
95 23824 : }
96 :
97 : //! construct a CCoins from a CTransaction, at a given height
98 11548 : CCoins(const CTransaction &tx, int nHeightIn) {
99 11548 : FromTx(tx, nHeightIn);
100 11548 : }
101 :
102 68509 : void Clear() {
103 68509 : fCoinBase = false;
104 137018 : std::vector<CTxOut>().swap(vout);
105 68509 : nHeight = 0;
106 68509 : nVersion = 0;
107 68509 : }
108 :
109 : //! empty constructor
110 2422204 : CCoins() : fCoinBase(false), vout(0), nHeight(0), nVersion(0) { }
111 :
112 : //!remove spent outputs at the end of vout
113 125456 : void Cleanup() {
114 518436 : while (vout.size() > 0 && vout.back().IsNull())
115 18693 : vout.pop_back();
116 250912 : if (vout.empty())
117 78934 : std::vector<CTxOut>().swap(vout);
118 125456 : }
119 :
120 35241 : void ClearUnspendable() {
121 449016 : BOOST_FOREACH(CTxOut &txout, vout) {
122 79190 : if (txout.scriptPubKey.IsUnspendable())
123 : txout.SetNull();
124 : }
125 35241 : Cleanup();
126 35241 : }
127 :
128 : void swap(CCoins &to) {
129 241792 : std::swap(to.fCoinBase, fCoinBase);
130 241792 : to.vout.swap(vout);
131 241792 : std::swap(to.nHeight, nHeight);
132 241792 : std::swap(to.nVersion, nVersion);
133 : }
134 :
135 : //! equality test
136 219741 : friend bool operator==(const CCoins &a, const CCoins &b) {
137 : // Empty CCoins objects are always equal.
138 219741 : if (a.IsPruned() && b.IsPruned())
139 : return true;
140 151257 : return a.fCoinBase == b.fCoinBase &&
141 151257 : a.nHeight == b.nHeight &&
142 302514 : a.nVersion == b.nVersion &&
143 302514 : a.vout == b.vout;
144 : }
145 : friend bool operator!=(const CCoins &a, const CCoins &b) {
146 11417 : return !(a == b);
147 : }
148 :
149 : void CalcMaskSize(unsigned int &nBytes, unsigned int &nNonzeroBytes) const;
150 :
151 0 : bool IsCoinBase() const {
152 0 : return fCoinBase;
153 : }
154 :
155 5514 : unsigned int GetSerializeSize(int nType, int nVersion) const {
156 5514 : unsigned int nSize = 0;
157 5514 : unsigned int nMaskSize = 0, nMaskCode = 0;
158 5514 : CalcMaskSize(nMaskSize, nMaskCode);
159 11028 : bool fFirst = vout.size() > 0 && !vout[0].IsNull();
160 11028 : bool fSecond = vout.size() > 1 && !vout[1].IsNull();
161 5514 : assert(fFirst || fSecond || nMaskCode);
162 5514 : unsigned int nCode = 8*(nMaskCode - (fFirst || fSecond ? 0 : 1)) + (fCoinBase ? 1 : 0) + (fFirst ? 2 : 0) + (fSecond ? 4 : 0);
163 : // version
164 11028 : nSize += ::GetSerializeSize(VARINT(this->nVersion), nType, nVersion);
165 : // size of header code
166 11028 : nSize += ::GetSerializeSize(VARINT(nCode), nType, nVersion);
167 : // spentness bitmask
168 5514 : nSize += nMaskSize;
169 : // txouts themself
170 22704 : for (unsigned int i = 0; i < vout.size(); i++)
171 11676 : if (!vout[i].IsNull())
172 11430 : nSize += ::GetSerializeSize(CTxOutCompressor(REF(vout[i])), nType, nVersion);
173 : // height
174 11028 : nSize += ::GetSerializeSize(VARINT(nHeight), nType, nVersion);
175 5514 : return nSize;
176 : }
177 :
178 : template<typename Stream>
179 5514 : void Serialize(Stream &s, int nType, int nVersion) const {
180 5514 : unsigned int nMaskSize = 0, nMaskCode = 0;
181 5514 : CalcMaskSize(nMaskSize, nMaskCode);
182 11028 : bool fFirst = vout.size() > 0 && !vout[0].IsNull();
183 11028 : bool fSecond = vout.size() > 1 && !vout[1].IsNull();
184 5514 : assert(fFirst || fSecond || nMaskCode);
185 5514 : unsigned int nCode = 8*(nMaskCode - (fFirst || fSecond ? 0 : 1)) + (fCoinBase ? 1 : 0) + (fFirst ? 2 : 0) + (fSecond ? 4 : 0);
186 : // version
187 5514 : ::Serialize(s, VARINT(this->nVersion), nType, nVersion);
188 : // header code
189 5514 : ::Serialize(s, VARINT(nCode), nType, nVersion);
190 : // spentness bitmask
191 26 : for (unsigned int b = 0; b<nMaskSize; b++) {
192 : unsigned char chAvail = 0;
193 126 : for (unsigned int i = 0; i < 8 && 2+b*8+i < vout.size(); i++)
194 100 : if (!vout[2+b*8+i].IsNull())
195 50 : chAvail |= (1 << i);
196 26 : ::Serialize(s, chAvail, nType, nVersion);
197 : }
198 : // txouts themself
199 17190 : for (unsigned int i = 0; i < vout.size(); i++) {
200 11676 : if (!vout[i].IsNull())
201 11430 : ::Serialize(s, CTxOutCompressor(REF(vout[i])), nType, nVersion);
202 : }
203 : // coinbase height
204 5514 : ::Serialize(s, VARINT(nHeight), nType, nVersion);
205 5514 : }
206 :
207 : template<typename Stream>
208 11521 : void Unserialize(Stream &s, int nType, int nVersion) {
209 11521 : unsigned int nCode = 0;
210 : // version
211 11521 : ::Unserialize(s, VARINT(this->nVersion), nType, nVersion);
212 : // header code
213 11521 : ::Unserialize(s, VARINT(nCode), nType, nVersion);
214 11521 : fCoinBase = nCode & 1;
215 23042 : std::vector<bool> vAvail(2, false);
216 23042 : vAvail[0] = (nCode & 2) != 0;
217 23042 : vAvail[1] = (nCode & 4) != 0;
218 11521 : unsigned int nMaskCode = (nCode / 8) + ((nCode & 6) != 0 ? 0 : 1);
219 : // spentness bitmask
220 23050 : while (nMaskCode > 0) {
221 8 : unsigned char chAvail = 0;
222 8 : ::Unserialize(s, chAvail, nType, nVersion);
223 72 : for (unsigned int p = 0; p < 8; p++) {
224 64 : bool f = (chAvail & (1 << p)) != 0;
225 64 : vAvail.push_back(f);
226 : }
227 8 : if (chAvail != 0)
228 8 : nMaskCode--;
229 : }
230 : // txouts themself
231 23042 : vout.assign(vAvail.size(), CTxOut());
232 69254 : for (unsigned int i = 0; i < vAvail.size(); i++) {
233 69318 : if (vAvail[i])
234 46740 : ::Unserialize(s, REF(CTxOutCompressor(vout[i])), nType, nVersion);
235 : }
236 : // coinbase height
237 11521 : ::Unserialize(s, VARINT(nHeight), nType, nVersion);
238 11521 : Cleanup();
239 11521 : }
240 :
241 : //! mark a vout spent
242 : bool Spend(uint32_t nPos);
243 :
244 : //! check whether a particular output is still available
245 : bool IsAvailable(unsigned int nPos) const {
246 86283 : return (nPos < vout.size() && !vout[nPos].IsNull());
247 : }
248 :
249 : //! check whether the entire CCoins is spent
250 : //! note that only !IsPruned() CCoins can be serialized
251 742022 : bool IsPruned() const {
252 5080564 : BOOST_FOREACH(const CTxOut &out, vout)
253 455313 : if (!out.IsNull())
254 453808 : return false;
255 288214 : return true;
256 : }
257 :
258 771388 : size_t DynamicMemoryUsage() const {
259 1542776 : size_t ret = memusage::DynamicUsage(vout);
260 6649115 : BOOST_FOREACH(const CTxOut &out, vout) {
261 1116870 : ret += RecursiveDynamicUsage(out.scriptPubKey);
262 : }
263 771388 : return ret;
264 : }
265 : };
266 :
267 : class CCoinsKeyHasher
268 : {
269 : private:
270 : uint256 salt;
271 :
272 : public:
273 : CCoinsKeyHasher();
274 :
275 : /**
276 : * This *must* return size_t. With Boost 1.46 on 32-bit systems the
277 : * unordered_map will behave unpredictably if the custom hasher returns a
278 : * uint64_t, resulting in failures when syncing the chain (#4634).
279 : */
280 : size_t operator()(const uint256& key) const {
281 847430 : return key.GetHash(salt);
282 : }
283 : };
284 :
285 3636772 : struct CCoinsCacheEntry
286 : {
287 : CCoins coins; // The actual cached data.
288 : unsigned char flags;
289 :
290 : enum Flags {
291 : DIRTY = (1 << 0), // This cache entry is potentially different from the version in the parent view.
292 : FRESH = (1 << 1), // The parent view does not have this entry (or it is pruned).
293 : };
294 :
295 280140 : CCoinsCacheEntry() : coins(), flags(0) {}
296 : };
297 :
298 : typedef boost::unordered_map<uint256, CCoinsCacheEntry, CCoinsKeyHasher> CCoinsMap;
299 :
300 : struct CCoinsStats
301 : {
302 : int nHeight;
303 : uint256 hashBlock;
304 : uint64_t nTransactions;
305 : uint64_t nTransactionOutputs;
306 : uint64_t nSerializedSize;
307 : uint256 hashSerialized;
308 : CAmount nTotalAmount;
309 :
310 0 : CCoinsStats() : nHeight(0), nTransactions(0), nTransactionOutputs(0), nSerializedSize(0), nTotalAmount(0) {}
311 : };
312 :
313 :
314 : /** Abstract view on the open txout dataset. */
315 21315 : class CCoinsView
316 : {
317 : public:
318 : //! Retrieve the CCoins (unspent transaction outputs) for a given txid
319 : virtual bool GetCoins(const uint256 &txid, CCoins &coins) const;
320 :
321 : //! Just check whether we have data for a given txid.
322 : //! This may (but cannot always) return true for fully spent transactions
323 : virtual bool HaveCoins(const uint256 &txid) const;
324 :
325 : //! Retrieve the block hash whose state this CCoinsView currently represents
326 : virtual uint256 GetBestBlock() const;
327 :
328 : //! Do a bulk modification (multiple CCoins changes + BestBlock change).
329 : //! The passed mapCoins can be modified.
330 : virtual bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock);
331 :
332 : //! Calculate statistics about the unspent transaction output set
333 : virtual bool GetStats(CCoinsStats &stats) const;
334 :
335 : //! As we use CCoinsViews polymorphically, have a virtual destructor
336 21315 : virtual ~CCoinsView() {}
337 : };
338 :
339 :
340 : /** CCoinsView backed by another CCoinsView */
341 41222 : class CCoinsViewBacked : public CCoinsView
342 : {
343 : protected:
344 : CCoinsView *base;
345 :
346 : public:
347 : CCoinsViewBacked(CCoinsView *viewIn);
348 : bool GetCoins(const uint256 &txid, CCoins &coins) const;
349 : bool HaveCoins(const uint256 &txid) const;
350 : uint256 GetBestBlock() const;
351 : void SetBackend(CCoinsView &viewIn);
352 : bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock);
353 : bool GetStats(CCoinsStats &stats) const;
354 : };
355 :
356 :
357 : class CCoinsViewCache;
358 :
359 : /**
360 : * A reference to a mutable cache entry. Encapsulating it allows us to run
361 : * cleanup code after the modification is finished, and keeping track of
362 : * concurrent modifications.
363 : */
364 : class CCoinsModifier
365 : {
366 : private:
367 : CCoinsViewCache& cache;
368 : CCoinsMap::iterator it;
369 : size_t cachedCoinUsage; // Cached memory usage of the CCoins object before modification
370 : CCoinsModifier(CCoinsViewCache& cache_, CCoinsMap::iterator it_, size_t usage);
371 :
372 : public:
373 168183 : CCoins* operator->() { return &it->second.coins; }
374 149642 : CCoins& operator*() { return it->second.coins; }
375 : ~CCoinsModifier();
376 : friend class CCoinsViewCache;
377 : };
378 :
379 : /** CCoinsView that adds a memory cache for transactions to another CCoinsView */
380 : class CCoinsViewCache : public CCoinsViewBacked
381 : {
382 : protected:
383 : /* Whether this cache has an active modifier. */
384 : bool hasModifier;
385 :
386 :
387 : /**
388 : * Make mutable so that we can "fill the cache" even from Get-methods
389 : * declared as "const".
390 : */
391 : mutable uint256 hashBlock;
392 : mutable CCoinsMap cacheCoins;
393 :
394 : /* Cached dynamic memory usage for the inner CCoins objects. */
395 : mutable size_t cachedCoinsUsage;
396 :
397 : public:
398 : CCoinsViewCache(CCoinsView *baseIn);
399 : ~CCoinsViewCache();
400 :
401 : // Standard CCoinsView methods
402 : bool GetCoins(const uint256 &txid, CCoins &coins) const;
403 : bool HaveCoins(const uint256 &txid) const;
404 : uint256 GetBestBlock() const;
405 : void SetBestBlock(const uint256 &hashBlock);
406 : bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock);
407 :
408 : /**
409 : * Return a pointer to CCoins in the cache, or NULL if not found. This is
410 : * more efficient than GetCoins. Modifications to other cache entries are
411 : * allowed while accessing the returned pointer.
412 : */
413 : const CCoins* AccessCoins(const uint256 &txid) const;
414 :
415 : /**
416 : * Return a modifiable reference to a CCoins. If no entry with the given
417 : * txid exists, a new one is created. Simultaneous modifications are not
418 : * allowed.
419 : */
420 : CCoinsModifier ModifyCoins(const uint256 &txid);
421 :
422 : /**
423 : * Push the modifications applied to this cache to its base.
424 : * Failure to call this method before destruction will cause the changes to be forgotten.
425 : * If false is returned, the state of this cache (and its backing view) will be undefined.
426 : */
427 : bool Flush();
428 :
429 : //! Calculate the size of the cache (in number of transactions)
430 : unsigned int GetCacheSize() const;
431 :
432 : //! Calculate the size of the cache (in bytes)
433 : size_t DynamicMemoryUsage() const;
434 :
435 : /**
436 : * Amount of bitcoins coming in to a transaction
437 : * Note that lightweight clients may not know anything besides the hash of previous transactions,
438 : * so may not be able to calculate this.
439 : *
440 : * @param[in] tx transaction for which we are checking input total
441 : * @return Sum of value of all inputs (scriptSigs)
442 : */
443 : CAmount GetValueIn(const CTransaction& tx) const;
444 :
445 : //! Check whether all prevouts of the transaction are present in the UTXO set represented by this view
446 : bool HaveInputs(const CTransaction& tx) const;
447 :
448 : //! Return priority of tx at height nHeight
449 : double GetPriority(const CTransaction &tx, int nHeight) const;
450 :
451 : const CTxOut &GetOutputFor(const CTxIn& input) const;
452 :
453 : friend class CCoinsModifier;
454 :
455 : private:
456 : CCoinsMap::iterator FetchCoins(const uint256 &txid);
457 : CCoinsMap::const_iterator FetchCoins(const uint256 &txid) const;
458 :
459 : /**
460 : * By making the copy constructor private, we prevent accidentally using it when one intends to create a cache on top of a base cache.
461 : */
462 : CCoinsViewCache(const CCoinsViewCache &);
463 : };
464 :
465 : #endif // BITCOIN_COINS_H
|