mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-04-11 16:17:54 +02:00
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:
@@ -29,6 +29,18 @@ namespace node {
|
||||
class Warnings;
|
||||
static constexpr int DEFAULT_STOPATHEIGHT{0};
|
||||
|
||||
//! State tracked by the KernelNotifications interface meant to be used by
|
||||
//! mining code, index code, RPCs, and other code sitting above the validation
|
||||
//! layer.
|
||||
//!
|
||||
//! Currently just tracks the chain tip, but could be used to hold other
|
||||
//! information in the future, like the last flushed block, pruning
|
||||
//! information, etc.
|
||||
struct KernelState {
|
||||
bool chainstate_loaded{false};
|
||||
std::optional<uint256> tip_block;
|
||||
};
|
||||
|
||||
class KernelNotifications : public kernel::Notifications
|
||||
{
|
||||
public:
|
||||
@@ -49,6 +61,13 @@ public:
|
||||
|
||||
void fatalError(const bilingual_str& message) override;
|
||||
|
||||
void setChainstateLoaded(bool chainstate_loaded) EXCLUSIVE_LOCKS_REQUIRED(!m_tip_block_mutex) {
|
||||
LOCK(m_tip_block_mutex);
|
||||
if (!chainstate_loaded) m_state = {};
|
||||
m_state.chainstate_loaded = chainstate_loaded;
|
||||
m_tip_block_cv.notify_all();
|
||||
}
|
||||
|
||||
//! Block height after which blockTip notification will return Interrupted{}, if >0.
|
||||
int m_stop_at_height{DEFAULT_STOPATHEIGHT};
|
||||
//! Useful for tests, can be set to false to avoid shutdown on fatal error.
|
||||
@@ -56,6 +75,7 @@ public:
|
||||
|
||||
Mutex m_tip_block_mutex;
|
||||
std::condition_variable m_tip_block_cv GUARDED_BY(m_tip_block_mutex);
|
||||
KernelState m_state GUARDED_BY(m_tip_block_mutex);
|
||||
//! The block for which the last blockTip notification was received.
|
||||
//! It's first set when the tip is connected during node initialization.
|
||||
//! Might be unset during an early shutdown.
|
||||
@@ -65,8 +85,6 @@ private:
|
||||
const std::function<bool()>& m_shutdown_request;
|
||||
std::atomic<int>& m_exit_status;
|
||||
node::Warnings& m_warnings;
|
||||
|
||||
std::optional<uint256> m_tip_block GUARDED_BY(m_tip_block_mutex);
|
||||
};
|
||||
|
||||
void ReadNotificationArgs(const ArgsManager& args, KernelNotifications& notifications);
|
||||
|
||||
Reference in New Issue
Block a user