From 92dc9b30059a326f34aff4333cc0234f20911b36 Mon Sep 17 00:00:00 2001 From: JuArce <52429267+JuArce@users.noreply.github.com> Date: Mon, 18 Aug 2025 13:14:59 -0300 Subject: [PATCH 1/5] perf(batcher): do not serialize to cbor on batch size calculation --- crates/batcher/src/types/batch_queue.rs | 17 ++++----------- crates/sdk/src/common/types.rs | 29 +++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/crates/batcher/src/types/batch_queue.rs b/crates/batcher/src/types/batch_queue.rs index af67715d77..3ac2a422f8 100644 --- a/crates/batcher/src/types/batch_queue.rs +++ b/crates/batcher/src/types/batch_queue.rs @@ -3,7 +3,6 @@ use aligned_sdk::{ constants::CBOR_ARRAY_MAX_OVERHEAD, types::{NoncedVerificationData, VerificationDataCommitment}, }, - communication::serialization::cbor_serialize, }; use ethers::types::{Address, Signature, U256}; use priority_queue::PriorityQueue; @@ -124,14 +123,9 @@ pub(crate) type BatchQueue = PriorityQueue Result { let folded_result = batch_queue.iter().try_fold(0, |acc, (entry, _)| { - if let Ok(verification_data_bytes) = - cbor_serialize(&entry.nonced_verification_data.verification_data) - { - let current_batch_size = acc + verification_data_bytes.len(); - ControlFlow::Continue(current_batch_size) - } else { - ControlFlow::Break(()) - } + let verification_data_size = entry.nonced_verification_data.cbor_size_upper_bound(); + let current_batch_size = acc + verification_data_size; + ControlFlow::Continue(current_batch_size) }); if let ControlFlow::Continue(batch_size) = folded_result { @@ -178,10 +172,7 @@ pub(crate) fn extract_batch_directly( let (rejected_entry, rejected_priority) = batch_queue.pop().unwrap(); // Update batch size - let verification_data_size = - cbor_serialize(&rejected_entry.nonced_verification_data.verification_data) - .unwrap() - .len(); + let verification_data_size = rejected_entry.nonced_verification_data.cbor_size_upper_bound(); batch_size -= verification_data_size; rejected_entries.push((rejected_entry, rejected_priority)); diff --git a/crates/sdk/src/common/types.rs b/crates/sdk/src/common/types.rs index 5f1a9931fc..1968b209f7 100644 --- a/crates/sdk/src/common/types.rs +++ b/crates/sdk/src/common/types.rs @@ -72,6 +72,7 @@ pub struct VerificationData { pub proof_generator_addr: Address, } + #[derive(Debug, Serialize, Deserialize, Clone)] pub struct NoncedVerificationData { pub verification_data: VerificationData, @@ -97,6 +98,34 @@ impl NoncedVerificationData { payment_service_addr, } } + + /// Returns an upper bound for the CBOR encoding size without performing serialization. + /// Sums the length of all Vec fields in the inner VerificationData and adds 256 bytes as overhead. + pub fn cbor_size_upper_bound(&self) -> usize { + const CBOR_OVERHEAD_BYTES: usize = 256; + let mut total_size = 0; + + // Add length of proof Vec + total_size += self.verification_data.proof.len(); + + // Add length of pub_input Option> + if let Some(ref pub_input) = self.verification_data.pub_input { + total_size += pub_input.len(); + } + + // Add length of verification_key Option> + if let Some(ref verification_key) = self.verification_data.verification_key { + total_size += verification_key.len(); + } + + // Add length of vm_program_code Option> + if let Some(ref vm_program_code) = self.verification_data.vm_program_code { + total_size += vm_program_code.len(); + } + + // Add overhead bytes for the full NoncedVerificationData structure + total_size + CBOR_OVERHEAD_BYTES + } } // Defines a price estimate type for the user. From 95de331d1e6900f95006891c099712d191c1c11f Mon Sep 17 00:00:00 2001 From: JuArce <52429267+JuArce@users.noreply.github.com> Date: Mon, 18 Aug 2025 13:15:23 -0300 Subject: [PATCH 2/5] cargo fmt --- crates/batcher/src/types/batch_queue.rs | 12 ++++++------ crates/sdk/src/common/types.rs | 11 +++++------ 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/crates/batcher/src/types/batch_queue.rs b/crates/batcher/src/types/batch_queue.rs index 3ac2a422f8..8d9acb3959 100644 --- a/crates/batcher/src/types/batch_queue.rs +++ b/crates/batcher/src/types/batch_queue.rs @@ -1,8 +1,6 @@ -use aligned_sdk::{ - common::{ - constants::CBOR_ARRAY_MAX_OVERHEAD, - types::{NoncedVerificationData, VerificationDataCommitment}, - }, +use aligned_sdk::common::{ + constants::CBOR_ARRAY_MAX_OVERHEAD, + types::{NoncedVerificationData, VerificationDataCommitment}, }; use ethers::types::{Address, Signature, U256}; use priority_queue::PriorityQueue; @@ -172,7 +170,9 @@ pub(crate) fn extract_batch_directly( let (rejected_entry, rejected_priority) = batch_queue.pop().unwrap(); // Update batch size - let verification_data_size = rejected_entry.nonced_verification_data.cbor_size_upper_bound(); + let verification_data_size = rejected_entry + .nonced_verification_data + .cbor_size_upper_bound(); batch_size -= verification_data_size; rejected_entries.push((rejected_entry, rejected_priority)); diff --git a/crates/sdk/src/common/types.rs b/crates/sdk/src/common/types.rs index 1968b209f7..b042bdeeac 100644 --- a/crates/sdk/src/common/types.rs +++ b/crates/sdk/src/common/types.rs @@ -72,7 +72,6 @@ pub struct VerificationData { pub proof_generator_addr: Address, } - #[derive(Debug, Serialize, Deserialize, Clone)] pub struct NoncedVerificationData { pub verification_data: VerificationData, @@ -104,25 +103,25 @@ impl NoncedVerificationData { pub fn cbor_size_upper_bound(&self) -> usize { const CBOR_OVERHEAD_BYTES: usize = 256; let mut total_size = 0; - + // Add length of proof Vec total_size += self.verification_data.proof.len(); - + // Add length of pub_input Option> if let Some(ref pub_input) = self.verification_data.pub_input { total_size += pub_input.len(); } - + // Add length of verification_key Option> if let Some(ref verification_key) = self.verification_data.verification_key { total_size += verification_key.len(); } - + // Add length of vm_program_code Option> if let Some(ref vm_program_code) = self.verification_data.vm_program_code { total_size += vm_program_code.len(); } - + // Add overhead bytes for the full NoncedVerificationData structure total_size + CBOR_OVERHEAD_BYTES } From d1e2170afdd15d0e1c3c4807ed776a42adbd6c5d Mon Sep 17 00:00:00 2001 From: JuArce <52429267+JuArce@users.noreply.github.com> Date: Mon, 18 Aug 2025 13:25:03 -0300 Subject: [PATCH 3/5] cargo clippy --- crates/batcher/src/types/batch_queue.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/batcher/src/types/batch_queue.rs b/crates/batcher/src/types/batch_queue.rs index 8d9acb3959..7461b4ac3d 100644 --- a/crates/batcher/src/types/batch_queue.rs +++ b/crates/batcher/src/types/batch_queue.rs @@ -123,7 +123,7 @@ pub(crate) fn calculate_batch_size(batch_queue: &BatchQueue) -> Result::Continue(current_batch_size) }); if let ControlFlow::Continue(batch_size) = folded_result { From 3f5a56c7c3ad8347ff54006a283f8a7851a9fbf2 Mon Sep 17 00:00:00 2001 From: MauroFab Date: Mon, 18 Aug 2025 15:52:06 -0300 Subject: [PATCH 4/5] Increase constant size buffer, add function call to handle proof --- crates/batcher/src/lib.rs | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/crates/batcher/src/lib.rs b/crates/batcher/src/lib.rs index a3d154987f..5fa13ff1e8 100644 --- a/crates/batcher/src/lib.rs +++ b/crates/batcher/src/lib.rs @@ -2398,22 +2398,9 @@ impl Batcher { client_msg: &SubmitProofMessage, ws_conn_sink: &WsMessageSink, ) -> bool { - let verification_data = match cbor_serialize(&client_msg.verification_data) { - Ok(data) => data, - // This should never happened, the user sent all his data serialized - Err(_) => { - error!("Proof serialization error"); - send_message( - ws_conn_sink.clone(), - SubmitProofResponseMessage::Error("Proof serialization error".to_string()), - ) - .await; - self.metrics.user_error(&["proof_serialization_error", ""]); - return false; - } - }; + let verification_data_size = client_msg.verification_data.cbor_size_upper_bound(); - if verification_data.len() > self.max_proof_size { + if verification_data_size > self.max_proof_size { error!("Proof size exceeds the maximum allowed size."); send_message( ws_conn_sink.clone(), From 40fbe392363ff5debb4d297e7e4838b88e9ecaf6 Mon Sep 17 00:00:00 2001 From: MauroFab Date: Mon, 18 Aug 2025 15:54:38 -0300 Subject: [PATCH 5/5] Increase constant size upper bound --- crates/sdk/src/common/types.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/sdk/src/common/types.rs b/crates/sdk/src/common/types.rs index b042bdeeac..2129ac38f1 100644 --- a/crates/sdk/src/common/types.rs +++ b/crates/sdk/src/common/types.rs @@ -99,9 +99,9 @@ impl NoncedVerificationData { } /// Returns an upper bound for the CBOR encoding size without performing serialization. - /// Sums the length of all Vec fields in the inner VerificationData and adds 256 bytes as overhead. + /// Sums the length of all Vec fields in the inner VerificationData and adds 1024 bytes as overhead. This covers the constant size data and extra headers pub fn cbor_size_upper_bound(&self) -> usize { - const CBOR_OVERHEAD_BYTES: usize = 256; + const CBOR_OVERHEAD_BYTES: usize = 1024; let mut total_size = 0; // Add length of proof Vec