aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorWladimir J. van der Laan <laanwj@protonmail.com>2019-09-30 11:36:51 +0200
committerWladimir J. van der Laan <laanwj@protonmail.com>2019-09-30 11:42:47 +0200
commitb658ca71b363ac248135f33d24e21df205fca7b7 (patch)
tree02b83c80037e17a09bda61aa762412bc6ec102aa /src
parentdf692600c677e7170b2597f398b17551747ce889 (diff)
parent0cc7dd74e0af735dddf7e786f4ed136c382a4ad5 (diff)
Merge #16931: test: add unittests for CheckProofOfWork
0cc7dd74e0af735dddf7e786f4ed136c382a4ad5 test: add unittests for CheckProofOfWork (soroosh-sdi) Pull request description: following situations are covered: - negative target - overflow target - target easier then powLimit - invalid hash (hash > target) Signed-off-by: soroosh-sdi <soroosh.sardari@gmail.com> ACKs for top commit: promag: ACK 0cc7dd74e0af735dddf7e786f4ed136c382a4ad5, just read the code. laanwj: ACK 0cc7dd74e0af735dddf7e786f4ed136c382a4ad5 Tree-SHA512: 9f9ee952ebb211202939450aa3d61b3c2fae992dcfcab085e877507d78e02ea39a51ccacfc4852a0555f3cba07504ee132abd5cbfed75489553bee45c760bc7e
Diffstat (limited to 'src')
-rw-r--r--src/test/pow_tests.cpp54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/test/pow_tests.cpp b/src/test/pow_tests.cpp
index 1123d4202c..deac349867 100644
--- a/src/test/pow_tests.cpp
+++ b/src/test/pow_tests.cpp
@@ -60,6 +60,60 @@ BOOST_AUTO_TEST_CASE(get_next_work_upper_limit_actual)
BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, chainParams->GetConsensus()), 0x1d00e1fdU);
}
+BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_negative_target)
+{
+ const auto consensus = CreateChainParams(CBaseChainParams::MAIN)->GetConsensus();
+ uint256 hash;
+ unsigned int nBits;
+ nBits = UintToArith256(consensus.powLimit).GetCompact(true);
+ hash.SetHex("0x1");
+ BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));
+}
+
+BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_overflow_target)
+{
+ const auto consensus = CreateChainParams(CBaseChainParams::MAIN)->GetConsensus();
+ uint256 hash;
+ unsigned int nBits = ~0x00800000;
+ hash.SetHex("0x1");
+ BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));
+}
+
+BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_too_easy_target)
+{
+ const auto consensus = CreateChainParams(CBaseChainParams::MAIN)->GetConsensus();
+ uint256 hash;
+ unsigned int nBits;
+ arith_uint256 nBits_arith = UintToArith256(consensus.powLimit);
+ nBits_arith *= 2;
+ nBits = nBits_arith.GetCompact();
+ hash.SetHex("0x1");
+ BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));
+}
+
+BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_biger_hash_than_target)
+{
+ const auto consensus = CreateChainParams(CBaseChainParams::MAIN)->GetConsensus();
+ uint256 hash;
+ unsigned int nBits;
+ arith_uint256 hash_arith = UintToArith256(consensus.powLimit);
+ nBits = hash_arith.GetCompact();
+ hash_arith *= 2; // hash > nBits
+ hash = ArithToUint256(hash_arith);
+ BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));
+}
+
+BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_zero_target)
+{
+ const auto consensus = CreateChainParams(CBaseChainParams::MAIN)->GetConsensus();
+ uint256 hash;
+ unsigned int nBits;
+ arith_uint256 hash_arith{0};
+ nBits = hash_arith.GetCompact();
+ hash = ArithToUint256(hash_arith);
+ BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));
+}
+
BOOST_AUTO_TEST_CASE(GetBlockProofEquivalentTime_test)
{
const auto chainParams = CreateChainParams(CBaseChainParams::MAIN);