Changeset View
Changeset View
Standalone View
Standalone View
src/coins.h
// Copyright (c) 2009-2010 Satoshi Nakamoto | // Copyright (c) 2009-2010 Satoshi Nakamoto | ||||
// Copyright (c) 2009-2016 The Bitcoin Core developers | // Copyright (c) 2009-2016 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#ifndef BITCOIN_COINS_H | #ifndef BITCOIN_COINS_H | ||||
#define BITCOIN_COINS_H | #define BITCOIN_COINS_H | ||||
#include "compressor.h" | #include "compressor.h" | ||||
#include "core_memusage.h" | #include "core_memusage.h" | ||||
#include "hash.h" | #include "hash.h" | ||||
#include "memusage.h" | #include "memusage.h" | ||||
#include "serialize.h" | #include "serialize.h" | ||||
#include "uint256.h" | #include "uint256.h" | ||||
#include "utxocommit.h" | |||||
#include <cassert> | #include <cassert> | ||||
#include <cstdint> | #include <cstdint> | ||||
#include <unordered_map> | #include <unordered_map> | ||||
class CUtxoCommit; | |||||
/** | /** | ||||
* A UTXO entry. | * A UTXO entry. | ||||
* | * | ||||
* Serialized format: | * Serialized format: | ||||
* - VARINT((coinbase ? 1 : 0) | (height << 1)) | * - VARINT((coinbase ? 1 : 0) | (height << 1)) | ||||
* - the non-spent CTxOut (via CTxOutCompressor) | * - the non-spent CTxOut (via CTxOutCompressor) | ||||
*/ | */ | ||||
class Coin { | class Coin { | ||||
▲ Show 20 Lines • Show All 130 Lines • ▼ Show 20 Lines | public: | ||||
//! 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<uint256> 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 uint256 &hashBlock, | ||||
CUtxoCommit *commitDelta); | |||||
//! 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; } | ||||
//! Returns the UTXO-commitment or nullptr if it is not yet up-to-date | |||||
virtual CUtxoCommit *GetCommitment() const; | |||||
}; | }; | ||||
/** 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; | uint256 GetBestBlock() const override; | ||||
std::vector<uint256> GetHeadBlocks() const override; | std::vector<uint256> GetHeadBlocks() const override; | ||||
void SetBackend(CCoinsView &viewIn); | void SetBackend(CCoinsView &viewIn); | ||||
bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) override; | bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock, | ||||
CUtxoCommit *commitDelta) override; | |||||
CCoinsViewCursor *Cursor() const override; | CCoinsViewCursor *Cursor() const override; | ||||
size_t EstimateSize() const override; | size_t EstimateSize() const override; | ||||
CUtxoCommit *GetCommitment() 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 uint256 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; | ||||
CUtxoCommit *cacheUtxoCommitDelta; | |||||
public: | public: | ||||
CCoinsViewCache(CCoinsView *baseIn); | CCoinsViewCache(CCoinsView *baseIn); | ||||
virtual ~CCoinsViewCache(); | |||||
// 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; | uint256 GetBestBlock() const override; | ||||
void SetBestBlock(const uint256 &hashBlock); | void SetBestBlock(const uint256 &hashBlock); | ||||
bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) override; | bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock, | ||||
CUtxoCommit *commitDelta) override; | |||||
/** | /** | ||||
* 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 | ||||
* CCoinsView are made. | * CCoinsView are made. | ||||
*/ | */ | ||||
bool HaveCoinInCache(const COutPoint &outpoint) const; | bool HaveCoinInCache(const COutPoint &outpoint) const; | ||||
Show All 22 Lines | public: | ||||
* Push the modifications applied to this cache to its base. | * Push the modifications applied to this cache to its base. | ||||
* Failure to call this method before destruction will cause the changes to | * Failure to call this method before destruction will cause the changes to | ||||
* be forgotten. If false is returned, the state of this cache (and its | * be forgotten. If false is returned, the state of this cache (and its | ||||
* backing view) will be undefined. | * backing view) will be undefined. | ||||
*/ | */ | ||||
bool Flush(); | bool Flush(); | ||||
/** | /** | ||||
* Returns the UTXO commitment from the current full set | |||||
* This is the combination of the backed commitment and the commitment delta | |||||
*/ | |||||
CUtxoCommit *GetCommitment() const; | |||||
//! Start maintaining the commitment for this View | |||||
void CalculateCommitment(); | |||||
//! Returns whether this cache is maintaining a commitment delta | |||||
bool HasCommitmentDelta() const; | |||||
/** | |||||
* Removes the UTXO with the given outpoint from the cache, if it is not | * Removes the UTXO with the given outpoint from the cache, if it is not | ||||
* modified. | * modified. | ||||
*/ | */ | ||||
void Uncache(const COutPoint &outpoint); | void Uncache(const COutPoint &outpoint); | ||||
//! Calculate the size of the cache (in number of transaction outputs) | //! Calculate the size of the cache (in number of transaction outputs) | ||||
unsigned int GetCacheSize() const; | unsigned int GetCacheSize() const; | ||||
▲ Show 20 Lines • Show All 51 Lines • Show Last 20 Lines |