Skip to content

Commit cc96a7e

Browse files
refactor: move the gas fees update to a separate method
1 parent e377dda commit cc96a7e

1 file changed

Lines changed: 68 additions & 35 deletions

File tree

  • aggregation_mode/proof_aggregator/src/backend

aggregation_mode/proof_aggregator/src/backend/mod.rs

Lines changed: 68 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -369,45 +369,15 @@ impl ProofAggregator {
369369
}
370370
};
371371

372-
let provider = self.proof_aggregation_service.provider();
373-
374-
// TODO: Move this to a separate method reset_gas_fees()
375372
// Increase gas price/fees for retries before filling
376373
if attempt > 0 {
377-
let multiplier = fee_multiplier.powi(attempt);
378-
379-
info!(
380-
"Retry attempt {} with increased fee ({}x)",
381-
attempt, multiplier
382-
);
383-
384-
// Obtain the current gas price if not set
385-
if tx_req.max_fee_per_gas.is_none() {
386-
let current_gas_price = provider.get_gas_price().await.map_err(|e| {
387-
RetryError::Transient(AggregatedProofSubmissionError::GasPriceError(
388-
e.to_string(),
389-
))
390-
})?;
391-
392-
let new_max_fee = (current_gas_price as f64 * multiplier) as u128;
393-
let new_priority_fee = (current_gas_price as f64 * multiplier * 0.1) as u128;
394-
395-
tx_req = tx_req
396-
.with_max_fee_per_gas(new_max_fee)
397-
.with_max_priority_fee_per_gas(new_priority_fee);
398-
} else {
399-
// If set, multiplicate the current ones
400-
if let Some(max_fee) = tx_req.max_fee_per_gas {
401-
let new_max_fee = (max_fee as f64 * multiplier) as u128;
402-
tx_req = tx_req.with_max_fee_per_gas(new_max_fee);
403-
}
404-
if let Some(priority_fee) = tx_req.max_priority_fee_per_gas {
405-
let new_priority_fee = (priority_fee as f64 * multiplier * 0.1) as u128;
406-
tx_req = tx_req.with_max_priority_fee_per_gas(new_priority_fee);
407-
}
408-
}
374+
tx_req = self
375+
.update_gas_fees(fee_multiplier, attempt, tx_req)
376+
.await?;
409377
}
410378

379+
let provider = self.proof_aggregation_service.provider();
380+
411381
let envelope = provider
412382
.fill(tx_req)
413383
.await
@@ -500,6 +470,69 @@ impl ProofAggregator {
500470
))
501471
}
502472

473+
// Updates the gas fees of a `TransactionRequest` for retry attempts by applying an exponential fee
474+
// multiplier based on the retry number. This method is intended to be used when a previous transaction
475+
// attempt was not confirmed (e.g. receipt timeout or transient failure). By increasing the gas fees
476+
// on each retry, it improves the likelihood that the transaction will be included
477+
// on the next block.
478+
//
479+
// Fee strategy:
480+
// An exponential multiplier is computed on each iteration. For example, with `fee_multiplier = 1.2`:
481+
// - `attempt = 1` → `1.2x`
482+
// - `attempt = 2` → `1.44x`
483+
// - `attempt = 3` → `1.728x`
484+
//
485+
// If the transaction request doesn't have `max_fee_per_gas` set, the current network gas price is fetched
486+
// from the provider, the max fee per gas is set to `current_gas_price * multiplier` and the max priority
487+
// fee per gas is set to `current_gas_price * multiplier * 0.1`.
488+
//
489+
// If the transaction request already contains the fee fields, the existing max fee per gas is multiplied
490+
// by `multiplier`, and the existing max priority fee per gas is multiplied by multiplier * 0.1`.
491+
async fn update_gas_fees(
492+
&self,
493+
fee_multiplier: f64,
494+
attempt: i32,
495+
tx_req: alloy::rpc::types::TransactionRequest,
496+
) -> Result<alloy::rpc::types::TransactionRequest, RetryError<AggregatedProofSubmissionError>>
497+
{
498+
let provider = self.proof_aggregation_service.provider();
499+
500+
let multiplier = fee_multiplier.powi(attempt);
501+
502+
info!(
503+
"Retry attempt {} with increased fee ({}x)",
504+
attempt, multiplier
505+
);
506+
507+
let mut current_tx_req = tx_req.clone();
508+
509+
// Obtain the current gas price if not set
510+
if tx_req.max_fee_per_gas.is_none() {
511+
let current_gas_price = provider.get_gas_price().await.map_err(|e| {
512+
RetryError::Transient(AggregatedProofSubmissionError::GasPriceError(e.to_string()))
513+
})?;
514+
515+
let new_max_fee = (current_gas_price as f64 * multiplier) as u128;
516+
let new_priority_fee = (current_gas_price as f64 * multiplier * 0.1) as u128;
517+
518+
current_tx_req = current_tx_req
519+
.with_max_fee_per_gas(new_max_fee)
520+
.with_max_priority_fee_per_gas(new_priority_fee);
521+
} else {
522+
// If set, multiplicate the current ones
523+
if let Some(max_fee) = tx_req.max_fee_per_gas {
524+
let new_max_fee = (max_fee as f64 * multiplier) as u128;
525+
current_tx_req = tx_req.clone().with_max_fee_per_gas(new_max_fee);
526+
}
527+
if let Some(priority_fee) = tx_req.max_priority_fee_per_gas {
528+
let new_priority_fee = (priority_fee as f64 * multiplier * 0.1) as u128;
529+
current_tx_req = tx_req.with_max_priority_fee_per_gas(new_priority_fee);
530+
}
531+
}
532+
533+
Ok(current_tx_req)
534+
}
535+
503536
async fn wait_until_can_submit_aggregated_proof(
504537
&self,
505538
) -> Result<(), RetryError<AggregatedProofSubmissionError>> {

0 commit comments

Comments
 (0)