From 79f1b9682ca3a5332801892368d03a72a500b7f1 Mon Sep 17 00:00:00 2001 From: andrejlukacovic <37964423+lukacan@users.noreply.github.com> Date: Thu, 15 Feb 2024 11:35:09 +0100 Subject: [PATCH] Feat/Replace Pubkey by AccountID (#116) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 🚧 WIP: instruction input argument of type Pubkey convert to AccointId, and add to FuzzAccounts struct * ✅ update test_program so it can be simply expanded, update expected code * 🔥 remove unnecessary code * 🐛 update ? * 🐛 simplified * 🐛 Fixed Pubkey replacement by AccountId --------- Co-authored-by: lukacan Co-authored-by: Ikrk --- crates/client/src/fuzzer/fuzzer_generator.rs | 16 +++++++++--- .../expanded_fuzz_example3.rs | 26 ++++++++++++------- .../expected_fuzz_instructions.rs | 4 ++- .../test_program/fuzz_example3/Cargo.toml | 2 ++ .../test_program/fuzz_example3/src/lib.rs | 1 + 5 files changed, 35 insertions(+), 14 deletions(-) diff --git a/crates/client/src/fuzzer/fuzzer_generator.rs b/crates/client/src/fuzzer/fuzzer_generator.rs index dca5f379..bb8b7efd 100644 --- a/crates/client/src/fuzzer/fuzzer_generator.rs +++ b/crates/client/src/fuzzer/fuzzer_generator.rs @@ -52,9 +52,17 @@ 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}"); + // 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 { + parse_str(ty).expect("Unable to parse ty") + }; + let parameter: syn::FnArg = parse_quote!(#name_ident: #ty); parameter }) .collect::>(); @@ -184,10 +192,10 @@ pub fn generate_source_code(idl: &Idl) -> String { fuzz_accounts }, ); - 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 6a990c32..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 @@ -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, }, )), @@ -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 a921a040..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 @@ -23,7 +23,8 @@ pub mod fuzz_example3_fuzz_instructions { } #[derive(Arbitrary, Clone)] pub struct InitVestingData { - pub recipient: Pubkey, + 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/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..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,