Skip to content

Commit b658ca7

Browse files
committed
Merge #16931: test: add unittests for CheckProofOfWork
0cc7dd7 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 0cc7dd7, just read the code. laanwj: ACK 0cc7dd7 Tree-SHA512: 9f9ee952ebb211202939450aa3d61b3c2fae992dcfcab085e877507d78e02ea39a51ccacfc4852a0555f3cba07504ee132abd5cbfed75489553bee45c760bc7e
2 parents df69260 + 0cc7dd7 commit b658ca7

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

src/test/pow_tests.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,60 @@ BOOST_AUTO_TEST_CASE(get_next_work_upper_limit_actual)
6060
BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, chainParams->GetConsensus()), 0x1d00e1fdU);
6161
}
6262

63+
BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_negative_target)
64+
{
65+
const auto consensus = CreateChainParams(CBaseChainParams::MAIN)->GetConsensus();
66+
uint256 hash;
67+
unsigned int nBits;
68+
nBits = UintToArith256(consensus.powLimit).GetCompact(true);
69+
hash.SetHex("0x1");
70+
BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));
71+
}
72+
73+
BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_overflow_target)
74+
{
75+
const auto consensus = CreateChainParams(CBaseChainParams::MAIN)->GetConsensus();
76+
uint256 hash;
77+
unsigned int nBits = ~0x00800000;
78+
hash.SetHex("0x1");
79+
BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));
80+
}
81+
82+
BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_too_easy_target)
83+
{
84+
const auto consensus = CreateChainParams(CBaseChainParams::MAIN)->GetConsensus();
85+
uint256 hash;
86+
unsigned int nBits;
87+
arith_uint256 nBits_arith = UintToArith256(consensus.powLimit);
88+
nBits_arith *= 2;
89+
nBits = nBits_arith.GetCompact();
90+
hash.SetHex("0x1");
91+
BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));
92+
}
93+
94+
BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_biger_hash_than_target)
95+
{
96+
const auto consensus = CreateChainParams(CBaseChainParams::MAIN)->GetConsensus();
97+
uint256 hash;
98+
unsigned int nBits;
99+
arith_uint256 hash_arith = UintToArith256(consensus.powLimit);
100+
nBits = hash_arith.GetCompact();
101+
hash_arith *= 2; // hash > nBits
102+
hash = ArithToUint256(hash_arith);
103+
BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));
104+
}
105+
106+
BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_zero_target)
107+
{
108+
const auto consensus = CreateChainParams(CBaseChainParams::MAIN)->GetConsensus();
109+
uint256 hash;
110+
unsigned int nBits;
111+
arith_uint256 hash_arith{0};
112+
nBits = hash_arith.GetCompact();
113+
hash = ArithToUint256(hash_arith);
114+
BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));
115+
}
116+
63117
BOOST_AUTO_TEST_CASE(GetBlockProofEquivalentTime_test)
64118
{
65119
const auto chainParams = CreateChainParams(CBaseChainParams::MAIN);

0 commit comments

Comments
 (0)