aboutsummaryrefslogtreecommitdiff
path: root/src/test/pow_tests.cpp
diff options
context:
space:
mode:
authorAnthony Towns <aj@erisian.com.au>2020-09-08 11:55:40 +1000
committerKarl-Johan Alm <karljohan-alm@garage.co.jp>2020-09-18 10:19:43 +0900
commit8258c4c0076bb5f27efdc117a04b27fcd6dd00b2 (patch)
tree401d5ec56e7dfe28a8c7e58a3ae3a373c44c55f5 /src/test/pow_tests.cpp
parente47ad375bf17557f805bd206e789b8db78c6338a (diff)
test: some sanity checks for consensus logic
Diffstat (limited to 'src/test/pow_tests.cpp')
-rw-r--r--src/test/pow_tests.cpp47
1 files changed, 47 insertions, 0 deletions
diff --git a/src/test/pow_tests.cpp b/src/test/pow_tests.cpp
index 0f9872f434..ca49b89ad8 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()