/tmp/bitcoin/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 <sync.h> |
11 | | #include <tinyformat.h> |
12 | | #include <univalue.h> |
13 | | #include <util/chaintype.h> |
14 | | #include <util/check.h> |
15 | | #include <util/fs.h> |
16 | | #include <util/fs_helpers.h> |
17 | | #include <util/log.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 | 339k | { |
59 | 339k | if (strValue.empty()) |
60 | 18.6k | return true; |
61 | 321k | return (LocaleIndependentAtoi<int>(strValue) != 0); |
62 | 339k | } |
63 | | |
64 | | static std::string SettingName(const std::string& arg) |
65 | 1.16M | { |
66 | 1.16M | return arg.size() > 0 && arg[0] == '-' ? arg.substr(1) : arg; |
67 | 1.16M | } |
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 | 391k | { |
79 | 391k | KeyInfo result; |
80 | | // Split section name from key name for keys like "testnet.foo" or "regtest.bar" |
81 | 391k | size_t option_index = key.find('.'); |
82 | 391k | if (option_index != std::string::npos) { |
83 | 141k | result.section = key.substr(0, option_index); |
84 | 141k | key.erase(0, option_index + 1); |
85 | 141k | } |
86 | 391k | if (key.starts_with("no")) { |
87 | 105k | key.erase(0, 2); |
88 | 105k | result.negated = true; |
89 | 105k | } |
90 | 391k | result.name = key; |
91 | 391k | return result; |
92 | 391k | } |
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 | 367k | { |
108 | | // Return negated settings as false values. |
109 | 367k | if (key.negated) { |
110 | 105k | if (flags & ArgsManager::DISALLOW_NEGATION) { |
111 | 0 | error = strprintf("Negating of -%s is meaningless and therefore forbidden", key.name); |
112 | 0 | return std::nullopt; |
113 | 0 | } |
114 | | // Double negatives like -nofoo=0 are supported (but discouraged) |
115 | 105k | if (value && !InterpretBool(*value)) { |
116 | 12 | LogWarning("Parsed potentially confusing double-negative -%s=%s", key.name, *value); |
117 | 12 | return true; |
118 | 12 | } |
119 | 105k | return false; |
120 | 105k | } |
121 | 262k | if (!value && (flags & ArgsManager::DISALLOW_ELISION)) { |
122 | 1 | error = strprintf("Can not set -%s with no value. Please specify value with -%s=value.", key.name, key.name); |
123 | 1 | return std::nullopt; |
124 | 1 | } |
125 | 262k | return value ? *value : ""; |
126 | 262k | } |
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 | 52.2k | ArgsManager::ArgsManager() = default; |
132 | 49.6k | ArgsManager::~ArgsManager() = default; |
133 | | |
134 | | std::set<std::string> ArgsManager::GetUnsuitableSectionOnlyArgs() const |
135 | 49.3k | { |
136 | 49.3k | std::set<std::string> unsuitables; |
137 | | |
138 | 49.3k | LOCK(cs_args); |
139 | | |
140 | | // if there's no section selected, don't worry |
141 | 49.3k | 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 | 49.3k | if (m_network == ChainTypeToString(ChainType::MAIN)) return std::set<std::string> {}; |
145 | | |
146 | 36.8k | for (const auto& arg : m_network_only_args) { |
147 | 27.9k | if (OnlyHasDefaultSectionSetting(m_settings, m_network, SettingName(arg))) { |
148 | 1.75k | unsuitables.insert(arg); |
149 | 1.75k | } |
150 | 27.9k | } |
151 | 36.8k | return unsuitables; |
152 | 49.3k | } |
153 | | |
154 | | std::list<SectionInfo> ArgsManager::GetUnrecognizedSections() const |
155 | 1.85k | { |
156 | | // Section names to be recognized in the config file. |
157 | 1.85k | static const std::set<std::string> available_sections{ |
158 | 1.85k | ChainTypeToString(ChainType::REGTEST), |
159 | 1.85k | ChainTypeToString(ChainType::SIGNET), |
160 | 1.85k | ChainTypeToString(ChainType::TESTNET), |
161 | 1.85k | ChainTypeToString(ChainType::TESTNET4), |
162 | 1.85k | ChainTypeToString(ChainType::MAIN), |
163 | 1.85k | }; |
164 | | |
165 | 1.85k | LOCK(cs_args); |
166 | 1.85k | std::list<SectionInfo> unrecognized = m_config_sections; |
167 | 1.85k | unrecognized.remove_if([](const SectionInfo& appeared){ return available_sections.contains(appeared.m_name); }); |
168 | 1.85k | return unrecognized; |
169 | 1.85k | } |
170 | | |
171 | | void ArgsManager::SelectConfigNetwork(const std::string& network) |
172 | 51.0k | { |
173 | 51.0k | LOCK(cs_args); |
174 | 51.0k | m_network = network; |
175 | 51.0k | } |
176 | | |
177 | | bool ArgsManager::ParseParameters(int argc, const char* const argv[], std::string& error) |
178 | 52.1k | { |
179 | 52.1k | LOCK(cs_args); |
180 | 52.1k | m_settings.command_line_options.clear(); |
181 | | |
182 | 205k | for (int i = 1; i < argc; i++) { |
183 | 153k | std::string key(argv[i]); |
184 | | |
185 | | #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 | | if (key.starts_with("-psn_")) continue; |
191 | | #endif |
192 | | |
193 | 153k | if (key == "-") break; //bitcoin-tx using stdin |
194 | 153k | std::optional<std::string> val; |
195 | 153k | size_t is_index = key.find('='); |
196 | 153k | if (is_index != std::string::npos) { |
197 | 140k | val = key.substr(is_index + 1); |
198 | 140k | key.erase(is_index); |
199 | 140k | } |
200 | | #ifdef WIN32 |
201 | | key = ToLower(key); |
202 | | if (key[0] == '/') |
203 | | key[0] = '-'; |
204 | | #endif |
205 | | |
206 | 153k | if (key[0] != '-') { |
207 | 945 | if (!m_accept_any_command && m_command.empty()) { |
208 | | // The first non-dash arg is a registered command |
209 | 65 | std::optional<unsigned int> flags = GetArgFlags_(key); |
210 | 65 | if (!flags || !(*flags & ArgsManager::COMMAND)) { |
211 | 5 | error = strprintf("Invalid command '%s'", argv[i]); |
212 | 5 | return false; |
213 | 5 | } |
214 | 65 | } |
215 | 940 | m_command.push_back(key); |
216 | 2.04k | while (++i < argc) { |
217 | | // The remaining args are command args |
218 | 1.10k | m_command.emplace_back(argv[i]); |
219 | 1.10k | } |
220 | 940 | break; |
221 | 945 | } |
222 | | |
223 | | // Transform --foo to -foo |
224 | 152k | if (key.length() > 1 && key[1] == '-') |
225 | 6 | key.erase(0, 1); |
226 | | |
227 | | // Transform -foo to foo |
228 | 152k | key.erase(0, 1); |
229 | 152k | KeyInfo keyinfo = InterpretKey(key); |
230 | 152k | 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 | 152k | if (!flags || !keyinfo.section.empty()) { |
236 | 11 | error = strprintf("Invalid parameter %s", argv[i]); |
237 | 11 | return false; |
238 | 11 | } |
239 | | |
240 | 152k | std::optional<common::SettingsValue> value = InterpretValue(keyinfo, val ? &*val : nullptr, *flags, error); |
241 | 152k | if (!value) return false; |
242 | | |
243 | 152k | m_settings.command_line_options[keyinfo.name].push_back(*value); |
244 | 152k | } |
245 | | |
246 | | // we do not allow -includeconf from command line, only -noincludeconf |
247 | 52.1k | if (auto* includes = common::FindKey(m_settings.command_line_options, "includeconf")) { |
248 | 5 | const common::SettingsSpan values{*includes}; |
249 | | // Range may be empty if -noincludeconf was passed |
250 | 5 | if (!values.empty()) { |
251 | 4 | error = "-includeconf cannot be used from commandline; -includeconf=" + values.begin()->write(); |
252 | 4 | return false; // pick first value as example |
253 | 4 | } |
254 | 5 | } |
255 | 52.1k | return true; |
256 | 52.1k | } |
257 | | |
258 | | std::optional<unsigned int> ArgsManager::GetArgFlags_(const std::string& name) const |
259 | 432k | { |
260 | 432k | AssertLockHeld(cs_args); |
261 | 835k | for (const auto& arg_map : m_available_args) { |
262 | 835k | const auto search = arg_map.second.find(name); |
263 | 835k | if (search != arg_map.second.end()) { |
264 | 409k | return search->second.m_flags; |
265 | 409k | } |
266 | 835k | } |
267 | 23.3k | return m_default_flags; |
268 | 432k | } |
269 | | |
270 | | std::optional<unsigned int> ArgsManager::GetArgFlags(const std::string& name) const |
271 | 0 | { |
272 | 0 | LOCK(cs_args); |
273 | 0 | return GetArgFlags_(name); |
274 | 0 | } |
275 | | |
276 | | void ArgsManager::SetDefaultFlags(std::optional<unsigned int> flags) |
277 | 0 | { |
278 | 0 | LOCK(cs_args); |
279 | 0 | m_default_flags = flags; |
280 | 0 | } |
281 | | |
282 | | fs::path ArgsManager::GetPathArg_(std::string arg, const fs::path& default_value) const |
283 | 30.6k | { |
284 | 30.6k | AssertLockHeld(cs_args); |
285 | 30.6k | const auto value = GetSetting_(arg); |
286 | 30.6k | if (value.isFalse()) return {}; |
287 | 30.6k | std::string path_str = SettingToString(value, ""); |
288 | 30.6k | if (path_str.empty()) return default_value; |
289 | 13.4k | fs::path result = fs::PathFromString(path_str).lexically_normal(); |
290 | | // Remove trailing slash, if present. |
291 | 13.4k | return result.has_filename() ? result : result.parent_path(); |
292 | 30.6k | } |
293 | | |
294 | | fs::path ArgsManager::GetPathArg(std::string arg, const fs::path& default_value) const |
295 | 20.7k | { |
296 | 20.7k | LOCK(cs_args); |
297 | 20.7k | return GetPathArg_(std::move(arg), default_value); |
298 | 20.7k | } |
299 | | |
300 | | fs::path ArgsManager::GetBlocksDirPath() const |
301 | 8.68k | { |
302 | 8.68k | LOCK(cs_args); |
303 | 8.68k | fs::path& path = m_cached_blocks_path; |
304 | | |
305 | | // Cache the path to avoid calling fs::create_directories on every call of |
306 | | // this function |
307 | 8.68k | if (!path.empty()) return path; |
308 | | |
309 | 2.02k | if (!GetSetting_("-blocksdir").isNull()) { |
310 | 3 | path = fs::absolute(GetPathArg_("-blocksdir")); |
311 | 3 | if (!fs::is_directory(path)) { |
312 | 1 | path = ""; |
313 | 1 | return path; |
314 | 1 | } |
315 | 2.02k | } else { |
316 | 2.02k | path = GetDataDir(/*net_specific=*/false); |
317 | 2.02k | } |
318 | | |
319 | 2.02k | path /= fs::PathFromString(BaseParams().DataDir()); |
320 | 2.02k | path /= "blocks"; |
321 | 2.02k | fs::create_directories(path); |
322 | 2.02k | return path; |
323 | 2.02k | } |
324 | | |
325 | 3.78k | fs::path ArgsManager::GetDataDirBase() const { |
326 | 3.78k | LOCK(cs_args); |
327 | 3.78k | return GetDataDir(/*net_specific=*/false); |
328 | 3.78k | } |
329 | | |
330 | 35.0k | fs::path ArgsManager::GetDataDirNet() const { |
331 | 35.0k | LOCK(cs_args); |
332 | 35.0k | return GetDataDir(/*net_specific=*/true); |
333 | 35.0k | } |
334 | | |
335 | | fs::path ArgsManager::GetDataDir(bool net_specific) const |
336 | 43.1k | { |
337 | 43.1k | AssertLockHeld(cs_args); |
338 | 43.1k | fs::path& path = net_specific ? m_cached_network_datadir_path : m_cached_datadir_path; |
339 | | |
340 | | // Used cached path if available |
341 | 43.1k | if (!path.empty()) return path; |
342 | | |
343 | 7.55k | const fs::path datadir{GetPathArg_("-datadir")}; |
344 | 7.55k | if (!datadir.empty()) { |
345 | 7.54k | path = fs::absolute(datadir); |
346 | 7.54k | if (!fs::is_directory(path)) { |
347 | 0 | path = ""; |
348 | 0 | return path; |
349 | 0 | } |
350 | 7.54k | } else { |
351 | 5 | path = GetDefaultDataDir(); |
352 | 5 | } |
353 | | |
354 | 7.55k | if (net_specific && !BaseParams().DataDir().empty()) { |
355 | 2.48k | path /= fs::PathFromString(BaseParams().DataDir()); |
356 | 2.48k | } |
357 | | |
358 | 7.55k | return path; |
359 | 7.55k | } |
360 | | |
361 | | void ArgsManager::ClearPathCache() |
362 | 2.99k | { |
363 | 2.99k | LOCK(cs_args); |
364 | | |
365 | 2.99k | m_cached_datadir_path = fs::path(); |
366 | 2.99k | m_cached_network_datadir_path = fs::path(); |
367 | 2.99k | m_cached_blocks_path = fs::path(); |
368 | 2.99k | } |
369 | | |
370 | | std::optional<const ArgsManager::Command> ArgsManager::GetCommand() const |
371 | 62 | { |
372 | 62 | Command ret; |
373 | 62 | LOCK(cs_args); |
374 | 62 | auto it = m_command.begin(); |
375 | 62 | if (it == m_command.end()) { |
376 | | // No command was passed |
377 | 2 | return std::nullopt; |
378 | 2 | } |
379 | 60 | if (!m_accept_any_command) { |
380 | | // The registered command |
381 | 60 | ret.command = *(it++); |
382 | 60 | } |
383 | 72 | while (it != m_command.end()) { |
384 | | // The unregistered command and args (if any) |
385 | 12 | ret.args.push_back(*(it++)); |
386 | 12 | } |
387 | 60 | return ret; |
388 | 62 | } |
389 | | |
390 | | bool ArgsManager::CheckCommandOptions(const std::string& command, std::vector<std::string>* errors) const |
391 | 44 | { |
392 | 44 | LOCK(cs_args); |
393 | | |
394 | 44 | auto command_options = m_available_args.find(OptionsCategory::COMMAND_OPTIONS); |
395 | 44 | if (command_options == m_available_args.end()) { |
396 | | // There are no command-specific options at all, so everything is fine |
397 | 0 | return true; |
398 | 0 | } |
399 | | |
400 | 44 | const auto command_args = m_command_args.find(command); |
401 | 44 | auto is_valid_opt = [&](const auto& opt) EXCLUSIVE_LOCKS_REQUIRED(cs_args) -> bool { |
402 | 32 | if (command_args == m_command_args.end()) { |
403 | | // Caller may not have checked that command actually exists |
404 | | // before calling this function. In that case, treat it as |
405 | | // having no valid command-specific options. |
406 | 5 | return false; |
407 | 27 | } else { |
408 | 27 | return command_args->second.contains(opt); |
409 | 27 | } |
410 | 32 | }; |
411 | | |
412 | 44 | bool ok = true; |
413 | 57 | for (const auto& [arg, _] : command_options->second) { |
414 | 57 | if (!GetSetting_(arg).isNull() && !is_valid_opt(arg)) { |
415 | 7 | ok = false; |
416 | 7 | if (errors != nullptr) { |
417 | 7 | errors->emplace_back(strprintf("The %s option cannot be used with the '%s' command.", arg, command)); |
418 | 7 | } |
419 | 7 | } |
420 | 57 | } |
421 | 44 | return ok; |
422 | 44 | } |
423 | | |
424 | | std::vector<std::string> ArgsManager::GetArgs(const std::string& strArg) const |
425 | 221k | { |
426 | 221k | std::vector<std::string> result; |
427 | 221k | for (const common::SettingsValue& value : GetSettingsList(strArg)) { |
428 | 95.3k | result.push_back(value.isFalse() ? "0" : value.isTrue() ? "1" : value.get_str()); |
429 | 95.3k | } |
430 | 221k | return result; |
431 | 221k | } |
432 | | |
433 | | bool ArgsManager::IsArgSet(const std::string& strArg) const |
434 | 78.0k | { |
435 | 78.0k | return !GetSetting(strArg).isNull(); |
436 | 78.0k | } |
437 | | |
438 | | bool ArgsManager::GetSettingsPath(fs::path* filepath, bool temp, bool backup) const |
439 | 5.08k | { |
440 | 5.08k | fs::path settings = GetPathArg("-settings", BITCOIN_SETTINGS_FILENAME); |
441 | 5.08k | if (settings.empty()) { |
442 | 3 | return false; |
443 | 3 | } |
444 | 5.07k | if (backup) { |
445 | 0 | settings += ".bak"; |
446 | 0 | } |
447 | 5.07k | if (filepath) { |
448 | 3.90k | *filepath = fsbridge::AbsPathJoin(GetDataDirNet(), temp ? settings + ".tmp" : settings); |
449 | 3.90k | } |
450 | 5.07k | return true; |
451 | 5.08k | } |
452 | | |
453 | | static void SaveErrors(const std::vector<std::string> errors, std::vector<std::string>* error_out) |
454 | 4 | { |
455 | 4 | for (const auto& error : errors) { |
456 | 4 | if (error_out) { |
457 | 3 | error_out->emplace_back(error); |
458 | 3 | } else { |
459 | 1 | LogWarning("%s", error); |
460 | 1 | } |
461 | 4 | } |
462 | 4 | } |
463 | | |
464 | | bool ArgsManager::ReadSettingsFile(std::vector<std::string>* errors) |
465 | 1.17k | { |
466 | 1.17k | fs::path path; |
467 | 1.17k | if (!GetSettingsPath(&path, /* temp= */ false)) { |
468 | 0 | return true; // Do nothing if settings file disabled. |
469 | 0 | } |
470 | | |
471 | 1.17k | LOCK(cs_args); |
472 | 1.17k | m_settings.rw_settings.clear(); |
473 | 1.17k | std::vector<std::string> read_errors; |
474 | 1.17k | if (!common::ReadSettings(path, m_settings.rw_settings, read_errors)) { |
475 | 3 | SaveErrors(read_errors, errors); |
476 | 3 | return false; |
477 | 3 | } |
478 | 1.17k | for (const auto& setting : m_settings.rw_settings) { |
479 | 126 | KeyInfo key = InterpretKey(setting.first); // Split setting key into section and argname |
480 | 126 | if (!GetArgFlags_('-' + key.name)) { |
481 | 7 | LogWarning("Ignoring unknown rw_settings value %s", setting.first); |
482 | 7 | } |
483 | 126 | } |
484 | 1.17k | return true; |
485 | 1.17k | } |
486 | | |
487 | | bool ArgsManager::WriteSettingsFile(std::vector<std::string>* errors, bool backup) const |
488 | 1.36k | { |
489 | 1.36k | fs::path path, path_tmp; |
490 | 1.36k | if (!GetSettingsPath(&path, /*temp=*/false, backup) || !GetSettingsPath(&path_tmp, /*temp=*/true, backup)) { |
491 | 0 | throw std::logic_error("Attempt to write settings file when dynamic settings are disabled."); |
492 | 0 | } |
493 | | |
494 | 1.36k | LOCK(cs_args); |
495 | 1.36k | std::vector<std::string> write_errors; |
496 | 1.36k | if (!common::WriteSettings(path_tmp, m_settings.rw_settings, write_errors)) { |
497 | 0 | SaveErrors(write_errors, errors); |
498 | 0 | return false; |
499 | 0 | } |
500 | 1.36k | if (!RenameOver(path_tmp, path)) { |
501 | 1 | SaveErrors({strprintf("Failed renaming settings file %s to %s\n", fs::PathToString(path_tmp), fs::PathToString(path))}, errors); |
502 | 1 | return false; |
503 | 1 | } |
504 | 1.36k | return true; |
505 | 1.36k | } |
506 | | |
507 | | common::SettingsValue ArgsManager::GetPersistentSetting(const std::string& name) const |
508 | 0 | { |
509 | 0 | LOCK(cs_args); |
510 | 0 | return common::GetSetting(m_settings, m_network, name, !UseDefaultSection("-" + name), |
511 | 0 | /*ignore_nonpersistent=*/true, /*get_chain_type=*/false); |
512 | 0 | } |
513 | | |
514 | | bool ArgsManager::IsArgNegated(const std::string& strArg) const |
515 | 53.9k | { |
516 | 53.9k | return GetSetting(strArg).isFalse(); |
517 | 53.9k | } |
518 | | |
519 | | std::string ArgsManager::GetArg(const std::string& strArg, const std::string& strDefault) const |
520 | 65.8k | { |
521 | 65.8k | return GetArg(strArg).value_or(strDefault); |
522 | 65.8k | } |
523 | | |
524 | | std::optional<std::string> ArgsManager::GetArg(const std::string& strArg) const |
525 | 105k | { |
526 | 105k | const common::SettingsValue value = GetSetting(strArg); |
527 | 105k | return SettingToString(value); |
528 | 105k | } |
529 | | |
530 | | std::optional<std::string> SettingToString(const common::SettingsValue& value) |
531 | 136k | { |
532 | 136k | if (value.isNull()) return std::nullopt; |
533 | 67.0k | if (value.isFalse()) return "0"; |
534 | 55.2k | if (value.isTrue()) return "1"; |
535 | 55.2k | if (value.isNum()) return value.getValStr(); |
536 | 55.2k | return value.get_str(); |
537 | 55.2k | } |
538 | | |
539 | | std::string SettingToString(const common::SettingsValue& value, const std::string& strDefault) |
540 | 30.6k | { |
541 | 30.6k | return SettingToString(value).value_or(strDefault); |
542 | 30.6k | } |
543 | | |
544 | | template <std::integral Int> |
545 | | Int ArgsManager::GetArg(const std::string& strArg, Int nDefault) const |
546 | 96.6k | { |
547 | 96.6k | return GetArg<Int>(strArg).value_or(nDefault); |
548 | 96.6k | } Unexecuted instantiation: signed char ArgsManager::GetArg<signed char>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, signed char) const unsigned char ArgsManager::GetArg<unsigned char>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, unsigned char) const Line | Count | Source | 546 | 7 | { | 547 | 7 | return GetArg<Int>(strArg).value_or(nDefault); | 548 | 7 | } |
Unexecuted instantiation: short ArgsManager::GetArg<short>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, short) const Unexecuted instantiation: unsigned short ArgsManager::GetArg<unsigned short>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, unsigned short) const int ArgsManager::GetArg<int>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, int) const Line | Count | Source | 546 | 2.23k | { | 547 | 2.23k | return GetArg<Int>(strArg).value_or(nDefault); | 548 | 2.23k | } |
Unexecuted instantiation: unsigned int ArgsManager::GetArg<unsigned int>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, unsigned int) const long ArgsManager::GetArg<long>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, long) const Line | Count | Source | 546 | 94.3k | { | 547 | 94.3k | return GetArg<Int>(strArg).value_or(nDefault); | 548 | 94.3k | } |
Unexecuted instantiation: unsigned long ArgsManager::GetArg<unsigned long>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, unsigned long) const |
549 | | |
550 | | template <std::integral Int> |
551 | | std::optional<Int> ArgsManager::GetArg(const std::string& strArg) const |
552 | 136k | { |
553 | 136k | const common::SettingsValue value = GetSetting(strArg); |
554 | 136k | return SettingTo<Int>(value); |
555 | 136k | } Unexecuted instantiation: std::optional<signed char> ArgsManager::GetArg<signed char>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&) const std::optional<unsigned char> ArgsManager::GetArg<unsigned char>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&) const Line | Count | Source | 552 | 12 | { | 553 | 12 | const common::SettingsValue value = GetSetting(strArg); | 554 | 12 | return SettingTo<Int>(value); | 555 | 12 | } |
Unexecuted instantiation: std::optional<short> ArgsManager::GetArg<short>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&) const Unexecuted instantiation: std::optional<unsigned short> ArgsManager::GetArg<unsigned short>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&) const std::optional<int> ArgsManager::GetArg<int>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&) const Line | Count | Source | 552 | 5.15k | { | 553 | 5.15k | const common::SettingsValue value = GetSetting(strArg); | 554 | 5.15k | return SettingTo<Int>(value); | 555 | 5.15k | } |
Unexecuted instantiation: std::optional<unsigned int> ArgsManager::GetArg<unsigned int>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&) const std::optional<long> ArgsManager::GetArg<long>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&) const Line | Count | Source | 552 | 124k | { | 553 | 124k | const common::SettingsValue value = GetSetting(strArg); | 554 | 124k | return SettingTo<Int>(value); | 555 | 124k | } |
std::optional<unsigned long> ArgsManager::GetArg<unsigned long>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&) const Line | Count | Source | 552 | 6.13k | { | 553 | 6.13k | const common::SettingsValue value = GetSetting(strArg); | 554 | 6.13k | return SettingTo<Int>(value); | 555 | 6.13k | } |
|
556 | | |
557 | | template <std::integral Int> |
558 | | std::optional<Int> SettingTo(const common::SettingsValue& value) |
559 | 136k | { |
560 | 136k | if (value.isNull()) return std::nullopt; |
561 | 15.3k | if (value.isFalse()) return 0; |
562 | 15.3k | if (value.isTrue()) return 1; |
563 | 15.3k | if (value.isNum()) return value.getInt<Int>(); |
564 | 15.2k | return LocaleIndependentAtoi<Int>(value.get_str()); |
565 | 15.3k | } Unexecuted instantiation: std::optional<signed char> SettingTo<signed char>(UniValue const&) std::optional<unsigned char> SettingTo<unsigned char>(UniValue const&) Line | Count | Source | 559 | 12 | { | 560 | 12 | if (value.isNull()) return std::nullopt; | 561 | 9 | if (value.isFalse()) return 0; | 562 | 9 | if (value.isTrue()) return 1; | 563 | 9 | if (value.isNum()) return value.getInt<Int>(); | 564 | 9 | return LocaleIndependentAtoi<Int>(value.get_str()); | 565 | 9 | } |
Unexecuted instantiation: std::optional<short> SettingTo<short>(UniValue const&) Unexecuted instantiation: std::optional<unsigned short> SettingTo<unsigned short>(UniValue const&) std::optional<int> SettingTo<int>(UniValue const&) Line | Count | Source | 559 | 5.15k | { | 560 | 5.15k | if (value.isNull()) return std::nullopt; | 561 | 3.32k | if (value.isFalse()) return 0; | 562 | 3.32k | if (value.isTrue()) return 1; | 563 | 3.32k | if (value.isNum()) return value.getInt<Int>(); | 564 | 3.32k | return LocaleIndependentAtoi<Int>(value.get_str()); | 565 | 3.32k | } |
Unexecuted instantiation: std::optional<unsigned int> SettingTo<unsigned int>(UniValue const&) std::optional<long> SettingTo<long>(UniValue const&) Line | Count | Source | 559 | 124k | { | 560 | 124k | if (value.isNull()) return std::nullopt; | 561 | 11.9k | if (value.isFalse()) return 0; | 562 | 11.9k | if (value.isTrue()) return 1; | 563 | 11.9k | if (value.isNum()) return value.getInt<Int>(); | 564 | 11.9k | return LocaleIndependentAtoi<Int>(value.get_str()); | 565 | 11.9k | } |
std::optional<unsigned long> SettingTo<unsigned long>(UniValue const&) Line | Count | Source | 559 | 6.13k | { | 560 | 6.13k | if (value.isNull()) return std::nullopt; | 561 | 16 | if (value.isFalse()) return 0; | 562 | 16 | if (value.isTrue()) return 1; | 563 | 16 | if (value.isNum()) return value.getInt<Int>(); | 564 | 16 | return LocaleIndependentAtoi<Int>(value.get_str()); | 565 | 16 | } |
|
566 | | |
567 | | template <std::integral Int> |
568 | | Int SettingTo(const common::SettingsValue& value, Int nDefault) |
569 | 0 | { |
570 | 0 | return SettingTo<Int>(value).value_or(nDefault); |
571 | 0 | } Unexecuted instantiation: signed char SettingTo<signed char>(UniValue const&, signed char) Unexecuted instantiation: unsigned char SettingTo<unsigned char>(UniValue const&, unsigned char) Unexecuted instantiation: short SettingTo<short>(UniValue const&, short) Unexecuted instantiation: unsigned short SettingTo<unsigned short>(UniValue const&, unsigned short) Unexecuted instantiation: int SettingTo<int>(UniValue const&, int) Unexecuted instantiation: unsigned int SettingTo<unsigned int>(UniValue const&, unsigned int) Unexecuted instantiation: long SettingTo<long>(UniValue const&, long) Unexecuted instantiation: unsigned long SettingTo<unsigned long>(UniValue const&, unsigned long) |
572 | | |
573 | | bool ArgsManager::GetBoolArg(const std::string& strArg, bool fDefault) const |
574 | 335k | { |
575 | 335k | return GetBoolArg(strArg).value_or(fDefault); |
576 | 335k | } |
577 | | |
578 | | std::optional<bool> ArgsManager::GetBoolArg(const std::string& strArg) const |
579 | 360k | { |
580 | 360k | const common::SettingsValue value = GetSetting(strArg); |
581 | 360k | return SettingToBool(value); |
582 | 360k | } |
583 | | |
584 | | std::optional<bool> SettingToBool(const common::SettingsValue& value) |
585 | 360k | { |
586 | 360k | if (value.isNull()) return std::nullopt; |
587 | 228k | if (value.isBool()) return value.get_bool(); |
588 | 226k | return InterpretBool(value.get_str()); |
589 | 228k | } |
590 | | |
591 | | bool SettingToBool(const common::SettingsValue& value, bool fDefault) |
592 | 0 | { |
593 | 0 | return SettingToBool(value).value_or(fDefault); |
594 | 0 | } |
595 | | |
596 | | #define INSTANTIATE_INT_TYPE(Type) \ |
597 | | template Type ArgsManager::GetArg<Type>(const std::string&, Type) const; \ |
598 | | template std::optional<Type> ArgsManager::GetArg<Type>(const std::string&) const; \ |
599 | | template Type SettingTo<Type>(const common::SettingsValue&, Type); \ |
600 | | template std::optional<Type> SettingTo<Type>(const common::SettingsValue&) |
601 | | |
602 | | INSTANTIATE_INT_TYPE(int8_t); |
603 | | INSTANTIATE_INT_TYPE(uint8_t); |
604 | | INSTANTIATE_INT_TYPE(int16_t); |
605 | | INSTANTIATE_INT_TYPE(uint16_t); |
606 | | INSTANTIATE_INT_TYPE(int32_t); |
607 | | INSTANTIATE_INT_TYPE(uint32_t); |
608 | | INSTANTIATE_INT_TYPE(int64_t); |
609 | | INSTANTIATE_INT_TYPE(uint64_t); |
610 | | |
611 | | #undef INSTANTIATE_INT_TYPE |
612 | | |
613 | | bool ArgsManager::SoftSetArg(const std::string& strArg, const std::string& strValue) |
614 | 52.3k | { |
615 | 52.3k | LOCK(cs_args); |
616 | 52.3k | if (!GetSetting_(strArg).isNull()) return false; |
617 | 1.96k | m_settings.forced_settings[SettingName(strArg)] = strValue; |
618 | 1.96k | return true; |
619 | 52.3k | } |
620 | | |
621 | | bool ArgsManager::SoftSetBoolArg(const std::string& strArg, bool fValue) |
622 | 4.86k | { |
623 | 4.86k | if (fValue) |
624 | 2.30k | return SoftSetArg(strArg, std::string("1")); |
625 | 2.55k | else |
626 | 2.55k | return SoftSetArg(strArg, std::string("0")); |
627 | 4.86k | } |
628 | | |
629 | | void ArgsManager::ForceSetArg(const std::string& strArg, const std::string& strValue) |
630 | 51.0k | { |
631 | 51.0k | LOCK(cs_args); |
632 | 51.0k | m_settings.forced_settings[SettingName(strArg)] = strValue; |
633 | 51.0k | } |
634 | | |
635 | | void ArgsManager::AddCommand(const std::string& cmd, const std::string& help, std::set<std::string> options) |
636 | 238 | { |
637 | 238 | Assert(cmd.find('=') == std::string::npos); |
638 | 238 | Assert(cmd.at(0) != '-'); |
639 | | |
640 | 238 | LOCK(cs_args); |
641 | 238 | m_accept_any_command = false; // latch to false |
642 | 238 | std::map<std::string, Arg>& arg_map = m_available_args[OptionsCategory::COMMANDS]; |
643 | 238 | auto ret = arg_map.emplace(cmd, Arg{"", help, ArgsManager::COMMAND}); |
644 | 238 | if (!options.empty()) { |
645 | 110 | auto& cmdopts = m_available_args[OptionsCategory::COMMAND_OPTIONS]; |
646 | 110 | bool command_has_all_options_defined = true; |
647 | 126 | for (const auto& opt : options) { |
648 | 126 | if (!cmdopts.contains(opt)) { |
649 | 0 | command_has_all_options_defined = false; |
650 | 0 | } |
651 | 126 | } |
652 | 110 | Assert(command_has_all_options_defined); |
653 | | |
654 | 110 | m_command_args.try_emplace(cmd, std::move(options)); |
655 | 110 | } |
656 | 238 | Assert(ret.second); // Fail on duplicate commands |
657 | 238 | } |
658 | | |
659 | | void ArgsManager::AddArg(const std::string& name, const std::string& help, unsigned int flags, const OptionsCategory& cat) |
660 | 462k | { |
661 | 462k | Assert((flags & ArgsManager::COMMAND) == 0); // use AddCommand |
662 | | |
663 | | // Split arg name from its help param |
664 | 462k | size_t eq_index = name.find('='); |
665 | 462k | if (eq_index == std::string::npos) { |
666 | 241k | eq_index = name.size(); |
667 | 241k | } |
668 | 462k | std::string arg_name = name.substr(0, eq_index); |
669 | | |
670 | 462k | LOCK(cs_args); |
671 | | |
672 | | // Allow duplicates involving HIDDEN — it is used as a placeholder for args |
673 | | // unavailable in this binary but tolerated for shared config files (see #13441). |
674 | 2.25M | for (const auto& arg_map : m_available_args) { |
675 | 2.25M | if (arg_map.first == OptionsCategory::HIDDEN || cat == OptionsCategory::HIDDEN) continue; |
676 | 1.50M | Assert(!arg_map.second.contains(arg_name)); |
677 | 1.50M | } |
678 | | |
679 | 462k | std::map<std::string, Arg>& arg_map = m_available_args[cat]; |
680 | 462k | auto ret = arg_map.emplace(arg_name, Arg{name.substr(eq_index, name.size() - eq_index), help, flags}); |
681 | 462k | assert(ret.second); // Make sure an insertion actually happened |
682 | | |
683 | 462k | if (flags & ArgsManager::NETWORK_ONLY) { |
684 | 40.1k | m_network_only_args.emplace(arg_name); |
685 | 40.1k | } |
686 | 462k | } |
687 | | |
688 | | void ArgsManager::AddHiddenArgs(const std::vector<std::string>& names) |
689 | 5.07k | { |
690 | 40.5k | for (const std::string& name : names) { |
691 | 40.5k | AddArg(name, "", ArgsManager::ALLOW_ANY, OptionsCategory::HIDDEN); |
692 | 40.5k | } |
693 | 5.07k | } |
694 | | |
695 | | void ArgsManager::ClearArgs() |
696 | 696 | { |
697 | 696 | LOCK(cs_args); |
698 | 696 | m_settings = {}; |
699 | 696 | m_available_args.clear(); |
700 | 696 | m_command_args.clear(); |
701 | 696 | m_network_only_args.clear(); |
702 | 696 | m_config_sections.clear(); |
703 | 696 | } |
704 | | |
705 | | void ArgsManager::CheckMultipleCLIArgs() const |
706 | 1.11k | { |
707 | 1.11k | LOCK(cs_args); |
708 | 1.11k | std::vector<std::string> found{}; |
709 | 1.11k | auto cmds = m_available_args.find(OptionsCategory::CLI_COMMANDS); |
710 | 1.11k | if (cmds != m_available_args.end()) { |
711 | 4.44k | for (const auto& [cmd, argspec] : cmds->second) { |
712 | 4.44k | if (!GetSetting_(cmd).isNull()) { |
713 | 42 | found.push_back(cmd); |
714 | 42 | } |
715 | 4.44k | } |
716 | 1.11k | if (found.size() > 1) { |
717 | 1 | throw std::runtime_error(strprintf("Only one of %s may be specified.", util::Join(found, ", "))); |
718 | 1 | } |
719 | 1.11k | } |
720 | 1.11k | } |
721 | | |
722 | | std::string ArgsManager::GetHelpMessage() const |
723 | 2 | { |
724 | 2 | const bool show_debug = GetBoolArg("-help-debug", false); |
725 | | |
726 | 2 | std::string usage; |
727 | 2 | LOCK(cs_args); |
728 | | |
729 | 2 | const auto command_options = m_available_args.find(OptionsCategory::COMMAND_OPTIONS); |
730 | 2 | const auto for_matching_cmd_opts = [&](const std::set<std::string>& select, auto&& fn) EXCLUSIVE_LOCKS_REQUIRED(cs_args) { |
731 | 1 | if (select.empty()) return; |
732 | 1 | if (command_options == m_available_args.end()) return; |
733 | 1 | for (const auto& [name, info] : command_options->second) { |
734 | 1 | if (!show_debug && (info.m_flags & ArgsManager::DEBUG_ONLY)) continue; |
735 | 1 | if (!select.contains(name)) continue; |
736 | 1 | fn(name, info); |
737 | 1 | } |
738 | 1 | }; |
739 | | |
740 | 12 | for (const auto& [category, category_args] : m_available_args) { |
741 | 12 | switch(category) { |
742 | 1 | case OptionsCategory::OPTIONS: |
743 | 1 | usage += HelpMessageGroup("Options:"); |
744 | 1 | break; |
745 | 1 | case OptionsCategory::CONNECTION: |
746 | 1 | usage += HelpMessageGroup("Connection options:"); |
747 | 1 | break; |
748 | 0 | case OptionsCategory::ZMQ: |
749 | 0 | usage += HelpMessageGroup("ZeroMQ notification options:"); |
750 | 0 | break; |
751 | 1 | case OptionsCategory::DEBUG_TEST: |
752 | 1 | usage += HelpMessageGroup("Debugging/Testing options:"); |
753 | 1 | break; |
754 | 1 | case OptionsCategory::NODE_RELAY: |
755 | 1 | usage += HelpMessageGroup("Node relay options:"); |
756 | 1 | break; |
757 | 1 | case OptionsCategory::BLOCK_CREATION: |
758 | 1 | usage += HelpMessageGroup("Block creation options:"); |
759 | 1 | break; |
760 | 1 | case OptionsCategory::RPC: |
761 | 1 | usage += HelpMessageGroup("RPC server options:"); |
762 | 1 | break; |
763 | 0 | case OptionsCategory::IPC: |
764 | 0 | usage += HelpMessageGroup("IPC interprocess connection options:"); |
765 | 0 | break; |
766 | 1 | case OptionsCategory::WALLET: |
767 | 1 | usage += HelpMessageGroup("Wallet options:"); |
768 | 1 | break; |
769 | 1 | case OptionsCategory::WALLET_DEBUG_TEST: |
770 | 1 | if (show_debug) usage += HelpMessageGroup("Wallet debugging/testing options:"); |
771 | 1 | break; |
772 | 1 | case OptionsCategory::CHAINPARAMS: |
773 | 1 | usage += HelpMessageGroup("Chain selection options:"); |
774 | 1 | break; |
775 | 0 | case OptionsCategory::GUI: |
776 | 0 | usage += HelpMessageGroup("UI Options:"); |
777 | 0 | break; |
778 | 1 | case OptionsCategory::COMMANDS: |
779 | 1 | usage += HelpMessageGroup("Commands:"); |
780 | 1 | break; |
781 | 0 | case OptionsCategory::REGISTER_COMMANDS: |
782 | 0 | usage += HelpMessageGroup("Register Commands:"); |
783 | 0 | break; |
784 | 0 | case OptionsCategory::CLI_COMMANDS: |
785 | 0 | usage += HelpMessageGroup("CLI Commands:"); |
786 | 0 | break; |
787 | 1 | case OptionsCategory::COMMAND_OPTIONS: |
788 | 2 | case OptionsCategory::HIDDEN: |
789 | 2 | break; |
790 | 12 | } // no default case, so the compiler can warn about missing cases |
791 | | |
792 | 12 | if (category == OptionsCategory::COMMAND_OPTIONS) continue; |
793 | | |
794 | | // When we get to the hidden options, stop |
795 | 11 | if (category == OptionsCategory::HIDDEN) break; |
796 | | |
797 | 172 | for (const auto& [arg_name, arg_info] : category_args) { |
798 | 172 | if (show_debug || !(arg_info.m_flags & ArgsManager::DEBUG_ONLY)) { |
799 | 132 | usage += HelpMessageOpt(arg_name, arg_info.m_help_param, arg_info.m_help_text); |
800 | | |
801 | 132 | if (category == OptionsCategory::COMMANDS) { |
802 | 1 | const auto cmd_args = m_command_args.find(arg_name); |
803 | 1 | if (cmd_args == m_command_args.end()) continue; |
804 | 1 | for_matching_cmd_opts(cmd_args->second, [&](const auto& cmdopt_name, const auto& cmdopt_info) { |
805 | 1 | usage += HelpMessageOpt(cmdopt_name, cmdopt_info.m_help_param, cmdopt_info.m_help_text, /*subopt=*/true); |
806 | 1 | }); |
807 | 1 | } |
808 | 132 | } |
809 | 172 | } |
810 | 10 | } |
811 | 2 | return usage; |
812 | 2 | } |
813 | | |
814 | | bool HelpRequested(const ArgsManager& args) |
815 | 2.43k | { |
816 | 2.43k | return args.IsArgSet("-?") || args.IsArgSet("-h") || args.IsArgSet("-help") || args.IsArgSet("-help-debug"); |
817 | 2.43k | } |
818 | | |
819 | | void SetupHelpOptions(ArgsManager& args) |
820 | 3.17k | { |
821 | 3.17k | args.AddArg("-help", "Print this help message and exit (also -h or -?)", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); |
822 | 3.17k | args.AddHiddenArgs({"-h", "-?"}); |
823 | 3.17k | } |
824 | | |
825 | 9 | std::string HelpMessageGroup(const std::string &message) { |
826 | 9 | return std::string(message) + std::string("\n\n"); |
827 | 9 | } |
828 | | |
829 | | std::string HelpMessageOpt(std::string_view option, std::string_view help_param, std::string_view message, bool subopt) |
830 | 133 | { |
831 | 133 | constexpr int screen_width = 79; |
832 | 133 | int opt_indent = 2; |
833 | 133 | int msg_indent = 7; |
834 | | |
835 | 133 | if (subopt) { |
836 | 1 | int bump = msg_indent - opt_indent; |
837 | 1 | opt_indent += bump; // opt_indent now at the old msg_indent level |
838 | 1 | msg_indent += bump; // indent by the same amount |
839 | 1 | } |
840 | 133 | int msg_width = screen_width - msg_indent; |
841 | | |
842 | 133 | return strprintf("%*s%s%s\n%*s%s\n\n", |
843 | 133 | opt_indent, "", option, help_param, |
844 | 133 | msg_indent, "", FormatParagraph(message, msg_width, msg_indent)); |
845 | 133 | } |
846 | | |
847 | | const std::vector<std::string> TEST_OPTIONS_DOC{ |
848 | | "addrman (use deterministic addrman)", |
849 | | "reindex_after_failure_noninteractive_yes (When asked for a reindex after failure interactively, simulate as-if answered with 'yes')", |
850 | | "bip94 (enforce BIP94 consensus rules)", |
851 | | }; |
852 | | |
853 | | bool HasTestOption(const ArgsManager& args, const std::string& test_option) |
854 | 4.40k | { |
855 | 4.40k | const auto options = args.GetArgs("-test"); |
856 | 4.40k | return std::any_of(options.begin(), options.end(), [test_option](const auto& option) { |
857 | 31 | return option == test_option; |
858 | 31 | }); |
859 | 4.40k | } |
860 | | |
861 | | fs::path GetDefaultDataDir() |
862 | 1.14k | { |
863 | | // Windows: |
864 | | // old: C:\Users\Username\AppData\Roaming\Bitcoin |
865 | | // new: C:\Users\Username\AppData\Local\Bitcoin |
866 | | // macOS: ~/Library/Application Support/Bitcoin |
867 | | // Unix-like: ~/.bitcoin |
868 | | #ifdef WIN32 |
869 | | // Windows |
870 | | // Check for existence of datadir in old location and keep it there |
871 | | fs::path legacy_path = GetSpecialFolderPath(CSIDL_APPDATA) / "Bitcoin"; |
872 | | if (fs::exists(legacy_path)) return legacy_path; |
873 | | |
874 | | // Otherwise, fresh installs can start in the new, "proper" location |
875 | | return GetSpecialFolderPath(CSIDL_LOCAL_APPDATA) / "Bitcoin"; |
876 | | #else |
877 | 1.14k | fs::path pathRet; |
878 | 1.14k | char* pszHome = getenv("HOME"); |
879 | 1.14k | if (pszHome == nullptr || strlen(pszHome) == 0) |
880 | 0 | pathRet = fs::path("/"); |
881 | 1.14k | else |
882 | 1.14k | pathRet = fs::path(pszHome); |
883 | | #ifdef __APPLE__ |
884 | | // macOS |
885 | | return pathRet / "Library/Application Support/Bitcoin"; |
886 | | #else |
887 | | // Unix-like |
888 | 1.14k | return pathRet / ".bitcoin"; |
889 | 1.14k | #endif |
890 | 1.14k | #endif |
891 | 1.14k | } |
892 | | |
893 | | bool CheckDataDirOption(const ArgsManager& args) |
894 | 4.63k | { |
895 | 4.63k | const fs::path datadir{args.GetPathArg("-datadir")}; |
896 | 4.63k | return datadir.empty() || fs::is_directory(fs::absolute(datadir)); |
897 | 4.63k | } |
898 | | |
899 | | fs::path ArgsManager::GetConfigFilePath() const |
900 | 3.45k | { |
901 | 3.45k | LOCK(cs_args); |
902 | 3.45k | return *Assert(m_config_path); |
903 | 3.45k | } |
904 | | |
905 | | void ArgsManager::SetConfigFilePath(fs::path path) |
906 | 1 | { |
907 | 1 | LOCK(cs_args); |
908 | 1 | assert(!m_config_path); |
909 | 1 | m_config_path = path; |
910 | 1 | } |
911 | | |
912 | | ChainType ArgsManager::GetChainType() const |
913 | 4.30k | { |
914 | 4.30k | std::variant<ChainType, std::string> arg = GetChainArg(); |
915 | 4.30k | if (auto* parsed = std::get_if<ChainType>(&arg)) return *parsed; |
916 | 0 | throw std::runtime_error(strprintf("Unknown chain %s.", std::get<std::string>(arg))); |
917 | 4.30k | } |
918 | | |
919 | | std::string ArgsManager::GetChainTypeString() const |
920 | 5.99k | { |
921 | 5.99k | auto arg = GetChainArg(); |
922 | 5.99k | if (auto* parsed = std::get_if<ChainType>(&arg)) return ChainTypeToString(*parsed); |
923 | 187 | return std::get<std::string>(arg); |
924 | 5.99k | } |
925 | | |
926 | | std::variant<ChainType, std::string> ArgsManager::GetChainArg() const |
927 | 10.3k | { |
928 | 41.2k | auto get_net = [&](const std::string& arg) { |
929 | 41.2k | LOCK(cs_args); |
930 | 41.2k | common::SettingsValue value = common::GetSetting(m_settings, /* section= */ "", SettingName(arg), |
931 | 41.2k | /* ignore_default_section_config= */ false, |
932 | 41.2k | /*ignore_nonpersistent=*/false, |
933 | 41.2k | /* get_chain_type= */ true); |
934 | 41.2k | return value.isNull() ? false : value.isBool() ? value.get_bool() : InterpretBool(value.get_str()); |
935 | 41.2k | }; |
936 | | |
937 | 10.3k | const bool fRegTest = get_net("-regtest"); |
938 | 10.3k | const bool fSigNet = get_net("-signet"); |
939 | 10.3k | const bool fTestNet = get_net("-testnet"); |
940 | 10.3k | const bool fTestNet4 = get_net("-testnet4"); |
941 | 10.3k | const auto chain_arg = GetArg("-chain"); |
942 | | |
943 | 10.3k | if ((int)chain_arg.has_value() + (int)fRegTest + (int)fSigNet + (int)fTestNet + (int)fTestNet4 > 1) { |
944 | 187 | throw std::runtime_error("Invalid combination of -regtest, -signet, -testnet, -testnet4 and -chain. Can use at most one."); |
945 | 187 | } |
946 | 10.1k | if (chain_arg) { |
947 | 32 | if (auto parsed = ChainTypeFromString(*chain_arg)) return *parsed; |
948 | | // Not a known string, so return original string |
949 | 0 | return *chain_arg; |
950 | 32 | } |
951 | 10.0k | if (fRegTest) return ChainType::REGTEST; |
952 | 1.85k | if (fSigNet) return ChainType::SIGNET; |
953 | 1.72k | if (fTestNet) return ChainType::TESTNET; |
954 | 1.71k | if (fTestNet4) return ChainType::TESTNET4; |
955 | 1.37k | return ChainType::MAIN; |
956 | 1.71k | } |
957 | | |
958 | | bool ArgsManager::UseDefaultSection(const std::string& arg) const |
959 | 1.04M | { |
960 | 1.04M | AssertLockHeld(cs_args); |
961 | 1.04M | return m_network == ChainTypeToString(ChainType::MAIN) || !m_network_only_args.contains(arg); |
962 | 1.04M | } |
963 | | |
964 | | common::SettingsValue ArgsManager::GetSetting_(const std::string& arg) const |
965 | 824k | { |
966 | 824k | AssertLockHeld(cs_args); |
967 | 824k | return common::GetSetting( |
968 | 824k | m_settings, m_network, SettingName(arg), !UseDefaultSection(arg), |
969 | 824k | /*ignore_nonpersistent=*/false, /*get_chain_type=*/false); |
970 | 824k | } |
971 | | |
972 | | common::SettingsValue ArgsManager::GetSetting(const std::string& arg) const |
973 | 734k | { |
974 | 734k | LOCK(cs_args); |
975 | 734k | return GetSetting_(arg); |
976 | 734k | } |
977 | | |
978 | | std::vector<common::SettingsValue> ArgsManager::GetSettingsList(const std::string& arg) const |
979 | 222k | { |
980 | 222k | LOCK(cs_args); |
981 | 222k | return common::GetSettingsList(m_settings, m_network, SettingName(arg), !UseDefaultSection(arg)); |
982 | 222k | } |
983 | | |
984 | | void ArgsManager::logArgsPrefix( |
985 | | const std::string& prefix, |
986 | | const std::string& section, |
987 | | const std::map<std::string, std::vector<common::SettingsValue>>& args) const |
988 | 3.41k | { |
989 | 3.41k | AssertLockHeld(cs_args); |
990 | 3.41k | std::string section_str = section.empty() ? "" : "[" + section + "] "; |
991 | 39.6k | for (const auto& arg : args) { |
992 | 41.0k | for (const auto& value : arg.second) { |
993 | 41.0k | std::optional<unsigned int> flags = GetArgFlags_('-' + arg.first); |
994 | 41.0k | if (flags) { |
995 | 41.0k | std::string value_str = (*flags & SENSITIVE) ? "****" : value.write(); |
996 | 41.0k | LogInfo("%s %s%s=%s\n", prefix, section_str, arg.first, value_str); |
997 | 41.0k | } |
998 | 41.0k | } |
999 | 39.6k | } |
1000 | 3.41k | } |
1001 | | |
1002 | | void ArgsManager::LogArgs() const |
1003 | 1.14k | { |
1004 | 1.14k | LOCK(cs_args); |
1005 | 2.27k | for (const auto& section : m_settings.ro_config) { |
1006 | 2.27k | logArgsPrefix("Config file arg:", section.first, section.second); |
1007 | 2.27k | } |
1008 | 1.14k | for (const auto& setting : m_settings.rw_settings) { |
1009 | 125 | LogInfo("Setting file arg: %s = %s\n", setting.first, setting.second.write()); |
1010 | 125 | } |
1011 | 1.14k | logArgsPrefix("Command-line arg:", "", m_settings.command_line_options); |
1012 | 1.14k | } |