Skip to content

Commit

Permalink
refactor: remove warnings globals
Browse files Browse the repository at this point in the history
  • Loading branch information
stickies-v committed May 7, 2024
1 parent 5ced1ec commit 5b8f68c
Show file tree
Hide file tree
Showing 28 changed files with 95 additions and 56 deletions.
3 changes: 3 additions & 0 deletions src/bitcoind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <interfaces/chain.h>
#include <interfaces/init.h>
#include <node/context.h>
#include <node/warnings.h>
#include <node/interface_ui.h>
#include <noui.h>
#include <util/check.h>
Expand Down Expand Up @@ -181,6 +182,8 @@ static bool AppInit(NodeContext& node)
return false;
}

node.warnings = std::make_unique<node::Warnings>();

node.kernel = std::make_unique<kernel::Context>();
if (!AppInitSanityChecks(*node.kernel))
{
Expand Down
2 changes: 1 addition & 1 deletion src/index/base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ template <typename... Args>
void BaseIndex::FatalErrorf(const char* fmt, const Args&... args)
{
auto message = tfm::format(fmt, args...);
node::AbortNode(m_chain->context()->shutdown, m_chain->context()->exit_status, Untranslated(message));
node::AbortNode(m_chain->context()->shutdown, m_chain->context()->exit_status, Untranslated(message), m_chain->context()->warnings.get());
}

CBlockLocator GetLocator(interfaces::Chain& chain, const uint256& block_hash)
Expand Down
5 changes: 3 additions & 2 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1476,7 +1476,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)

// ********************************************************* Step 7: load block chain

node.notifications = std::make_unique<KernelNotifications>(*Assert(node.shutdown), node.exit_status);
node.notifications = std::make_unique<KernelNotifications>(*Assert(node.shutdown), node.exit_status, *Assert(node.warnings));
ReadNotificationArgs(args, *node.notifications);
fReindex = args.GetBoolArg("-reindex", false);
bool fReindexChainState = args.GetBoolArg("-reindex-chainstate", false);
Expand Down Expand Up @@ -1630,7 +1630,8 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
assert(!node.peerman);
node.peerman = PeerManager::make(*node.connman, *node.addrman,
node.banman.get(), chainman,
*node.mempool, peerman_opts);
*node.mempool, peerman_opts,
*node.warnings);
validation_signals.RegisterValidationInterface(node.peerman.get());

// ********************************************************* Step 8: start indexers
Expand Down
15 changes: 9 additions & 6 deletions src/net_processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <node/blockstorage.h>
#include <node/timeoffsets.h>
#include <node/txreconciliation.h>
#include <node/warnings.h>
#include <policy/fees.h>
#include <policy/policy.h>
#include <policy/settings.h>
Expand Down Expand Up @@ -488,7 +489,7 @@ class PeerManagerImpl final : public PeerManager
public:
PeerManagerImpl(CConnman& connman, AddrMan& addrman,
BanMan* banman, ChainstateManager& chainman,
CTxMemPool& pool, Options opts);
CTxMemPool& pool, Options opts, node::Warnings& warnings);

/** Overridden from CValidationInterface. */
void BlockConnected(ChainstateRole role, const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindexConnected) override
Expand Down Expand Up @@ -789,7 +790,8 @@ class PeerManagerImpl final : public PeerManager
/** Next time to check for stale tip */
std::chrono::seconds m_stale_tip_check_time GUARDED_BY(cs_main){0s};

TimeOffsets m_outbound_time_offsets;
node::Warnings& m_warnings;
TimeOffsets m_outbound_time_offsets{m_warnings};

const Options m_opts;

Expand Down Expand Up @@ -2041,14 +2043,14 @@ std::optional<std::string> PeerManagerImpl::FetchBlock(NodeId peer_id, const CBl

std::unique_ptr<PeerManager> PeerManager::make(CConnman& connman, AddrMan& addrman,
BanMan* banman, ChainstateManager& chainman,
CTxMemPool& pool, Options opts)
CTxMemPool& pool, Options opts, node::Warnings& warnings)
{
return std::make_unique<PeerManagerImpl>(connman, addrman, banman, chainman, pool, opts);
return std::make_unique<PeerManagerImpl>(connman, addrman, banman, chainman, pool, opts, warnings);
}

