7#include <bitcoin-build-config.h>
28 CONFIG_FILE_NETWORK_SECTION,
29 CONFIG_FILE_DEFAULT_SECTION
33const std::string SETTINGS_WARN_MSG_KEY{
"_warning_"};
41static void MergeSettings(
const Settings& settings,
const std::string& section,
const std::string&
name, Fn&& fn)
44 if (
auto* value =
FindKey(settings.forced_settings,
name)) {
45 fn(SettingsSpan(*value), Source::FORCED);
49 fn(SettingsSpan(*
values), Source::COMMAND_LINE);
53 fn(SettingsSpan(*value), Source::RW_SETTINGS);
56 if (!section.empty()) {
57 if (
auto* map =
FindKey(settings.ro_config, section)) {
59 fn(SettingsSpan(*
values), Source::CONFIG_FILE_NETWORK_SECTION);
64 if (
auto* map =
FindKey(settings.ro_config,
"")) {
66 fn(SettingsSpan(*
values), Source::CONFIG_FILE_DEFAULT_SECTION);
82 if (!file.is_open()) {
88 if (!in.
read(std::string{std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>()})) {
89 errors.emplace_back(
strprintf(
"Settings file %s does not contain valid JSON. This is probably caused by disk corruption or a crash, "
90 "and can be fixed by removing the file, which will reset settings to default values.",
106 const std::vector<std::string>& in_keys = in.
getKeys();
107 const std::vector<SettingsValue>& in_values = in.
getValues();
108 for (
size_t i = 0; i < in_keys.size(); ++i) {
109 auto inserted =
values.emplace(in_keys[i], in_values[i]);
110 if (!inserted.second) {
118 values.erase(SETTINGS_WARN_MSG_KEY);
120 return errors.empty();
124 const std::map<std::string, SettingsValue>&
values,
125 std::vector<std::string>& errors)
129 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 "
130 "is running, as any changes might be ignored or overwritten.", CLIENT_NAME));
132 for (
const auto& value :
values) {
133 out.pushKVEnd(value.first, value.second);
141 file <<
out.write( 4, 1) << std::endl;
147 const std::string& section,
148 const std::string&
name,
149 bool ignore_default_section_config,
150 bool ignore_nonpersistent,
160 const bool never_ignore_negated_setting = span.
last_negated();
167 const bool reverse_precedence =
168 (
source == Source::CONFIG_FILE_NETWORK_SECTION ||
source == Source::CONFIG_FILE_DEFAULT_SECTION) &&
176 const bool skip_negated_command_line = get_chain_type;
181 if (ignore_default_section_config &&
source == Source::CONFIG_FILE_DEFAULT_SECTION &&
182 !never_ignore_negated_setting) {
187 if (ignore_nonpersistent && (
source == Source::COMMAND_LINE ||
source == Source::FORCED))
return;
190 if (skip_negated_command_line && span.
last_negated())
return;
193 result = reverse_precedence ? span.
begin()[0] : span.
end()[-1];
204 const std::string& section,
205 const std::string&
name,
206 bool ignore_default_section_config)
208 std::vector<SettingsValue> result;
210 bool prev_negated_empty =
false;
219 const bool add_zombie_config_values =
220 (
source == Source::CONFIG_FILE_NETWORK_SECTION ||
source == Source::CONFIG_FILE_DEFAULT_SECTION) &&
224 if (ignore_default_section_config &&
source == Source::CONFIG_FILE_DEFAULT_SECTION)
return;
228 if (!done || add_zombie_config_values) {
229 for (
const auto& value : span) {
230 if (value.isArray()) {
231 result.insert(result.end(), value.getValues().begin(), value.getValues().end());
233 result.push_back(value);
243 prev_negated_empty |= span.
last_negated() && result.empty();
250 bool has_default_section_setting =
false;
251 bool has_other_setting =
false;
253 if (span.
empty())
return;
254 else if (
source == Source::CONFIG_FILE_DEFAULT_SECTION) has_default_section_setting =
true;
255 else has_other_setting =
true;
260 return has_default_section_setting && !has_other_setting;
270 for (
size_t i =
size; i > 0; --i) {
271 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)
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
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 bool exists(const path &p)
static std::string PathToString(const path &path)
Convert path object to a byte string.
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