Skip to content

Commit 0cf435a

Browse files
authored
Optimize verification data size calculation (#2055)
1 parent c73d995 commit 0cf435a

File tree

3 files changed

+37
-35
lines changed

3 files changed

+37
-35
lines changed

crates/batcher/src/lib.rs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2050,22 +2050,9 @@ impl Batcher {
20502050
client_msg: &SubmitProofMessage,
20512051
ws_conn_sink: &WsMessageSink,
20522052
) -> bool {
2053-
let verification_data = match cbor_serialize(&client_msg.verification_data) {
2054-
Ok(data) => data,
2055-
// This should never happened, the user sent all his data serialized
2056-
Err(_) => {
2057-
error!("Proof serialization error");
2058-
send_message(
2059-
ws_conn_sink.clone(),
2060-
SubmitProofResponseMessage::Error("Proof serialization error".to_string()),
2061-
)
2062-
.await;
2063-
self.metrics.user_error(&["proof_serialization_error", ""]);
2064-
return false;
2065-
}
2066-
};
2053+
let verification_data_size = client_msg.verification_data.cbor_size_upper_bound();
20672054

2068-
if verification_data.len() > self.max_proof_size {
2055+
if verification_data_size > self.max_proof_size {
20692056
error!("Proof size exceeds the maximum allowed size.");
20702057
send_message(
20712058
ws_conn_sink.clone(),

crates/batcher/src/types/batch_queue.rs

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
use aligned_sdk::{
2-
common::{
3-
constants::CBOR_ARRAY_MAX_OVERHEAD,
4-
types::{NoncedVerificationData, VerificationDataCommitment},
5-
},
6-
communication::serialization::cbor_serialize,
1+
use aligned_sdk::common::{
2+
constants::CBOR_ARRAY_MAX_OVERHEAD,
3+
types::{NoncedVerificationData, VerificationDataCommitment},
74
};
85
use ethers::types::{Address, Signature, U256};
96
use priority_queue::PriorityQueue;
@@ -124,14 +121,9 @@ pub(crate) type BatchQueue = PriorityQueue<BatchQueueEntry, BatchQueueEntryPrior
124121
/// Calculates the size of the batch represented by the given batch queue.
125122
pub(crate) fn calculate_batch_size(batch_queue: &BatchQueue) -> Result<usize, BatcherError> {
126123
let folded_result = batch_queue.iter().try_fold(0, |acc, (entry, _)| {
127-
if let Ok(verification_data_bytes) =
128-
cbor_serialize(&entry.nonced_verification_data.verification_data)
129-
{
130-
let current_batch_size = acc + verification_data_bytes.len();
131-
ControlFlow::Continue(current_batch_size)
132-
} else {
133-
ControlFlow::Break(())
134-
}
124+
let verification_data_size = entry.nonced_verification_data.cbor_size_upper_bound();
125+
let current_batch_size = acc + verification_data_size;
126+
ControlFlow::<usize, usize>::Continue(current_batch_size)
135127
});
136128

137129
if let ControlFlow::Continue(batch_size) = folded_result {
@@ -178,12 +170,7 @@ pub(crate) fn try_build_batch(
178170
// * Subtract this entry size to the size of the batch size.
179171
// * Push the current entry to the resulting batch queue.
180172

181-
// It is safe to call `.unwrap()` here since any serialization error should have been caught
182-
// when calculating the total size of the batch with the `calculate_batch_size` function
183-
let verification_data_size =
184-
cbor_serialize(&entry.nonced_verification_data.verification_data)
185-
.unwrap()
186-
.len();
173+
let verification_data_size = entry.nonced_verification_data.cbor_size_upper_bound();
187174
batch_size -= verification_data_size;
188175

189176
finalized_batch.pop();

crates/sdk/src/common/types.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,34 @@ impl NoncedVerificationData {
9797
payment_service_addr,
9898
}
9999
}
100+
101+
/// Returns an upper bound for the CBOR encoding size without performing serialization.
102+
/// Sums the length of all Vec<u8> fields in the inner VerificationData and adds 1024 bytes as overhead. This covers the constant size data and extra headers
103+
pub fn cbor_size_upper_bound(&self) -> usize {
104+
const CBOR_OVERHEAD_BYTES: usize = 1024;
105+
let mut total_size = 0;
106+
107+
// Add length of proof Vec<u8>
108+
total_size += self.verification_data.proof.len();
109+
110+
// Add length of pub_input Option<Vec<u8>>
111+
if let Some(ref pub_input) = self.verification_data.pub_input {
112+
total_size += pub_input.len();
113+
}
114+
115+
// Add length of verification_key Option<Vec<u8>>
116+
if let Some(ref verification_key) = self.verification_data.verification_key {
117+
total_size += verification_key.len();
118+
}
119+
120+
// Add length of vm_program_code Option<Vec<u8>>
121+
if let Some(ref vm_program_code) = self.verification_data.vm_program_code {
122+
total_size += vm_program_code.len();
123+
}
124+
125+
// Add overhead bytes for the full NoncedVerificationData structure
126+
total_size + CBOR_OVERHEAD_BYTES
127+
}
100128
}
101129

102130
// Defines a price estimate type for the user.

0 commit comments

Comments
 (0)