Skip to content

Commit 601eeac

Browse files
apollo_consensus_orchestrator: add SNIP-35 module edge case tests
1 parent 138fc88 commit 601eeac

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

  • crates/apollo_consensus_orchestrator/src/snip35

crates/apollo_consensus_orchestrator/src/snip35/test.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,45 @@ fn test_compute_fee_proposal(
9292
) {
9393
assert_eq!(compute_fee_proposal(fee_target, fee_actual, margin_ppt), expected);
9494
}
95+
96+
#[test]
97+
fn test_compute_fee_actual_u128_max_does_not_overflow() {
98+
// Naive (a+b)/2 would overflow when a and b are near u128::MAX.
99+
let proposals = vec![GasPrice(u128::MAX); 10];
100+
assert_eq!(compute_fee_actual(&proposals, 10), Some(GasPrice(u128::MAX)));
101+
}
102+
103+
#[test]
104+
fn test_compute_fee_target_extreme_values_do_not_panic() {
105+
// The U256 internal arithmetic must saturate, not panic.
106+
let _ = compute_fee_target(u128::MAX, u128::MAX, 0, u128::MAX);
107+
let _ = compute_fee_target(u128::MAX, 1, 0, u128::MAX);
108+
let _ = compute_fee_target(1, u128::MAX, 0, u128::MAX);
109+
}
110+
111+
#[test]
112+
fn test_compute_fee_proposal_saturating_on_extreme_actual() {
113+
// actual near u128::MAX: saturating_mul must prevent overflow.
114+
let _ = compute_fee_proposal(Some(GasPrice(1)), GasPrice(u128::MAX), 2);
115+
let _ = compute_fee_proposal(Some(GasPrice(u128::MAX)), GasPrice(u128::MAX), 2);
116+
}
117+
118+
#[test]
119+
fn test_compute_fee_target_monotonic_in_strk_price() {
120+
// As STRK/USD rises, fewer FRI needed → fee_target monotonically decreases.
121+
let target = 3_000_000_000;
122+
let mut prev = compute_fee_target(target, 10u128.pow(17), 0, u128::MAX);
123+
for exp in 17..=21 {
124+
let curr = compute_fee_target(target, 10u128.pow(exp), 0, u128::MAX);
125+
assert!(curr.0 <= prev.0, "not monotonic: prev={} curr={}", prev.0, curr.0);
126+
prev = curr;
127+
}
128+
}
129+
130+
#[test]
131+
fn test_compute_fee_actual_lone_adversary_cannot_skew_median() {
132+
// With 9 honest values and 1 outlier, median resists the adversary.
133+
let mut window = vec![GasPrice(1_000_000); 9];
134+
window.push(GasPrice(u128::MAX / 2));
135+
assert_eq!(compute_fee_actual(&window, 10), Some(GasPrice(1_000_000)));
136+
}

0 commit comments

Comments
 (0)