diff options
author | TheCharlatan <seb.kung@gmail.com> | 2023-04-17 22:20:59 +0200 |
---|---|---|
committer | TheCharlatan <seb.kung@gmail.com> | 2023-05-09 15:49:14 +0200 |
commit | ba8fc7d788932b25864fb260ca14983aa2398c23 (patch) | |
tree | 3049d9b5282c243faf7e0ab1e7d2a7ee17937a36 /src/qt | |
parent | 401453df419af35957ec711423ac3d93ad512fe8 (diff) |
refactor: Replace string chain name constants with ChainTypes
This commit effectively moves the definition of these constants
out of the chainparamsbase to their own file.
Using the ChainType enums provides better type safety compared to
passing around strings.
The commit is part of an ongoing effort to decouple the libbitcoinkernel
library from the ArgsManager and other functionality that should not be
part of the kernel library.
Diffstat (limited to 'src/qt')
-rw-r--r-- | src/qt/bitcoin.cpp | 2 | ||||
-rw-r--r-- | src/qt/guiutil.cpp | 25 | ||||
-rw-r--r-- | src/qt/intro.cpp | 3 | ||||
-rw-r--r-- | src/qt/networkstyle.cpp | 15 | ||||
-rw-r--r-- | src/qt/networkstyle.h | 4 | ||||
-rw-r--r-- | src/qt/rpcconsole.cpp | 2 | ||||
-rw-r--r-- | src/qt/test/apptests.cpp | 2 | ||||
-rw-r--r-- | src/qt/test/test_main.cpp | 3 |
8 files changed, 31 insertions, 25 deletions
diff --git a/src/qt/bitcoin.cpp b/src/qt/bitcoin.cpp index 2f00009596..d602d2c1ac 100644 --- a/src/qt/bitcoin.cpp +++ b/src/qt/bitcoin.cpp @@ -601,7 +601,7 @@ int GuiMain(int argc, char* argv[]) PaymentServer::ipcParseCommandLine(argc, argv); #endif - QScopedPointer<const NetworkStyle> networkStyle(NetworkStyle::instantiate(Params().NetworkIDString())); + QScopedPointer<const NetworkStyle> networkStyle(NetworkStyle::instantiate(Params().GetChainType())); assert(!networkStyle.isNull()); // Allow for separate UI settings for testnets QApplication::setApplicationName(networkStyle->getAppName()); diff --git a/src/qt/guiutil.cpp b/src/qt/guiutil.cpp index dc7daed4bc..8d8328aad8 100644 --- a/src/qt/guiutil.cpp +++ b/src/qt/guiutil.cpp @@ -21,6 +21,7 @@ #include <protocol.h> #include <script/script.h> #include <script/standard.h> +#include <util/chaintype.h> #include <util/exception.h> #include <util/fs.h> #include <util/fs_helpers.h> @@ -503,12 +504,12 @@ bool LabelOutOfFocusEventFilter::eventFilter(QObject* watched, QEvent* event) #ifdef WIN32 fs::path static StartupShortcutPath() { - std::string chain = gArgs.GetChainName(); - if (chain == CBaseChainParams::MAIN) + ChainType chain = gArgs.GetChainType(); + if (chain == ChainType::MAIN) return GetSpecialFolderPath(CSIDL_STARTUP) / "Bitcoin.lnk"; - if (chain == CBaseChainParams::TESTNET) // Remove this special case when CBaseChainParams::TESTNET = "testnet4" + if (chain == ChainType::TESTNET) // Remove this special case when testnet CBaseChainParams::DataDir() is incremented to "testnet4" return GetSpecialFolderPath(CSIDL_STARTUP) / "Bitcoin (testnet).lnk"; - return GetSpecialFolderPath(CSIDL_STARTUP) / fs::u8path(strprintf("Bitcoin (%s).lnk", chain)); + return GetSpecialFolderPath(CSIDL_STARTUP) / fs::u8path(strprintf("Bitcoin (%s).lnk", ChainTypeToString(chain))); } bool GetStartOnSystemStartup() @@ -541,7 +542,7 @@ bool SetStartOnSystemStartup(bool fAutoStart) // Start client minimized QString strArgs = "-min"; // Set -testnet /-regtest options - strArgs += QString::fromStdString(strprintf(" -chain=%s", gArgs.GetChainName())); + strArgs += QString::fromStdString(strprintf(" -chain=%s", gArgs.GetChainTypeString())); // Set the path to the shortcut target psl->SetPath(pszExePath); @@ -586,10 +587,10 @@ fs::path static GetAutostartDir() fs::path static GetAutostartFilePath() { - std::string chain = gArgs.GetChainName(); - if (chain == CBaseChainParams::MAIN) + ChainType chain = gArgs.GetChainType(); + if (chain == ChainType::MAIN) return GetAutostartDir() / "bitcoin.desktop"; - return GetAutostartDir() / fs::u8path(strprintf("bitcoin-%s.desktop", chain)); + return GetAutostartDir() / fs::u8path(strprintf("bitcoin-%s.desktop", ChainTypeToString(chain))); } bool GetStartOnSystemStartup() @@ -629,15 +630,15 @@ bool SetStartOnSystemStartup(bool fAutoStart) std::ofstream optionFile{GetAutostartFilePath(), std::ios_base::out | std::ios_base::trunc}; if (!optionFile.good()) return false; - std::string chain = gArgs.GetChainName(); + ChainType chain = gArgs.GetChainType(); // Write a bitcoin.desktop file to the autostart directory: optionFile << "[Desktop Entry]\n"; optionFile << "Type=Application\n"; - if (chain == CBaseChainParams::MAIN) + if (chain == ChainType::MAIN) optionFile << "Name=Bitcoin\n"; else - optionFile << strprintf("Name=Bitcoin (%s)\n", chain); - optionFile << "Exec=" << pszExePath << strprintf(" -min -chain=%s\n", chain); + optionFile << strprintf("Name=Bitcoin (%s)\n", ChainTypeToString(chain)); + optionFile << "Exec=" << pszExePath << strprintf(" -min -chain=%s\n", ChainTypeToString(chain)); optionFile << "Terminal=false\n"; optionFile << "Hidden=false\n"; optionFile.close(); diff --git a/src/qt/intro.cpp b/src/qt/intro.cpp index a54ba19354..f86b167076 100644 --- a/src/qt/intro.cpp +++ b/src/qt/intro.cpp @@ -9,6 +9,7 @@ #include <chainparams.h> #include <qt/intro.h> #include <qt/forms/ui_intro.h> +#include <util/chaintype.h> #include <util/fs.h> #include <qt/guiconstants.h> @@ -219,7 +220,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.cpp b/src/qt/networkstyle.cpp index b789e6a958..e9ec4349b5 100644 --- a/src/qt/networkstyle.cpp +++ b/src/qt/networkstyle.cpp @@ -8,19 +8,20 @@ #include <chainparamsbase.h> #include <tinyformat.h> +#include <util/chaintype.h> #include <QApplication> static const struct { - const char *networkId; + const ChainType networkId; const char *appName; const int iconColorHueShift; const int iconColorSaturationReduction; } network_styles[] = { - {"main", QAPP_APP_NAME_DEFAULT, 0, 0}, - {"test", QAPP_APP_NAME_TESTNET, 70, 30}, - {"signet", QAPP_APP_NAME_SIGNET, 35, 15}, - {"regtest", QAPP_APP_NAME_REGTEST, 160, 30}, + {ChainType::MAIN, QAPP_APP_NAME_DEFAULT, 0, 0}, + {ChainType::TESTNET, QAPP_APP_NAME_TESTNET, 70, 30}, + {ChainType::SIGNET, QAPP_APP_NAME_SIGNET, 35, 15}, + {ChainType::REGTEST, QAPP_APP_NAME_REGTEST, 160, 30}, }; // titleAddText needs to be const char* for tr() @@ -77,9 +78,9 @@ NetworkStyle::NetworkStyle(const QString &_appName, const int iconColorHueShift, trayAndWindowIcon = QIcon(pixmap.scaled(QSize(256,256))); } -const NetworkStyle* NetworkStyle::instantiate(const std::string& networkId) +const NetworkStyle* NetworkStyle::instantiate(const ChainType networkId) { - std::string titleAddText = networkId == CBaseChainParams::MAIN ? "" : strprintf("[%s]", networkId); + std::string titleAddText = networkId == ChainType::MAIN ? "" : strprintf("[%s]", ChainTypeToString(networkId)); for (const auto& network_style : network_styles) { if (networkId == network_style.networkId) { return new NetworkStyle( diff --git a/src/qt/networkstyle.h b/src/qt/networkstyle.h index a73e3e2625..dd2aee3eb3 100644 --- a/src/qt/networkstyle.h +++ b/src/qt/networkstyle.h @@ -5,6 +5,8 @@ #ifndef BITCOIN_QT_NETWORKSTYLE_H #define BITCOIN_QT_NETWORKSTYLE_H +#include <util/chaintype.h> + #include <QIcon> #include <QPixmap> #include <QString> @@ -14,7 +16,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 76500a4a36..972bc36ad2 100644 --- a/src/qt/rpcconsole.cpp +++ b/src/qt/rpcconsole.cpp @@ -744,7 +744,7 @@ void RPCConsole::setClientModel(ClientModel *model, int bestblock_height, int64_ ui->dataDir->setText(model->dataDir()); ui->blocksDir->setText(model->blocksDir()); ui->startupTime->setText(model->formatClientStartupTime()); - ui->networkName->setText(QString::fromStdString(Params().NetworkIDString())); + ui->networkName->setText(QString::fromStdString(Params().GetChainTypeString())); //Setup autocomplete and attach it QStringList wordList; diff --git a/src/qt/test/apptests.cpp b/src/qt/test/apptests.cpp index 000bbe65be..e918e84184 100644 --- a/src/qt/test/apptests.cpp +++ b/src/qt/test/apptests.cpp @@ -73,7 +73,7 @@ void AppTests::appTests() qRegisterMetaType<interfaces::BlockAndHeaderTipInfo>("interfaces::BlockAndHeaderTipInfo"); m_app.parameterSetup(); QVERIFY(m_app.createOptionsModel(/*resetSettings=*/true)); - QScopedPointer<const NetworkStyle> style(NetworkStyle::instantiate(Params().NetworkIDString())); + QScopedPointer<const NetworkStyle> style(NetworkStyle::instantiate(Params().GetChainType())); m_app.setupPlatformStyle(); m_app.createWindow(style.data()); connect(&m_app, &BitcoinApplication::windowShown, this, &AppTests::guiTests); diff --git a/src/qt/test/test_main.cpp b/src/qt/test/test_main.cpp index 2d069f76a0..eaadbd7f7a 100644 --- a/src/qt/test/test_main.cpp +++ b/src/qt/test/test_main.cpp @@ -14,6 +14,7 @@ #include <qt/test/rpcnestedtests.h> #include <qt/test/uritests.h> #include <test/util/setup_common.h> +#include <util/chaintype.h> #ifdef ENABLE_WALLET #include <qt/test/addressbooktests.h> @@ -57,7 +58,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"; }()); |