Skip to content

Commit 9278ed3

Browse files
committed
Use user_mid_fee for price user minimum balance check.
1 parent b338f32 commit 9278ed3

File tree

1 file changed

+23
-16
lines changed
  • batcher/aligned-batcher/src

1 file changed

+23
-16
lines changed

batcher/aligned-batcher/src/lib.rs

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use aligned_sdk::core::constants::{
2323
ADDITIONAL_SUBMISSION_GAS_COST_PER_PROOF, AGGREGATOR_GAS_COST, CANCEL_TRANSACTION_MAX_RETRIES,
2424
CONSTANT_GAS_COST, DEFAULT_AGGREGATOR_FEE_PERCENTAGE_MULTIPLIER, DEFAULT_BACKOFF_FACTOR,
2525
DEFAULT_MAX_FEE_PER_PROOF, DEFAULT_MAX_RETRIES, DEFAULT_MIN_RETRY_DELAY,
26-
GAS_PRICE_PERCENTAGE_MULTIPLIER, MIN_FEE_PER_PROOF, PERCENTAGE_DIVIDER,
26+
GAS_PRICE_PERCENTAGE_MULTIPLIER, PERCENTAGE_DIVIDER,
2727
RESPOND_TO_TASK_FEE_LIMIT_PERCENTAGE_MULTIPLIER,
2828
};
2929
use aligned_sdk::core::types::{
@@ -672,7 +672,20 @@ impl Batcher {
672672
return Ok(());
673673
};
674674

675-
if !self.check_min_balance(proofs_in_batch + 1, user_balance) {
675+
let msg_max_fee = nonced_verification_data.max_fee;
676+
let Some(user_min_fee) = batch_state_lock.get_user_min_fee(&addr).await else {
677+
std::mem::drop(batch_state_lock);
678+
send_message(
679+
ws_conn_sink.clone(),
680+
SubmitProofResponseMessage::InvalidNonce,
681+
)
682+
.await;
683+
self.metrics.user_error(&["invalid_nonce", ""]);
684+
return Ok(());
685+
};
686+
687+
// We estimate the min balance to be the minimum balance needed to pay for the
688+
if !self.check_min_balance( user_min_fee, proofs_in_batch + 1, user_balance, msg_max_fee) {
676689
std::mem::drop(batch_state_lock);
677690
send_message(
678691
ws_conn_sink.clone(),
@@ -724,18 +737,6 @@ impl Batcher {
724737
return Ok(());
725738
}
726739

727-
let msg_max_fee = nonced_verification_data.max_fee;
728-
let Some(user_min_fee) = batch_state_lock.get_user_min_fee(&addr).await else {
729-
std::mem::drop(batch_state_lock);
730-
send_message(
731-
ws_conn_sink.clone(),
732-
SubmitProofResponseMessage::InvalidNonce,
733-
)
734-
.await;
735-
self.metrics.user_error(&["invalid_nonce", ""]);
736-
return Ok(());
737-
};
738-
739740
if msg_max_fee > user_min_fee {
740741
std::mem::drop(batch_state_lock);
741742
warn!("Invalid max fee for address {addr}, had fee {user_min_fee:?} < {msg_max_fee:?}");
@@ -778,8 +779,14 @@ impl Batcher {
778779
}
779780

780781
// Checks user has sufficient balance for paying all its the proofs in the current batch.
781-
fn check_min_balance(&self, user_proofs_in_batch: usize, user_balance: U256) -> bool {
782-
let min_balance = U256::from(user_proofs_in_batch) * U256::from(MIN_FEE_PER_PROOF);
782+
fn check_min_balance(&self, user_min_fee: U256, user_proofs_in_batch: usize, user_balance: U256, user_max_fee: U256) -> bool {
783+
// If user proof has not been submitted yet its default is U256::max_value().
784+
// In this case we check the user can pay for its proof.
785+
let mut min_fee = user_min_fee;
786+
if user_min_fee == U256::max_value() {
787+
min_fee = user_max_fee
788+
}
789+
let min_balance: U256 = U256::from(user_proofs_in_batch) * min_fee;
783790
user_balance >= min_balance
784791
}
785792

0 commit comments

Comments
 (0)