Skip to content

Commit

Permalink
fix(difficulty_control): Add epsilon to function for upper-bounds on …
Browse files Browse the repository at this point in the history
…difficulty evolution

Ensure that this function never reports false negatives when given a
num_blocks input of 0.
  • Loading branch information
Sword-Smith authored and aszepieniec committed Jan 24, 2025
1 parent 05ad1be commit 847fa71
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/models/blockchain/block/difficulty_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,8 @@ pub(crate) fn max_cumulative_pow_after(
/ 16.0
+ EPSILON;
let mut max_difficulty: f64 = BigUint::from(difficulty_start).to_f64().unwrap();
let mut max_cumpow: f64 = BigUint::from(cumulative_pow_start).to_f64().unwrap();
let mut max_cumpow: f64 =
BigUint::from(cumulative_pow_start).to_f64().unwrap() * (1.0 + EPSILON);
let cap = BigUint::from(ProofOfWork::MAXIMUM).to_f64().unwrap();
for _ in 0..num_blocks {
max_cumpow += max_difficulty;
Expand Down Expand Up @@ -859,6 +860,18 @@ mod test {
let _calculated = max_cumulative_pow_after(init_cumpow, init_difficulty, 0);
}

#[proptest]
fn ensure_no_false_negatives_when_num_blocks_is_zero(
#[strategy(arb())] init_pow: ProofOfWork,
#[strategy(arb())] init_difficulty: Difficulty,
) {
let max = max_cumulative_pow_after(init_pow, init_difficulty, 0);
prop_assert!(
max >= init_pow,
"Max-calculator must upward bound pow-value for zero-blocks input"
);
}

#[proptest]
fn test_sanity_max_pow_after_prop(
#[strategy(arb())] init_difficulty: u64,
Expand Down

0 comments on commit 847fa71

Please sign in to comment.