Dash Core  0.12.2.1
P2P Digital Currency
coins.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 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 
73 class CCoins
74 {
75 public:
77  bool fCoinBase;
78 
80  std::vector<CTxOut> vout;
81 
83  int nHeight;
84 
87  int nVersion;
88 
89  void FromTx(const CTransaction &tx, int nHeightIn) {
90  fCoinBase = tx.IsCoinBase();
91  vout = tx.vout;
92  nHeight = nHeightIn;
93  nVersion = tx.nVersion;
95  }
96 
98  CCoins(const CTransaction &tx, int nHeightIn) {
99  FromTx(tx, nHeightIn);
100  }
101 
102  void Clear() {
103  fCoinBase = false;
104  std::vector<CTxOut>().swap(vout);
105  nHeight = 0;
106  nVersion = 0;
107  }
108 
110  CCoins() : fCoinBase(false), vout(0), nHeight(0), nVersion(0) { }
111 
113  void Cleanup() {
114  while (vout.size() > 0 && vout.back().IsNull())
115  vout.pop_back();
116  if (vout.empty())
117  std::vector<CTxOut>().swap(vout);
118  }
119 
121  BOOST_FOREACH(CTxOut &txout, vout) {
122  if (txout.scriptPubKey.IsUnspendable())
123  txout.SetNull();
124  }
125  Cleanup();
126  }
127 
128  void swap(CCoins &to) {
129  std::swap(to.fCoinBase, fCoinBase);
130  to.vout.swap(vout);
131  std::swap(to.nHeight, nHeight);
132  std::swap(to.nVersion, nVersion);
133  }
134 
136  friend bool operator==(const CCoins &a, const CCoins &b) {
137  // Empty CCoins objects are always equal.
138  if (a.IsPruned() && b.IsPruned())
139  return true;
140  return a.fCoinBase == b.fCoinBase &&
141  a.nHeight == b.nHeight &&
142  a.nVersion == b.nVersion &&
143  a.vout == b.vout;
144  }
145  friend bool operator!=(const CCoins &a, const CCoins &b) {
146  return !(a == b);
147  }
148 
149  void CalcMaskSize(unsigned int &nBytes, unsigned int &nNonzeroBytes) const;
150 
151  bool IsCoinBase() const {
152  return fCoinBase;
153  }
154 
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);
163  // version
164  nSize += ::GetSerializeSize(VARINT(this->nVersion), nType, nVersion);
165  // size of header code
166  nSize += ::GetSerializeSize(VARINT(nCode), nType, nVersion);
167  // spentness bitmask
168  nSize += nMaskSize;
169  // txouts themself
170  for (unsigned int i = 0; i < vout.size(); i++)
171  if (!vout[i].IsNull())
172  nSize += ::GetSerializeSize(CTxOutCompressor(REF(vout[i])), nType, nVersion);
173  // height
174  nSize += ::GetSerializeSize(VARINT(nHeight), nType, nVersion);
175  return nSize;
176  }
177 
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);
186  // version
187  ::Serialize(s, VARINT(this->nVersion), nType, nVersion);
188  // header code
189  ::Serialize(s, VARINT(nCode), nType, nVersion);
190  // spentness bitmask
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())
195  chAvail |= (1 << i);
196  ::Serialize(s, chAvail, nType, nVersion);
197  }
198  // txouts themself
199  for (unsigned int i = 0; i < vout.size(); i++) {
200  if (!vout[i].IsNull())
201  ::Serialize(s, CTxOutCompressor(REF(vout[i])), nType, nVersion);
202  }
203  // coinbase height
204  ::Serialize(s, VARINT(nHeight), nType, nVersion);
205  }
206 
207  template<typename Stream>
208  void Unserialize(Stream &s, int nType, int nVersion) {
209  unsigned int nCode = 0;
210  // version
211  ::Unserialize(s, VARINT(this->nVersion), nType, nVersion);
212  // header code
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);
219  // spentness bitmask
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;
225  vAvail.push_back(f);
226  }
227  if (chAvail != 0)
228  nMaskCode--;
229  }
230  // txouts themself
231  vout.assign(vAvail.size(), CTxOut());
232  for (unsigned int i = 0; i < vAvail.size(); i++) {
233  if (vAvail[i])
235  }
236  // coinbase height
237  ::Unserialize(s, VARINT(nHeight), nType, nVersion);
238  Cleanup();
239  }
240 
242  bool Spend(uint32_t nPos);
243 
245  bool IsAvailable(unsigned int nPos) const {
246  return (nPos < vout.size() && !vout[nPos].IsNull());
247  }
248 
251  bool IsPruned() const {
252  BOOST_FOREACH(const CTxOut &out, vout)
253  if (!out.IsNull())
254  return false;
255  return true;
256  }
257 
258  size_t DynamicMemoryUsage() const {
259  size_t ret = memusage::DynamicUsage(vout);
260  BOOST_FOREACH(const CTxOut &out, vout) {
261  ret += RecursiveDynamicUsage(out.scriptPubKey);
262  }
263  return ret;
264  }
265 };
266 
268 {
269 private:
271 
272 public:
273  CCoinsKeyHasher();
274 
280  size_t operator()(const uint256& key) const {
281  return key.GetHash(salt);
282  }
283 };
284 
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 
296 };
297 
298 typedef boost::unordered_map<uint256, CCoinsCacheEntry, CCoinsKeyHasher> CCoinsMap;
299 
301 {
302  int nHeight;
304  uint64_t nTransactions;
306  uint64_t nSerializedSize;
309 
311 };
312 
313 
316 {
317 public:
319  virtual bool GetCoins(const uint256 &txid, CCoins &coins) const;
320 
323  virtual bool HaveCoins(const uint256 &txid) const;
324 
326  virtual uint256 GetBestBlock() const;
327 
330  virtual bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock);
331 
333  virtual bool GetStats(CCoinsStats &stats) const;
334 
336  virtual ~CCoinsView() {}
337 };
338 
339 
342 {
343 protected:
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 
365 {
366 private:
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  CCoins* operator->() { return &it->second.coins; }
374  CCoins& operator*() { return it->second.coins; }
375  ~CCoinsModifier();
376  friend class CCoinsViewCache;
377 };
378 
381 {
382 protected:
383  /* Whether this cache has an active modifier. */
385 
386 
393 
394  /* Cached dynamic memory usage for the inner CCoins objects. */
395  mutable size_t cachedCoinsUsage;
396 
397 public:
398  CCoinsViewCache(CCoinsView *baseIn);
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 
413  bool HaveCoinsInCache(const uint256 &txid) const;
414 
420  const CCoins* AccessCoins(const uint256 &txid) const;
421 
427  CCoinsModifier ModifyCoins(const uint256 &txid);
428 
439 
445  bool Flush();
446 
451  void Uncache(const uint256 &txid);
452 
454  unsigned int GetCacheSize() const;
455 
457  size_t DynamicMemoryUsage() const;
458 
467  CAmount GetValueIn(const CTransaction& tx) const;
468 
470  bool HaveInputs(const CTransaction& tx) const;
471 
477  double GetPriority(const CTransaction &tx, int nHeight, CAmount &inChainInputValue) const;
478 
479  const CTxOut &GetOutputFor(const CTxIn& input) const;
480 
481  friend class CCoinsModifier;
482 
483 private:
484  CCoinsMap::iterator FetchCoins(const uint256 &txid);
485  CCoinsMap::const_iterator FetchCoins(const uint256 &txid) const;
486 
491 };
492 
493 #endif // BITCOIN_COINS_H
#define VARINT(obj)
Definition: serialize.h:388
bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock)
Definition: coins.cpp:56
bool IsCoinBase() const
Definition: coins.h:151
friend bool operator==(const CCoins &a, const CCoins &b)
equality test
Definition: coins.h:136
bool GetCoins(const uint256 &txid, CCoins &coins) const
Retrieve the CCoins (unspent transaction outputs) for a given txid.
Definition: coins.cpp:90
Definition: coins.h:73
void Clear()
Definition: coins.h:102
unsigned int GetSerializeSize(int nType, int nVersion) const
Definition: coins.h:155
CAmount nTotalAmount
Definition: coins.h:308
bool GetStats(CCoinsStats &stats) const
Calculate statistics about the unspent transaction output set.
Definition: coins.cpp:57
CCoinsView * base
Definition: coins.h:344
size_t DynamicMemoryUsage() const
Calculate the size of the cache (in bytes)
Definition: coins.cpp:68
uint256 hashSerialized
Definition: coins.h:307
void swap(CCoins &to)
Definition: coins.h:128
static size_t DynamicUsage(const int8_t &v)
Definition: memusage.h:25
CCoinsMap cacheCoins
Definition: coins.h:392
CCoinsModifier(CCoinsViewCache &cache_, CCoinsMap::iterator it_, size_t usage)
Definition: coins.cpp:279
uint64_t nTransactionOutputs
Definition: coins.h:305
bool IsAvailable(unsigned int nPos) const
check whether a particular output is still available
Definition: coins.h:245
int nVersion
Definition: coins.h:87
bool fCoinBase
whether transaction is a coinbase
Definition: coins.h:77
void FromTx(const CTransaction &tx, int nHeightIn)
Definition: coins.h:89
void Unserialize(Stream &s, int nType, int nVersion)
Definition: coins.h:208
boost::unordered_map< uint256, CCoinsCacheEntry, CCoinsKeyHasher > CCoinsMap
Definition: coins.h:298
bool HaveCoins(const uint256 &txid) const
Definition: coins.cpp:138
const CTxOut & GetOutputFor(const CTxIn &input) const
Definition: coins.cpp:227
virtual ~CCoinsView()
As we use CCoinsViews polymorphically, have a virtual destructor.
Definition: coins.h:336
virtual uint256 GetBestBlock() const
Retrieve the block hash whose state this CCoinsView currently represents.
Definition: coins.cpp:46
void Serialize(Stream &s, int nType, int nVersion) const
Definition: coins.h:179
CCoinsMap::iterator FetchCoins(const uint256 &txid)
uint256 hashBlock
Definition: coins.h:391
void Cleanup()
remove spent outputs at the end of vout
Definition: coins.h:113
CCoins coins
Definition: coins.h:287
size_t DynamicMemoryUsage() const
Definition: coins.h:258
virtual bool GetCoins(const uint256 &txid, CCoins &coins) const
Retrieve the CCoins (unspent transaction outputs) for a given txid.
Definition: coins.cpp:44
virtual bool GetStats(CCoinsStats &stats) const
Calculate statistics about the unspent transaction output set.
Definition: coins.cpp:48
CCoins(const CTransaction &tx, int nHeightIn)
construct a CCoins from a CTransaction, at a given height
Definition: coins.h:98
int64_t CAmount
Definition: amount.h:14
int nHeight
Definition: coins.h:302
CCoins * operator->()
Definition: coins.h:373
bool IsUnspendable() const
Definition: script.h:634
void ClearUnspendable()
Definition: coins.h:120
std::vector< CTxOut > vout
unspent transaction outputs; spent outputs are .IsNull(); spent outputs at the end of the array are d...
Definition: coins.h:80
CCoinsMap::iterator it
Definition: coins.h:368
bool IsPruned() const
Definition: coins.h:251
CCoinsViewCache & cache
Definition: coins.h:367
const int32_t nVersion
Definition: transaction.h:232
bool Spend(uint32_t nPos)
mark a vout spent
Definition: coins.cpp:35
unsigned char flags
Definition: coins.h:288
CCoinsViewCache(CCoinsView *baseIn)
Definition: coins.cpp:61
CCoinsCacheEntry()
Definition: coins.h:295
void SetBackend(CCoinsView &viewIn)
Definition: coins.cpp:55
~CCoinsViewCache()
Definition: coins.cpp:63
bool HaveInputs(const CTransaction &tx) const
Check whether all prevouts of the transaction are present in the UTXO set represented by this view...
Definition: coins.cpp:246
uint256 hashBlock
Definition: coins.h:303
virtual bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock)
Definition: coins.cpp:47
virtual bool HaveCoins(const uint256 &txid) const
Definition: coins.cpp:45
void SetBestBlock(const uint256 &hashBlock)
Definition: coins.cpp:158
CCoinsModifier ModifyCoins(const uint256 &txid)
Definition: coins.cpp:99
size_t cachedCoinUsage
Definition: coins.h:369
friend bool operator!=(const CCoins &a, const CCoins &b)
Definition: coins.h:145
CScript scriptPubKey
Definition: transaction.h:137
uint256 salt
Definition: coins.h:270
bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock)
Definition: coins.cpp:162
uint256 GetBestBlock() const
Retrieve the block hash whose state this CCoinsView currently represents.
Definition: coins.cpp:54
void Uncache(const uint256 &txid)
Definition: coins.cpp:214
CCoins & operator*()
Definition: coins.h:374
size_t cachedCoinsUsage
Definition: coins.h:395
CCoins()
empty constructor
Definition: coins.h:110
CCoinsViewBacked(CCoinsView *viewIn)
Definition: coins.cpp:51
size_t operator()(const uint256 &key) const
Definition: coins.h:280
uint64_t nSerializedSize
Definition: coins.h:306
CCoinsStats()
Definition: coins.h:310
bool Flush()
Definition: coins.cpp:207
bool HaveCoinsInCache(const uint256 &txid) const
Definition: coins.cpp:147
double GetPriority(const CTransaction &tx, int nHeight, CAmount &inChainInputValue) const
Definition: coins.cpp:260
bool IsCoinBase() const
Definition: transaction.h:284
unsigned int GetCacheSize() const
Calculate the size of the cache (in number of transactions)
Definition: coins.cpp:223
bool HaveCoins(const uint256 &txid) const
Definition: coins.cpp:53
uint256 GetBestBlock() const
Retrieve the block hash whose state this CCoinsView currently represents.
Definition: coins.cpp:152
const std::vector< CTxOut > vout
Definition: transaction.h:234
void CalcMaskSize(unsigned int &nBytes, unsigned int &nNonzeroBytes) const
Definition: coins.cpp:17
uint64_t nTransactions
Definition: coins.h:304
static size_t RecursiveDynamicUsage(const CScript &script)
Definition: core_memusage.h:12
bool hasModifier
Definition: coins.h:384
T & REF(const T &val)
Definition: serialize.h:33
void SetNull()
Definition: transaction.h:155
CCoinsModifier ModifyNewCoins(const uint256 &txid)
Definition: coins.cpp:120
bool GetCoins(const uint256 &txid, CCoins &coins) const
Retrieve the CCoins (unspent transaction outputs) for a given txid.
Definition: coins.cpp:52
CAmount GetValueIn(const CTransaction &tx) const
Definition: coins.cpp:234
int nHeight
at which height this transaction was included in the active block chain
Definition: coins.h:83
const CCoins * AccessCoins(const uint256 &txid) const
Definition: coins.cpp:129