Skip to content

Commit

Permalink
kernel: Remove shutdown from kernel library
Browse files Browse the repository at this point in the history
This decouples the libbitcoinkernel library from the shutdown routines.
As a library, it should not directly rely on code that manages the
termination of processes.

While touching the lines around StartShutdown, it became apparent that
there was no logging indicating the reason for a call to StartShutdown.
Add ShutdownReason enum to the startShutdown notification to communicate
this.
  • Loading branch information
TheCharlatan committed May 31, 2023
1 parent 2db5ddf commit a6a3c32
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/Makefile.am
Expand Up @@ -396,6 +396,7 @@ libbitcoin_node_a_SOURCES = \
kernel/context.cpp \
kernel/cs_main.cpp \
kernel/mempool_persist.cpp \
kernel/notifications_interface.cpp \
mapport.cpp \
net.cpp \
net_processing.cpp \
Expand Down Expand Up @@ -935,6 +936,7 @@ libbitcoinkernel_la_SOURCES = \
kernel/context.cpp \
kernel/cs_main.cpp \
kernel/mempool_persist.cpp \
kernel/notifications_interface.cpp \
key.cpp \
logging.cpp \
node/blockstorage.cpp \
Expand All @@ -958,7 +960,6 @@ libbitcoinkernel_la_SOURCES = \
script/script_error.cpp \
script/sigcache.cpp \
script/standard.cpp \
shutdown.cpp \
signet.cpp \
support/cleanse.cpp \
support/lockedpool.cpp \
Expand Down
5 changes: 5 additions & 0 deletions src/bitcoin-chainstate.cpp
Expand Up @@ -117,6 +117,11 @@ int main(int argc, char* argv[])
std::cerr << "Error: " << debug_message << std::endl;
std::cerr << (user_message.empty() ? "A fatal internal error occurred." : user_message.original) << std::endl;
}
void startShutdown(const kernel::ShutdownReason reason) override
{
m_shutdown_requested = true;
std::cout << "Received start shutdown notification: " << ShutdownReasonToString(reason);
}
};
auto notifications = std::make_unique<KernelNotifications>(shutdown_requested);

Expand Down
24 changes: 24 additions & 0 deletions src/kernel/notifications_interface.cpp
@@ -0,0 +1,24 @@
// Copyright (c) 2023 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <kernel/notifications_interface.h>

#include <string>

namespace kernel {

std::string ShutdownReasonToString(ShutdownReason reason)
{
switch (reason) {
case kernel::ShutdownReason::FailedConnectingBestBlock:
return "Failed connecting best block.";
case kernel::ShutdownReason::StopAfterBlockImport:
return "Stopping after block import.";
case kernel::ShutdownReason::StopAtHeight:
return "Reached block height specified by stop at height.";
} // no default case, so the compiler can warn about missing cases
assert(false);
}

} // namespace kernel
9 changes: 9 additions & 0 deletions src/kernel/notifications_interface.h
Expand Up @@ -15,6 +15,14 @@ enum class SynchronizationState;

