-
Notifications
You must be signed in to change notification settings - Fork 396
Expand file tree
/
Copy patheth.rs
More file actions
144 lines (125 loc) · 6.76 KB
/
eth.rs
File metadata and controls
144 lines (125 loc) · 6.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
use alloy::primitives::{utils::parse_ether, U256};
use std::time::Duration;
// We assume a fixed gas cost of 300,000 for each of the 2 transactions
const ON_CHAIN_COST_IN_GAS_UNITS: u64 = 600_000u64;
/// Decides whether to send the aggregated proof to be verified on-chain based on
/// time elapsed since last submission and monthly ETH budget.
/// We make a linear function with the eth to spend this month and the time elapsed since last submission.
/// If eth to spend / elapsed time is over the linear function, we skip the submission.
pub fn should_send_proof_to_verify_on_chain(
time_elapsed: Duration,
monthly_eth_budget: f64,
network_gas_price: U256,
) -> bool {
let on_chain_cost_in_gas: U256 = U256::from(ON_CHAIN_COST_IN_GAS_UNITS);
let max_to_spend_wei = max_to_spend_in_wei(time_elapsed, monthly_eth_budget);
let expected_cost_in_wei = network_gas_price * on_chain_cost_in_gas;
expected_cost_in_wei <= max_to_spend_wei
}
fn max_to_spend_in_wei(time_elapsed: Duration, monthly_eth_budget: f64) -> U256 {
const SECONDS_PER_MONTH: u64 = 30 * 24 * 60 * 60;
// Note: this expect is safe because in case it was invalid, should have been caught at startup
let monthly_budget_in_wei = parse_ether(&monthly_eth_budget.to_string())
.expect("The monthly budget should be a non-negative value");
let elapsed_seconds = U256::from(time_elapsed.as_secs());
let budget_available_per_second_in_wei = monthly_budget_in_wei / U256::from(SECONDS_PER_MONTH);
budget_available_per_second_in_wei * elapsed_seconds
}
#[cfg(test)]
mod tests {
use super::should_send_proof_to_verify_on_chain;
use alloy::primitives::U256;
use std::time::Duration;
#[test]
fn test_should_send_proof_to_verify_on_chain_updated_cases() {
// The should_send_proof_to_verify_on_chain function returns true when:
// gas_price * 600_000 <= (seconds_elapsed) * (monthly_eth_budget / (30 * 24 * 60 * 60))
const BUDGET_PER_MONTH_IN_ETH: f64 = 0.15;
const ONE_DAY_SECONDS: u64 = 24 * 60 * 60;
let gas_price = U256::from(1_000_000_000u64); // 1 Gwei
// Case 1: Base case -> should return true
// Monthly Budget: 0.15 ETH -> 0.005 ETH per day -> 0.000000058 ETH per hour
// Elapsed Time: 24 hours
// Gas Price: 1 Gwei
// Max to spend: 0.000000058 ETH/hour * 24 hours = 0.005 ETH
// Expected cost: 600,000 * 1 Gwei = 0.0006 ETH
// Expected cost < Max to spend, so we can send the proof
assert!(should_send_proof_to_verify_on_chain(
Duration::from_secs(ONE_DAY_SECONDS), // 24 hours
BUDGET_PER_MONTH_IN_ETH, // 0.15 ETH monthly budget
gas_price, // 1 Gwei gas price
));
// Case 2: Slightly Increased Gas Price -> should return false
// Monthly Budget: 0.15 ETH -> 0.005 ETH per day -> 0.000000058 ETH per hour
// Elapsed Time: 24 hours
// Gas Price: 8 Gwei
// Max to spend: 0.000000058 ETH/hour * 24 hours = 0.005 ETH
// Expected cost: 600,000 * 8 Gwei = 0.0048 ETH
// Expected cost < Max to spend, so we can send the proof
assert!(should_send_proof_to_verify_on_chain(
Duration::from_secs(ONE_DAY_SECONDS), // 24 hours
BUDGET_PER_MONTH_IN_ETH, // 0.15 ETH monthly budget
U256::from(8_000_000_000u64), // 8 Gwei gas price
));
// Case 3: Increased Gas Price -> should return false
// Monthly Budget: 0.15 ETH -> 0.005 ETH per day -> 0.000000058 ETH per hour
// Elapsed Time: 24 hours
// Gas Price: 10 Gwei
// Max to spend: 0.000000058 ETH/hour * 24 hours = 0.005 ETH
// Expected cost: 600,000 * 10 Gwei = 0.006 ETH
// Expected cost > Max to spend, so we cannot send the proof
assert!(!should_send_proof_to_verify_on_chain(
Duration::from_secs(ONE_DAY_SECONDS), // 24 hours
BUDGET_PER_MONTH_IN_ETH, // 0.15 ETH monthly budget
U256::from(10_000_000_000u64), // 10 Gwei gas price
));
// Case 4: Slightly Reduced Time Elapsed -> should return true
// Monthly Budget: 0.15 ETH -> 0.005 ETH per day -> 0.000000058 ETH per hour
// Elapsed Time: 2 hours
// Gas Price: 1 Gwei
// Max to spend: 0.000000058 ETH/hour * 3 hours = 0.000625 ETH
// Expected cost: 600,000 * 1 Gwei = 0.0006 ETH
// Expected cost < Max to spend, so we can send the proof
assert!(should_send_proof_to_verify_on_chain(
Duration::from_secs(3 * 3600), // 3 hours
BUDGET_PER_MONTH_IN_ETH, // 0.15 ETH monthly budget
gas_price, // 1 Gwei gas price
));
// Case 5: Reduced Time Elapsed -> should return false
// Monthly Budget: 0.15 ETH -> 0.005 ETH per day -> 0.000000058 ETH per hour
// Elapsed Time: 1.2 hours
// Gas Price: 1 Gwei
// Max to spend: 0.000000058 ETH/hour * 1.2 hours = 0.00025 ETH
// Expected cost: 600,000 * 1 Gwei = 0.0006 ETH
// Expected cost > Max to spend, so we cannot send the proof
assert!(!should_send_proof_to_verify_on_chain(
Duration::from_secs_f64(1.2 * 3600.0), // 1.2 hours
BUDGET_PER_MONTH_IN_ETH, // 0.15 ETH monthly budget
gas_price, // 1 Gwei gas price
));
// Case 6: Slightly Reduced Monthly Budget -> should return true
// Monthly Budget: 0.1 ETH -> 0.0033 ETH per day -> 0.000000038 ETH per hour
// Elapsed Time: 24 hours
// Gas Price: 1 Gwei
// Max to spend: 0.000000038 ETH/hour * 24 hours = 0.0032832 ETH
// Expected cost: 600,000 * 1 Gwei = 0.0006 ETH
// Expected cost < Max to spend, so we can send the proof
assert!(should_send_proof_to_verify_on_chain(
Duration::from_secs(ONE_DAY_SECONDS), // 24 hours
0.1, // 0.1 ETH monthly budget
gas_price, // 1 Gwei gas price
));
// Case 7: Decreased Monthly Budget -> should return false
// Monthly Budget: 0.01 ETH -> 0.00033 ETH per day -> 0.0000000038 ETH per hour
// Elapsed Time: 24 hours
// Gas Price: 1 Gwei
// Max to spend: 0.0000000038 ETH/hour * 24 hours = 0.00032832 ETH
// Expected cost: 600,000 * 1 Gwei = 0.0006 ETH
// Expected cost > Max to spend, so we cannot send the proof
assert!(!should_send_proof_to_verify_on_chain(
Duration::from_secs(ONE_DAY_SECONDS), // 24 hours
0.01, // 0.01 ETH monthly budget
gas_price, // 1 Gwei gas price
));
}
}