fn compute_next_difficulty(
    network: &Network,
    store: &impl HeaderStore,
    prev_header: &StoredHeader,
) -> u32 {
    // Difficulty is adjusted only once in every interval of 2 weeks (2016 blocks)
    // If an interval boundary is not reached, then previous difficulty target is
    // returned Regtest network doesn't adjust PoW difficult levels. For
    // regtest, simply return the previous difficulty target

    if (prev_header.height + 1) % DIFFICULTY_ADJUSTMENT_INTERVAL != 0 || no_pow_retargeting(network)
    {
        return prev_header.header.bits;
    }

    // Computing the last header with height multiple of 2016
    let mut current_header = prev_header;
    for _i in 0..(DIFFICULTY_ADJUSTMENT_INTERVAL - 1) {
        if let Some(h) = store.get_header(&current_header.header.prev_blockhash) {
            current_header = h;
        }
    }
    // last_adjustment_header is the last header with height multiple of 2016
    let last_adjustment_header = current_header;
    let last_adjustment_time = last_adjustment_header.header.time;

    // Computing the time interval between the last adjustment header time and
    // current time. The expected value actual_interval is 2 weeks assuming
    // the expected block time is 10 mins. But most of the time, the
    // actual_interval will deviate slightly from 2 weeks. Our goal is to
    // readjust the difficulty target so that the expected time taken for the next
    // 2016 blocks is again 2 weeks.
    let actual_interval = prev_header.header.time - last_adjustment_time;
    let mut adjusted_interval = actual_interval as u32;

    // The target_adjustment_interval_time is 2 weeks of time expressed in seconds
    let target_adjustment_interval_time: u32 = DIFFICULTY_ADJUSTMENT_INTERVAL * TEN_MINUTES; //Number of seconds in 2 weeks

    // Adjusting the actual_interval to [0.5 week, 8 week] range in case the
    // actual_interval deviates too much from the expected 2 weeks.
    adjusted_interval = u32::max(adjusted_interval, target_adjustment_interval_time / 4);
    adjusted_interval = u32::min(adjusted_interval, target_adjustment_interval_time * 4);

    // Computing new difficulty target.
    // new difficulty target = old difficult target * (adjusted_interval /
    // 2_weeks);
    let mut target = prev_header.header.target();
    target = target.mul_u32(adjusted_interval);
    target = target / Uint256::from_u64(target_adjustment_interval_time as u64).unwrap();

    // Adjusting the newly computed difficulty target so that it doesn't exceed the
    // max_difficulty_target limit
    target = Uint256::min(target, max_target(network));

    // Converting the target (Uint256) into a 32 bit representation used by Bitcoin
    BlockHeader::compact_target_from_u256(&target)
}
/// This function returns the difficult target to be used for the current
/// header given the previous header
This function returns the difficult target to be used for the current
header given the previous header
This function returns the difficult target to be used for the current header given the previous header
fn compute_next_difficulty(
    network: &Network,
    store: &impl HeaderStore,
    prev_header: &StoredHeader,
) -> u32 {
    // Difficulty is adjusted only once in every interval of 2 weeks (2016 blocks)
    // If an interval boundary is not reached, then previous difficulty target is
    // returned Regtest network doesn't adjust PoW difficult levels. For
    // regtest, simply return the previous difficulty target
    if (prev_header.height + 1) % DIFFICULTY_ADJUSTMENT_INTERVAL != 0 || no_pow_retargeting(network)
    {
        return prev_header.header.bits;
    }
    // Computing the last header with height multiple of 2016
    let mut current_header = prev_header;
    for _i in 0..(DIFFICULTY_ADJUSTMENT_INTERVAL - 1) {
        if let Some(h) = store.get_header(&current_header.header.prev_blockhash) {
            current_header = h;
        }
    }
    // last_adjustment_header is the last header with height multiple of 2016
    let last_adjustment_header = current_header;
    let last_adjustment_time = last_adjustment_header.header.time;
    // Computing the time interval between the last adjustment header time and
    // current time. The expected value actual_interval is 2 weeks assuming
    // the expected block time is 10 mins. But most of the time, the
    // actual_interval will deviate slightly from 2 weeks. Our goal is to
    // readjust the difficulty target so that the expected time taken for the next
    // 2016 blocks is again 2 weeks.
    let actual_interval = prev_header.header.time - last_adjustment_time;
    let mut adjusted_interval = actual_interval as u32;
    // The target_adjustment_interval_time is 2 weeks of time expressed in seconds
    let target_adjustment_interval_time: u32 = DIFFICULTY_ADJUSTMENT_INTERVAL * TEN_MINUTES; //Number of seconds in 2 weeks
    // Adjusting the actual_interval to [0.5 week, 8 week] range in case the
    // actual_interval deviates too much from the expected 2 weeks.
    adjusted_interval = u32::max(adjusted_interval, target_adjustment_interval_time / 4);
    adjusted_interval = u32::min(adjusted_interval, target_adjustment_interval_time * 4);
    // Computing new difficulty target.
    // new difficulty target = old difficult target * (adjusted_interval /
    // 2_weeks);
    let mut target = prev_header.header.target();
    target = target.mul_u32(adjusted_interval);
    target = target / Uint256::from_u64(target_adjustment_interval_time as u64).unwrap();
    // Adjusting the newly computed difficulty target so that it doesn't exceed the
    // max_difficulty_target limit
    target = Uint256::min(target, max_target(network));
    // Converting the target (Uint256) into a 32 bit representation used by Bitcoin
    BlockHeader::compact_target_from_u256(&target)
}
fn compute_next_difficulty ( network : & Network , store : & impl HeaderStore , prev_header : & StoredHeader , ) -> u32 { // Difficulty is adjusted only once in every interval of 2 weeks (2016 blocks) // If an interval boundary is not reached, then previous difficulty target is // returned Regtest network doesn't adjust PoW difficult levels. For // regtest, simply return the previous difficulty target if ( prev_header . height + 1 ) % DIFFICULTY_ADJUSTMENT_INTERVAL != 0 || no_pow_retargeting ( network ) { return prev_header . header . bits ; } // Computing the last header with height multiple of 2016 let mut current_header = prev_header ; for _i in 0 .. ( DIFFICULTY_ADJUSTMENT_INTERVAL - 1 ) { if let Some ( h ) = store . get_header ( & current_header . header . prev_blockhash ) { current_header = h ; } } // last_adjustment_header is the last header with height multiple of 2016 let last_adjustment_header = current_header ; let last_adjustment_time = last_adjustment_header . header . time ; // Computing the time interval between the last adjustment header time and // current time. The expected value actual_interval is 2 weeks assuming // the expected block time is 10 mins. But most of the time, the // actual_interval will deviate slightly from 2 weeks. Our goal is to // readjust the difficulty target so that the expected time taken for the next // 2016 blocks is again 2 weeks. let actual_interval = prev_header . header . time - last_adjustment_time ; let mut adjusted_interval = actual_interval as u32 ; // The target_adjustment_interval_time is 2 weeks of time expressed in seconds let target_adjustment_interval_time : u32 = DIFFICULTY_ADJUSTMENT_INTERVAL * TEN_MINUTES ; //Number of seconds in 2 weeks // Adjusting the actual_interval to [0.5 week, 8 week] range in case the // actual_interval deviates too much from the expected 2 weeks. adjusted_interval = u32 :: max ( adjusted_interval , target_adjustment_interval_time / 4 ) ; adjusted_interval = u32 :: min ( adjusted_interval , target_adjustment_interval_time * 4 ) ; // Computing new difficulty target. // new difficulty target = old difficult target * (adjusted_interval / // 2_weeks); let mut target = prev_header . header . target ( ) ; target = target . mul_u32 ( adjusted_interval ) ; target = target / Uint256 :: from_u64 ( target_adjustment_interval_time as u64 ) . unwrap ( ) ; // Adjusting the newly computed difficulty target so that it doesn't exceed the // max_difficulty_target limit target = Uint256 :: min ( target , max_target ( network ) ) ; // Converting the target (Uint256) into a 32 bit representation used by Bitcoin BlockHeader :: compact_target_from_u256 ( & target ) }
This function returns the difficult target to be used for the current
header given the previous header
This function returns the difficult target to be used for the current header given the previous header
network
&Network
store
&impl HeaderStore
prev_header
&StoredHeader
network
&Network
store
&impl HeaderStore
prev_header
&StoredHeader