mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-06-05 18:52:29 +02:00
Merge bitcoin/bitcoin#27491: refactor: Move chain constants to the util library
d168458d1fscripted-diff: Remove unused chainparamsbase includes (TheCharlatan)e9ee8aaf3aAdd missing definitions in prep for scripted diff (TheCharlatan)ba8fc7d788refactor: Replace string chain name constants with ChainTypes (TheCharlatan)401453df41refactor: Introduce ChainType getters for ArgsManager (TheCharlatan)bfc21c31b2refactor: 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 ACKd168458d1f. Just suggested changes since last review. Tree-SHA512: ac2fbe5cbbab4f52eae1e30af1f16700b6589eb4764c328a151a712adfc37f326cc94a65c385534c57d4bc92cc1a13bf1777d92bc924a20dbb30440e7380b316
This commit is contained in:
39
src/util/chaintype.cpp
Normal file
39
src/util/chaintype.cpp
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
22
src/util/chaintype.h
Normal file
22
src/util/chaintype.h
Normal file
@@ -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
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user