Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

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 
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>
16 
17 #include <assert.h>
18 #include <stdint.h>
19 
20 #include <unordered_map>
21 
29 class Coin
30 {
31 public:
34 
36  unsigned int fCoinBase : 1;
37 
39  uint32_t nHeight : 31;
40 
42  Coin(CTxOut&& outIn, int nHeightIn, bool fCoinBaseIn) : out(std::move(outIn)), fCoinBase(fCoinBaseIn), nHeight(nHeightIn) {}
43  Coin(const CTxOut& outIn, int nHeightIn, bool fCoinBaseIn) : out(outIn), fCoinBase(fCoinBaseIn),nHeight(nHeightIn) {}
44 
45  void Clear() {
46  out.SetNull();
47  fCoinBase = false;
48  nHeight = 0;
49  }
50 
52  Coin() : fCoinBase(false), nHeight(0) { }
53 
54  bool IsCoinBase() const {
55  return fCoinBase;
56  }
57 
58  template<typename Stream>
59  void Serialize(Stream &s) const {
60  assert(!IsSpent());
61  uint32_t code = nHeight * 2 + fCoinBase;
62  ::Serialize(s, VARINT(code));
64  }
65 
66  template<typename Stream>
67  void Unserialize(Stream &s) {
68  uint32_t code = 0;
69  ::Unserialize(s, VARINT(code));
70  nHeight = code >> 1;
71  fCoinBase = code & 1;
73  }
74 
75  bool IsSpent() const {
76  return out.IsNull();
77  }
78 
79  size_t DynamicMemoryUsage() const {
81  }
82 };
83 
85 {
86 private:
88  const uint64_t k0, k1;
89 
90 public:
92 
98  size_t operator()(const COutPoint& id) const {
99  return SipHashUint256Extra(k0, k1, id.hash, id.n);
100  }
101 };
102 
104 {
105  Coin coin; // The actual cached data.
106  unsigned char flags;
107 
108  enum Flags {
109  DIRTY = (1 << 0), // This cache entry is potentially different from the version in the parent view.
110  FRESH = (1 << 1), // The parent view does not have this entry (or it is pruned).
111  /* Note that FRESH is a performance optimization with which we can
112  * erase coins that are fully spent if we know we do not need to
113  * flush the changes to the parent cache. It is always safe to
114  * not mark FRESH if that condition is not guaranteed.
115  */
116  };
117 
119  explicit CCoinsCacheEntry(Coin&& coin_) : coin(std::move(coin_)), flags(0) {}
120 };
121 
122 typedef std::unordered_map<COutPoint, CCoinsCacheEntry, SaltedOutpointHasher> CCoinsMap;
123 
126 {
127 public:
128  CCoinsViewCursor(const uint256 &hashBlockIn): hashBlock(hashBlockIn) {}
129  virtual ~CCoinsViewCursor() {}
130 
131  virtual bool GetKey(COutPoint &key) const = 0;
132  virtual bool GetValue(Coin &coin) const = 0;
133  virtual unsigned int GetValueSize() const = 0;
134 
135  virtual bool Valid() const = 0;
136  virtual void Next() = 0;
137 
139  const uint256 &GetBestBlock() const { return hashBlock; }
140 private:
142 };
143 
146 {
147 public:
152  virtual bool GetCoin(const COutPoint &outpoint, Coin &coin) const;
153 
155  virtual bool HaveCoin(const COutPoint &outpoint) const;
156 
158  virtual uint256 GetBestBlock() const;
159 
164  virtual std::vector<uint256> GetHeadBlocks() const;
165 
168  virtual bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock);
169 
171  virtual CCoinsViewCursor *Cursor() const;
172 
174  virtual ~CCoinsView() {}
175 
177  virtual size_t EstimateSize() const { return 0; }
178 };
179 
180 
183 {
184 protected:
186 
187 public:
188  CCoinsViewBacked(CCoinsView *viewIn);
189  bool GetCoin(const COutPoint &outpoint, Coin &coin) const override;
190  bool HaveCoin(const COutPoint &outpoint) const override;
191  uint256 GetBestBlock() const override;
192  std::vector<uint256> GetHeadBlocks() const override;
193  void SetBackend(CCoinsView &viewIn);
194  bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) override;
195  CCoinsViewCursor *Cursor() const override;
196  size_t EstimateSize() const override;
197 };
198 
199 
202 {
203 protected:
210 
211  /* Cached dynamic memory usage for the inner Coin objects. */
212  mutable size_t cachedCoinsUsage;
213 
214 public:
215  CCoinsViewCache(CCoinsView *baseIn);
216 
220  CCoinsViewCache(const CCoinsViewCache &) = delete;
221 
222  // Standard CCoinsView methods
223  bool GetCoin(const COutPoint &outpoint, Coin &coin) const override;
224  bool HaveCoin(const COutPoint &outpoint) const override;
225  uint256 GetBestBlock() const override;
226  void SetBestBlock(const uint256 &hashBlock);
227  bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) override;
228  CCoinsViewCursor* Cursor() const override {
229  throw std::logic_error("CCoinsViewCache cursor iteration not supported.");
230  }
231 
237  bool HaveCoinInCache(const COutPoint &outpoint) const;
238 
249  const Coin& AccessCoin(const COutPoint &output) const;
250 
255  void AddCoin(const COutPoint& outpoint, Coin&& coin, bool potential_overwrite);
256 
262  bool SpendCoin(const COutPoint &outpoint, Coin* moveto = nullptr);
263 
269  bool Flush();
270 
275  void Uncache(const COutPoint &outpoint);
276 
278  unsigned int GetCacheSize() const;
279 
281  size_t DynamicMemoryUsage() const;
282 
291  CAmount GetValueIn(const CTransaction& tx) const;
292 
294  bool HaveInputs(const CTransaction& tx) const;
295 
296 private:
297  CCoinsMap::iterator FetchCoin(const COutPoint &outpoint) const;
298 };
299 
301 // When check is false, this assumes that overwrites are only possible for coinbase transactions.
302 // When check is true, the underlying view may be queried to determine whether an addition is
303 // an overwrite.
304 // TODO: pass in a boolean to limit these possible overwrites to known
305 // (pre-BIP34) cases.
306 void AddCoins(CCoinsViewCache& cache, const CTransaction& tx, int nHeight, bool check = false);
307 
309 // This function can be quite expensive because in the event of a transaction
310 // which is not found in the cache, it can cause up to MAX_OUTPUTS_PER_BLOCK
311 // lookups to database, so it should be used with care.
312 const Coin& AccessByTxid(const CCoinsViewCache& cache, const uint256& txid);
313 
314 #endif // BITCOIN_COINS_H
uint256 GetBestBlock() const override
Retrieve the block hash whose state this CCoinsView currently represents.
Definition: coins.cpp:25
#define VARINT(obj)
Definition: serialize.h:375
bool IsSpent() const
Definition: coins.h:75
void SetNull()
Definition: transaction.h:166
bool IsCoinBase() const
Definition: coins.h:54
void AddCoin(const COutPoint &outpoint, Coin &&coin, bool potential_overwrite)
Add a coin.
Definition: coins.cpp:66
CCoinsViewCache(CCoinsView *baseIn)
Definition: coins.cpp:34
Definition: coins.h:103
virtual bool GetCoin(const COutPoint &outpoint, Coin &coin) const
Retrieve the Coin (unspent transaction output) for a given outpoint.
Definition: coins.cpp:10
CScript scriptPubKey
Definition: transaction.h:148
Flags
Definition: coins.h:108
const Coin & AccessCoin(const COutPoint &output) const
Return a reference to Coin in the cache, or a pruned one if not found.
Definition: coins.cpp:116
bool Flush()
Push the modifications applied to this cache to its base.
Definition: coins.cpp:203
void SetBackend(CCoinsView &viewIn)
Definition: coins.cpp:27
A UTXO entry.
Definition: coins.h:29
virtual unsigned int GetValueSize() const =0
static size_t DynamicUsage(const int8_t &v)
Dynamic memory usage for built-in types is zero.
Definition: memusage.h:27
wrapper for CTxOut that provides a more compact serialization
Definition: compressor.h:93
virtual void Next()=0
size_t DynamicMemoryUsage() const
Calculate the size of the cache (in bytes)
Definition: coins.cpp:36
CTxOut out
unspent transaction output
Definition: coins.h:33
std::vector< uint256 > GetHeadBlocks() const override
Retrieve the range of blocks that may have been only partially written.
Definition: coins.cpp:26
virtual bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock)
Do a bulk modification (multiple Coin changes + BestBlock change).
Definition: coins.cpp:13
unsigned int fCoinBase
whether containing transaction was a coinbase
Definition: coins.h:36
bool HaveCoinInCache(const COutPoint &outpoint) const
Check if we have the given utxo already loaded in this cache.
Definition: coins.cpp:130
virtual CCoinsViewCursor * Cursor() const
Get a cursor to iterate over the whole state.
Definition: coins.cpp:14
Definition: box.hpp:161
virtual bool HaveCoin(const COutPoint &outpoint) const
Just check whether a given outpoint is unspent.
Definition: coins.cpp:16
CCoinsViewCursor * Cursor() const override
Get a cursor to iterate over the whole state.
Definition: coins.cpp:29
CCoinsViewCursor * Cursor() const override
Get a cursor to iterate over the whole state.
Definition: coins.h:228
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:235
bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) override
Do a bulk modification (multiple Coin changes + BestBlock change).
Definition: coins.cpp:145
void Serialize(Stream &s) const
Definition: coins.h:59
bool SpendCoin(const COutPoint &outpoint, Coin *moveto=nullptr)
Spend a coin.
Definition: coins.cpp:98
Definition: coins.h:109
bool IsNull() const
Definition: transaction.h:173
virtual ~CCoinsView()
As we use CCoinsViews polymorphically, have a virtual destructor.
Definition: coins.h:174
uint256 hashBlock
Definition: coins.h:141
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
void SetBestBlock(const uint256 &hashBlock)
Definition: coins.cpp:141
virtual bool GetValue(Coin &coin) const =0
CCoinsCacheEntry()
Definition: coins.h:118
uint32_t nHeight
at which height this containing transaction was included in the active block chain ...
Definition: coins.h:39
unsigned int GetCacheSize() const
Calculate the size of the cache (in number of transaction outputs)
Definition: coins.cpp:219
false
Definition: bls_dkg.cpp:168
CCoinsMap cacheCoins
Definition: coins.h:209
bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) override
Do a bulk modification (multiple Coin changes + BestBlock change).
Definition: coins.cpp:28
virtual bool Valid() const =0
Abstract view on the open txout dataset.
Definition: coins.h:145
const Coin & AccessByTxid(const CCoinsViewCache &cache, const uint256 &txid)
Utility function to find any unspent output with a given txid.
Definition: coins.cpp:249
CCoinsCacheEntry(Coin &&coin_)
Definition: coins.h:119
CCoinsView * base
Definition: coins.h:185
std::unordered_map< COutPoint, CCoinsCacheEntry, SaltedOutpointHasher > CCoinsMap
Definition: coins.h:122
const uint64_t k1
Definition: coins.h:88
virtual std::vector< uint256 > GetHeadBlocks() const
Retrieve the range of blocks that may have been only partially written.
Definition: coins.cpp:12
An output of a transaction.
Definition: transaction.h:144
size_t operator()(const COutPoint &id) const
This must return size_t.
Definition: coins.h:98
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:26
Coin()
empty constructor
Definition: coins.h:52
CCoinsViewBacked(CCoinsView *viewIn)
Definition: coins.cpp:22
virtual bool GetKey(COutPoint &key) const =0
uint256 GetBestBlock() const override
Retrieve the block hash whose state this CCoinsView currently represents.
Definition: coins.cpp:135
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether a given outpoint is unspent.
Definition: coins.cpp:24
256-bit opaque blob.
Definition: uint256.h:123
uint256 hashBlock
Make mutable so that we can "fill the cache" even from Get-methods declared as "const".
Definition: coins.h:208
void AddCoins(CCoinsViewCache &cache, const CTransaction &tx, int nHeight, bool check=false)
Utility function to add all of a transaction&#39;s outputs to a cache.
Definition: coins.cpp:87
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override
Retrieve the Coin (unspent transaction output) for a given outpoint.
Definition: coins.cpp:57
void Unserialize(Stream &s)
Definition: coins.h:67
Definition: coins.h:110
virtual size_t EstimateSize() const
Estimate database size (0 if not implemented)
Definition: coins.h:177
void Uncache(const COutPoint &outpoint)
Removes the UTXO with the given outpoint from the cache, if it is not modified.
Definition: coins.cpp:210
virtual ~CCoinsViewCursor()
Definition: coins.h:129
virtual uint256 GetBestBlock() const
Retrieve the block hash whose state this CCoinsView currently represents.
Definition: coins.cpp:11
size_t DynamicMemoryUsage() const
Definition: coins.h:79
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override
Retrieve the Coin (unspent transaction output) for a given outpoint.
Definition: coins.cpp:23
CAmount GetValueIn(const CTransaction &tx) const
Amount of dash coming in to a transaction Note that lightweight clients may not know anything besides...
Definition: coins.cpp:223
size_t cachedCoinsUsage
Definition: coins.h:212
void Clear()
Definition: coins.h:45
CCoinsMap::iterator FetchCoin(const COutPoint &outpoint) const
Definition: coins.cpp:40
CCoinsViewCursor(const uint256 &hashBlockIn)
Definition: coins.h:128
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:198
CCoinsView backed by another CCoinsView.
Definition: coins.h:182
size_t EstimateSize() const override
Estimate database size (0 if not implemented)
Definition: coins.cpp:30
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:201
const uint256 & GetBestBlock() const
Get best block at the time this cursor was created.
Definition: coins.h:139
T & REF(const T &val)
Used to bypass the rule against non-const reference to temporary where it makes sense with wrappers s...
Definition: serialize.h:50
unsigned char flags
Definition: coins.h:106
Coin(const CTxOut &outIn, int nHeightIn, bool fCoinBaseIn)
Definition: coins.h:43
uint64_t SipHashUint256Extra(uint64_t k0, uint64_t k1, const uint256 &val, uint32_t extra)
Definition: hash.cpp:208
const uint64_t k0
Salt.
Definition: coins.h:88
Coin coin
Definition: coins.h:105
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether a given outpoint is unspent.
Definition: coins.cpp:125
Coin(CTxOut &&outIn, int nHeightIn, bool fCoinBaseIn)
construct a Coin from a CTxOut and height/coinbase information.
Definition: coins.h:42
Cursor for iterating over CoinsView state.
Definition: coins.h:125
Released under the MIT license