init refactor: Only initialize node.notifications one time

Instead of having the InitAndLoadChainstate function delete and create the
KernelNotifications object each time it is called (it can be called twice when
reindexing) to clear cached state, create it just one time and add a
setChainstateLoaded() method to manage state as it is loaded and unloaded.

This refactoring should make sense by itself to be more explicit about how
KernelNotifications state is cleared, but it's also needed to make outside code
accessing KernelNotifications state (currently just mining code) safe during
node startup and shutdown so the KernelNofications mutex can be used for
synchronization and does not get recreated itself.
This commit is contained in:
Ryan Ofsky
2026-02-24 10:15:14 -05:00
parent c8e332cb33
commit a7cabf92e4
3 changed files with 31 additions and 9 deletions

View File

@@ -53,7 +53,7 @@ kernel::InterruptResult KernelNotifications::blockTip(SynchronizationState state
{
LOCK(m_tip_block_mutex);
Assume(index.GetBlockHash() != uint256::ZERO);
m_tip_block = index.GetBlockHash();
m_state.tip_block = index.GetBlockHash();
m_tip_block_cv.notify_all();
}
@@ -103,7 +103,7 @@ void KernelNotifications::fatalError(const bilingual_str& message)
std::optional<uint256> KernelNotifications::TipBlock()
{
AssertLockHeld(m_tip_block_mutex);
return m_tip_block;
return m_state.tip_block;
};