warnings.cpp raw
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2022 The Limenka developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6 #include <limenka-build-config.h> // IWYU pragma: keep
7
8 #include <node/warnings.h>
9
10 #include <common/system.h>
11 #include <node/interface_ui.h>
12 #include <sync.h>
13 #include <univalue.h>
14 #include <util/string.h>
15 #include <util/translation.h>
16
17 #include <utility>
18 #include <vector>
19
20 namespace node {
21 Warnings::Warnings()
22 {
23 // Pre-release build warning
24 if (!CLIENT_VERSION_IS_RELEASE) {
25 m_warnings.insert(
26 {Warning::PRE_RELEASE_TEST_BUILD,
27 _("This is a pre-release test build - use at your own risk - do not use for mining or merchant applications")});
28 }
29 }
30 bool Warnings::Set(warning_type id, bilingual_str message, const bool update)
31 {
32 bool inserted{false};
33 if (update) {
34 LOCK(m_mutex);
35 auto& warning_msg = m_warnings[id];
36 if (warning_msg.original != message.original) {
37 warning_msg = message;
38 inserted = true;
39 }
40 } else {
41 const auto& [_, inserted_res]{WITH_LOCK(m_mutex, return m_warnings.insert({id, std::move(message)}))};
42 inserted = inserted_res;
43 }
44 if (inserted) uiInterface.NotifyAlertChanged();
45 return inserted;
46 }
47
48 bool Warnings::Unset(warning_type id)
49 {
50 auto success{WITH_LOCK(m_mutex, return m_warnings.erase(id))};
51 if (success) uiInterface.NotifyAlertChanged();
52 return success;
53 }
54
55 std::vector<bilingual_str> Warnings::GetMessages() const
56 {
57 LOCK(m_mutex);
58 std::vector<bilingual_str> messages;
59 messages.reserve(m_warnings.size());
60 for (const auto& [id, msg] : m_warnings) {
61 messages.push_back(msg);
62 }
63 return messages;
64 }
65
66 UniValue GetWarningsForRpc(const Warnings& warnings, bool use_deprecated)
67 {
68 if (use_deprecated) {
69 const auto all_messages{warnings.GetMessages()};
70 return all_messages.empty() ? "" : util::Join(all_messages, Untranslated("\n")).original;
71 }
72
73 UniValue messages{UniValue::VARR};
74 for (auto&& message : warnings.GetMessages()) {
75 messages.push_back(std::move(message.original));
76 }
77 return messages;
78 }
79 } // namespace node
80