Merge bitcoin/bitcoin#27491: refactor: Move chain constants to the util library

d168458d1f scripted-diff: Remove unused chainparamsbase includes (TheCharlatan)
e9ee8aaf3a Add missing definitions in prep for scripted diff (TheCharlatan)
ba8fc7d788 refactor: Replace string chain name constants with ChainTypes (TheCharlatan)
401453df41 refactor: Introduce ChainType getters for ArgsManager (TheCharlatan)
bfc21c31b2 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 d168458d1f. Just suggested changes since last review.

Tree-SHA512: ac2fbe5cbbab4f52eae1e30af1f16700b6589eb4764c328a151a712adfc37f326cc94a65c385534c57d4bc92cc1a13bf1777d92bc924a20dbb30440e7380b316
This commit is contained in:
fanquake
2023-05-09 15:23:32 +01:00
86 changed files with 402 additions and 254 deletions

39
src/util/chaintype.cpp Normal file
View 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
View 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

View File

@@ -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;

View File

@@ -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.