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_COINS_H
7 #define BITCOIN_COINS_H
9 #include "compressor.h"
10 #include "core_memusage.h"
12 #include "serialize.h"
18 #include <boost/foreach.hpp>
19 #include <boost/unordered_map.hpp>
22 * Pruned version of CTransaction: only retains metadata and unspent transaction outputs
27 * - unspentness bitvector, for vout[2] and further; least significant byte first
28 * - the non-spent CTxOuts (via CTxOutCompressor)
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).
39 * Example: 0104835800816115944e077fe7c803cfa57f29b36bf87c1d358bb85e
40 * <><><--------------------------------------------><---->
42 * version code vout[1] height
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
54 * Example: 0109044086ef97d5790061b01caab50f1b8e9c50a5057eb43c2d9563a4eebbd123008c988f1a4a4de2161e0f50aac7f17e7f9555caa486af3b
55 * <><><--><--------------------------------------------------><----------------------------------------------><---->
57 * version code unspentness vout[4] vout[16] height
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
76 //! whether transaction is a coinbase
79 //! unspent transaction outputs; spent outputs are .IsNull(); spent outputs at the end of the array are dropped
80 std::vector
<CTxOut
> vout
;
82 //! at which height this transaction was included in the active block chain
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
89 void FromTx(const CTransaction
&tx
, int nHeightIn
) {
90 fCoinBase
= tx
.IsCoinBase();
93 nVersion
= tx
.nVersion
;
97 //! construct a CCoins from a CTransaction, at a given height
98 CCoins(const CTransaction
&tx
, int nHeightIn
) {
99 FromTx(tx
, nHeightIn
);
104 std::vector
<CTxOut
>().swap(vout
);
109 //! empty constructor
110 CCoins() : fCoinBase(false), vout(0), nHeight(0), nVersion(0) { }
112 //!remove spent outputs at the end of vout
114 while (vout
.size() > 0 && vout
.back().IsNull())
117 std::vector
<CTxOut
>().swap(vout
);
120 void ClearUnspendable() {
121 BOOST_FOREACH(CTxOut
&txout
, vout
) {
122 if (txout
.scriptPubKey
.IsUnspendable())
128 void swap(CCoins
&to
) {
129 std::swap(to
.fCoinBase
, fCoinBase
);
131 std::swap(to
.nHeight
, nHeight
);
132 std::swap(to
.nVersion
, nVersion
);
136 friend bool operator==(const CCoins
&a
, const CCoins
&b
) {
137 // Empty CCoins objects are always equal.
138 if (a
.IsPruned() && b
.IsPruned())
140 return a
.fCoinBase
== b
.fCoinBase
&&
141 a
.nHeight
== b
.nHeight
&&
142 a
.nVersion
== b
.nVersion
&&
145 friend bool operator!=(const CCoins
&a
, const CCoins
&b
) {
149 void CalcMaskSize(unsigned int &nBytes
, unsigned int &nNonzeroBytes
) const;
151 bool IsCoinBase() const {
155 unsigned int GetSerializeSize(int nType
, int nVersion
) const {
156 unsigned int nSize
= 0;
157 unsigned int nMaskSize
= 0, nMaskCode
= 0;
158 CalcMaskSize(nMaskSize
, nMaskCode
);
159 bool fFirst
= vout
.size() > 0 && !vout
[0].IsNull();
160 bool fSecond
= vout
.size() > 1 && !vout
[1].IsNull();
161 assert(fFirst
|| fSecond
|| nMaskCode
);
162 unsigned int nCode
= 8*(nMaskCode
- (fFirst
|| fSecond
? 0 : 1)) + (fCoinBase
? 1 : 0) + (fFirst
? 2 : 0) + (fSecond
? 4 : 0);
164 nSize
+= ::GetSerializeSize(VARINT(this->nVersion
), nType
, nVersion
);
165 // size of header code
166 nSize
+= ::GetSerializeSize(VARINT(nCode
), nType
, nVersion
);
170 for (unsigned int i
= 0; i
< vout
.size(); i
++)
171 if (!vout
[i
].IsNull())
172 nSize
+= ::GetSerializeSize(CTxOutCompressor(REF(vout
[i
])), nType
, nVersion
);
174 nSize
+= ::GetSerializeSize(VARINT(nHeight
), nType
, nVersion
);
178 template<typename Stream
>
179 void Serialize(Stream
&s
, int nType
, int nVersion
) const {
180 unsigned int nMaskSize
= 0, nMaskCode
= 0;
181 CalcMaskSize(nMaskSize
, nMaskCode
);
182 bool fFirst
= vout
.size() > 0 && !vout
[0].IsNull();
183 bool fSecond
= vout
.size() > 1 && !vout
[1].IsNull();
184 assert(fFirst
|| fSecond
|| nMaskCode
);
185 unsigned int nCode
= 8*(nMaskCode
- (fFirst
|| fSecond
? 0 : 1)) + (fCoinBase
? 1 : 0) + (fFirst
? 2 : 0) + (fSecond
? 4 : 0);
187 ::Serialize(s
, VARINT(this->nVersion
), nType
, nVersion
);
189 ::Serialize(s
, VARINT(nCode
), nType
, nVersion
);
191 for (unsigned int b
= 0; b
<nMaskSize
; b
++) {
192 unsigned char chAvail
= 0;
193 for (unsigned int i
= 0; i
< 8 && 2+b
*8+i
< vout
.size(); i
++)
194 if (!vout
[2+b
*8+i
].IsNull())
196 ::Serialize(s
, chAvail
, nType
, nVersion
);
199 for (unsigned int i
= 0; i
< vout
.size(); i
++) {
200 if (!vout
[i
].IsNull())
201 ::Serialize(s
, CTxOutCompressor(REF(vout
[i
])), nType
, nVersion
);
204 ::Serialize(s
, VARINT(nHeight
), nType
, nVersion
);
207 template<typename Stream
>
208 void Unserialize(Stream
&s
, int nType
, int nVersion
) {
209 unsigned int nCode
= 0;
211 ::Unserialize(s
, VARINT(this->nVersion
), nType
, nVersion
);
213 ::Unserialize(s
, VARINT(nCode
), nType
, nVersion
);
214 fCoinBase
= nCode
& 1;
215 std::vector
<bool> vAvail(2, false);
216 vAvail
[0] = (nCode
& 2) != 0;
217 vAvail
[1] = (nCode
& 4) != 0;
218 unsigned int nMaskCode
= (nCode
/ 8) + ((nCode
& 6) != 0 ? 0 : 1);
220 while (nMaskCode
> 0) {
221 unsigned char chAvail
= 0;
222 ::Unserialize(s
, chAvail
, nType
, nVersion
);
223 for (unsigned int p
= 0; p
< 8; p
++) {
224 bool f
= (chAvail
& (1 << p
)) != 0;
231 vout
.assign(vAvail
.size(), CTxOut());
232 for (unsigned int i
= 0; i
< vAvail
.size(); i
++) {
234 ::Unserialize(s
, REF(CTxOutCompressor(vout
[i
])), nType
, nVersion
);
237 ::Unserialize(s
, VARINT(nHeight
), nType
, nVersion
);
241 //! mark a vout spent
242 bool Spend(uint32_t nPos
);
244 //! check whether a particular output is still available
245 bool IsAvailable(unsigned int nPos
) const {
246 return (nPos
< vout
.size() && !vout
[nPos
].IsNull());
249 //! check whether the entire CCoins is spent
250 //! note that only !IsPruned() CCoins can be serialized
251 bool IsPruned() const {
252 BOOST_FOREACH(const CTxOut
&out
, vout
)
258 size_t DynamicMemoryUsage() const {
259 size_t ret
= memusage::DynamicUsage(vout
);
260 BOOST_FOREACH(const CTxOut
&out
, vout
) {
261 ret
+= RecursiveDynamicUsage(out
.scriptPubKey
);
267 class CCoinsKeyHasher
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).
280 size_t operator()(const uint256
& key
) const {
281 return key
.GetHash(salt
);
285 struct CCoinsCacheEntry
287 CCoins coins
; // The actual cached data.
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).
295 CCoinsCacheEntry() : coins(), flags(0) {}
298 typedef boost::unordered_map
<uint256
, CCoinsCacheEntry
, CCoinsKeyHasher
> CCoinsMap
;
304 uint64_t nTransactions
;
305 uint64_t nTransactionOutputs
;
306 uint64_t nSerializedSize
;
307 uint256 hashSerialized
;
308 CAmount nTotalAmount
;
310 CCoinsStats() : nHeight(0), nTransactions(0), nTransactionOutputs(0), nSerializedSize(0), nTotalAmount(0) {}
314 /** Abstract view on the open txout dataset. */
318 //! Retrieve the CCoins (unspent transaction outputs) for a given txid
319 virtual bool GetCoins(const uint256
&txid
, CCoins
&coins
) const;
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;
325 //! Retrieve the block hash whose state this CCoinsView currently represents
326 virtual uint256
GetBestBlock() const;
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
);
332 //! Calculate statistics about the unspent transaction output set
333 virtual bool GetStats(CCoinsStats
&stats
) const;
335 //! As we use CCoinsViews polymorphically, have a virtual destructor
336 virtual ~CCoinsView() {}
340 /** CCoinsView backed by another CCoinsView */
341 class CCoinsViewBacked
: public CCoinsView
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;
357 class CCoinsViewCache
;
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.
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
);
373 CCoins
* operator->() { return &it
->second
.coins
; }
374 CCoins
& operator*() { return it
->second
.coins
; }
376 friend class CCoinsViewCache
;
379 /** CCoinsView that adds a memory cache for transactions to another CCoinsView */
380 class CCoinsViewCache
: public CCoinsViewBacked
383 /* Whether this cache has an active modifier. */
388 * Make mutable so that we can "fill the cache" even from Get-methods
389 * declared as "const".
391 mutable uint256 hashBlock
;
392 mutable CCoinsMap cacheCoins
;
394 /* Cached dynamic memory usage for the inner CCoins objects. */
395 mutable size_t cachedCoinsUsage
;
398 CCoinsViewCache(CCoinsView
*baseIn
);
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
);
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.
413 const CCoins
* AccessCoins(const uint256
&txid
) const;
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
420 CCoinsModifier
ModifyCoins(const uint256
&txid
);
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.
429 //! Calculate the size of the cache (in number of transactions)
430 unsigned int GetCacheSize() const;
432 //! Calculate the size of the cache (in bytes)
433 size_t DynamicMemoryUsage() const;
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.
440 * @param[in] tx transaction for which we are checking input total
441 * @return Sum of value of all inputs (scriptSigs)
443 CAmount
GetValueIn(const CTransaction
& tx
) const;
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;
448 //! Return priority of tx at height nHeight
449 double GetPriority(const CTransaction
&tx
, int nHeight
) const;
451 const CTxOut
&GetOutputFor(const CTxIn
& input
) const;
453 friend class CCoinsModifier
;
456 CCoinsMap::iterator
FetchCoins(const uint256
&txid
);
457 CCoinsMap::const_iterator
FetchCoins(const uint256
&txid
) const;
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.
462 CCoinsViewCache(const CCoinsViewCache
&);
465 #endif // BITCOIN_COINS_H