mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-24 12:41:41 -05:00

Instead of having separate warning functions (and globals) for each different warning that can be raised, encapsulate this logic into a single class and allow to (un)set any number of warnings. Introduces behaviour change: - the `-alertnotify` command is executed for all `KernelNotifications::warningSet` calls, which now also covers the `LARGE_WORK_INVALID_CHAIN` warning. - previously, warnings were returned based on a predetermined order, e.g. with the "pre-release test build" warning always first. This is no longer the case, and Warnings::GetMessages() will return messages sorted by the id of the warning. Removes warnings.cpp from kernel.
66 lines
1.7 KiB
C++
66 lines
1.7 KiB
C++
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
|
// Copyright (c) 2009-2022 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 <config/bitcoin-config.h> // IWYU pragma: keep
|
|
|
|
#include <node/warnings.h>
|
|
|
|
#include <common/system.h>
|
|
#include <sync.h>
|
|
#include <univalue.h>
|
|
#include <util/translation.h>
|
|
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
namespace node {
|
|
Warnings g_warnings;
|
|
|
|
Warnings::Warnings()
|
|
{
|
|
// Pre-release build warning
|
|
if (!CLIENT_VERSION_IS_RELEASE) {
|
|
m_warnings.insert(
|
|
{Warning::PRE_RELEASE_TEST_BUILD,
|
|
_("This is a pre-release test build - use at your own risk - do not use for mining or merchant applications")});
|
|
}
|
|
}
|
|
bool Warnings::Set(warning_type id, bilingual_str message)
|
|
{
|
|
LOCK(m_mutex);
|
|
const auto& [_, inserted]{m_warnings.insert({id, std::move(message)})};
|
|
return inserted;
|
|
}
|
|
|
|
bool Warnings::Unset(warning_type id)
|
|
{
|
|
return WITH_LOCK(m_mutex, return m_warnings.erase(id));
|
|
}
|
|
|
|
std::vector<bilingual_str> Warnings::GetMessages() const
|
|
{
|
|
LOCK(m_mutex);
|
|
std::vector<bilingual_str> messages;
|
|
messages.reserve(m_warnings.size());
|
|
for (const auto& [id, msg] : m_warnings) {
|
|
messages.push_back(msg);
|
|
}
|
|
return messages;
|
|
}
|
|
|
|
UniValue GetWarningsForRpc(bool use_deprecated)
|
|
{
|
|
if (use_deprecated) {
|
|
const auto all_warnings{g_warnings.GetMessages()};
|
|
return all_warnings.empty() ? "" : all_warnings.back().original;
|
|
}
|
|
|
|
UniValue warnings{UniValue::VARR};
|
|
for (auto&& warning : g_warnings.GetMessages()) {
|
|
warnings.push_back(std::move(warning.original));
|
|
}
|
|
return warnings;
|
|
}
|
|
} // namespace node
|