Skip to content

Commit 0203f50

Browse files
committed
feat(batcher): get first nonce in queue message
1 parent 650beeb commit 0203f50

File tree

2 files changed

+79
-3
lines changed

2 files changed

+79
-3
lines changed

batcher/aligned-batcher/src/lib.rs

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ use aligned_sdk::core::constants::{
3131
RESPOND_TO_TASK_FEE_LIMIT_PERCENTAGE_MULTIPLIER,
3232
};
3333
use aligned_sdk::core::types::{
34-
ClientMessage, GetNonceResponseMessage, NoncedVerificationData, ProofInvalidReason,
35-
ProvingSystemId, SubmitProofMessage, SubmitProofResponseMessage, VerificationCommitmentBatch,
36-
VerificationData, VerificationDataCommitment,
34+
ClientMessage, GetFirstNonceInQueueResponseMessage, GetNonceResponseMessage,
35+
NoncedVerificationData, ProofInvalidReason, ProvingSystemId, SubmitProofMessage,
36+
SubmitProofResponseMessage, VerificationCommitmentBatch, VerificationData,
37+
VerificationDataCommitment,
3738
};
3839

3940
use aws_sdk_s3::client::Client as S3Client;
@@ -473,6 +474,11 @@ impl Batcher {
473474
.handle_submit_proof_msg(msg, ws_conn_sink)
474475
.await
475476
}
477+
ClientMessage::GetFirstNonceInQueueForAddress(address) => {
478+
self.clone()
479+
.handle_get_first_nonce_in_queue_for_address_msg(address, ws_conn_sink)
480+
.await
481+
}
476482
}
477483
}
478484

@@ -533,6 +539,65 @@ impl Batcher {
533539
Ok(())
534540
}
535541

542+
async fn handle_get_first_nonce_in_queue_for_address_msg(
543+
self: Arc<Self>,
544+
mut address: Address,
545+
ws_conn_sink: WsMessageSink,
546+
) -> Result<(), Error> {
547+
if self.is_nonpaying(&address) {
548+
info!("Handling nonpaying message");
549+
let Some(non_paying_config) = self.non_paying_config.as_ref() else {
550+
warn!(
551+
"There isn't a non-paying configuration loaded. This message will be ignored"
552+
);
553+
send_message(
554+
ws_conn_sink.clone(),
555+
GetFirstNonceInQueueResponseMessage::InvalidRequest(
556+
"There isn't a non-paying configuration loaded.".to_string(),
557+
),
558+
)
559+
.await;
560+
return Ok(());
561+
};
562+
let replacement_addr = non_paying_config.replacement.address();
563+
address = replacement_addr;
564+
}
565+
566+
let (cached_user_nonce, proof_count) = {
567+
let batch_state_lock = self.batch_state.lock().await;
568+
(
569+
batch_state_lock.get_user_nonce(&address).await,
570+
batch_state_lock.get_user_proof_count(&address).await,
571+
)
572+
};
573+
574+
let Some(user_nonce) = cached_user_nonce else {
575+
send_message(
576+
ws_conn_sink.clone(),
577+
GetFirstNonceInQueueResponseMessage::NoProofsInQueue,
578+
)
579+
.await;
580+
return Ok(());
581+
};
582+
583+
let Some(proof_count) = proof_count else {
584+
send_message(
585+
ws_conn_sink.clone(),
586+
GetFirstNonceInQueueResponseMessage::NoProofsInQueue,
587+
)
588+
.await;
589+
return Ok(());
590+
};
591+
592+
send_message(
593+
ws_conn_sink.clone(),
594+
GetFirstNonceInQueueResponseMessage::Nonce(user_nonce - proof_count),
595+
)
596+
.await;
597+
598+
Ok(())
599+
}
600+
536601
async fn handle_submit_proof_msg(
537602
self: Arc<Self>,
538603
client_msg: Box<SubmitProofMessage>,

batcher/aligned-sdk/src/core/types.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ pub enum ClientMessage {
231231
// Needs to be wrapped in box as the message is 3x bigger than the others
232232
// see https://rust-lang.github.io/rust-clippy/master/index.html#large_enum_variant
233233
SubmitProof(Box<SubmitProofMessage>),
234+
GetFirstNonceInQueueForAddress(Address),
234235
}
235236

236237
impl Display for ClientMessage {
@@ -239,6 +240,9 @@ impl Display for ClientMessage {
239240
match self {
240241
ClientMessage::GetNonceForAddress(_) => write!(f, "GetNonceForAddress"),
241242
ClientMessage::SubmitProof(_) => write!(f, "SubmitProof"),
243+
ClientMessage::GetFirstNonceInQueueForAddress(_) => {
244+
write!(f, "GetFirstNonceInQueueForAddress")
245+
}
242246
}
243247
}
244248
}
@@ -396,6 +400,13 @@ pub enum GetNonceResponseMessage {
396400
InvalidRequest(String),
397401
}
398402

403+
#[derive(Debug, Clone, Serialize, Deserialize)]
404+
pub enum GetFirstNonceInQueueResponseMessage {
405+
Nonce(U256),
406+
InvalidRequest(String),
407+
NoProofsInQueue,
408+
}
409+
399410
#[derive(Debug, Clone, Copy)]
400411
pub enum Network {
401412
Devnet,

0 commit comments

Comments
 (0)