From 286e3012580e7ec4985daf99b195312acf984c0d Mon Sep 17 00:00:00 2001 From: lukacan Date: Sun, 21 Jan 2024 13:09:04 +0100 Subject: [PATCH 1/6] =?UTF-8?q?=F0=9F=9A=A7=20WIP:=20instruction=20input?= =?UTF-8?q?=20argument=20of=20type=20Pubkey=20convert=20to=20AccointId,=20?= =?UTF-8?q?and=20add=20to=20FuzzAccounts=20struct?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/client/src/fuzzer/fuzzer_generator.rs | 37 ++++++++++++++++---- 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/crates/client/src/fuzzer/fuzzer_generator.rs b/crates/client/src/fuzzer/fuzzer_generator.rs index dca5f379..2c7b9f60 100644 --- a/crates/client/src/fuzzer/fuzzer_generator.rs +++ b/crates/client/src/fuzzer/fuzzer_generator.rs @@ -13,6 +13,7 @@ pub fn generate_source_code(idl: &Idl) -> String { let program_name = idl_program.name.snake_case.replace('-', "_"); let fuzz_instructions_module_name = format_ident!("{}_fuzz_instructions", program_name); let module_name: syn::Ident = parse_str(&program_name).unwrap(); + let mut accounts_from_instr_input: Vec<(String, String)> = vec![]; let instructions = idl_program .instruction_account_pairs @@ -52,9 +53,25 @@ pub fn generate_source_code(idl: &Idl) -> String { .parameters .iter() .map(|(name, ty)| { - let name = format_ident!("{name}"); - let ty: syn::Type = parse_str(ty).unwrap(); - let parameter: syn::FnArg = parse_quote!(#name: #ty); + let name_ident = format_ident!("{name}"); + // TODO What about custom Enums and Structs on Instr Input ? + let ty = parse_str(ty).unwrap(); + let ty: syn::Type = match &ty { + syn::Type::Path(tp) => { + let last_type = + &tp.path.segments.last().unwrap().ident.to_string(); + if last_type == "Pubkey" { + let t: syn::Type = parse_str("AccountId").unwrap(); + accounts_from_instr_input + .push((name.to_string(), "AccountId".to_string())); + t + } else { + ty + } + } + _ => ty, + }; + let parameter: syn::FnArg = parse_quote!(#name_ident: #ty); parameter }) .collect::>(); @@ -173,7 +190,7 @@ pub fn generate_source_code(idl: &Idl) -> String { ) .into_iter(); - let fuzz_accounts = idl_program.instruction_account_pairs.iter().fold( + let mut fuzz_accounts = idl_program.instruction_account_pairs.iter().fold( HashMap::new(), |mut fuzz_accounts, (_idl_instruction, idl_account_group)| { idl_account_group.accounts.iter().fold( @@ -184,10 +201,19 @@ pub fn generate_source_code(idl: &Idl) -> String { fuzz_accounts }, ); - fuzz_accounts }, ); + + fuzz_accounts.extend(accounts_from_instr_input.iter().fold( + HashMap::new(), + |mut fuzz_accounts, (name, _ty)| { + let name = format_ident!("{name}"); + fuzz_accounts.entry(name).or_insert_with(|| "".to_string()); + fuzz_accounts + }, + )); + // this ensures that the order of accounts is deterministic // so we can use expected generated template within tests let mut sorted_fuzz_accounts: Vec<_> = fuzz_accounts.keys().collect(); @@ -195,7 +221,6 @@ pub fn generate_source_code(idl: &Idl) -> String { let fuzzer_module: syn::ItemMod = parse_quote! { pub mod #fuzz_instructions_module_name { - use trdelnik_client::fuzzing::*; use crate::accounts_snapshots::*; #[derive(Arbitrary, Clone, DisplayIx, FuzzTestExecutor, FuzzDeserialize)] From 608c93d4d509c6ae76a74a22ffab99f25c0597ee Mon Sep 17 00:00:00 2001 From: lukacan Date: Sat, 3 Feb 2024 11:51:38 +0100 Subject: [PATCH 2/6] =?UTF-8?q?=E2=9C=85=20update=20test=5Fprogram=20so=20?= =?UTF-8?q?it=20can=20be=20simply=20expanded,=20update=20expected=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/client/src/fuzzer/fuzzer_generator.rs | 1 + .../expanded_fuzz_example3.rs | 32 +++++++++---------- .../expected_fuzz_instructions.rs | 5 +-- .../test_program/fuzz_example3/Cargo.toml | 2 ++ .../test_program/fuzz_example3/src/lib.rs | 4 +-- 5 files changed, 24 insertions(+), 20 deletions(-) diff --git a/crates/client/src/fuzzer/fuzzer_generator.rs b/crates/client/src/fuzzer/fuzzer_generator.rs index 2c7b9f60..be9d62fe 100644 --- a/crates/client/src/fuzzer/fuzzer_generator.rs +++ b/crates/client/src/fuzzer/fuzzer_generator.rs @@ -221,6 +221,7 @@ pub fn generate_source_code(idl: &Idl) -> String { let fuzzer_module: syn::ItemMod = parse_quote! { pub mod #fuzz_instructions_module_name { + use trdelnik_client::fuzzing::*; use crate::accounts_snapshots::*; #[derive(Arbitrary, Clone, DisplayIx, FuzzTestExecutor, FuzzDeserialize)] diff --git a/crates/client/tests/test_data/expanded_source_codes/expanded_fuzz_example3.rs b/crates/client/tests/test_data/expanded_source_codes/expanded_fuzz_example3.rs index 6a990c32..e2e4984f 100644 --- a/crates/client/tests/test_data/expanded_source_codes/expanded_fuzz_example3.rs +++ b/crates/client/tests/test_data/expanded_source_codes/expanded_fuzz_example3.rs @@ -108,7 +108,7 @@ mod instructions { error_msg: VestingError::InvalidAmount.to_string(), error_origin: Some(anchor_lang::error::ErrorOrigin::Source( anchor_lang::error::Source { - filename: "programs/fuzz_example3/src/instructions/initialize.rs", + filename: "src/instructions/initialize.rs", line: 18u32, }, )), @@ -124,7 +124,7 @@ mod instructions { error_msg: VestingError::InvalidTimeRange.to_string(), error_origin: Some(anchor_lang::error::ErrorOrigin::Source( anchor_lang::error::Source { - filename: "programs/fuzz_example3/src/instructions/initialize.rs", + filename: "src/instructions/initialize.rs", line: 20u32, }, )), @@ -140,7 +140,7 @@ mod instructions { error_msg: VestingError::InvalidInterval.to_string(), error_origin: Some(anchor_lang::error::ErrorOrigin::Source( anchor_lang::error::Source { - filename: "programs/fuzz_example3/src/instructions/initialize.rs", + filename: "src/instructions/initialize.rs", line: 22u32, }, )), @@ -156,7 +156,7 @@ mod instructions { error_msg: VestingError::InvalidInterval.to_string(), error_origin: Some(anchor_lang::error::ErrorOrigin::Source( anchor_lang::error::Source { - filename: "programs/fuzz_example3/src/instructions/initialize.rs", + filename: "src/instructions/initialize.rs", line: 23u32, }, )), @@ -365,7 +365,7 @@ mod instructions { error_code_number: anchor_lang::error::ErrorCode::TryingToInitPayerAsProgramAccount.into(), error_msg: anchor_lang::error::ErrorCode::TryingToInitPayerAsProgramAccount.to_string(), error_origin: Some(anchor_lang::error::ErrorOrigin::Source(anchor_lang::error::Source { - filename: "programs/fuzz_example3/src/instructions/initialize.rs", + filename: "src/instructions/initialize.rs", line: 64u32, })), compared_values: None, @@ -1364,7 +1364,7 @@ pub mod state { .to_string(), error_origin: Some(anchor_lang::error::ErrorOrigin::Source( anchor_lang::error::Source { - filename: "programs/fuzz_example3/src/state.rs", + filename: "src/state.rs", line: 3u32, }, )), @@ -1764,7 +1764,7 @@ mod __private { .to_string(), error_origin: Some(anchor_lang::error::ErrorOrigin::Source( anchor_lang::error::Source { - filename: "programs/fuzz_example3/src/lib.rs", + filename: "src/lib.rs", line: 11u32, }, )), @@ -3424,7 +3424,7 @@ mod __private { error_msg: anchor_lang::error::ErrorCode::RequireEqViolated.to_string(), error_origin: Some(anchor_lang::error::ErrorOrigin::Source( anchor_lang::error::Source { - filename: "programs/fuzz_example3/src/lib.rs", + filename: "src/lib.rs", line: 11u32, }, )), @@ -3466,7 +3466,7 @@ mod __private { error_msg: anchor_lang::error::ErrorCode::RequireGteViolated.to_string(), error_origin: Some(anchor_lang::error::ErrorOrigin::Source( anchor_lang::error::Source { - filename: "programs/fuzz_example3/src/lib.rs", + filename: "src/lib.rs", line: 11u32, }, )), @@ -3492,7 +3492,7 @@ mod __private { let ix = instruction::InitVesting::deserialize(&mut &__ix_data[..]) .map_err(|_| anchor_lang::error::ErrorCode::InstructionDidNotDeserialize)?; let instruction::InitVesting { - recipient, + i_recipient, amount, start_at, end_at, @@ -3515,7 +3515,7 @@ mod __private { __remaining_accounts, __bumps, ), - recipient, + i_recipient, amount, start_at, end_at, @@ -3557,13 +3557,13 @@ pub mod fuzz_example3 { use super::*; pub fn init_vesting( ctx: Context, - recipient: Pubkey, + i_recipient: Pubkey, amount: u64, start_at: u64, end_at: u64, interval: u64, ) -> Result<()> { - _init_vesting(ctx, recipient, amount, start_at, end_at, interval) + _init_vesting(ctx, i_recipient, amount, start_at, end_at, interval) } pub fn withdraw_unlocked(ctx: Context) -> Result<()> { _withdraw_unlocked(ctx) @@ -3579,7 +3579,7 @@ pub mod instruction { use super::*; #[doc = r" Instruction."] pub struct InitVesting { - pub recipient: Pubkey, + pub i_recipient: Pubkey, pub amount: u64, pub start_at: u64, pub end_at: u64, @@ -3597,7 +3597,7 @@ pub mod instruction { &self, writer: &mut W, ) -> ::core::result::Result<(), borsh::maybestd::io::Error> { - borsh::BorshSerialize::serialize(&self.recipient, writer)?; + borsh::BorshSerialize::serialize(&self.i_recipient, writer)?; borsh::BorshSerialize::serialize(&self.amount, writer)?; borsh::BorshSerialize::serialize(&self.start_at, writer)?; borsh::BorshSerialize::serialize(&self.end_at, writer)?; @@ -3617,7 +3617,7 @@ pub mod instruction { reader: &mut R, ) -> ::core::result::Result { Ok(Self { - recipient: borsh::BorshDeserialize::deserialize_reader(reader)?, + i_recipient: borsh::BorshDeserialize::deserialize_reader(reader)?, amount: borsh::BorshDeserialize::deserialize_reader(reader)?, start_at: borsh::BorshDeserialize::deserialize_reader(reader)?, end_at: borsh::BorshDeserialize::deserialize_reader(reader)?, diff --git a/crates/client/tests/test_data/expected_source_codes/expected_fuzz_instructions.rs b/crates/client/tests/test_data/expected_source_codes/expected_fuzz_instructions.rs index a921a040..96a28496 100644 --- a/crates/client/tests/test_data/expected_source_codes/expected_fuzz_instructions.rs +++ b/crates/client/tests/test_data/expected_source_codes/expected_fuzz_instructions.rs @@ -23,7 +23,7 @@ pub mod fuzz_example3_fuzz_instructions { } #[derive(Arbitrary, Clone)] pub struct InitVestingData { - pub recipient: Pubkey, + pub i_recipient: AccountId, pub amount: u64, pub start_at: u64, pub end_at: u64, @@ -57,7 +57,7 @@ pub mod fuzz_example3_fuzz_instructions { _fuzz_accounts: &mut FuzzAccounts, ) -> Result { let data = fuzz_example3::instruction::InitVesting { - recipient: todo!(), + i_recipient: todo!(), amount: todo!(), start_at: todo!(), end_at: todo!(), @@ -123,6 +123,7 @@ pub mod fuzz_example3_fuzz_instructions { escrow: AccountsStorage, escrow_pda_authority: AccountsStorage, escrow_token_account: AccountsStorage, + i_recipient: AccountsStorage, mint: AccountsStorage, recipient: AccountsStorage, recipient_token_account: AccountsStorage, diff --git a/crates/client/tests/test_program/fuzz_example3/Cargo.toml b/crates/client/tests/test_program/fuzz_example3/Cargo.toml index edcc50d3..a09e1684 100644 --- a/crates/client/tests/test_program/fuzz_example3/Cargo.toml +++ b/crates/client/tests/test_program/fuzz_example3/Cargo.toml @@ -1,3 +1,5 @@ +[workspace] + [package] name = "fuzz_example3" version = "0.1.0" diff --git a/crates/client/tests/test_program/fuzz_example3/src/lib.rs b/crates/client/tests/test_program/fuzz_example3/src/lib.rs index 20a9a43a..f892fe65 100644 --- a/crates/client/tests/test_program/fuzz_example3/src/lib.rs +++ b/crates/client/tests/test_program/fuzz_example3/src/lib.rs @@ -14,13 +14,13 @@ pub mod fuzz_example3 { pub fn init_vesting( ctx: Context, - recipient: Pubkey, + i_recipient: Pubkey, amount: u64, start_at: u64, end_at: u64, interval: u64, ) -> Result<()> { - _init_vesting(ctx, recipient, amount, start_at, end_at, interval) + _init_vesting(ctx, i_recipient, amount, start_at, end_at, interval) } pub fn withdraw_unlocked(ctx: Context) -> Result<()> { From 523dff9dfa060ec45b779ae832c40e73bd969d79 Mon Sep 17 00:00:00 2001 From: lukacan Date: Wed, 14 Feb 2024 22:39:53 +0100 Subject: [PATCH 3/6] =?UTF-8?q?=F0=9F=94=A5=20remove=20unnecessary=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/client/src/fuzzer/fuzzer_generator.rs | 15 +-------------- .../expanded_fuzz_example3.rs | 14 +++++++------- .../expected_fuzz_instructions.rs | 5 ++--- .../tests/test_program/fuzz_example3/src/lib.rs | 4 ++-- 4 files changed, 12 insertions(+), 26 deletions(-) diff --git a/crates/client/src/fuzzer/fuzzer_generator.rs b/crates/client/src/fuzzer/fuzzer_generator.rs index be9d62fe..cbcaa7ba 100644 --- a/crates/client/src/fuzzer/fuzzer_generator.rs +++ b/crates/client/src/fuzzer/fuzzer_generator.rs @@ -13,7 +13,6 @@ pub fn generate_source_code(idl: &Idl) -> String { let program_name = idl_program.name.snake_case.replace('-', "_"); let fuzz_instructions_module_name = format_ident!("{}_fuzz_instructions", program_name); let module_name: syn::Ident = parse_str(&program_name).unwrap(); - let mut accounts_from_instr_input: Vec<(String, String)> = vec![]; let instructions = idl_program .instruction_account_pairs @@ -54,7 +53,6 @@ pub fn generate_source_code(idl: &Idl) -> String { .iter() .map(|(name, ty)| { let name_ident = format_ident!("{name}"); - // TODO What about custom Enums and Structs on Instr Input ? let ty = parse_str(ty).unwrap(); let ty: syn::Type = match &ty { syn::Type::Path(tp) => { @@ -62,8 +60,6 @@ pub fn generate_source_code(idl: &Idl) -> String { &tp.path.segments.last().unwrap().ident.to_string(); if last_type == "Pubkey" { let t: syn::Type = parse_str("AccountId").unwrap(); - accounts_from_instr_input - .push((name.to_string(), "AccountId".to_string())); t } else { ty @@ -190,7 +186,7 @@ pub fn generate_source_code(idl: &Idl) -> String { ) .into_iter(); - let mut fuzz_accounts = idl_program.instruction_account_pairs.iter().fold( + let fuzz_accounts = idl_program.instruction_account_pairs.iter().fold( HashMap::new(), |mut fuzz_accounts, (_idl_instruction, idl_account_group)| { idl_account_group.accounts.iter().fold( @@ -205,15 +201,6 @@ pub fn generate_source_code(idl: &Idl) -> String { }, ); - fuzz_accounts.extend(accounts_from_instr_input.iter().fold( - HashMap::new(), - |mut fuzz_accounts, (name, _ty)| { - let name = format_ident!("{name}"); - fuzz_accounts.entry(name).or_insert_with(|| "".to_string()); - fuzz_accounts - }, - )); - // this ensures that the order of accounts is deterministic // so we can use expected generated template within tests let mut sorted_fuzz_accounts: Vec<_> = fuzz_accounts.keys().collect(); diff --git a/crates/client/tests/test_data/expanded_source_codes/expanded_fuzz_example3.rs b/crates/client/tests/test_data/expanded_source_codes/expanded_fuzz_example3.rs index e2e4984f..91873695 100644 --- a/crates/client/tests/test_data/expanded_source_codes/expanded_fuzz_example3.rs +++ b/crates/client/tests/test_data/expanded_source_codes/expanded_fuzz_example3.rs @@ -3492,7 +3492,7 @@ mod __private { let ix = instruction::InitVesting::deserialize(&mut &__ix_data[..]) .map_err(|_| anchor_lang::error::ErrorCode::InstructionDidNotDeserialize)?; let instruction::InitVesting { - i_recipient, + recipient, amount, start_at, end_at, @@ -3515,7 +3515,7 @@ mod __private { __remaining_accounts, __bumps, ), - i_recipient, + recipient, amount, start_at, end_at, @@ -3557,13 +3557,13 @@ pub mod fuzz_example3 { use super::*; pub fn init_vesting( ctx: Context, - i_recipient: Pubkey, + recipient: Pubkey, amount: u64, start_at: u64, end_at: u64, interval: u64, ) -> Result<()> { - _init_vesting(ctx, i_recipient, amount, start_at, end_at, interval) + _init_vesting(ctx, recipient, amount, start_at, end_at, interval) } pub fn withdraw_unlocked(ctx: Context) -> Result<()> { _withdraw_unlocked(ctx) @@ -3579,7 +3579,7 @@ pub mod instruction { use super::*; #[doc = r" Instruction."] pub struct InitVesting { - pub i_recipient: Pubkey, + pub recipient: Pubkey, pub amount: u64, pub start_at: u64, pub end_at: u64, @@ -3597,7 +3597,7 @@ pub mod instruction { &self, writer: &mut W, ) -> ::core::result::Result<(), borsh::maybestd::io::Error> { - borsh::BorshSerialize::serialize(&self.i_recipient, writer)?; + borsh::BorshSerialize::serialize(&self.recipient, writer)?; borsh::BorshSerialize::serialize(&self.amount, writer)?; borsh::BorshSerialize::serialize(&self.start_at, writer)?; borsh::BorshSerialize::serialize(&self.end_at, writer)?; @@ -3617,7 +3617,7 @@ pub mod instruction { reader: &mut R, ) -> ::core::result::Result { Ok(Self { - i_recipient: borsh::BorshDeserialize::deserialize_reader(reader)?, + recipient: borsh::BorshDeserialize::deserialize_reader(reader)?, amount: borsh::BorshDeserialize::deserialize_reader(reader)?, start_at: borsh::BorshDeserialize::deserialize_reader(reader)?, end_at: borsh::BorshDeserialize::deserialize_reader(reader)?, diff --git a/crates/client/tests/test_data/expected_source_codes/expected_fuzz_instructions.rs b/crates/client/tests/test_data/expected_source_codes/expected_fuzz_instructions.rs index 96a28496..bd7d2ba7 100644 --- a/crates/client/tests/test_data/expected_source_codes/expected_fuzz_instructions.rs +++ b/crates/client/tests/test_data/expected_source_codes/expected_fuzz_instructions.rs @@ -23,7 +23,7 @@ pub mod fuzz_example3_fuzz_instructions { } #[derive(Arbitrary, Clone)] pub struct InitVestingData { - pub i_recipient: AccountId, + pub recipient: AccountId, pub amount: u64, pub start_at: u64, pub end_at: u64, @@ -57,7 +57,7 @@ pub mod fuzz_example3_fuzz_instructions { _fuzz_accounts: &mut FuzzAccounts, ) -> Result { let data = fuzz_example3::instruction::InitVesting { - i_recipient: todo!(), + recipient: todo!(), amount: todo!(), start_at: todo!(), end_at: todo!(), @@ -123,7 +123,6 @@ pub mod fuzz_example3_fuzz_instructions { escrow: AccountsStorage, escrow_pda_authority: AccountsStorage, escrow_token_account: AccountsStorage, - i_recipient: AccountsStorage, mint: AccountsStorage, recipient: AccountsStorage, recipient_token_account: AccountsStorage, diff --git a/crates/client/tests/test_program/fuzz_example3/src/lib.rs b/crates/client/tests/test_program/fuzz_example3/src/lib.rs index f892fe65..20a9a43a 100644 --- a/crates/client/tests/test_program/fuzz_example3/src/lib.rs +++ b/crates/client/tests/test_program/fuzz_example3/src/lib.rs @@ -14,13 +14,13 @@ pub mod fuzz_example3 { pub fn init_vesting( ctx: Context, - i_recipient: Pubkey, + recipient: Pubkey, amount: u64, start_at: u64, end_at: u64, interval: u64, ) -> Result<()> { - _init_vesting(ctx, i_recipient, amount, start_at, end_at, interval) + _init_vesting(ctx, recipient, amount, start_at, end_at, interval) } pub fn withdraw_unlocked(ctx: Context) -> Result<()> { From a04cb645981b77c3b21da6cc04653d2a91be2953 Mon Sep 17 00:00:00 2001 From: lukacan Date: Wed, 14 Feb 2024 23:34:02 +0100 Subject: [PATCH 4/6] =?UTF-8?q?=F0=9F=90=9B=20update=20=3F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/client/src/fuzzer/fuzzer_generator.rs | 18 ++++++++++++------ .../expanded_fuzz_example3.rs | 8 ++++++++ .../expected_fuzz_instructions.rs | 2 ++ .../test_program/fuzz_example3/src/lib.rs | 1 + 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/crates/client/src/fuzzer/fuzzer_generator.rs b/crates/client/src/fuzzer/fuzzer_generator.rs index cbcaa7ba..45b44ccc 100644 --- a/crates/client/src/fuzzer/fuzzer_generator.rs +++ b/crates/client/src/fuzzer/fuzzer_generator.rs @@ -53,14 +53,20 @@ pub fn generate_source_code(idl: &Idl) -> String { .iter() .map(|(name, ty)| { let name_ident = format_ident!("{name}"); - let ty = parse_str(ty).unwrap(); + let ty = parse_str(ty).expect("Unable To parse argument type"); let ty: syn::Type = match &ty { syn::Type::Path(tp) => { - let last_type = - &tp.path.segments.last().unwrap().ident.to_string(); - if last_type == "Pubkey" { - let t: syn::Type = parse_str("AccountId").unwrap(); - t + if &tp + .path + .segments + .last() + .expect("Unable To obtain last type") + .ident + .to_string() + == "Pubkey" + { + parse_str("AccountId") + .expect("Unable To parse AccountId") } else { ty } diff --git a/crates/client/tests/test_data/expanded_source_codes/expanded_fuzz_example3.rs b/crates/client/tests/test_data/expanded_source_codes/expanded_fuzz_example3.rs index 91873695..2b970578 100644 --- a/crates/client/tests/test_data/expanded_source_codes/expanded_fuzz_example3.rs +++ b/crates/client/tests/test_data/expanded_source_codes/expanded_fuzz_example3.rs @@ -3493,6 +3493,7 @@ mod __private { .map_err(|_| anchor_lang::error::ErrorCode::InstructionDidNotDeserialize)?; let instruction::InitVesting { recipient, + _recipient, amount, start_at, end_at, @@ -3516,6 +3517,7 @@ mod __private { __bumps, ), recipient, + _recipient, amount, start_at, end_at, @@ -3558,6 +3560,7 @@ pub mod fuzz_example3 { pub fn init_vesting( ctx: Context, recipient: Pubkey, + _recipient: anchor_lang::prelude::Pubkey, amount: u64, start_at: u64, end_at: u64, @@ -3580,6 +3583,7 @@ pub mod instruction { #[doc = r" Instruction."] pub struct InitVesting { pub recipient: Pubkey, + pub _recipient: anchor_lang::prelude::Pubkey, pub amount: u64, pub start_at: u64, pub end_at: u64, @@ -3588,6 +3592,7 @@ pub mod instruction { impl borsh::ser::BorshSerialize for InitVesting where Pubkey: borsh::ser::BorshSerialize, + anchor_lang::prelude::Pubkey: borsh::ser::BorshSerialize, u64: borsh::ser::BorshSerialize, u64: borsh::ser::BorshSerialize, u64: borsh::ser::BorshSerialize, @@ -3598,6 +3603,7 @@ pub mod instruction { writer: &mut W, ) -> ::core::result::Result<(), borsh::maybestd::io::Error> { borsh::BorshSerialize::serialize(&self.recipient, writer)?; + borsh::BorshSerialize::serialize(&self._recipient, writer)?; borsh::BorshSerialize::serialize(&self.amount, writer)?; borsh::BorshSerialize::serialize(&self.start_at, writer)?; borsh::BorshSerialize::serialize(&self.end_at, writer)?; @@ -3608,6 +3614,7 @@ pub mod instruction { impl borsh::de::BorshDeserialize for InitVesting where Pubkey: borsh::BorshDeserialize, + anchor_lang::prelude::Pubkey: borsh::BorshDeserialize, u64: borsh::BorshDeserialize, u64: borsh::BorshDeserialize, u64: borsh::BorshDeserialize, @@ -3618,6 +3625,7 @@ pub mod instruction { ) -> ::core::result::Result { Ok(Self { recipient: borsh::BorshDeserialize::deserialize_reader(reader)?, + _recipient: borsh::BorshDeserialize::deserialize_reader(reader)?, amount: borsh::BorshDeserialize::deserialize_reader(reader)?, start_at: borsh::BorshDeserialize::deserialize_reader(reader)?, end_at: borsh::BorshDeserialize::deserialize_reader(reader)?, diff --git a/crates/client/tests/test_data/expected_source_codes/expected_fuzz_instructions.rs b/crates/client/tests/test_data/expected_source_codes/expected_fuzz_instructions.rs index bd7d2ba7..9559efea 100644 --- a/crates/client/tests/test_data/expected_source_codes/expected_fuzz_instructions.rs +++ b/crates/client/tests/test_data/expected_source_codes/expected_fuzz_instructions.rs @@ -24,6 +24,7 @@ pub mod fuzz_example3_fuzz_instructions { #[derive(Arbitrary, Clone)] pub struct InitVestingData { pub recipient: AccountId, + pub _recipient: AccountId, pub amount: u64, pub start_at: u64, pub end_at: u64, @@ -58,6 +59,7 @@ pub mod fuzz_example3_fuzz_instructions { ) -> Result { let data = fuzz_example3::instruction::InitVesting { recipient: todo!(), + _recipient: todo!(), amount: todo!(), start_at: todo!(), end_at: todo!(), diff --git a/crates/client/tests/test_program/fuzz_example3/src/lib.rs b/crates/client/tests/test_program/fuzz_example3/src/lib.rs index 20a9a43a..175edde1 100644 --- a/crates/client/tests/test_program/fuzz_example3/src/lib.rs +++ b/crates/client/tests/test_program/fuzz_example3/src/lib.rs @@ -15,6 +15,7 @@ pub mod fuzz_example3 { pub fn init_vesting( ctx: Context, recipient: Pubkey, + _recipient: anchor_lang::prelude::Pubkey, amount: u64, start_at: u64, end_at: u64, From 43d173cb3cb16ad0cac825dac5f42d00e7fe8674 Mon Sep 17 00:00:00 2001 From: lukacan Date: Wed, 14 Feb 2024 23:50:50 +0100 Subject: [PATCH 5/6] =?UTF-8?q?=F0=9F=90=9B=20simplified?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/client/src/fuzzer/fuzzer_generator.rs | 28 +++++++------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/crates/client/src/fuzzer/fuzzer_generator.rs b/crates/client/src/fuzzer/fuzzer_generator.rs index 45b44ccc..fda517d6 100644 --- a/crates/client/src/fuzzer/fuzzer_generator.rs +++ b/crates/client/src/fuzzer/fuzzer_generator.rs @@ -53,25 +53,15 @@ pub fn generate_source_code(idl: &Idl) -> String { .iter() .map(|(name, ty)| { let name_ident = format_ident!("{name}"); - let ty = parse_str(ty).expect("Unable To parse argument type"); - let ty: syn::Type = match &ty { - syn::Type::Path(tp) => { - if &tp - .path - .segments - .last() - .expect("Unable To obtain last type") - .ident - .to_string() - == "Pubkey" - { - parse_str("AccountId") - .expect("Unable To parse AccountId") - } else { - ty - } - } - _ => ty, + // the option ":: Pubkey"is for "anchor_lang::pubkey::Pubkey", that looks + // like "anchor_lang :: pubkey :: Pubkey" + let ty: syn::Type = if ty.ends_with("Pubkey") + || ty.ends_with("::Pubkey") + || ty.ends_with(":: Pubkey") + { + parse_str("AccountId").expect("Unable to parse AccountId") + } else { + parse_str(ty).expect("Unable to parse ty") }; let parameter: syn::FnArg = parse_quote!(#name_ident: #ty); parameter From 93a58efe385551b0b6091444b21ff9fb0d6676a4 Mon Sep 17 00:00:00 2001 From: Ikrk Date: Thu, 15 Feb 2024 11:19:49 +0100 Subject: [PATCH 6/6] =?UTF-8?q?=F0=9F=90=9B=20Fixed=20Pubkey=20replacement?= =?UTF-8?q?=20by=20AccountId?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/client/src/fuzzer/fuzzer_generator.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/crates/client/src/fuzzer/fuzzer_generator.rs b/crates/client/src/fuzzer/fuzzer_generator.rs index fda517d6..bb8b7efd 100644 --- a/crates/client/src/fuzzer/fuzzer_generator.rs +++ b/crates/client/src/fuzzer/fuzzer_generator.rs @@ -53,11 +53,10 @@ pub fn generate_source_code(idl: &Idl) -> String { .iter() .map(|(name, ty)| { let name_ident = format_ident!("{name}"); - // the option ":: Pubkey"is for "anchor_lang::pubkey::Pubkey", that looks - // like "anchor_lang :: pubkey :: Pubkey" - let ty: syn::Type = if ty.ends_with("Pubkey") - || ty.ends_with("::Pubkey") - || ty.ends_with(":: Pubkey") + // Replace Pubkey type by AccountId, so the fuzzer will generate only Account indices + // a not always unique Pubkeys + let ty: syn::Type = if ty == "Pubkey" + || ty.replace(' ', "").ends_with("::Pubkey") { parse_str("AccountId").expect("Unable to parse AccountId") } else {