PeerManagerImpl::PeerManagerImpl(CConnman& connman, AddrMan& addrman,
BanMan* banman, ChainstateManager& chainman,
CTxMemPool& pool, Options opts)
CTxMemPool& pool, Options opts, node::Warnings& warnings)
: m_rng{opts.deterministic_rng},
m_fee_filter_rounder{CFeeRate{DEFAULT_MIN_RELAY_TX_FEE}, m_rng},
m_chainparams(chainman.GetParams()),
Expand All @@ -2057,7 +2059,8 @@ PeerManagerImpl::PeerManagerImpl(CConnman& connman, AddrMan& addrman,
m_banman(banman),
m_chainman(chainman),
m_mempool(pool),
m_opts{opts}
m_opts{opts},
m_warnings{warnings}
{
// While Erlay support is incomplete, it must be enabled explicitly via -txreconciliation.
// This argument can go away after Erlay support is complete.
Expand Down
7 changes: 6 additions & 1 deletion src/net_processing.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ class CChainParams;
class CTxMemPool;
class ChainstateManager;

namespace node
{
class Warnings;
} // namespace node

/** Whether transaction reconciliation protocol should be enabled by default. */
static constexpr bool DEFAULT_TXRECONCILIATION_ENABLE{false};
/** Default for -maxorphantx, maximum number of orphan transactions kept in memory */
Expand Down Expand Up @@ -73,7 +78,7 @@ class PeerManager : public CValidationInterface, public NetEventsInterface

static std::unique_ptr<PeerManager> make(CConnman& connman, AddrMan& addrman,
BanMan* banman, ChainstateManager& chainman,
CTxMemPool& pool, Options opts);
CTxMemPool& pool, Options opts, node::Warnings& warnings);
virtual ~PeerManager() { }

/**
Expand Down
4 changes: 2 additions & 2 deletions src/node/abort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

namespace node {

void AbortNode(util::SignalInterrupt* shutdown, std::atomic<int>& exit_status, const bilingual_str& message)
void AbortNode(util::SignalInterrupt* shutdown, std::atomic<int>& exit_status, const bilingual_str& message, node::Warnings* warnings)
{
g_warnings.Set("internal", message);
if (warnings) warnings->Set("internal", message);
InitError(_("A fatal internal error occurred, see debug.log for details: ") + message);
exit_status.store(EXIT_FAILURE);
if (shutdown && !(*shutdown)()) {
Expand Down
3 changes: 2 additions & 1 deletion src/node/abort.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ class SignalInterrupt;
} // namespace util

namespace node {
void AbortNode(util::SignalInterrupt* shutdown, std::atomic<int>& exit_status, const bilingual_str& message);
class Warnings;
void AbortNode(util::SignalInterrupt* shutdown, std::atomic<int>& exit_status, const bilingual_str& message, node::Warnings* warnings);
} // namespace node

#endif // BITCOIN_NODE_ABORT_H
1 change: 1 addition & 0 deletions src/node/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <net_processing.h>
#include <netgroup.h>
#include <node/kernel_notifications.h>
#include <node/warnings.h>
#include <policy/fees.h>
#include <scheduler.h>
#include <txmempool.h>
Expand Down
3 changes: 3 additions & 0 deletions src/node/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class WalletLoader;

namespace node {
class KernelNotifications;
class Warnings;

//! NodeContext struct containing references to chain state and connection
//! state.
Expand Down Expand Up @@ -76,6 +77,8 @@ struct NodeContext {
//! Issues calls about blocks and transactions
std::unique_ptr<ValidationSignals> validation_signals;
std::atomic<int> exit_status{EXIT_SUCCESS};
//! Node warnings
std::unique_ptr<node::Warnings> warnings;

//! Declare default constructor and destructor that are not inline, so code
//! instantiating the NodeContext struct doesn't need to #include class
Expand Down
3 changes: 2 additions & 1 deletion src/node/interfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,15 @@ class NodeImpl : public Node
explicit NodeImpl(NodeContext& context) { setContext(&context); }
void initLogging() override { InitLogging(args()); }
void initParameterInteraction() override { InitParameterInteraction(args()); }
bilingual_str getWarnings() override { return Join(node::g_warnings.GetMessages(), Untranslated("<hr />")); }
bilingual_str getWarnings() override { return Join(m_context->warnings->GetMessages(), Untranslated("<hr />")); }
int getExitStatus() override { return Assert(m_context)->exit_status.load(); }
uint32_t getLogCategories() override { return LogInstance().GetCategoryMask(); }
bool baseInitialize() override
{
if (!AppInitBasicSetup(args(), Assert(context())->exit_status)) return false;
if (!AppInitParameterInteraction(args())) return false;

m_context->warnings = std::make_unique<node::Warnings>();
m_context->kernel = std::make_unique<kernel::Context>();
if (!AppInitSanityChecks(*m_context->kernel)) return false;

Expand Down
17 changes: 6 additions & 11 deletions src/node/kernel_notifications.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,6 @@ static void AlertNotify(const std::string& strMessage)
#endif
}

static void DoWarning(const std::string& id, const bilingual_str& warning)
{
if (node::g_warnings.Set(id, warning)) {
AlertNotify(warning.original);
}
}

namespace node {

kernel::InterruptResult KernelNotifications::blockTip(SynchronizationState state, CBlockIndex& index)
Expand All @@ -78,23 +71,25 @@ void KernelNotifications::progress(const bilingual_str& title, int progress_perc

void KernelNotifications::warningSet(const std::string& id, const bilingual_str& warning)
{
DoWarning(id, warning);
if (m_warnings.Set(id, warning)) {
AlertNotify(warning.original);
}
}

void KernelNotifications::warningUnset(const std::string& id)
{
g_warnings.Unset(id);
m_warnings.Unset(id);
}

void KernelNotifications::flushError(const bilingual_str& message)
{
AbortNode(&m_shutdown, m_exit_status, message);
AbortNode(&m_shutdown, m_exit_status, message, &m_warnings);
}

void KernelNotifications::fatalError(const bilingual_str& message)
{
node::AbortNode(m_shutdown_on_fatal_error ? &m_shutdown : nullptr,
m_exit_status, message);
m_exit_status, message, &m_warnings);
}

void ReadNotificationArgs(const ArgsManager& args, KernelNotifications& notifications)
Expand Down
5 changes: 4 additions & 1 deletion src/node/kernel_notifications.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ class SignalInterrupt;

namespace node {

class Warnings;
static constexpr int DEFAULT_STOPATHEIGHT{0};

class KernelNotifications : public kernel::Notifications
{
public:
KernelNotifications(util::SignalInterrupt& shutdown, std::atomic<int>& exit_status) : m_shutdown(shutdown), m_exit_status{exit_status} {}
KernelNotifications(util::SignalInterrupt& shutdown, std::atomic<int>& exit_status, node::Warnings& warnings)
: m_shutdown(shutdown), m_exit_status{exit_status}, m_warnings{warnings} {}

[[nodiscard]] kernel::InterruptResult blockTip(SynchronizationState state, CBlockIndex& index) override;

Expand All @@ -50,6 +52,7 @@ class KernelNotifications : public kernel::Notifications
private:
util::SignalInterrupt& m_shutdown;
std::atomic<int>& m_exit_status;
node::Warnings& m_warnings;
};

void ReadNotificationArgs(const ArgsManager& args, KernelNotifications& notifications);
Expand Down
4 changes: 2 additions & 2 deletions src/node/timeoffsets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ bool TimeOffsets::WarnIfOutOfSync() const
// when median == std::numeric_limits<int64_t>::min(), calling std::chrono::abs is UB
auto median{std::max(Median(), std::chrono::seconds(std::numeric_limits<int64_t>::min() + 1))};
if (std::chrono::abs(median) <= WARN_THRESHOLD) {
node::g_warnings.Unset("clock-out-of-sync");
m_warnings.Unset("clock-out-of-sync");
return false;
}

Expand All @@ -62,6 +62,6 @@ bool TimeOffsets::WarnIfOutOfSync() const
"RPC methods to get more info."
), Ticks<std::chrono::minutes>(WARN_THRESHOLD))};
LogWarning("%s\n", msg.original);
node::g_warnings.Set("clock-out-of-sync", msg);
m_warnings.Set("clock-out-of-sync", msg);
return true;
}
11 changes: 11 additions & 0 deletions src/node/timeoffsets.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,17 @@
#include <cstddef>
#include <deque>

namespace node
{
class Warnings;
} //namespace node

class TimeOffsets
{
public:
TimeOffsets(node::Warnings& warnings) : m_warnings{warnings} {}

private:
//! Maximum number of timeoffsets stored.
static constexpr size_t MAX_SIZE{50};
//! Minimum difference between system and network time for a warning to be raised.
Expand All @@ -23,6 +32,8 @@ class TimeOffsets
* positive offset means our peer's clock is ahead of our local clock. */
std::deque<std::chrono::seconds> m_offsets GUARDED_BY(m_mutex){};

node::Warnings& m_warnings;

public:
/** Add a new time offset sample. */
void Add(std::chrono::seconds offset) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
Expand Down
5 changes: 1 addition & 4 deletions src/node/warnings.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@
#define BITCOIN_NODE_WARNINGS_H

#include <sync.h>
#include <util/translation.h>

#include <map>
#include <string>
#include <vector>

struct bilingual_str;

namespace node {
class Warnings
{
Expand All @@ -25,8 +24,6 @@ class Warnings
/** Return potential problems detected by the node. */
std::vector<bilingual_str> GetMessages() const;
};

static Warnings g_warnings;
} // namespace node

#endif // BITCOIN_NODE_WARNINGS_H
3 changes: 2 additions & 1 deletion src/rpc/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1304,7 +1304,8 @@ RPCHelpMan getblockchaininfo()
}
}

