Skip to main content

get_next_work_required

Function get_next_work_required 

Source
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):

  1. Use the previous block’s bits (last block before adjustment)
  2. Calculate timespan between first and last block of adjustment period
  3. Clamp timespan to [expected_time/4, expected_time*4]
  4. Expand previous block’s bits to full U256 target
  5. Multiply target by clamped_timespan (integer)
  6. Divide by expected_time (integer)
  7. Compress result back to compact bits format
  8. 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