@@ -20,7 +20,14 @@ BOOST_AUTO_TEST_CASE(get_next_work)
20
20
pindexLast.nHeight = 32255 ;
21
21
pindexLast.nTime = 1262152739 ; // Block #32255
22
22
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));
24
31
}
25
32
26
33
/* Test the constraint on the upper bound for next work */
@@ -32,7 +39,9 @@ BOOST_AUTO_TEST_CASE(get_next_work_pow_limit)
32
39
pindexLast.nHeight = 2015 ;
33
40
pindexLast.nTime = 1233061996 ; // Block #2015
34
41
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));
36
45
}
37
46
38
47
/* 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)
44
53
pindexLast.nHeight = 68543 ;
45
54
pindexLast.nTime = 1279297671 ; // Block #68543
46
55
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));
48
62
}
49
63
50
64
/* 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)
56
70
pindexLast.nHeight = 46367 ;
57
71
pindexLast.nTime = 1269211443 ; // Block #46367
58
72
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));
60
79
}
61
80
62
81
BOOST_AUTO_TEST_CASE (CheckProofOfWork_test_negative_target)
0 commit comments