Skip to content

Commit e873b90

Browse files
Move the should send tests to the eth module
1 parent c380944 commit e873b90

File tree

2 files changed

+108
-111
lines changed

2 files changed

+108
-111
lines changed

aggregation_mode/proof_aggregator/src/backend/eth.rs

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,111 @@ fn max_to_spend_in_wei(time_elapsed: Duration, monthly_eth_budget: f64) -> U256
3434

3535
budget_available_per_second_in_wei * elapsed_seconds
3636
}
37+
38+
#[cfg(test)]
39+
mod tests {
40+
use super::should_send_proof_to_verify_on_chain;
41+
use alloy::primitives::U256;
42+
use std::time::Duration;
43+
44+
#[test]
45+
fn test_should_send_proof_to_verify_on_chain_updated_cases() {
46+
// The should_send_proof_to_verify_on_chain function returns true when:
47+
// gas_price * 600_000 <= (seconds_elapsed) * (monthly_eth_budget / (30 * 24 * 60 * 60))
48+
49+
const BUDGET_PER_MONTH_IN_ETH: f64 = 0.15;
50+
const ONE_DAY_SECONDS: u64 = 24 * 60 * 60;
51+
let gas_price = U256::from(1_000_000_000u64); // 1 Gwei
52+
53+
// Case 1: Base case -> should return true
54+
// Monthly Budget: 0.15 ETH -> 0.005 ETH per day -> 0.000000058 ETH per hour
55+
// Elapsed Time: 24 hours
56+
// Gas Price: 1 Gwei
57+
// Max to spend: 0.000000058 ETH/hour * 24 hours = 0.005 ETH
58+
// Expected cost: 600,000 * 1 Gwei = 0.0006 ETH
59+
// Expected cost < Max to spend, so we can send the proof
60+
assert!(should_send_proof_to_verify_on_chain(
61+
Duration::from_secs(ONE_DAY_SECONDS), // 24 hours
62+
BUDGET_PER_MONTH_IN_ETH, // 0.15 ETH monthly budget
63+
gas_price, // 1 Gwei gas price
64+
));
65+
66+
// Case 2: Slightly Increased Gas Price -> should return false
67+
// Monthly Budget: 0.15 ETH -> 0.005 ETH per day -> 0.000000058 ETH per hour
68+
// Elapsed Time: 24 hours
69+
// Gas Price: 8 Gwei
70+
// Max to spend: 0.000000058 ETH/hour * 24 hours = 0.005 ETH
71+
// Expected cost: 600,000 * 8 Gwei = 0.0048 ETH
72+
// Expected cost < Max to spend, so we can send the proof
73+
assert!(should_send_proof_to_verify_on_chain(
74+
Duration::from_secs(ONE_DAY_SECONDS), // 24 hours
75+
BUDGET_PER_MONTH_IN_ETH, // 0.15 ETH monthly budget
76+
U256::from(8_000_000_000u64), // 8 Gwei gas price
77+
));
78+
79+
// Case 3: Increased Gas Price -> should return false
80+
// Monthly Budget: 0.15 ETH -> 0.005 ETH per day -> 0.000000058 ETH per hour
81+
// Elapsed Time: 24 hours
82+
// Gas Price: 10 Gwei
83+
// Max to spend: 0.000000058 ETH/hour * 24 hours = 0.005 ETH
84+
// Expected cost: 600,000 * 10 Gwei = 0.006 ETH
85+
// Expected cost > Max to spend, so we cannot send the proof
86+
assert!(!should_send_proof_to_verify_on_chain(
87+
Duration::from_secs(ONE_DAY_SECONDS), // 24 hours
88+
BUDGET_PER_MONTH_IN_ETH, // 0.15 ETH monthly budget
89+
U256::from(10_000_000_000u64), // 10 Gwei gas price
90+
));
91+
92+
// Case 4: Slightly Reduced Time Elapsed -> should return true
93+
// Monthly Budget: 0.15 ETH -> 0.005 ETH per day -> 0.000000058 ETH per hour
94+
// Elapsed Time: 2 hours
95+
// Gas Price: 1 Gwei
96+
// Max to spend: 0.000000058 ETH/hour * 3 hours = 0.000625 ETH
97+
// Expected cost: 600,000 * 1 Gwei = 0.0006 ETH
98+
// Expected cost < Max to spend, so we can send the proof
99+
assert!(should_send_proof_to_verify_on_chain(
100+
Duration::from_secs(3 * 3600), // 3 hours
101+
BUDGET_PER_MONTH_IN_ETH, // 0.15 ETH monthly budget
102+
gas_price, // 1 Gwei gas price
103+
));
104+
105+
// Case 5: Reduced Time Elapsed -> should return false
106+
// Monthly Budget: 0.15 ETH -> 0.005 ETH per day -> 0.000000058 ETH per hour
107+
// Elapsed Time: 1.2 hours
108+
// Gas Price: 1 Gwei
109+
// Max to spend: 0.000000058 ETH/hour * 1.2 hours = 0.00025 ETH
110+
// Expected cost: 600,000 * 1 Gwei = 0.0006 ETH
111+
// Expected cost > Max to spend, so we cannot send the proof
112+
assert!(!should_send_proof_to_verify_on_chain(
113+
Duration::from_secs_f64(1.2 * 3600.0), // 1.2 hours
114+
BUDGET_PER_MONTH_IN_ETH, // 0.15 ETH monthly budget
115+
gas_price, // 1 Gwei gas price
116+
));
117+
118+
// Case 6: Slightly Reduced Monthly Budget -> should return true
119+
// Monthly Budget: 0.1 ETH -> 0.0033 ETH per day -> 0.000000038 ETH per hour
120+
// Elapsed Time: 24 hours
121+
// Gas Price: 1 Gwei
122+
// Max to spend: 0.000000038 ETH/hour * 24 hours = 0.0032832 ETH
123+
// Expected cost: 600,000 * 1 Gwei = 0.0006 ETH
124+
// Expected cost < Max to spend, so we can send the proof
125+
assert!(should_send_proof_to_verify_on_chain(
126+
Duration::from_secs(ONE_DAY_SECONDS), // 24 hours
127+
0.1, // 0.1 ETH monthly budget
128+
gas_price, // 1 Gwei gas price
129+
));
130+
131+
// Case 7: Decreased Monthly Budget -> should return false
132+
// Monthly Budget: 0.01 ETH -> 0.00033 ETH per day -> 0.0000000038 ETH per hour
133+
// Elapsed Time: 24 hours
134+
// Gas Price: 1 Gwei
135+
// Max to spend: 0.0000000038 ETH/hour * 24 hours = 0.00032832 ETH
136+
// Expected cost: 600,000 * 1 Gwei = 0.0006 ETH
137+
// Expected cost > Max to spend, so we cannot send the proof
138+
assert!(!should_send_proof_to_verify_on_chain(
139+
Duration::from_secs(ONE_DAY_SECONDS), // 24 hours
140+
0.01, // 0.01 ETH monthly budget
141+
gas_price, // 1 Gwei gas price
142+
));
143+
}
144+
}

