0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-24 12:41:41 -05:00
bitcoin-bitcoin-core/src/node/warnings.cpp
Ava Chow 6af51e8198 Use WITH_LOCK in Warnings::Set
The scope of the lock should be limited to just guarding m_warnings as
anything listening on `NotifyAlertChanged` may execute code that
requires the lock as well.
2024-07-06 13:00:53 -04:00

67 lines
1.9 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 <node/interface_ui.h>
#include <sync.h>
#include <univalue.h>
#include <util/translation.h>
#include <utility>
#include <vector>
namespace node {
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)
{
const auto& [_, inserted]{WITH_LOCK(m_mutex, return m_warnings.insert({id, std::move(message)}))};
if (inserted) uiInterface.NotifyAlertChanged();
return inserted;
}
bool Warnings::Unset(warning_type id)
{
auto success{WITH_LOCK(m_mutex, return m_warnings.erase(id))};
if (success) uiInterface.NotifyAlertChanged();
return success;
}
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(const Warnings& warnings, bool use_deprecated)
{
if (use_deprecated) {
const auto all_messages{warnings.GetMessages()};
return all_messages.empty() ? "" : all_messages.back().original;
}
UniValue messages{UniValue::VARR};
for (auto&& message : warnings.GetMessages()) {
messages.push_back(std::move(message.original));
}
return messages;
}
} // namespace node