Skip to content

Commit 6bb2431

Browse files
committed
feat: get last max fee limit message
1 parent 8924dbe commit 6bb2431

File tree

2 files changed

+94
-3
lines changed

2 files changed

+94
-3
lines changed

crates/batcher/src/lib.rs

Lines changed: 85 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ use aligned_sdk::common::constants::{
3333
RESPOND_TO_TASK_FEE_LIMIT_PERCENTAGE_MULTIPLIER,
3434
};
3535
use aligned_sdk::common::types::{
36-
ClientMessage, GetNonceResponseMessage, NoncedVerificationData, ProofInvalidReason,
37-
ProvingSystemId, SubmitProofMessage, SubmitProofResponseMessage, VerificationCommitmentBatch,
38-
VerificationData, VerificationDataCommitment,
36+
ClientMessage, GetLastMaxFeeResponseMessage, GetNonceResponseMessage, NoncedVerificationData,
37+
ProofInvalidReason, ProvingSystemId, SubmitProofMessage, SubmitProofResponseMessage,
38+
VerificationCommitmentBatch, VerificationData, VerificationDataCommitment,
3939
};
4040

4141
use aws_sdk_s3::client::Client as S3Client;
@@ -916,6 +916,11 @@ impl Batcher {
916916
.handle_submit_proof_msg(msg, ws_conn_sink)
917917
.await
918918
}
919+
ClientMessage::GetLastMaxFee(address) => {
920+
self.clone()
921+
.handle_get_last_max_fee(address, ws_conn_sink)
922+
.await
923+
}
919924
}
920925
}
921926

@@ -1004,6 +1009,83 @@ impl Batcher {
10041009
Ok(())
10051010
}
10061011

1012+
async fn handle_get_last_max_fee(
1013+
self: Arc<Self>,
1014+
mut address: Address,
1015+
ws_conn_sink: WsMessageSink,
1016+
) -> Result<(), Error> {
1017+
// If the address is not paying, we will return the last max fee of the aligned_payment_address
1018+
if !self.has_to_pay(&address) {
1019+
info!("Handling nonpaying message");
1020+
let Some(non_paying_config) = self.non_paying_config.as_ref() else {
1021+
warn!(
1022+
"There isn't a non-paying configuration loaded. This message will be ignored"
1023+
);
1024+
send_message(
1025+
ws_conn_sink.clone(),
1026+
GetLastMaxFeeResponseMessage::InvalidRequest(
1027+
"There isn't a non-paying configuration loaded.".to_string(),
1028+
),
1029+
)
1030+
.await;
1031+
return Ok(());
1032+
};
1033+
let replacement_addr = non_paying_config.replacement.address();
1034+
address = replacement_addr;
1035+
}
1036+
1037+
let user_states_guard = match timeout(MESSAGE_HANDLER_LOCK_TIMEOUT, self.user_states.read())
1038+
.await
1039+
{
1040+
Ok(guard) => guard,
1041+
Err(_) => {
1042+
warn!("User states read lock acquisition timed out in handle_get_last_max_fee_for_address_msg");
1043+
self.metrics.inc_message_handler_user_states_lock_timeouts();
1044+
send_message(ws_conn_sink, GetLastMaxFeeResponseMessage::ServerBusy).await;
1045+
return Ok(());
1046+
}
1047+
};
1048+
1049+
let Some(usr_ref) = user_states_guard.get(&address).cloned() else {
1050+
drop(user_states_guard);
1051+
// if the user isn't in the cache, return zero
1052+
send_message(
1053+
ws_conn_sink.clone(),
1054+
GetLastMaxFeeResponseMessage::LastMaxFee(U256::MAX),
1055+
)
1056+
.await;
1057+
return Ok(());
1058+
};
1059+
1060+
let Some(usr_lock) = self
1061+
.try_user_lock_with_timeout(address, usr_ref.lock())
1062+
.await
1063+
else {
1064+
send_message(
1065+
ws_conn_sink.clone(),
1066+
GetLastMaxFeeResponseMessage::ServerBusy,
1067+
)
1068+
.await;
1069+
return Ok(());
1070+
};
1071+
1072+
let proofs_in_queue = usr_lock.proofs_in_batch;
1073+
let max_fee = if proofs_in_queue > 0 {
1074+
usr_lock.last_max_fee_limit
1075+
} else {
1076+
U256::MAX
1077+
};
1078+
drop(usr_lock);
1079+
1080+
send_message(
1081+
ws_conn_sink.clone(),
1082+
GetLastMaxFeeResponseMessage::LastMaxFee(max_fee),
1083+
)
1084+
.await;
1085+
1086+
Ok(())
1087+
}
1088+
10071089
/// Returns the Aligned-funded address that will be used to pay for proofs when users don't need to pay themselves.
10081090
/// This function assumes that the non-paying configuration is set.
10091091
fn aligned_payment_address(&self) -> Address {

crates/sdk/src/common/types.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ pub enum ClientMessage {
295295
// Needs to be wrapped in box as the message is 3x bigger than the others
296296
// see https://rust-lang.github.io/rust-clippy/master/index.html#large_enum_variant
297297
SubmitProof(Box<SubmitProofMessage>),
298+
GetLastMaxFee(Address),
298299
}
299300

300301
impl Display for ClientMessage {
@@ -303,6 +304,7 @@ impl Display for ClientMessage {
303304
match self {
304305
ClientMessage::GetNonceForAddress(_) => write!(f, "GetNonceForAddress"),
305306
ClientMessage::SubmitProof(_) => write!(f, "SubmitProof"),
307+
ClientMessage::GetLastMaxFee(_) => write!(f, "GetLastMaxFee"),
306308
}
307309
}
308310
}
@@ -464,6 +466,13 @@ pub enum GetNonceResponseMessage {
464466
ServerBusy,
465467
}
466468

469+
#[derive(Debug, Clone, Serialize, Deserialize)]
470+
pub enum GetLastMaxFeeResponseMessage {
471+
LastMaxFee(U256),
472+
InvalidRequest(String),
473+
ServerBusy,
474+
}
475+
467476
#[derive(Debug, Clone)]
468477
pub enum Network {
469478
Devnet,

0 commit comments

Comments
 (0)