Page MenuHomePhabricator

D14685.id42786.diff
No OneTemporary

D14685.id42786.diff

diff --git a/src/validation.h b/src/validation.h
--- a/src/validation.h
+++ b/src/validation.h
@@ -683,6 +683,20 @@
//! `m_chain`.
std::unique_ptr<CoinsViews> m_coins_views;
+ //! This toggle exists for use when doing background validation for UTXO
+ //! snapshots.
+ //!
+ //! In the expected case, it is set once the background validation chain
+ //! reaches the same height as the base of the snapshot and its UTXO set is
+ //! found to hash to the expected assumeutxo value. It signals that we
+ //! should no longer connect blocks to the background chainstate. When set
+ //! on the background validation chainstate, it signifies that we have fully
+ //! validated the snapshot chainstate.
+ //!
+ //! In the unlikely case that the snapshot chainstate is found to be
+ //! invalid, this is set to true on the snapshot chainstate.
+ bool m_disabled GUARDED_BY(::cs_main){false};
+
mutable Mutex cs_avalancheFinalizedBlockIndex;
/**
@@ -1104,10 +1118,6 @@
//! that call.
Chainstate *m_active_chainstate GUARDED_BY(::cs_main){nullptr};
- //! If true, the assumed-valid chainstate has been fully validated
- //! by the background validation chainstate.
- bool m_snapshot_validated GUARDED_BY(::cs_main){false};
-
CBlockIndex *m_best_invalid GUARDED_BY(::cs_main){nullptr};
CBlockIndex *m_best_parked GUARDED_BY(::cs_main){nullptr};
@@ -1128,6 +1138,16 @@
EXCLUSIVE_LOCKS_REQUIRED(cs_main);
friend Chainstate;
+ //! Return true if a chainstate is considered usable.
+ //!
+ //! This is false when a background validation chainstate has completed its
+ //! validation of an assumed-valid chainstate, or when a snapshot
+ //! chainstate has been found to be invalid.
+ bool IsUsable(const Chainstate *const pchainstate) const
+ EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
+ return pchainstate && !pchainstate->m_disabled;
+ }
+
public:
explicit ChainstateManager(const Config &config) : m_config{config} {}
@@ -1243,7 +1263,8 @@
//! Is there a snapshot in use and has it been fully validated?
bool IsSnapshotValidated() const EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
- return m_snapshot_validated;
+ return m_snapshot_chainstate && m_ibd_chainstate &&
+ m_ibd_chainstate->m_disabled;
}
/**
diff --git a/src/validation.cpp b/src/validation.cpp
--- a/src/validation.cpp
+++ b/src/validation.cpp
@@ -5770,12 +5770,11 @@
LOCK(::cs_main);
std::vector<Chainstate *> out;
- if (!IsSnapshotValidated() && m_ibd_chainstate) {
- out.push_back(m_ibd_chainstate.get());
- }
-
- if (m_snapshot_chainstate) {
- out.push_back(m_snapshot_chainstate.get());
+ for (Chainstate *pchainstate :
+ {m_ibd_chainstate.get(), m_snapshot_chainstate.get()}) {
+ if (this->IsUsable(pchainstate)) {
+ out.push_back(pchainstate);
+ }
}
return out;
@@ -6194,18 +6193,24 @@
}
void ChainstateManager::MaybeRebalanceCaches() {
AssertLockHeld(::cs_main);
- if (m_ibd_chainstate && !m_snapshot_chainstate) {
+ bool ibd_usable = this->IsUsable(m_ibd_chainstate.get());
+ bool snapshot_usable = this->IsUsable(m_snapshot_chainstate.get());
+ assert(ibd_usable || snapshot_usable);
+
+ if (ibd_usable && !snapshot_usable) {
LogPrintf("[snapshot] allocating all cache to the IBD chainstate\n");
// Allocate everything to the IBD chainstate.
m_ibd_chainstate->ResizeCoinsCaches(m_total_coinstip_cache,
m_total_coinsdb_cache);
- } else if (m_snapshot_chainstate && !m_ibd_chainstate) {
+ } else if (snapshot_usable && !ibd_usable) {
+ // If background validation has completed and snapshot is our active
+ // chain...
LogPrintf(
"[snapshot] allocating all cache to the snapshot chainstate\n");
// Allocate everything to the snapshot chainstate.
m_snapshot_chainstate->ResizeCoinsCaches(m_total_coinstip_cache,
m_total_coinsdb_cache);
- } else if (m_ibd_chainstate && m_snapshot_chainstate) {
+ } else if (ibd_usable && snapshot_usable) {
// If both chainstates exist, determine who needs more cache based on
// IBD status.
//

File Metadata

Mime Type
text/plain
Expires
Sat, Apr 26, 10:34 (3 h, 12 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
5561821
Default Alt Text
D14685.id42786.diff (4 KB)

Event Timeline