Branch data Line data Source code
1 : : // Copyright (c) 2019-present The Bitcoin Core developers
2 : : // Distributed under the MIT software license, see the accompanying
3 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 : :
5 : : #ifndef BITCOIN_COMMON_SETTINGS_H
6 : : #define BITCOIN_COMMON_SETTINGS_H
7 : :
8 : : #include <util/fs.h>
9 : :
10 : : #include <cstddef>
11 : : #include <map>
12 : : #include <string>
13 : : #include <vector>
14 : :
15 : : // Users of this header need to explicitly #include <univalue.h>
16 : : // IWYU pragma: no_include <univalue.h>
17 : : class UniValue; // IWYU pragma: keep
18 : :
19 : : namespace common {
20 : :
21 : : //! Settings value type (string/integer/boolean/null variant).
22 : : //!
23 : : //! @note UniValue is used here for convenience and because it can be easily
24 : : //! serialized in a readable format. But any other variant type that can
25 : : //! be assigned strings, int64_t, and bool values and has get_str(),
26 : : //! getInt<int64_t>(), get_bool(), isNum(), isBool(), isFalse(), isTrue() and
27 : : //! isNull() methods can be substituted if there's a need to move away
28 : : //! from UniValue. (An implementation with boost::variant was posted at
29 : : //! https://github.com/bitcoin/bitcoin/pull/15934/files#r337691812)
30 : : using SettingsValue = UniValue;
31 : :
32 : : //! Stored settings. This struct combines settings from the command line, a
33 : : //! read-only configuration file, and a read-write runtime settings file.
34 : : struct Settings {
35 : : //! Map of setting name to forced setting value.
36 : : std::map<std::string, SettingsValue> forced_settings;
37 : : //! Map of setting name to list of command line values.
38 : : std::map<std::string, std::vector<SettingsValue>> command_line_options;
39 : : //! Map of setting name to read-write file setting value.
40 : : std::map<std::string, SettingsValue> rw_settings;
41 : : //! Map of config section name and setting name to list of config file values.
42 : : std::map<std::string, std::map<std::string, std::vector<SettingsValue>>> ro_config;
43 : : };
44 : :
45 : : //! Read settings file.
46 : : bool ReadSettings(const fs::path& path,
47 : : std::map<std::string, SettingsValue>& values,
48 : : std::vector<std::string>& errors);
49 : :
50 : : //! Write settings file.
51 : : bool WriteSettings(const fs::path& path,
52 : : const std::map<std::string, SettingsValue>& values,
53 : : std::vector<std::string>& errors);
54 : :
55 : : //! Get settings value from combined sources: forced settings, command line
56 : : //! arguments, runtime read-write settings, and the read-only config file.
57 : : //!
58 : : //! @param ignore_default_section_config - ignore values in the default section
59 : : //! of the config file (part before any
60 : : //! [section] keywords)
61 : : //! @param ignore_nonpersistent - ignore non-persistent settings values (forced
62 : : //! settings values and values specified on the
63 : : //! command line). Only return settings in the
64 : : //! read-only config and read-write settings
65 : : //! files.
66 : : //! @param get_chain_type - enable special backwards compatible behavior
67 : : //! for GetChainType
68 : : SettingsValue GetSetting(const Settings& settings,
69 : : const std::string& section,
70 : : const std::string& name,
71 : : bool ignore_default_section_config,
72 : : bool ignore_nonpersistent,
73 : : bool get_chain_type);
74 : :
75 : : //! Get combined setting value similar to GetSetting(), except if setting was
76 : : //! specified multiple times, return a list of all the values specified.
77 : : std::vector<SettingsValue> GetSettingsList(const Settings& settings,
78 : : const std::string& section,
79 : : const std::string& name,
80 : : bool ignore_default_section_config);
81 : :
82 : : //! Return true if a setting is set in the default config file section, and not
83 : : //! overridden by a higher priority command-line or network section value.
84 : : //!
85 : : //! This is used to provide user warnings about values that might be getting
86 : : //! ignored unintentionally.
87 : : bool OnlyHasDefaultSectionSetting(const Settings& settings, const std::string& section, const std::string& name);
88 : :
89 : : //! Accessor for list of settings that skips negated values when iterated over.
90 : : //! The last boolean `false` value in the list and all earlier values are
91 : : //! considered negated.
92 : : struct SettingsSpan {
93 : : explicit SettingsSpan() = default;
94 : 110646 : explicit SettingsSpan(const SettingsValue& value) noexcept : SettingsSpan(&value, 1) {}
95 : 110646 : explicit SettingsSpan(const SettingsValue* data, size_t size) noexcept : data(data), size(size) {}
96 : : explicit SettingsSpan(const std::vector<SettingsValue>& vec) noexcept;
97 : : const SettingsValue* begin() const; //!< Pointer to first non-negated value.
98 : : const SettingsValue* end() const; //!< Pointer to end of values.
99 : : bool empty() const; //!< True if there are any non-negated values.
100 : : bool last_negated() const; //!< True if the last value is negated.
101 : : size_t negated() const; //!< Number of negated values.
102 : :
103 : : const SettingsValue* data = nullptr;
104 : : size_t size = 0;
105 : : };
106 : :
107 : : //! Map lookup helper.
108 : : template <typename Map, typename Key>
109 : 2088003 : auto FindKey(Map&& map, Key&& key) -> decltype(&map.at(key))
110 : : {
111 [ + + ]: 2489871 : auto it = map.find(key);
112 [ + + ]: 2088003 : return it == map.end() ? nullptr : &it->second;
113 : : }
114 : :
115 : : } // namespace common
116 : :
117 : : #endif // BITCOIN_COMMON_SETTINGS_H
|