/Users/eugenesiegel/btc/bitcoin/src/common/settings.h
| Line | Count | Source | 
| 1 |  | // Copyright (c) 2019-2022 The Bitcoin Core developers | 
| 2 |  | // Distributed under the MIT software license, see the accompanying | 
| 3 |  | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | 
| 4 |  |  | 
| 5 |  | #ifndef BITCOIN_COMMON_SETTINGS_H | 
| 6 |  | #define BITCOIN_COMMON_SETTINGS_H | 
| 7 |  |  | 
| 8 |  | #include <util/fs.h> | 
| 9 |  |  | 
| 10 |  | #include <cstddef> | 
| 11 |  | #include <map> | 
| 12 |  | #include <string> | 
| 13 |  | #include <vector> | 
| 14 |  |  | 
| 15 |  | class UniValue; | 
| 16 |  |  | 
| 17 |  | namespace common { | 
| 18 |  |  | 
| 19 |  | //! Settings value type (string/integer/boolean/null variant). | 
| 20 |  | //! | 
| 21 |  | //! @note UniValue is used here for convenience and because it can be easily | 
| 22 |  | //!       serialized in a readable format. But any other variant type that can | 
| 23 |  | //!       be assigned strings, int64_t, and bool values and has get_str(), | 
| 24 |  | //!       getInt<int64_t>(), get_bool(), isNum(), isBool(), isFalse(), isTrue() and | 
| 25 |  | //!       isNull() methods can be substituted if there's a need to move away | 
| 26 |  | //!       from UniValue. (An implementation with boost::variant was posted at | 
| 27 |  | //!       https://github.com/bitcoin/bitcoin/pull/15934/files#r337691812) | 
| 28 |  | using SettingsValue = UniValue; | 
| 29 |  |  | 
| 30 |  | //! Stored settings. This struct combines settings from the command line, a | 
| 31 |  | //! read-only configuration file, and a read-write runtime settings file. | 
| 32 |  | struct Settings { | 
| 33 |  |     //! Map of setting name to forced setting value. | 
| 34 |  |     std::map<std::string, SettingsValue> forced_settings; | 
| 35 |  |     //! Map of setting name to list of command line values. | 
| 36 |  |     std::map<std::string, std::vector<SettingsValue>> command_line_options; | 
| 37 |  |     //! Map of setting name to read-write file setting value. | 
| 38 |  |     std::map<std::string, SettingsValue> rw_settings; | 
| 39 |  |     //! Map of config section name and setting name to list of config file values. | 
| 40 |  |     std::map<std::string, std::map<std::string, std::vector<SettingsValue>>> ro_config; | 
| 41 |  | }; | 
| 42 |  |  | 
| 43 |  | //! Read settings file. | 
| 44 |  | bool ReadSettings(const fs::path& path, | 
| 45 |  |     std::map<std::string, SettingsValue>& values, | 
| 46 |  |     std::vector<std::string>& errors); | 
| 47 |  |  | 
| 48 |  | //! Write settings file. | 
| 49 |  | bool WriteSettings(const fs::path& path, | 
| 50 |  |     const std::map<std::string, SettingsValue>& values, | 
| 51 |  |     std::vector<std::string>& errors); | 
| 52 |  |  | 
| 53 |  | //! Get settings value from combined sources: forced settings, command line | 
| 54 |  | //! arguments, runtime read-write settings, and the read-only config file. | 
| 55 |  | //! | 
| 56 |  | //! @param ignore_default_section_config - ignore values in the default section | 
| 57 |  | //!                                        of the config file (part before any | 
| 58 |  | //!                                        [section] keywords) | 
| 59 |  | //! @param ignore_nonpersistent - ignore non-persistent settings values (forced | 
| 60 |  | //!                               settings values and values specified on the | 
| 61 |  | //!                               command line). Only return settings in the | 
| 62 |  | //!                               read-only config and read-write settings | 
| 63 |  | //!                               files. | 
| 64 |  | //! @param get_chain_type - enable special backwards compatible behavior | 
| 65 |  | //!                         for GetChainType | 
| 66 |  | SettingsValue GetSetting(const Settings& settings, | 
| 67 |  |     const std::string& section, | 
| 68 |  |     const std::string& name, | 
| 69 |  |     bool ignore_default_section_config, | 
| 70 |  |     bool ignore_nonpersistent, | 
| 71 |  |     bool get_chain_type); | 
| 72 |  |  | 
| 73 |  | //! Get combined setting value similar to GetSetting(), except if setting was | 
| 74 |  | //! specified multiple times, return a list of all the values specified. | 
| 75 |  | std::vector<SettingsValue> GetSettingsList(const Settings& settings, | 
| 76 |  |     const std::string& section, | 
| 77 |  |     const std::string& name, | 
| 78 |  |     bool ignore_default_section_config); | 
| 79 |  |  | 
| 80 |  | //! Return true if a setting is set in the default config file section, and not | 
| 81 |  | //! overridden by a higher priority command-line or network section value. | 
| 82 |  | //! | 
| 83 |  | //! This is used to provide user warnings about values that might be getting | 
| 84 |  | //! ignored unintentionally. | 
| 85 |  | bool OnlyHasDefaultSectionSetting(const Settings& settings, const std::string& section, const std::string& name); | 
| 86 |  |  | 
| 87 |  | //! Accessor for list of settings that skips negated values when iterated over. | 
| 88 |  | //! The last boolean `false` value in the list and all earlier values are | 
| 89 |  | //! considered negated. | 
| 90 |  | struct SettingsSpan { | 
| 91 |  |     explicit SettingsSpan() = default; | 
| 92 | 410k |     explicit SettingsSpan(const SettingsValue& value) noexcept : SettingsSpan(&value, 1) {} | 
| 93 | 1.07M |     explicit SettingsSpan(const SettingsValue* data, size_t size) noexcept : data(data), size(size) {} | 
| 94 |  |     explicit SettingsSpan(const std::vector<SettingsValue>& vec) noexcept; | 
| 95 |  |     const SettingsValue* begin() const; //!< Pointer to first non-negated value. | 
| 96 |  |     const SettingsValue* end() const;   //!< Pointer to end of values. | 
| 97 |  |     bool empty() const;                 //!< True if there are any non-negated values. | 
| 98 |  |     bool last_negated() const;          //!< True if the last value is negated. | 
| 99 |  |     size_t negated() const;             //!< Number of negated values. | 
| 100 |  |  | 
| 101 |  |     const SettingsValue* data = nullptr; | 
| 102 |  |     size_t size = 0; | 
| 103 |  | }; | 
| 104 |  |  | 
| 105 |  | //! Map lookup helper. | 
| 106 |  | template <typename Map, typename Key> | 
| 107 |  | auto FindKey(Map&& map, Key&& key) -> decltype(&map.at(key)) | 
| 108 | 40.7M | { | 
| 109 | 40.7M |     auto it = map.find(key); | 
| 110 | 40.7M |     return it == map.end() ? nullptr39.7M: &it->second1.07M; | 
| 111 | 40.7M | } _ZN6common7FindKeyIRNSt3__13mapINS1_12basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEENS1_6vectorI8UniValueNS6_ISA_EEEENS1_4lessIS8_EENS6_INS1_4pairIKS8_SC_EEEEEERA12_KcEEDTadcldtfp_2atfp0_EEOT_OT0_| Line | Count | Source |  | 108 | 51.2k | { |  | 109 | 51.2k |     auto it = map.find(key); |  | 110 | 51.2k |     return it == map.end() ? nullptr : &it->second0; |  | 111 | 51.2k | } | 
Unexecuted instantiation: _ZN6common7FindKeyIRNSt3__13mapINS1_12basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEENS2_IS8_NS1_6vectorI8UniValueNS6_ISA_EEEENS1_4lessIS8_EENS6_INS1_4pairIKS8_SC_EEEEEESE_NS6_INSF_ISG_SJ_EEEEEERSG_EEDTadcldtfp_2atfp0_EEOT_OT0__ZN6common7FindKeyIRKNSt3__13mapINS1_12basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEE8UniValueNS1_4lessIS8_EENS6_INS1_4pairIKS8_S9_EEEEEERSD_EEDTadcldtfp_2atfp0_EEOT_OT0_| Line | Count | Source |  | 108 | 16.6M | { |  | 109 | 16.6M |     auto it = map.find(key); |  | 110 | 16.6M |     return it == map.end() ? nullptr16.2M: &it->second410k; |  | 111 | 16.6M | } | 
_ZN6common7FindKeyIRKNSt3__13mapINS1_12basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEENS1_6vectorI8UniValueNS6_ISA_EEEENS1_4lessIS8_EENS6_INS1_4pairIKS8_SC_EEEEEERSG_EEDTadcldtfp_2atfp0_EEOT_OT0_| Line | Count | Source |  | 108 | 8.33M | { |  | 109 | 8.33M |     auto it = map.find(key); |  | 110 | 8.33M |     return it == map.end() ? nullptr7.66M: &it->second666k; |  | 111 | 8.33M | } | 
_ZN6common7FindKeyIRKNSt3__13mapINS1_12basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEENS2_IS8_NS1_6vectorI8UniValueNS6_ISA_EEEENS1_4lessIS8_EENS6_INS1_4pairIKS8_SC_EEEEEESE_NS6_INSF_ISG_SJ_EEEEEERSG_EEDTadcldtfp_2atfp0_EEOT_OT0_| Line | Count | Source |  | 108 | 7.41M | { |  | 109 | 7.41M |     auto it = map.find(key); |  | 110 | 7.41M |     return it == map.end() ? nullptr : &it->second0; |  | 111 | 7.41M | } | 
_ZN6common7FindKeyIRKNSt3__13mapINS1_12basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEENS2_IS8_NS1_6vectorI8UniValueNS6_ISA_EEEENS1_4lessIS8_EENS6_INS1_4pairIKS8_SC_EEEEEESE_NS6_INSF_ISG_SJ_EEEEEERA1_KcEEDTadcldtfp_2atfp0_EEOT_OT0_| Line | Count | Source |  | 108 | 8.33M | { |  | 109 | 8.33M |     auto it = map.find(key); |  | 110 | 8.33M |     return it == map.end() ? nullptr : &it->second0; |  | 111 | 8.33M | } | 
Unexecuted instantiation: _ZN6common7FindKeyIRNSt3__13mapINS1_7variantIJ14CNoDestination17PubKeyDestination6PKHash10ScriptHash19WitnessV0ScriptHash16WitnessV0KeyHash16WitnessV1Taproot11PayToAnchor14WitnessUnknownEEEN6wallet16CAddressBookDataENS1_4lessISD_EENS1_9allocatorINS1_4pairIKSD_SF_EEEEEERSK_EEDTadcldtfp_2atfp0_EEOT_OT0_Unexecuted instantiation: _ZN6common7FindKeyIRKNSt3__13mapINS1_7variantIJ14CNoDestination17PubKeyDestination6PKHash10ScriptHash19WitnessV0ScriptHash16WitnessV0KeyHash16WitnessV1Taproot11PayToAnchor14WitnessUnknownEEEN6wallet16CAddressBookDataENS1_4lessISD_EENS1_9allocatorINS1_4pairIKSD_SF_EEEEEERSK_EEDTadcldtfp_2atfp0_EEOT_OT0_Unexecuted instantiation: _ZN6common7FindKeyIRNSt3__13mapINS1_12basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEENS1_6vectorI8UniValueNS6_ISA_EEEENS1_4lessIS8_EENS6_INS1_4pairIKS8_SC_EEEEEERSG_EEDTadcldtfp_2atfp0_EEOT_OT0_Unexecuted instantiation: _ZN6common7FindKeyIRNSt3__13mapINS1_12basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEE8UniValueNS1_4lessIS8_EENS6_INS1_4pairIKS8_S9_EEEEEERSD_EEDTadcldtfp_2atfp0_EEOT_OT0_ | 
| 112 |  |  | 
| 113 |  | } // namespace common | 
| 114 |  |  | 
| 115 |  | #endif // BITCOIN_COMMON_SETTINGS_H |