aggregation_mode/proof_aggregator/src/backend/mod.rs

Lines changed: 0 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -462,114 +462,3 @@ impl ProofAggregator {
462462
Ok(receipt)
463463
}
464464
}
465-
466-
#[cfg(test)]
467-
mod tests {
468-
469-
use super::*;
470-
471-
use alloy::primitives::U256;
472-
use eth::should_send_proof_to_verify_on_chain;
473-
use std::time::Duration;
474-
475-
#[test]
476-
fn test_should_send_proof_to_verify_on_chain_updated_cases() {
477-
// The should_send_proof_to_verify_on_chain function returns true when:
478-
// gas_price * 600_000 <= (seconds_elapsed) * (monthly_eth_budget / (30 * 24 * 60 * 60))
479-
480-
const BUDGET_PER_MONTH_IN_ETH: f64 = 0.15;
481-
const ONE_DAY_SECONDS: u64 = 24 * 60 * 60;
482-
let gas_price = U256::from(1_000_000_000u64); // 1 Gwei
483-
484-
// Case 1: Base case -> should return true
485-
// Monthly Budget: 0.15 ETH -> 0.005 ETH per day -> 0.000000058 ETH per hour
486-
// Elapsed Time: 24 hours
487-
// Gas Price: 1 Gwei
488-
// Max to spend: 0.000000058 ETH/hour * 24 hours = 0.005 ETH
489-
// Expected cost: 600,000 * 1 Gwei = 0.0006 ETH
490-
// Expected cost < Max to spend, so we can send the proof
491-
assert!(should_send_proof_to_verify_on_chain(
492-
Duration::from_secs(ONE_DAY_SECONDS), // 24 hours
493-
BUDGET_PER_MONTH_IN_ETH, // 0.15 ETH monthly budget
494-
gas_price, // 1 Gwei gas price
495-
));
496-
497-
// Case 2: Slightly Increased Gas Price -> should return false
498-
// Monthly Budget: 0.15 ETH -> 0.005 ETH per day -> 0.000000058 ETH per hour
499-
// Elapsed Time: 24 hours
500-
// Gas Price: 8 Gwei
501-
// Max to spend: 0.000000058 ETH/hour * 24 hours = 0.005 ETH
502-
// Expected cost: 600,000 * 8 Gwei = 0.0048 ETH
503-
// Expected cost < Max to spend, so we can send the proof
504-
assert!(should_send_proof_to_verify_on_chain(
505-
Duration::from_secs(ONE_DAY_SECONDS), // 24 hours
506-
BUDGET_PER_MONTH_IN_ETH, // 0.15 ETH monthly budget
507-
U256::from(8_000_000_000u64), // 8 Gwei gas price
508-
));
509-
510-
// Case 3: Increased Gas Price -> should return false
511-
// Monthly Budget: 0.15 ETH -> 0.005 ETH per day -> 0.000000058 ETH per hour
512-
// Elapsed Time: 24 hours
513-
// Gas Price: 10 Gwei
514-
// Max to spend: 0.000000058 ETH/hour * 24 hours = 0.005 ETH
515-
// Expected cost: 600,000 * 10 Gwei = 0.006 ETH
516-
// Expected cost > Max to spend, so we cannot send the proof
517-
assert!(!should_send_proof_to_verify_on_chain(
518-
Duration::from_secs(ONE_DAY_SECONDS), // 24 hours
519-
BUDGET_PER_MONTH_IN_ETH, // 0.15 ETH monthly budget
520-
U256::from(10_000_000_000u64), // 10 Gwei gas price
521-
));
522-
523-
// Case 4: Slightly Reduced Time Elapsed -> should return true
524-
// Monthly Budget: 0.15 ETH -> 0.005 ETH per day -> 0.000000058 ETH per hour
525-
// Elapsed Time: 2 hours
526-
// Gas Price: 1 Gwei
527-
// Max to spend: 0.000000058 ETH/hour * 3 hours = 0.000625 ETH
528-
// Expected cost: 600,000 * 1 Gwei = 0.0006 ETH
529-
// Expected cost < Max to spend, so we can send the proof
530-
assert!(should_send_proof_to_verify_on_chain(
531-
Duration::from_secs(3 * 3600), // 3 hours
532-
BUDGET_PER_MONTH_IN_ETH, // 0.15 ETH monthly budget
533-
gas_price, // 1 Gwei gas price
534-
));
535-
536-
// Case 5: Reduced Time Elapsed -> should return false
537-
// Monthly Budget: 0.15 ETH -> 0.005 ETH per day -> 0.000000058 ETH per hour
538-
// Elapsed Time: 1.2 hours
539-
// Gas Price: 1 Gwei
540-
// Max to spend: 0.000000058 ETH/hour * 1.2 hours = 0.00025 ETH
541-
// Expected cost: 600,000 * 1 Gwei = 0.0006 ETH
542-
// Expected cost > Max to spend, so we cannot send the proof
543-
assert!(!should_send_proof_to_verify_on_chain(
544-
Duration::from_secs_f64(1.2 * 3600.0), // 1.2 hours
545-
BUDGET_PER_MONTH_IN_ETH, // 0.15 ETH monthly budget
546-
gas_price, // 1 Gwei gas price
547-
));
548-
549-
// Case 6: Slightly Reduced Monthly Budget -> should return true
550-
// Monthly Budget: 0.1 ETH -> 0.0033 ETH per day -> 0.000000038 ETH per hour
551-
// Elapsed Time: 24 hours
552-
// Gas Price: 1 Gwei
553-
// Max to spend: 0.000000038 ETH/hour * 24 hours = 0.0032832 ETH
554-
// Expected cost: 600,000 * 1 Gwei = 0.0006 ETH
555-
// Expected cost < Max to spend, so we can send the proof
556-
assert!(should_send_proof_to_verify_on_chain(
557-
Duration::from_secs(ONE_DAY_SECONDS), // 24 hours
558-
0.1, // 0.1 ETH monthly budget
559-
gas_price, // 1 Gwei gas price
560-
));
561-
562-
// Case 7: Decreased Monthly Budget -> should return false
563-
// Monthly Budget: 0.01 ETH -> 0.00033 ETH per day -> 0.0000000038 ETH per hour
564-
// Elapsed Time: 24 hours
565-
// Gas Price: 1 Gwei
566-
// Max to spend: 0.0000000038 ETH/hour * 24 hours = 0.00032832 ETH
567-
// Expected cost: 600,000 * 1 Gwei = 0.0006 ETH
568-
// Expected cost > Max to spend, so we cannot send the proof
569-
assert!(!should_send_proof_to_verify_on_chain(
570-
Duration::from_secs(ONE_DAY_SECONDS), // 24 hours
571-
0.01, // 0.01 ETH monthly budget
572-
gas_price, // 1 Gwei gas price
573-
));
574-
}
575-
}

0 commit comments

Comments
 (0)