Skip to content

Commit bd1ac2e

Browse files
committed
Update assertions
1 parent a8ddc21 commit bd1ac2e

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

interface/src/warmup_cooldown_allowance.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ fn rate_limited_stake_change(
9696
mod test {
9797
#[allow(deprecated)]
9898
use crate::state::{DEFAULT_WARMUP_COOLDOWN_RATE, NEW_WARMUP_COOLDOWN_RATE};
99-
use {super::*, crate::ulp::max_ulp_tolerance, proptest::prelude::*};
99+
use {super::*, crate::ulp::max_ulp_tolerance, proptest::prelude::*, std::ops::Div};
100100

101101
// === Rate selector ===
102102

@@ -207,7 +207,7 @@ mod test {
207207
..Default::default()
208208
};
209209

210-
let result = calculate_activation_allowance(
210+
let actual_result = calculate_activation_allowance(
211211
100,
212212
account_portion,
213213
&prev,
@@ -228,14 +228,25 @@ mod test {
228228

229229
// With saturation fix:
230230
// Numerator saturates to u128::MAX (≈ 3.4e38)
231+
let numerator = (account_portion as u128)
232+
.saturating_mul(supply_lamports as u128)
233+
.saturating_mul(rate_bps as u128);
234+
assert_eq!(numerator, u128::MAX);
235+
231236
// Denominator = 4e17 * 10,000 = 4e21
232-
// Result = 3.4e38 / 4e21 ≈ 8.5e16 (85M SOL)
237+
let denominator = (supply_lamports as u128).saturating_mul(BASIS_POINTS_PER_UNIT as u128);
238+
assert_eq!(denominator, 4_000_000_000_000_000_000_000);
239+
240+
// Result = u128::MAX / 4e21 ≈ 8.5e16 (~85M SOL)
233241
// 85M is ~21.25% of the stake (fail-safe)
234242
// If we allowed unlocking the full account portion it would have been 100% (fail-open)
235-
assert!(result < account_portion);
243+
let expected_result = numerator.div(denominator).min(account_portion as u128) as u64;
244+
assert_eq!(expected_result, 85_070_591_730_234_615);
236245

237-
// Assert result is in a reasonable throttling range
238-
assert!(result > 0 && result <= ideal_allowance);
246+
// Assert actual result is expected
247+
assert_eq!(actual_result, expected_result);
248+
assert!(actual_result < account_portion);
249+
assert!(actual_result <= ideal_allowance);
239250
}
240251

241252
// === Cooldown allowance ===
@@ -411,8 +422,10 @@ mod test {
411422
prop_assert_eq!(integer_math_result, 0);
412423
prop_assert_eq!(float_math_result, 0);
413424
} else if would_overflow {
414-
// In the u128 overflow region, the `f64` implementation is guaranteed to be imprecise.
415-
// We only assert that the result does not exceed the account's balance
425+
// In the overflow path, the numerator is `u128::MAX` and is divided then clamped to
426+
// `account_portion`. This math often results in less than `account_portion`, but
427+
// never should exceed it. It may be equal in the case the denominator is small and
428+
// post-division result gets clamped.
416429
prop_assert!(integer_math_result <= account_portion);
417430
} else {
418431
prop_assert!(integer_math_result <= account_portion);

0 commit comments

Comments
 (0)