From d8f5ab25b0d6c93a181ba4865d35323eab27e161 Mon Sep 17 00:00:00 2001 From: fanquake Date: Fri, 21 Oct 2022 16:31:04 +0800 Subject: [PATCH] Merge bitcoin/bitcoin#25727: util, config: error on startup if `conf` or `reindex` are set in config file deba6fe3158cd0b2283e0901a072e434ba5b594e test: update feature_config_args.py (josibake) 2e3826cbcd675dcd1d03970233ba5e143e09eb75 util: warn if reindex is used in conf (josibake) 5e744f423838fe7d45453541271bc1a07cd62eac util: disallow setting conf in bitcoin.conf (josibake) Pull request description: In help from `bitcoind -h` it specifes that `conf` can only be used from the commandline. However, if `conf` is set in a `bitcoin.conf` file, there is no error and from reading the logs it seems as if the `conf=` is being used, despite it being ignored. To recreate, you can setup a `bitcoin.conf` file in the default directory, add `conf=.conf` and in the separate config file set whichever config value you want and verify that it is being ignored. alternatively, if you set `includeconf=.conf` , your config in `` will be picked up. This PR fixes this by having the node error when reading the config file if `conf=` is set. Additionally, it was mentioned in a recent [PR review club](https://bitcoincore.reviews/24858) that if `reindex=1` is set in the config file, the node will reindex on every startup, which is undesirable: ```irc 17:14 michaelfolkson: Reindex is requested by the user (node operator) as a configuration option (command line or in the config file, tho you probably would never put it in the file, or else it would reindex on every startup!) ``` This PR also has a commit to warn if `reindex=1` is set in the config file. ACKs for top commit: hebasto: ACK deba6fe3158cd0b2283e0901a072e434ba5b594e, tested on Ubuntu 22.04. aureleoules: tACK deba6fe3158cd0b2283e0901a072e434ba5b594e ryanofsky: Code review ACK deba6fe3158cd0b2283e0901a072e434ba5b594e. Tree-SHA512: 619fd0aa14e98af1166d6beb92651f5ba3f10d38b8ee132957f094f19c3a37313d9f4d7be2e4019f3fc9a2ca5fa42d03eb539ad820e27efec7ee58a26eb520b1 --- src/util/system.cpp | 15 +++++++++++++++ test/functional/feature_config_args.py | 22 ++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/util/system.cpp b/src/util/system.cpp index abe25b9bb3a7e..674eaccfbb54d 100644 --- a/src/util/system.cpp +++ b/src/util/system.cpp @@ -928,6 +928,20 @@ static bool GetConfigOptions(std::istream& stream, const std::string& filepath, return true; } +bool IsConfSupported(KeyInfo& key, std::string& error) { + if (key.name == "conf") { + error = "conf cannot be set in the configuration file; use includeconf= if you want to include additional config files"; + return false; + } + if (key.name == "reindex") { + // reindex can be set in a config file but it is strongly discouraged as this will cause the node to reindex on + // every restart. Allow the config but throw a warning + LogPrintf("Warning: reindex=1 is set in the configuration file, which will significantly slow down startup. Consider removing or commenting out this option for better performance, unless there is currently a condition which makes rebuilding the indexes necessary\n"); + return true; + } + return true; +} + bool ArgsManager::ReadConfigStream(std::istream& stream, const std::string& filepath, std::string& error, bool ignore_invalid_keys) { LOCK(cs_args); @@ -938,6 +952,7 @@ bool ArgsManager::ReadConfigStream(std::istream& stream, const std::string& file for (const std::pair& option : options) { KeyInfo key = InterpretKey(option.first); std::optional flags = GetArgFlags('-' + key.name); + if (!IsConfSupported(key, error)) return false; if (flags) { std::optional value = InterpretValue(key, &option.second, *flags, error); if (!value) { diff --git a/test/functional/feature_config_args.py b/test/functional/feature_config_args.py index cba1d17cb537f..a3b51207dec57 100755 --- a/test/functional/feature_config_args.py +++ b/test/functional/feature_config_args.py @@ -19,11 +19,25 @@ def set_test_params(self): self.wallet_names = [] def test_config_file_parser(self): + self.log.info('Test config file parser') self.stop_node(0) + # Check that startup fails if conf= is set in bitcoin.conf or in an included conf file + bad_conf_file_path = os.path.join(self.options.tmpdir, 'node0', 'bitcoin_bad.conf') + util.write_config(bad_conf_file_path, n=0, chain='', extra_config=f'conf=some.conf\n') + conf_in_config_file_err = 'Error: Error reading configuration file: conf cannot be set in the configuration file; use includeconf= if you want to include additional config files' + self.nodes[0].assert_start_raises_init_error( + extra_args=[f'-conf={bad_conf_file_path}'], + expected_msg=conf_in_config_file_err, + ) inc_conf_file_path = os.path.join(self.nodes[0].datadir, 'include.conf') with open(os.path.join(self.nodes[0].datadir, 'dash.conf'), 'a', encoding='utf-8') as conf: conf.write(f'includeconf={inc_conf_file_path}\n') + with open(inc_conf_file_path, 'w', encoding='utf-8') as conf: + conf.write('conf=some.conf\n') + self.nodes[0].assert_start_raises_init_error( + expected_msg=conf_in_config_file_err, + ) self.nodes[0].assert_start_raises_init_error( expected_msg='Error: Error parsing command line arguments: Invalid parameter -dash_cli=1', @@ -31,10 +45,18 @@ def test_config_file_parser(self): ) with open(inc_conf_file_path, 'w', encoding='utf-8') as conf: conf.write('dash_conf=1\n') + with self.nodes[0].assert_debug_log(expected_msgs=['Ignoring unknown configuration value dash_conf']): self.start_node(0) self.stop_node(0) + with open(inc_conf_file_path, 'w', encoding='utf-8') as conf: + conf.write('reindex=1\n') + + with self.nodes[0].assert_debug_log(expected_msgs=['Warning: reindex=1 is set in the configuration file, which will significantly slow down startup. Consider removing or commenting out this option for better performance, unless there is currently a condition which makes rebuilding the indexes necessary']): + self.start_node(0) + self.stop_node(0) + with open(inc_conf_file_path, 'w', encoding='utf-8') as conf: conf.write('-dash=1\n') self.nodes[0].assert_start_raises_init_error(expected_msg='Error: Error reading configuration file: parse error on line 1: -dash=1, options in configuration file must be specified without leading -')