LCOV - code coverage report
Current view: top level - src/common - args.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 71.3 % 501 357
Test Date: 2026-03-16 04:21:26 Functions: 53.6 % 97 52
Branches: 43.0 % 912 392

             Branch data     Line data    Source code
       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                 :       13125 : static bool InterpretBool(const std::string& strValue)
      58                 :             : {
      59         [ +  + ]:       13125 :     if (strValue.empty())
      60                 :             :         return true;
      61         [ -  + ]:       10114 :     return (LocaleIndependentAtoi<int>(strValue) != 0);
      62                 :             : }
      63                 :             : 
      64                 :      673648 : static std::string SettingName(const std::string& arg)
      65                 :             : {
      66   [ -  +  +  +  :      717682 :     return arg.size() > 0 && arg[0] == '-' ? arg.substr(1) : arg;
                   +  + ]
      67                 :             : }
      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                 :       60699 : KeyInfo InterpretKey(std::string key)
      78                 :             : {
      79         [ +  + ]:       60699 :     KeyInfo result;
      80                 :             :     // Split section name from key name for keys like "testnet.foo" or "regtest.bar"
      81                 :       60699 :     size_t option_index = key.find('.');
      82         [ +  + ]:       60699 :     if (option_index != std::string::npos) {
      83         [ +  - ]:        1430 :         result.section = key.substr(0, option_index);
      84         [ +  - ]:        1430 :         key.erase(0, option_index + 1);
      85                 :             :     }
      86   [ -  +  +  + ]:       60699 :     if (key.starts_with("no")) {
      87         [ +  - ]:       14546 :         key.erase(0, 2);
      88                 :       14546 :         result.negated = true;
      89                 :             :     }
      90         [ +  - ]:       60699 :     result.name = key;
      91                 :       60699 :     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                 :       53753 : std::optional<common::SettingsValue> InterpretValue(const KeyInfo& key, const std::string* value,
     106                 :             :                                                   unsigned int flags, std::string& error)
     107                 :             : {
     108                 :             :     // Return negated settings as false values.
     109         [ +  + ]:       53753 :     if (key.negated) {
     110         [ +  + ]:       12887 :         if (flags & ArgsManager::DISALLOW_NEGATION) {
     111                 :         226 :             error = strprintf("Negating of -%s is meaningless and therefore forbidden", key.name);
     112                 :         226 :             return std::nullopt;
     113                 :             :         }
     114                 :             :         // Double negatives like -nofoo=0 are supported (but discouraged)
     115   [ +  +  +  + ]:       12661 :         if (value && !InterpretBool(*value)) {
     116                 :        3392 :             LogWarning("Parsed potentially confusing double-negative -%s=%s", key.name, *value);
     117                 :        3392 :             return true;
     118                 :             :         }
     119                 :        9269 :         return false;
     120                 :             :     }
     121   [ +  +  +  + ]:       40866 :     if (!value && (flags & ArgsManager::DISALLOW_ELISION)) {
     122                 :         483 :         error = strprintf("Can not set -%s with no value. Please specify value with -%s=value.", key.name, key.name);
     123                 :         483 :         return std::nullopt;
     124                 :             :     }
     125         [ +  - ]:       40383 :     return value ? *value : "";
     126                 :             : }
     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                 :        1511 : ArgsManager::ArgsManager() = default;
     132         [ -  + ]:        7555 : ArgsManager::~ArgsManager() = default;
     133                 :             : 
     134                 :        1280 : std::set<std::string> ArgsManager::GetUnsuitableSectionOnlyArgs() const
     135                 :             : {
     136         [ +  - ]:        1280 :     std::set<std::string> unsuitables;
     137                 :             : 
     138         [ +  - ]:        1280 :     LOCK(cs_args);
     139                 :             : 
     140                 :             :     // if there's no section selected, don't worry
     141         [ +  + ]:        1280 :     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   [ +  -  +  + ]:        1013 :     if (m_network == ChainTypeToString(ChainType::MAIN)) return std::set<std::string> {};
     145                 :             : 
     146         [ +  + ]:        9557 :     for (const auto& arg : m_network_only_args) {
     147   [ +  -  +  -  :        8555 :         if (OnlyHasDefaultSectionSetting(m_settings, m_network, SettingName(arg))) {
                   -  + ]
     148         [ #  # ]:           0 :             unsuitables.insert(arg);
     149                 :             :         }
     150                 :             :     }
     151                 :        1002 :     return unsuitables;
     152                 :        1280 : }
     153                 :             : 
     154                 :        1280 : std::list<SectionInfo> ArgsManager::GetUnrecognizedSections() const
     155                 :             : {
     156                 :             :     // Section names to be recognized in the config file.
     157                 :        1280 :     static const std::set<std::string> available_sections{
     158                 :             :         ChainTypeToString(ChainType::REGTEST),
     159                 :             :         ChainTypeToString(ChainType::SIGNET),
     160                 :             :         ChainTypeToString(ChainType::TESTNET),
     161                 :             :         ChainTypeToString(ChainType::TESTNET4),
     162                 :             :         ChainTypeToString(ChainType::MAIN),
     163   [ +  +  +  -  :        1802 :     };
          -  +  +  +  -  
                      - ]
     164                 :             : 
     165                 :        1280 :     LOCK(cs_args);
     166         [ +  - ]:        1280 :     std::list<SectionInfo> unrecognized = m_config_sections;
     167                 :        1280 :     unrecognized.remove_if([](const SectionInfo& appeared){ return available_sections.contains(appeared.m_name); });
     168         [ +  - ]:        1280 :     return unrecognized;
     169   [ +  -  +  -  :        1367 : }
          +  -  +  -  +  
                -  -  - ]
     170                 :             : 
     171                 :        2107 : void ArgsManager::SelectConfigNetwork(const std::string& network)
     172                 :             : {
     173                 :        2107 :     LOCK(cs_args);
     174   [ +  -  +  - ]:        4214 :     m_network = network;
     175                 :        2107 : }
     176                 :             : 
     177                 :       54590 : bool ArgsManager::ParseParameters(int argc, const char* const argv[], std::string& error)
     178                 :             : {
     179                 :       54590 :     LOCK(cs_args);
     180                 :       54590 :     m_settings.command_line_options.clear();
     181                 :             : 
     182         [ +  + ]:      107634 :     for (int i = 1; i < argc; i++) {
     183         [ +  - ]:       96419 :         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         [ +  + ]:       96419 :         if (key == "-") break; //bitcoin-tx using stdin
     194                 :       95592 :         std::optional<std::string> val;
     195                 :       95592 :         size_t is_index = key.find('=');
     196         [ +  + ]:       95592 :         if (is_index != std::string::npos) {
     197         [ +  - ]:       25684 :             val = key.substr(is_index + 1);
     198         [ +  - ]:       25684 :             key.erase(is_index);
     199                 :             :         }
     200                 :             : #ifdef WIN32
     201                 :             :         key = ToLower(key);
     202                 :             :         if (key[0] == '/')
     203                 :             :             key[0] = '-';
     204                 :             : #endif
     205                 :             : 
     206         [ +  + ]:       95592 :         if (key[0] != '-') {
     207   [ -  +  -  - ]:       34893 :             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]);
     212                 :           0 :                     return false;
     213                 :             :                 }
     214                 :             :             }
     215         [ +  - ]:       34893 :             m_command.push_back(key);
     216         [ +  + ]:      152086 :             while (++i < argc) {
     217                 :             :                 // The remaining args are command args
     218         [ +  - ]:      117193 :                 m_command.emplace_back(argv[i]);
     219                 :             :             }
     220                 :       34893 :             break;
     221                 :             :         }
     222                 :             : 
     223                 :             :         // Transform --foo to -foo
     224   [ -  +  +  +  :       60699 :         if (key.length() > 1 && key[1] == '-')
                   +  + ]
     225         [ +  - ]:        6066 :             key.erase(0, 1);
     226                 :             : 
     227                 :             :         // Transform -foo to foo
     228         [ +  - ]:       60699 :         key.erase(0, 1);
     229   [ -  +  +  - ]:      121398 :         KeyInfo keyinfo = InterpretKey(key);
     230   [ +  -  +  - ]:       60699 :         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   [ +  +  +  + ]:       60699 :         if (!flags || !keyinfo.section.empty()) {
     236         [ +  - ]:        6946 :             error = strprintf("Invalid parameter %s", argv[i]);
     237                 :        6946 :             return false;
     238                 :             :         }
     239                 :             : 
     240   [ +  +  +  - ]:       85356 :         std::optional<common::SettingsValue> value = InterpretValue(keyinfo, val ? &*val : nullptr, *flags, error);
     241         [ +  + ]:       53753 :         if (!value) return false;
     242                 :             : 
     243   [ +  -  +  - ]:       53044 :         m_settings.command_line_options[keyinfo.name].push_back(*value);
     244                 :      192720 :     }
     245                 :             : 
     246                 :             :     // we do not allow -includeconf from command line, only -noincludeconf
     247   [ +  -  +  + ]:       46935 :     if (auto* includes = common::FindKey(m_settings.command_line_options, "includeconf")) {
     248                 :        2948 :         const common::SettingsSpan values{*includes};
     249                 :             :         // Range may be empty if -noincludeconf was passed
     250   [ +  -  +  + ]:        2948 :         if (!values.empty()) {
     251   [ +  -  +  -  :        2730 :             error = "-includeconf cannot be used from commandline; -includeconf=" + values.begin()->write();
                   +  - ]
     252                 :        2730 :             return false; // pick first value as example
     253                 :             :         }
     254                 :             :     }
     255                 :             :     return true;
     256                 :       54590 : }
     257                 :             : 
     258                 :      127277 : std::optional<unsigned int> ArgsManager::GetArgFlags_(const std::string& name) const
     259                 :             : {
     260                 :      127277 :     AssertLockHeld(cs_args);
     261         [ +  + ]:      300231 :     for (const auto& arg_map : m_available_args) {
     262                 :      241409 :         const auto search = arg_map.second.find(name);
     263         [ +  + ]:      241409 :         if (search != arg_map.second.end()) {
     264                 :       68455 :             return search->second.m_flags;
     265                 :             :         }
     266                 :             :     }
     267                 :       58822 :     return m_default_flags;
     268                 :             : }
     269                 :             : 
     270                 :       66578 : std::optional<unsigned int> ArgsManager::GetArgFlags(const std::string& name) const
     271                 :             : {
     272                 :       66578 :     LOCK(cs_args);
     273         [ +  - ]:       66578 :     return GetArgFlags_(name);
     274                 :       66578 : }
     275                 :             : 
     276                 :           0 : void ArgsManager::SetDefaultFlags(std::optional<unsigned int> flags)
     277                 :             : {
     278                 :           0 :     LOCK(cs_args);
     279         [ #  # ]:           0 :     m_default_flags = flags;
     280                 :           0 : }
     281                 :             : 
     282                 :        4348 : fs::path ArgsManager::GetPathArg_(std::string arg, const fs::path& default_value) const
     283                 :             : {
     284                 :        4348 :     AssertLockHeld(cs_args);
     285                 :        4348 :     const auto value = GetSetting_(arg);
     286         [ +  + ]:        4348 :     if (value.isFalse()) return {};
     287   [ +  -  +  - ]:        4260 :     std::string path_str = SettingToString(value, "");
     288   [ +  +  +  - ]:        7714 :     if (path_str.empty()) return default_value;
     289   [ +  -  +  - ]:       10362 :     fs::path result = fs::PathFromString(path_str).lexically_normal();
     290                 :             :     // Remove trailing slash, if present.
     291   [ +  -  +  -  :       10362 :     return result.has_filename() ? result : result.parent_path();
                   -  - ]
     292                 :        7802 : }
     293                 :             : 
     294                 :         894 : fs::path ArgsManager::GetPathArg(std::string arg, const fs::path& default_value) const
     295                 :             : {
     296                 :         894 :     LOCK(cs_args);
     297   [ +  -  +  - ]:        1788 :     return GetPathArg_(std::move(arg), default_value);
     298                 :         894 : }
     299                 :             : 
     300                 :        3290 : fs::path ArgsManager::GetBlocksDirPath() const
     301                 :             : {
     302                 :        3290 :     LOCK(cs_args);
     303                 :        3290 :     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   [ +  +  +  - ]:        3290 :     if (!path.empty()) return path;
     308                 :             : 
     309   [ +  -  +  -  :        1727 :     if (!GetSetting_("-blocksdir").isNull()) {
                   -  + ]
     310   [ #  #  #  #  :           0 :         path = fs::absolute(GetPathArg_("-blocksdir"));
                   #  # ]
     311   [ #  #  #  # ]:           0 :         if (!fs::is_directory(path)) {
     312         [ #  # ]:           0 :             path = "";
     313         [ #  # ]:           0 :             return path;
     314                 :             :         }
     315                 :             :     } else {
     316         [ +  - ]:        5181 :         path = GetDataDir(/*net_specific=*/false);
     317                 :             :     }
     318                 :             : 
     319   [ +  -  +  - ]:        3454 :     path /= fs::PathFromString(BaseParams().DataDir());
     320         [ +  - ]:        1727 :     path /= "blocks";
     321         [ +  - ]:        1727 :     fs::create_directories(path);
     322   [ +  -  +  - ]:        3290 :     return path;
     323                 :        3290 : }
     324                 :             : 
     325                 :          23 : fs::path ArgsManager::GetDataDirBase() const {
     326                 :          23 :     LOCK(cs_args);
     327         [ +  - ]:          23 :     return GetDataDir(/*net_specific=*/false);
     328                 :          23 : }
     329                 :             : 
     330                 :       19808 : fs::path ArgsManager::GetDataDirNet() const {
     331                 :       19808 :     LOCK(cs_args);
     332         [ +  - ]:       19808 :     return GetDataDir(/*net_specific=*/true);
     333                 :       19808 : }
     334                 :             : 
     335                 :       21558 : fs::path ArgsManager::GetDataDir(bool net_specific) const
     336                 :             : {
     337                 :       21558 :     AssertLockHeld(cs_args);
     338         [ +  + ]:       21558 :     fs::path& path = net_specific ? m_cached_network_datadir_path : m_cached_datadir_path;
     339                 :             : 
     340                 :             :     // Used cached path if available
     341         [ +  + ]:       21558 :     if (!path.empty()) return path;
     342                 :             : 
     343   [ +  -  +  - ]:        6908 :     const fs::path datadir{GetPathArg_("-datadir")};
     344         [ +  - ]:        3454 :     if (!datadir.empty()) {
     345         [ +  - ]:        6908 :         path = fs::absolute(datadir);
     346   [ +  -  -  + ]:        3454 :         if (!fs::is_directory(path)) {
     347         [ #  # ]:           0 :             path = "";
     348         [ #  # ]:           0 :             return path;
     349                 :             :         }
     350                 :             :     } else {
     351         [ #  # ]:           0 :         path = GetDefaultDataDir();
     352                 :             :     }
     353                 :             : 
     354   [ +  +  +  -  :        3454 :     if (net_specific && !BaseParams().DataDir().empty()) {
                   +  + ]
     355   [ +  -  +  - ]:        5160 :         path /= fs::PathFromString(BaseParams().DataDir());
     356                 :             :     }
     357                 :             : 
     358         [ +  - ]:        3454 :     return path;
     359                 :        3454 : }
     360                 :             : 
     361                 :         894 : void ArgsManager::ClearPathCache()
     362                 :             : {
     363                 :         894 :     LOCK(cs_args);
     364                 :             : 
     365                 :         894 :     m_cached_datadir_path = fs::path();
     366                 :         894 :     m_cached_network_datadir_path = fs::path();
     367         [ +  - ]:        1788 :     m_cached_blocks_path = fs::path();
     368                 :         894 : }
     369                 :             : 
     370                 :           0 : std::optional<const ArgsManager::Command> ArgsManager::GetCommand() const
     371                 :             : {
     372         [ #  # ]:           0 :     Command ret;
     373         [ #  # ]:           0 :     LOCK(cs_args);
     374         [ #  # ]:           0 :     auto it = m_command.begin();
     375         [ #  # ]:           0 :     if (it == m_command.end()) {
     376                 :             :         // No command was passed
     377                 :           0 :         return std::nullopt;
     378                 :             :     }
     379         [ #  # ]:           0 :     if (!m_accept_any_command) {
     380                 :             :         // The registered command
     381         [ #  # ]:           0 :         ret.command = *(it++);
     382                 :             :     }
     383         [ #  # ]:           0 :     while (it != m_command.end()) {
     384                 :             :         // The unregistered command and args (if any)
     385         [ #  # ]:           0 :         ret.args.push_back(*(it++));
     386                 :             :     }
     387                 :           0 :     return ret;
     388                 :           0 : }
     389                 :             : 
     390                 :      137579 : std::vector<std::string> ArgsManager::GetArgs(const std::string& strArg) const
     391                 :             : {
     392                 :      137579 :     std::vector<std::string> result;
     393   [ +  -  +  + ]:      142217 :     for (const common::SettingsValue& value : GetSettingsList(strArg)) {
     394   [ -  +  -  -  :       13833 :         result.push_back(value.isFalse() ? "0" : value.isTrue() ? "1" : value.get_str());
          +  +  +  -  +  
             -  -  +  +  
                      - ]
     395                 :             :     }
     396                 :      137579 :     return result;
     397                 :           0 : }
     398                 :             : 
     399                 :        8279 : bool ArgsManager::IsArgSet(const std::string& strArg) const
     400                 :             : {
     401                 :        8279 :     return !GetSetting(strArg).isNull();
     402                 :             : }
     403                 :             : 
     404                 :           0 : bool ArgsManager::GetSettingsPath(fs::path* filepath, bool temp, bool backup) const
     405                 :             : {
     406   [ #  #  #  # ]:           0 :     fs::path settings = GetPathArg("-settings", BITCOIN_SETTINGS_FILENAME);
     407         [ #  # ]:           0 :     if (settings.empty()) {
     408                 :             :         return false;
     409                 :             :     }
     410         [ #  # ]:           0 :     if (backup) {
     411         [ #  # ]:           0 :         settings += ".bak";
     412                 :             :     }
     413         [ #  # ]:           0 :     if (filepath) {
     414   [ #  #  #  #  :           0 :         *filepath = fsbridge::AbsPathJoin(GetDataDirNet(), temp ? settings + ".tmp" : settings);
          #  #  #  #  #  
                #  #  # ]
     415                 :             :     }
     416                 :             :     return true;
     417                 :           0 : }
     418                 :             : 
     419                 :           0 : static void SaveErrors(const std::vector<std::string> errors, std::vector<std::string>* error_out)
     420                 :             : {
     421         [ #  # ]:           0 :     for (const auto& error : errors) {
     422         [ #  # ]:           0 :         if (error_out) {
     423                 :           0 :             error_out->emplace_back(error);
     424                 :             :         } else {
     425                 :           0 :             LogWarning("%s", error);
     426                 :             :         }
     427                 :             :     }
     428                 :           0 : }
     429                 :             : 
     430                 :           0 : bool ArgsManager::ReadSettingsFile(std::vector<std::string>* errors)
     431                 :             : {
     432                 :           0 :     fs::path path;
     433   [ #  #  #  # ]:           0 :     if (!GetSettingsPath(&path, /* temp= */ false)) {
     434                 :             :         return true; // Do nothing if settings file disabled.
     435                 :             :     }
     436                 :             : 
     437         [ #  # ]:           0 :     LOCK(cs_args);
     438                 :           0 :     m_settings.rw_settings.clear();
     439                 :           0 :     std::vector<std::string> read_errors;
     440   [ #  #  #  # ]:           0 :     if (!common::ReadSettings(path, m_settings.rw_settings, read_errors)) {
     441   [ #  #  #  # ]:           0 :         SaveErrors(read_errors, errors);
     442                 :           0 :         return false;
     443                 :             :     }
     444         [ #  # ]:           0 :     for (const auto& setting : m_settings.rw_settings) {
     445   [ #  #  #  # ]:           0 :         KeyInfo key = InterpretKey(setting.first); // Split setting key into section and argname
     446   [ #  #  #  #  :           0 :         if (!GetArgFlags_('-' + key.name)) {
                   #  # ]
     447         [ #  # ]:           0 :             LogWarning("Ignoring unknown rw_settings value %s", setting.first);
     448                 :             :         }
     449                 :           0 :     }
     450                 :             :     return true;
     451         [ #  # ]:           0 : }
     452                 :             : 
     453                 :           0 : bool ArgsManager::WriteSettingsFile(std::vector<std::string>* errors, bool backup) const
     454                 :             : {
     455                 :           0 :     fs::path path, path_tmp;
     456   [ #  #  #  #  :           0 :     if (!GetSettingsPath(&path, /*temp=*/false, backup) || !GetSettingsPath(&path_tmp, /*temp=*/true, backup)) {
             #  #  #  # ]
     457         [ #  # ]:           0 :         throw std::logic_error("Attempt to write settings file when dynamic settings are disabled.");
     458                 :             :     }
     459                 :             : 
     460         [ #  # ]:           0 :     LOCK(cs_args);
     461                 :           0 :     std::vector<std::string> write_errors;
     462   [ #  #  #  # ]:           0 :     if (!common::WriteSettings(path_tmp, m_settings.rw_settings, write_errors)) {
     463   [ #  #  #  # ]:           0 :         SaveErrors(write_errors, errors);
     464                 :           0 :         return false;
     465                 :             :     }
     466   [ #  #  #  #  :           0 :     if (!RenameOver(path_tmp, path)) {
             #  #  #  # ]
     467   [ #  #  #  #  :           0 :         SaveErrors({strprintf("Failed renaming settings file %s to %s\n", fs::PathToString(path_tmp), fs::PathToString(path))}, errors);
          #  #  #  #  #  
                      # ]
     468                 :           0 :         return false;
     469                 :             :     }
     470                 :             :     return true;
     471   [ #  #  #  #  :           0 : }
                   #  # ]
     472                 :             : 
     473                 :           0 : common::SettingsValue ArgsManager::GetPersistentSetting(const std::string& name) const
     474                 :             : {
     475                 :           0 :     LOCK(cs_args);
     476   [ #  #  #  #  :           0 :     return common::GetSetting(m_settings, m_network, name, !UseDefaultSection("-" + name),
                   #  # ]
     477         [ #  # ]:           0 :         /*ignore_nonpersistent=*/true, /*get_chain_type=*/false);
     478                 :           0 : }
     479                 :             : 
     480                 :        1280 : bool ArgsManager::IsArgNegated(const std::string& strArg) const
     481                 :             : {
     482                 :        1280 :     return GetSetting(strArg).isFalse();
     483                 :             : }
     484                 :             : 
     485                 :       17645 : std::string ArgsManager::GetArg(const std::string& strArg, const std::string& strDefault) const
     486                 :             : {
     487         [ +  - ]:       35290 :     return GetArg(strArg).value_or(strDefault);
     488                 :             : }
     489                 :             : 
     490                 :       57144 : std::optional<std::string> ArgsManager::GetArg(const std::string& strArg) const
     491                 :             : {
     492                 :       57144 :     const common::SettingsValue value = GetSetting(strArg);
     493         [ +  - ]:      114288 :     return SettingToString(value);
     494                 :       57144 : }
     495                 :             : 
     496                 :       61404 : std::optional<std::string> SettingToString(const common::SettingsValue& value)
     497                 :             : {
     498         [ +  + ]:       61404 :     if (value.isNull()) return std::nullopt;
     499         [ +  + ]:        3667 :     if (value.isFalse()) return "0";
     500         [ +  + ]:        3652 :     if (value.isTrue()) return "1";
     501   [ -  +  -  - ]:        3638 :     if (value.isNum()) return value.getValStr();
     502         [ -  + ]:        7276 :     return value.get_str();
     503                 :             : }
     504                 :             : 
     505                 :        4260 : std::string SettingToString(const common::SettingsValue& value, const std::string& strDefault)
     506                 :             : {
     507         [ +  - ]:        8520 :     return SettingToString(value).value_or(strDefault);
     508                 :             : }
     509                 :             : 
     510                 :             : template <std::integral Int>
     511                 :      258297 : Int ArgsManager::GetArg(const std::string& strArg, Int nDefault) const
     512                 :             : {
     513         [ +  + ]:      258297 :     return GetArg<Int>(strArg).value_or(nDefault);
     514                 :             : }
     515                 :             : 
     516                 :             : template <std::integral Int>
     517                 :      300055 : std::optional<Int> ArgsManager::GetArg(const std::string& strArg) const
     518                 :             : {
     519                 :      300055 :     const common::SettingsValue value = GetSetting(strArg);
     520         [ +  - ]:      600110 :     return SettingTo<Int>(value);
     521                 :      300055 : }
     522                 :             : 
     523                 :             : template <std::integral Int>
     524         [ +  + ]:      300055 : std::optional<Int> SettingTo(const common::SettingsValue& value)
     525                 :             : {
     526         [ +  + ]:      300055 :     if (value.isNull()) return std::nullopt;
     527         [ +  + ]:       15327 :     if (value.isFalse()) return 0;
     528         [ +  + ]:       15313 :     if (value.isTrue()) return 1;
     529         [ -  + ]:       15300 :     if (value.isNum()) return value.getInt<Int>();
     530         [ -  + ]:       15300 :     return LocaleIndependentAtoi<Int>(value.get_str());
     531                 :             : }
     532                 :             : 
     533                 :             : template <std::integral Int>
     534                 :           0 : Int SettingTo(const common::SettingsValue& value, Int nDefault)
     535                 :             : {
     536         [ #  # ]:           0 :     return SettingTo<Int>(value).value_or(nDefault);
     537                 :             : }
     538                 :             : 
     539                 :       74810 : bool ArgsManager::GetBoolArg(const std::string& strArg, bool fDefault) const
     540                 :             : {
     541         [ +  + ]:       74810 :     return GetBoolArg(strArg).value_or(fDefault);
     542                 :             : }
     543                 :             : 
     544                 :       80280 : std::optional<bool> ArgsManager::GetBoolArg(const std::string& strArg) const
     545                 :             : {
     546                 :       80280 :     const common::SettingsValue value = GetSetting(strArg);
     547         [ +  - ]:      160560 :     return SettingToBool(value);
     548                 :       80280 : }
     549                 :             : 
     550                 :       80280 : std::optional<bool> SettingToBool(const common::SettingsValue& value)
     551                 :             : {
     552         [ +  + ]:       80280 :     if (value.isNull()) return std::nullopt;
     553         [ +  + ]:        3986 :     if (value.isBool()) return value.get_bool();
     554                 :        3957 :     return InterpretBool(value.get_str());
     555                 :             : }
     556                 :             : 
     557                 :           0 : bool SettingToBool(const common::SettingsValue& value, bool fDefault)
     558                 :             : {
     559         [ #  # ]:           0 :     return SettingToBool(value).value_or(fDefault);
     560                 :             : }
     561                 :             : 
     562                 :             : #define INSTANTIATE_INT_TYPE(Type)                                                    \
     563                 :             :     template Type ArgsManager::GetArg<Type>(const std::string&, Type) const;          \
     564                 :             :     template std::optional<Type> ArgsManager::GetArg<Type>(const std::string&) const; \
     565                 :             :     template Type SettingTo<Type>(const common::SettingsValue&, Type);                \
     566                 :             :     template std::optional<Type> SettingTo<Type>(const common::SettingsValue&)
     567                 :             : 
     568                 :             : INSTANTIATE_INT_TYPE(int8_t);
     569                 :             : INSTANTIATE_INT_TYPE(uint8_t);
     570                 :             : INSTANTIATE_INT_TYPE(int16_t);
     571                 :             : INSTANTIATE_INT_TYPE(uint16_t);
     572                 :             : INSTANTIATE_INT_TYPE(int32_t);
     573                 :             : INSTANTIATE_INT_TYPE(uint32_t);
     574                 :             : INSTANTIATE_INT_TYPE(int64_t);
     575                 :             : INSTANTIATE_INT_TYPE(uint64_t);
     576                 :             : 
     577                 :             : #undef INSTANTIATE_INT_TYPE
     578                 :             : 
     579                 :       31345 : bool ArgsManager::SoftSetArg(const std::string& strArg, const std::string& strValue)
     580                 :             : {
     581                 :       31345 :     LOCK(cs_args);
     582   [ +  -  +  + ]:       31345 :     if (!GetSetting_(strArg).isNull()) return false;
     583   [ +  -  +  -  :       18000 :     m_settings.forced_settings[SettingName(strArg)] = strValue;
                   +  - ]
     584                 :       18000 :     return true;
     585                 :       31345 : }
     586                 :             : 
     587                 :       22912 : bool ArgsManager::SoftSetBoolArg(const std::string& strArg, bool fValue)
     588                 :             : {
     589         [ +  + ]:       22912 :     if (fValue)
     590         [ +  - ]:       17009 :         return SoftSetArg(strArg, std::string("1"));
     591                 :             :     else
     592         [ +  - ]:        5903 :         return SoftSetArg(strArg, std::string("0"));
     593                 :             : }
     594                 :             : 
     595                 :       19936 : void ArgsManager::ForceSetArg(const std::string& strArg, const std::string& strValue)
     596                 :             : {
     597                 :       19936 :     LOCK(cs_args);
     598   [ +  -  +  -  :       19936 :     m_settings.forced_settings[SettingName(strArg)] = strValue;
             +  -  +  - ]
     599                 :       19936 : }
     600                 :             : 
     601                 :           0 : void ArgsManager::AddCommand(const std::string& cmd, const std::string& help)
     602                 :             : {
     603         [ #  # ]:           0 :     Assert(cmd.find('=') == std::string::npos);
     604         [ #  # ]:           0 :     Assert(cmd.at(0) != '-');
     605                 :             : 
     606                 :           0 :     LOCK(cs_args);
     607                 :           0 :     m_accept_any_command = false; // latch to false
     608         [ #  # ]:           0 :     std::map<std::string, Arg>& arg_map = m_available_args[OptionsCategory::COMMANDS];
     609   [ #  #  #  #  :           0 :     auto ret = arg_map.emplace(cmd, Arg{"", help, ArgsManager::COMMAND});
                   #  # ]
     610   [ #  #  #  # ]:           0 :     Assert(ret.second); // Fail on duplicate commands
     611                 :           0 : }
     612                 :             : 
     613                 :      214396 : void ArgsManager::AddArg(const std::string& name, const std::string& help, unsigned int flags, const OptionsCategory& cat)
     614                 :             : {
     615         [ -  + ]:      214396 :     Assert((flags & ArgsManager::COMMAND) == 0); // use AddCommand
     616                 :             : 
     617                 :             :     // Split arg name from its help param
     618                 :      214396 :     size_t eq_index = name.find('=');
     619         [ +  + ]:      214396 :     if (eq_index == std::string::npos) {
     620         [ -  + ]:      121420 :         eq_index = name.size();
     621                 :             :     }
     622                 :      214396 :     std::string arg_name = name.substr(0, eq_index);
     623                 :             : 
     624         [ +  - ]:      214396 :     LOCK(cs_args);
     625         [ +  - ]:      214396 :     std::map<std::string, Arg>& arg_map = m_available_args[cat];
     626   [ -  +  +  -  :      428792 :     auto ret = arg_map.emplace(arg_name, Arg{name.substr(eq_index, name.size() - eq_index), help, flags});
             -  +  +  - ]
     627         [ -  + ]:      214396 :     assert(ret.second); // Make sure an insertion actually happened
     628                 :             : 
     629         [ +  + ]:      214396 :     if (flags & ArgsManager::NETWORK_ONLY) {
     630         [ +  - ]:       13248 :         m_network_only_args.emplace(arg_name);
     631                 :             :     }
     632                 :      214396 : }
     633                 :             : 
     634                 :        8714 : void ArgsManager::AddHiddenArgs(const std::vector<std::string>& names)
     635                 :             : {
     636         [ +  + ]:       59773 :     for (const std::string& name : names) {
     637         [ +  - ]:      102118 :         AddArg(name, "", ArgsManager::ALLOW_ANY, OptionsCategory::HIDDEN);
     638                 :             :     }
     639                 :        8714 : }
     640                 :             : 
     641                 :        7498 : void ArgsManager::ClearArgs()
     642                 :             : {
     643                 :        7498 :     LOCK(cs_args);
     644                 :        7498 :     m_settings = {};
     645                 :        7498 :     m_available_args.clear();
     646                 :        7498 :     m_network_only_args.clear();
     647         [ +  - ]:        7498 :     m_config_sections.clear();
     648                 :        7498 : }
     649                 :             : 
     650                 :           0 : void ArgsManager::CheckMultipleCLIArgs() const
     651                 :             : {
     652                 :           0 :     LOCK(cs_args);
     653                 :           0 :     std::vector<std::string> found{};
     654                 :           0 :     auto cmds = m_available_args.find(OptionsCategory::CLI_COMMANDS);
     655         [ #  # ]:           0 :     if (cmds != m_available_args.end()) {
     656   [ #  #  #  # ]:           0 :         for (const auto& [cmd, argspec] : cmds->second) {
     657   [ #  #  #  # ]:           0 :             if (!GetSetting_(cmd).isNull()) {
     658         [ #  # ]:           0 :                 found.push_back(cmd);
     659                 :             :             }
     660                 :             :         }
     661   [ #  #  #  # ]:           0 :         if (found.size() > 1) {
     662   [ #  #  #  #  :           0 :             throw std::runtime_error(strprintf("Only one of %s may be specified.", util::Join(found, ", ")));
                   #  # ]
     663                 :             :         }
     664                 :             :     }
     665         [ #  # ]:           0 : }
     666                 :             : 
     667                 :         386 : std::string ArgsManager::GetHelpMessage() const
     668                 :             : {
     669         [ +  - ]:         386 :     const bool show_debug = GetBoolArg("-help-debug", false);
     670                 :             : 
     671         [ +  - ]:         386 :     std::string usage;
     672         [ +  - ]:         386 :     LOCK(cs_args);
     673         [ +  + ]:        1007 :     for (const auto& arg_map : m_available_args) {
     674   [ +  +  +  +  :         920 :         switch(arg_map.first) {
          +  +  +  +  +  
          +  +  +  +  +  
                   +  + ]
     675                 :         252 :             case OptionsCategory::OPTIONS:
     676   [ +  -  +  - ]:         504 :                 usage += HelpMessageGroup("Options:");
     677                 :         252 :                 break;
     678                 :          28 :             case OptionsCategory::CONNECTION:
     679   [ +  -  +  - ]:          56 :                 usage += HelpMessageGroup("Connection options:");
     680                 :          28 :                 break;
     681                 :          22 :             case OptionsCategory::ZMQ:
     682   [ +  -  +  - ]:          44 :                 usage += HelpMessageGroup("ZeroMQ notification options:");
     683                 :          22 :                 break;
     684                 :          25 :             case OptionsCategory::DEBUG_TEST:
     685   [ +  -  +  - ]:          50 :                 usage += HelpMessageGroup("Debugging/Testing options:");
     686                 :          25 :                 break;
     687                 :          11 :             case OptionsCategory::NODE_RELAY:
     688   [ +  -  +  - ]:          22 :                 usage += HelpMessageGroup("Node relay options:");
     689                 :          11 :                 break;
     690                 :          11 :             case OptionsCategory::BLOCK_CREATION:
     691   [ +  -  +  - ]:          22 :                 usage += HelpMessageGroup("Block creation options:");
     692                 :          11 :                 break;
     693                 :          23 :             case OptionsCategory::RPC:
     694   [ +  -  +  - ]:          46 :                 usage += HelpMessageGroup("RPC server options:");
     695                 :          23 :                 break;
     696                 :          11 :             case OptionsCategory::IPC:
     697   [ +  -  +  - ]:          22 :                 usage += HelpMessageGroup("IPC interprocess connection options:");
     698                 :          11 :                 break;
     699                 :          29 :             case OptionsCategory::WALLET:
     700   [ +  -  +  - ]:          58 :                 usage += HelpMessageGroup("Wallet options:");
     701                 :          29 :                 break;
     702                 :          37 :             case OptionsCategory::WALLET_DEBUG_TEST:
     703   [ +  +  +  -  :          49 :                 if (show_debug) usage += HelpMessageGroup("Wallet debugging/testing options:");
                   +  - ]
     704                 :             :                 break;
     705                 :          19 :             case OptionsCategory::CHAINPARAMS:
     706   [ +  -  +  - ]:          38 :                 usage += HelpMessageGroup("Chain selection options:");
     707                 :          19 :                 break;
     708                 :          18 :             case OptionsCategory::GUI:
     709   [ +  -  +  - ]:          36 :                 usage += HelpMessageGroup("UI Options:");
     710                 :          18 :                 break;
     711                 :          35 :             case OptionsCategory::COMMANDS:
     712   [ +  -  +  - ]:          70 :                 usage += HelpMessageGroup("Commands:");
     713                 :          35 :                 break;
     714                 :          40 :             case OptionsCategory::REGISTER_COMMANDS:
     715   [ +  -  +  - ]:          80 :                 usage += HelpMessageGroup("Register Commands:");
     716                 :          40 :                 break;
     717                 :          60 :             case OptionsCategory::CLI_COMMANDS:
     718   [ +  -  +  - ]:         120 :                 usage += HelpMessageGroup("CLI Commands:");
     719                 :          60 :                 break;
     720                 :             :             default:
     721                 :             :                 break;
     722                 :             :         }
     723                 :             : 
     724                 :             :         // When we get to the hidden options, stop
     725         [ +  + ]:         920 :         if (arg_map.first == OptionsCategory::HIDDEN) break;
     726                 :             : 
     727         [ +  + ]:        3090 :         for (const auto& arg : arg_map.second) {
     728   [ +  +  +  + ]:        2469 :             if (show_debug || !(arg.second.m_flags & ArgsManager::DEBUG_ONLY)) {
     729         [ +  - ]:        1710 :                 std::string name;
     730         [ +  - ]:        1710 :                 if (arg.second.m_help_param.empty()) {
     731         [ +  - ]:        1710 :                     name = arg.first;
     732                 :             :                 } else {
     733         [ #  # ]:           0 :                     name = arg.first + arg.second.m_help_param;
     734                 :             :                 }
     735         [ +  - ]:        3420 :                 usage += HelpMessageOpt(name, arg.second.m_help_text);
     736                 :        1710 :             }
     737                 :             :         }
     738                 :             :     }
     739         [ +  - ]:         386 :     return usage;
     740                 :         386 : }
     741                 :             : 
     742                 :         386 : bool HelpRequested(const ArgsManager& args)
     743                 :             : {
     744   [ +  -  +  -  :        1699 :     return args.IsArgSet("-?") || args.IsArgSet("-h") || args.IsArgSet("-help") || args.IsArgSet("-help-debug");
          +  +  +  -  +  
          -  +  +  +  -  
          +  -  +  +  +  
          -  +  -  +  +  
          +  +  +  +  -  
             -  -  -  -  
                      - ]
     745                 :             : }
     746                 :             : 
     747                 :        1180 : void SetupHelpOptions(ArgsManager& args)
     748                 :             : {
     749   [ +  -  +  - ]:        2360 :     args.AddArg("-help", "Print this help message and exit (also -h or -?)", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
     750         [ +  - ]:        1180 :     args.AddHiddenArgs({"-h", "-?"});
     751                 :        1180 : }
     752                 :             : 
     753                 :             : static const int screenWidth = 79;
     754                 :             : static const int optIndent = 2;
     755                 :             : static const int msgIndent = 7;
     756                 :             : 
     757                 :        1305 : std::string HelpMessageGroup(const std::string &message) {
     758         [ +  - ]:        3915 :     return std::string(message) + std::string("\n\n");
     759                 :             : }
     760                 :             : 
     761                 :        2419 : std::string HelpMessageOpt(const std::string &option, const std::string &message) {
     762   [ +  -  +  - ]:        4838 :     return std::string(optIndent,' ') + std::string(option) +
     763   [ +  -  +  -  :        9676 :            std::string("\n") + std::string(msgIndent,' ') +
                   +  - ]
     764   [ +  -  +  - ]:        7257 :            FormatParagraph(message, screenWidth - msgIndent, msgIndent) +
     765         [ +  - ]:        7257 :            std::string("\n\n");
     766                 :             : }
     767                 :             : 
     768                 :             : const std::vector<std::string> TEST_OPTIONS_DOC{
     769                 :             :     "addrman (use deterministic addrman)",
     770                 :             :     "reindex_after_failure_noninteractive_yes (When asked for a reindex after failure interactively, simulate as-if answered with 'yes')",
     771                 :             :     "bip94 (enforce BIP94 consensus rules)",
     772                 :             : };
     773                 :             : 
     774                 :        1798 : bool HasTestOption(const ArgsManager& args, const std::string& test_option)
     775                 :             : {
     776         [ +  - ]:        1798 :     const auto options = args.GetArgs("-test");
     777   [ -  +  -  +  :       21576 :     return std::any_of(options.begin(), options.end(), [test_option](const auto& option) {
          -  +  +  -  -  
          +  +  -  -  +  
          +  -  -  +  +  
                      - ]
     778         [ #  # ]:           0 :         return option == test_option;
     779                 :        1798 :     });
     780                 :        1798 : }
     781                 :             : 
     782                 :           0 : fs::path GetDefaultDataDir()
     783                 :             : {
     784                 :             :     // Windows:
     785                 :             :     //   old: C:\Users\Username\AppData\Roaming\Bitcoin
     786                 :             :     //   new: C:\Users\Username\AppData\Local\Bitcoin
     787                 :             :     // macOS: ~/Library/Application Support/Bitcoin
     788                 :             :     // Unix-like: ~/.bitcoin
     789                 :             : #ifdef WIN32
     790                 :             :     // Windows
     791                 :             :     // Check for existence of datadir in old location and keep it there
     792                 :             :     fs::path legacy_path = GetSpecialFolderPath(CSIDL_APPDATA) / "Bitcoin";
     793                 :             :     if (fs::exists(legacy_path)) return legacy_path;
     794                 :             : 
     795                 :             :     // Otherwise, fresh installs can start in the new, "proper" location
     796                 :             :     return GetSpecialFolderPath(CSIDL_LOCAL_APPDATA) / "Bitcoin";
     797                 :             : #else
     798                 :           0 :     fs::path pathRet;
     799                 :           0 :     char* pszHome = getenv("HOME");
     800   [ #  #  #  # ]:           0 :     if (pszHome == nullptr || strlen(pszHome) == 0)
     801         [ #  # ]:           0 :         pathRet = fs::path("/");
     802                 :             :     else
     803         [ #  # ]:           0 :         pathRet = fs::path(pszHome);
     804                 :             : #ifdef __APPLE__
     805                 :             :     // macOS
     806                 :             :     return pathRet / "Library/Application Support/Bitcoin";
     807                 :             : #else
     808                 :             :     // Unix-like
     809   [ #  #  #  # ]:           0 :     return pathRet / ".bitcoin";
     810                 :             : #endif
     811                 :             : #endif
     812                 :           0 : }
     813                 :             : 
     814                 :           0 : bool CheckDataDirOption(const ArgsManager& args)
     815                 :             : {
     816   [ #  #  #  # ]:           0 :     const fs::path datadir{args.GetPathArg("-datadir")};
     817   [ #  #  #  #  :           0 :     return datadir.empty() || fs::is_directory(fs::absolute(datadir));
             #  #  #  # ]
     818                 :           0 : }
     819                 :             : 
     820                 :           0 : fs::path ArgsManager::GetConfigFilePath() const
     821                 :             : {
     822                 :           0 :     LOCK(cs_args);
     823   [ #  #  #  #  :           0 :     return *Assert(m_config_path);
                   #  # ]
     824                 :           0 : }
     825                 :             : 
     826                 :           0 : void ArgsManager::SetConfigFilePath(fs::path path)
     827                 :             : {
     828                 :           0 :     LOCK(cs_args);
     829         [ #  # ]:           0 :     assert(!m_config_path);
     830         [ #  # ]:           0 :     m_config_path = path;
     831                 :           0 : }
     832                 :             : 
     833                 :         894 : ChainType ArgsManager::GetChainType() const
     834                 :             : {
     835                 :         894 :     std::variant<ChainType, std::string> arg = GetChainArg();
     836         [ +  - ]:         894 :     if (auto* parsed = std::get_if<ChainType>(&arg)) return *parsed;
     837   [ #  #  #  #  :           0 :     throw std::runtime_error(strprintf("Unknown chain %s.", std::get<std::string>(arg)));
                   #  # ]
     838                 :         894 : }
     839                 :             : 
     840                 :         386 : std::string ArgsManager::GetChainTypeString() const
     841                 :             : {
     842                 :         386 :     auto arg = GetChainArg();
     843   [ +  +  +  - ]:         379 :     if (auto* parsed = std::get_if<ChainType>(&arg)) return ChainTypeToString(*parsed);
     844   [ -  +  -  + ]:         393 :     return std::get<std::string>(arg);
     845                 :         379 : }
     846                 :             : 
     847                 :        1280 : std::variant<ChainType, std::string> ArgsManager::GetChainArg() const
     848                 :             : {
     849                 :        6400 :     auto get_net = [&](const std::string& arg) {
     850                 :        5120 :         LOCK(cs_args);
     851   [ +  -  +  - ]:       10240 :         common::SettingsValue value = common::GetSetting(m_settings, /* section= */ "", SettingName(arg),
     852                 :             :             /* ignore_default_section_config= */ false,
     853                 :             :             /*ignore_nonpersistent=*/false,
     854         [ +  - ]:        5120 :             /* get_chain_type= */ true);
     855   [ +  +  +  +  :        5120 :         return value.isNull() ? false : value.isBool() ? value.get_bool() : InterpretBool(value.get_str());
          +  -  +  -  +  
                      - ]
     856         [ +  - ]:       10240 :     };
     857                 :             : 
     858         [ +  - ]:        1280 :     const bool fRegTest = get_net("-regtest");
     859         [ +  - ]:        1280 :     const bool fSigNet  = get_net("-signet");
     860         [ +  - ]:        1280 :     const bool fTestNet = get_net("-testnet");
     861         [ +  - ]:        1280 :     const bool fTestNet4 = get_net("-testnet4");
     862         [ +  - ]:        1280 :     const auto chain_arg = GetArg("-chain");
     863                 :             : 
     864         [ +  + ]:        1280 :     if ((int)chain_arg.has_value() + (int)fRegTest + (int)fSigNet + (int)fTestNet + (int)fTestNet4 > 1) {
     865         [ +  - ]:           7 :         throw std::runtime_error("Invalid combination of -regtest, -signet, -testnet, -testnet4 and -chain. Can use at most one.");
     866                 :             :     }
     867         [ +  + ]:        1273 :     if (chain_arg) {
     868   [ -  +  +  -  :          20 :         if (auto parsed = ChainTypeFromString(*chain_arg)) return *parsed;
                   +  + ]
     869                 :             :         // Not a known string, so return original string
     870         [ -  + ]:          28 :         return *chain_arg;
     871                 :             :     }
     872         [ +  + ]:        1253 :     if (fRegTest) return ChainType::REGTEST;
     873         [ +  + ]:        1248 :     if (fSigNet) return ChainType::SIGNET;
     874         [ +  + ]:        1247 :     if (fTestNet) return ChainType::TESTNET;
     875         [ +  + ]:        1246 :     if (fTestNet4) return ChainType::TESTNET4;
     876                 :        1243 :     return ChainType::MAIN;
     877                 :        1273 : }
     878                 :             : 
     879                 :      622037 : bool ArgsManager::UseDefaultSection(const std::string& arg) const
     880                 :             : {
     881                 :      622037 :     AssertLockHeld(cs_args);
     882   [ +  +  +  + ]:      622037 :     return m_network == ChainTypeToString(ChainType::MAIN) || !m_network_only_args.contains(arg);
     883                 :             : }
     884                 :             : 
     885                 :      484458 : common::SettingsValue ArgsManager::GetSetting_(const std::string& arg) const
     886                 :             : {
     887                 :      484458 :     AssertLockHeld(cs_args);
     888                 :      484458 :     return common::GetSetting(
     889                 :      484458 :         m_settings, m_network, SettingName(arg), !UseDefaultSection(arg),
     890         [ +  - ]:      968916 :         /*ignore_nonpersistent=*/false, /*get_chain_type=*/false);
     891                 :             : }
     892                 :             : 
     893                 :      447038 : common::SettingsValue ArgsManager::GetSetting(const std::string& arg) const
     894                 :             : {
     895                 :      447038 :     LOCK(cs_args);
     896         [ +  - ]:      447038 :     return GetSetting_(arg);
     897                 :      447038 : }
     898                 :             : 
     899                 :      137579 : std::vector<common::SettingsValue> ArgsManager::GetSettingsList(const std::string& arg) const
     900                 :             : {
     901                 :      137579 :     LOCK(cs_args);
     902   [ +  -  +  -  :      275158 :     return common::GetSettingsList(m_settings, m_network, SettingName(arg), !UseDefaultSection(arg));
             +  -  +  - ]
     903                 :      137579 : }
     904                 :             : 
     905                 :           0 : void ArgsManager::logArgsPrefix(
     906                 :             :     const std::string& prefix,
     907                 :             :     const std::string& section,
     908                 :             :     const std::map<std::string, std::vector<common::SettingsValue>>& args) const
     909                 :             : {
     910                 :           0 :     AssertLockHeld(cs_args);
     911   [ #  #  #  # ]:           0 :     std::string section_str = section.empty() ? "" : "[" + section + "] ";
     912         [ #  # ]:           0 :     for (const auto& arg : args) {
     913         [ #  # ]:           0 :         for (const auto& value : arg.second) {
     914   [ #  #  #  # ]:           0 :             std::optional<unsigned int> flags = GetArgFlags_('-' + arg.first);
     915         [ #  # ]:           0 :             if (flags) {
     916   [ #  #  #  #  :           0 :                 std::string value_str = (*flags & SENSITIVE) ? "****" : value.write();
                   #  # ]
     917         [ #  # ]:           0 :                 LogInfo("%s %s%s=%s\n", prefix, section_str, arg.first, value_str);
     918                 :           0 :             }
     919                 :             :         }
     920                 :             :     }
     921                 :           0 : }
     922                 :             : 
     923                 :           0 : void ArgsManager::LogArgs() const
     924                 :             : {
     925                 :           0 :     LOCK(cs_args);
     926         [ #  # ]:           0 :     for (const auto& section : m_settings.ro_config) {
     927   [ #  #  #  # ]:           0 :         logArgsPrefix("Config file arg:", section.first, section.second);
     928                 :             :     }
     929         [ #  # ]:           0 :     for (const auto& setting : m_settings.rw_settings) {
     930   [ #  #  #  # ]:           0 :         LogInfo("Setting file arg: %s = %s\n", setting.first, setting.second.write());
     931                 :             :     }
     932   [ #  #  #  #  :           0 :     logArgsPrefix("Command-line arg:", "", m_settings.command_line_options);
             #  #  #  # ]
     933                 :           0 : }
        

Generated by: LCOV version 2.0-1