Skip to content

Commit 45b8a23

Browse files
committed
feat: compute min max fee only once
1 parent 02e456b commit 45b8a23

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

crates/batcher/src/lib.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ pub struct Batcher {
9797
disabled_verifiers: Mutex<U256>,
9898
aggregator_fee_percentage_multiplier: u128,
9999
aggregator_gas_cost: u128,
100-
latest_block_gas_price: RwLock<U256>,
100+
current_min_max_fee: RwLock<U256>,
101101
amount_of_proofs_for_min_max_fee: usize,
102102
min_bump_percentage: U256,
103103
pub metrics: metrics::BatcherMetrics,
@@ -281,7 +281,7 @@ impl Batcher {
281281
posting_batch: Mutex::new(false),
282282
batch_state: Mutex::new(batch_state),
283283
disabled_verifiers: Mutex::new(disabled_verifiers),
284-
latest_block_gas_price: RwLock::new(U256::zero()),
284+
current_min_max_fee: RwLock::new(U256::zero()),
285285
metrics,
286286
telemetry,
287287
}
@@ -1252,12 +1252,9 @@ impl Batcher {
12521252
let last_uploaded_batch_block_lock = self.last_uploaded_batch_block.lock().await;
12531253

12541254
if current_batch_len < 1 {
1255-
let min_max_fee = self.get_min_max_fee().await;
12561255
info!(
1257-
"Current batch has {} proofs, min max fee {} for a batch of {}. Waiting for more proofs...",
1258-
current_batch_len,
1259-
min_max_fee,
1260-
self.amount_of_proofs_for_min_max_fee
1256+
"Current batch has {} proofs. Waiting for more proofs...",
1257+
current_batch_len
12611258
);
12621259
return None;
12631260
}
@@ -1526,9 +1523,20 @@ impl Batcher {
15261523
tokio::join!(gas_price_future, disabled_verifiers_future);
15271524

15281525
let gas_price = gas_price.map_err(|_| BatcherError::GasPriceError)?;
1526+
1527+
// compute the new min max fee
1528+
let min_max_fee = aligned_sdk::verification_layer::compute_fee_per_proof_formula(
1529+
self.amount_of_proofs_for_min_max_fee,
1530+
gas_price,
1531+
);
15291532
// Acquire a write lock to update the latest gas price.
15301533
// The lock is dropped immediately after this assignment completes.
1531-
*self.latest_block_gas_price.write().await = gas_price;
1534+
*self.current_min_max_fee.write().await = min_max_fee;
1535+
info!(
1536+
"Updated min-max fee: {} ETH per proof (batch size: {})",
1537+
ethers::utils::format_ether(min_max_fee),
1538+
self.amount_of_proofs_for_min_max_fee
1539+
);
15321540

15331541
{
15341542
let new_disable_verifiers = disable_verifiers
@@ -1939,14 +1947,6 @@ impl Batcher {
19391947
+ BATCHER_SUBMISSION_BASE_GAS_COST
19401948
}
19411949

1942-
async fn get_min_max_fee(&self) -> U256 {
1943-
let gas_price = *self.latest_block_gas_price.read().await;
1944-
aligned_sdk::verification_layer::compute_fee_per_proof_formula(
1945-
self.amount_of_proofs_for_min_max_fee,
1946-
gas_price,
1947-
)
1948-
}
1949-
19501950
/// Checks if the message signature is valid
19511951
/// and returns the address if its.
19521952
/// If not, returns false, logs the error,
@@ -2058,8 +2058,8 @@ impl Batcher {
20582058
}
20592059

20602060
async fn msg_covers_minimum_max_fee(&self, msg_max_fee: U256) -> bool {
2061-
let min_max_fee_per_proof = self.get_min_max_fee().await;
2062-
msg_max_fee >= min_max_fee_per_proof
2061+
let min_max_fee_per_proof = self.current_min_max_fee.read().await;
2062+
msg_max_fee >= *min_max_fee_per_proof
20632063
}
20642064

20652065
/// Checks if the user's balance is unlocked

0 commit comments

Comments
 (0)