Skip to content

Commit 83b892c

Browse files
apollo_consensus_orchestrator: add SNIP-35 fee_market edge case tests
1 parent 2e8ab7b commit 83b892c

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
@@ -142,3 +142,45 @@ fn test_compute_fee_proposal_custom_margin() {
142142
let proposal_down = compute_fee_proposal(Some(GasPrice(1)), GasPrice(10000), 10);
143143
assert_eq!(proposal_down, GasPrice(9900));
144144
}
145+
146+
#[test]
147+
fn test_compute_fee_actual_u128_max_does_not_overflow() {
148+
// Naive (a+b)/2 would overflow when a and b are near u128::MAX.
149+
let proposals = vec![GasPrice(u128::MAX); 10];
150+
assert_eq!(compute_fee_actual(&proposals, 10), Some(GasPrice(u128::MAX)));
151+
}
152+
153+
#[test]
154+
fn test_compute_fee_target_extreme_values_do_not_panic() {
155+
// The U256 internal arithmetic must saturate, not panic.
156+
let _ = compute_fee_target(u128::MAX, u128::MAX, 0, u128::MAX);
157+
let _ = compute_fee_target(u128::MAX, 1, 0, u128::MAX);
158+
let _ = compute_fee_target(1, u128::MAX, 0, u128::MAX);
159+
}
160+
161+
#[test]
162+
fn test_compute_fee_proposal_saturating_on_extreme_actual() {
163+
// actual near u128::MAX: saturating_mul must prevent overflow.
164+
let _ = compute_fee_proposal(Some(GasPrice(1)), GasPrice(u128::MAX), 2);
165+
let _ = compute_fee_proposal(Some(GasPrice(u128::MAX)), GasPrice(u128::MAX), 2);
166+
}
167+
168+
#[test]
169+
fn test_compute_fee_target_monotonic_in_strk_price() {
170+
// As STRK/USD rises, fewer FRI needed → fee_target monotonically decreases.
171+
let target = 3_000_000_000;
172+
let mut prev = compute_fee_target(target, 10u128.pow(17), 0, u128::MAX);
173+
for exp in 17..=21 {
174+
let curr = compute_fee_target(target, 10u128.pow(exp), 0, u128::MAX);
175+
assert!(curr.0 <= prev.0, "not monotonic: prev={} curr={}", prev.0, curr.0);
176+
prev = curr;
177+
}
178+
}
179+
180+
#[test]
181+
fn test_compute_fee_actual_lone_adversary_cannot_skew_median() {
182+
// With 9 honest values and 1 outlier, median resists the adversary.
183+
let mut window = vec![GasPrice(1_000_000); 9];
184+
window.push(GasPrice(u128::MAX / 2));
185+
assert_eq!(compute_fee_actual(&window, 10), Some(GasPrice(1_000_000)));
186+
}

0 commit comments

Comments
 (0)