Inside Bitcoin
Opening the selected file…
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2023-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#include <common/args.h> // IWYU pragma: associated7#include <common/settings.h>8#include <sync.h>9#include <tinyformat.h>10#include <univalue.h>11#include <util/check.h>12#include <util/fs.h>13#include <util/log.h>14#include <util/string.h>16#include <algorithm>17#include <cstddef>18#include <fstream>19#include <iostream>20#include <list>21#include <map>22#include <optional>23#include <sstream>24#include <string>25#include <string_view>26#include <utility>27#include <vector>29using util::TrimString;30using util::TrimStringView;32static bool GetConfigOptions(std::istream& stream, const std::string& filepath, std::string& error, std::vector<std::pair<std::string, std::string>>& options, std::list<SectionInfo>& sections)33{34std::string str, prefix;35std::string::size_type pos;36int linenr = 1;37while (std::getline(stream, str)) {38bool used_hash = false;39if ((pos = str.find('#')) != std::string::npos) {40str = str.substr(0, pos);41used_hash = true;42}43const static std::string pattern = " \t\r\n";44str = TrimString(str, pattern);45if (!str.empty()) {46if (*str.begin() == '[' && *str.rbegin() == ']') {47const std::string section = str.substr(1, str.size() - 2);48sections.emplace_back(SectionInfo{section, filepath, linenr});49prefix = section + '.';50} else if (*str.begin() == '-') {51error = strprintf("parse error on line %i: %s, options in configuration file must be specified without leading -", linenr, str);52return false;53} else if ((pos = str.find('=')) != std::string::npos) {54std::string name = prefix + TrimString(std::string_view{str}.substr(0, pos), pattern);55std::string_view value = TrimStringView(std::string_view{str}.substr(pos + 1), pattern);56if (used_hash && name.find("rpcpassword") != std::string::npos) {57error = strprintf("parse error on line %i, using # in rpcpassword can be ambiguous and should be avoided", linenr);58return false;59}60options.emplace_back(name, value);61if ((pos = name.rfind('.')) != std::string::npos && prefix.length() <= pos) {62sections.emplace_back(SectionInfo{name.substr(0, pos), filepath, linenr});63}64} else {65error = strprintf("parse error on line %i: %s", linenr, str);66if (str.size() >= 2 && str.starts_with("no")) {67error += strprintf(", if you intended to specify a negated option, use %s=1 instead", str);68}69return false;70}71}72++linenr;73}74return true;75}77bool IsConfSupported(KeyInfo& key, std::string& error) {78if (key.name == "conf") {79error = "conf cannot be set in the configuration file; use includeconf= if you want to include additional config files";80return false;81}82if (key.name == "reindex") {83// reindex can be set in a config file but it is strongly discouraged as this will cause the node to reindex on84// every restart. Allow the config but throw a warning85LogWarning("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");86return true;87}88return true;89}91bool ArgsManager::ReadConfigStream(std::istream& stream, const std::string& filepath, std::string& error, bool ignore_invalid_keys)92{93LOCK(cs_args);94std::vector<std::pair<std::string, std::string>> options;95if (!GetConfigOptions(stream, filepath, error, options, m_config_sections)) {96return false;97}98for (const std::pair<std::string, std::string>& option : options) {99KeyInfo key = InterpretKey(option.first);100std::optional<unsigned int> flags = GetArgFlags_('-' + key.name);101if (!IsConfSupported(key, error)) return false;102if (flags) {103std::optional<common::SettingsValue> value = InterpretValue(key, &option.second, *flags, error);104if (!value) {105return false;106}107m_settings.ro_config[key.section][key.name].push_back(*value);108} else {109if (ignore_invalid_keys) {110LogWarning("Ignoring unknown configuration value %s", option.first);111} else {112error = strprintf("Invalid configuration value %s", option.first);113return false;114}115}116}117return true;118}120bool ArgsManager::ReadConfigString(const std::string& str_config)121{122std::istringstream streamConfig(str_config);123{124LOCK(cs_args);125m_settings.ro_config.clear();126m_config_sections.clear();127}128std::string error;129return ReadConfigStream(streamConfig, "", error);130}132bool ArgsManager::ReadConfigFiles(std::string& error, bool ignore_invalid_keys)133{134{135LOCK(cs_args);136m_settings.ro_config.clear();137m_config_sections.clear();138const auto conf_val = GetPathArg_("-conf", BITCOIN_CONF_FILENAME);139m_config_path = (conf_val.is_absolute() || conf_val.empty()) ? conf_val : fsbridge::AbsPathJoin(GetDataDir(/*net_specific=*/false), conf_val);140}142const auto conf_path{GetConfigFilePath()};143std::ifstream stream;144if (!conf_path.empty()) { // path is empty when -noconf is specified145if (fs::is_directory(conf_path)) {146error = strprintf("Config file \"%s\" is a directory.", fs::PathToString(conf_path));147return false;148}149stream = std::ifstream{conf_path.std_path()};150// If the file is explicitly specified, it must be readable151if (IsArgSet("-conf") && !stream.good()) {152error = strprintf("specified config file \"%s\" could not be opened.", fs::PathToString(conf_path));153return false;154}155}156// ok to not have a config file157if (stream.good()) {158if (!ReadConfigStream(stream, fs::PathToString(conf_path), error, ignore_invalid_keys)) {159return false;160}161// `-includeconf` cannot be included in the command line arguments except162// as `-noincludeconf` (which indicates that no included conf file should be used).163bool use_conf_file{true};164{165LOCK(cs_args);166if (auto* includes = common::FindKey(m_settings.command_line_options, "includeconf")) {167// ParseParameters() fails if a non-negated -includeconf is passed on the command-line168assert(common::SettingsSpan(*includes).last_negated());169use_conf_file = false;170}171}172if (use_conf_file) {173std::string chain_id = GetChainTypeString();174std::vector<std::string> conf_file_names;176auto add_includes = [&](const std::string& network, size_t skip = 0) {177size_t num_values = 0;178LOCK(cs_args);179if (auto* section = common::FindKey(m_settings.ro_config, network)) {180if (auto* values = common::FindKey(*section, "includeconf")) {181for (size_t i = std::max(skip, common::SettingsSpan(*values).negated()); i < values->size(); ++i) {182conf_file_names.push_back((*values)[i].get_str());183}184num_values = values->size();185}186}187return num_values;188};190// We haven't set m_network yet (that happens in SelectParams()), so manually check191// for network.includeconf args.192const size_t chain_includes = add_includes(chain_id);193const size_t default_includes = add_includes({});195for (const std::string& conf_file_name : conf_file_names) {196const auto include_conf_path{AbsPathForConfigVal(*this, fs::PathFromString(conf_file_name), /*net_specific=*/false)};197if (fs::is_directory(include_conf_path)) {198error = strprintf("Included config file \"%s\" is a directory.", fs::PathToString(include_conf_path));199return false;200}201std::ifstream conf_file_stream{include_conf_path.std_path()};202if (conf_file_stream.good()) {203if (!ReadConfigStream(conf_file_stream, conf_file_name, error, ignore_invalid_keys)) {204return false;205}206LogInfo("Included configuration file %s\n", conf_file_name);207} else {208error = "Failed to include configuration file " + conf_file_name;209return false;210}211}213// Warn about recursive -includeconf214conf_file_names.clear();215add_includes(chain_id, /* skip= */ chain_includes);216add_includes({}, /* skip= */ default_includes);217std::string chain_id_final = GetChainTypeString();218if (chain_id_final != chain_id) {219// Also warn about recursive includeconf for the chain that was specified in one of the includeconfs220add_includes(chain_id_final);221}222for (const std::string& conf_file_name : conf_file_names) {223tfm::format(std::cerr, "warning: -includeconf cannot be used from included files; ignoring -includeconf=%s\n", conf_file_name);224}225}226}228// If datadir is changed in .conf file:229ClearPathCache();230if (!CheckDataDirOption(*this)) {231error = strprintf("specified data directory \"%s\" does not exist.", GetArg("-datadir", ""));232return false;233}234return true;235}237fs::path AbsPathForConfigVal(const ArgsManager& args, const fs::path& path, bool net_specific)238{239if (path.is_absolute() || path.empty()) {240return path;241}242return fsbridge::AbsPathJoin(net_specific ? args.GetDataDirNet() : args.GetDataDirBase(), path);243}
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.