aboutsummaryrefslogtreecommitdiff
path: root/src/validation.h
diff options
context:
space:
mode:
authorJames O'Beirne <james.obeirne@gmail.com>2019-12-12 10:20:44 -0500
committerJames O'Beirne <james.obeirne@pm.me>2020-03-17 13:27:39 -0400
commit89cdf4d5692d396b8c7177b3918aa9dab07f9624 (patch)
treefee6785bd277c90c0c50cb9572b67d510c349ed0 /src/validation.h
parent8e2ecfe2496d8a015f3ee8723025a438feffbd28 (diff)
downloadbitcoin-89cdf4d5692d396b8c7177b3918aa9dab07f9624.tar.xz
validation: introduce unused ChainstateManager
ChainstateManager is responsible for creating and managing multiple chainstates, and will provide a high-level interface for accessing the appropriate chainstate based upon a certain use. Incorporates feedback from Marco Falke. Additional documentation written by Russ Yanofsky. Co-authored-by: Russell Yanofsky <russ@yanofsky.org>
Diffstat (limited to 'src/validation.h')
-rw-r--r--src/validation.h113
1 files changed, 113 insertions, 0 deletions
diff --git a/src/validation.h b/src/validation.h
index 07d90ab110..35e4380bc0 100644
--- a/src/validation.h
+++ b/src/validation.h
@@ -14,6 +14,7 @@
#include <coins.h>
#include <crypto/common.h> // for ReadLE64
#include <fs.h>
+#include <optional.h>
#include <policy/feerate.h>
#include <protocol.h> // For CMessageHeader::MessageStartChars
#include <script/script_error.h>
@@ -539,6 +540,9 @@ enum class CoinsCacheSizeState
OK = 0
};
+// Defined below, but needed for `friend` usage in CChainState.
+class ChainstateManager;
+
/**
* CChainState stores and provides an API to update our local knowledge of the
* current best chain.
@@ -748,6 +752,8 @@ public:
size_t max_coins_cache_size_bytes,
size_t max_mempool_size_bytes) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
+ std::string ToString() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
+
private:
bool ActivateBestChainStep(BlockValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexMostWork, const std::shared_ptr<const CBlock>& pblock, bool& fInvalidFound, ConnectTrace& connectTrace) EXCLUSIVE_LOCKS_REQUIRED(cs_main, ::mempool.cs);
bool ConnectTip(BlockValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexNew, const std::shared_ptr<const CBlock>& pblock, ConnectTrace& connectTrace, DisconnectedBlockTransactions& disconnectpool) EXCLUSIVE_LOCKS_REQUIRED(cs_main, ::mempool.cs);
@@ -760,6 +766,8 @@ private:
//! Mark a block as not having block data
void EraseBlockData(CBlockIndex* index) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
+
+ friend ChainstateManager;
};
/** Mark a block as precious and reorganize.
@@ -775,6 +783,111 @@ bool InvalidateBlock(BlockValidationState& state, const CChainParams& chainparam
/** Remove invalidity status from a block and its descendants. */
void ResetBlockFailureFlags(CBlockIndex* pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
+/**
+ * Provides an interface for creating and interacting with one or two
+ * chainstates: an IBD chainstate generated by downloading blocks, and
+ * an optional snapshot chainstate loaded from a UTXO snapshot. Managed
+ * chainstates can be maintained at different heights simultaneously.
+ *
+ * This class provides abstractions that allow the retrieval of the current
+ * most-work chainstate ("Active") as well as chainstates which may be in
+ * background use to validate UTXO snapshots.
+ *
+ * Definitions:
+ *
+ * *IBD chainstate*: a chainstate whose current state has been "fully"
+ * validated by the initial block download process.
+ *
+ * *Snapshot chainstate*: a chainstate populated by loading in an
+ * assumeutxo UTXO snapshot.
+ *
+ * *Active chainstate*: the chainstate containing the current most-work
+ * chain. Consulted by most parts of the system (net_processing,
+ * wallet) as a reflection of the current chain and UTXO set.
+ * This may either be an IBD chainstate or a snapshot chainstate.
+ *
+ * *Background IBD chainstate*: an IBD chainstate for which the
+ * IBD process is happening in the background while use of the
+ * active (snapshot) chainstate allows the rest of the system to function.
+ *
+ * *Validated chainstate*: the most-work chainstate which has been validated
+ * locally via initial block download. This will be the snapshot chainstate
+ * if a snapshot was loaded and all blocks up to the snapshot starting point
+ * have been downloaded and validated (via background validation), otherwise
+ * it will be the IBD chainstate.
+ */
+class ChainstateManager
+{
+private:
+ //! The chainstate used under normal operation (i.e. "regular" IBD) or, if
+ //! a snapshot is in use, for background validation.
+ //!
+ //! Its contents (including on-disk data) will be deleted *upon shutdown*
+ //! after background validation of the snapshot has completed. We do not
+ //! free the chainstate contents immediately after it finishes validation
+ //! to cautiously avoid a case where some other part of the system is still
+ //! using this pointer (e.g. net_processing).
+ std::unique_ptr<CChainState> m_ibd_chainstate;
+
+ //! A chainstate initialized on the basis of a UTXO snapshot. If this is
+ //! non-null, it is always our active chainstate.
+ std::unique_ptr<CChainState> m_snapshot_chainstate;
+
+ //! Points to either the ibd or snapshot chainstate; indicates our
+ //! most-work chain.
+ CChainState* m_active_chainstate{nullptr};
+
+ //! If true, the assumed-valid chainstate has been fully validated
+ //! by the background validation chainstate.
+ bool m_snapshot_validated{false};
+
+ // For access to m_active_chainstate.
+ friend CChain& ChainActive();
+
+public:
+ //! Instantiate a new chainstate and assign it based upon whether it is
+ //! from a snapshot.
+ //!
+ //! @param[in] snapshot_blockhash If given, signify that this chainstate
+ //! is based on a snapshot.
+ CChainState& InitializeChainstate(const uint256& snapshot_blockhash = uint256())
+ EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
+
+ //! Get all chainstates currently being used.
+ std::vector<CChainState*> GetAll();
+
+ //! The most-work chain.
+ CChain& ActiveChain() const;
+ int ActiveHeight() const { return ActiveChain().Height(); }
+ CBlockIndex* ActiveTip() const { return ActiveChain().Tip(); }
+
+ bool IsSnapshotActive() const;
+
+ Optional<uint256> SnapshotBlockhash() const;
+
+ //! Is there a snapshot in use and has it been fully validated?
+ bool IsSnapshotValidated() const { return m_snapshot_validated; }
+
+ //! @returns true if this chainstate is being used to validate an active
+ //! snapshot in the background.
+ bool IsBackgroundIBD(CChainState* chainstate) const;
+
+ //! Return the most-work chainstate that has been fully validated.
+ //!
+ //! During background validation of a snapshot, this is the IBD chain. After
+ //! background validation has completed, this is the snapshot chain.
+ CChainState& ValidatedChainstate() const;
+
+ CChain& ValidatedChain() const { return ValidatedChainstate().m_chain; }
+ CBlockIndex* ValidatedTip() const { return ValidatedChain().Tip(); }
+
+ //! Unload block index and chain data before shutdown.
+ void Unload() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
+
+ //! Clear (deconstruct) chainstate data.
+ void Reset();
+};
+
/** @returns the most-work valid chainstate. */
CChainState& ChainstateActive();