obj.pushKV("warnings", GetNodeWarnings(IsDeprecatedRPCEnabled("warnings")));
NodeContext& node = EnsureAnyNodeContext(request.context);
obj.pushKV("warnings", GetNodeWarnings(*CHECK_NONFATAL(node.warnings), IsDeprecatedRPCEnabled("warnings")));
return obj;
},
};
Expand Down
2 changes: 1 addition & 1 deletion src/rpc/mining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ static RPCHelpMan getmininginfo()
obj.pushKV("networkhashps", getnetworkhashps().HandleRequest(request));
obj.pushKV("pooledtx", (uint64_t)mempool.size());
obj.pushKV("chain", chainman.GetParams().GetChainTypeString());
obj.pushKV("warnings", GetNodeWarnings(IsDeprecatedRPCEnabled("warnings")));
obj.pushKV("warnings", GetNodeWarnings(*CHECK_NONFATAL(node.warnings), IsDeprecatedRPCEnabled("warnings")));
return obj;
},
};
Expand Down
2 changes: 1 addition & 1 deletion src/rpc/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ static RPCHelpMan getnetworkinfo()
}
}
obj.pushKV("localaddresses", localAddresses);
obj.pushKV("warnings", GetNodeWarnings(IsDeprecatedRPCEnabled("warnings")));
obj.pushKV("warnings", GetNodeWarnings(*CHECK_NONFATAL(node.warnings), IsDeprecatedRPCEnabled("warnings")));
return obj;
},
};
Expand Down
12 changes: 6 additions & 6 deletions src/rpc/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1361,16 +1361,16 @@ void PushWarnings(const std::vector<bilingual_str>& warnings, UniValue& obj)
obj.pushKV("warnings", BilingualStringsToUniValue(warnings));
}

