MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / PermittedDifficultyTransition

Function PermittedDifficultyTransition

src/pow.cpp:89–136  ·  view source on GitHub ↗

Check that on difficulty adjustments, the new difficulty does not increase or decrease beyond the permitted limits.

Source from the content-addressed store, hash-verified

87// Check that on difficulty adjustments, the new difficulty does not increase
88// or decrease beyond the permitted limits.
89bool PermittedDifficultyTransition(const Consensus::Params& params, int64_t height, uint32_t old_nbits, uint32_t new_nbits)
90{
91 if (params.fPowAllowMinDifficultyBlocks) return true;
92
93 if (height % params.DifficultyAdjustmentInterval() == 0) {
94 int64_t smallest_timespan = params.nPowTargetTimespan/4;
95 int64_t largest_timespan = params.nPowTargetTimespan*4;
96
97 const arith_uint256 pow_limit = UintToArith256(params.powLimit);
98 arith_uint256 observed_new_target;
99 observed_new_target.SetCompact(new_nbits);
100
101 // Calculate the largest difficulty value possible:
102 arith_uint256 largest_difficulty_target;
103 largest_difficulty_target.SetCompact(old_nbits);
104 largest_difficulty_target *= largest_timespan;
105 largest_difficulty_target /= params.nPowTargetTimespan;
106
107 if (largest_difficulty_target > pow_limit) {
108 largest_difficulty_target = pow_limit;
109 }
110
111 // Round and then compare this new calculated value to what is
112 // observed.
113 arith_uint256 maximum_new_target;
114 maximum_new_target.SetCompact(largest_difficulty_target.GetCompact());
115 if (maximum_new_target < observed_new_target) return false;
116
117 // Calculate the smallest difficulty value possible:
118 arith_uint256 smallest_difficulty_target;
119 smallest_difficulty_target.SetCompact(old_nbits);
120 smallest_difficulty_target *= smallest_timespan;
121 smallest_difficulty_target /= params.nPowTargetTimespan;
122
123 if (smallest_difficulty_target > pow_limit) {
124 smallest_difficulty_target = pow_limit;
125 }
126
127 // Round and then compare this new calculated value to what is
128 // observed.
129 arith_uint256 minimum_new_target;
130 minimum_new_target.SetCompact(smallest_difficulty_target.GetCompact());
131 if (minimum_new_target > observed_new_target) return false;
132 } else if (old_nbits != new_nbits) {
133 return false;
134 }
135 return true;
136}
137
138// Bypasses the actual proof of work check during fuzz testing with a simplified validation checking whether
139// the most significant bit of the last byte of the hash is set.

Callers 4

BOOST_AUTO_TEST_CASEFunction · 0.85
pow.cppFile · 0.85

Calls 3

UintToArith256Function · 0.85
GetCompactMethod · 0.80

Tested by 1

BOOST_AUTO_TEST_CASEFunction · 0.68