Changeset View
Changeset View
Standalone View
Standalone View
src/coins.h
Show First 20 Lines • Show All 112 Lines • ▼ Show 20 Lines | |||||
}; | }; | ||||
typedef std::unordered_map<COutPoint, CCoinsCacheEntry, SaltedOutpointHasher> | typedef std::unordered_map<COutPoint, CCoinsCacheEntry, SaltedOutpointHasher> | ||||
CCoinsMap; | CCoinsMap; | ||||
/** Cursor for iterating over CoinsView state */ | /** Cursor for iterating over CoinsView state */ | ||||
class CCoinsViewCursor { | class CCoinsViewCursor { | ||||
public: | public: | ||||
CCoinsViewCursor(const uint256 &hashBlockIn) : hashBlock(hashBlockIn) {} | CCoinsViewCursor(const BlockHash &hashBlockIn) : hashBlock(hashBlockIn) {} | ||||
virtual ~CCoinsViewCursor() {} | virtual ~CCoinsViewCursor() {} | ||||
virtual bool GetKey(COutPoint &key) const = 0; | virtual bool GetKey(COutPoint &key) const = 0; | ||||
virtual bool GetValue(Coin &coin) const = 0; | virtual bool GetValue(Coin &coin) const = 0; | ||||
virtual unsigned int GetValueSize() const = 0; | virtual unsigned int GetValueSize() const = 0; | ||||
virtual bool Valid() const = 0; | virtual bool Valid() const = 0; | ||||
virtual void Next() = 0; | virtual void Next() = 0; | ||||
//! Get best block at the time this cursor was created | //! Get best block at the time this cursor was created | ||||
const uint256 &GetBestBlock() const { return hashBlock; } | const BlockHash &GetBestBlock() const { return hashBlock; } | ||||
private: | private: | ||||
uint256 hashBlock; | BlockHash hashBlock; | ||||
}; | }; | ||||
/** Abstract view on the open txout dataset. */ | /** Abstract view on the open txout dataset. */ | ||||
class CCoinsView { | class CCoinsView { | ||||
public: | public: | ||||
/** | /** | ||||
* Retrieve the Coin (unspent transaction output) for a given outpoint. | * Retrieve the Coin (unspent transaction output) for a given outpoint. | ||||
* Returns true only when an unspent coin was found, which is returned in | * Returns true only when an unspent coin was found, which is returned in | ||||
* coin. When false is returned, coin's value is unspecified. | * coin. When false is returned, coin's value is unspecified. | ||||
*/ | */ | ||||
virtual bool GetCoin(const COutPoint &outpoint, Coin &coin) const; | virtual bool GetCoin(const COutPoint &outpoint, Coin &coin) const; | ||||
//! Just check whether a given outpoint is unspent. | //! Just check whether a given outpoint is unspent. | ||||
virtual bool HaveCoin(const COutPoint &outpoint) const; | virtual bool HaveCoin(const COutPoint &outpoint) const; | ||||
//! Retrieve the block hash whose state this CCoinsView currently represents | //! Retrieve the block hash whose state this CCoinsView currently represents | ||||
virtual uint256 GetBestBlock() const; | virtual BlockHash GetBestBlock() const; | ||||
//! Retrieve the range of blocks that may have been only partially written. | //! Retrieve the range of blocks that may have been only partially written. | ||||
//! If the database is in a consistent state, the result is the empty | //! If the database is in a consistent state, the result is the empty | ||||
//! vector. | //! vector. | ||||
//! Otherwise, a two-element vector is returned consisting of the new and | //! Otherwise, a two-element vector is returned consisting of the new and | ||||
//! the old block hash, in that order. | //! the old block hash, in that order. | ||||
virtual std::vector<uint256> GetHeadBlocks() const; | virtual std::vector<BlockHash> GetHeadBlocks() const; | ||||
//! Do a bulk modification (multiple Coin changes + BestBlock change). | //! Do a bulk modification (multiple Coin changes + BestBlock change). | ||||
//! The passed mapCoins can be modified. | //! The passed mapCoins can be modified. | ||||
virtual bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock); | virtual bool BatchWrite(CCoinsMap &mapCoins, const BlockHash &hashBlock); | ||||
//! Get a cursor to iterate over the whole state | //! Get a cursor to iterate over the whole state | ||||
virtual CCoinsViewCursor *Cursor() const; | virtual CCoinsViewCursor *Cursor() const; | ||||
//! As we use CCoinsViews polymorphically, have a virtual destructor | //! As we use CCoinsViews polymorphically, have a virtual destructor | ||||
virtual ~CCoinsView() {} | virtual ~CCoinsView() {} | ||||
//! Estimate database size (0 if not implemented) | //! Estimate database size (0 if not implemented) | ||||
virtual size_t EstimateSize() const { return 0; } | virtual size_t EstimateSize() const { return 0; } | ||||
}; | }; | ||||
/** CCoinsView backed by another CCoinsView */ | /** CCoinsView backed by another CCoinsView */ | ||||
class CCoinsViewBacked : public CCoinsView { | class CCoinsViewBacked : public CCoinsView { | ||||
protected: | protected: | ||||
CCoinsView *base; | CCoinsView *base; | ||||
public: | public: | ||||
CCoinsViewBacked(CCoinsView *viewIn); | CCoinsViewBacked(CCoinsView *viewIn); | ||||
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override; | bool GetCoin(const COutPoint &outpoint, Coin &coin) const override; | ||||
bool HaveCoin(const COutPoint &outpoint) const override; | bool HaveCoin(const COutPoint &outpoint) const override; | ||||
uint256 GetBestBlock() const override; | BlockHash GetBestBlock() const override; | ||||
std::vector<uint256> GetHeadBlocks() const override; | std::vector<BlockHash> GetHeadBlocks() const override; | ||||
void SetBackend(CCoinsView &viewIn); | void SetBackend(CCoinsView &viewIn); | ||||
bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) override; | bool BatchWrite(CCoinsMap &mapCoins, const BlockHash &hashBlock) override; | ||||
CCoinsViewCursor *Cursor() const override; | CCoinsViewCursor *Cursor() const override; | ||||
size_t EstimateSize() const override; | size_t EstimateSize() const override; | ||||
}; | }; | ||||
/** | /** | ||||
* CCoinsView that adds a memory cache for transactions to another CCoinsView | * CCoinsView that adds a memory cache for transactions to another CCoinsView | ||||
*/ | */ | ||||
class CCoinsViewCache : public CCoinsViewBacked { | class CCoinsViewCache : public CCoinsViewBacked { | ||||
protected: | protected: | ||||
/** | /** | ||||
* Make mutable so that we can "fill the cache" even from Get-methods | * Make mutable so that we can "fill the cache" even from Get-methods | ||||
* declared as "const". | * declared as "const". | ||||
*/ | */ | ||||
mutable uint256 hashBlock; | mutable BlockHash hashBlock; | ||||
mutable CCoinsMap cacheCoins; | mutable CCoinsMap cacheCoins; | ||||
/* Cached dynamic memory usage for the inner Coin objects. */ | /* Cached dynamic memory usage for the inner Coin objects. */ | ||||
mutable size_t cachedCoinsUsage; | mutable size_t cachedCoinsUsage; | ||||
public: | public: | ||||
CCoinsViewCache(CCoinsView *baseIn); | CCoinsViewCache(CCoinsView *baseIn); | ||||
/** | /** | ||||
* By deleting the copy constructor, we prevent accidentally using it when | * By deleting the copy constructor, we prevent accidentally using it when | ||||
* one intends to create a cache on top of a base cache. | * one intends to create a cache on top of a base cache. | ||||
*/ | */ | ||||
CCoinsViewCache(const CCoinsViewCache &) = delete; | CCoinsViewCache(const CCoinsViewCache &) = delete; | ||||
// Standard CCoinsView methods | // Standard CCoinsView methods | ||||
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override; | bool GetCoin(const COutPoint &outpoint, Coin &coin) const override; | ||||
bool HaveCoin(const COutPoint &outpoint) const override; | bool HaveCoin(const COutPoint &outpoint) const override; | ||||
uint256 GetBestBlock() const override; | BlockHash GetBestBlock() const override; | ||||
void SetBestBlock(const uint256 &hashBlock); | void SetBestBlock(const BlockHash &hashBlock); | ||||
bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) override; | bool BatchWrite(CCoinsMap &mapCoins, const BlockHash &hashBlock) override; | ||||
CCoinsViewCursor *Cursor() const override { | CCoinsViewCursor *Cursor() const override { | ||||
throw std::logic_error( | throw std::logic_error( | ||||
"CCoinsViewCache cursor iteration not supported."); | "CCoinsViewCache cursor iteration not supported."); | ||||
} | } | ||||
/** | /** | ||||
* Check if we have the given utxo already loaded in this cache. | * Check if we have the given utxo already loaded in this cache. | ||||
* The semantics are the same as HaveCoin(), but no calls to the backing | * The semantics are the same as HaveCoin(), but no calls to the backing | ||||
▲ Show 20 Lines • Show All 95 Lines • Show Last 20 Lines |