-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathbatch.rs
More file actions
1202 lines (1122 loc) · 43.9 KB
/
batch.rs
File metadata and controls
1202 lines (1122 loc) · 43.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use light_bloom_filter::{BloomFilter, BloomFilterRef};
use light_hasher::{Hasher, Poseidon};
use light_zero_copy::vec::ZeroCopyVecU64;
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::{errors::BatchedMerkleTreeError, BorshDeserialize, BorshSerialize};
#[derive(Clone, Debug, PartialEq, Eq, Copy)]
#[repr(u64)]
pub enum BatchState {
/// Batch can be filled with values.
Fill,
/// Batch has been inserted into the tree.
Inserted,
/// Batch is full.
Full,
}
impl From<u64> for BatchState {
fn from(value: u64) -> Self {
match value {
0 => BatchState::Fill,
1 => BatchState::Inserted,
2 => BatchState::Full,
_ => panic!("Invalid BatchState value"),
}
}
}
impl From<BatchState> for u64 {
fn from(val: BatchState) -> Self {
val as u64
}
}
/// Batch structure that holds
/// the metadata and state of a batch.
///
/// A batch:
/// - has a size and a number of zkp batches.
/// - size must be divisible by zkp batch size.
/// - is part of a queue, each queue has two batches.
/// - is inserted into the tree by zkp batch.
#[repr(C)]
#[derive(
Clone,
Copy,
Debug,
PartialEq,
Eq,
KnownLayout,
Immutable,
IntoBytes,
FromBytes,
Default,
BorshSerialize,
BorshDeserialize,
)]
pub struct Batch {
/// Number of inserted elements in the zkp batch.
num_inserted: u64,
state: u64,
/// Number of full zkp batches in the batch,
/// that are ready to be inserted into the tree.
pub(crate) num_full_zkp_batches: u64,
/// Number zkp batches that are inserted into the tree.
num_inserted_zkp_batches: u64,
/// Number of iterations for the bloom_filter.
pub num_iters: u64,
/// Theoretical capacity of the bloom_filter in bits.
/// We want to make it much larger
/// than batch_size to avoid false positives.
pub bloom_filter_capacity: u64,
/// Number of elements in a batch.
pub batch_size: u64,
/// Number of elements in a zkp batch.
/// A batch consists out of one or more zkp batches.
pub zkp_batch_size: u64,
/// Sequence number when it is save to clear the batch without advancing to
/// the saved root index.
pub sequence_number: u64,
/// Start leaf index of the first
pub start_index: u64,
/// Slot of the first insertion into the batch.
/// Indexers can use this slot to reindex inserted elements.
/// Is not used for the batch itself.
pub start_slot: u64,
pub root_index: u32,
start_slot_is_set: u8,
bloom_filter_is_zeroed: u8,
_padding: [u8; 2],
}
impl Batch {
pub fn new(
num_iters: u64,
bloom_filter_capacity: u64,
batch_size: u64,
zkp_batch_size: u64,
start_index: u64,
) -> Self {
Batch {
num_iters,
bloom_filter_capacity,
batch_size,
num_inserted: 0,
state: BatchState::Fill.into(),
zkp_batch_size,
num_full_zkp_batches: 0,
num_inserted_zkp_batches: 0,
sequence_number: 0,
root_index: 0,
start_index,
start_slot: 0,
start_slot_is_set: 0,
bloom_filter_is_zeroed: 0,
_padding: [0u8; 2],
}
}
/// Returns the state of the batch.
pub fn get_state(&self) -> BatchState {
self.state.into()
}
pub fn bloom_filter_is_zeroed(&self) -> bool {
self.bloom_filter_is_zeroed == 1
}
pub fn set_bloom_filter_to_zeroed(&mut self) {
// 1 if bloom filter is zeroed
// 0 if bloom filter is not zeroed
self.bloom_filter_is_zeroed = 1;
}
pub fn set_bloom_filter_to_not_zeroed(&mut self) {
// 1 if bloom filter is zeroed
// 0 if bloom filter is not zeroed
self.bloom_filter_is_zeroed = 0;
}
pub fn start_slot_is_set(&self) -> bool {
self.start_slot_is_set == 1
}
pub fn set_start_slot(&mut self, start_slot: &u64) {
if !self.start_slot_is_set() {
self.start_slot = *start_slot;
self.start_slot_is_set = 1;
}
}
/// fill -> full -> inserted -> fill
/// (from tree insertion perspective is pending if fill or full)
pub fn advance_state_to_fill(
&mut self,
start_index: Option<u64>,
) -> Result<(), BatchedMerkleTreeError> {
if self.get_state() == BatchState::Inserted {
self.state = BatchState::Fill.into();
self.set_bloom_filter_to_not_zeroed();
self.sequence_number = 0;
self.root_index = 0;
self.num_inserted_zkp_batches = 0;
self.start_slot_is_set = 0;
self.start_slot = 0;
if let Some(start_index) = start_index {
self.start_index = start_index;
}
self.num_full_zkp_batches = 0;
} else {
#[cfg(feature = "solana")]
solana_msg::msg!(
"Batch is in incorrect state {} expected BatchState::Inserted 1",
self.state
);
return Err(BatchedMerkleTreeError::BatchNotReady);
}
Ok(())
}
/// fill -> full -> inserted -> fill
/// (from tree insertion perspective is pending if fill or full)
pub fn advance_state_to_inserted(&mut self) -> Result<(), BatchedMerkleTreeError> {
if self.get_state() == BatchState::Full {
self.state = BatchState::Inserted.into();
} else {
#[cfg(feature = "solana")]
solana_msg::msg!(
"Batch is in incorrect state {} expected BatchState::Full 2",
self.state
);
return Err(BatchedMerkleTreeError::BatchNotReady);
}
Ok(())
}
/// fill -> full -> inserted -> fill
/// (from tree insertion perspective is pending if fill or full)
pub fn advance_state_to_full(&mut self) -> Result<(), BatchedMerkleTreeError> {
if self.get_state() == BatchState::Fill {
self.state = BatchState::Full.into();
} else {
#[cfg(feature = "solana")]
solana_msg::msg!(
"Batch is in incorrect state {} expected BatchState::Fill 0",
self.state
);
return Err(BatchedMerkleTreeError::BatchNotReady);
}
Ok(())
}
pub fn get_first_ready_zkp_batch(&self) -> Result<u64, BatchedMerkleTreeError> {
if self.get_state() == BatchState::Inserted {
Err(BatchedMerkleTreeError::BatchAlreadyInserted)
} else if self.batch_is_ready_to_insert() {
Ok(self.num_inserted_zkp_batches)
} else {
Err(BatchedMerkleTreeError::BatchNotReady)
}
}
pub fn batch_is_ready_to_insert(&self) -> bool {
self.num_full_zkp_batches > self.num_inserted_zkp_batches
}
/// Returns the number of zkp batch updates
/// that are ready to be inserted into the tree.
pub fn get_num_ready_zkp_updates(&self) -> u64 {
self.num_full_zkp_batches
.saturating_sub(self.num_inserted_zkp_batches)
}
/// Returns the number of inserted elements
/// in the current zkp batch.
pub fn get_num_inserted_zkp_batch(&self) -> u64 {
self.num_inserted
}
/// Returns the current zkp batch index.
/// New values are inserted into the current zkp batch.
pub fn get_current_zkp_batch_index(&self) -> u64 {
self.num_full_zkp_batches
}
/// Returns the number of inserted zkps.
pub fn get_num_inserted_zkps(&self) -> u64 {
self.num_inserted_zkp_batches
}
/// Returns the number of elements inserted into the tree.
pub fn get_num_elements_inserted_into_tree(&self) -> u64 {
self.num_inserted_zkp_batches * self.zkp_batch_size
}
/// Returns the number of inserted elements in the batch.
pub fn get_num_inserted_elements(&self) -> u64 {
self.num_full_zkp_batches * self.zkp_batch_size + self.num_inserted
}
/// Returns the number of zkp batches in the batch.
pub fn get_num_zkp_batches(&self) -> u64 {
self.batch_size / self.zkp_batch_size
}
/// Returns the number of the hash_chain stores.
pub fn get_num_hash_chain_store(&self) -> usize {
self.get_num_zkp_batches() as usize
}
/// Returns the index of a value by leaf index in the value store,
/// provided it does exist in the batch.
pub fn get_value_index_in_batch(&self, leaf_index: u64) -> Result<u64, BatchedMerkleTreeError> {
self.check_leaf_index_exists(leaf_index)?;
let index = leaf_index
.checked_sub(self.start_index)
.ok_or(BatchedMerkleTreeError::LeafIndexNotInBatch)?;
Ok(index)
}
/// Stores the value in a value store,
/// and adds the value to the current hash chain.
pub fn store_and_hash_value(
&mut self,
value: &[u8; 32],
value_store: &mut ZeroCopyVecU64<[u8; 32]>,
hash_chain_store: &mut ZeroCopyVecU64<[u8; 32]>,
start_slot: &u64,
) -> Result<(), BatchedMerkleTreeError> {
self.set_start_slot(start_slot);
self.add_to_hash_chain(value, hash_chain_store)?;
value_store.push(*value)?;
Ok(())
}
/// Insert into the bloom filter and
/// add value to current hash chain.
/// (used by nullifier & address queues)
/// 1. set start slot
/// 2. Add value to hash chain.
/// 3. Insert value into the bloom filter at bloom_filter_index.
/// 4. Check that value is not in any other bloom filter.
pub fn insert(
&mut self,
bloom_filter_value: &[u8; 32],
hash_chain_value: &[u8; 32],
bloom_filter_stores: &mut [&mut [u8]],
hash_chain_store: &mut ZeroCopyVecU64<[u8; 32]>,
bloom_filter_index: usize,
start_slot: &u64,
) -> Result<(), BatchedMerkleTreeError> {
// 1. set start slot if not set.
self.set_start_slot(start_slot);
// 2. add value to hash chain
self.add_to_hash_chain(hash_chain_value, hash_chain_store)?;
// insert into bloom filter & check non inclusion
{
let other_bloom_filter_index = if bloom_filter_index == 0 { 1 } else { 0 };
// 3. Insert value into the bloom filter at bloom_filter_index.
BloomFilter::new(
self.num_iters as usize,
self.bloom_filter_capacity,
bloom_filter_stores[bloom_filter_index],
)?
.insert(bloom_filter_value)?;
// 4. Check that value is not in any other bloom filter.
Self::check_non_inclusion(
self.num_iters as usize,
self.bloom_filter_capacity,
bloom_filter_value,
bloom_filter_stores[other_bloom_filter_index],
)?;
}
Ok(())
}
/// Add a value to the current hash chain, and advance batch state.
/// 1. Check that the batch is ready.
/// 2. If the zkp batch is empty, start a new hash chain.
/// 3. If the zkp batch is not empty, add value to last hash chain.
/// 4. If the zkp batch is full, increment the zkp batch index.
/// 5. If all zkp batches are full, set batch state to full.
pub fn add_to_hash_chain(
&mut self,
value: &[u8; 32],
hash_chain_store: &mut ZeroCopyVecU64<[u8; 32]>,
) -> Result<(), BatchedMerkleTreeError> {
// 1. Check that the batch is ready.
if self.get_state() != BatchState::Fill {
return Err(BatchedMerkleTreeError::BatchNotReady);
}
let start_new_hash_chain = self.num_inserted == 0;
if start_new_hash_chain {
// 2. Start a new hash chain.
hash_chain_store.push(*value)?;
} else if let Some(last_hash_chain) = hash_chain_store.last_mut() {
// 3. Add value to last hash chain.
let hash_chain = Poseidon::hashv(&[last_hash_chain, value.as_slice()])?;
*last_hash_chain = hash_chain;
} else {
unreachable!();
}
self.num_inserted += 1;
// 4. If the zkp batch is full, increment the zkp batch index.
let zkp_batch_is_full = self.num_inserted == self.zkp_batch_size;
if zkp_batch_is_full {
self.num_full_zkp_batches += 1;
// To start a new hash chain in the next insertion
// set num inserted to zero.
self.num_inserted = 0;
// 5. If all zkp batches are full, set batch state to full.
let batch_is_full = self.num_full_zkp_batches == self.get_num_zkp_batches();
if batch_is_full {
self.advance_state_to_full()?;
}
}
Ok(())
}
/// Checks that value is not in the bloom filter.
pub fn check_non_inclusion(
num_iters: usize,
bloom_filter_capacity: u64,
value: &[u8; 32],
store: &mut [u8],
) -> Result<(), BatchedMerkleTreeError> {
let mut bloom_filter = BloomFilter::new(num_iters, bloom_filter_capacity, store)?;
if bloom_filter.contains(value) {
return Err(BatchedMerkleTreeError::NonInclusionCheckFailed);
}
Ok(())
}
/// Immutable version of `check_non_inclusion` using `BloomFilterRef`.
pub fn check_non_inclusion_ref(
num_iters: usize,
bloom_filter_capacity: u64,
value: &[u8; 32],
store: &[u8],
) -> Result<(), BatchedMerkleTreeError> {
let bloom_filter = BloomFilterRef::new(num_iters, bloom_filter_capacity, store)?;
if bloom_filter.contains(value) {
return Err(BatchedMerkleTreeError::NonInclusionCheckFailed);
}
Ok(())
}
/// Marks the batch as inserted in the merkle tree.
/// 1. Checks that the batch is ready.
/// 2. increments the number of inserted zkps.
/// 3. If all zkps are inserted, sets the state to inserted.
/// 4. Returns the updated state of the batch.
pub fn mark_as_inserted_in_merkle_tree(
&mut self,
sequence_number: u64,
root_index: u32,
root_history_length: u32,
) -> Result<BatchState, BatchedMerkleTreeError> {
// 1. Check that batch is ready.
self.get_first_ready_zkp_batch()?;
let num_zkp_batches = self.get_num_zkp_batches();
// 2. increments the number of inserted zkps.
self.num_inserted_zkp_batches += 1;
// 3. If all zkp batches are inserted, sets the state to inserted.
let batch_is_completely_inserted = self.num_inserted_zkp_batches == num_zkp_batches;
if batch_is_completely_inserted {
self.advance_state_to_inserted()?;
// Saving sequence number and root index for the batch.
// When the batch is cleared check that sequence number is greater or equal than self.sequence_number
// if not advance current root index to root index
self.sequence_number = sequence_number + root_history_length as u64;
self.root_index = root_index;
}
Ok(self.get_state())
}
pub fn check_leaf_index_exists(&self, leaf_index: u64) -> Result<(), BatchedMerkleTreeError> {
if !self.leaf_index_exists(leaf_index) {
return Err(BatchedMerkleTreeError::LeafIndexNotInBatch);
}
Ok(())
}
/// Returns true if value of leaf index could exist in batch.
/// `True` doesn't mean that the value exists in the batch,
/// just that it is possible. The value might already be spent
/// or never have been inserted in case an invalid index was provided.
pub fn leaf_index_exists(&self, leaf_index: u64) -> bool {
let next_batch_leaf_index = self.get_num_inserted_elements() + self.start_index;
let min_batch_leaf_index = self.start_index;
leaf_index < next_batch_leaf_index && leaf_index >= min_batch_leaf_index
}
}
#[cfg(test)]
mod tests {
use light_compressed_account::{pubkey::Pubkey, QueueType};
use light_merkle_tree_metadata::queue::QueueMetadata;
use super::*;
use crate::queue::BatchedQueueAccount;
fn get_test_batch() -> Batch {
Batch::new(3, 160_000, 500, 100, 0)
}
/// simulate zkp batch insertion
fn test_mark_as_inserted(mut batch: Batch) {
let mut sequence_number = 10;
let mut root_index = 20;
let root_history_length = 23;
let current_slot = 1;
for i in 0..batch.get_num_zkp_batches() {
sequence_number += i;
root_index += i as u32;
batch
.mark_as_inserted_in_merkle_tree(sequence_number, root_index, root_history_length)
.unwrap();
if i != batch.get_num_zkp_batches() - 1 {
assert_eq!(batch.get_state(), BatchState::Full);
assert_eq!(batch.get_num_inserted_zkp_batch(), 0);
assert_eq!(batch.get_current_zkp_batch_index(), 5);
assert_eq!(batch.get_num_inserted_zkps(), i + 1);
} else {
assert_eq!(batch.get_state(), BatchState::Inserted);
assert_eq!(batch.get_num_inserted_zkp_batch(), 0);
assert_eq!(batch.get_current_zkp_batch_index(), 5);
assert_eq!(batch.get_num_inserted_zkps(), i + 1);
}
}
assert_eq!(batch.get_state(), BatchState::Inserted);
assert_eq!(batch.get_num_inserted_zkp_batch(), 0);
let mut ref_batch = get_test_batch();
ref_batch.state = BatchState::Inserted.into();
ref_batch.root_index = root_index;
ref_batch.sequence_number = sequence_number + root_history_length as u64;
ref_batch.num_inserted_zkp_batches = 5;
ref_batch.start_slot = current_slot;
ref_batch.start_slot_is_set = 1;
ref_batch.num_full_zkp_batches = 5;
assert_eq!(batch, ref_batch);
batch.advance_state_to_fill(Some(1)).unwrap();
let mut ref_batch = get_test_batch();
ref_batch.start_index = 1;
assert_eq!(batch, ref_batch);
}
#[test]
fn test_store_value() {
let mut batch = get_test_batch();
let current_slot = 1;
let mut value_store_bytes =
vec![0u8; ZeroCopyVecU64::<[u8; 32]>::required_size_for_capacity(batch.batch_size)];
let mut value_store =
ZeroCopyVecU64::new(batch.batch_size, &mut value_store_bytes).unwrap();
let mut hash_chain_store_bytes = vec![
0u8;
ZeroCopyVecU64::<[u8; 32]>::required_size_for_capacity(
batch.get_num_hash_chain_store() as u64
)
];
let mut hash_chain_store = ZeroCopyVecU64::new(
batch.get_num_hash_chain_store() as u64,
hash_chain_store_bytes.as_mut_slice(),
)
.unwrap();
let mut ref_batch = get_test_batch();
for i in 0..batch.batch_size {
if i == 0 {
ref_batch.start_slot = current_slot;
ref_batch.start_slot_is_set = 1;
}
ref_batch.num_inserted %= ref_batch.zkp_batch_size;
let mut value = [0u8; 32];
value[24..].copy_from_slice(&i.to_be_bytes());
assert!(batch
.store_and_hash_value(
&value,
&mut value_store,
&mut hash_chain_store,
¤t_slot
)
.is_ok());
ref_batch.num_inserted += 1;
if ref_batch.num_inserted == ref_batch.zkp_batch_size {
ref_batch.num_full_zkp_batches += 1;
ref_batch.num_inserted = 0;
}
if ref_batch.num_full_zkp_batches == ref_batch.get_num_zkp_batches() {
ref_batch.state = BatchState::Full.into();
ref_batch.num_inserted = 0;
}
assert_eq!(batch, ref_batch);
assert_eq!(*value_store.get(i as usize).unwrap(), value);
}
let result = batch.store_and_hash_value(
&[1u8; 32],
&mut value_store,
&mut hash_chain_store,
¤t_slot,
);
assert_eq!(result.unwrap_err(), BatchedMerkleTreeError::BatchNotReady);
assert_eq!(batch.get_state(), BatchState::Full);
assert_eq!(batch.get_num_inserted_zkp_batch(), 0);
assert_eq!(batch.get_current_zkp_batch_index(), 5);
assert_eq!(batch.get_num_zkp_batches(), 5);
assert_eq!(batch.get_num_inserted_zkps(), 0);
test_mark_as_inserted(batch);
}
#[test]
fn test_insert() {
// Behavior Input queue
let mut batch = get_test_batch();
let mut current_slot = 1;
let mut stores = vec![vec![0u8; 20_000]; 2];
let mut bloom_filter_stores = stores
.iter_mut()
.map(|store| &mut store[..])
.collect::<Vec<_>>();
let mut hash_chain_store_bytes = vec![
0u8;
ZeroCopyVecU64::<[u8; 32]>::required_size_for_capacity(
batch.get_num_hash_chain_store() as u64
)
];
ZeroCopyVecU64::<[u8; 32]>::new(
batch.get_num_hash_chain_store() as u64,
hash_chain_store_bytes.as_mut_slice(),
)
.unwrap();
let mut ref_batch = get_test_batch();
for processing_index in 0..=1 {
for i in 0..(batch.batch_size / 2) {
let i = i + (batch.batch_size / 2) * (processing_index as u64);
if i == 0 && processing_index == 0 {
assert_eq!(batch.start_slot, 0);
assert_eq!(batch.start_slot_is_set, 0);
ref_batch.start_slot = current_slot;
ref_batch.start_slot_is_set = 1;
} else {
assert_eq!(batch.start_slot, 1);
assert_eq!(batch.start_slot_is_set, 1);
}
ref_batch.num_inserted %= ref_batch.zkp_batch_size;
let mut hash_chain_store =
ZeroCopyVecU64::<[u8; 32]>::from_bytes(hash_chain_store_bytes.as_mut_slice())
.unwrap();
let mut value = [0u8; 32];
value[24..].copy_from_slice(&i.to_be_bytes());
#[allow(clippy::manual_is_multiple_of)]
let ref_hash_chain = if i % batch.zkp_batch_size == 0 {
value
} else {
Poseidon::hashv(&[hash_chain_store.last().unwrap(), &value]).unwrap()
};
let result = batch.insert(
&value,
&value,
bloom_filter_stores.as_mut_slice(),
&mut hash_chain_store,
processing_index,
¤t_slot,
);
// First insert should succeed
assert!(result.is_ok(), "Failed result: {:?}", result);
assert_eq!(*hash_chain_store.last().unwrap(), ref_hash_chain);
{
let mut cloned_hash_chain_store = hash_chain_store_bytes.clone();
let mut hash_chain_store = ZeroCopyVecU64::<[u8; 32]>::from_bytes(
cloned_hash_chain_store.as_mut_slice(),
)
.unwrap();
let mut batch = batch;
// Reinsert should fail
assert!(batch
.insert(
&value,
&value,
bloom_filter_stores.as_mut_slice(),
&mut hash_chain_store,
processing_index,
¤t_slot
)
.is_err());
}
let mut bloom_filter = BloomFilter {
num_iters: batch.num_iters as usize,
capacity: batch.bloom_filter_capacity,
store: bloom_filter_stores[processing_index],
};
assert!(bloom_filter.contains(&value));
let other_index = if processing_index == 0 { 1 } else { 0 };
Batch::check_non_inclusion(
batch.num_iters as usize,
batch.bloom_filter_capacity,
&value,
bloom_filter_stores[other_index],
)
.unwrap();
Batch::check_non_inclusion(
batch.num_iters as usize,
batch.bloom_filter_capacity,
&value,
bloom_filter_stores[processing_index],
)
.unwrap_err();
ref_batch.num_inserted += 1;
if ref_batch.num_inserted == ref_batch.zkp_batch_size {
ref_batch.num_full_zkp_batches += 1;
ref_batch.num_inserted = 0;
}
if i == batch.batch_size - 1 {
ref_batch.state = BatchState::Full.into();
ref_batch.num_inserted = 0;
}
assert_eq!(batch, ref_batch);
current_slot += 1;
}
}
test_mark_as_inserted(batch);
}
#[test]
fn test_add_to_hash_chain() {
let mut batch = get_test_batch();
let mut hash_chain_store_bytes = vec![
0u8;
ZeroCopyVecU64::<[u8; 32]>::required_size_for_capacity(
batch.get_num_hash_chain_store() as u64
)
];
let mut hash_chain_store = ZeroCopyVecU64::<[u8; 32]>::new(
batch.get_num_hash_chain_store() as u64,
hash_chain_store_bytes.as_mut_slice(),
)
.unwrap();
let value = [1u8; 32];
assert!(batch
.add_to_hash_chain(&value, &mut hash_chain_store)
.is_ok());
let mut ref_batch = get_test_batch();
let user_hash_chain = value;
ref_batch.num_inserted = 1;
assert_eq!(batch, ref_batch);
assert_eq!(hash_chain_store[0], user_hash_chain);
let value = [2u8; 32];
let ref_hash_chain = Poseidon::hashv(&[&user_hash_chain, &value]).unwrap();
assert!(batch
.add_to_hash_chain(&value, &mut hash_chain_store)
.is_ok());
ref_batch.num_inserted = 2;
assert_eq!(batch, ref_batch);
assert_eq!(hash_chain_store[0], ref_hash_chain);
}
#[test]
fn test_check_non_inclusion() {
let mut current_slot = 1;
for processing_index in 0..=1 {
let mut batch = get_test_batch();
let value = [1u8; 32];
let mut stores = vec![vec![0u8; 20_000]; 2];
let mut bloom_filter_stores = stores
.iter_mut()
.map(|store| &mut store[..])
.collect::<Vec<_>>();
let mut hash_chain_store_bytes = vec![
0u8;
ZeroCopyVecU64::<[u8; 32]>::required_size_for_capacity(
batch.get_num_hash_chain_store() as u64
)
];
let mut hash_chain_store = ZeroCopyVecU64::<[u8; 32]>::new(
batch.get_num_hash_chain_store() as u64,
hash_chain_store_bytes.as_mut_slice(),
)
.unwrap();
assert_eq!(
Batch::check_non_inclusion(
batch.num_iters as usize,
batch.bloom_filter_capacity,
&value,
bloom_filter_stores[processing_index]
),
Ok(())
);
let ref_batch = get_test_batch();
assert_eq!(batch, ref_batch);
batch
.insert(
&value,
&value,
bloom_filter_stores.as_mut_slice(),
&mut hash_chain_store,
processing_index,
¤t_slot,
)
.unwrap();
current_slot += 1;
assert!(Batch::check_non_inclusion(
batch.num_iters as usize,
batch.bloom_filter_capacity,
&value,
bloom_filter_stores[processing_index]
)
.is_err());
let other_index = if processing_index == 0 { 1 } else { 0 };
assert!(Batch::check_non_inclusion(
batch.num_iters as usize,
batch.bloom_filter_capacity,
&value,
bloom_filter_stores[other_index]
)
.is_ok());
}
}
#[test]
fn test_getters() {
let mut batch = get_test_batch();
assert_eq!(batch.get_num_zkp_batches(), 5);
assert_eq!(batch.get_num_hash_chain_store(), 5);
assert_eq!(batch.get_state(), BatchState::Fill);
assert_eq!(batch.get_num_inserted_zkp_batch(), 0);
assert_eq!(batch.get_current_zkp_batch_index(), 0);
assert_eq!(batch.get_num_inserted_zkps(), 0);
batch.advance_state_to_full().unwrap();
assert_eq!(batch.get_state(), BatchState::Full);
batch.advance_state_to_inserted().unwrap();
assert_eq!(batch.get_state(), BatchState::Inserted);
}
/// Tests:
/// 1. Failing test lowest value in eligble range - 1
/// 2. Functional test lowest value in eligble range
/// 3. Functional test highest value in eligble range
/// 4. Failing test eligble range + 1
#[test]
fn test_value_is_inserted_in_batch() {
let mut batch = get_test_batch();
batch.advance_state_to_full().unwrap();
batch.advance_state_to_inserted().unwrap();
batch.start_index = 1;
batch.num_inserted = 5;
let lowest_eligible_value = batch.start_index;
let highest_eligible_value = batch.start_index + batch.get_num_inserted_elements() - 1;
// 1. Failing test lowest value in eligible range - 1
assert!(!batch.leaf_index_exists(lowest_eligible_value - 1));
// 2. Functional test lowest value in eligible range
assert!(batch.leaf_index_exists(lowest_eligible_value));
// 3. Functional test highest value in eligible range
assert!(batch.leaf_index_exists(highest_eligible_value));
// 4. Failing test eligible range + 1
assert!(!batch.leaf_index_exists(highest_eligible_value + 1));
}
/// 1. Failing: empty batch
/// 2. Functional: if zkp batch size is full else failing
/// 3. Failing: batch is completely inserted
#[test]
fn test_can_insert_batch() {
let mut batch = get_test_batch();
let mut current_slot = 1;
assert_eq!(
batch.get_first_ready_zkp_batch(),
Err(BatchedMerkleTreeError::BatchNotReady)
);
let mut value_store_bytes =
vec![0u8; ZeroCopyVecU64::<[u8; 32]>::required_size_for_capacity(batch.batch_size)];
let mut value_store =
ZeroCopyVecU64::<[u8; 32]>::new(batch.batch_size, &mut value_store_bytes).unwrap();
let mut hash_chain_store_bytes = vec![
0u8;
ZeroCopyVecU64::<[u8; 32]>::required_size_for_capacity(
batch.get_num_hash_chain_store() as u64
)
];
let mut hash_chain_store = ZeroCopyVecU64::<[u8; 32]>::new(
batch.get_num_hash_chain_store() as u64,
hash_chain_store_bytes.as_mut_slice(),
)
.unwrap();
for i in 0..batch.batch_size + 10 {
let mut value = [0u8; 32];
value[24..].copy_from_slice(&i.to_be_bytes());
if i < batch.batch_size {
batch
.store_and_hash_value(
&value,
&mut value_store,
&mut hash_chain_store,
¤t_slot,
)
.unwrap();
}
#[allow(clippy::manual_is_multiple_of)]
if (i + 1) % batch.zkp_batch_size == 0 && i != 0 {
assert_eq!(
batch.get_first_ready_zkp_batch().unwrap(),
i / batch.zkp_batch_size
);
batch.mark_as_inserted_in_merkle_tree(0, 0, 0).unwrap();
} else if i >= batch.batch_size {
assert_eq!(
batch.get_first_ready_zkp_batch(),
Err(BatchedMerkleTreeError::BatchAlreadyInserted)
);
} else {
assert_eq!(
batch.get_first_ready_zkp_batch(),
Err(BatchedMerkleTreeError::BatchNotReady)
);
}
current_slot += 1;
}
}
#[test]
fn test_get_state() {
let mut batch = get_test_batch();
assert_eq!(batch.get_state(), BatchState::Fill);
{
let result = batch.advance_state_to_inserted();
assert_eq!(result, Err(BatchedMerkleTreeError::BatchNotReady));
let result = batch.advance_state_to_fill(None);
assert_eq!(result, Err(BatchedMerkleTreeError::BatchNotReady));
}
batch.advance_state_to_full().unwrap();
assert_eq!(batch.get_state(), BatchState::Full);
{
let result = batch.advance_state_to_full();
assert_eq!(result, Err(BatchedMerkleTreeError::BatchNotReady));
let result = batch.advance_state_to_fill(None);
assert_eq!(result, Err(BatchedMerkleTreeError::BatchNotReady));
}
batch.advance_state_to_inserted().unwrap();
assert_eq!(batch.get_state(), BatchState::Inserted);
}
#[test]
fn test_bloom_filter_is_zeroed() {
let mut batch = get_test_batch();
assert!(!batch.bloom_filter_is_zeroed());
batch.set_bloom_filter_to_zeroed();
assert!(batch.bloom_filter_is_zeroed());
batch.set_bloom_filter_to_not_zeroed();
assert!(!batch.bloom_filter_is_zeroed());
}
#[test]
fn test_num_ready_zkp_updates() {
let mut batch = get_test_batch();
assert_eq!(batch.get_num_ready_zkp_updates(), 0);
batch.num_full_zkp_batches = 1;
assert_eq!(batch.get_num_ready_zkp_updates(), 1);
batch.num_inserted_zkp_batches = 1;
assert_eq!(batch.get_num_ready_zkp_updates(), 0);
batch.num_full_zkp_batches = 2;
assert_eq!(batch.get_num_ready_zkp_updates(), 1);
}
#[test]
fn test_get_num_inserted_elements() {
let mut batch = get_test_batch();
assert_eq!(batch.get_num_inserted_elements(), 0);
let mut hash_chain_bytes = vec![0u8; 32 * batch.batch_size as usize];
let mut hash_chain_store = ZeroCopyVecU64::<[u8; 32]>::new(
batch.get_num_zkp_batches(),
hash_chain_bytes.as_mut_slice(),
)
.unwrap();
for i in 0..batch.batch_size {
let mut value = [0u8; 32];
value[24..].copy_from_slice(&i.to_be_bytes());
batch
.add_to_hash_chain(&value, &mut hash_chain_store)
.unwrap();
assert_eq!(batch.get_num_inserted_elements(), i + 1);
}
}
#[test]
fn test_get_num_elements_inserted_into_tree() {
let mut batch = get_test_batch();
assert_eq!(batch.get_num_elements_inserted_into_tree(), 0);
for i in 0..batch.get_num_zkp_batches() {
#[allow(clippy::manual_is_multiple_of)]
if i % batch.zkp_batch_size == 0 {
batch.num_full_zkp_batches += 1;
batch
.mark_as_inserted_in_merkle_tree(i, i as u32, 0)
.unwrap();
assert_eq!(
batch.get_num_elements_inserted_into_tree(),
(i + 1) * batch.zkp_batch_size
);
}
}
}
// Moved BatchedQueueAccount test to this file
// to modify private Batch variables for assertions.
#[test]
fn test_get_num_inserted() {
let mut account_data = vec![0u8; 1000];
let mut queue_metadata = QueueMetadata::default();
let associated_merkle_tree = Pubkey::new_unique();
queue_metadata.associated_merkle_tree = associated_merkle_tree;
queue_metadata.queue_type = QueueType::OutputStateV2 as u64;
let batch_size = 4;
let zkp_batch_size = 2;
let bloom_filter_capacity = 0;
let num_iters = 0;
let mut current_slot = 1;