Bitcoin ABC  0.22.13
P2P Digital Currency
coins.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 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 <crypto/siphash.h>
11 #include <memusage.h>
12 #include <primitives/blockhash.h>
13 #include <serialize.h>
14 
15 #include <cassert>
16 #include <cstdint>
17 #include <functional>
18 #include <unordered_map>
19 
27 class Coin {
30 
34 
35 public:
37  Coin() : nHeightAndIsCoinBase(0) {}
38 
40  Coin(CTxOut outIn, uint32_t nHeightIn, bool IsCoinbase)
41  : out(std::move(outIn)),
42  nHeightAndIsCoinBase((nHeightIn << 1) | IsCoinbase) {}
43 
44  uint32_t GetHeight() const { return nHeightAndIsCoinBase >> 1; }
45  bool IsCoinBase() const { return nHeightAndIsCoinBase & 0x01; }
46  bool IsSpent() const { return out.IsNull(); }
47 
48  CTxOut &GetTxOut() { return out; }
49  const CTxOut &GetTxOut() const { return out; }
50 
51  void Clear() {
52  out.SetNull();
53  nHeightAndIsCoinBase = 0;
54  }
55 
56  template <typename Stream> void Serialize(Stream &s) const {
57  assert(!IsSpent());
58  ::Serialize(s, VARINT(nHeightAndIsCoinBase));
59  ::Serialize(s, Using<TxOutCompression>(out));
60  }
61 
62  template <typename Stream> void Unserialize(Stream &s) {
63  ::Unserialize(s, VARINT(nHeightAndIsCoinBase));
64  ::Unserialize(s, Using<TxOutCompression>(out));
65  }
66 
67  size_t DynamicMemoryUsage() const {
69  }
70 };
71 
73 private:
75  const uint64_t k0, k1;
76 
77 public:
79 
97  size_t operator()(const COutPoint &outpoint) const noexcept {
98  return SipHashUint256Extra(k0, k1, outpoint.GetTxId(), outpoint.GetN());
99  }
100 };
101 
121  // The actual cached data.
123  uint8_t flags;
124 
125  enum Flags {
133  DIRTY = (1 << 0),
143  FRESH = (1 << 1),
144  };
145 
146  CCoinsCacheEntry() : flags(0) {}
147  explicit CCoinsCacheEntry(Coin coinIn)
148  : coin(std::move(coinIn)), flags(0) {}
149 };
150 
151 typedef std::unordered_map<COutPoint, CCoinsCacheEntry, SaltedOutpointHasher>
153 
156 public:
157  CCoinsViewCursor(const BlockHash &hashBlockIn) : hashBlock(hashBlockIn) {}
158  virtual ~CCoinsViewCursor() {}
159 
160  virtual bool GetKey(COutPoint &key) const = 0;
161  virtual bool GetValue(Coin &coin) const = 0;
162  virtual unsigned int GetValueSize() const = 0;
163 
164  virtual bool Valid() const = 0;
165  virtual void Next() = 0;
166 
168  const BlockHash &GetBestBlock() const { return hashBlock; }
169 
170 private:
172 };
173 
175 class CCoinsView {
176 public:
182  virtual bool GetCoin(const COutPoint &outpoint, Coin &coin) const;
183 
185  virtual bool HaveCoin(const COutPoint &outpoint) const;
186 
188  virtual BlockHash GetBestBlock() const;
189 
195  virtual std::vector<BlockHash> GetHeadBlocks() const;
196 
199  virtual bool BatchWrite(CCoinsMap &mapCoins, const BlockHash &hashBlock);
200 
202  virtual CCoinsViewCursor *Cursor() const;
203 
205  virtual ~CCoinsView() {}
206 
208  virtual size_t EstimateSize() const { return 0; }
209 };
210 
212 class CCoinsViewBacked : public CCoinsView {
213 protected:
215 
216 public:
217  CCoinsViewBacked(CCoinsView *viewIn);
218  bool GetCoin(const COutPoint &outpoint, Coin &coin) const override;
219  bool HaveCoin(const COutPoint &outpoint) const override;
220  BlockHash GetBestBlock() const override;
221  std::vector<BlockHash> GetHeadBlocks() const override;
222  void SetBackend(CCoinsView &viewIn);
223  bool BatchWrite(CCoinsMap &mapCoins, const BlockHash &hashBlock) override;
224  CCoinsViewCursor *Cursor() const override;
225  size_t EstimateSize() const override;
226 };
227 
232 protected:
239 
240  /* Cached dynamic memory usage for the inner Coin objects. */
241  mutable size_t cachedCoinsUsage;
242 
243 public:
244  CCoinsViewCache(CCoinsView *baseIn);
245 
250  CCoinsViewCache(const CCoinsViewCache &) = delete;
251 
252  // Standard CCoinsView methods
253  bool GetCoin(const COutPoint &outpoint, Coin &coin) const override;
254  bool HaveCoin(const COutPoint &outpoint) const override;
255  BlockHash GetBestBlock() const override;
256  void SetBestBlock(const BlockHash &hashBlock);
257  bool BatchWrite(CCoinsMap &mapCoins, const BlockHash &hashBlock) override;
258  CCoinsViewCursor *Cursor() const override {
259  throw std::logic_error(
260  "CCoinsViewCache cursor iteration not supported.");
261  }
262 
268  bool HaveCoinInCache(const COutPoint &outpoint) const;
269 
280  const Coin &AccessCoin(const COutPoint &output) const;
281 
286  void AddCoin(const COutPoint &outpoint, Coin coin, bool possible_overwrite);
287 
293  bool SpendCoin(const COutPoint &outpoint, Coin *moveto = nullptr);
294 
301  bool Flush();
302 
307  void Uncache(const COutPoint &outpoint);
308 
310  unsigned int GetCacheSize() const;
311 
313  size_t DynamicMemoryUsage() const;
314 
323  Amount GetValueIn(const CTransaction &tx) const;
324 
327  bool HaveInputs(const CTransaction &tx) const;
328 
329 private:
334  CCoinsMap::iterator FetchCoin(const COutPoint &outpoint) const;
335 };
336 
341 // TODO: pass in a boolean to limit these possible overwrites to known
342 // (pre-BIP34) cases.
343 void AddCoins(CCoinsViewCache &cache, const CTransaction &tx, int nHeight,
344  bool check = false);
345 
350 const Coin &AccessByTxid(const CCoinsViewCache &cache, const TxId &txid);
351 
361 public:
363  : CCoinsViewBacked(view) {}
364 
365  void AddReadErrCallback(std::function<void()> f) {
366  m_err_callbacks.emplace_back(std::move(f));
367  }
368 
369  bool GetCoin(const COutPoint &outpoint, Coin &coin) const override;
370 
371 private:
375  std::vector<std::function<void()>> m_err_callbacks;
376 };
377 
378 #endif // BITCOIN_COINS_H
#define VARINT(obj)
Definition: serialize.h:608
bool IsSpent() const
Definition: coins.h:46
void SetNull()
Definition: transaction.h:159
bool IsCoinBase() const
Definition: coins.h:45
A Coin in one level of the coins database caching hierarchy.
Definition: coins.h:120
CScript scriptPubKey
Definition: transaction.h:144
Flags
Definition: coins.h:125
uint32_t nHeightAndIsCoinBase
Whether containing transaction was a coinbase and height at which the transaction was included into a...
Definition: coins.h:33
A UTXO entry.
Definition: coins.h:27
static size_t DynamicUsage(const int8_t &v)
Dynamic memory usage for built-in types is zero.
Definition: memusage.h:26
std::vector< std::function< void()> > m_err_callbacks
A list of callbacks to execute upon leveldb read error.
Definition: coins.h:375
unsigned int nHeight
void AddReadErrCallback(std::function< void()> f)
Definition: coins.h:365
CTxOut out
Unspent transaction output.
Definition: coins.h:29
Definition: amount.h:17
Coin(CTxOut outIn, uint32_t nHeightIn, bool IsCoinbase)
Constructor from a CTxOut and height/coinbase information.
Definition: coins.h:40
CCoinsViewCursor(const BlockHash &hashBlockIn)
Definition: coins.h:157
CCoinsViewCursor * Cursor() const override
Get a cursor to iterate over the whole state.
Definition: coins.h:258
void Serialize(Stream &s) const
Definition: coins.h:56
bool IsNull() const
Definition: transaction.h:164
CCoinsViewErrorCatcher(CCoinsView *view)
Definition: coins.h:362
virtual ~CCoinsView()
As we use CCoinsViews polymorphically, have a virtual destructor.
Definition: coins.h:205
CCoinsCacheEntry()
Definition: coins.h:146
CCoinsMap cacheCoins
Definition: coins.h:238
Abstract view on the open txout dataset.
Definition: coins.h:175
CCoinsView * base
Definition: coins.h:214
std::unordered_map< COutPoint, CCoinsCacheEntry, SaltedOutpointHasher > CCoinsMap
Definition: coins.h:152
const uint64_t k1
Definition: coins.h:75
An output of a transaction.
Definition: transaction.h:141
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:22
uint64_t SipHashUint256Extra(uint64_t k0, uint64_t k1, const uint256 &val, uint32_t extra)
Definition: siphash.cpp:138
CCoinsCacheEntry(Coin coinIn)
Definition: coins.h:147
Coin()
Empty constructor.
Definition: coins.h:37
BlockHash hashBlock
Make mutable so that we can "fill the cache" even from Get-methods declared as "const".
Definition: coins.h:237
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:144
void Unserialize(Stream &s)
Definition: coins.h:62
const BlockHash & GetBestBlock() const
Get best block at the time this cursor was created.
Definition: coins.h:168
A BlockHash is a unqiue identifier for a block.
Definition: blockhash.h:13
virtual size_t EstimateSize() const
Estimate database size (0 if not implemented)
Definition: coins.h:208
A TxId is the identifier of a transaction.
Definition: txid.h:14
uint32_t GetHeight() const
Definition: coins.h:44
CTxOut & GetTxOut()
Definition: coins.h:48
virtual ~CCoinsViewCursor()
Definition: coins.h:158
size_t DynamicMemoryUsage() const
Definition: coins.h:67
size_t cachedCoinsUsage
Definition: coins.h:241
uint8_t flags
Definition: coins.h:123
void Clear()
Definition: coins.h:51
const CTxOut & GetTxOut() const
Definition: coins.h:49
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:211
CCoinsView backed by another CCoinsView.
Definition: coins.h:212
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:231
size_t operator()(const COutPoint &outpoint) const noexcept
This must return size_t.
Definition: coins.h:97
This is a minimally invasive approach to shutdown on LevelDB read errors from the chainstate...
Definition: coins.h:360
const Coin & AccessByTxid(const CCoinsViewCache &cache, const TxId &txid)
Utility function to find any unspent output with a given txid.
Definition: coins.cpp:321
Coin coin
Definition: coins.h:122
BlockHash hashBlock
Definition: coins.h:171
Cursor for iterating over CoinsView state.
Definition: coins.h:155