namespace kernel {

enum class ShutdownReason {
StopAtHeight,
StopAfterBlockImport,
FailedConnectingBestBlock,
};

std::string ShutdownReasonToString(ShutdownReason reason);

/**
* A base class defining functions for notifying about certain kernel
* events.
Expand All @@ -34,6 +42,7 @@ class Notifications
//! from. After this notification is sent, whatever function triggered the
//! error should also return an error code or raise an exception.
virtual void fatalError(const std::string& debug_message, const bilingual_str& user_message = {}) {}
virtual void startShutdown(const ShutdownReason reason) {}
};
} // namespace kernel

Expand Down
8 changes: 5 additions & 3 deletions src/node/blockstorage.cpp
Expand Up @@ -10,10 +10,10 @@
#include <flatfile.h>
#include <hash.h>
#include <kernel/chainparams.h>
#include <kernel/notifications_interface.h>
#include <logging.h>
#include <pow.h>
#include <reverse_iterator.h>
#include <shutdown.h>
#include <signet.h>
#include <streams.h>
#include <undo.h>
Expand All @@ -25,6 +25,8 @@
#include <map>
#include <unordered_map>

using kernel::ShutdownReason;

namespace node {
std::atomic_bool fReindex(false);
std::atomic_bool g_indexes_ready_to_sync{false};
Expand Down Expand Up @@ -931,14 +933,14 @@ void ThreadImport(ChainstateManager& chainman, std::vector<fs::path> vImportFile
BlockValidationState state;
if (!chainstate->ActivateBestChain(state, nullptr)) {
LogPrintf("Failed to connect best block (%s)\n", state.ToString());
StartShutdown();
chainman.GetNotifications().startShutdown(ShutdownReason::FailedConnectingBestBlock);
return;
}
}

if (chainman.m_blockman.StopAfterBlockImport()) {
LogPrintf("Stopping after block import\n");
StartShutdown();
chainman.GetNotifications().startShutdown(ShutdownReason::StopAfterBlockImport);
return;
}
} // End scope of ImportingNow
Expand Down
8 changes: 8 additions & 0 deletions src/node/kernel_notifications.cpp
Expand Up @@ -82,4 +82,12 @@ void KernelNotifications::fatalError(const std::string& debug_message, const bil
if (m_shutdown_on_fatal_error) StartShutdown();
}

void KernelNotifications::startShutdown(const kernel::ShutdownReason reason)
{
if (!ShutdownRequested()) {
LogPrintf("Starting shutdown: %s\n", ShutdownReasonToString(reason));
}
StartShutdown();
}

} // namespace node
2 changes: 2 additions & 0 deletions src/node/kernel_notifications.h
Expand Up @@ -28,6 +28,8 @@ class KernelNotifications : public kernel::Notifications

void fatalError(const std::string& debug_message, const bilingual_str& user_message = {}) override;

void startShutdown(const kernel::ShutdownReason reason) override;

//! Useful for tests, can be set to false to avoid shutdown on fatal error.
bool m_shutdown_on_fatal_error{true};
};
Expand Down
4 changes: 2 additions & 2 deletions src/validation.cpp
Expand Up @@ -38,7 +38,6 @@
#include <reverse_iterator.h>
#include <script/script.h>
#include <script/sigcache.h>
#include <shutdown.h>
#include <signet.h>
#include <tinyformat.h>
#include <txdb.h>
Expand Down Expand Up @@ -72,6 +71,7 @@ using kernel::CoinStatsHashType;
using kernel::ComputeUTXOStats;
using kernel::LoadMempool;
using kernel::Notifications;
using kernel::ShutdownReason;

using fsbridge::FopenFn;
using node::BlockManager;
Expand Down Expand Up @@ -3179,7 +3179,7 @@ bool Chainstate::ActivateBestChain(BlockValidationState& state, std::shared_ptr<
}
// When we reach this point, we switched to a new tip (stored in pindexNewTip).

if (nStopAtHeight && pindexNewTip && pindexNewTip->nHeight >= nStopAtHeight) StartShutdown();
if (nStopAtHeight && pindexNewTip && pindexNewTip->nHeight >= nStopAtHeight) m_chainman.GetNotifications().startShutdown(ShutdownReason::StopAtHeight);

if (WITH_LOCK(::cs_main, return m_disabled)) {
// Background chainstate has reached the snapshot base block, so exit.
Expand Down
1 change: 0 additions & 1 deletion src/validation.h
Expand Up @@ -23,7 +23,6 @@
#include <policy/packages.h>
#include <policy/policy.h>
#include <script/script_error.h>
#include <shutdown.h>
#include <sync.h>
#include <txdb.h>
#include <txmempool.h> // For CTxMemPool::cs
Expand Down

0 comments on commit a6a3c32

Please sign in to comment.