Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
3630400
Move the comments on the aggregate_and_submit_proofs_on_chain send loop
maximopalopoli Dec 17, 2025
36a8369
Initial version with retry only on proof sending
maximopalopoli Dec 18, 2025
62cb33b
Include the bump part in the retryable function
maximopalopoli Dec 18, 2025
aa9ddc5
Remove unnecessary rpc provider param from Proof aggregator
maximopalopoli Dec 18, 2025
bca9ee1
Move the helpers and retry logic to separate files
maximopalopoli Dec 18, 2025
5828919
Move the wait part to a separate function
maximopalopoli Dec 18, 2025
f89957f
Move the retryable function to the retry module
maximopalopoli Dec 18, 2025
fe32da1
Remove unnecessary derive tags
maximopalopoli Dec 18, 2025
54f6fea
Use tokio sleep instead of std sleep
maximopalopoli Dec 18, 2025
68e7c5b
Minor style changes
maximopalopoli Dec 18, 2025
5349bcc
Use permanent retry case
maximopalopoli Dec 18, 2025
5587f2d
Add logs to track the retries
maximopalopoli Dec 18, 2025
5a728d1
Move the retryables functions back to the ProofAggregatorStruct
maximopalopoli Dec 22, 2025
c380944
rename the helpers module to eth
maximopalopoli Dec 22, 2025
e873b90
Move the should send tests to the eth module
maximopalopoli Dec 22, 2025
352cce0
Remove the backon dependency
maximopalopoli Dec 23, 2025
e828d99
Minor fixes in should send test cases comments
maximopalopoli Jan 5, 2026
3600aad
Merge remote-tracking branch 'origin/staging' into feataggmode/retry-…
MarcosNicolau Jan 6, 2026
9c9a50a
Move and improve a bit the defensive comment
maximopalopoli Jan 6, 2026
846cd98
Add a warn in case the retryable function fails notifying the error
maximopalopoli Jan 6, 2026
6f092e3
Make wait_and_send_proof_to_verify_on_chain into a ProofAggregator in…
maximopalopoli Jan 6, 2026
9857933
Do the same for the wait_until_can_submit_aggregated_proof method
maximopalopoli Jan 6, 2026
f8ddb52
Move the waiting method to below the one that does both tasks to impr…
maximopalopoli Jan 6, 2026
677f419
move consts and add documentation comments to the retry module
maximopalopoli Jan 6, 2026
90a24ed
fix clippy
JuArce Jan 6, 2026
e4a7a91
use min instead of if
JuArce Jan 6, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 1 addition & 25 deletions aggregation_mode/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion aggregation_mode/proof_aggregator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ reqwest = { version = "0.12" }
ciborium = "=0.2.2"
lambdaworks-crypto = { git = "https://github.com/lambdaclass/lambdaworks.git", rev = "5f8f2cfcc8a1a22f77e8dff2d581f1166eefb80b", features = ["serde"]}
rayon = "1.10.0"
backon = "1.2.0"
sqlx = { version = "0.8", features = [ "runtime-tokio", "postgres", "uuid", "bigdecimal" ] }

# zkvms
Expand Down
144 changes: 144 additions & 0 deletions aggregation_mode/proof_aggregator/src/backend/eth.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,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 true
// 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: 3 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
));
}
}
Loading
Loading