Skip to content

Commit

Permalink
Calculate new diff by specific network target
Browse files Browse the repository at this point in the history
  • Loading branch information
yaziciahmet committed Dec 21, 2024
1 parent 87605de commit 58d021a
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions crates/bitcoin-da/src/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ use crate::spec::{BitcoinNetwork, BitcoinSpec};

pub const WITNESS_COMMITMENT_PREFIX: &[u8] = &[0x6a, 0x24, 0xaa, 0x21, 0xa9, 0xed];

/// The maximum target value, which corresponds to the minimum difficulty
const MAX_TARGET: U256 =
U256::from_be_hex("00000000FFFF0000000000000000000000000000000000000000000000000000");

/// An epoch should be two weeks (represented as number of seconds)
/// seconds/minute * minutes/hour * hours/day * 14 days
const EXPECTED_EPOCH_TIMESPAN: u32 = 60 * 60 * 24 * 14;
Expand Down Expand Up @@ -290,6 +286,7 @@ impl DaVerifier for BitcoinVerifier {
epoch_start_time,
block_header.time().secs() as u32,
block_header.bits(),
da_constants.max_target,
);
current_target_bits = target_to_bits(&next_target);
}
Expand Down Expand Up @@ -417,6 +414,7 @@ fn calculate_new_difficulty(
epoch_start_time: u32,
last_timestamp: u32,
current_target: u32,
max_target: U256,
) -> [u8; 32] {
// Step 1: Calculate the actual timespan of the epoch
let mut actual_timespan = last_timestamp - epoch_start_time;
Expand All @@ -431,8 +429,8 @@ fn calculate_new_difficulty(
.wrapping_mul(&U256::from(actual_timespan))
.wrapping_div(&U256::from(EXPECTED_EPOCH_TIMESPAN));
// Step 3: Clamp the new target to the maximum target
if new_target > MAX_TARGET {
new_target = MAX_TARGET;
if new_target > max_target {
new_target = max_target;
}

new_target.to_be_bytes()
Expand Down

0 comments on commit 58d021a

Please sign in to comment.