Bitcoin Core  0.20.99
P2P Digital Currency
coins.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2020 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 <crypto/siphash.h>
12 #include <memusage.h>
13 #include <primitives/transaction.h>
14 #include <serialize.h>
15 #include <uint256.h>
16 
17 #include <assert.h>
18 #include <stdint.h>
19 
20 #include <functional>
21 #include <unordered_map>
22 
30 class Coin
31 {
32 public:
35 
37  unsigned int fCoinBase : 1;
38 
40  uint32_t nHeight : 31;
41 
43  Coin(CTxOut&& outIn, int nHeightIn, bool fCoinBaseIn) : out(std::move(outIn)), fCoinBase(fCoinBaseIn), nHeight(nHeightIn) {}
44  Coin(const CTxOut& outIn, int nHeightIn, bool fCoinBaseIn) : out(outIn), fCoinBase(fCoinBaseIn),nHeight(nHeightIn) {}
45 
46  void Clear() {
47  out.SetNull();
48  fCoinBase = false;
49  nHeight = 0;
50  }
51 
53  Coin() : fCoinBase(false), nHeight(0) { }
54 
55  bool IsCoinBase() const {
56  return fCoinBase;
57  }
58 
59  template<typename Stream>
60  void Serialize(Stream &s) const {
61  assert(!IsSpent());
62  uint32_t code = nHeight * uint32_t{2} + fCoinBase;
63  ::Serialize(s, VARINT(code));
64  ::Serialize(s, Using<TxOutCompression>(out));
65  }
66 
67  template<typename Stream>
68  void Unserialize(Stream &s) {
69  uint32_t code = 0;
70  ::Unserialize(s, VARINT(code));
71  nHeight = code >> 1;
72  fCoinBase = code & 1;
73  ::Unserialize(s, Using<TxOutCompression>(out));
74  }
75 
76  bool IsSpent() const {
77  return out.IsNull();
78  }
79 
80  size_t DynamicMemoryUsage() const {
82  }
83 };
84 
86 {
87 private:
89  const uint64_t k0, k1;
90 
91 public:
93 
107  size_t operator()(const COutPoint& id) const noexcept {
108  return SipHashUint256Extra(k0, k1, id.hash, id.n);
109  }
110 };
111 
128 {
129  Coin coin; // The actual cached data.
130  unsigned char flags;
131 
132  enum Flags {
140  DIRTY = (1 << 0),
150  FRESH = (1 << 1),
151  };
152 
153  CCoinsCacheEntry() : flags(0) {}
154  explicit CCoinsCacheEntry(Coin&& coin_) : coin(std::move(coin_)), flags(0) {}
155 };
156 
157 typedef std::unordered_map<COutPoint, CCoinsCacheEntry, SaltedOutpointHasher> CCoinsMap;
158 
161 {
162 public:
163  CCoinsViewCursor(const uint256 &hashBlockIn): hashBlock(hashBlockIn) {}
164  virtual ~CCoinsViewCursor() {}
165 
166  virtual bool GetKey(COutPoint &key) const = 0;
167  virtual bool GetValue(Coin &coin) const = 0;
168  virtual unsigned int GetValueSize() const = 0;
169 
170  virtual bool Valid() const = 0;
171  virtual void Next() = 0;
172 
174  const uint256 &GetBestBlock() const { return hashBlock; }
175 private:
177 };
178 
181 {
182 public:
187  virtual bool GetCoin(const COutPoint &outpoint, Coin &coin) const;
188 
190  virtual bool HaveCoin(const COutPoint &outpoint) const;
191 
193  virtual uint256 GetBestBlock() const;
194 
199  virtual std::vector<uint256> GetHeadBlocks() const;
200 
203  virtual bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock);
204 
206  virtual CCoinsViewCursor *Cursor() const;
207 
209  virtual ~CCoinsView() {}
210 
212  virtual size_t EstimateSize() const { return 0; }
213 };
214 
215 
218 {
219 protected:
221 
222 public:
223  CCoinsViewBacked(CCoinsView *viewIn);
224  bool GetCoin(const COutPoint &outpoint, Coin &coin) const override;
225  bool HaveCoin(const COutPoint &outpoint) const override;
226  uint256 GetBestBlock() const override;
227  std::vector<uint256> GetHeadBlocks() const override;
228  void SetBackend(CCoinsView &viewIn);
229  bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) override;
230  CCoinsViewCursor *Cursor() const override;
231  size_t EstimateSize() const override;
232 };
233 
234 
237 {
238 protected:
244  mutable CCoinsMap cacheCoins;
245 
246  /* Cached dynamic memory usage for the inner Coin objects. */
247  mutable size_t cachedCoinsUsage;
248 
249 public:
250  CCoinsViewCache(CCoinsView *baseIn);
251 
255  CCoinsViewCache(const CCoinsViewCache &) = delete;
256 
257  // Standard CCoinsView methods
258  bool GetCoin(const COutPoint &outpoint, Coin &coin) const override;
259  bool HaveCoin(const COutPoint &outpoint) const override;
260  uint256 GetBestBlock() const override;
261  void SetBestBlock(const uint256 &hashBlock);
262  bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) override;
263  CCoinsViewCursor* Cursor() const override {
264  throw std::logic_error("CCoinsViewCache cursor iteration not supported.");
265  }
266 
272  bool HaveCoinInCache(const COutPoint &outpoint) const;
273 
284  const Coin& AccessCoin(const COutPoint &output) const;
285 
290  void AddCoin(const COutPoint& outpoint, Coin&& coin, bool possible_overwrite);
291 
297  bool SpendCoin(const COutPoint &outpoint, Coin* moveto = nullptr);
298 
304  bool Flush();
305 
310  void Uncache(const COutPoint &outpoint);
311 
313  unsigned int GetCacheSize() const;
314 
316  size_t DynamicMemoryUsage() const;
317 
319  bool HaveInputs(const CTransaction& tx) const;
320 
326  void ReallocateCache();
327 
328 private:
333  CCoinsMap::iterator FetchCoin(const COutPoint &outpoint) const;
334 };
335 
340 // TODO: pass in a boolean to limit these possible overwrites to known
341 // (pre-BIP34) cases.
342 void AddCoins(CCoinsViewCache& cache, const CTransaction& tx, int nHeight, bool check = false);
343 
348 const Coin& AccessByTxid(const CCoinsViewCache& cache, const uint256& txid);
349 
358 {
359 public:
361 
362  void AddReadErrCallback(std::function<void()> f) {
363  m_err_callbacks.emplace_back(std::move(f));
364  }
365 
366  bool GetCoin(const COutPoint &outpoint, Coin &coin) const override;
367 
368 private:
370  std::vector<std::function<void()>> m_err_callbacks;
371 
372 };
373 
374 #endif // BITCOIN_COINS_H
#define VARINT(obj)
Definition: serialize.h:479
bool IsSpent() const
Definition: coins.h:76
void SetNull()
Definition: transaction.h:143
bool IsCoinBase() const
Definition: coins.h:55
A Coin in one level of the coins database caching hierarchy.
Definition: coins.h:127
CScript scriptPubKey
Definition: transaction.h:132
Flags
Definition: coins.h:132
A UTXO entry.
Definition: coins.h:30
static size_t DynamicUsage(const int8_t &v)
Dynamic memory usage for built-in types is zero.
Definition: memusage.h:29
std::vector< std::function< void()> > m_err_callbacks
A list of callbacks to execute upon leveldb read error.
Definition: coins.h:370
void AddReadErrCallback(std::function< void()> f)
Definition: coins.h:362
CTxOut out
unspent transaction output
Definition: coins.h:34
unsigned int fCoinBase
whether containing transaction was a coinbase
Definition: coins.h:37
CCoinsViewCursor * Cursor() const override
Get a cursor to iterate over the whole state.
Definition: coins.h:263
void Serialize(Stream &s) const
Definition: coins.h:60
bool IsNull() const
Definition: transaction.h:149
CCoinsViewErrorCatcher(CCoinsView *view)
Definition: coins.h:360
virtual ~CCoinsView()
As we use CCoinsViews polymorphically, have a virtual destructor.
Definition: coins.h:209
uint256 hashBlock
Definition: coins.h:176
CCoinsCacheEntry()
Definition: coins.h:153
uint32_t nHeight
at which height this containing transaction was included in the active block chain ...
Definition: coins.h:40
CCoinsMap cacheCoins
Definition: coins.h:244
Abstract view on the open txout dataset.
Definition: coins.h:180
const Coin & AccessByTxid(const CCoinsViewCache &cache, const uint256 &txid)
Utility function to find any unspent output with a given txid.
Definition: coins.cpp:259
CCoinsCacheEntry(Coin &&coin_)
Definition: coins.h:154
CCoinsView * base
Definition: coins.h:220
size_t operator()(const COutPoint &id) const noexcept
This must return size_t.
Definition: coins.h:107
std::unordered_map< COutPoint, CCoinsCacheEntry, SaltedOutpointHasher > CCoinsMap
Definition: coins.h:157
const uint64_t k1
Definition: coins.h:89
An output of a transaction.
Definition: transaction.h:128
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:26
uint64_t SipHashUint256Extra(uint64_t k0, uint64_t k1, const uint256 &val, uint32_t extra)
Definition: siphash.cpp:134
Coin()
empty constructor
Definition: coins.h:53
256-bit opaque blob.
Definition: uint256.h:124
uint256 hashBlock
Make mutable so that we can "fill the cache" even from Get-methods declared as "const".
Definition: coins.h:243
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:102
void Unserialize(Stream &s)
Definition: coins.h:68
virtual size_t EstimateSize() const
Estimate database size (0 if not implemented)
Definition: coins.h:212
virtual ~CCoinsViewCursor()
Definition: coins.h:164
size_t DynamicMemoryUsage() const
Definition: coins.h:80
size_t cachedCoinsUsage
Definition: coins.h:247
void Clear()
Definition: coins.h:46
CCoinsViewCursor(const uint256 &hashBlockIn)
Definition: coins.h:163
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:259
CCoinsView backed by another CCoinsView.
Definition: coins.h:217
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:236
const uint256 & GetBestBlock() const
Get best block at the time this cursor was created.
Definition: coins.h:174
This is a minimally invasive approach to shutdown on LevelDB read errors from the chainstate...
Definition: coins.h:357
unsigned char flags
Definition: coins.h:130
Coin(const CTxOut &outIn, int nHeightIn, bool fCoinBaseIn)
Definition: coins.h:44
Coin coin
Definition: coins.h:129
Coin(CTxOut &&outIn, int nHeightIn, bool fCoinBaseIn)
construct a Coin from a CTxOut and height/coinbase information.
Definition: coins.h:43
Cursor for iterating over CoinsView state.
Definition: coins.h:160