Skip to content

Commit dd4d04f

Browse files
authored
chore: enable zero out bloom filter in update from output queue (#1555)
1 parent 88086aa commit dd4d04f

File tree

3 files changed

+71
-5
lines changed

3 files changed

+71
-5
lines changed

program-libs/batched-merkle-tree/src/batch_metadata.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,22 @@ impl BatchMetadata {
5656
&self.batches[self.currently_processing_batch_index as usize]
5757
}
5858

59+
pub fn get_previous_batch_index(&self) -> usize {
60+
if self.currently_processing_batch_index == 0 {
61+
1
62+
} else {
63+
0
64+
}
65+
}
66+
67+
pub fn get_previous_batch(&self) -> &Batch {
68+
&self.batches[self.get_previous_batch_index()]
69+
}
70+
71+
pub fn get_previous_batch_mut(&mut self) -> &mut Batch {
72+
&mut self.batches[self.get_previous_batch_index()]
73+
}
74+
5975
pub fn get_current_batch_mut(&mut self) -> &mut Batch {
6076
&mut self.batches[self.currently_processing_batch_index as usize]
6177
}

program-libs/batched-merkle-tree/src/merkle_tree.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,9 @@ impl<'a> BatchedMerkleTreeAccount<'a> {
342342
/// 3.2. Increment the number of inserted zkps.
343343
/// 3.3. If all zkps are inserted, set batch state to inserted.
344344
/// 4. Increment next full batch index if inserted.
345-
/// 5. Return the batch append event.
345+
/// 5. Zero out previous batch bloom filter of input queue
346+
/// if current batch is 50% inserted.
347+
/// 6. Return the batch append event.
346348
///
347349
/// Note: when proving inclusion by index in
348350
/// value array we need to insert the value into a bloom_filter once it is
@@ -401,8 +403,12 @@ impl<'a> BatchedMerkleTreeAccount<'a> {
401403
queue_account
402404
.batch_metadata
403405
.increment_next_full_batch_index_if_inserted(full_batch_state);
406+
// 5. Zero out previous batch bloom filter
407+
// if current batch is 50% inserted.
408+
// Needs to be executed post mark_as_inserted_in_merkle_tree.
409+
self.zero_out_previous_batch_bloom_filter()?;
404410
}
405-
// 5. Return the batch append event.
411+
// 6. Return the batch append event.
406412
Ok(BatchAppendEvent {
407413
id,
408414
batch_index: full_batch_index as u64,
@@ -756,14 +762,13 @@ impl<'a> BatchedMerkleTreeAccount<'a> {
756762
self.queue_metadata.batches[current_batch].get_num_inserted_elements();
757763
num_inserted_elements >= batch_size / 2
758764
};
759-
760765
let previous_full_batch = self
761766
.queue_metadata
762767
.batches
763768
.get_mut(previous_full_batch_index)
764769
.ok_or(BatchedMerkleTreeError::InvalidBatchIndex)?;
765-
766770
let batch_is_inserted = previous_full_batch.get_state() == BatchState::Inserted;
771+
767772
let previous_batch_is_ready =
768773
batch_is_inserted && !previous_full_batch.bloom_filter_is_zeroed();
769774

program-libs/batched-merkle-tree/tests/merkle_tree.rs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1302,6 +1302,52 @@ fn assert_merkle_tree_update(
13021302
queue_account: Option<BatchedQueueAccount>,
13031303
root: [u8; 32],
13041304
) {
1305+
let input_queue_previous_batch_state =
1306+
old_account.queue_metadata.get_previous_batch().get_state();
1307+
let input_queue_current_batch = old_account.queue_metadata.get_current_batch();
1308+
let previous_batch_index = old_account.queue_metadata.get_previous_batch_index();
1309+
let is_half_full = input_queue_current_batch.get_num_inserted_elements()
1310+
>= input_queue_current_batch.batch_size / 2;
1311+
if is_half_full
1312+
&& input_queue_previous_batch_state == BatchState::Inserted
1313+
&& !old_account
1314+
.queue_metadata
1315+
.get_previous_batch()
1316+
.bloom_filter_is_zeroed()
1317+
{
1318+
old_account
1319+
.queue_metadata
1320+
.get_previous_batch_mut()
1321+
.set_bloom_filter_to_zeroed();
1322+
old_account.bloom_filter_stores[previous_batch_index]
1323+
.iter_mut()
1324+
.for_each(|elem| {
1325+
*elem = 0;
1326+
});
1327+
let previous_full_batch = old_account
1328+
.queue_metadata
1329+
.batches
1330+
.get(previous_batch_index)
1331+
.unwrap();
1332+
let sequence_number = previous_full_batch.sequence_number;
1333+
let overlapping_roots_exits = sequence_number > old_account.sequence_number;
1334+
if overlapping_roots_exits {
1335+
let mut oldest_root_index = old_account.root_history.first_index();
1336+
// 2.1. Get, num of remaining roots.
1337+
// Remaining roots have not been updated since
1338+
// the update of the previous batch hence enable to prove
1339+
// inclusion of values nullified in the previous batch.
1340+
let num_remaining_roots = sequence_number - old_account.sequence_number;
1341+
// 2.2. Zero out roots oldest to first safe root index.
1342+
// Skip one iteration we don't need to zero out
1343+
// the first safe root.
1344+
for _ in 1..num_remaining_roots {
1345+
old_account.root_history[oldest_root_index] = [0u8; 32];
1346+
oldest_root_index += 1;
1347+
oldest_root_index %= old_account.root_history.len();
1348+
}
1349+
}
1350+
}
13051351
// Output queue update
13061352
if let Some(mut old_queue_account) = old_queue_account {
13071353
let queue_account = queue_account.unwrap();
@@ -1311,7 +1357,6 @@ fn assert_merkle_tree_update(
13111357
.batches
13121358
.get_mut(old_full_batch_index as usize)
13131359
.unwrap();
1314-
println!("old full batch {:?}", old_full_batch);
13151360
old_full_batch
13161361
.mark_as_inserted_in_merkle_tree(
13171362
account.sequence_number,

0 commit comments

Comments
 (0)