aboutsummaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
authorfanquake <fanquake@gmail.com>2023-05-09 15:23:32 +0100
committerfanquake <fanquake@gmail.com>2023-05-09 15:42:21 +0100
commitfc06881f13495154c888a64a38c7d538baf00435 (patch)
tree9be742027b5debdbd7c70d03b6c67a9efde05b3a /src/util
parentd5ff96f92008b12660007327aa8dc87bc5f63897 (diff)
parentd168458d1ff987e0d741c75ac1d4b63ae0cfb7e7 (diff)
Merge bitcoin/bitcoin#27491: refactor: Move chain constants to the util library
d168458d1ff987e0d741c75ac1d4b63ae0cfb7e7 scripted-diff: Remove unused chainparamsbase includes (TheCharlatan) e9ee8aaf3acdf6dce2b339916d4c602484570050 Add missing definitions in prep for scripted diff (TheCharlatan) ba8fc7d788932b25864fb260ca14983aa2398c23 refactor: Replace string chain name constants with ChainTypes (TheCharlatan) 401453df419af35957ec711423ac3d93ad512fe8 refactor: Introduce ChainType getters for ArgsManager (TheCharlatan) bfc21c31b2186f7d30fc9a9ca7d6887ab61c6fb9 refactor: Create chaintype files (TheCharlatan) Pull request description: This pull request is part of the `libbitcoinkernel` project https://github.com/bitcoin/bitcoin/issues/24303 https://github.com/bitcoin/bitcoin/projects/18 and more specifically its "Step 2: Decouple most non-consensus code from libbitcoinkernel". It is also a follow up to #26177. It replaces pull request https://github.com/bitcoin/bitcoin/pull/27294, which just moved the constants to a new file, but did not re-declare them as enums. The code move of the chain name constants out of the `chainparamsbase` to their own separate header allows the kernel `chainparams` to no longer include `chainparamsbase`. The `chainparamsbase` contain references to the `ArgsManager` and networking related options that should not belong to the kernel library. Besides this move, the constants are re-declared as enums with helper functions facilitating string conversions. ACKs for top commit: ryanofsky: Code review ACK d168458d1ff987e0d741c75ac1d4b63ae0cfb7e7. Just suggested changes since last review. Tree-SHA512: ac2fbe5cbbab4f52eae1e30af1f16700b6589eb4764c328a151a712adfc37f326cc94a65c385534c57d4bc92cc1a13bf1777d92bc924a20dbb30440e7380b316
Diffstat (limited to 'src/util')
-rw-r--r--src/util/chaintype.cpp39
-rw-r--r--src/util/chaintype.h22
-rw-r--r--src/util/settings.cpp8
-rw-r--r--src/util/settings.h6
4 files changed, 68 insertions, 7 deletions
diff --git a/src/util/chaintype.cpp b/src/util/chaintype.cpp
new file mode 100644
index 0000000000..8a199e352a
--- /dev/null
+++ b/src/util/chaintype.cpp
@@ -0,0 +1,39 @@
+// Copyright (c) 2023 The Bitcoin Core developers
+// Distributed under the MIT software license, see the accompanying
+// file COPYING or http://www.opensource.org/licenses/mit-license.php.
+
+#include <util/chaintype.h>
+
+#include <cassert>
+#include <optional>
+#include <string>
+
+std::string ChainTypeToString(ChainType chain)
+{
+ switch (chain) {
+ case ChainType::MAIN:
+ return "main";
+ case ChainType::TESTNET:
+ return "test";
+ case ChainType::SIGNET:
+ return "signet";
+ case ChainType::REGTEST:
+ return "regtest";
+ }
+ assert(false);
+}
+
+std::optional<ChainType> ChainTypeFromString(std::string_view chain)
+{
+ if (chain == "main") {
+ return ChainType::MAIN;
+ } else if (chain == "test") {
+ return ChainType::TESTNET;
+ } else if (chain == "signet") {
+ return ChainType::SIGNET;
+ } else if (chain == "regtest") {
+ return ChainType::REGTEST;
+ } else {
+ return std::nullopt;
+ }
+}
diff --git a/src/util/chaintype.h b/src/util/chaintype.h
new file mode 100644
index 0000000000..c73985df57
--- /dev/null
+++ b/src/util/chaintype.h
@@ -0,0 +1,22 @@
+// Copyright (c) 2023 The Bitcoin Core developers
+// Distributed under the MIT software license, see the accompanying
+// file COPYING or http://www.opensource.org/licenses/mit-license.php.
+
+#ifndef BITCOIN_UTIL_CHAINTYPE_H
+#define BITCOIN_UTIL_CHAINTYPE_H
+
+#include <optional>
+#include <string>
+
+enum class ChainType {
+ MAIN,
+ TESTNET,
+ SIGNET,
+ REGTEST,
+};
+
+std::string ChainTypeToString(ChainType chain);
+
+std::optional<ChainType> ChainTypeFromString(std::string_view chain);
+
+#endif // BITCOIN_UTIL_CHAINTYPE_H
diff --git a/src/util/settings.cpp b/src/util/settings.cpp
index bb257c0584..db3d60046e 100644
--- a/src/util/settings.cpp
+++ b/src/util/settings.cpp
@@ -130,7 +130,7 @@ SettingsValue GetSetting(const Settings& settings,
const std::string& name,
bool ignore_default_section_config,
bool ignore_nonpersistent,
- bool get_chain_name)
+ bool get_chain_type)
{
SettingsValue result;
bool done = false; // Done merging any more settings sources.
@@ -145,17 +145,17 @@ SettingsValue GetSetting(const Settings& settings,
// assigned value instead of last. In general, later settings take
// precedence over early settings, but for backwards compatibility in
// the config file the precedence is reversed for all settings except
- // chain name settings.
+ // chain type settings.
const bool reverse_precedence =
(source == Source::CONFIG_FILE_NETWORK_SECTION || source == Source::CONFIG_FILE_DEFAULT_SECTION) &&
- !get_chain_name;
+ !get_chain_type;
// Weird behavior preserved for backwards compatibility: Negated
// -regtest and -testnet arguments which you would expect to override
// values set in the configuration file are currently accepted but
// silently ignored. It would be better to apply these just like other
// negated values, or at least warn they are ignored.
- const bool skip_negated_command_line = get_chain_name;
+ const bool skip_negated_command_line = get_chain_type;
if (done) return;
diff --git a/src/util/settings.h b/src/util/settings.h
index 27e87a4751..bb1fe585e1 100644
--- a/src/util/settings.h
+++ b/src/util/settings.h
@@ -60,14 +60,14 @@ bool WriteSettings(const fs::path& path,
//! command line). Only return settings in the
//! read-only config and read-write settings
//! files.
-//! @param get_chain_name - enable special backwards compatible behavior
-//! for GetChainName
+//! @param get_chain_type - enable special backwards compatible behavior
+//! for GetChainType
SettingsValue GetSetting(const Settings& settings,
const std::string& section,
const std::string& name,
bool ignore_default_section_config,
bool ignore_nonpersistent,
- bool get_chain_name);
+ bool get_chain_type);
//! Get combined setting value similar to GetSetting(), except if setting was
//! specified multiple times, return a list of all the values specified.