Changeset View
Changeset View
Standalone View
Standalone View
src/validation.h
Show All 17 Lines | |||||
#include <consensus/consensus.h> | #include <consensus/consensus.h> | ||||
#include <disconnectresult.h> | #include <disconnectresult.h> | ||||
#include <flatfile.h> | #include <flatfile.h> | ||||
#include <fs.h> | #include <fs.h> | ||||
#include <protocol.h> // For CMessageHeader::MessageMagic | #include <protocol.h> // For CMessageHeader::MessageMagic | ||||
#include <script/script_error.h> | #include <script/script_error.h> | ||||
#include <script/script_metrics.h> | #include <script/script_metrics.h> | ||||
#include <sync.h> | #include <sync.h> | ||||
#include <txdb.h> | |||||
#include <txmempool.h> // For CTxMemPool::cs | #include <txmempool.h> // For CTxMemPool::cs | ||||
#include <versionbits.h> | #include <versionbits.h> | ||||
#include <algorithm> | #include <algorithm> | ||||
#include <atomic> | #include <atomic> | ||||
#include <cstdint> | #include <cstdint> | ||||
#include <map> | #include <map> | ||||
#include <memory> | #include <memory> | ||||
#include <set> | #include <set> | ||||
#include <utility> | #include <utility> | ||||
#include <vector> | #include <vector> | ||||
class BlockValidationState; | class BlockValidationState; | ||||
class CBlockIndex; | class CBlockIndex; | ||||
class CBlockTreeDB; | class CBlockTreeDB; | ||||
class CBlockUndo; | class CBlockUndo; | ||||
class CChainParams; | class CChainParams; | ||||
class CChain; | class CChain; | ||||
class CCoinsViewDB; | |||||
class CConnman; | class CConnman; | ||||
class CInv; | class CInv; | ||||
class Config; | class Config; | ||||
class CScriptCheck; | class CScriptCheck; | ||||
class CTxMemPool; | class CTxMemPool; | ||||
class CTxUndo; | class CTxUndo; | ||||
class DisconnectedBlockTransactions; | class DisconnectedBlockTransactions; | ||||
class TxValidationState; | class TxValidationState; | ||||
▲ Show 20 Lines • Show All 701 Lines • ▼ Show 20 Lines | public: | ||||
* m_block_index. | * m_block_index. | ||||
*/ | */ | ||||
bool AcceptBlockHeader(const Config &config, const CBlockHeader &block, | bool AcceptBlockHeader(const Config &config, const CBlockHeader &block, | ||||
BlockValidationState &state, CBlockIndex **ppindex) | BlockValidationState &state, CBlockIndex **ppindex) | ||||
EXCLUSIVE_LOCKS_REQUIRED(cs_main); | EXCLUSIVE_LOCKS_REQUIRED(cs_main); | ||||
}; | }; | ||||
/** | /** | ||||
* A convenience class for constructing the CCoinsView* hierarchy used | |||||
* to facilitate access to the UTXO set. | |||||
* | |||||
* This class consists of an arrangement of layered CCoinsView objects, | |||||
* preferring to store and retrieve coins in memory via `m_cacheview` but | |||||
* ultimately falling back on cache misses to the canonical store of UTXOs on | |||||
* disk, `m_dbview`. | |||||
*/ | |||||
class CoinsViews { | |||||
public: | |||||
//! The lowest level of the CoinsViews cache hierarchy sits in a leveldb | |||||
//! database on disk. All unspent coins reside in this store. | |||||
CCoinsViewDB m_dbview; | |||||
//! This view wraps access to the leveldb instance and handles read errors | |||||
//! gracefully. | |||||
CCoinsViewErrorCatcher m_catcherview; | |||||
//! This is the top layer of the cache hierarchy - it keeps as many coins in | |||||
//! memory as can fit per the dbcache setting. | |||||
std::unique_ptr<CCoinsViewCache> m_cacheview; | |||||
//! This constructor initializes CCoinsViewDB and CCoinsViewErrorCatcher | |||||
//! instances, but it *does not* create a CCoinsViewCache instance by | |||||
//! default. This is done separately because the presence of the cache has | |||||
//! implications on whether or not we're allowed to flush the cache's state | |||||
//! to disk, which should not be done until the health of the database is | |||||
//! verified. | |||||
//! | |||||
//! All arguments forwarded onto CCoinsViewDB. | |||||
CoinsViews(std::string ldb_name, size_t cache_size_bytes, bool in_memory, | |||||
bool should_wipe); | |||||
//! Initialize the CCoinsViewCache member. | |||||
void InitCache(); | |||||
}; | |||||
/** | |||||
* CChainState stores and provides an API to update our local knowledge of the | * CChainState stores and provides an API to update our local knowledge of the | ||||
* current best chain. | * current best chain. | ||||
* | * | ||||
* Eventually, the API here is targeted at being exposed externally as a | * Eventually, the API here is targeted at being exposed externally as a | ||||
* consumable libconsensus library, so any functions added must only call | * consumable libconsensus library, so any functions added must only call | ||||
* other class member functions, pure functions in other parts of the consensus | * other class member functions, pure functions in other parts of the consensus | ||||
* library, callbacks via the validation interface, or read/write-to-disk | * library, callbacks via the validation interface, or read/write-to-disk | ||||
* functions (eventually this will also be via callbacks). | * functions (eventually this will also be via callbacks). | ||||
Show All 30 Lines | private: | ||||
*/ | */ | ||||
mutable std::atomic<bool> m_cached_finished_ibd{false}; | mutable std::atomic<bool> m_cached_finished_ibd{false}; | ||||
//! Reference to a BlockManager instance which itself is shared across all | //! Reference to a BlockManager instance which itself is shared across all | ||||
//! CChainState instances. Keeping a local reference allows us to test more | //! CChainState instances. Keeping a local reference allows us to test more | ||||
//! easily as opposed to referencing a global. | //! easily as opposed to referencing a global. | ||||
BlockManager &m_blockman; | BlockManager &m_blockman; | ||||
//! Manages the UTXO set, which is a reflection of the contents of | |||||
//! `m_chain`. | |||||
std::unique_ptr<CoinsViews> m_coins_views; | |||||
/** | /** | ||||
* The best finalized block. | * The best finalized block. | ||||
* This block cannot be reorged in any way except by explicit user action. | * This block cannot be reorged in any way except by explicit user action. | ||||
*/ | */ | ||||
const CBlockIndex *m_finalizedBlockIndex GUARDED_BY(cs_main) = nullptr; | const CBlockIndex *m_finalizedBlockIndex GUARDED_BY(cs_main) = nullptr; | ||||
public: | public: | ||||
explicit CChainState(BlockManager &blockman) : m_blockman(blockman) {} | CChainState(BlockManager &blockman) : m_blockman(blockman) {} | ||||
CChainState(); | |||||
/** | |||||
* Initialize the CoinsViews UTXO set database management data structures. | |||||
* The in-memory cache is initialized separately. | |||||
* | |||||
* All parameters forwarded to CoinsViews. | |||||
*/ | |||||
void InitCoinsDB(size_t cache_size_bytes, bool in_memory, bool should_wipe, | |||||
std::string leveldb_name = "chainstate"); | |||||
//! Initialize the in-memory coins cache (to be done after the health of the | |||||
//! on-disk database is verified). | |||||
void InitCoinsCache(); | |||||
//! @returns whether or not the CoinsViews object has been fully initialized | |||||
//! and we can | |||||
//! safely flush this object to disk. | |||||
bool CanFlushToDisk() { | |||||
return m_coins_views && m_coins_views->m_cacheview; | |||||
} | |||||
//! The current chain of blockheaders we consult and build on. | //! The current chain of blockheaders we consult and build on. | ||||
//! @see CChain, CBlockIndex. | //! @see CChain, CBlockIndex. | ||||
CChain m_chain; | CChain m_chain; | ||||
/** | /** | ||||
* The set of all CBlockIndex entries with BLOCK_VALID_TRANSACTIONS (for | * The set of all CBlockIndex entries with BLOCK_VALID_TRANSACTIONS (for | ||||
* itself and all ancestors) and as good as our current tip or better. | * itself and all ancestors) and as good as our current tip or better. | ||||
* Entries may be failed, though, and pruning nodes may be missing the data | * Entries may be failed, though, and pruning nodes may be missing the data | ||||
* for the block. | * for the block. | ||||
*/ | */ | ||||
std::set<CBlockIndex *, CBlockIndexWorkComparator> setBlockIndexCandidates; | std::set<CBlockIndex *, CBlockIndexWorkComparator> setBlockIndexCandidates; | ||||
//! @returns A reference to the in-memory cache of the UTXO set. | //! @returns A reference to the in-memory cache of the UTXO set. | ||||
CCoinsViewCache &CoinsTip() { return *::pcoinsTip; } | CCoinsViewCache &CoinsTip() { | ||||
assert(m_coins_views->m_cacheview); | |||||
return *m_coins_views->m_cacheview.get(); | |||||
} | |||||
//! @returns A reference to the on-disk UTXO set database. | |||||
CCoinsViewDB &CoinsDB() { return m_coins_views->m_dbview; } | |||||
//! @returns A reference to a wrapped view of the in-memory UTXO set that | |||||
//! handles disk read errors gracefully. | |||||
CCoinsViewErrorCatcher &CoinsErrorCatcher() { | |||||
return m_coins_views->m_catcherview; | |||||
} | |||||
//! Destructs all objects related to accessing the UTXO set. | |||||
void ResetCoinsViews() { m_coins_views.reset(); } | |||||
/** | /** | ||||
* Update the on-disk chain state. | * Update the on-disk chain state. | ||||
* The caches and indexes are flushed depending on the mode we're called | * The caches and indexes are flushed depending on the mode we're called | ||||
* with if they're too large, if it's been a while since the last write, or | * with if they're too large, if it's been a while since the last write, or | ||||
* always and in all cases if we're in prune mode and are deleting files. | * always and in all cases if we're in prune mode and are deleting files. | ||||
* | * | ||||
* If FlushStateMode::NONE is used, then FlushStateToDisk(...) won't do | * If FlushStateMode::NONE is used, then FlushStateToDisk(...) won't do | ||||
▲ Show 20 Lines • Show All 155 Lines • ▼ Show 20 Lines | |||||
CChainState &ChainstateActive(); | CChainState &ChainstateActive(); | ||||
/** @returns the most-work chain. */ | /** @returns the most-work chain. */ | ||||
CChain &ChainActive(); | CChain &ChainActive(); | ||||
/** @returns the global block index map. */ | /** @returns the global block index map. */ | ||||
BlockMap &BlockIndex(); | BlockMap &BlockIndex(); | ||||
/** | // Most often ::ChainstateActive() should be used instead of this, but some code | ||||
* Global variable that points to the coins database (protected by cs_main) | // may not be able to assume that this has been initialized yet and so must use | ||||
*/ | // it directly, e.g. init.cpp. | ||||
extern std::unique_ptr<CCoinsViewDB> pcoinsdbview; | extern std::unique_ptr<CChainState> g_chainstate; | ||||
/** | /** | ||||
* Global variable that points to the active block tree (protected by cs_main) | * Global variable that points to the active block tree (protected by cs_main) | ||||
*/ | */ | ||||
extern std::unique_ptr<CBlockTreeDB> pblocktree; | extern std::unique_ptr<CBlockTreeDB> pblocktree; | ||||
/** | /** | ||||
* Return the spend height, which is one more than the inputs.GetBestBlock(). | * Return the spend height, which is one more than the inputs.GetBestBlock(). | ||||
Show All 36 Lines |