[tests] Add libFuzzer support.
[bitcoinplatinum.git] / src / coins.h
blob065bae56e9e47ec1eba7c8911a6b4402610e470e
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_COINS_H
7 #define BITCOIN_COINS_H
9 #include "primitives/transaction.h"
10 #include "compressor.h"
11 #include "core_memusage.h"
12 #include "hash.h"
13 #include "memusage.h"
14 #include "serialize.h"
15 #include "uint256.h"
17 #include <assert.h>
18 #include <stdint.h>
20 #include <boost/foreach.hpp>
21 #include <unordered_map>
23 /**
24 * Pruned version of CTransaction: only retains metadata and unspent transaction outputs
26 * Serialized format:
27 * - VARINT(nVersion)
28 * - VARINT(nCode)
29 * - unspentness bitvector, for vout[2] and further; least significant byte first
30 * - the non-spent CTxOuts (via CTxOutCompressor)
31 * - VARINT(nHeight)
33 * The nCode value consists of:
34 * - bit 0: IsCoinBase()
35 * - bit 1: vout[0] is not spent
36 * - bit 2: vout[1] is not spent
37 * - The higher bits encode N, the number of non-zero bytes in the following bitvector.
38 * - In case both bit 1 and bit 2 are unset, they encode N-1, as there must be at
39 * least one non-spent output).
41 * Example: 0104835800816115944e077fe7c803cfa57f29b36bf87c1d358bb85e
42 * <><><--------------------------------------------><---->
43 * | \ | /
44 * version code vout[1] height
46 * - version = 1
47 * - code = 4 (vout[1] is not spent, and 0 non-zero bytes of bitvector follow)
48 * - unspentness bitvector: as 0 non-zero bytes follow, it has length 0
49 * - vout[1]: 835800816115944e077fe7c803cfa57f29b36bf87c1d35
50 * * 8358: compact amount representation for 60000000000 (600 BTC)
51 * * 00: special txout type pay-to-pubkey-hash
52 * * 816115944e077fe7c803cfa57f29b36bf87c1d35: address uint160
53 * - height = 203998
56 * Example: 0109044086ef97d5790061b01caab50f1b8e9c50a5057eb43c2d9563a4eebbd123008c988f1a4a4de2161e0f50aac7f17e7f9555caa486af3b
57 * <><><--><--------------------------------------------------><----------------------------------------------><---->
58 * / \ \ | | /
59 * version code unspentness vout[4] vout[16] height
61 * - version = 1
62 * - code = 9 (coinbase, neither vout[0] or vout[1] are unspent,
63 * 2 (1, +1 because both bit 1 and bit 2 are unset) non-zero bitvector bytes follow)
64 * - unspentness bitvector: bits 2 (0x04) and 14 (0x4000) are set, so vout[2+2] and vout[14+2] are unspent
65 * - vout[4]: 86ef97d5790061b01caab50f1b8e9c50a5057eb43c2d9563a4ee
66 * * 86ef97d579: compact amount representation for 234925952 (2.35 BTC)
67 * * 00: special txout type pay-to-pubkey-hash
68 * * 61b01caab50f1b8e9c50a5057eb43c2d9563a4ee: address uint160
69 * - vout[16]: bbd123008c988f1a4a4de2161e0f50aac7f17e7f9555caa4
70 * * bbd123: compact amount representation for 110397 (0.001 BTC)
71 * * 00: special txout type pay-to-pubkey-hash
72 * * 8c988f1a4a4de2161e0f50aac7f17e7f9555caa4: address uint160
73 * - height = 120891
75 class CCoins
77 public:
78 //! whether transaction is a coinbase
79 bool fCoinBase;
81 //! unspent transaction outputs; spent outputs are .IsNull(); spent outputs at the end of the array are dropped
82 std::vector<CTxOut> vout;
84 //! at which height this transaction was included in the active block chain
85 int nHeight;
87 //! version of the CTransaction; accesses to this value should probably check for nHeight as well,
88 //! as new tx version will probably only be introduced at certain heights
89 int nVersion;
91 void FromTx(const CTransaction &tx, int nHeightIn) {
92 fCoinBase = tx.IsCoinBase();
93 vout = tx.vout;
94 nHeight = nHeightIn;
95 nVersion = tx.nVersion;
96 ClearUnspendable();
99 //! construct a CCoins from a CTransaction, at a given height
100 CCoins(const CTransaction &tx, int nHeightIn) {
101 FromTx(tx, nHeightIn);
104 void Clear() {
105 fCoinBase = false;
106 std::vector<CTxOut>().swap(vout);
107 nHeight = 0;
108 nVersion = 0;
111 //! empty constructor
112 CCoins() : fCoinBase(false), vout(0), nHeight(0), nVersion(0) { }
114 //!remove spent outputs at the end of vout
115 void Cleanup() {
116 while (vout.size() > 0 && vout.back().IsNull())
117 vout.pop_back();
118 if (vout.empty())
119 std::vector<CTxOut>().swap(vout);
122 void ClearUnspendable() {
123 BOOST_FOREACH(CTxOut &txout, vout) {
124 if (txout.scriptPubKey.IsUnspendable())
125 txout.SetNull();
127 Cleanup();
130 void swap(CCoins &to) {
131 std::swap(to.fCoinBase, fCoinBase);
132 to.vout.swap(vout);
133 std::swap(to.nHeight, nHeight);
134 std::swap(to.nVersion, nVersion);
137 //! equality test
138 friend bool operator==(const CCoins &a, const CCoins &b) {
139 // Empty CCoins objects are always equal.
140 if (a.IsPruned() && b.IsPruned())
141 return true;
142 return a.fCoinBase == b.fCoinBase &&
143 a.nHeight == b.nHeight &&
144 a.nVersion == b.nVersion &&
145 a.vout == b.vout;
147 friend bool operator!=(const CCoins &a, const CCoins &b) {
148 return !(a == b);
151 void CalcMaskSize(unsigned int &nBytes, unsigned int &nNonzeroBytes) const;
153 bool IsCoinBase() const {
154 return fCoinBase;
157 template<typename Stream>
158 void Serialize(Stream &s) const {
159 unsigned int nMaskSize = 0, nMaskCode = 0;
160 CalcMaskSize(nMaskSize, nMaskCode);
161 bool fFirst = vout.size() > 0 && !vout[0].IsNull();
162 bool fSecond = vout.size() > 1 && !vout[1].IsNull();
163 assert(fFirst || fSecond || nMaskCode);
164 unsigned int nCode = 8*(nMaskCode - (fFirst || fSecond ? 0 : 1)) + (fCoinBase ? 1 : 0) + (fFirst ? 2 : 0) + (fSecond ? 4 : 0);
165 // version
166 ::Serialize(s, VARINT(this->nVersion));
167 // header code
168 ::Serialize(s, VARINT(nCode));
169 // spentness bitmask
170 for (unsigned int b = 0; b<nMaskSize; b++) {
171 unsigned char chAvail = 0;
172 for (unsigned int i = 0; i < 8 && 2+b*8+i < vout.size(); i++)
173 if (!vout[2+b*8+i].IsNull())
174 chAvail |= (1 << i);
175 ::Serialize(s, chAvail);
177 // txouts themself
178 for (unsigned int i = 0; i < vout.size(); i++) {
179 if (!vout[i].IsNull())
180 ::Serialize(s, CTxOutCompressor(REF(vout[i])));
182 // coinbase height
183 ::Serialize(s, VARINT(nHeight));
186 template<typename Stream>
187 void Unserialize(Stream &s) {
188 unsigned int nCode = 0;
189 // version
190 ::Unserialize(s, VARINT(this->nVersion));
191 // header code
192 ::Unserialize(s, VARINT(nCode));
193 fCoinBase = nCode & 1;
194 std::vector<bool> vAvail(2, false);
195 vAvail[0] = (nCode & 2) != 0;
196 vAvail[1] = (nCode & 4) != 0;
197 unsigned int nMaskCode = (nCode / 8) + ((nCode & 6) != 0 ? 0 : 1);
198 // spentness bitmask
199 while (nMaskCode > 0) {
200 unsigned char chAvail = 0;
201 ::Unserialize(s, chAvail);
202 for (unsigned int p = 0; p < 8; p++) {
203 bool f = (chAvail & (1 << p)) != 0;
204 vAvail.push_back(f);
206 if (chAvail != 0)
207 nMaskCode--;
209 // txouts themself
210 vout.assign(vAvail.size(), CTxOut());
211 for (unsigned int i = 0; i < vAvail.size(); i++) {
212 if (vAvail[i])
213 ::Unserialize(s, REF(CTxOutCompressor(vout[i])));
215 // coinbase height
216 ::Unserialize(s, VARINT(nHeight));
217 Cleanup();
220 //! mark a vout spent
221 bool Spend(uint32_t nPos);
223 //! check whether a particular output is still available
224 bool IsAvailable(unsigned int nPos) const {
225 return (nPos < vout.size() && !vout[nPos].IsNull());
228 //! check whether the entire CCoins is spent
229 //! note that only !IsPruned() CCoins can be serialized
230 bool IsPruned() const {
231 BOOST_FOREACH(const CTxOut &out, vout)
232 if (!out.IsNull())
233 return false;
234 return true;
237 size_t DynamicMemoryUsage() const {
238 size_t ret = memusage::DynamicUsage(vout);
239 BOOST_FOREACH(const CTxOut &out, vout) {
240 ret += RecursiveDynamicUsage(out.scriptPubKey);
242 return ret;
246 class SaltedTxidHasher
248 private:
249 /** Salt */
250 const uint64_t k0, k1;
252 public:
253 SaltedTxidHasher();
256 * This *must* return size_t. With Boost 1.46 on 32-bit systems the
257 * unordered_map will behave unpredictably if the custom hasher returns a
258 * uint64_t, resulting in failures when syncing the chain (#4634).
260 size_t operator()(const uint256& txid) const {
261 return SipHashUint256(k0, k1, txid);
265 struct CCoinsCacheEntry
267 CCoins coins; // The actual cached data.
268 unsigned char flags;
270 enum Flags {
271 DIRTY = (1 << 0), // This cache entry is potentially different from the version in the parent view.
272 FRESH = (1 << 1), // The parent view does not have this entry (or it is pruned).
273 /* Note that FRESH is a performance optimization with which we can
274 * erase coins that are fully spent if we know we do not need to
275 * flush the changes to the parent cache. It is always safe to
276 * not mark FRESH if that condition is not guaranteed.
280 CCoinsCacheEntry() : coins(), flags(0) {}
283 typedef std::unordered_map<uint256, CCoinsCacheEntry, SaltedTxidHasher> CCoinsMap;
285 /** Cursor for iterating over CoinsView state */
286 class CCoinsViewCursor
288 public:
289 CCoinsViewCursor(const uint256 &hashBlockIn): hashBlock(hashBlockIn) {}
290 virtual ~CCoinsViewCursor();
292 virtual bool GetKey(uint256 &key) const = 0;
293 virtual bool GetValue(CCoins &coins) const = 0;
294 virtual unsigned int GetValueSize() const = 0;
296 virtual bool Valid() const = 0;
297 virtual void Next() = 0;
299 //! Get best block at the time this cursor was created
300 const uint256 &GetBestBlock() const { return hashBlock; }
301 private:
302 uint256 hashBlock;
305 /** Abstract view on the open txout dataset. */
306 class CCoinsView
308 public:
309 //! Retrieve the CCoins (unspent transaction outputs) for a given txid
310 virtual bool GetCoins(const uint256 &txid, CCoins &coins) const;
312 //! Just check whether we have data for a given txid.
313 //! This may (but cannot always) return true for fully spent transactions
314 virtual bool HaveCoins(const uint256 &txid) const;
316 //! Retrieve the block hash whose state this CCoinsView currently represents
317 virtual uint256 GetBestBlock() const;
319 //! Do a bulk modification (multiple CCoins changes + BestBlock change).
320 //! The passed mapCoins can be modified.
321 virtual bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock);
323 //! Get a cursor to iterate over the whole state
324 virtual CCoinsViewCursor *Cursor() const;
326 //! As we use CCoinsViews polymorphically, have a virtual destructor
327 virtual ~CCoinsView() {}
331 /** CCoinsView backed by another CCoinsView */
332 class CCoinsViewBacked : public CCoinsView
334 protected:
335 CCoinsView *base;
337 public:
338 CCoinsViewBacked(CCoinsView *viewIn);
339 bool GetCoins(const uint256 &txid, CCoins &coins) const;
340 bool HaveCoins(const uint256 &txid) const;
341 uint256 GetBestBlock() const;
342 void SetBackend(CCoinsView &viewIn);
343 bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock);
344 CCoinsViewCursor *Cursor() const;
348 class CCoinsViewCache;
350 /**
351 * A reference to a mutable cache entry. Encapsulating it allows us to run
352 * cleanup code after the modification is finished, and keeping track of
353 * concurrent modifications.
355 class CCoinsModifier
357 private:
358 CCoinsViewCache& cache;
359 CCoinsMap::iterator it;
360 size_t cachedCoinUsage; // Cached memory usage of the CCoins object before modification
361 CCoinsModifier(CCoinsViewCache& cache_, CCoinsMap::iterator it_, size_t usage);
363 public:
364 CCoins* operator->() { return &it->second.coins; }
365 CCoins& operator*() { return it->second.coins; }
366 ~CCoinsModifier();
367 friend class CCoinsViewCache;
370 /** CCoinsView that adds a memory cache for transactions to another CCoinsView */
371 class CCoinsViewCache : public CCoinsViewBacked
373 protected:
374 /* Whether this cache has an active modifier. */
375 bool hasModifier;
379 * Make mutable so that we can "fill the cache" even from Get-methods
380 * declared as "const".
382 mutable uint256 hashBlock;
383 mutable CCoinsMap cacheCoins;
385 /* Cached dynamic memory usage for the inner CCoins objects. */
386 mutable size_t cachedCoinsUsage;
388 public:
389 CCoinsViewCache(CCoinsView *baseIn);
390 ~CCoinsViewCache();
392 // Standard CCoinsView methods
393 bool GetCoins(const uint256 &txid, CCoins &coins) const;
394 bool HaveCoins(const uint256 &txid) const;
395 uint256 GetBestBlock() const;
396 void SetBestBlock(const uint256 &hashBlock);
397 bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock);
400 * Check if we have the given tx already loaded in this cache.
401 * The semantics are the same as HaveCoins(), but no calls to
402 * the backing CCoinsView are made.
404 bool HaveCoinsInCache(const uint256 &txid) const;
407 * Return a pointer to CCoins in the cache, or NULL if not found. This is
408 * more efficient than GetCoins. Modifications to other cache entries are
409 * allowed while accessing the returned pointer.
411 const CCoins* AccessCoins(const uint256 &txid) const;
414 * Return a modifiable reference to a CCoins. If no entry with the given
415 * txid exists, a new one is created. Simultaneous modifications are not
416 * allowed.
418 CCoinsModifier ModifyCoins(const uint256 &txid);
421 * Return a modifiable reference to a CCoins. Assumes that no entry with the given
422 * txid exists and creates a new one. This saves a database access in the case where
423 * the coins were to be wiped out by FromTx anyway. This should not be called with
424 * the 2 historical coinbase duplicate pairs because the new coins are marked fresh, and
425 * in the event the duplicate coinbase was spent before a flush, the now pruned coins
426 * would not properly overwrite the first coinbase of the pair. Simultaneous modifications
427 * are not allowed.
429 CCoinsModifier ModifyNewCoins(const uint256 &txid, bool coinbase);
432 * Push the modifications applied to this cache to its base.
433 * Failure to call this method before destruction will cause the changes to be forgotten.
434 * If false is returned, the state of this cache (and its backing view) will be undefined.
436 bool Flush();
439 * Removes the transaction with the given hash from the cache, if it is
440 * not modified.
442 void Uncache(const uint256 &txid);
444 //! Calculate the size of the cache (in number of transactions)
445 unsigned int GetCacheSize() const;
447 //! Calculate the size of the cache (in bytes)
448 size_t DynamicMemoryUsage() const;
450 /**
451 * Amount of bitcoins coming in to a transaction
452 * Note that lightweight clients may not know anything besides the hash of previous transactions,
453 * so may not be able to calculate this.
455 * @param[in] tx transaction for which we are checking input total
456 * @return Sum of value of all inputs (scriptSigs)
458 CAmount GetValueIn(const CTransaction& tx) const;
460 //! Check whether all prevouts of the transaction are present in the UTXO set represented by this view
461 bool HaveInputs(const CTransaction& tx) const;
463 const CTxOut &GetOutputFor(const CTxIn& input) const;
465 friend class CCoinsModifier;
467 private:
468 CCoinsMap::const_iterator FetchCoins(const uint256 &txid) const;
471 * By making the copy constructor private, we prevent accidentally using it when one intends to create a cache on top of a base cache.
473 CCoinsViewCache(const CCoinsViewCache &);
476 #endif // BITCOIN_COINS_H