MCPcopy Create free account
hub / github.com/BitVM/BitVM / calculate_new_difficulty

Function calculate_new_difficulty

header-chain/src/header_chain.rs:322–343  ·  view source on GitHub ↗
(
    epoch_start_time: u32,
    last_timestamp: u32,
    current_target: u32,
)

Source from the content-addressed store, hash-verified

320}
321
322fn calculate_new_difficulty(
323 epoch_start_time: u32,
324 last_timestamp: u32,
325 current_target: u32,
326) -> [u8; 32] {
327 let mut actual_timespan = last_timestamp - epoch_start_time;
328 if actual_timespan < EXPECTED_EPOCH_TIMESPAN / 4 {
329 actual_timespan = EXPECTED_EPOCH_TIMESPAN / 4;
330 } else if actual_timespan > EXPECTED_EPOCH_TIMESPAN * 4 {
331 actual_timespan = EXPECTED_EPOCH_TIMESPAN * 4;
332 }
333
334 let new_target_bytes = bits_to_target(current_target);
335 let mut new_target = U256::from_be_bytes(new_target_bytes)
336 .wrapping_mul(&U256::from(actual_timespan))
337 .wrapping_div(&U256::from(EXPECTED_EPOCH_TIMESPAN));
338
339 if new_target > NETWORK_CONSTANTS.max_target {
340 new_target = NETWORK_CONSTANTS.max_target;
341 }
342 new_target.to_be_bytes()
343}
344
345fn check_hash_valid(hash: &[u8; 32], target_bytes: &[u8; 32]) {
346 for i in 0..32 {

Callers 2

apply_blocksMethod · 0.85

Calls 1

bits_to_targetFunction · 0.85

Tested by 1