pub fn get_next_work_required(
_current_header: &BlockHeader,
prev_headers: &[BlockHeader],
) -> Result<u64, ConsensusError>Expand description
GetNextWorkRequired: ℋ × ℋ* → ℕ
Calculate the next work required based on difficulty adjustment using integer arithmetic.
Difficulty adjustment algorithm (BIP adjustments, including known off-by-one in interval count):
- Use the previous block’s bits (last block before adjustment)
- Calculate timespan between first and last block of adjustment period
- Clamp timespan to [expected_time/4, expected_time*4]
- Expand previous block’s bits to full U256 target
- Multiply target by clamped_timespan (integer)
- Divide by expected_time (integer)
- Compress result back to compact bits format
- Clamp to MAX_TARGET
Known Issue (Bitcoin Compatibility): This function measures time for (n-1) intervals
when given n blocks, but compares against n intervals. Standard implementation
behavior exactly for consensus compatibility. For corrected behavior, use
get_next_work_required_corrected().
For block header h and previous headers prev:
- prev[0] is the first block of the adjustment period
- prev[prev.len()-1] is the last block before the adjustment (use its bits)
Note: current_header parameter is kept for API compatibility but not used in calculation