From 8258c4c0076bb5f27efdc117a04b27fcd6dd00b2 Mon Sep 17 00:00:00 2001 From: Anthony Towns Date: Tue, 8 Sep 2020 11:55:40 +1000 Subject: [PATCH] test: some sanity checks for consensus logic --- src/test/pow_tests.cpp | 47 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src/test/pow_tests.cpp b/src/test/pow_tests.cpp index 0f9872f434b..ca49b89ad84 100644 --- a/src/test/pow_tests.cpp +++ b/src/test/pow_tests.cpp @@ -135,4 +135,51 @@ BOOST_AUTO_TEST_CASE(GetBlockProofEquivalentTime_test) } } +void sanity_check_chainparams(std::string chainName) +{ + const auto chainParams = CreateChainParams(chainName); + const auto consensus = chainParams->GetConsensus(); + + // hash genesis is correct + BOOST_CHECK_EQUAL(consensus.hashGenesisBlock, chainParams->GenesisBlock().GetHash()); + + // target timespan is an even multiple of spacing + BOOST_CHECK_EQUAL(consensus.nPowTargetTimespan % consensus.nPowTargetSpacing, 0); + + // genesis nBits is positive, doesn't overflow and is lower than powLimit + arith_uint256 pow_compact; + bool neg, over; + pow_compact.SetCompact(chainParams->GenesisBlock().nBits, &neg, &over); + BOOST_CHECK(!neg && pow_compact != 0); + BOOST_CHECK(!over); + BOOST_CHECK(UintToArith256(consensus.powLimit) >= pow_compact); + + // check max target * 4*nPowTargetTimespan doesn't overflow -- see pow.cpp:CalculateNextWorkRequired() + if (!consensus.fPowNoRetargeting) { + arith_uint256 targ_max("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"); + targ_max /= consensus.nPowTargetTimespan*4; + BOOST_CHECK(UintToArith256(consensus.powLimit) < targ_max); + } +} + +BOOST_AUTO_TEST_CASE(ChainParams_MAIN_sanity) +{ + sanity_check_chainparams(CBaseChainParams::MAIN); +} + +BOOST_AUTO_TEST_CASE(ChainParams_REGTEST_sanity) +{ + sanity_check_chainparams(CBaseChainParams::REGTEST); +} + +BOOST_AUTO_TEST_CASE(ChainParams_TESTNET_sanity) +{ + sanity_check_chainparams(CBaseChainParams::TESTNET); +} + +BOOST_AUTO_TEST_CASE(ChainParams_SIGNET_sanity) +{ + sanity_check_chainparams(CBaseChainParams::SIGNET); +} + BOOST_AUTO_TEST_SUITE_END()