test: some sanity checks for consensus logic

This commit is contained in:
Anthony Towns 2020-09-08 11:55:40 +10:00 committed by Karl-Johan Alm
parent e47ad375bf
commit 8258c4c007
No known key found for this signature in database
GPG Key ID: 57AF762DB3353322

View File

@ -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()