Skip to content

Commit 9e9ab02

Browse files
committed
refactor: consolidate ValidityResponseMessage into ResponseMessage
1 parent d78960e commit 9e9ab02

File tree

2 files changed

+38
-80
lines changed

2 files changed

+38
-80
lines changed

batcher/aligned-batcher/src/lib.rs

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ use aligned_sdk::core::constants::{
2121
};
2222
use aligned_sdk::core::types::{
2323
ClientMessage, NoncedVerificationData, ProofInvalidReason, ProvingSystemId, ResponseMessage,
24-
ValidityResponseMessage, VerificationCommitmentBatch, VerificationData,
25-
VerificationDataCommitment,
24+
VerificationCommitmentBatch, VerificationData, VerificationDataCommitment,
2625
};
2726

2827
use aws_sdk_s3::client::Client as S3Client;
@@ -371,7 +370,7 @@ impl Batcher {
371370
warn!("Received message with incorrect chain id: {msg_chain_id}");
372371
send_message(
373372
ws_conn_sink.clone(),
374-
ValidityResponseMessage::InvalidChainId,
373+
ResponseMessage::InvalidChainId,
375374
)
376375
.await;
377376

@@ -384,7 +383,7 @@ impl Batcher {
384383
warn!("Received message with incorrect payment service address: {msg_payment_service_addr}");
385384
send_message(
386385
ws_conn_sink.clone(),
387-
ValidityResponseMessage::InvalidPaymentServiceAddress(
386+
ResponseMessage::InvalidPaymentServiceAddress(
388387
msg_payment_service_addr,
389388
self.payment_service.address(),
390389
),
@@ -399,7 +398,7 @@ impl Batcher {
399398
error!("Signature verification error");
400399
send_message(
401400
ws_conn_sink.clone(),
402-
ValidityResponseMessage::InvalidSignature,
401+
ResponseMessage::InvalidSignature,
403402
)
404403
.await;
405404
return Ok(());
@@ -409,7 +408,7 @@ impl Batcher {
409408
let proof_size = client_msg.verification_data.verification_data.proof.len();
410409
if proof_size > self.max_proof_size {
411410
error!("Proof size exceeds the maximum allowed size.");
412-
send_message(ws_conn_sink.clone(), ValidityResponseMessage::ProofTooLarge).await;
411+
send_message(ws_conn_sink.clone(), ResponseMessage::ProofTooLarge).await;
413412
return Ok(());
414413
}
415414

@@ -428,7 +427,7 @@ impl Batcher {
428427
);
429428
send_message(
430429
ws_conn_sink.clone(),
431-
ValidityResponseMessage::InvalidProof(ProofInvalidReason::DisabledVerifier(
430+
ResponseMessage::InvalidProof(ProofInvalidReason::DisabledVerifier(
432431
verification_data.proving_system,
433432
)),
434433
)
@@ -440,7 +439,7 @@ impl Batcher {
440439
error!("Invalid proof detected. Verification failed");
441440
send_message(
442441
ws_conn_sink.clone(),
443-
ValidityResponseMessage::InvalidProof(ProofInvalidReason::RejectedProof),
442+
ResponseMessage::InvalidProof(ProofInvalidReason::RejectedProof),
444443
)
445444
.await;
446445
return Ok(());
@@ -461,7 +460,7 @@ impl Batcher {
461460
if self.user_balance_is_unlocked(&addr).await {
462461
send_message(
463462
ws_conn_sink.clone(),
464-
ValidityResponseMessage::InsufficientBalance(addr),
463+
ResponseMessage::InsufficientBalance(addr),
465464
)
466465
.await;
467466
return Ok(());
@@ -471,7 +470,7 @@ impl Batcher {
471470
let max_fee = nonced_verification_data.max_fee;
472471
if max_fee < U256::from(MIN_FEE_PER_PROOF) {
473472
error!("The max fee signed in the message is less than the accepted minimum fee to be included in the batch.");
474-
send_message(ws_conn_sink.clone(), ValidityResponseMessage::InvalidMaxFee).await;
473+
send_message(ws_conn_sink.clone(), ResponseMessage::InvalidMaxFee).await;
475474
return Ok(());
476475
}
477476

@@ -494,7 +493,7 @@ impl Batcher {
494493
error!(
495494
"Failed to get user nonce from Ethereum for address {addr:?}. Error: {e:?}"
496495
);
497-
send_message(ws_conn_sink.clone(), ValidityResponseMessage::InvalidNonce).await;
496+
send_message(ws_conn_sink.clone(), ResponseMessage::InvalidNonce).await;
498497
return Ok(());
499498
}
500499
};
@@ -512,7 +511,7 @@ impl Batcher {
512511

513512
let Some(user_balance) = self.get_user_balance(&addr).await else {
514513
error!("Could not get balance for address {addr:?}");
515-
send_message(ws_conn_sink.clone(), ValidityResponseMessage::EthRpcError).await;
514+
send_message(ws_conn_sink.clone(), ResponseMessage::EthRpcError).await;
516515
return Ok(());
517516
};
518517

@@ -524,15 +523,15 @@ impl Batcher {
524523
let Some(proofs_in_batch) = batch_state_lock.get_user_proof_count(&addr).await else {
525524
error!("Failed to get user proof count: User not found in user states, but it should have been already inserted");
526525
std::mem::drop(batch_state_lock);
527-
send_message(ws_conn_sink.clone(), ValidityResponseMessage::InvalidNonce).await;
526+
send_message(ws_conn_sink.clone(), ResponseMessage::InvalidNonce).await;
528527
return Ok(());
529528
};
530529

531530
if !self.check_min_balance(proofs_in_batch + 1, user_balance) {
532531
std::mem::drop(batch_state_lock);
533532
send_message(
534533
ws_conn_sink.clone(),
535-
ValidityResponseMessage::InsufficientBalance(addr),
534+
ResponseMessage::InsufficientBalance(addr),
536535
)
537536
.await;
538537
return Ok(());
@@ -542,14 +541,14 @@ impl Batcher {
542541
let Some(expected_nonce) = cached_user_nonce else {
543542
error!("Failed to get cached user nonce: User not found in user states, but it should have been already inserted");
544543
std::mem::drop(batch_state_lock);
545-
send_message(ws_conn_sink.clone(), ValidityResponseMessage::InvalidNonce).await;
544+
send_message(ws_conn_sink.clone(), ResponseMessage::InvalidNonce).await;
546545
return Ok(());
547546
};
548547

549548
if expected_nonce < msg_nonce {
550549
std::mem::drop(batch_state_lock);
551550
warn!("Invalid nonce for address {addr}, had nonce {expected_nonce:?} < {msg_nonce:?}");
552-
send_message(ws_conn_sink.clone(), ValidityResponseMessage::InvalidNonce).await;
551+
send_message(ws_conn_sink.clone(), ResponseMessage::InvalidNonce).await;
553552
return Ok(());
554553
}
555554

@@ -572,14 +571,14 @@ impl Batcher {
572571
let msg_max_fee = nonced_verification_data.max_fee;
573572
let Some(user_min_fee) = batch_state_lock.get_user_min_fee(&addr).await else {
574573
std::mem::drop(batch_state_lock);
575-
send_message(ws_conn_sink.clone(), ValidityResponseMessage::InvalidNonce).await;
574+
send_message(ws_conn_sink.clone(), ResponseMessage::InvalidNonce).await;
576575
return Ok(());
577576
};
578577

579578
if msg_max_fee > user_min_fee {
580579
std::mem::drop(batch_state_lock);
581580
warn!("Invalid max fee for address {addr}, had fee {user_min_fee:?} < {msg_max_fee:?}");
582-
send_message(ws_conn_sink.clone(), ValidityResponseMessage::InvalidMaxFee).await;
581+
send_message(ws_conn_sink.clone(), ResponseMessage::InvalidMaxFee).await;
583582
return Ok(());
584583
}
585584

@@ -598,12 +597,12 @@ impl Batcher {
598597
.await
599598
{
600599
error!("Error while adding entry to batch: {e:?}");
601-
send_message(ws_conn_sink, ValidityResponseMessage::AddToBatchError).await;
600+
send_message(ws_conn_sink, ResponseMessage::AddToBatchError).await;
602601
return Ok(());
603602
};
604603

605604
info!("Verification data message handled");
606-
send_message(ws_conn_sink, ValidityResponseMessage::Valid).await;
605+
send_message(ws_conn_sink, ResponseMessage::Valid).await;
607606
Ok(())
608607
}
609608

@@ -638,7 +637,7 @@ impl Batcher {
638637
let Some(entry) = batch_state_lock.get_entry(addr, nonce) else {
639638
std::mem::drop(batch_state_lock);
640639
warn!("Invalid nonce for address {addr}. Queue entry with nonce {nonce} not found");
641-
send_message(ws_conn_sink.clone(), ValidityResponseMessage::InvalidNonce).await;
640+
send_message(ws_conn_sink.clone(), ResponseMessage::InvalidNonce).await;
642641
return;
643642
};
644643

@@ -648,7 +647,7 @@ impl Batcher {
648647
warn!("Invalid replacement message for address {addr}, had fee {original_max_fee:?} < {replacement_max_fee:?}");
649648
send_message(
650649
ws_conn_sink.clone(),
651-
ValidityResponseMessage::InvalidReplacementMessage,
650+
ResponseMessage::InvalidReplacementMessage,
652651
)
653652
.await;
654653

@@ -685,7 +684,7 @@ impl Batcher {
685684
warn!("Invalid replacement message");
686685
send_message(
687686
ws_conn_sink.clone(),
688-
ValidityResponseMessage::InvalidReplacementMessage,
687+
ResponseMessage::InvalidReplacementMessage,
689688
)
690689
.await;
691690
return;
@@ -1201,7 +1200,7 @@ impl Batcher {
12011200
info!("Handling nonpaying message");
12021201
let Some(non_paying_config) = self.non_paying_config.as_ref() else {
12031202
warn!("There isn't a non-paying configuration loaded. This message will be ignored");
1204-
send_message(ws_sink.clone(), ValidityResponseMessage::InvalidNonce).await;
1203+
send_message(ws_sink.clone(), ResponseMessage::InvalidNonce).await;
12051204
return Ok(());
12061205
};
12071206

@@ -1210,7 +1209,7 @@ impl Batcher {
12101209
error!("Could not get balance for non-paying address {replacement_addr:?}");
12111210
send_message(
12121211
ws_sink.clone(),
1213-
ValidityResponseMessage::InsufficientBalance(replacement_addr),
1212+
ResponseMessage::InsufficientBalance(replacement_addr),
12141213
)
12151214
.await;
12161215
return Ok(());
@@ -1220,7 +1219,7 @@ impl Batcher {
12201219
error!("Insufficient funds for non-paying address {replacement_addr:?}");
12211220
send_message(
12221221
ws_sink.clone(),
1223-
ValidityResponseMessage::InsufficientBalance(replacement_addr),
1222+
ResponseMessage::InsufficientBalance(replacement_addr),
12241223
)
12251224
.await;
12261225
return Ok(());
@@ -1231,7 +1230,7 @@ impl Batcher {
12311230
else {
12321231
std::mem::drop(batch_state_lock);
12331232
error!("Nonce for non-paying address {replacement_addr:?} not found in cache.");
1234-
send_message(ws_sink.clone(), ValidityResponseMessage::EthRpcError).await;
1233+
send_message(ws_sink.clone(), ResponseMessage::EthRpcError).await;
12351234
return Ok(());
12361235
};
12371236

@@ -1263,12 +1262,12 @@ impl Batcher {
12631262
.await
12641263
{
12651264
info!("Error while adding nonpaying address entry to batch: {e:?}");
1266-
send_message(ws_sink, ValidityResponseMessage::AddToBatchError).await;
1265+
send_message(ws_sink, ResponseMessage::AddToBatchError).await;
12671266
return Ok(());
12681267
};
12691268

12701269
info!("Non-paying verification data message handled");
1271-
send_message(ws_sink, ValidityResponseMessage::Valid).await;
1270+
send_message(ws_sink, ResponseMessage::Valid).await;
12721271
Ok(())
12731272
}
12741273

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

Lines changed: 10 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -329,24 +329,6 @@ impl AlignedVerificationData {
329329
}
330330
}
331331

332-
#[derive(Debug, Clone, Serialize, Deserialize)]
333-
pub enum ValidityResponseMessage {
334-
Valid,
335-
InvalidNonce,
336-
InvalidSignature,
337-
InvalidChainId,
338-
InvalidProof(ProofInvalidReason),
339-
InvalidMaxFee,
340-
InvalidReplacementMessage,
341-
AddToBatchError,
342-
ProofTooLarge,
343-
InsufficientBalance(Address),
344-
EthRpcError,
345-
InvalidPaymentServiceAddress(Address, Address),
346-
BatchInclusionData(BatchInclusionData),
347-
CreateNewTaskError(String, String), //merkle-root, error
348-
349-
}
350332

351333
#[derive(Debug, Clone, Serialize, Deserialize)]
352334
pub enum ProofInvalidReason {
@@ -355,39 +337,6 @@ pub enum ProofInvalidReason {
355337
DisabledVerifier(ProvingSystemId),
356338
}
357339

358-
impl Display for ValidityResponseMessage {
359-
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
360-
match self {
361-
ValidityResponseMessage::Valid => write!(f, "Valid"),
362-
ValidityResponseMessage::InvalidNonce => write!(f, "Invalid nonce"),
363-
ValidityResponseMessage::InvalidSignature => write!(f, "Invalid signature"),
364-
ValidityResponseMessage::InvalidChainId => write!(f, "Invalid chain id"),
365-
ValidityResponseMessage::InvalidProof(reason) => {
366-
write!(f, "Invalid proof: {}", reason)
367-
}
368-
ValidityResponseMessage::InvalidMaxFee => write!(f, "Invalid max fee"),
369-
ValidityResponseMessage::InvalidReplacementMessage => {
370-
write!(f, "Invalid replacement message")
371-
}
372-
ValidityResponseMessage::AddToBatchError => write!(f, "Add to batch error"),
373-
ValidityResponseMessage::ProofTooLarge => write!(f, "Proof too large"),
374-
ValidityResponseMessage::InsufficientBalance(addr) => {
375-
write!(f, "Insufficient balance for address {}", addr)
376-
}
377-
ValidityResponseMessage::EthRpcError => write!(f, "Eth RPC error"),
378-
ValidityResponseMessage::InvalidPaymentServiceAddress(addr, expected) => {
379-
write!(
380-
f,
381-
"Invalid payment service address: {}, expected: {}",
382-
addr, expected
383-
)
384-
}
385-
ValidityResponseMessage::BatchInclusionData(data) => write!(f, "Received Batch inclusion data, this means the task was processed: {:?}", data),
386-
ValidityResponseMessage::CreateNewTaskError(merkle_root, error) => write!(f, "Create new task error: merkle root: {}, error: {}", merkle_root, error)
387-
}
388-
}
389-
}
390-
391340
impl Display for ProofInvalidReason {
392341
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
393342
match self {
@@ -408,6 +357,16 @@ pub enum ResponseMessage {
408357
InvalidProof(ProofInvalidReason),
409358
BatchReset,
410359
Error(String),
360+
InvalidNonce,
361+
InvalidSignature,
362+
ProofTooLarge,
363+
InvalidMaxFee,
364+
InsufficientBalance,
365+
InvalidChainId,
366+
InvalidReplacementMessage,
367+
AddToBatchError,
368+
EthRpcError,
369+
InvalidPaymentServiceAddress(Address, Address)
411370
}
412371

413372
#[derive(Debug, Clone, Copy)]

0 commit comments

Comments
 (0)