/Users/brunogarcia/projects/bitcoin-core-dev/src/common/args.cpp
Line | Count | Source |
1 | | // Copyright (c) 2009-2010 Satoshi Nakamoto |
2 | | // Copyright (c) 2009-present The Bitcoin Core 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 <common/args.h> |
7 | | |
8 | | #include <chainparamsbase.h> |
9 | | #include <common/settings.h> |
10 | | #include <logging.h> |
11 | | #include <sync.h> |
12 | | #include <tinyformat.h> |
13 | | #include <univalue.h> |
14 | | #include <util/chaintype.h> |
15 | | #include <util/check.h> |
16 | | #include <util/fs.h> |
17 | | #include <util/fs_helpers.h> |
18 | | #include <util/strencodings.h> |
19 | | #include <util/string.h> |
20 | | |
21 | | #ifdef WIN32 |
22 | | #include <shlobj.h> |
23 | | #endif |
24 | | |
25 | | #include <algorithm> |
26 | | #include <cassert> |
27 | | #include <cstdint> |
28 | | #include <cstdlib> |
29 | | #include <cstring> |
30 | | #include <map> |
31 | | #include <optional> |
32 | | #include <stdexcept> |
33 | | #include <string> |
34 | | #include <utility> |
35 | | #include <variant> |
36 | | |
37 | | const char * const BITCOIN_CONF_FILENAME = "bitcoin.conf"; |
38 | | const char * const BITCOIN_SETTINGS_FILENAME = "settings.json"; |
39 | | |
40 | | ArgsManager gArgs; |
41 | | |
42 | | /** |
43 | | * Interpret a string argument as a boolean. |
44 | | * |
45 | | * The definition of LocaleIndependentAtoi<int>() requires that non-numeric string values |
46 | | * like "foo", return 0. This means that if a user unintentionally supplies a |
47 | | * non-integer argument here, the return value is always false. This means that |
48 | | * -foo=false does what the user probably expects, but -foo=true is well defined |
49 | | * but does not do what they probably expected. |
50 | | * |
51 | | * The return value of LocaleIndependentAtoi<int>(...) is zero when given input not |
52 | | * representable as an int. |
53 | | * |
54 | | * For a more extensive discussion of this topic (and a wide range of opinions |
55 | | * on the Right Way to change this code), see PR12713. |
56 | | */ |
57 | | static bool InterpretBool(const std::string& strValue) |
58 | 0 | { |
59 | 0 | if (strValue.empty()) |
60 | 0 | return true; |
61 | 0 | return (LocaleIndependentAtoi<int>(strValue) != 0); |
62 | 0 | } |
63 | | |
64 | | static std::string SettingName(const std::string& arg) |
65 | 148k | { |
66 | 148k | return arg.size() > 0 && arg[0] == '-' ? arg.substr(1) : arg0 ; |
67 | 148k | } |
68 | | |
69 | | /** |
70 | | * Parse "name", "section.name", "noname", "section.noname" settings keys. |
71 | | * |
72 | | * @note Where an option was negated can be later checked using the |
73 | | * IsArgNegated() method. One use case for this is to have a way to disable |
74 | | * options that are not normally boolean (e.g. using -nodebuglogfile to request |
75 | | * that debug log output is not sent to any file at all). |
76 | | */ |
77 | | KeyInfo InterpretKey(std::string key) |
78 | 0 | { |
79 | 0 | KeyInfo result; |
80 | | // Split section name from key name for keys like "testnet.foo" or "regtest.bar" |
81 | 0 | size_t option_index = key.find('.'); |
82 | 0 | if (option_index != std::string::npos) { |
83 | 0 | result.section = key.substr(0, option_index); |
84 | 0 | key.erase(0, option_index + 1); |
85 | 0 | } |
86 | 0 | if (key.starts_with("no")) { |
87 | 0 | key.erase(0, 2); |
88 | 0 | result.negated = true; |
89 | 0 | } |
90 | 0 | result.name = key; |
91 | 0 | return result; |
92 | 0 | } |
93 | | |
94 | | /** |
95 | | * Interpret settings value based on registered flags. |
96 | | * |
97 | | * @param[in] key key information to know if key was negated |
98 | | * @param[in] value string value of setting to be parsed |
99 | | * @param[in] flags ArgsManager registered argument flags |
100 | | * @param[out] error Error description if settings value is not valid |
101 | | * |
102 | | * @return parsed settings value if it is valid, otherwise nullopt accompanied |
103 | | * by a descriptive error string |
104 | | */ |
105 | | std::optional<common::SettingsValue> InterpretValue(const KeyInfo& key, const std::string* value, |
106 | | unsigned int flags, std::string& error) |
107 | 0 | { |
108 | | // Return negated settings as false values. |
109 | 0 | if (key.negated) { |
110 | 0 | if (flags & ArgsManager::DISALLOW_NEGATION) { |
111 | 0 | error = strprintf("Negating of -%s is meaningless and therefore forbidden", key.name);Line | Count | Source | 1172 | 0 | #define strprintf tfm::format |
|
112 | 0 | return std::nullopt; |
113 | 0 | } |
114 | | // Double negatives like -nofoo=0 are supported (but discouraged) |
115 | 0 | if (value && !InterpretBool(*value)) { |
116 | 0 | LogPrintf("Warning: parsed potentially confusing double-negative -%s=%s\n", key.name, *value);Line | Count | Source | 373 | 0 | #define LogPrintf(...) LogInfo(__VA_ARGS__) Line | Count | Source | 368 | 0 | #define LogInfo(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Info, /*should_ratelimit=*/true, __VA_ARGS__) Line | Count | Source | 362 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(std::source_location::current(), category, level, should_ratelimit, __VA_ARGS__) |
|
|
|
117 | 0 | return true; |
118 | 0 | } |
119 | 0 | return false; |
120 | 0 | } |
121 | 0 | if (!value && (flags & ArgsManager::DISALLOW_ELISION)) { |
122 | 0 | error = strprintf("Can not set -%s with no value. Please specify value with -%s=value.", key.name, key.name);Line | Count | Source | 1172 | 0 | #define strprintf tfm::format |
|
123 | 0 | return std::nullopt; |
124 | 0 | } |
125 | 0 | return value ? *value : ""; |
126 | 0 | } |
127 | | |
128 | | // Define default constructor and destructor that are not inline, so code instantiating this class doesn't need to |
129 | | // #include class definitions for all members. |
130 | | // For example, m_settings has an internal dependency on univalue. |
131 | 0 | ArgsManager::ArgsManager() = default; |
132 | 1 | ArgsManager::~ArgsManager() = default; |
133 | | |
134 | | std::set<std::string> ArgsManager::GetUnsuitableSectionOnlyArgs() const |
135 | 0 | { |
136 | 0 | std::set<std::string> unsuitables; |
137 | |
|
138 | 0 | LOCK(cs_args); Line | Count | Source | 259 | 0 | #define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__) Line | Count | Source | 11 | 0 | #define UNIQUE_NAME(name) PASTE2(name, __COUNTER__) Line | Count | Source | 9 | 0 | #define PASTE2(x, y) PASTE(x, y) Line | Count | Source | 8 | 0 | #define PASTE(x, y) x ## y |
|
|
|
|
139 | | |
140 | | // if there's no section selected, don't worry |
141 | 0 | if (m_network.empty()) return std::set<std::string> {}; |
142 | | |
143 | | // if it's okay to use the default section for this network, don't worry |
144 | 0 | if (m_network == ChainTypeToString(ChainType::MAIN)) return std::set<std::string> {}; |
145 | | |
146 | 0 | for (const auto& arg : m_network_only_args) { |
147 | 0 | if (OnlyHasDefaultSectionSetting(m_settings, m_network, SettingName(arg))) { |
148 | 0 | unsuitables.insert(arg); |
149 | 0 | } |
150 | 0 | } |
151 | 0 | return unsuitables; |
152 | 0 | } |
153 | | |
154 | | std::list<SectionInfo> ArgsManager::GetUnrecognizedSections() const |
155 | 0 | { |
156 | | // Section names to be recognized in the config file. |
157 | 0 | static const std::set<std::string> available_sections{ |
158 | 0 | ChainTypeToString(ChainType::REGTEST), |
159 | 0 | ChainTypeToString(ChainType::SIGNET), |
160 | 0 | ChainTypeToString(ChainType::TESTNET), |
161 | 0 | ChainTypeToString(ChainType::TESTNET4), |
162 | 0 | ChainTypeToString(ChainType::MAIN), |
163 | 0 | }; |
164 | |
|
165 | 0 | LOCK(cs_args); Line | Count | Source | 259 | 0 | #define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__) Line | Count | Source | 11 | 0 | #define UNIQUE_NAME(name) PASTE2(name, __COUNTER__) Line | Count | Source | 9 | 0 | #define PASTE2(x, y) PASTE(x, y) Line | Count | Source | 8 | 0 | #define PASTE(x, y) x ## y |
|
|
|
|
166 | 0 | std::list<SectionInfo> unrecognized = m_config_sections; |
167 | 0 | unrecognized.remove_if([](const SectionInfo& appeared){ return available_sections.find(appeared.m_name) != available_sections.end(); }); |
168 | 0 | return unrecognized; |
169 | 0 | } |
170 | | |
171 | | void ArgsManager::SelectConfigNetwork(const std::string& network) |
172 | 0 | { |
173 | 0 | LOCK(cs_args); Line | Count | Source | 259 | 0 | #define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__) Line | Count | Source | 11 | 0 | #define UNIQUE_NAME(name) PASTE2(name, __COUNTER__) Line | Count | Source | 9 | 0 | #define PASTE2(x, y) PASTE(x, y) Line | Count | Source | 8 | 0 | #define PASTE(x, y) x ## y |
|
|
|
|
174 | 0 | m_network = network; |
175 | 0 | } |
176 | | |
177 | | bool ArgsManager::ParseParameters(int argc, const char* const argv[], std::string& error) |
178 | 0 | { |
179 | 0 | LOCK(cs_args); Line | Count | Source | 259 | 0 | #define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__) Line | Count | Source | 11 | 0 | #define UNIQUE_NAME(name) PASTE2(name, __COUNTER__) Line | Count | Source | 9 | 0 | #define PASTE2(x, y) PASTE(x, y) Line | Count | Source | 8 | 0 | #define PASTE(x, y) x ## y |
|
|
|
|
180 | 0 | m_settings.command_line_options.clear(); |
181 | |
|
182 | 0 | for (int i = 1; i < argc; i++) { |
183 | 0 | std::string key(argv[i]); |
184 | |
|
185 | 0 | #ifdef __APPLE__ |
186 | | // At the first time when a user gets the "App downloaded from the |
187 | | // internet" warning, and clicks the Open button, macOS passes |
188 | | // a unique process serial number (PSN) as -psn_... command-line |
189 | | // argument, which we filter out. |
190 | 0 | if (key.starts_with("-psn_")) continue; |
191 | 0 | #endif |
192 | | |
193 | 0 | if (key == "-") break; //bitcoin-tx using stdin |
194 | 0 | std::optional<std::string> val; |
195 | 0 | size_t is_index = key.find('='); |
196 | 0 | if (is_index != std::string::npos) { |
197 | 0 | val = key.substr(is_index + 1); |
198 | 0 | key.erase(is_index); |
199 | 0 | } |
200 | | #ifdef WIN32 |
201 | | key = ToLower(key); |
202 | | if (key[0] == '/') |
203 | | key[0] = '-'; |
204 | | #endif |
205 | |
|
206 | 0 | if (key[0] != '-') { |
207 | 0 | if (!m_accept_any_command && m_command.empty()) { |
208 | | // The first non-dash arg is a registered command |
209 | 0 | std::optional<unsigned int> flags = GetArgFlags(key); |
210 | 0 | if (!flags || !(*flags & ArgsManager::COMMAND)) { |
211 | 0 | error = strprintf("Invalid command '%s'", argv[i]);Line | Count | Source | 1172 | 0 | #define strprintf tfm::format |
|
212 | 0 | return false; |
213 | 0 | } |
214 | 0 | } |
215 | 0 | m_command.push_back(key); |
216 | 0 | while (++i < argc) { |
217 | | // The remaining args are command args |
218 | 0 | m_command.emplace_back(argv[i]); |
219 | 0 | } |
220 | 0 | break; |
221 | 0 | } |
222 | | |
223 | | // Transform --foo to -foo |
224 | 0 | if (key.length() > 1 && key[1] == '-') |
225 | 0 | key.erase(0, 1); |
226 | | |
227 | | // Transform -foo to foo |
228 | 0 | key.erase(0, 1); |
229 | 0 | KeyInfo keyinfo = InterpretKey(key); |
230 | 0 | std::optional<unsigned int> flags = GetArgFlags('-' + keyinfo.name); |
231 | | |
232 | | // Unknown command line options and command line options with dot |
233 | | // characters (which are returned from InterpretKey with nonempty |
234 | | // section strings) are not valid. |
235 | 0 | if (!flags || !keyinfo.section.empty()) { |
236 | 0 | error = strprintf("Invalid parameter %s", argv[i]);Line | Count | Source | 1172 | 0 | #define strprintf tfm::format |
|
237 | 0 | return false; |
238 | 0 | } |
239 | | |
240 | 0 | std::optional<common::SettingsValue> value = InterpretValue(keyinfo, val ? &*val : nullptr, *flags, error); |
241 | 0 | if (!value) return false; |
242 | | |
243 | 0 | m_settings.command_line_options[keyinfo.name].push_back(*value); |
244 | 0 | } |
245 | | |
246 | | // we do not allow -includeconf from command line, only -noincludeconf |
247 | 0 | if (auto* includes = common::FindKey(m_settings.command_line_options, "includeconf")) { |
248 | 0 | const common::SettingsSpan values{*includes}; |
249 | | // Range may be empty if -noincludeconf was passed |
250 | 0 | if (!values.empty()) { |
251 | 0 | error = "-includeconf cannot be used from commandline; -includeconf=" + values.begin()->write(); |
252 | 0 | return false; // pick first value as example |
253 | 0 | } |
254 | 0 | } |
255 | 0 | return true; |
256 | 0 | } |
257 | | |
258 | | std::optional<unsigned int> ArgsManager::GetArgFlags(const std::string& name) const |
259 | 0 | { |
260 | 0 | LOCK(cs_args); Line | Count | Source | 259 | 0 | #define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__) Line | Count | Source | 11 | 0 | #define UNIQUE_NAME(name) PASTE2(name, __COUNTER__) Line | Count | Source | 9 | 0 | #define PASTE2(x, y) PASTE(x, y) Line | Count | Source | 8 | 0 | #define PASTE(x, y) x ## y |
|
|
|
|
261 | 0 | for (const auto& arg_map : m_available_args) { |
262 | 0 | const auto search = arg_map.second.find(name); |
263 | 0 | if (search != arg_map.second.end()) { |
264 | 0 | return search->second.m_flags; |
265 | 0 | } |
266 | 0 | } |
267 | 0 | return m_default_flags; |
268 | 0 | } |
269 | | |
270 | | void ArgsManager::SetDefaultFlags(std::optional<unsigned int> flags) |
271 | 0 | { |
272 | 0 | LOCK(cs_args); Line | Count | Source | 259 | 0 | #define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__) Line | Count | Source | 11 | 0 | #define UNIQUE_NAME(name) PASTE2(name, __COUNTER__) Line | Count | Source | 9 | 0 | #define PASTE2(x, y) PASTE(x, y) Line | Count | Source | 8 | 0 | #define PASTE(x, y) x ## y |
|
|
|
|
273 | 0 | m_default_flags = flags; |
274 | 0 | } |
275 | | |
276 | | fs::path ArgsManager::GetPathArg(std::string arg, const fs::path& default_value) const |
277 | 0 | { |
278 | 0 | if (IsArgNegated(arg)) return fs::path{}; |
279 | 0 | std::string path_str = GetArg(arg, ""); |
280 | 0 | if (path_str.empty()) return default_value; |
281 | 0 | fs::path result = fs::PathFromString(path_str).lexically_normal(); |
282 | | // Remove trailing slash, if present. |
283 | 0 | return result.has_filename() ? result : result.parent_path(); |
284 | 0 | } |
285 | | |
286 | | fs::path ArgsManager::GetBlocksDirPath() const |
287 | 0 | { |
288 | 0 | LOCK(cs_args); Line | Count | Source | 259 | 0 | #define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__) Line | Count | Source | 11 | 0 | #define UNIQUE_NAME(name) PASTE2(name, __COUNTER__) Line | Count | Source | 9 | 0 | #define PASTE2(x, y) PASTE(x, y) Line | Count | Source | 8 | 0 | #define PASTE(x, y) x ## y |
|
|
|
|
289 | 0 | fs::path& path = m_cached_blocks_path; |
290 | | |
291 | | // Cache the path to avoid calling fs::create_directories on every call of |
292 | | // this function |
293 | 0 | if (!path.empty()) return path; |
294 | | |
295 | 0 | if (IsArgSet("-blocksdir")) { |
296 | 0 | path = fs::absolute(GetPathArg("-blocksdir")); |
297 | 0 | if (!fs::is_directory(path)) { |
298 | 0 | path = ""; |
299 | 0 | return path; |
300 | 0 | } |
301 | 0 | } else { |
302 | 0 | path = GetDataDirBase(); |
303 | 0 | } |
304 | | |
305 | 0 | path /= fs::PathFromString(BaseParams().DataDir()); |
306 | 0 | path /= "blocks"; |
307 | 0 | fs::create_directories(path); |
308 | 0 | return path; |
309 | 0 | } |
310 | | |
311 | | fs::path ArgsManager::GetDataDir(bool net_specific) const |
312 | 0 | { |
313 | 0 | LOCK(cs_args); Line | Count | Source | 259 | 0 | #define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__) Line | Count | Source | 11 | 0 | #define UNIQUE_NAME(name) PASTE2(name, __COUNTER__) Line | Count | Source | 9 | 0 | #define PASTE2(x, y) PASTE(x, y) Line | Count | Source | 8 | 0 | #define PASTE(x, y) x ## y |
|
|
|
|
314 | 0 | fs::path& path = net_specific ? m_cached_network_datadir_path : m_cached_datadir_path; |
315 | | |
316 | | // Used cached path if available |
317 | 0 | if (!path.empty()) return path; |
318 | | |
319 | 0 | const fs::path datadir{GetPathArg("-datadir")}; |
320 | 0 | if (!datadir.empty()) { |
321 | 0 | path = fs::absolute(datadir); |
322 | 0 | if (!fs::is_directory(path)) { |
323 | 0 | path = ""; |
324 | 0 | return path; |
325 | 0 | } |
326 | 0 | } else { |
327 | 0 | path = GetDefaultDataDir(); |
328 | 0 | } |
329 | | |
330 | 0 | if (net_specific && !BaseParams().DataDir().empty()) { |
331 | 0 | path /= fs::PathFromString(BaseParams().DataDir()); |
332 | 0 | } |
333 | |
|
334 | 0 | return path; |
335 | 0 | } |
336 | | |
337 | | void ArgsManager::ClearPathCache() |
338 | 0 | { |
339 | 0 | LOCK(cs_args); Line | Count | Source | 259 | 0 | #define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__) Line | Count | Source | 11 | 0 | #define UNIQUE_NAME(name) PASTE2(name, __COUNTER__) Line | Count | Source | 9 | 0 | #define PASTE2(x, y) PASTE(x, y) Line | Count | Source | 8 | 0 | #define PASTE(x, y) x ## y |
|
|
|
|
340 | |
|
341 | 0 | m_cached_datadir_path = fs::path(); |
342 | 0 | m_cached_network_datadir_path = fs::path(); |
343 | 0 | m_cached_blocks_path = fs::path(); |
344 | 0 | } |
345 | | |
346 | | std::optional<const ArgsManager::Command> ArgsManager::GetCommand() const |
347 | 0 | { |
348 | 0 | Command ret; |
349 | 0 | LOCK(cs_args); Line | Count | Source | 259 | 0 | #define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__) Line | Count | Source | 11 | 0 | #define UNIQUE_NAME(name) PASTE2(name, __COUNTER__) Line | Count | Source | 9 | 0 | #define PASTE2(x, y) PASTE(x, y) Line | Count | Source | 8 | 0 | #define PASTE(x, y) x ## y |
|
|
|
|
350 | 0 | auto it = m_command.begin(); |
351 | 0 | if (it == m_command.end()) { |
352 | | // No command was passed |
353 | 0 | return std::nullopt; |
354 | 0 | } |
355 | 0 | if (!m_accept_any_command) { |
356 | | // The registered command |
357 | 0 | ret.command = *(it++); |
358 | 0 | } |
359 | 0 | while (it != m_command.end()) { |
360 | | // The unregistered command and args (if any) |
361 | 0 | ret.args.push_back(*(it++)); |
362 | 0 | } |
363 | 0 | return ret; |
364 | 0 | } |
365 | | |
366 | | std::vector<std::string> ArgsManager::GetArgs(const std::string& strArg) const |
367 | 0 | { |
368 | 0 | std::vector<std::string> result; |
369 | 0 | for (const common::SettingsValue& value : GetSettingsList(strArg)) { |
370 | 0 | result.push_back(value.isFalse() ? "0" : value.isTrue() ? "1" : value.get_str()); |
371 | 0 | } |
372 | 0 | return result; |
373 | 0 | } |
374 | | |
375 | | bool ArgsManager::IsArgSet(const std::string& strArg) const |
376 | 0 | { |
377 | 0 | return !GetSetting(strArg).isNull(); |
378 | 0 | } |
379 | | |
380 | | bool ArgsManager::GetSettingsPath(fs::path* filepath, bool temp, bool backup) const |
381 | 0 | { |
382 | 0 | fs::path settings = GetPathArg("-settings", BITCOIN_SETTINGS_FILENAME); |
383 | 0 | if (settings.empty()) { |
384 | 0 | return false; |
385 | 0 | } |
386 | 0 | if (backup) { |
387 | 0 | settings += ".bak"; |
388 | 0 | } |
389 | 0 | if (filepath) { |
390 | 0 | *filepath = fsbridge::AbsPathJoin(GetDataDirNet(), temp ? settings + ".tmp" : settings); |
391 | 0 | } |
392 | 0 | return true; |
393 | 0 | } |
394 | | |
395 | | static void SaveErrors(const std::vector<std::string> errors, std::vector<std::string>* error_out) |
396 | 0 | { |
397 | 0 | for (const auto& error : errors) { |
398 | 0 | if (error_out) { |
399 | 0 | error_out->emplace_back(error); |
400 | 0 | } else { |
401 | 0 | LogPrintf("%s\n", error);Line | Count | Source | 373 | 0 | #define LogPrintf(...) LogInfo(__VA_ARGS__) Line | Count | Source | 368 | 0 | #define LogInfo(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Info, /*should_ratelimit=*/true, __VA_ARGS__) Line | Count | Source | 362 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(std::source_location::current(), category, level, should_ratelimit, __VA_ARGS__) |
|
|
|
402 | 0 | } |
403 | 0 | } |
404 | 0 | } |
405 | | |
406 | | bool ArgsManager::ReadSettingsFile(std::vector<std::string>* errors) |
407 | 0 | { |
408 | 0 | fs::path path; |
409 | 0 | if (!GetSettingsPath(&path, /* temp= */ false)) { |
410 | 0 | return true; // Do nothing if settings file disabled. |
411 | 0 | } |
412 | | |
413 | 0 | LOCK(cs_args); Line | Count | Source | 259 | 0 | #define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__) Line | Count | Source | 11 | 0 | #define UNIQUE_NAME(name) PASTE2(name, __COUNTER__) Line | Count | Source | 9 | 0 | #define PASTE2(x, y) PASTE(x, y) Line | Count | Source | 8 | 0 | #define PASTE(x, y) x ## y |
|
|
|
|
414 | 0 | m_settings.rw_settings.clear(); |
415 | 0 | std::vector<std::string> read_errors; |
416 | 0 | if (!common::ReadSettings(path, m_settings.rw_settings, read_errors)) { |
417 | 0 | SaveErrors(read_errors, errors); |
418 | 0 | return false; |
419 | 0 | } |
420 | 0 | for (const auto& setting : m_settings.rw_settings) { |
421 | 0 | KeyInfo key = InterpretKey(setting.first); // Split setting key into section and argname |
422 | 0 | if (!GetArgFlags('-' + key.name)) { |
423 | 0 | LogPrintf("Ignoring unknown rw_settings value %s\n", setting.first);Line | Count | Source | 373 | 0 | #define LogPrintf(...) LogInfo(__VA_ARGS__) Line | Count | Source | 368 | 0 | #define LogInfo(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Info, /*should_ratelimit=*/true, __VA_ARGS__) Line | Count | Source | 362 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(std::source_location::current(), category, level, should_ratelimit, __VA_ARGS__) |
|
|
|
424 | 0 | } |
425 | 0 | } |
426 | 0 | return true; |
427 | 0 | } |
428 | | |
429 | | bool ArgsManager::WriteSettingsFile(std::vector<std::string>* errors, bool backup) const |
430 | 0 | { |
431 | 0 | fs::path path, path_tmp; |
432 | 0 | if (!GetSettingsPath(&path, /*temp=*/false, backup) || !GetSettingsPath(&path_tmp, /*temp=*/true, backup)) { |
433 | 0 | throw std::logic_error("Attempt to write settings file when dynamic settings are disabled."); |
434 | 0 | } |
435 | | |
436 | 0 | LOCK(cs_args); Line | Count | Source | 259 | 0 | #define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__) Line | Count | Source | 11 | 0 | #define UNIQUE_NAME(name) PASTE2(name, __COUNTER__) Line | Count | Source | 9 | 0 | #define PASTE2(x, y) PASTE(x, y) Line | Count | Source | 8 | 0 | #define PASTE(x, y) x ## y |
|
|
|
|
437 | 0 | std::vector<std::string> write_errors; |
438 | 0 | if (!common::WriteSettings(path_tmp, m_settings.rw_settings, write_errors)) { |
439 | 0 | SaveErrors(write_errors, errors); |
440 | 0 | return false; |
441 | 0 | } |
442 | 0 | if (!RenameOver(path_tmp, path)) { |
443 | 0 | SaveErrors({strprintf("Failed renaming settings file %s to %s\n", fs::PathToString(path_tmp), fs::PathToString(path))}, errors);Line | Count | Source | 1172 | 0 | #define strprintf tfm::format |
|
444 | 0 | return false; |
445 | 0 | } |
446 | 0 | return true; |
447 | 0 | } |
448 | | |
449 | | common::SettingsValue ArgsManager::GetPersistentSetting(const std::string& name) const |
450 | 0 | { |
451 | 0 | LOCK(cs_args); Line | Count | Source | 259 | 0 | #define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__) Line | Count | Source | 11 | 0 | #define UNIQUE_NAME(name) PASTE2(name, __COUNTER__) Line | Count | Source | 9 | 0 | #define PASTE2(x, y) PASTE(x, y) Line | Count | Source | 8 | 0 | #define PASTE(x, y) x ## y |
|
|
|
|
452 | 0 | return common::GetSetting(m_settings, m_network, name, !UseDefaultSection("-" + name), |
453 | 0 | /*ignore_nonpersistent=*/true, /*get_chain_type=*/false); |
454 | 0 | } |
455 | | |
456 | | bool ArgsManager::IsArgNegated(const std::string& strArg) const |
457 | 0 | { |
458 | 0 | return GetSetting(strArg).isFalse(); |
459 | 0 | } |
460 | | |
461 | | std::string ArgsManager::GetArg(const std::string& strArg, const std::string& strDefault) const |
462 | 0 | { |
463 | 0 | return GetArg(strArg).value_or(strDefault); |
464 | 0 | } |
465 | | |
466 | | std::optional<std::string> ArgsManager::GetArg(const std::string& strArg) const |
467 | 29.7k | { |
468 | 29.7k | const common::SettingsValue value = GetSetting(strArg); |
469 | 29.7k | return SettingToString(value); |
470 | 29.7k | } |
471 | | |
472 | | std::optional<std::string> SettingToString(const common::SettingsValue& value) |
473 | 29.7k | { |
474 | 29.7k | if (value.isNull()) return std::nullopt; |
475 | 0 | if (value.isFalse()) return "0"; |
476 | 0 | if (value.isTrue()) return "1"; |
477 | 0 | if (value.isNum()) return value.getValStr(); |
478 | 0 | return value.get_str(); |
479 | 0 | } |
480 | | |
481 | | std::string SettingToString(const common::SettingsValue& value, const std::string& strDefault) |
482 | 0 | { |
483 | 0 | return SettingToString(value).value_or(strDefault); |
484 | 0 | } |
485 | | |
486 | | int64_t ArgsManager::GetIntArg(const std::string& strArg, int64_t nDefault) const |
487 | 39.6k | { |
488 | 39.6k | return GetIntArg(strArg).value_or(nDefault); |
489 | 39.6k | } |
490 | | |
491 | | std::optional<int64_t> ArgsManager::GetIntArg(const std::string& strArg) const |
492 | 79.3k | { |
493 | 79.3k | const common::SettingsValue value = GetSetting(strArg); |
494 | 79.3k | return SettingToInt(value); |
495 | 79.3k | } |
496 | | |
497 | | std::optional<int64_t> SettingToInt(const common::SettingsValue& value) |
498 | 79.3k | { |
499 | 79.3k | if (value.isNull()) return std::nullopt; |
500 | 0 | if (value.isFalse()) return 0; |
501 | 0 | if (value.isTrue()) return 1; |
502 | 0 | if (value.isNum()) return value.getInt<int64_t>(); |
503 | 0 | return LocaleIndependentAtoi<int64_t>(value.get_str()); |
504 | 0 | } |
505 | | |
506 | | int64_t SettingToInt(const common::SettingsValue& value, int64_t nDefault) |
507 | 0 | { |
508 | 0 | return SettingToInt(value).value_or(nDefault); |
509 | 0 | } |
510 | | |
511 | | bool ArgsManager::GetBoolArg(const std::string& strArg, bool fDefault) const |
512 | 39.6k | { |
513 | 39.6k | return GetBoolArg(strArg).value_or(fDefault); |
514 | 39.6k | } |
515 | | |
516 | | std::optional<bool> ArgsManager::GetBoolArg(const std::string& strArg) const |
517 | 39.6k | { |
518 | 39.6k | const common::SettingsValue value = GetSetting(strArg); |
519 | 39.6k | return SettingToBool(value); |
520 | 39.6k | } |
521 | | |
522 | | std::optional<bool> SettingToBool(const common::SettingsValue& value) |
523 | 39.6k | { |
524 | 39.6k | if (value.isNull()) return std::nullopt; |
525 | 0 | if (value.isBool()) return value.get_bool(); |
526 | 0 | return InterpretBool(value.get_str()); |
527 | 0 | } |
528 | | |
529 | | bool SettingToBool(const common::SettingsValue& value, bool fDefault) |
530 | 0 | { |
531 | 0 | return SettingToBool(value).value_or(fDefault); |
532 | 0 | } |
533 | | |
534 | | bool ArgsManager::SoftSetArg(const std::string& strArg, const std::string& strValue) |
535 | 0 | { |
536 | 0 | LOCK(cs_args); Line | Count | Source | 259 | 0 | #define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__) Line | Count | Source | 11 | 0 | #define UNIQUE_NAME(name) PASTE2(name, __COUNTER__) Line | Count | Source | 9 | 0 | #define PASTE2(x, y) PASTE(x, y) Line | Count | Source | 8 | 0 | #define PASTE(x, y) x ## y |
|
|
|
|
537 | 0 | if (IsArgSet(strArg)) return false; |
538 | 0 | ForceSetArg(strArg, strValue); |
539 | 0 | return true; |
540 | 0 | } |
541 | | |
542 | | bool ArgsManager::SoftSetBoolArg(const std::string& strArg, bool fValue) |
543 | 0 | { |
544 | 0 | if (fValue) |
545 | 0 | return SoftSetArg(strArg, std::string("1")); |
546 | 0 | else |
547 | 0 | return SoftSetArg(strArg, std::string("0")); |
548 | 0 | } |
549 | | |
550 | | void ArgsManager::ForceSetArg(const std::string& strArg, const std::string& strValue) |
551 | 1 | { |
552 | 1 | LOCK(cs_args); Line | Count | Source | 259 | 1 | #define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__) Line | Count | Source | 11 | 1 | #define UNIQUE_NAME(name) PASTE2(name, __COUNTER__) Line | Count | Source | 9 | 1 | #define PASTE2(x, y) PASTE(x, y) Line | Count | Source | 8 | 1 | #define PASTE(x, y) x ## y |
|
|
|
|
553 | 1 | m_settings.forced_settings[SettingName(strArg)] = strValue; |
554 | 1 | } |
555 | | |
556 | | void ArgsManager::AddCommand(const std::string& cmd, const std::string& help) |
557 | 0 | { |
558 | 0 | Assert(cmd.find('=') == std::string::npos);Line | Count | Source | 113 | 0 | #define Assert(val) inline_assertion_check<true>(val, std::source_location::current(), #val) |
|
559 | 0 | Assert(cmd.at(0) != '-'); Line | Count | Source | 113 | 0 | #define Assert(val) inline_assertion_check<true>(val, std::source_location::current(), #val) |
|
560 | |
|
561 | 0 | LOCK(cs_args); Line | Count | Source | 259 | 0 | #define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__) Line | Count | Source | 11 | 0 | #define UNIQUE_NAME(name) PASTE2(name, __COUNTER__) Line | Count | Source | 9 | 0 | #define PASTE2(x, y) PASTE(x, y) Line | Count | Source | 8 | 0 | #define PASTE(x, y) x ## y |
|
|
|
|
562 | 0 | m_accept_any_command = false; // latch to false |
563 | 0 | std::map<std::string, Arg>& arg_map = m_available_args[OptionsCategory::COMMANDS]; |
564 | 0 | auto ret = arg_map.emplace(cmd, Arg{"", help, ArgsManager::COMMAND}); |
565 | 0 | Assert(ret.second); // Fail on duplicate commands Line | Count | Source | 113 | 0 | #define Assert(val) inline_assertion_check<true>(val, std::source_location::current(), #val) |
|
566 | 0 | } |
567 | | |
568 | | void ArgsManager::AddArg(const std::string& name, const std::string& help, unsigned int flags, const OptionsCategory& cat) |
569 | 0 | { |
570 | 0 | Assert((flags & ArgsManager::COMMAND) == 0); // use AddCommand Line | Count | Source | 113 | 0 | #define Assert(val) inline_assertion_check<true>(val, std::source_location::current(), #val) |
|
571 | | |
572 | | // Split arg name from its help param |
573 | 0 | size_t eq_index = name.find('='); |
574 | 0 | if (eq_index == std::string::npos) { |
575 | 0 | eq_index = name.size(); |
576 | 0 | } |
577 | 0 | std::string arg_name = name.substr(0, eq_index); |
578 | |
|
579 | 0 | LOCK(cs_args); Line | Count | Source | 259 | 0 | #define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__) Line | Count | Source | 11 | 0 | #define UNIQUE_NAME(name) PASTE2(name, __COUNTER__) Line | Count | Source | 9 | 0 | #define PASTE2(x, y) PASTE(x, y) Line | Count | Source | 8 | 0 | #define PASTE(x, y) x ## y |
|
|
|
|
580 | 0 | std::map<std::string, Arg>& arg_map = m_available_args[cat]; |
581 | 0 | auto ret = arg_map.emplace(arg_name, Arg{name.substr(eq_index, name.size() - eq_index), help, flags}); |
582 | 0 | assert(ret.second); // Make sure an insertion actually happened |
583 | | |
584 | 0 | if (flags & ArgsManager::NETWORK_ONLY) { |
585 | 0 | m_network_only_args.emplace(arg_name); |
586 | 0 | } |
587 | 0 | } |
588 | | |
589 | | void ArgsManager::AddHiddenArgs(const std::vector<std::string>& names) |
590 | 0 | { |
591 | 0 | for (const std::string& name : names) { |
592 | 0 | AddArg(name, "", ArgsManager::ALLOW_ANY, OptionsCategory::HIDDEN); |
593 | 0 | } |
594 | 0 | } |
595 | | |
596 | | void ArgsManager::ClearArgs() |
597 | 1 | { |
598 | 1 | LOCK(cs_args); Line | Count | Source | 259 | 1 | #define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__) Line | Count | Source | 11 | 1 | #define UNIQUE_NAME(name) PASTE2(name, __COUNTER__) Line | Count | Source | 9 | 1 | #define PASTE2(x, y) PASTE(x, y) Line | Count | Source | 8 | 1 | #define PASTE(x, y) x ## y |
|
|
|
|
599 | 1 | m_settings = {}; |
600 | 1 | m_available_args.clear(); |
601 | 1 | m_network_only_args.clear(); |
602 | 1 | } |
603 | | |
604 | | void ArgsManager::CheckMultipleCLIArgs() const |
605 | 0 | { |
606 | 0 | LOCK(cs_args); Line | Count | Source | 259 | 0 | #define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__) Line | Count | Source | 11 | 0 | #define UNIQUE_NAME(name) PASTE2(name, __COUNTER__) Line | Count | Source | 9 | 0 | #define PASTE2(x, y) PASTE(x, y) Line | Count | Source | 8 | 0 | #define PASTE(x, y) x ## y |
|
|
|
|
607 | 0 | std::vector<std::string> found{}; |
608 | 0 | auto cmds = m_available_args.find(OptionsCategory::CLI_COMMANDS); |
609 | 0 | if (cmds != m_available_args.end()) { |
610 | 0 | for (const auto& [cmd, argspec] : cmds->second) { |
611 | 0 | if (IsArgSet(cmd)) { |
612 | 0 | found.push_back(cmd); |
613 | 0 | } |
614 | 0 | } |
615 | 0 | if (found.size() > 1) { |
616 | 0 | throw std::runtime_error(strprintf("Only one of %s may be specified.", util::Join(found, ", ")));Line | Count | Source | 1172 | 0 | #define strprintf tfm::format |
|
617 | 0 | } |
618 | 0 | } |
619 | 0 | } |
620 | | |
621 | | std::string ArgsManager::GetHelpMessage() const |
622 | 0 | { |
623 | 0 | const bool show_debug = GetBoolArg("-help-debug", false); |
624 | |
|
625 | 0 | std::string usage; |
626 | 0 | LOCK(cs_args); Line | Count | Source | 259 | 0 | #define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__) Line | Count | Source | 11 | 0 | #define UNIQUE_NAME(name) PASTE2(name, __COUNTER__) Line | Count | Source | 9 | 0 | #define PASTE2(x, y) PASTE(x, y) Line | Count | Source | 8 | 0 | #define PASTE(x, y) x ## y |
|
|
|
|
627 | 0 | for (const auto& arg_map : m_available_args) { |
628 | 0 | switch(arg_map.first) { |
629 | 0 | case OptionsCategory::OPTIONS: |
630 | 0 | usage += HelpMessageGroup("Options:"); |
631 | 0 | break; |
632 | 0 | case OptionsCategory::CONNECTION: |
633 | 0 | usage += HelpMessageGroup("Connection options:"); |
634 | 0 | break; |
635 | 0 | case OptionsCategory::ZMQ: |
636 | 0 | usage += HelpMessageGroup("ZeroMQ notification options:"); |
637 | 0 | break; |
638 | 0 | case OptionsCategory::DEBUG_TEST: |
639 | 0 | usage += HelpMessageGroup("Debugging/Testing options:"); |
640 | 0 | break; |
641 | 0 | case OptionsCategory::NODE_RELAY: |
642 | 0 | usage += HelpMessageGroup("Node relay options:"); |
643 | 0 | break; |
644 | 0 | case OptionsCategory::BLOCK_CREATION: |
645 | 0 | usage += HelpMessageGroup("Block creation options:"); |
646 | 0 | break; |
647 | 0 | case OptionsCategory::RPC: |
648 | 0 | usage += HelpMessageGroup("RPC server options:"); |
649 | 0 | break; |
650 | 0 | case OptionsCategory::IPC: |
651 | 0 | usage += HelpMessageGroup("IPC interprocess connection options:"); |
652 | 0 | break; |
653 | 0 | case OptionsCategory::WALLET: |
654 | 0 | usage += HelpMessageGroup("Wallet options:"); |
655 | 0 | break; |
656 | 0 | case OptionsCategory::WALLET_DEBUG_TEST: |
657 | 0 | if (show_debug) usage += HelpMessageGroup("Wallet debugging/testing options:"); |
658 | 0 | break; |
659 | 0 | case OptionsCategory::CHAINPARAMS: |
660 | 0 | usage += HelpMessageGroup("Chain selection options:"); |
661 | 0 | break; |
662 | 0 | case OptionsCategory::GUI: |
663 | 0 | usage += HelpMessageGroup("UI Options:"); |
664 | 0 | break; |
665 | 0 | case OptionsCategory::COMMANDS: |
666 | 0 | usage += HelpMessageGroup("Commands:"); |
667 | 0 | break; |
668 | 0 | case OptionsCategory::REGISTER_COMMANDS: |
669 | 0 | usage += HelpMessageGroup("Register Commands:"); |
670 | 0 | break; |
671 | 0 | case OptionsCategory::CLI_COMMANDS: |
672 | 0 | usage += HelpMessageGroup("CLI Commands:"); |
673 | 0 | break; |
674 | 0 | default: |
675 | 0 | break; |
676 | 0 | } |
677 | | |
678 | | // When we get to the hidden options, stop |
679 | 0 | if (arg_map.first == OptionsCategory::HIDDEN) break; |
680 | | |
681 | 0 | for (const auto& arg : arg_map.second) { |
682 | 0 | if (show_debug || !(arg.second.m_flags & ArgsManager::DEBUG_ONLY)) { |
683 | 0 | std::string name; |
684 | 0 | if (arg.second.m_help_param.empty()) { |
685 | 0 | name = arg.first; |
686 | 0 | } else { |
687 | 0 | name = arg.first + arg.second.m_help_param; |
688 | 0 | } |
689 | 0 | usage += HelpMessageOpt(name, arg.second.m_help_text); |
690 | 0 | } |
691 | 0 | } |
692 | 0 | } |
693 | 0 | return usage; |
694 | 0 | } |
695 | | |
696 | | bool HelpRequested(const ArgsManager& args) |
697 | 0 | { |
698 | 0 | return args.IsArgSet("-?") || args.IsArgSet("-h") || args.IsArgSet("-help") || args.IsArgSet("-help-debug"); |
699 | 0 | } |
700 | | |
701 | | void SetupHelpOptions(ArgsManager& args) |
702 | 0 | { |
703 | 0 | args.AddArg("-help", "Print this help message and exit (also -h or -?)", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); |
704 | 0 | args.AddHiddenArgs({"-h", "-?"}); |
705 | 0 | } |
706 | | |
707 | | static const int screenWidth = 79; |
708 | | static const int optIndent = 2; |
709 | | static const int msgIndent = 7; |
710 | | |
711 | 0 | std::string HelpMessageGroup(const std::string &message) { |
712 | 0 | return std::string(message) + std::string("\n\n"); |
713 | 0 | } |
714 | | |
715 | 0 | std::string HelpMessageOpt(const std::string &option, const std::string &message) { |
716 | 0 | return std::string(optIndent,' ') + std::string(option) + |
717 | 0 | std::string("\n") + std::string(msgIndent,' ') + |
718 | 0 | FormatParagraph(message, screenWidth - msgIndent, msgIndent) + |
719 | 0 | std::string("\n\n"); |
720 | 0 | } |
721 | | |
722 | | const std::vector<std::string> TEST_OPTIONS_DOC{ |
723 | | "addrman (use deterministic addrman)", |
724 | | "reindex_after_failure_noninteractive_yes (When asked for a reindex after failure interactively, simulate as-if answered with 'yes')", |
725 | | "bip94 (enforce BIP94 consensus rules)", |
726 | | }; |
727 | | |
728 | | bool HasTestOption(const ArgsManager& args, const std::string& test_option) |
729 | 0 | { |
730 | 0 | const auto options = args.GetArgs("-test"); |
731 | 0 | return std::any_of(options.begin(), options.end(), [test_option](const auto& option) { |
732 | 0 | return option == test_option; |
733 | 0 | }); |
734 | 0 | } |
735 | | |
736 | | fs::path GetDefaultDataDir() |
737 | 0 | { |
738 | | // Windows: |
739 | | // old: C:\Users\Username\AppData\Roaming\Bitcoin |
740 | | // new: C:\Users\Username\AppData\Local\Bitcoin |
741 | | // macOS: ~/Library/Application Support/Bitcoin |
742 | | // Unix-like: ~/.bitcoin |
743 | | #ifdef WIN32 |
744 | | // Windows |
745 | | // Check for existence of datadir in old location and keep it there |
746 | | fs::path legacy_path = GetSpecialFolderPath(CSIDL_APPDATA) / "Bitcoin"; |
747 | | if (fs::exists(legacy_path)) return legacy_path; |
748 | | |
749 | | // Otherwise, fresh installs can start in the new, "proper" location |
750 | | return GetSpecialFolderPath(CSIDL_LOCAL_APPDATA) / "Bitcoin"; |
751 | | #else |
752 | 0 | fs::path pathRet; |
753 | 0 | char* pszHome = getenv("HOME"); |
754 | 0 | if (pszHome == nullptr || strlen(pszHome) == 0) |
755 | 0 | pathRet = fs::path("/"); |
756 | 0 | else |
757 | 0 | pathRet = fs::path(pszHome); |
758 | 0 | #ifdef __APPLE__ |
759 | | // macOS |
760 | 0 | return pathRet / "Library/Application Support/Bitcoin"; |
761 | | #else |
762 | | // Unix-like |
763 | | return pathRet / ".bitcoin"; |
764 | | #endif |
765 | 0 | #endif |
766 | 0 | } |
767 | | |
768 | | bool CheckDataDirOption(const ArgsManager& args) |
769 | 0 | { |
770 | 0 | const fs::path datadir{args.GetPathArg("-datadir")}; |
771 | 0 | return datadir.empty() || fs::is_directory(fs::absolute(datadir)); |
772 | 0 | } |
773 | | |
774 | | fs::path ArgsManager::GetConfigFilePath() const |
775 | 0 | { |
776 | 0 | LOCK(cs_args); Line | Count | Source | 259 | 0 | #define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__) Line | Count | Source | 11 | 0 | #define UNIQUE_NAME(name) PASTE2(name, __COUNTER__) Line | Count | Source | 9 | 0 | #define PASTE2(x, y) PASTE(x, y) Line | Count | Source | 8 | 0 | #define PASTE(x, y) x ## y |
|
|
|
|
777 | 0 | return *Assert(m_config_path); Line | Count | Source | 113 | 0 | #define Assert(val) inline_assertion_check<true>(val, std::source_location::current(), #val) |
|
778 | 0 | } |
779 | | |
780 | | void ArgsManager::SetConfigFilePath(fs::path path) |
781 | 0 | { |
782 | 0 | LOCK(cs_args); Line | Count | Source | 259 | 0 | #define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__) Line | Count | Source | 11 | 0 | #define UNIQUE_NAME(name) PASTE2(name, __COUNTER__) Line | Count | Source | 9 | 0 | #define PASTE2(x, y) PASTE(x, y) Line | Count | Source | 8 | 0 | #define PASTE(x, y) x ## y |
|
|
|
|
783 | 0 | assert(!m_config_path); |
784 | 0 | m_config_path = path; |
785 | 0 | } |
786 | | |
787 | | ChainType ArgsManager::GetChainType() const |
788 | 0 | { |
789 | 0 | std::variant<ChainType, std::string> arg = GetChainArg(); |
790 | 0 | if (auto* parsed = std::get_if<ChainType>(&arg)) return *parsed; |
791 | 0 | throw std::runtime_error(strprintf("Unknown chain %s.", std::get<std::string>(arg)));Line | Count | Source | 1172 | 0 | #define strprintf tfm::format |
|
792 | 0 | } |
793 | | |
794 | | std::string ArgsManager::GetChainTypeString() const |
795 | 0 | { |
796 | 0 | auto arg = GetChainArg(); |
797 | 0 | if (auto* parsed = std::get_if<ChainType>(&arg)) return ChainTypeToString(*parsed); |
798 | 0 | return std::get<std::string>(arg); |
799 | 0 | } |
800 | | |
801 | | std::variant<ChainType, std::string> ArgsManager::GetChainArg() const |
802 | 0 | { |
803 | 0 | auto get_net = [&](const std::string& arg) { |
804 | 0 | LOCK(cs_args); Line | Count | Source | 259 | 0 | #define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__) Line | Count | Source | 11 | 0 | #define UNIQUE_NAME(name) PASTE2(name, __COUNTER__) Line | Count | Source | 9 | 0 | #define PASTE2(x, y) PASTE(x, y) Line | Count | Source | 8 | 0 | #define PASTE(x, y) x ## y |
|
|
|
|
805 | 0 | common::SettingsValue value = common::GetSetting(m_settings, /* section= */ "", SettingName(arg), |
806 | 0 | /* ignore_default_section_config= */ false, |
807 | 0 | /*ignore_nonpersistent=*/false, |
808 | 0 | /* get_chain_type= */ true); |
809 | 0 | return value.isNull() ? false : value.isBool() ? value.get_bool() : InterpretBool(value.get_str()); |
810 | 0 | }; |
811 | |
|
812 | 0 | const bool fRegTest = get_net("-regtest"); |
813 | 0 | const bool fSigNet = get_net("-signet"); |
814 | 0 | const bool fTestNet = get_net("-testnet"); |
815 | 0 | const bool fTestNet4 = get_net("-testnet4"); |
816 | 0 | const auto chain_arg = GetArg("-chain"); |
817 | |
|
818 | 0 | if ((int)chain_arg.has_value() + (int)fRegTest + (int)fSigNet + (int)fTestNet + (int)fTestNet4 > 1) { |
819 | 0 | throw std::runtime_error("Invalid combination of -regtest, -signet, -testnet, -testnet4 and -chain. Can use at most one."); |
820 | 0 | } |
821 | 0 | if (chain_arg) { |
822 | 0 | if (auto parsed = ChainTypeFromString(*chain_arg)) return *parsed; |
823 | | // Not a known string, so return original string |
824 | 0 | return *chain_arg; |
825 | 0 | } |
826 | 0 | if (fRegTest) return ChainType::REGTEST; |
827 | 0 | if (fSigNet) return ChainType::SIGNET; |
828 | 0 | if (fTestNet) return ChainType::TESTNET; |
829 | 0 | if (fTestNet4) return ChainType::TESTNET4; |
830 | 0 | return ChainType::MAIN; |
831 | 0 | } |
832 | | |
833 | | bool ArgsManager::UseDefaultSection(const std::string& arg) const |
834 | 148k | { |
835 | 148k | return m_network == ChainTypeToString(ChainType::MAIN) || m_network_only_args.count(arg) == 0; |
836 | 148k | } |
837 | | |
838 | | common::SettingsValue ArgsManager::GetSetting(const std::string& arg) const |
839 | 148k | { |
840 | 148k | LOCK(cs_args); Line | Count | Source | 259 | 148k | #define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__) Line | Count | Source | 11 | 148k | #define UNIQUE_NAME(name) PASTE2(name, __COUNTER__) Line | Count | Source | 9 | 148k | #define PASTE2(x, y) PASTE(x, y) Line | Count | Source | 8 | 148k | #define PASTE(x, y) x ## y |
|
|
|
|
841 | 148k | return common::GetSetting( |
842 | 148k | m_settings, m_network, SettingName(arg), !UseDefaultSection(arg), |
843 | 148k | /*ignore_nonpersistent=*/false, /*get_chain_type=*/false); |
844 | 148k | } |
845 | | |
846 | | std::vector<common::SettingsValue> ArgsManager::GetSettingsList(const std::string& arg) const |
847 | 0 | { |
848 | 0 | LOCK(cs_args); Line | Count | Source | 259 | 0 | #define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__) Line | Count | Source | 11 | 0 | #define UNIQUE_NAME(name) PASTE2(name, __COUNTER__) Line | Count | Source | 9 | 0 | #define PASTE2(x, y) PASTE(x, y) Line | Count | Source | 8 | 0 | #define PASTE(x, y) x ## y |
|
|
|
|
849 | 0 | return common::GetSettingsList(m_settings, m_network, SettingName(arg), !UseDefaultSection(arg)); |
850 | 0 | } |
851 | | |
852 | | void ArgsManager::logArgsPrefix( |
853 | | const std::string& prefix, |
854 | | const std::string& section, |
855 | | const std::map<std::string, std::vector<common::SettingsValue>>& args) const |
856 | 0 | { |
857 | 0 | std::string section_str = section.empty() ? "" : "[" + section + "] "; |
858 | 0 | for (const auto& arg : args) { |
859 | 0 | for (const auto& value : arg.second) { |
860 | 0 | std::optional<unsigned int> flags = GetArgFlags('-' + arg.first); |
861 | 0 | if (flags) { |
862 | 0 | std::string value_str = (*flags & SENSITIVE) ? "****" : value.write(); |
863 | 0 | LogPrintf("%s %s%s=%s\n", prefix, section_str, arg.first, value_str);Line | Count | Source | 373 | 0 | #define LogPrintf(...) LogInfo(__VA_ARGS__) Line | Count | Source | 368 | 0 | #define LogInfo(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Info, /*should_ratelimit=*/true, __VA_ARGS__) Line | Count | Source | 362 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(std::source_location::current(), category, level, should_ratelimit, __VA_ARGS__) |
|
|
|
864 | 0 | } |
865 | 0 | } |
866 | 0 | } |
867 | 0 | } |
868 | | |
869 | | void ArgsManager::LogArgs() const |
870 | 0 | { |
871 | 0 | LOCK(cs_args); Line | Count | Source | 259 | 0 | #define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__) Line | Count | Source | 11 | 0 | #define UNIQUE_NAME(name) PASTE2(name, __COUNTER__) Line | Count | Source | 9 | 0 | #define PASTE2(x, y) PASTE(x, y) Line | Count | Source | 8 | 0 | #define PASTE(x, y) x ## y |
|
|
|
|
872 | 0 | for (const auto& section : m_settings.ro_config) { |
873 | 0 | logArgsPrefix("Config file arg:", section.first, section.second); |
874 | 0 | } |
875 | 0 | for (const auto& setting : m_settings.rw_settings) { |
876 | 0 | LogPrintf("Setting file arg: %s = %s\n", setting.first, setting.second.write());Line | Count | Source | 373 | 0 | #define LogPrintf(...) LogInfo(__VA_ARGS__) Line | Count | Source | 368 | 0 | #define LogInfo(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Info, /*should_ratelimit=*/true, __VA_ARGS__) Line | Count | Source | 362 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(std::source_location::current(), category, level, should_ratelimit, __VA_ARGS__) |
|
|
|
877 | 0 | } |
878 | 0 | logArgsPrefix("Command-line arg:", "", m_settings.command_line_options); |
879 | 0 | } |