Bitcoin Core  21.99.0
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 <memusage.h>
12 #include <primitives/transaction.h>
13 #include <serialize.h>
14 #include <uint256.h>
15 #include <util/hasher.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 
101 {
102  Coin coin; // The actual cached data.
103  unsigned char flags;
104 
105  enum Flags {
113  DIRTY = (1 << 0),
123  FRESH = (1 << 1),
124  };
125 
126  CCoinsCacheEntry() : flags(0) {}
127  explicit CCoinsCacheEntry(Coin&& coin_) : coin(std::move(coin_)), flags(0) {}
128 };
129 
130 typedef std::unordered_map<COutPoint, CCoinsCacheEntry, SaltedOutpointHasher> CCoinsMap;
131 
134 {
135 public:
136  CCoinsViewCursor(const uint256 &hashBlockIn): hashBlock(hashBlockIn) {}
137  virtual ~CCoinsViewCursor() {}
138 
139  virtual bool GetKey(COutPoint &key) const = 0;
140  virtual bool GetValue(Coin &coin) const = 0;
141  virtual unsigned int GetValueSize() const = 0;
142 
143  virtual bool Valid() const = 0;
144  virtual void Next() = 0;
145 
147  const uint256 &GetBestBlock() const { return hashBlock; }
148 private:
150 };
151 
154 {
155 public:
160  virtual bool GetCoin(const COutPoint &outpoint, Coin &coin) const;
161 
163  virtual bool HaveCoin(const COutPoint &outpoint) const;
164 
166  virtual uint256 GetBestBlock() const;
167 
172  virtual std::vector<uint256> GetHeadBlocks() const;
173 
176  virtual bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock);
177 
179  virtual CCoinsViewCursor *Cursor() const;
180 
182  virtual ~CCoinsView() {}
183 
185  virtual size_t EstimateSize() const { return 0; }
186 };
187 
188 
191 {
192 protected:
194 
195 public:
196  CCoinsViewBacked(CCoinsView *viewIn);
197  bool GetCoin(const COutPoint &outpoint, Coin &coin) const override;
198  bool HaveCoin(const COutPoint &outpoint) const override;
199  uint256 GetBestBlock() const override;
200  std::vector<uint256> GetHeadBlocks() const override;
201  void SetBackend(CCoinsView &viewIn);
202  bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) override;
203  CCoinsViewCursor *Cursor() const override;
204  size_t EstimateSize() const override;
205 };
206 
207 
210 {
211 protected:
217  mutable CCoinsMap cacheCoins;
218 
219  /* Cached dynamic memory usage for the inner Coin objects. */
220  mutable size_t cachedCoinsUsage;
221 
222 public:
223  CCoinsViewCache(CCoinsView *baseIn);
224 
228  CCoinsViewCache(const CCoinsViewCache &) = delete;
229 
230  // Standard CCoinsView methods
231  bool GetCoin(const COutPoint &outpoint, Coin &coin) const override;
232  bool HaveCoin(const COutPoint &outpoint) const override;
233  uint256 GetBestBlock() const override;
234  void SetBestBlock(const uint256 &hashBlock);
235  bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) override;
236  CCoinsViewCursor* Cursor() const override {
237  throw std::logic_error("CCoinsViewCache cursor iteration not supported.");
238  }
239 
245  bool HaveCoinInCache(const COutPoint &outpoint) const;
246 
257  const Coin& AccessCoin(const COutPoint &output) const;
258 
263  void AddCoin(const COutPoint& outpoint, Coin&& coin, bool possible_overwrite);
264 
270  bool SpendCoin(const COutPoint &outpoint, Coin* moveto = nullptr);
271 
277  bool Flush();
278 
283  void Uncache(const COutPoint &outpoint);
284 
286  unsigned int GetCacheSize() const;
287 
289  size_t DynamicMemoryUsage() const;
290 
292  bool HaveInputs(const CTransaction& tx) const;
293 
299  void ReallocateCache();
300 
301 private:
306  CCoinsMap::iterator FetchCoin(const COutPoint &outpoint) const;
307 };
308 
313 // TODO: pass in a boolean to limit these possible overwrites to known
314 // (pre-BIP34) cases.
315 void AddCoins(CCoinsViewCache& cache, const CTransaction& tx, int nHeight, bool check = false);
316 
321 const Coin& AccessByTxid(const CCoinsViewCache& cache, const uint256& txid);
322 
331 {
332 public:
334 
335  void AddReadErrCallback(std::function<void()> f) {
336  m_err_callbacks.emplace_back(std::move(f));
337  }
338 
339  bool GetCoin(const COutPoint &outpoint, Coin &coin) const override;
340 
341 private:
343  std::vector<std::function<void()>> m_err_callbacks;
344 
345 };
346 
347 #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:100
assert(!tx.IsCoinBase())
CScript scriptPubKey
Definition: transaction.h:132
Flags
Definition: coins.h:105
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:343
void AddReadErrCallback(std::function< void()> f)
Definition: coins.h:335
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:236
void Serialize(Stream &s) const
Definition: coins.h:60
bool IsNull() const
Definition: transaction.h:149
CCoinsViewErrorCatcher(CCoinsView *view)
Definition: coins.h:333
virtual ~CCoinsView()
As we use CCoinsViews polymorphically, have a virtual destructor.
Definition: coins.h:182
uint256 hashBlock
Definition: coins.h:149
CCoinsCacheEntry()
Definition: coins.h:126
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:217
Abstract view on the open txout dataset.
Definition: coins.h:153
const Coin & AccessByTxid(const CCoinsViewCache &cache, const uint256 &txid)
Utility function to find any unspent output with a given txid.
Definition: coins.cpp:257
CCoinsCacheEntry(Coin &&coin_)
Definition: coins.h:127
CCoinsView * base
Definition: coins.h:193
std::unordered_map< COutPoint, CCoinsCacheEntry, SaltedOutpointHasher > CCoinsMap
Definition: coins.h:130
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
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:216
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:100
void Unserialize(Stream &s)
Definition: coins.h:68
virtual size_t EstimateSize() const
Estimate database size (0 if not implemented)
Definition: coins.h:185
virtual ~CCoinsViewCursor()
Definition: coins.h:137
size_t DynamicMemoryUsage() const
Definition: coins.h:80
size_t cachedCoinsUsage
Definition: coins.h:220
void Clear()
Definition: coins.h:46
CCoinsViewCursor(const uint256 &hashBlockIn)
Definition: coins.h:136
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:190
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:209
const uint256 & GetBestBlock() const
Get best block at the time this cursor was created.
Definition: coins.h:147
This is a minimally invasive approach to shutdown on LevelDB read errors from the chainstate...
Definition: coins.h:330
unsigned char flags
Definition: coins.h:103
Coin(const CTxOut &outIn, int nHeightIn, bool fCoinBaseIn)
Definition: coins.h:44
Coin coin
Definition: coins.h:102
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:133