Skip to content

Commit 9f706ef

Browse files
committed
feat: batch cant have more proofs than max_batch_len
1 parent b338f32 commit 9f706ef

File tree

4 files changed

+120
-6
lines changed

4 files changed

+120
-6
lines changed

batcher/aligned-batcher/src/config/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ pub struct BatcherConfigFromYaml {
4141
pub transaction_wait_timeout: u64,
4242
pub max_proof_size: usize,
4343
pub max_batch_size: usize,
44+
pub max_batch_len: usize,
4445
pub pre_verification_is_enabled: bool,
4546
pub metrics_port: u16,
4647
pub telemetry_ip_port_address: String,

batcher/aligned-batcher/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ pub struct Batcher {
8282
transaction_wait_timeout: u64,
8383
max_proof_size: usize,
8484
max_batch_size: usize,
85+
max_batch_len: usize,
8586
last_uploaded_batch_block: Mutex<u64>,
8687
pre_verification_is_enabled: bool,
8788
non_paying_config: Option<NonPayingConfig>,
@@ -244,6 +245,7 @@ impl Batcher {
244245
transaction_wait_timeout: config.batcher.transaction_wait_timeout,
245246
max_proof_size: config.batcher.max_proof_size,
246247
max_batch_size: config.batcher.max_batch_size,
248+
max_batch_len: config.batcher.max_batch_len,
247249
last_uploaded_batch_block: Mutex::new(last_uploaded_batch_block),
248250
pre_verification_is_enabled: config.batcher.pre_verification_is_enabled,
249251
non_paying_config,
@@ -1048,7 +1050,7 @@ impl Batcher {
10481050
*batch_posting = true;
10491051
let batch_queue_copy = batch_state_lock.batch_queue.clone();
10501052
let (resulting_batch_queue, finalized_batch) =
1051-
batch_queue::try_build_batch(batch_queue_copy, gas_price, self.max_batch_size)
1053+
batch_queue::try_build_batch(batch_queue_copy, gas_price, self.max_batch_size, self.max_batch_len)
10521054
.inspect_err(|e| {
10531055
*batch_posting = false;
10541056
match e {

batcher/aligned-batcher/src/types/batch_queue.rs

Lines changed: 115 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ pub(crate) fn try_build_batch(
147147
batch_queue: BatchQueue,
148148
gas_price: U256,
149149
max_batch_size: usize,
150+
max_batch_len: usize,
150151
) -> Result<(BatchQueue, Vec<BatchQueueEntry>), BatcherError> {
151152
let mut batch_queue = batch_queue;
152153
let mut batch_size = calculate_batch_size(&batch_queue)?;
@@ -156,7 +157,7 @@ pub(crate) fn try_build_batch(
156157
let batch_len = batch_queue.len();
157158
let fee_per_proof = calculate_fee_per_proof(batch_len, gas_price);
158159

159-
if batch_size > max_batch_size || fee_per_proof > entry.nonced_verification_data.max_fee {
160+
if batch_size > max_batch_size || fee_per_proof > entry.nonced_verification_data.max_fee || batch_len > max_batch_len {
160161
// Update the state for the next iteration:
161162
// * Subtract this entry size to the size of the batch size.
162163
// * Push the current entry to the resulting batch queue.
@@ -298,7 +299,7 @@ mod test {
298299

299300
let gas_price = U256::from(1);
300301
let (resulting_batch_queue, batch) =
301-
try_build_batch(batch_queue, gas_price, 5000000).unwrap();
302+
try_build_batch(batch_queue, gas_price, 5000000, 50).unwrap();
302303

303304
assert!(resulting_batch_queue.is_empty());
304305

@@ -401,7 +402,7 @@ mod test {
401402

402403
let gas_price = U256::from(1);
403404
let (resulting_batch_queue, finalized_batch) =
404-
try_build_batch(batch_queue, gas_price, 5000000).unwrap();
405+
try_build_batch(batch_queue, gas_price, 5000000, 50).unwrap();
405406

406407
// The resulting batch queue (entries from the old batch queue that were not willing to pay
407408
// in this batch), should be empty and hence, all entries from the batch queue should be in
@@ -512,7 +513,7 @@ mod test {
512513

513514
let gas_price = U256::from(1);
514515
let (resulting_batch_queue, finalized_batch) =
515-
try_build_batch(batch_queue, gas_price, 5000000).unwrap();
516+
try_build_batch(batch_queue, gas_price, 5000000, 50).unwrap();
516517

517518
// The resulting batch queue (entries from the old batch queue that were not willing to pay
518519
// in this batch), should be empty and hence, all entries from the batch queue should be in
@@ -529,4 +530,113 @@ mod test {
529530
max_fee_1
530531
);
531532
}
532-
}
533+
534+
#[test]
535+
fn batch_finalization_algorithm_works_not_bigger_than_max_batch_len() {
536+
// The following information will be the same for each entry, it is just some dummy data to see
537+
// algorithm working.
538+
539+
let proof_generator_addr = Address::random();
540+
let payment_service_addr = Address::random();
541+
let sender_addr = Address::random();
542+
let bytes_for_verification_data = vec![42_u8; 10];
543+
let dummy_signature = Signature {
544+
r: U256::from(1),
545+
s: U256::from(2),
546+
v: 3,
547+
};
548+
let verification_data = VerificationData {
549+
proving_system: ProvingSystemId::Risc0,
550+
proof: bytes_for_verification_data.clone(),
551+
pub_input: Some(bytes_for_verification_data.clone()),
552+
verification_key: Some(bytes_for_verification_data.clone()),
553+
vm_program_code: Some(bytes_for_verification_data),
554+
proof_generator_addr,
555+
};
556+
let chain_id = U256::from(42);
557+
558+
// Here we create different entries for the batch queue.
559+
// Since we are sending with the same address, the low nonces should have higher max fees.
560+
561+
// Entry 1
562+
let nonce_1 = U256::from(1);
563+
let max_fee_1 = U256::from(1_300_000_000_000_002u128);
564+
let nonced_verification_data_1 = NoncedVerificationData::new(
565+
verification_data.clone(),
566+
nonce_1,
567+
max_fee_1,
568+
chain_id,
569+
payment_service_addr,
570+
);
571+
let vd_commitment_1: VerificationDataCommitment = nonced_verification_data_1.clone().into();
572+
let entry_1 = BatchQueueEntry::new_for_testing(
573+
nonced_verification_data_1,
574+
vd_commitment_1,
575+
dummy_signature,
576+
sender_addr,
577+
);
578+
let batch_priority_1 = BatchQueueEntryPriority::new(max_fee_1, nonce_1);
579+
580+
// Entry 2
581+
let nonce_2 = U256::from(2);
582+
let max_fee_2 = U256::from(1_300_000_000_000_001u128);
583+
let nonced_verification_data_2 = NoncedVerificationData::new(
584+
verification_data.clone(),
585+
nonce_2,
586+
max_fee_2,
587+
chain_id,
588+
payment_service_addr,
589+
);
590+
let vd_commitment_2: VerificationDataCommitment = nonced_verification_data_2.clone().into();
591+
let entry_2 = BatchQueueEntry::new_for_testing(
592+
nonced_verification_data_2,
593+
vd_commitment_2,
594+
dummy_signature,
595+
sender_addr,
596+
);
597+
let batch_priority_2 = BatchQueueEntryPriority::new(max_fee_2, nonce_2);
598+
599+
// Entry 3
600+
let nonce_3 = U256::from(3);
601+
let max_fee_3 = U256::from(1_300_000_000_000_000u128);
602+
let nonced_verification_data_3 = NoncedVerificationData::new(
603+
verification_data.clone(),
604+
nonce_3,
605+
max_fee_3,
606+
chain_id,
607+
payment_service_addr,
608+
);
609+
let vd_commitment_3: VerificationDataCommitment = nonced_verification_data_3.clone().into();
610+
let entry_3 = BatchQueueEntry::new_for_testing(
611+
nonced_verification_data_3,
612+
vd_commitment_3,
613+
dummy_signature,
614+
sender_addr,
615+
);
616+
let batch_priority_3 = BatchQueueEntryPriority::new(max_fee_3, nonce_3);
617+
618+
let mut batch_queue = BatchQueue::new();
619+
batch_queue.push(entry_1, batch_priority_1);
620+
batch_queue.push(entry_2, batch_priority_2);
621+
batch_queue.push(entry_3, batch_priority_3);
622+
623+
let gas_price = U256::from(1);
624+
625+
let max_batch_len = 2;
626+
627+
let (resulting_batch_queue, finalized_batch) =
628+
try_build_batch(batch_queue, gas_price, 5000000, max_batch_len).unwrap();
629+
630+
assert_eq!(resulting_batch_queue.len(), 1);
631+
assert_eq!(finalized_batch.len(), 2);
632+
assert_eq!(
633+
finalized_batch[0].nonced_verification_data.max_fee,
634+
max_fee_2
635+
);
636+
assert_eq!(
637+
finalized_batch[1].nonced_verification_data.max_fee,
638+
max_fee_1
639+
);
640+
}
641+
642+
}

config-files/config-batcher.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ batcher:
2121
transaction_wait_timeout: 36000 # 3 blocks
2222
max_proof_size: 67108864 # 64 MiB
2323
max_batch_size: 268435456 # 256 MiB
24+
max_batch_len: 3000 # 3000 proofs in a batch
2425
pre_verification_is_enabled: true
2526
metrics_port: 9093
2627
telemetry_ip_port_address: localhost:4001

0 commit comments

Comments
 (0)