chain: add CChain::IsTipRecent helper
Factor the chain tip work/recency check out of `ChainstateManager::IsInitialBlockDownload()` into a reusable `CChain::IsTipRecent()` helper, and annotate it as requiring `cs_main` since it's reading mutable state. Also introduce a local `chainman_ref` in the kernel import-blocks wrapper to reduce repetition and keep follow-up diffs small. `IsInitialBlockDownload` returns were also unified to make the followup move clean. Co-authored-by: Patrick Strateman <patrick.strateman@gmail.com> Co-authored-by: Martin Zumsande <mzumsande@gmail.com>
This commit is contained in:
3 files changed
+15
-18
No files matched your search
@@ -420,6 +420,15 @@ public:
|
|||||||
return int(vChain.size()) - 1;
|
return int(vChain.size()) - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Check whether this chain's tip exists, has enough work, and is recent. */
|
||||||
|
bool IsTipRecent(const arith_uint256& min_chain_work, std::chrono::seconds max_tip_age) const EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
|
||||||
|
{
|
||||||
|
const auto tip{Tip()};
|
||||||
|
return tip &&
|
||||||
|
tip->nChainWork >= min_chain_work &&
|
||||||
|
tip->Time() >= Now<NodeSeconds>() - max_tip_age;
|
||||||
|
}
|
||||||
|
|
||||||
/** Set/initialize a chain with a given tip. */
|
/** Set/initialize a chain with a given tip. */
|
||||||
void SetTip(CBlockIndex& block);
|
void SetTip(CBlockIndex& block);
|
||||||
|
|
||||||
|
|||||||
@@ -1054,7 +1054,8 @@ int btck_chainstate_manager_import_blocks(btck_ChainstateManager* chainman, cons
|
|||||||
import_files.emplace_back(std::string{block_file_paths_data[i], block_file_paths_lens[i]}.c_str());
|
import_files.emplace_back(std::string{block_file_paths_data[i], block_file_paths_lens[i]}.c_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
node::ImportBlocks(*btck_ChainstateManager::get(chainman).m_chainman, import_files);
|
auto& chainman_ref{*btck_ChainstateManager::get(chainman).m_chainman};
|
||||||
|
node::ImportBlocks(chainman_ref, import_files);
|
||||||
} catch (const std::exception& e) {
|
} catch (const std::exception& e) {
|
||||||
LogError("Failed to import blocks: %s", e.what());
|
LogError("Failed to import blocks: %s", e.what());
|
||||||
return -1;
|
return -1;
|
||||||
|
|||||||
+4
-17
@@ -1947,25 +1947,12 @@ void Chainstate::InitCoinsCache(size_t cache_size_bytes)
|
|||||||
bool ChainstateManager::IsInitialBlockDownload() const
|
bool ChainstateManager::IsInitialBlockDownload() const
|
||||||
{
|
{
|
||||||
// Optimization: pre-test latch before taking the lock.
|
// Optimization: pre-test latch before taking the lock.
|
||||||
if (!m_cached_is_ibd.load(std::memory_order_relaxed))
|
if (!m_cached_is_ibd.load(std::memory_order_relaxed)) return false;
|
||||||
return false;
|
|
||||||
|
|
||||||
LOCK(cs_main);
|
LOCK(cs_main);
|
||||||
if (!m_cached_is_ibd.load(std::memory_order_relaxed))
|
if (!m_cached_is_ibd.load(std::memory_order_relaxed)) return false;
|
||||||
return false;
|
if (m_blockman.LoadingBlocks()) return true;
|
||||||
if (m_blockman.LoadingBlocks()) {
|
if (!ActiveChain().IsTipRecent(MinimumChainWork(), m_options.max_tip_age)) return true;
|
||||||
return true;
|
|
||||||
}
|
|
||||||
CChain& chain{ActiveChain()};
|
|
||||||
if (chain.Tip() == nullptr) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if (chain.Tip()->nChainWork < MinimumChainWork()) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if (chain.Tip()->Time() < Now<NodeSeconds>() - m_options.max_tip_age) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
LogInfo("Leaving InitialBlockDownload (latching to false)");
|
LogInfo("Leaving InitialBlockDownload (latching to false)");
|
||||||
m_cached_is_ibd.store(false, std::memory_order_relaxed);
|
m_cached_is_ibd.store(false, std::memory_order_relaxed);
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
Reference in new issue
Block a user