UniValue GetNodeWarnings(bool use_deprecated)
UniValue GetNodeWarnings(const node::Warnings& warnings, bool use_deprecated)
{
if (use_deprecated) {
const auto all_warnings{GetWarnings()};
const auto all_warnings{warnings.GetMessages()};
return all_warnings.empty() ? "" : all_warnings.back().original;
}

UniValue warnings{UniValue::VARR};
for (auto&& warning : node::g_warnings.GetMessages()) {
warnings.push_back(std::move(warning.original));
UniValue messages{UniValue::VARR};
for (auto&& warning : warnings.GetMessages()) {
messages.push_back(std::move(warning.original));
}
return warnings;
return messages;
}
7 changes: 6 additions & 1 deletion src/rpc/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ enum class TransactionError;
struct FlatSigningProvider;
struct bilingual_str;

namespace node
{
class Warnings;
} // namespace node

static constexpr bool DEFAULT_RPC_DOC_CHECK{
#ifdef RPC_DOC_CHECK
true
Expand Down Expand Up @@ -513,6 +518,6 @@ class RPCHelpMan
void PushWarnings(const UniValue& warnings, UniValue& obj);
void PushWarnings(const std::vector<bilingual_str>& warnings, UniValue& obj);

UniValue GetNodeWarnings(bool use_deprecated);
UniValue GetNodeWarnings(const node::Warnings& warnings, bool use_deprecated);

#endif // BITCOIN_RPC_UTIL_H
Loading

0 comments on commit 5b8f68c

Please sign in to comment.