Raven Core  3.0.0
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 // Copyright (c) 2017-2019 The Raven Core developers
4 // Distributed under the MIT software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
7 #ifndef RAVEN_COINS_H
8 #define RAVEN_COINS_H
9 
10 #include "primitives/transaction.h"
11 #include "compressor.h"
12 #include "core_memusage.h"
13 #include "hash.h"
14 #include "memusage.h"
15 #include "serialize.h"
16 #include "uint256.h"
17 
18 #include <assert.h>
19 #include <stdint.h>
20 
21 #include <unordered_map>
22 #include <assets/assets.h>
23 #include <assets/assetdb.h>
24 
32 class Coin
33 {
34 public:
37 
39  unsigned int fCoinBase : 1;
40 
42  uint32_t nHeight : 31;
43 
45  Coin(CTxOut&& outIn, int nHeightIn, bool fCoinBaseIn) : out(std::move(outIn)), fCoinBase(fCoinBaseIn), nHeight(nHeightIn) {}
46  Coin(const CTxOut& outIn, int nHeightIn, bool fCoinBaseIn) : out(outIn), fCoinBase(fCoinBaseIn),nHeight(nHeightIn) {}
47 
48  void Clear() {
49  out.SetNull();
50  fCoinBase = false;
51  nHeight = 0;
52  }
53 
55  Coin() : fCoinBase(false), nHeight(0) { }
56 
57  bool IsCoinBase() const {
58  return fCoinBase;
59  }
60 
61  template<typename Stream>
62  void Serialize(Stream &s) const {
63  assert(!IsSpent());
64  uint32_t code = nHeight * 2 + fCoinBase;
65  ::Serialize(s, VARINT(code));
67  }
68 
69  template<typename Stream>
70  void Unserialize(Stream &s) {
71  uint32_t code = 0;
72  ::Unserialize(s, VARINT(code));
73  nHeight = code >> 1;
74  fCoinBase = code & 1;
76  }
77 
78  bool IsSpent() const {
79  return out.IsNull();
80  }
81 
82  bool IsAsset() const {
83  return out.scriptPubKey.IsAssetScript();
84  }
85 
86  size_t DynamicMemoryUsage() const {
87  return memusage::DynamicUsage(out.scriptPubKey);
88  }
89 };
90 
92 {
93 private:
95  const uint64_t k0, k1;
96 
97 public:
99 
105  size_t operator()(const COutPoint& id) const {
106  return SipHashUint256Extra(k0, k1, id.hash, id.n);
107  }
108 };
109 
111 {
112  Coin coin; // The actual cached data.
113  unsigned char flags;
114 
115  enum Flags {
116  DIRTY = (1 << 0), // This cache entry is potentially different from the version in the parent view.
117  FRESH = (1 << 1), // The parent view does not have this entry (or it is pruned).
118  /* Note that FRESH is a performance optimization with which we can
119  * erase coins that are fully spent if we know we do not need to
120  * flush the changes to the parent cache. It is always safe to
121  * not mark FRESH if that condition is not guaranteed.
122  */
123  };
124 
125  CCoinsCacheEntry() : flags(0) {}
126  explicit CCoinsCacheEntry(Coin&& coin_) : coin(std::move(coin_)), flags(0) {}
127 };
128 
129 typedef std::unordered_map<COutPoint, CCoinsCacheEntry, SaltedOutpointHasher> CCoinsMap;
130 
133 {
134 public:
135  CCoinsViewCursor(const uint256 &hashBlockIn): hashBlock(hashBlockIn) {}
136  virtual ~CCoinsViewCursor() {}
137 
138  virtual bool GetKey(COutPoint &key) const = 0;
139  virtual bool GetValue(Coin &coin) const = 0;
140  virtual unsigned int GetValueSize() const = 0;
141 
142  virtual bool Valid() const = 0;
143  virtual void Next() = 0;
144 
146  const uint256 &GetBestBlock() const { return hashBlock; }
147 private:
149 };
150 
153 {
154 public:
159  virtual bool GetCoin(const COutPoint &outpoint, Coin &coin) const;
160 
162  virtual bool HaveCoin(const COutPoint &outpoint) const;
163 
165  virtual uint256 GetBestBlock() const;
166 
171  virtual std::vector<uint256> GetHeadBlocks() const;
172 
175  virtual bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock);
176 
178  virtual CCoinsViewCursor *Cursor() const;
179 
181  virtual ~CCoinsView() {}
182 
184  virtual size_t EstimateSize() const { return 0; }
185 };
186 
187 
190 {
191 protected:
193 
194 public:
195  CCoinsViewBacked(CCoinsView *viewIn);
196  bool GetCoin(const COutPoint &outpoint, Coin &coin) const override;
197  bool HaveCoin(const COutPoint &outpoint) const override;
198  uint256 GetBestBlock() const override;
199  std::vector<uint256> GetHeadBlocks() const override;
200  void SetBackend(CCoinsView &viewIn);
201  bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) override;
202  CCoinsViewCursor *Cursor() const override;
203  size_t EstimateSize() const override;
204 };
205 
206 
209 {
210 protected:
216  mutable CCoinsMap cacheCoins;
217 
218  /* Cached dynamic memory usage for the inner Coin objects. */
219  mutable size_t cachedCoinsUsage;
220 
221 public:
222  CCoinsViewCache(CCoinsView *baseIn);
223 
227  CCoinsViewCache(const CCoinsViewCache &) = delete;
228 
229  // Standard CCoinsView methods
230  bool GetCoin(const COutPoint &outpoint, Coin &coin) const override;
231  bool HaveCoin(const COutPoint &outpoint) const override;
232  uint256 GetBestBlock() const override;
233  void SetBestBlock(const uint256 &hashBlock);
234  bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) override;
235  CCoinsViewCursor* Cursor() const override {
236  throw std::logic_error("CCoinsViewCache cursor iteration not supported.");
237  }
238 
244  bool HaveCoinInCache(const COutPoint &outpoint) const;
245 
256  const Coin& AccessCoin(const COutPoint &output) const;
257 
262  void AddCoin(const COutPoint& outpoint, Coin&& coin, bool potential_overwrite);
263 
269  bool SpendCoin(const COutPoint &outpoint, Coin* moveto = nullptr, CAssetsCache* assetsCache = nullptr);
270 
276  bool Flush();
277 
282  void Uncache(const COutPoint &outpoint);
283 
285  unsigned int GetCacheSize() const;
286 
288  size_t DynamicMemoryUsage() const;
289 
298  CAmount GetValueIn(const CTransaction& tx) const;
299 
301  bool HaveInputs(const CTransaction& tx) const;
302 
303 private:
304  CCoinsMap::iterator FetchCoin(const COutPoint &outpoint) const;
305 };
306 
308 // When check is false, this assumes that overwrites are only possible for coinbase transactions.
309 // When check is true, the underlying view may be queried to determine whether an addition is
310 // an overwrite.
311 // TODO: pass in a boolean to limit these possible overwrites to known
312 // (pre-BIP34) cases.
313 void AddCoins(CCoinsViewCache& cache, const CTransaction& tx, int nHeight, uint256 blockHash, bool check = false, CAssetsCache* assetsCache = nullptr, std::pair<std::string, CBlockAssetUndo>* undoAssetData = nullptr);
314 
316 // This function can be quite expensive because in the event of a transaction
317 // which is not found in the cache, it can cause up to MAX_OUTPUTS_PER_BLOCK
318 // lookups to database, so it should be used with care.
319 const Coin& AccessByTxid(const CCoinsViewCache& cache, const uint256& txid);
320 
321 #endif // RAVEN_COINS_H
#define VARINT(obj)
Definition: serialize.h:367
bool IsSpent() const
Definition: coins.h:78
void SetNull()
Definition: transaction.h:158
bool IsCoinBase() const
Definition: coins.h:57
Definition: coins.h:110
CScript scriptPubKey
Definition: transaction.h:141
Flags
Definition: coins.h:115
A UTXO entry.
Definition: coins.h:32
wrapper for CTxOut that provides a more compact serialization
Definition: compressor.h:94
CTxOut out
unspent transaction output
Definition: coins.h:36
unsigned int fCoinBase
whether containing transaction was a coinbase
Definition: coins.h:39
CCoinsViewCursor * Cursor() const override
Get a cursor to iterate over the whole state.
Definition: coins.h:235
void Serialize(Stream &s) const
Definition: coins.h:62
bool IsNull() const
Definition: transaction.h:164
virtual ~CCoinsView()
As we use CCoinsViews polymorphically, have a virtual destructor.
Definition: coins.h:181
uint256 hashBlock
Definition: coins.h:148
int64_t CAmount
Amount in corbies (Can be negative)
Definition: amount.h:13
CCoinsCacheEntry()
Definition: coins.h:125
uint32_t nHeight
at which height this containing transaction was included in the active block chain ...
Definition: coins.h:42
CCoinsMap cacheCoins
Definition: coins.h:216
Abstract view on the open txout dataset.
Definition: coins.h:152
const Coin & AccessByTxid(const CCoinsViewCache &cache, const uint256 &txid)
Utility function to find any unspent output with a given txid.
Definition: coins.cpp:522
CCoinsCacheEntry(Coin &&coin_)
Definition: coins.h:126
CCoinsView * base
Definition: coins.h:192
std::unordered_map< COutPoint, CCoinsCacheEntry, SaltedOutpointHasher > CCoinsMap
Definition: coins.h:129
const uint64_t k1
Definition: coins.h:95
An output of a transaction.
Definition: transaction.h:137
size_t operator()(const COutPoint &id) const
This must return size_t.
Definition: coins.h:105
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:22
Coin()
empty constructor
Definition: coins.h:55
void AddCoins(CCoinsViewCache &cache, const CTransaction &tx, int nHeight, uint256 blockHash, bool check=false, CAssetsCache *assetsCache=nullptr, std::pair< std::string, CBlockAssetUndo > *undoAssetData=nullptr)
Utility function to add all of a transaction&#39;s outputs to a cache.
Definition: coins.cpp:97
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:215
void Unserialize(Stream &s)
Definition: coins.h:70
virtual size_t EstimateSize() const
Estimate database size (0 if not implemented)
Definition: coins.h:184
virtual ~CCoinsViewCursor()
Definition: coins.h:136
size_t DynamicMemoryUsage() const
Definition: coins.h:86
size_t cachedCoinsUsage
Definition: coins.h:219
void Clear()
Definition: coins.h:48
CCoinsViewCursor(const uint256 &hashBlockIn)
Definition: coins.h:135
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:270
CCoinsView backed by another CCoinsView.
Definition: coins.h:189
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:208
bool IsAsset() const
Definition: coins.h:82
const uint256 & GetBestBlock() const
Get best block at the time this cursor was created.
Definition: coins.h:146
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:48
unsigned char flags
Definition: coins.h:113
Coin(const CTxOut &outIn, int nHeightIn, bool fCoinBaseIn)
Definition: coins.h:46
uint64_t SipHashUint256Extra(uint64_t k0, uint64_t k1, const uint256 &val, uint32_t extra)
Definition: hash.cpp:213
Coin coin
Definition: coins.h:112
bool IsAssetScript(int &nType, bool &fIsOwner, int &nStartingIndex) const
Definition: script.cpp:245
Coin(CTxOut &&outIn, int nHeightIn, bool fCoinBaseIn)
construct a Coin from a CTxOut and height/coinbase information.
Definition: coins.h:45
Cursor for iterating over CoinsView state.
Definition: coins.h:132