Skip to content

Commit 893209d

Browse files
Improve the unit test cases and their descriptions
1 parent cf219bd commit 893209d

File tree

1 file changed

+73
-44
lines changed
  • aggregation_mode/src/backend

1 file changed

+73
-44
lines changed

aggregation_mode/src/backend/mod.rs

Lines changed: 73 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -407,69 +407,98 @@ mod tests {
407407
fn test_should_send_proof_to_verify_on_chain_updated_cases() {
408408
let aggregator = make_aggregator();
409409

410-
let gas_price_20gwei: U256 = U256::from(20_000_000_000u64);
411-
412-
// With 0 seconds elapsed and 1 ETH budget, we cannot send the proof
413-
assert!(!aggregator.should_send_proof_to_verify_on_chain(
414-
Duration::from_secs(0),
415-
1.0,
416-
gas_price_20gwei,
417-
));
418-
419-
// After 24 hours and 1 ETH monthly budget, we can send the proof
410+
// The should_send_proof_to_verify_on_chain function returns true when:
411+
// gas_price * 600_000 <= (seconds_elapsed) * (monthly_eth_budget / (30 * 24 * 60 * 60))
412+
413+
// Case 1: Base case -> should return true
414+
// Monthly Budget: 0.15 ETH -> 0.5 ETH per day -> 0.000058 ETH per hour
415+
// Elapsed Time: 24 hours
416+
// Gas Price: 1 Gwei
417+
// Max to spend: 0.000058 ETH/hour * 24 hours = 0.005 ETH
418+
// Expected cost: 600,000 * 1 Gwei = 0.0006 ETH
419+
// Expected cost < Max to spend, so we can send the proof
420420
assert!(aggregator.should_send_proof_to_verify_on_chain(
421-
Duration::from_secs(24 * 3600),
422-
1.0,
423-
gas_price_20gwei,
424-
));
425-
426-
// After 24 hours and a very low budget, we cannot send the proof
427-
assert!(!aggregator.should_send_proof_to_verify_on_chain(
428-
Duration::from_secs(24 * 3600),
429-
0.00325,
430-
U256::from(0_200_000_000u64),
421+
Duration::from_secs(24 * 3600), // 24 hours
422+
0.15, // 0.15 ETH monthly budget
423+
U256::from(1_000_000_000u64), // 1 Gwei gas price
431424
));
432425

433-
// After 27 hours with the same budget as before, we can send the proof
426+
// Case 2: Slightly Increased Gas Price -> should return false
427+
// Monthly Budget: 0.15 ETH -> 0.5 ETH per day -> 0.000058 ETH per hour
428+
// Elapsed Time: 24 hours
429+
// Gas Price: 10 Gwei
430+
// Max to spend: 0.000058 ETH/hour * 24 hours = 0.005 ETH
431+
// Expected cost: 600,000 * 8 Gwei = 0.0048 ETH
432+
// Expected cost < Max to spend, so we can send the proof
434433
assert!(aggregator.should_send_proof_to_verify_on_chain(
435-
Duration::from_secs(27 * 3600),
436-
0.00325,
437-
U256::from(0_200_000_000u64),
434+
Duration::from_secs(24 * 3600), // 24 hours
435+
0.15, // 0.15 ETH monthly budget
436+
U256::from(8_000_000_000u64), // 10 Gwei gas price
438437
));
439438

440-
// After 30 days but with a very low budget, we cannot send the proof
439+
// Case 3: Increased Gas Price -> should return false
440+
// Monthly Budget: 0.15 ETH -> 0.5 ETH per day -> 0.000058 ETH per hour
441+
// Elapsed Time: 24 hours
442+
// Gas Price: 10 Gwei
443+
// Max to spend: 0.000058 ETH/hour * 24 hours = 0.001392 ETH
444+
// Expected cost: 600,000 * 10 Gwei = 0.006 ETH
445+
// Expected cost > Max to spend, so we cannot send the proof
441446
assert!(!aggregator.should_send_proof_to_verify_on_chain(
442-
Duration::from_secs(30 * 24 * 3600),
443-
0.001,
444-
gas_price_20gwei,
447+
Duration::from_secs(24 * 3600), // 24 hours
448+
0.15, // 0.15 ETH monthly budget
449+
U256::from(10_000_000_000u64), // 10 Gwei gas price
445450
));
446451

447-
// After 15 days, a moderate budget and a high gas price, we can still send the proof
452+
// Case 4: Slightly Reduced Time Elapsed -> should return true
453+
// Monthly Budget: 0.15 ETH -> 0.5 ETH per day -> 0.000000058 ETH per hour
454+
// Elapsed Time: 2 hours
455+
// Gas Price: 1 Gwei
456+
// Max to spend: 0.000000058 ETH/hour * 3 hours = 0.000625 ETH
457+
// Expected cost: 600,000 * 1 Gwei = 0.0006 ETH
458+
// Expected cost < Max to spend, so we can send the proof
448459
assert!(aggregator.should_send_proof_to_verify_on_chain(
449-
Duration::from_secs(15 * 24 * 3600),
450-
10.0,
451-
U256::from(2_000_000_000_000u64),
460+
Duration::from_secs(3 * 3600), // 3 hours
461+
0.15, // 0.15 ETH monthly budget
462+
U256::from(1_000_000_000u64), // 1 Gwei gas price
452463
));
453464

454-
// After 30 days and a medium budget, we cannot send the proof
465+
// Case 5: Reduced Time Elapsed -> should return false
466+
// Monthly Budget: 0.15 ETH -> 0.5 ETH per day -> 0.000000058 ETH per hour
467+
// Elapsed Time: 1.2 hours
468+
// Gas Price: 1 Gwei
469+
// Max to spend: 0.000000058 ETH/hour * 1.2 hours = 0.00025 ETH
470+
// Expected cost: 600,000 * 1 Gwei = 0.0006 ETH
471+
// Expected cost > Max to spend, so we cannot send the proof
455472
assert!(!aggregator.should_send_proof_to_verify_on_chain(
456-
Duration::from_secs(30 * 24 * 3600),
457-
0.012,
458-
gas_price_20gwei,
473+
Duration::from_secs_f64(1.2 * 3600.0), // 1.2 hours
474+
0.15, // 0.15 ETH monthly budget
475+
U256::from(1_000_000_000u64), // 1 Gwei gas price
459476
));
460477

461-
// After 2 days and a reasonable budget, we can send the proof
478+
// Case 6: Slightly Reduced Monthly Budget -> should return true
479+
// Monthly Budget: 0.1 ETH -> 0.0033 ETH per day -> 0.000000038 ETH per hour
480+
// Elapsed Time: 24 hours
481+
// Gas Price: 1 Gwei
482+
// Max to spend: 0.000000038 ETH/hour * 24 hours = 0.0032832 ETH
483+
// Expected cost: 600,000 * 1 Gwei = 0.0006 ETH
484+
// Expected cost < Max to spend, so we can send the proof
462485
assert!(aggregator.should_send_proof_to_verify_on_chain(
463-
Duration::from_secs(2 * 24 * 3600),
464-
5.0,
465-
gas_price_20gwei,
486+
Duration::from_secs(24 * 3600), // 24 hours
487+
0.1, // 0.1 ETH monthly budget
488+
U256::from(1_000_000_000u64), // 1 Gwei gas price
466489
));
467490

468-
// After 10 days and a medium budget with a very high gas price, we cannot send the proof
491+
// Case 7: Decreased Monthly Budget -> should return false
492+
// Monthly Budget: 0.01 ETH -> 0.00033 ETH per day -> 0.0000000038 ETH per hour
493+
// Elapsed Time: 24 hours
494+
// Gas Price: 1 Gwei
495+
// Max to spend: 0.0000000038 ETH/hour * 24 hours = 0.00032832 ETH
496+
// Expected cost: 600,000 * 1 Gwei = 0.0006 ETH
497+
// Expected cost > Max to spend, so we cannot send the proof
469498
assert!(!aggregator.should_send_proof_to_verify_on_chain(
470-
Duration::from_secs(10 * 24 * 3600),
471-
2.0,
472-
U256::from(100_000_000_000_000u64),
499+
Duration::from_secs(24 * 3600), // 24 hours
500+
0.01, // 0.01 ETH monthly budget
501+
U256::from(1_000_000_000u64), // 1 Gwei gas price
473502
));
474503
}
475504
}

0 commit comments

Comments
 (0)