Skip to content

Commit e4244dc

Browse files
authored
test: add randmized test zero copy test_instruction_data_invoke_cpi_rnd (#1571)
1 parent a982efb commit e4244dc

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

program-libs/utils/src/instruction/instruction_data_zero_copy.rs

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,10 @@ impl From<&ZInstructionDataInvokeCpi<'_>> for InstructionDataInvokeCpi {
517517
#[cfg(test)]
518518
mod test {
519519
use borsh::BorshSerialize;
520+
use rand::{
521+
rngs::{StdRng, ThreadRng},
522+
Rng,
523+
};
520524

521525
use super::*;
522526
use crate::{
@@ -548,6 +552,33 @@ mod test {
548552
}
549553
}
550554

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+
551582
fn compare_invoke_cpi_instruction_data(
552583
reference: &InstructionDataInvokeCpi,
553584
z_copy: &ZInstructionDataInvokeCpi,
@@ -683,6 +714,14 @@ mod test {
683714
}
684715
}
685716

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+
686725
#[test]
687726
fn test_cpi_context_deserialize() {
688727
let cpi_context = get_cpi_context();
@@ -713,6 +752,14 @@ mod test {
713752
}
714753
}
715754

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+
716763
fn get_test_account() -> CompressedAccount {
717764
CompressedAccount {
718765
owner: solana_program::pubkey::Pubkey::new_unique(),
@@ -722,13 +769,29 @@ mod test {
722769
}
723770
}
724771

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+
725781
fn get_test_output_account() -> OutputCompressedAccountWithPackedContext {
726782
OutputCompressedAccountWithPackedContext {
727783
compressed_account: get_test_account(),
728784
merkle_tree_index: 1,
729785
}
730786
}
731787

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+
732795
#[test]
733796
fn test_output_account_deserialize() {
734797
let test_output_account = get_test_output_account();
@@ -774,6 +837,25 @@ mod test {
774837
read_only: false,
775838
}
776839
}
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+
}
777859
#[test]
778860
fn test_input_account_deserialize() {
779861
let input_account = get_test_input_account();
@@ -796,6 +878,16 @@ mod test {
796878
address_merkle_tree_root_index: 3,
797879
}
798880
}
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+
}
799891
#[test]
800892
fn test_account_data_deserialize() {
801893
let test_data = CompressedAccountData {
@@ -988,4 +1080,24 @@ mod test {
9881080

9891081
Ok(())
9901082
}
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\ne2e 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+
}
9911103
}

0 commit comments

Comments
 (0)