5#include <bitcoin-build-config.h>
27 CONFIG_FILE_NETWORK_SECTION,
28 CONFIG_FILE_DEFAULT_SECTION
32const std::string SETTINGS_WARN_MSG_KEY{
"_warning_"};
40static void MergeSettings(
const Settings& settings,
const std::string& section,
const std::string&
name, Fn&& fn)
43 if (
auto* value =
FindKey(settings.forced_settings,
name)) {
44 fn(SettingsSpan(*value), Source::FORCED);
48 fn(SettingsSpan(*
values), Source::COMMAND_LINE);
52 fn(SettingsSpan(*value), Source::RW_SETTINGS);
55 if (!section.empty()) {
56 if (
auto* map =
FindKey(settings.ro_config, section)) {
58 fn(SettingsSpan(*
values), Source::CONFIG_FILE_NETWORK_SECTION);
63 if (
auto* map =
FindKey(settings.ro_config,
"")) {
65 fn(SettingsSpan(*
values), Source::CONFIG_FILE_DEFAULT_SECTION);
71bool ReadSettings(
const fs::path& path, std::map<std::string, SettingsValue>&
values, std::vector<std::string>& errors)
80 file.open(path.std_path());
81 if (!file.is_open()) {
87 if (!in.
read(std::string{std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>()})) {
88 errors.emplace_back(
strprintf(
"Settings file %s does not contain valid JSON. This may be caused by a crash, power loss, full disk, or storage error, "
89 "and can be fixed by removing the file, which will reset settings to default values.",
105 const std::vector<std::string>& in_keys = in.
getKeys();
106 const std::vector<SettingsValue>& in_values = in.
getValues();
107 for (
size_t i = 0; i < in_keys.size(); ++i) {
108 auto inserted =
values.emplace(in_keys[i], in_values[i]);
109 if (!inserted.second) {
117 values.erase(SETTINGS_WARN_MSG_KEY);
119 return errors.empty();
123 const std::map<std::string, SettingsValue>&
values,
124 std::vector<std::string>& errors)
128 out.pushKV(SETTINGS_WARN_MSG_KEY,
strprintf(
"This file is automatically generated and updated by %s. Please do not edit this file while the node "
129 "is running, as any changes might be ignored or overwritten.", CLIENT_NAME));
131 for (
const auto& value :
values) {
132 out.pushKVEnd(value.first, value.second);
135 file.open(path.std_path());
140 file <<
out.write( 4, 1) << std::endl;
154 const std::string& section,
155 const std::string&
name,
156 bool ignore_default_section_config,
157 bool ignore_nonpersistent,
167 const bool never_ignore_negated_setting = span.
last_negated();
174 const bool reverse_precedence =
175 (
source == Source::CONFIG_FILE_NETWORK_SECTION ||
source == Source::CONFIG_FILE_DEFAULT_SECTION) &&
183 const bool skip_negated_command_line = get_chain_type;
188 if (ignore_default_section_config &&
source == Source::CONFIG_FILE_DEFAULT_SECTION &&
189 !never_ignore_negated_setting) {
194 if (ignore_nonpersistent && (
source == Source::COMMAND_LINE ||
source == Source::FORCED))
return;
197 if (skip_negated_command_line && span.
last_negated())
return;
200 result = reverse_precedence ? span.
begin()[0] : span.
end()[-1];
211 const std::string& section,
212 const std::string&
name,
213 bool ignore_default_section_config)
215 std::vector<SettingsValue> result;
217 bool prev_negated_empty =
false;
226 const bool add_zombie_config_values =
227 (
source == Source::CONFIG_FILE_NETWORK_SECTION ||
source == Source::CONFIG_FILE_DEFAULT_SECTION) &&
231 if (ignore_default_section_config &&
source == Source::CONFIG_FILE_DEFAULT_SECTION)
return;
235 if (!done || add_zombie_config_values) {
236 for (
const auto& value : span) {
237 if (value.isArray()) {
238 result.insert(result.end(), value.getValues().begin(), value.getValues().end());
240 result.push_back(value);
250 prev_negated_empty |= span.
last_negated() && result.empty();
257 bool has_default_section_setting =
false;
258 bool has_other_setting =
false;
260 if (span.
empty())
return;
261 else if (
source == Source::CONFIG_FILE_DEFAULT_SECTION) has_default_section_setting =
true;
262 else has_other_setting =
true;
267 return has_default_section_setting && !has_other_setting;
277 for (
size_t i =
size; i > 0; --i) {
278 if (
data[i - 1].isFalse())
return i;
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
const std::vector< UniValue > & getValues() const
const std::vector< std::string > & getKeys() const
bool read(std::string_view raw)
static bool exists(const path &p)
static std::string PathToString(const path &path)
Convert path object to a byte string.
bool WriteSettings(const fs::path &path, const std::map< std::string, SettingsValue > &values, std::vector< std::string > &errors)
Write settings file.
bool ReadSettings(const fs::path &path, std::map< std::string, SettingsValue > &values, std::vector< std::string > &errors)
Read settings file.
SettingsValue GetSetting(const Settings &settings, const std::string §ion, const std::string &name, bool ignore_default_section_config, bool ignore_nonpersistent, bool get_chain_type)
Get settings value from combined sources: forced settings, command line arguments,...
auto FindKey(Map &&map, Key &&key) -> decltype(&map.at(key))
Map lookup helper.
std::vector< SettingsValue > GetSettingsList(const Settings &settings, const std::string §ion, const std::string &name, bool ignore_default_section_config)
Get combined setting value similar to GetSetting(), except if setting was specified multiple times,...
bool OnlyHasDefaultSectionSetting(const Settings &settings, const std::string §ion, const std::string &name)
Return true if a setting is set in the default config file section, and not overridden by a higher pr...
UniValue SettingsValue
Settings value type (string/integer/boolean/null variant).
static const int64_t values[]
A selection of numbers that do not trigger int64_t overflow when added/subtracted.
Accessor for list of settings that skips negated values when iterated over.
bool last_negated() const
True if the last value is negated.
const SettingsValue * begin() const
Pointer to first non-negated value.
const SettingsValue * end() const
Pointer to end of values.
bool empty() const
True if there are any non-negated values.
size_t negated() const
Number of negated values.
const SettingsValue * data