Skip to content

Commit

Permalink
Preparations for more testchains
Browse files Browse the repository at this point in the history
Summary:
 * Testchains: Generic selection with -chain=<str> in addition of -testnet and -regtest

 * Testchains: Qt: Simplify network/chain styles

This is a backport of Core [[bitcoin/bitcoin#16680 | PR16680]]

Depends on D5887

Test Plan:
  make check

Run bitcoin-qt on win64

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Subscribers: Fabien

Differential Revision: https://reviews.bitcoinabc.org/D5888
  • Loading branch information
jtimon authored and deadalnix committed Apr 30, 2020
1 parent e1fcaa7 commit cf6846f
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 46 deletions.
4 changes: 2 additions & 2 deletions src/bitcoin-cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ static int AppInitRPC(int argc, char *argv[]) {
error.c_str());
return EXIT_FAILURE;
}
// Check for -testnet or -regtest parameter (BaseParams() calls are only
// valid after this clause)
// Check for -chain, -testnet or -regtest parameter (BaseParams() calls are
// only valid after this clause)
try {
SelectBaseParams(gArgs.GetChainName());
} catch (const std::exception &e) {
Expand Down
4 changes: 2 additions & 2 deletions src/bitcoin-tx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ static int AppInitRawTx(int argc, char *argv[]) {
return EXIT_FAILURE;
}

// Check for -testnet or -regtest parameter (Params() calls are only valid
// after this clause)
// Check for -chain, -testnet or -regtest parameter (Params() calls are only
// valid after this clause)
try {
SelectParams(gArgs.GetChainName());
} catch (const std::exception &e) {
Expand Down
4 changes: 2 additions & 2 deletions src/bitcoind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ static bool AppInit(int argc, char *argv[]) {
return InitError(
strprintf("Error reading configuration file: %s\n", error));
}
// Check for -testnet or -regtest parameter (Params() calls are only
// valid after this clause)
// Check for -chain, -testnet or -regtest parameter (Params() calls are
// only valid after this clause)
try {
SelectParams(gArgs.GetChainName());
} catch (const std::exception &e) {
Expand Down
21 changes: 13 additions & 8 deletions src/chainparamsbase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,19 @@ const std::string CBaseChainParams::TESTNET = "test";
const std::string CBaseChainParams::REGTEST = "regtest";

void SetupChainParamsBaseOptions() {
gArgs.AddArg("-regtest",
"Enter regression test mode, which uses a special chain in "
"which blocks can be solved instantly. This is intended for "
"regression testing tools and app development.",
ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY,
OptionsCategory::CHAINPARAMS);
gArgs.AddArg("-testnet", "Use the test chain", ArgsManager::ALLOW_ANY,
OptionsCategory::CHAINPARAMS);
gArgs.AddArg("-chain=<chain>",
"Use the chain <chain> (default: main). Allowed values: main, "
"test, regtest",
ArgsManager::ALLOW_ANY, OptionsCategory::CHAINPARAMS);
gArgs.AddArg(
"-regtest",
"Enter regression test mode, which uses a special chain in which "
"blocks can be solved instantly. This is intended for regression "
"testing tools and app development. Equivalent to -chain=regtest.",
ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY,
OptionsCategory::CHAINPARAMS);
gArgs.AddArg("-testnet", "Use the test chain. Equivalent to -chain=test.",
ArgsManager::ALLOW_ANY, OptionsCategory::CHAINPARAMS);
}

static std::unique_ptr<CBaseChainParams> globalChainBaseParams;
Expand Down
8 changes: 4 additions & 4 deletions src/qt/bitcoin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -655,8 +655,8 @@ int GuiMain(int argc, char *argv[]) {
// network-specific settings.
// - Needs to be done before createOptionsModel.

// Check for -testnet or -regtest parameter (Params() calls are only valid
// after this clause)
// Check for -chain, -testnet or -regtest parameter (Params() calls are only
// valid after this clause)
try {
node->selectParams(gArgs.GetChainName());
} catch (std::exception &e) {
Expand All @@ -670,8 +670,8 @@ int GuiMain(int argc, char *argv[]) {
PaymentServer::ipcParseCommandLine(*node, argc, argv);
#endif

QScopedPointer<const NetworkStyle> networkStyle(NetworkStyle::instantiate(
QString::fromStdString(Params().NetworkIDString())));
QScopedPointer<const NetworkStyle> networkStyle(
NetworkStyle::instantiate(Params().NetworkIDString()));
assert(!networkStyle.isNull());
// Allow for separate UI settings for testnets
QApplication::setApplicationName(networkStyle->getAppName());
Expand Down
9 changes: 3 additions & 6 deletions src/qt/guiutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -598,9 +598,8 @@ bool SetStartOnSystemStartup(bool fAutoStart) {
// Start client minimized
QString strArgs = "-min";
// Set -testnet /-regtest options
strArgs += QString::fromStdString(strprintf(
" -testnet=%d -regtest=%d", gArgs.GetBoolArg("-testnet", false),
gArgs.GetBoolArg("-regtest", false)));
strArgs += QString::fromStdString(
strprintf(" -chain=%s", gArgs.GetChainName()));

// Set the path to the shortcut target
psl->SetPath(pszExePath);
Expand Down Expand Up @@ -702,9 +701,7 @@ bool SetStartOnSystemStartup(bool fAutoStart) {
optionFile << strprintf("Name=Bitcoin (%s)\n", chain);
}
optionFile << "Exec=" << pszExePath
<< strprintf(" -min -testnet=%d -regtest=%d\n",
gArgs.GetBoolArg("-testnet", false),
gArgs.GetBoolArg("-regtest", false));
<< strprintf(" -min -chain=%s\n", chain);
optionFile << "Terminal=false\n";
optionFile << "Hidden=false\n";
optionFile.close();
Expand Down
17 changes: 10 additions & 7 deletions src/qt/networkstyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@

#include <qt/guiconstants.h>

#include <chainparamsbase.h>
#include <tinyformat.h>

#include <QApplication>

static const struct {
const char *networkId;
const char *appName;
const int iconColorHueShift;
const int iconColorSaturationReduction;
const char *titleAddText;
} network_styles[] = {{"main", QAPP_APP_NAME_DEFAULT, 0, 0, ""},
{"test", QAPP_APP_NAME_TESTNET, 70, 30,
QT_TRANSLATE_NOOP("SplashScreen", "[testnet]")},
{"regtest", QAPP_APP_NAME_REGTEST, 160, 30, "[regtest]"}};
} network_styles[] = {{"main", QAPP_APP_NAME_DEFAULT, 0, 0},
{"test", QAPP_APP_NAME_TESTNET, 70, 30},
{"regtest", QAPP_APP_NAME_REGTEST, 160, 30}};
static const unsigned network_styles_count =
sizeof(network_styles) / sizeof(*network_styles);

Expand Down Expand Up @@ -73,13 +74,15 @@ NetworkStyle::NetworkStyle(const QString &_appName, const int iconColorHueShift,
trayAndWindowIcon = QIcon(pixmap.scaled(QSize(256, 256)));
}

const NetworkStyle *NetworkStyle::instantiate(const QString &networkId) {
const NetworkStyle *NetworkStyle::instantiate(const std::string &networkId) {
std::string titleAddText =
networkId == CBaseChainParams::MAIN ? "" : strprintf("[%s]", networkId);
for (unsigned x = 0; x < network_styles_count; ++x) {
if (networkId == network_styles[x].networkId) {
return new NetworkStyle(
network_styles[x].appName, network_styles[x].iconColorHueShift,
network_styles[x].iconColorSaturationReduction,
network_styles[x].titleAddText);
titleAddText.c_str());
}
}
return nullptr;
Expand Down
5 changes: 3 additions & 2 deletions src/qt/networkstyle.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
/* Coin network-specific GUI style information */
class NetworkStyle {
public:
/** Get style associated with provided BIP70 network id, or 0 if not known
/**
* Get style associated with provided BIP70 network id, or 0 if not known.
*/
static const NetworkStyle *instantiate(const QString &networkId);
static const NetworkStyle *instantiate(const std::string &networkId);

const QString &getAppName() const { return appName; }
const QIcon &getAppIcon() const { return appIcon; }
Expand Down
4 changes: 2 additions & 2 deletions src/qt/test/apptests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ void AppTests::appTests() {

m_app.parameterSetup();
m_app.createOptionsModel(true /* reset settings */);
QScopedPointer<const NetworkStyle> style(NetworkStyle::instantiate(
QString::fromStdString(Params().NetworkIDString())));
QScopedPointer<const NetworkStyle> style(
NetworkStyle::instantiate(Params().NetworkIDString()));
m_app.setupPlatformStyle();
m_app.createWindow(&config, style.data());
connect(&m_app, &BitcoinApplication::windowShown, this,
Expand Down
2 changes: 1 addition & 1 deletion src/test/util_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1138,7 +1138,7 @@ BOOST_FIXTURE_TEST_CASE(util_ChainMerge, ChainMergeTestingSetup) {
// <input> || <output>
BOOST_CHECK_EQUAL(
out_sha_hex,
"b284f4b4a15dd6bf8c06213a69a004b1960388e1d9917173927db52ac220927f");
"94b4ad55c8ac639a56b93e36f7e32e4c611fd7d7dd7b2be6a71707b1eadcaec7");
}

BOOST_AUTO_TEST_CASE(util_FormatMoney) {
Expand Down
13 changes: 7 additions & 6 deletions src/util/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1108,20 +1108,21 @@ bool ArgsManager::ReadConfigFiles(std::string &error,

std::string ArgsManager::GetChainName() const {
LOCK(cs_args);
bool fRegTest = ArgsManagerHelper::GetNetBoolArg(*this, "-regtest");
bool fTestNet = ArgsManagerHelper::GetNetBoolArg(*this, "-testnet");
const bool fRegTest = ArgsManagerHelper::GetNetBoolArg(*this, "-regtest");
const bool fTestNet = ArgsManagerHelper::GetNetBoolArg(*this, "-testnet");
const bool is_chain_arg_set = IsArgSet("-chain");

if (fTestNet && fRegTest) {
throw std::runtime_error(
"Invalid combination of -regtest and -testnet.");
if (int(is_chain_arg_set) + int(fRegTest) + int(fTestNet) > 1) {
throw std::runtime_error("Invalid combination of -regtest, -testnet "
"and -chain. Can use at most one.");
}
if (fRegTest) {
return CBaseChainParams::REGTEST;
}
if (fTestNet) {
return CBaseChainParams::TESTNET;
}
return CBaseChainParams::MAIN;
return GetArg("-chain", CBaseChainParams::MAIN);
}

bool RenameOver(fs::path src, fs::path dest) {
Expand Down
12 changes: 8 additions & 4 deletions test/lint/check-doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@
REGEX_DOC = r'AddArg\(\s*"(-[^"=]+?)(?:=|")'

# list false positive unknows arguments
SET_FALSE_POSITIVE_UNKNOWNS = set(['-zmqpubhashblock',
'-zmqpubhashtx',
'-zmqpubrawblock',
'-zmqpubrawtx'])
SET_FALSE_POSITIVE_UNKNOWNS = set([
'-regtest',
'-testnet',
'-zmqpubhashblock',
'-zmqpubhashtx',
'-zmqpubrawblock',
'-zmqpubrawtx',
])

# list false positive undocumented arguments
SET_FALSE_POSITIVE_UNDOCUMENTED = set(['-dbcrashratio',
Expand Down

0 comments on commit cf6846f

Please sign in to comment.