@@ -517,6 +517,10 @@ impl From<&ZInstructionDataInvokeCpi<'_>> for InstructionDataInvokeCpi {
517
517
#[ cfg( test) ]
518
518
mod test {
519
519
use borsh:: BorshSerialize ;
520
+ use rand:: {
521
+ rngs:: { StdRng , ThreadRng } ,
522
+ Rng ,
523
+ } ;
520
524
521
525
use super :: * ;
522
526
use crate :: {
@@ -548,6 +552,33 @@ mod test {
548
552
}
549
553
}
550
554
555
+ fn get_rnd_instruction_data_invoke_cpi ( rng : & mut StdRng ) -> InstructionDataInvokeCpi {
556
+ InstructionDataInvokeCpi {
557
+ proof : Some ( CompressedProof {
558
+ a : rng. gen ( ) ,
559
+ b : ( 0 ..64 )
560
+ . map ( |_| rng. gen ( ) )
561
+ . collect :: < Vec < u8 > > ( )
562
+ . try_into ( )
563
+ . unwrap ( ) ,
564
+ c : rng. gen ( ) ,
565
+ } ) ,
566
+ new_address_params : vec ! [ get_rnd_new_address_params( rng) ; rng. gen_range( 0 ..10 ) ] ,
567
+ input_compressed_accounts_with_merkle_context : vec ! [
568
+ get_rnd_test_input_account( rng) ;
569
+ rng. gen_range( 0 ..10 )
570
+ ] ,
571
+ output_compressed_accounts : vec ! [
572
+ get_rnd_test_output_account( rng) ;
573
+ rng. gen_range( 0 ..10 )
574
+ ] ,
575
+ relay_fee : None ,
576
+ compress_or_decompress_lamports : rng. gen ( ) ,
577
+ is_compress : rng. gen ( ) ,
578
+ cpi_context : Some ( get_rnd_cpi_context ( rng) ) ,
579
+ }
580
+ }
581
+
551
582
fn compare_invoke_cpi_instruction_data (
552
583
reference : & InstructionDataInvokeCpi ,
553
584
z_copy : & ZInstructionDataInvokeCpi ,
@@ -683,6 +714,14 @@ mod test {
683
714
}
684
715
}
685
716
717
+ fn get_rnd_cpi_context ( rng : & mut StdRng ) -> CompressedCpiContext {
718
+ CompressedCpiContext {
719
+ first_set_context : rng. gen ( ) ,
720
+ set_context : rng. gen ( ) ,
721
+ cpi_context_account_index : rng. gen ( ) ,
722
+ }
723
+ }
724
+
686
725
#[ test]
687
726
fn test_cpi_context_deserialize ( ) {
688
727
let cpi_context = get_cpi_context ( ) ;
@@ -713,6 +752,14 @@ mod test {
713
752
}
714
753
}
715
754
755
+ fn get_rnd_test_account_data ( rng : & mut StdRng ) -> CompressedAccountData {
756
+ CompressedAccountData {
757
+ discriminator : rng. gen ( ) ,
758
+ data : ( 0 ..100 ) . map ( |_| rng. gen ( ) ) . collect :: < Vec < u8 > > ( ) ,
759
+ data_hash : rng. gen ( ) ,
760
+ }
761
+ }
762
+
716
763
fn get_test_account ( ) -> CompressedAccount {
717
764
CompressedAccount {
718
765
owner : solana_program:: pubkey:: Pubkey :: new_unique ( ) ,
@@ -722,13 +769,29 @@ mod test {
722
769
}
723
770
}
724
771
772
+ fn get_rnd_test_account ( rng : & mut StdRng ) -> CompressedAccount {
773
+ CompressedAccount {
774
+ owner : solana_program:: pubkey:: Pubkey :: new_unique ( ) ,
775
+ lamports : rng. gen ( ) ,
776
+ address : Some ( Pubkey :: new_unique ( ) . to_bytes ( ) ) ,
777
+ data : Some ( get_rnd_test_account_data ( rng) ) ,
778
+ }
779
+ }
780
+
725
781
fn get_test_output_account ( ) -> OutputCompressedAccountWithPackedContext {
726
782
OutputCompressedAccountWithPackedContext {
727
783
compressed_account : get_test_account ( ) ,
728
784
merkle_tree_index : 1 ,
729
785
}
730
786
}
731
787
788
+ fn get_rnd_test_output_account ( rng : & mut StdRng ) -> OutputCompressedAccountWithPackedContext {
789
+ OutputCompressedAccountWithPackedContext {
790
+ compressed_account : get_rnd_test_account ( rng) ,
791
+ merkle_tree_index : rng. gen ( ) ,
792
+ }
793
+ }
794
+
732
795
#[ test]
733
796
fn test_output_account_deserialize ( ) {
734
797
let test_output_account = get_test_output_account ( ) ;
@@ -774,6 +837,25 @@ mod test {
774
837
read_only : false ,
775
838
}
776
839
}
840
+
841
+ fn get_rnd_test_input_account ( rng : & mut StdRng ) -> PackedCompressedAccountWithMerkleContext {
842
+ PackedCompressedAccountWithMerkleContext {
843
+ compressed_account : CompressedAccount {
844
+ owner : solana_program:: pubkey:: Pubkey :: new_unique ( ) ,
845
+ lamports : 100 ,
846
+ address : Some ( Pubkey :: new_unique ( ) . to_bytes ( ) ) ,
847
+ data : Some ( get_rnd_test_account_data ( rng) ) ,
848
+ } ,
849
+ merkle_context : PackedMerkleContext {
850
+ merkle_tree_pubkey_index : rng. gen ( ) ,
851
+ nullifier_queue_pubkey_index : rng. gen ( ) ,
852
+ leaf_index : rng. gen ( ) ,
853
+ prove_by_index : rng. gen ( ) ,
854
+ } ,
855
+ root_index : rng. gen ( ) ,
856
+ read_only : false ,
857
+ }
858
+ }
777
859
#[ test]
778
860
fn test_input_account_deserialize ( ) {
779
861
let input_account = get_test_input_account ( ) ;
@@ -796,6 +878,16 @@ mod test {
796
878
address_merkle_tree_root_index : 3 ,
797
879
}
798
880
}
881
+
882
+ // get_instruction_data_invoke_cpi
883
+ fn get_rnd_new_address_params ( rng : & mut StdRng ) -> NewAddressParamsPacked {
884
+ NewAddressParamsPacked {
885
+ seed : rng. gen ( ) ,
886
+ address_queue_account_index : rng. gen ( ) ,
887
+ address_merkle_tree_account_index : rng. gen ( ) ,
888
+ address_merkle_tree_root_index : rng. gen ( ) ,
889
+ }
890
+ }
799
891
#[ test]
800
892
fn test_account_data_deserialize ( ) {
801
893
let test_data = CompressedAccountData {
@@ -988,4 +1080,24 @@ mod test {
988
1080
989
1081
Ok ( ( ) )
990
1082
}
1083
+
1084
+ #[ test]
1085
+ fn test_instruction_data_invoke_cpi_rnd ( ) {
1086
+ use rand:: { rngs:: StdRng , Rng , SeedableRng } ;
1087
+ let mut thread_rng = ThreadRng :: default ( ) ;
1088
+ let seed = thread_rng. gen ( ) ;
1089
+ // Keep this print so that in case the test fails
1090
+ // we can use the seed to reproduce the error.
1091
+ println ! ( "\n \n e2e test seed {}\n \n " , seed) ;
1092
+ let mut rng = StdRng :: seed_from_u64 ( seed) ;
1093
+
1094
+ let num_iters = 10000 ;
1095
+ for _ in 0 ..num_iters {
1096
+ let value = get_rnd_instruction_data_invoke_cpi ( & mut rng) ;
1097
+ let mut vec = Vec :: new ( ) ;
1098
+ value. serialize ( & mut vec) . unwrap ( ) ;
1099
+ let ( zero_copy, _) = ZInstructionDataInvokeCpi :: zero_copy_at ( & vec) . unwrap ( ) ;
1100
+ compare_invoke_cpi_instruction_data ( & value, & zero_copy) . unwrap ( ) ;
1101
+ }
1102
+ }
991
1103
}
0 commit comments