From fa23308e9aad70c99a31f91d8556f1876ea02c04 Mon Sep 17 00:00:00 2001 From: MarcoFalke Date: Thu, 24 Sep 2020 15:11:27 +0200 Subject: [PATCH 1/2] Remove gArgs global from CreateChainParams to aid testing --- src/bench/checkblock.cpp | 3 ++- src/chainparams.cpp | 8 ++++---- src/chainparams.h | 2 +- src/init.cpp | 8 ++++---- src/qt/paymentserver.cpp | 4 ++-- src/test/miner_tests.cpp | 2 +- src/test/pow_tests.cpp | 32 ++++++++++++++++---------------- src/test/validation_tests.cpp | 4 ++-- src/test/versionbits_tests.cpp | 4 ++-- 9 files changed, 34 insertions(+), 33 deletions(-) diff --git a/src/bench/checkblock.cpp b/src/bench/checkblock.cpp index dc0aa4031c..a9f3f5f84d 100644 --- a/src/bench/checkblock.cpp +++ b/src/bench/checkblock.cpp @@ -34,7 +34,8 @@ static void DeserializeAndCheckBlockTest(benchmark::Bench& bench) char a = '\0'; stream.write(&a, 1); // Prevent compaction - const auto chainParams = CreateChainParams(CBaseChainParams::MAIN); + ArgsManager bench_args; + const auto chainParams = CreateChainParams(bench_args, CBaseChainParams::MAIN); bench.unit("block").run([&] { CBlock block; // Note that CBlock caches its checked state, so we need to recreate it here diff --git a/src/chainparams.cpp b/src/chainparams.cpp index d7f7888ef3..fc16abbad0 100644 --- a/src/chainparams.cpp +++ b/src/chainparams.cpp @@ -485,16 +485,16 @@ const CChainParams &Params() { return *globalChainParams; } -std::unique_ptr CreateChainParams(const std::string& chain) +std::unique_ptr CreateChainParams(const ArgsManager& args, const std::string& chain) { if (chain == CBaseChainParams::MAIN) { return std::unique_ptr(new CMainParams()); } else if (chain == CBaseChainParams::TESTNET) { return std::unique_ptr(new CTestNetParams()); } else if (chain == CBaseChainParams::SIGNET) { - return std::unique_ptr(new SigNetParams(gArgs)); + return std::unique_ptr(new SigNetParams(args)); } else if (chain == CBaseChainParams::REGTEST) { - return std::unique_ptr(new CRegTestParams(gArgs)); + return std::unique_ptr(new CRegTestParams(args)); } throw std::runtime_error(strprintf("%s: Unknown chain %s.", __func__, chain)); } @@ -502,5 +502,5 @@ std::unique_ptr CreateChainParams(const std::string& chain) void SelectParams(const std::string& network) { SelectBaseParams(network); - globalChainParams = CreateChainParams(network); + globalChainParams = CreateChainParams(gArgs, network); } diff --git a/src/chainparams.h b/src/chainparams.h index 7aa999fb75..d8b25c7220 100644 --- a/src/chainparams.h +++ b/src/chainparams.h @@ -119,7 +119,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 std::string& chain); +std::unique_ptr CreateChainParams(const ArgsManager& args, const std::string& chain); /** * Return the currently selected parameters. This won't change after app diff --git a/src/init.cpp b/src/init.cpp index 7a5739fded..44fe56dba2 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -380,10 +380,10 @@ void SetupServerArgs(NodeContext& node) const auto testnetBaseParams = CreateBaseChainParams(CBaseChainParams::TESTNET); const auto signetBaseParams = CreateBaseChainParams(CBaseChainParams::SIGNET); const auto regtestBaseParams = CreateBaseChainParams(CBaseChainParams::REGTEST); - const auto defaultChainParams = CreateChainParams(CBaseChainParams::MAIN); - const auto testnetChainParams = CreateChainParams(CBaseChainParams::TESTNET); - const auto signetChainParams = CreateChainParams(CBaseChainParams::SIGNET); - const auto regtestChainParams = CreateChainParams(CBaseChainParams::REGTEST); + const auto defaultChainParams = CreateChainParams(argsman, CBaseChainParams::MAIN); + const auto testnetChainParams = CreateChainParams(argsman, CBaseChainParams::TESTNET); + const auto signetChainParams = CreateChainParams(argsman, CBaseChainParams::SIGNET); + const auto regtestChainParams = CreateChainParams(argsman, CBaseChainParams::REGTEST); // Hidden Options std::vector hidden_args = { diff --git a/src/qt/paymentserver.cpp b/src/qt/paymentserver.cpp index 8679ced685..6c2db52f63 100644 --- a/src/qt/paymentserver.cpp +++ b/src/qt/paymentserver.cpp @@ -94,12 +94,12 @@ void PaymentServer::ipcParseCommandLine(int argc, char* argv[]) SendCoinsRecipient r; if (GUIUtil::parseBitcoinURI(arg, &r) && !r.address.isEmpty()) { - auto tempChainParams = CreateChainParams(CBaseChainParams::MAIN); + auto tempChainParams = CreateChainParams(gArgs, CBaseChainParams::MAIN); if (IsValidDestinationString(r.address.toStdString(), *tempChainParams)) { SelectParams(CBaseChainParams::MAIN); } else { - tempChainParams = CreateChainParams(CBaseChainParams::TESTNET); + tempChainParams = CreateChainParams(gArgs, CBaseChainParams::TESTNET); if (IsValidDestinationString(r.address.toStdString(), *tempChainParams)) { SelectParams(CBaseChainParams::TESTNET); } diff --git a/src/test/miner_tests.cpp b/src/test/miner_tests.cpp index 8686012af7..3de79a9f45 100644 --- a/src/test/miner_tests.cpp +++ b/src/test/miner_tests.cpp @@ -198,7 +198,7 @@ void MinerTestingSetup::TestPackageSelection(const CChainParams& chainparams, co BOOST_AUTO_TEST_CASE(CreateNewBlock_validity) { // Note that by default, these tests run with size accounting enabled. - const auto chainParams = CreateChainParams(CBaseChainParams::MAIN); + const auto chainParams = CreateChainParams(*m_node.args, CBaseChainParams::MAIN); const CChainParams& chainparams = *chainParams; CScript scriptPubKey = CScript() << ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f") << OP_CHECKSIG; std::unique_ptr pblocktemplate; diff --git a/src/test/pow_tests.cpp b/src/test/pow_tests.cpp index ca49b89ad8..1d7f4861fb 100644 --- a/src/test/pow_tests.cpp +++ b/src/test/pow_tests.cpp @@ -14,7 +14,7 @@ BOOST_FIXTURE_TEST_SUITE(pow_tests, BasicTestingSetup) /* Test calculation of next difficulty target with no constraints applying */ BOOST_AUTO_TEST_CASE(get_next_work) { - const auto chainParams = CreateChainParams(CBaseChainParams::MAIN); + const auto chainParams = CreateChainParams(*m_node.args, CBaseChainParams::MAIN); int64_t nLastRetargetTime = 1261130161; // Block #30240 CBlockIndex pindexLast; pindexLast.nHeight = 32255; @@ -26,7 +26,7 @@ BOOST_AUTO_TEST_CASE(get_next_work) /* Test the constraint on the upper bound for next work */ BOOST_AUTO_TEST_CASE(get_next_work_pow_limit) { - const auto chainParams = CreateChainParams(CBaseChainParams::MAIN); + const auto chainParams = CreateChainParams(*m_node.args, CBaseChainParams::MAIN); int64_t nLastRetargetTime = 1231006505; // Block #0 CBlockIndex pindexLast; pindexLast.nHeight = 2015; @@ -38,7 +38,7 @@ BOOST_AUTO_TEST_CASE(get_next_work_pow_limit) /* Test the constraint on the lower bound for actual time taken */ BOOST_AUTO_TEST_CASE(get_next_work_lower_limit_actual) { - const auto chainParams = CreateChainParams(CBaseChainParams::MAIN); + const auto chainParams = CreateChainParams(*m_node.args, CBaseChainParams::MAIN); int64_t nLastRetargetTime = 1279008237; // Block #66528 CBlockIndex pindexLast; pindexLast.nHeight = 68543; @@ -50,7 +50,7 @@ BOOST_AUTO_TEST_CASE(get_next_work_lower_limit_actual) /* Test the constraint on the upper bound for actual time taken */ BOOST_AUTO_TEST_CASE(get_next_work_upper_limit_actual) { - const auto chainParams = CreateChainParams(CBaseChainParams::MAIN); + const auto chainParams = CreateChainParams(*m_node.args, CBaseChainParams::MAIN); int64_t nLastRetargetTime = 1263163443; // NOTE: Not an actual block time CBlockIndex pindexLast; pindexLast.nHeight = 46367; @@ -61,7 +61,7 @@ BOOST_AUTO_TEST_CASE(get_next_work_upper_limit_actual) BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_negative_target) { - const auto consensus = CreateChainParams(CBaseChainParams::MAIN)->GetConsensus(); + const auto consensus = CreateChainParams(*m_node.args, CBaseChainParams::MAIN)->GetConsensus(); uint256 hash; unsigned int nBits; nBits = UintToArith256(consensus.powLimit).GetCompact(true); @@ -71,7 +71,7 @@ BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_negative_target) BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_overflow_target) { - const auto consensus = CreateChainParams(CBaseChainParams::MAIN)->GetConsensus(); + const auto consensus = CreateChainParams(*m_node.args, CBaseChainParams::MAIN)->GetConsensus(); uint256 hash; unsigned int nBits = ~0x00800000; hash.SetHex("0x1"); @@ -80,7 +80,7 @@ BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_overflow_target) BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_too_easy_target) { - const auto consensus = CreateChainParams(CBaseChainParams::MAIN)->GetConsensus(); + const auto consensus = CreateChainParams(*m_node.args, CBaseChainParams::MAIN)->GetConsensus(); uint256 hash; unsigned int nBits; arith_uint256 nBits_arith = UintToArith256(consensus.powLimit); @@ -92,7 +92,7 @@ BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_too_easy_target) BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_biger_hash_than_target) { - const auto consensus = CreateChainParams(CBaseChainParams::MAIN)->GetConsensus(); + const auto consensus = CreateChainParams(*m_node.args, CBaseChainParams::MAIN)->GetConsensus(); uint256 hash; unsigned int nBits; arith_uint256 hash_arith = UintToArith256(consensus.powLimit); @@ -104,7 +104,7 @@ BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_biger_hash_than_target) BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_zero_target) { - const auto consensus = CreateChainParams(CBaseChainParams::MAIN)->GetConsensus(); + const auto consensus = CreateChainParams(*m_node.args, CBaseChainParams::MAIN)->GetConsensus(); uint256 hash; unsigned int nBits; arith_uint256 hash_arith{0}; @@ -115,7 +115,7 @@ BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_zero_target) BOOST_AUTO_TEST_CASE(GetBlockProofEquivalentTime_test) { - const auto chainParams = CreateChainParams(CBaseChainParams::MAIN); + const auto chainParams = CreateChainParams(*m_node.args, CBaseChainParams::MAIN); std::vector blocks(10000); for (int i = 0; i < 10000; i++) { blocks[i].pprev = i ? &blocks[i - 1] : nullptr; @@ -135,9 +135,9 @@ BOOST_AUTO_TEST_CASE(GetBlockProofEquivalentTime_test) } } -void sanity_check_chainparams(std::string chainName) +void sanity_check_chainparams(const ArgsManager& args, std::string chainName) { - const auto chainParams = CreateChainParams(chainName); + const auto chainParams = CreateChainParams(args, chainName); const auto consensus = chainParams->GetConsensus(); // hash genesis is correct @@ -164,22 +164,22 @@ void sanity_check_chainparams(std::string chainName) BOOST_AUTO_TEST_CASE(ChainParams_MAIN_sanity) { - sanity_check_chainparams(CBaseChainParams::MAIN); + sanity_check_chainparams(*m_node.args, CBaseChainParams::MAIN); } BOOST_AUTO_TEST_CASE(ChainParams_REGTEST_sanity) { - sanity_check_chainparams(CBaseChainParams::REGTEST); + sanity_check_chainparams(*m_node.args, CBaseChainParams::REGTEST); } BOOST_AUTO_TEST_CASE(ChainParams_TESTNET_sanity) { - sanity_check_chainparams(CBaseChainParams::TESTNET); + sanity_check_chainparams(*m_node.args, CBaseChainParams::TESTNET); } BOOST_AUTO_TEST_CASE(ChainParams_SIGNET_sanity) { - sanity_check_chainparams(CBaseChainParams::SIGNET); + sanity_check_chainparams(*m_node.args, CBaseChainParams::SIGNET); } BOOST_AUTO_TEST_SUITE_END() diff --git a/src/test/validation_tests.cpp b/src/test/validation_tests.cpp index 3b961db52d..aff7b7ca3e 100644 --- a/src/test/validation_tests.cpp +++ b/src/test/validation_tests.cpp @@ -39,7 +39,7 @@ static void TestBlockSubsidyHalvings(int nSubsidyHalvingInterval) BOOST_AUTO_TEST_CASE(block_subsidy_test) { - const auto chainParams = CreateChainParams(CBaseChainParams::MAIN); + const auto chainParams = CreateChainParams(*m_node.args, CBaseChainParams::MAIN); TestBlockSubsidyHalvings(chainParams->GetConsensus()); // As in main TestBlockSubsidyHalvings(150); // As in regtest TestBlockSubsidyHalvings(1000); // Just another interval @@ -47,7 +47,7 @@ BOOST_AUTO_TEST_CASE(block_subsidy_test) BOOST_AUTO_TEST_CASE(subsidy_limit_test) { - const auto chainParams = CreateChainParams(CBaseChainParams::MAIN); + const auto chainParams = CreateChainParams(*m_node.args, CBaseChainParams::MAIN); CAmount nSum = 0; for (int nHeight = 0; nHeight < 14000000; nHeight += 1000) { CAmount nSubsidy = GetBlockSubsidy(nHeight, chainParams->GetConsensus()); diff --git a/src/test/versionbits_tests.cpp b/src/test/versionbits_tests.cpp index 11c6bdad91..50444f7bbe 100644 --- a/src/test/versionbits_tests.cpp +++ b/src/test/versionbits_tests.cpp @@ -223,7 +223,7 @@ BOOST_AUTO_TEST_CASE(versionbits_test) } // Sanity checks of version bit deployments - const auto chainParams = CreateChainParams(CBaseChainParams::MAIN); + const auto chainParams = CreateChainParams(*m_node.args, CBaseChainParams::MAIN); const Consensus::Params &mainnetParams = chainParams->GetConsensus(); for (int i=0; i<(int) Consensus::MAX_VERSION_BITS_DEPLOYMENTS; i++) { uint32_t bitmask = VersionBitsMask(mainnetParams, static_cast(i)); @@ -250,7 +250,7 @@ BOOST_AUTO_TEST_CASE(versionbits_computeblockversion) { // Check that ComputeBlockVersion will set the appropriate bit correctly // on mainnet. - const auto chainParams = CreateChainParams(CBaseChainParams::MAIN); + const auto chainParams = CreateChainParams(*m_node.args, CBaseChainParams::MAIN); const Consensus::Params &mainnetParams = chainParams->GetConsensus(); // Use the TESTDUMMY deployment for testing purposes. From fa29b5ae666bbb4c19188f0dcf8a1ba738aac624 Mon Sep 17 00:00:00 2001 From: MarcoFalke Date: Wed, 23 Sep 2020 16:17:50 +0200 Subject: [PATCH 2/2] test: Add signet witness commitment section parse tests --- src/test/validation_tests.cpp | 62 +++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/src/test/validation_tests.cpp b/src/test/validation_tests.cpp index aff7b7ca3e..a9ece50136 100644 --- a/src/test/validation_tests.cpp +++ b/src/test/validation_tests.cpp @@ -4,6 +4,7 @@ #include #include +#include #include #include @@ -58,6 +59,67 @@ BOOST_AUTO_TEST_CASE(subsidy_limit_test) BOOST_CHECK_EQUAL(nSum, CAmount{2099999997690000}); } +BOOST_AUTO_TEST_CASE(signet_parse_tests) +{ + ArgsManager signet_argsman; + signet_argsman.ForceSetArg("-signetchallenge", "51"); // set challenge to OP_TRUE + const auto signet_params = CreateChainParams(signet_argsman, CBaseChainParams::SIGNET); + CBlock block; + BOOST_CHECK(signet_params->GetConsensus().signet_challenge == std::vector{{OP_TRUE}}); + CScript challenge{OP_TRUE}; + + // empty block is invalid + BOOST_CHECK(!SignetTxs::Create(block, challenge)); + BOOST_CHECK(!CheckSignetBlockSolution(block, signet_params->GetConsensus())); + + // no witness commitment + CMutableTransaction cb; + cb.vout.emplace_back(0, CScript{}); + block.vtx.push_back(MakeTransactionRef(cb)); + block.vtx.push_back(MakeTransactionRef(cb)); // Add dummy tx to excercise merkle root code + BOOST_CHECK(!SignetTxs::Create(block, challenge)); + BOOST_CHECK(!CheckSignetBlockSolution(block, signet_params->GetConsensus())); + + // no header is treated valid + std::vector witness_commitment_section_141{0xaa, 0x21, 0xa9, 0xed}; + for (int i = 0; i < 32; ++i) { + witness_commitment_section_141.push_back(0xff); + } + cb.vout.at(0).scriptPubKey = CScript{} << OP_RETURN << witness_commitment_section_141; + block.vtx.at(0) = MakeTransactionRef(cb); + BOOST_CHECK(SignetTxs::Create(block, challenge)); + BOOST_CHECK(CheckSignetBlockSolution(block, signet_params->GetConsensus())); + + // no data after header, valid + std::vector witness_commitment_section_325{0xec, 0xc7, 0xda, 0xa2}; + cb.vout.at(0).scriptPubKey = CScript{} << OP_RETURN << witness_commitment_section_141 << witness_commitment_section_325; + block.vtx.at(0) = MakeTransactionRef(cb); + BOOST_CHECK(SignetTxs::Create(block, challenge)); + BOOST_CHECK(CheckSignetBlockSolution(block, signet_params->GetConsensus())); + + // Premature end of data, invalid + witness_commitment_section_325.push_back(0x01); + witness_commitment_section_325.push_back(0x51); + cb.vout.at(0).scriptPubKey = CScript{} << OP_RETURN << witness_commitment_section_141 << witness_commitment_section_325; + block.vtx.at(0) = MakeTransactionRef(cb); + BOOST_CHECK(!SignetTxs::Create(block, challenge)); + BOOST_CHECK(!CheckSignetBlockSolution(block, signet_params->GetConsensus())); + + // has data, valid + witness_commitment_section_325.push_back(0x00); + cb.vout.at(0).scriptPubKey = CScript{} << OP_RETURN << witness_commitment_section_141 << witness_commitment_section_325; + block.vtx.at(0) = MakeTransactionRef(cb); + BOOST_CHECK(SignetTxs::Create(block, challenge)); + BOOST_CHECK(CheckSignetBlockSolution(block, signet_params->GetConsensus())); + + // Extraneous data, invalid + witness_commitment_section_325.push_back(0x00); + cb.vout.at(0).scriptPubKey = CScript{} << OP_RETURN << witness_commitment_section_141 << witness_commitment_section_325; + block.vtx.at(0) = MakeTransactionRef(cb); + BOOST_CHECK(!SignetTxs::Create(block, challenge)); + BOOST_CHECK(!CheckSignetBlockSolution(block, signet_params->GetConsensus())); +} + static bool ReturnFalse() { return false; } static bool ReturnTrue() { return true; }