diff --git a/src/Makefile.am b/src/Makefile.am index a0eca537b48d..9c8142b22dc1 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -403,6 +403,7 @@ BITCOIN_CORE_H = \ util/hash_type.h \ util/helpers.h \ util/asmap.h \ + util/chaintype.h \ util/getuniquepath.h \ util/macros.h \ util/message.h \ @@ -993,6 +994,7 @@ libbitcoin_util_a_SOURCES = \ util/bip32.cpp \ util/bytevectorhash.cpp \ util/check.cpp \ + util/chaintype.cpp \ util/edge.cpp \ util/error.cpp \ util/fees.cpp \ diff --git a/src/bench/load_external.cpp b/src/bench/load_external.cpp index 150654a095ca..71373718ea5c 100644 --- a/src/bench/load_external.cpp +++ b/src/bench/load_external.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include /** @@ -22,7 +23,7 @@ */ static void LoadExternalBlockFile(benchmark::Bench& bench) { - const auto testing_setup{MakeNoLogFileContext(CBaseChainParams::MAIN)}; + const auto testing_setup{MakeNoLogFileContext(ChainType::MAIN)}; // Create a single block as in the blocks files (magic bytes, block size, // block data) as a stream object. diff --git a/src/bench/logging.cpp b/src/bench/logging.cpp index a67e847c523e..bda582490bce 100644 --- a/src/bench/logging.cpp +++ b/src/bench/logging.cpp @@ -5,6 +5,7 @@ #include #include #include +#include // All but 2 of the benchmarks should have roughly similar performance: // @@ -18,7 +19,7 @@ static void Logging(benchmark::Bench& bench, const std::vector& ext LogInstance().DisableCategory(BCLog::LogFlags::ALL); TestingSetup test_setup{ - CBaseChainParams::REGTEST, + ChainType::REGTEST, extra_args, }; diff --git a/src/bench/mempool_stress.cpp b/src/bench/mempool_stress.cpp index 0cf4e83ef8ce..b439c1430449 100644 --- a/src/bench/mempool_stress.cpp +++ b/src/bench/mempool_stress.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include @@ -85,7 +86,7 @@ static void ComplexMemPool(benchmark::Bench& bench) childTxs = static_cast(bench.complexityN()); } std::vector ordered_coins = CreateOrderedCoins(det_rand, childTxs, /*min_ancestors=*/1); - const auto testing_setup = MakeNoLogFileContext(CBaseChainParams::MAIN); + const auto testing_setup = MakeNoLogFileContext(ChainType::MAIN); CTxMemPool& pool = *testing_setup.get()->m_node.mempool; LOCK2(cs_main, pool.cs); bench.run([&]() NO_THREAD_SAFETY_ANALYSIS { @@ -100,7 +101,7 @@ static void ComplexMemPool(benchmark::Bench& bench) static void MempoolCheck(benchmark::Bench& bench) { FastRandomContext det_rand{true}; - auto testing_setup = MakeNoLogFileContext(CBaseChainParams::REGTEST, {"-checkmempool=1"}); + auto testing_setup = MakeNoLogFileContext(ChainType::REGTEST, {"-checkmempool=1"}); CTxMemPool& pool = *testing_setup.get()->m_node.mempool; LOCK2(cs_main, pool.cs); testing_setup->PopulateMempool(det_rand, 400, true); diff --git a/src/bench/rpc_blockchain.cpp b/src/bench/rpc_blockchain.cpp index 5a39e3393411..f3e91540b94b 100644 --- a/src/bench/rpc_blockchain.cpp +++ b/src/bench/rpc_blockchain.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include @@ -19,7 +20,7 @@ namespace { struct TestBlockAndIndex { - const std::unique_ptr testing_setup{MakeNoLogFileContext(CBaseChainParams::MAIN)}; + const std::unique_ptr testing_setup{MakeNoLogFileContext(ChainType::MAIN)}; CBlock block{}; uint256 blockHash{}; CBlockIndex blockindex{}; diff --git a/src/bitcoin-util.cpp b/src/bitcoin-util.cpp index 15d24fe7908a..956a921b42c1 100644 --- a/src/bitcoin-util.cpp +++ b/src/bitcoin-util.cpp @@ -70,7 +70,7 @@ static int AppInitUtil(ArgsManager& args, int argc, char* argv[]) // Check for chain settings (Params() calls are only valid after this clause) try { - SelectParams(args.GetChainName()); + SelectParams(args.GetChainType()); } catch (const std::exception& e) { tfm::format(std::cerr, "Error: %s\n", e.what()); return EXIT_FAILURE; diff --git a/src/bitcoin-wallet.cpp b/src/bitcoin-wallet.cpp index be9ba957ec4b..0aef1d857cb3 100644 --- a/src/bitcoin-wallet.cpp +++ b/src/bitcoin-wallet.cpp @@ -90,7 +90,7 @@ static std::optional WalletAppInit(ArgsManager& args, int argc, char* argv[ return EXIT_FAILURE; } // Check for chain settings (Params() calls are only valid after this clause) - SelectParams(args.GetChainName()); + SelectParams(args.GetChainType()); return std::nullopt; } diff --git a/src/chainparams.cpp b/src/chainparams.cpp index 377b6b5af5b7..37c874978e87 100644 --- a/src/chainparams.cpp +++ b/src/chainparams.cpp @@ -1349,24 +1349,42 @@ const CChainParams &Params() { return *globalChainParams; } -std::unique_ptr CreateChainParams(const ArgsManager& args, const std::string& chain) +std::unique_ptr CreateChainParams(const ArgsManager& args, const ChainType chain) { - if (chain == CBaseChainParams::MAIN) { + if (chain == ChainType::MAIN) { return std::unique_ptr(new CMainParams()); - } else if (chain == CBaseChainParams::TESTNET) { + } else if (chain == ChainType::TESTNET) { return std::unique_ptr(new CTestNetParams()); - } else if (chain == CBaseChainParams::DEVNET) { + } else if (chain == ChainType::DEVNET) { return std::unique_ptr(new CDevNetParams(args)); - } else if (chain == CBaseChainParams::REGTEST) { + } else if (chain == ChainType::REGTEST) { return std::unique_ptr(new CRegTestParams(args)); } - throw std::runtime_error(strprintf("%s: Unknown chain %s.", __func__, chain)); + throw std::invalid_argument(strprintf("%s: Invalid ChainType value", __func__)); +} + +std::unique_ptr CreateChainParams(const ArgsManager& args, const std::string& chain) +{ + const auto chain_type{ChainTypeFromString(chain)}; + if (!chain_type) { + throw std::runtime_error(strprintf("%s: Unknown chain %s.", __func__, chain)); + } + return CreateChainParams(args, *chain_type); } -void SelectParams(const std::string& network) +void SelectParams(const ChainType chain) { - SelectBaseParams(network); - globalChainParams = CreateChainParams(gArgs, network); + SelectBaseParams(chain); + globalChainParams = CreateChainParams(gArgs, chain); +} + +void SelectParams(const std::string& chain) +{ + const auto chain_type{ChainTypeFromString(chain)}; + if (!chain_type) { + throw std::runtime_error(strprintf("%s: Unknown chain %s.", __func__, chain)); + } + SelectParams(*chain_type); } void SetupChainParamsOptions(ArgsManager& argsman) diff --git a/src/chainparams.h b/src/chainparams.h index 7b5528b9b550..73670172dc57 100644 --- a/src/chainparams.h +++ b/src/chainparams.h @@ -12,6 +12,7 @@ #include #include #include +#include #include #include @@ -121,6 +122,8 @@ class CChainParams int LLMQConnectionRetryTimeout() const { return nLLMQConnectionRetryTimeout; } /** Return the network string */ std::string NetworkIDString() const { return strNetworkID; } + ChainType GetChainType() const { return ChainTypeFromString(strNetworkID).value(); } + std::string GetChainTypeString() const { return strNetworkID; } /** Return the list of hostnames to look up for DNS seeds */ const std::vector& DNSSeeds() const { return vSeeds; } const std::vector& Base58Prefix(Base58Type type) const { return base58Prefixes[type]; } @@ -197,6 +200,7 @@ class CChainParams * @returns a CChainParams* of the chosen chain. * @throws a std::runtime_error if the chain is not supported. */ +std::unique_ptr CreateChainParams(const ArgsManager& args, const ChainType chain); std::unique_ptr CreateChainParams(const ArgsManager& args, const std::string& chain); /** @@ -209,6 +213,7 @@ const CChainParams &Params(); * Sets the params returned by Params() to those for the given chain name. * @throws std::runtime_error when the chain is not supported. */ +void SelectParams(const ChainType chain); void SelectParams(const std::string& chain); /** diff --git a/src/chainparamsbase.cpp b/src/chainparamsbase.cpp index 1a5fb9a8a4b2..9e1e2b6ff4b9 100644 --- a/src/chainparamsbase.cpp +++ b/src/chainparamsbase.cpp @@ -36,22 +36,40 @@ const CBaseChainParams& BaseParams() * Port numbers for incoming Tor connections (9996, 19996, 19796, 19896) have * been chosen arbitrarily to keep ranges of used ports tight. */ -std::unique_ptr CreateBaseChainParams(const std::string& chain) +std::unique_ptr CreateBaseChainParams(const ChainType chain) { - if (chain == CBaseChainParams::MAIN) + if (chain == ChainType::MAIN) return std::make_unique("", 9998, 9996); - else if (chain == CBaseChainParams::TESTNET) + else if (chain == ChainType::TESTNET) return std::make_unique("testnet3", 19998, 19996); - else if (chain == CBaseChainParams::DEVNET) + else if (chain == ChainType::DEVNET) return std::make_unique(gArgs.GetDevNetName(), 19798, 19796); - else if (chain == CBaseChainParams::REGTEST) + else if (chain == ChainType::REGTEST) return std::make_unique("regtest", 19898, 19896); else + throw std::invalid_argument(strprintf("%s: Invalid ChainType value", __func__)); +} + +std::unique_ptr CreateBaseChainParams(const std::string& chain) +{ + const auto chain_type{ChainTypeFromString(chain)}; + if (!chain_type) { throw std::runtime_error(strprintf("%s: Unknown chain %s.", __func__, chain)); + } + return CreateBaseChainParams(*chain_type); } -void SelectBaseParams(const std::string& chain) +void SelectBaseParams(const ChainType chain) { globalChainBaseParams = CreateBaseChainParams(chain); - gArgs.SelectConfigNetwork(chain); + gArgs.SelectConfigNetwork(ChainTypeToString(chain)); +} + +void SelectBaseParams(const std::string& chain) +{ + const auto chain_type{ChainTypeFromString(chain)}; + if (!chain_type) { + throw std::runtime_error(strprintf("%s: Unknown chain %s.", __func__, chain)); + } + SelectBaseParams(*chain_type); } diff --git a/src/chainparamsbase.h b/src/chainparamsbase.h index 718392e04def..391f8b35ef90 100644 --- a/src/chainparamsbase.h +++ b/src/chainparamsbase.h @@ -5,6 +5,8 @@ #ifndef BITCOIN_CHAINPARAMSBASE_H #define BITCOIN_CHAINPARAMSBASE_H +#include + #include #include @@ -44,6 +46,7 @@ class CBaseChainParams * @returns a CBaseChainParams* of the chosen chain. * @throws a std::runtime_error if the chain is not supported. */ +std::unique_ptr CreateBaseChainParams(const ChainType chain); std::unique_ptr CreateBaseChainParams(const std::string& chain); /** @@ -58,6 +61,7 @@ void SetupChainParamsBaseOptions(ArgsManager& argsman); const CBaseChainParams& BaseParams(); /** Sets the params returned by Params() to those for the given network. */ +void SelectBaseParams(const ChainType chain); void SelectBaseParams(const std::string& chain); #endif // BITCOIN_CHAINPARAMSBASE_H diff --git a/src/node/interfaces.cpp b/src/node/interfaces.cpp index f3eac13383ad..0686c947fba9 100644 --- a/src/node/interfaces.cpp +++ b/src/node/interfaces.cpp @@ -831,7 +831,7 @@ class NodeImpl : public Node std::vector signers = {}; const std::string command = gArgs.GetArg("-signer", ""); if (command == "") return {}; - ExternalSigner::Enumerate(command, signers, Params().NetworkIDString()); + ExternalSigner::Enumerate(command, signers, Params().GetChainTypeString()); std::vector> result; for (auto& signer : signers) { result.emplace_back(std::make_unique(std::move(signer))); diff --git a/src/qt/intro.cpp b/src/qt/intro.cpp index a11b2d447c0e..6ef7843dd2e5 100644 --- a/src/qt/intro.cpp +++ b/src/qt/intro.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include @@ -223,7 +224,7 @@ bool Intro::showIfNeeded(bool& did_show_intro, int64_t& prune_MiB) { /* Use selectParams here to guarantee Params() can be used by node interface */ try { - SelectParams(gArgs.GetChainName()); + SelectParams(gArgs.GetChainType()); } catch (const std::exception&) { return false; } diff --git a/src/qt/networkstyle.h b/src/qt/networkstyle.h index f9d872e37f46..238baabc1707 100644 --- a/src/qt/networkstyle.h +++ b/src/qt/networkstyle.h @@ -6,6 +6,8 @@ #ifndef BITCOIN_QT_NETWORKSTYLE_H #define BITCOIN_QT_NETWORKSTYLE_H +#include + #include #include #include @@ -17,7 +19,7 @@ class NetworkStyle { public: /** Get style associated with provided network id, or 0 if not known */ - static const NetworkStyle* instantiate(const std::string& networkId); + static const NetworkStyle* instantiate(const ChainType networkId); const QString &getAppName() const { return appName; } const QIcon &getAppIcon() const { return appIcon; } diff --git a/src/qt/rpcconsole.cpp b/src/qt/rpcconsole.cpp index 1c5596baafb7..12402fff5039 100644 --- a/src/qt/rpcconsole.cpp +++ b/src/qt/rpcconsole.cpp @@ -617,6 +617,8 @@ RPCConsole::RPCConsole(interfaces::Node& node, QWidget* parent, Qt::WindowFlags reloadThemedWidgets(); GUIUtil::handleCloseWindowShortcut(this); + + updateWindowTitle(); } RPCConsole::~RPCConsole() @@ -1523,3 +1525,13 @@ void RPCConsole::updateAlerts(const QString& warnings) this->ui->label_alerts->setVisible(!warnings.isEmpty()); this->ui->label_alerts->setText(warnings); } + +void RPCConsole::updateWindowTitle() +{ + const ChainType chain = Params().GetChainType(); + if (chain == ChainType::MAIN) return; + + const QString chainType = QString::fromStdString(Params().GetChainTypeString()); + const QString title = tr("Node window - [%1]").arg(chainType); + this->setWindowTitle(title); +} \ No newline at end of file diff --git a/src/qt/rpcconsole.h b/src/qt/rpcconsole.h index c978d1af14c6..4d4e56cf6307 100644 --- a/src/qt/rpcconsole.h +++ b/src/qt/rpcconsole.h @@ -217,6 +217,8 @@ public Q_SLOTS: return time_at_event.count() ? GUIUtil::formatDurationStr(time_now - time_at_event) : tr("Never"); } + void updateWindowTitle(); + private Q_SLOTS: void updateAlerts(const QString& warnings); }; diff --git a/src/qt/test/test_main.cpp b/src/qt/test/test_main.cpp index 466c347c19fb..52e02ff20453 100644 --- a/src/qt/test/test_main.cpp +++ b/src/qt/test/test_main.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #ifdef ENABLE_WALLET #include @@ -59,7 +60,7 @@ int main(int argc, char* argv[]) // // All tests must use their own testing setup (if needed). fs::create_directories([] { - BasicTestingSetup dummy{CBaseChainParams::REGTEST}; + BasicTestingSetup dummy{ChainType::REGTEST}; return gArgs.GetDataDirNet() / "blocks"; }()); diff --git a/src/rpc/net.cpp b/src/rpc/net.cpp index 2e75a19f3bcc..0b220b12b17e 100644 --- a/src/rpc/net.cpp +++ b/src/rpc/net.cpp @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -397,7 +398,7 @@ static RPCHelpMan addconnection() }, [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue { - if (Params().NetworkIDString() != CBaseChainParams::REGTEST) { + if (Params().GetChainType() != ChainType::REGTEST) { throw std::runtime_error("addconnection is for regression testing (-regtest mode) only."); } diff --git a/src/test/blockmanager_tests.cpp b/src/test/blockmanager_tests.cpp index 6b29b76e2184..0153c68aadbb 100644 --- a/src/test/blockmanager_tests.cpp +++ b/src/test/blockmanager_tests.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -18,7 +19,7 @@ BOOST_FIXTURE_TEST_SUITE(blockmanager_tests, BasicTestingSetup) BOOST_AUTO_TEST_CASE(blockmanager_find_block_pos) { - const auto params {CreateChainParams(ArgsManager{}, CBaseChainParams::MAIN)}; + const auto params {CreateChainParams(ArgsManager{}, ChainType::MAIN)}; node::BlockManager::Options blockman_opts{ .chainparams = *params, }; diff --git a/src/test/fuzz/block.cpp b/src/test/fuzz/block.cpp index 3d21c6ba200c..d5b60907cbe4 100644 --- a/src/test/fuzz/block.cpp +++ b/src/test/fuzz/block.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include @@ -19,7 +20,7 @@ void initialize_block() { - SelectParams(CBaseChainParams::REGTEST); + SelectParams(ChainType::REGTEST); } FUZZ_TARGET(block, .init = initialize_block) diff --git a/src/test/fuzz/coins_view.cpp b/src/test/fuzz/coins_view.cpp index b8f68fc76e42..b3db4dc17d19 100644 --- a/src/test/fuzz/coins_view.cpp +++ b/src/test/fuzz/coins_view.cpp @@ -3,7 +3,6 @@ // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include -#include #include #include #include diff --git a/src/test/fuzz/descriptor_parse.cpp b/src/test/fuzz/descriptor_parse.cpp index 96d0e58f04b8..1f29a9e99f04 100644 --- a/src/test/fuzz/descriptor_parse.cpp +++ b/src/test/fuzz/descriptor_parse.cpp @@ -8,11 +8,12 @@ #include