@@ -20,7 +20,14 @@ BOOST_AUTO_TEST_CASE(get_next_work)
2020 pindexLast.nHeight = 32255 ;
2121 pindexLast.nTime = 1262152739 ; // Block #32255
2222 pindexLast.nBits = 0x1d00ffff ;
23- BOOST_CHECK_EQUAL (CalculateNextWorkRequired (&pindexLast, nLastRetargetTime, chainParams->GetConsensus ()), 0x1d00d86aU );
23+
24+ // Here (and below): expected_nbits is calculated in
25+ // CalculateNextWorkRequired(); redoing the calculation here would be just
26+ // reimplementing the same code that is written in pow.cpp. Rather than
27+ // copy that code, we just hardcode the expected result.
28+ unsigned int expected_nbits = 0x1d00d86aU ;
29+ BOOST_CHECK_EQUAL (CalculateNextWorkRequired (&pindexLast, nLastRetargetTime, chainParams->GetConsensus ()), expected_nbits);
30+ BOOST_CHECK (PermittedDifficultyTransition (chainParams->GetConsensus (), pindexLast.nHeight +1 , pindexLast.nBits , expected_nbits));
2431}
2532
2633/* Test the constraint on the upper bound for next work */
@@ -32,7 +39,9 @@ BOOST_AUTO_TEST_CASE(get_next_work_pow_limit)
3239 pindexLast.nHeight = 2015 ;
3340 pindexLast.nTime = 1233061996 ; // Block #2015
3441 pindexLast.nBits = 0x1d00ffff ;
35- BOOST_CHECK_EQUAL (CalculateNextWorkRequired (&pindexLast, nLastRetargetTime, chainParams->GetConsensus ()), 0x1d00ffffU );
42+ unsigned int expected_nbits = 0x1d00ffffU ;
43+ BOOST_CHECK_EQUAL (CalculateNextWorkRequired (&pindexLast, nLastRetargetTime, chainParams->GetConsensus ()), expected_nbits);
44+ BOOST_CHECK (PermittedDifficultyTransition (chainParams->GetConsensus (), pindexLast.nHeight +1 , pindexLast.nBits , expected_nbits));
3645}
3746
3847/* Test the constraint on the lower bound for actual time taken */
@@ -44,7 +53,12 @@ BOOST_AUTO_TEST_CASE(get_next_work_lower_limit_actual)
4453 pindexLast.nHeight = 68543 ;
4554 pindexLast.nTime = 1279297671 ; // Block #68543
4655 pindexLast.nBits = 0x1c05a3f4 ;
47- BOOST_CHECK_EQUAL (CalculateNextWorkRequired (&pindexLast, nLastRetargetTime, chainParams->GetConsensus ()), 0x1c0168fdU );
56+ unsigned int expected_nbits = 0x1c0168fdU ;
57+ BOOST_CHECK_EQUAL (CalculateNextWorkRequired (&pindexLast, nLastRetargetTime, chainParams->GetConsensus ()), expected_nbits);
58+ BOOST_CHECK (PermittedDifficultyTransition (chainParams->GetConsensus (), pindexLast.nHeight +1 , pindexLast.nBits , expected_nbits));
59+ // Test that reducing nbits further would not be a PermittedDifficultyTransition.
60+ unsigned int invalid_nbits = expected_nbits-1 ;
61+ BOOST_CHECK (!PermittedDifficultyTransition (chainParams->GetConsensus (), pindexLast.nHeight +1 , pindexLast.nBits , invalid_nbits));
4862}
4963
5064/* Test the constraint on the upper bound for actual time taken */
@@ -56,7 +70,12 @@ BOOST_AUTO_TEST_CASE(get_next_work_upper_limit_actual)
5670 pindexLast.nHeight = 46367 ;
5771 pindexLast.nTime = 1269211443 ; // Block #46367
5872 pindexLast.nBits = 0x1c387f6f ;
59- BOOST_CHECK_EQUAL (CalculateNextWorkRequired (&pindexLast, nLastRetargetTime, chainParams->GetConsensus ()), 0x1d00e1fdU );
73+ unsigned int expected_nbits = 0x1d00e1fdU ;
74+ BOOST_CHECK_EQUAL (CalculateNextWorkRequired (&pindexLast, nLastRetargetTime, chainParams->GetConsensus ()), expected_nbits);
75+ BOOST_CHECK (PermittedDifficultyTransition (chainParams->GetConsensus (), pindexLast.nHeight +1 , pindexLast.nBits , expected_nbits));
76+ // Test that increasing nbits further would not be a PermittedDifficultyTransition.
77+ unsigned int invalid_nbits = expected_nbits+1 ;
78+ BOOST_CHECK (!PermittedDifficultyTransition (chainParams->GetConsensus (), pindexLast.nHeight +1 , pindexLast.nBits , invalid_nbits));
6079}
6180
6281BOOST_AUTO_TEST_CASE (CheckProofOfWork_test_negative_target)
0 commit comments