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) 2019-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#ifndef BITCOIN_COMMON_SETTINGS_H6#define BITCOIN_COMMON_SETTINGS_H8#include <util/fs.h>10#include <cstddef>11#include <map>12#include <string>13#include <vector>15// Users of this header need to explicitly #include <univalue.h>16// IWYU pragma: no_include <univalue.h>17class UniValue; // IWYU pragma: keep19namespace common {21//! Settings value type (string/integer/boolean/null variant).22//!23//! @note UniValue is used here for convenience and because it can be easily24//! serialized in a readable format. But any other variant type that can25//! be assigned strings, int64_t, and bool values and has get_str(),26//! getInt<int64_t>(), get_bool(), isNum(), isBool(), isFalse(), isTrue() and27//! isNull() methods can be substituted if there's a need to move away28//! from UniValue. (An implementation with boost::variant was posted at29//! https://github.com/bitcoin/bitcoin/pull/15934/files#r337691812)30using SettingsValue = UniValue;32//! Stored settings. This struct combines settings from the command line, a33//! read-only configuration file, and a read-write runtime settings file.34struct Settings {35//! Map of setting name to forced setting value.36std::map<std::string, SettingsValue> forced_settings;37//! Map of setting name to list of command line values.38std::map<std::string, std::vector<SettingsValue>> command_line_options;39//! Map of setting name to read-write file setting value.40std::map<std::string, SettingsValue> rw_settings;41//! Map of config section name and setting name to list of config file values.42std::map<std::string, std::map<std::string, std::vector<SettingsValue>>> ro_config;43};45//! Read settings file.46bool ReadSettings(const fs::path& path,47std::map<std::string, SettingsValue>& values,48std::vector<std::string>& errors);50//! Write settings file.51bool WriteSettings(const fs::path& path,52const std::map<std::string, SettingsValue>& values,53std::vector<std::string>& errors);55//! Get settings value from combined sources: forced settings, command line56//! arguments, runtime read-write settings, and the read-only config file.57//!58//! @param ignore_default_section_config - ignore values in the default section59//! of the config file (part before any60//! [section] keywords)61//! @param ignore_nonpersistent - ignore non-persistent settings values (forced62//! settings values and values specified on the63//! command line). Only return settings in the64//! read-only config and read-write settings65//! files.66//! @param get_chain_type - enable special backwards compatible behavior67//! for GetChainType68SettingsValue GetSetting(const Settings& settings,69const std::string& section,70const std::string& name,71bool ignore_default_section_config,72bool ignore_nonpersistent,73bool get_chain_type);75//! Get combined setting value similar to GetSetting(), except if setting was76//! specified multiple times, return a list of all the values specified.77std::vector<SettingsValue> GetSettingsList(const Settings& settings,78const std::string& section,79const std::string& name,80bool ignore_default_section_config);82//! Return true if a setting is set in the default config file section, and not83//! overridden by a higher priority command-line or network section value.84//!85//! This is used to provide user warnings about values that might be getting86//! ignored unintentionally.87bool OnlyHasDefaultSectionSetting(const Settings& settings, const std::string& section, const std::string& name);89//! Accessor for list of settings that skips negated values when iterated over.90//! The last boolean `false` value in the list and all earlier values are91//! considered negated.92struct SettingsSpan {93explicit SettingsSpan() = default;94explicit SettingsSpan(const SettingsValue& value) noexcept : SettingsSpan(&value, 1) {}95explicit SettingsSpan(const SettingsValue* data, size_t size) noexcept : data(data), size(size) {}96explicit SettingsSpan(const std::vector<SettingsValue>& vec) noexcept;97const SettingsValue* begin() const; //!< Pointer to first non-negated value.98const SettingsValue* end() const; //!< Pointer to end of values.99bool empty() const; //!< True if there are any non-negated values.100bool last_negated() const; //!< True if the last value is negated.101size_t negated() const; //!< Number of negated values.103const SettingsValue* data = nullptr;104size_t size = 0;105};107//! Map lookup helper.108template <typename Map, typename Key>109auto FindKey(Map&& map, Key&& key) -> decltype(&map.at(key))110{111auto it = map.find(key);112return it == map.end() ? nullptr : &it->second;113}115} // namespace common117#endif // BITCOIN_COMMON_SETTINGS_H
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.