Skip to content

Commit

Permalink
Feat/Replace Pubkey by AccountID (#116)
Browse files Browse the repository at this point in the history
* 🚧 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 <[email protected]>
Co-authored-by: Ikrk <[email protected]>
  • Loading branch information
3 people authored Feb 15, 2024
1 parent b676b79 commit 79f1b96
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 14 deletions.
16 changes: 12 additions & 4 deletions crates/client/src/fuzzer/fuzzer_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>();
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
)),
Expand All @@ -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,
},
)),
Expand All @@ -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,
},
)),
Expand All @@ -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,
},
)),
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
},
)),
Expand Down Expand Up @@ -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,
},
)),
Expand Down Expand Up @@ -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,
},
)),
Expand Down Expand Up @@ -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,
},
)),
Expand All @@ -3493,6 +3493,7 @@ mod __private {
.map_err(|_| anchor_lang::error::ErrorCode::InstructionDidNotDeserialize)?;
let instruction::InitVesting {
recipient,
_recipient,
amount,
start_at,
end_at,
Expand All @@ -3516,6 +3517,7 @@ mod __private {
__bumps,
),
recipient,
_recipient,
amount,
start_at,
end_at,
Expand Down Expand Up @@ -3558,6 +3560,7 @@ pub mod fuzz_example3 {
pub fn init_vesting(
ctx: Context<InitVesting>,
recipient: Pubkey,
_recipient: anchor_lang::prelude::Pubkey,
amount: u64,
start_at: u64,
end_at: u64,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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)?;
Expand All @@ -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,
Expand All @@ -3618,6 +3625,7 @@ pub mod instruction {
) -> ::core::result::Result<Self, borsh::maybestd::io::Error> {
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)?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -58,6 +59,7 @@ pub mod fuzz_example3_fuzz_instructions {
) -> Result<Self::IxData, FuzzingError> {
let data = fuzz_example3::instruction::InitVesting {
recipient: todo!(),
_recipient: todo!(),
amount: todo!(),
start_at: todo!(),
end_at: todo!(),
Expand Down
2 changes: 2 additions & 0 deletions crates/client/tests/test_program/fuzz_example3/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[workspace]

[package]
name = "fuzz_example3"
version = "0.1.0"
Expand Down
1 change: 1 addition & 0 deletions crates/client/tests/test_program/fuzz_example3/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub mod fuzz_example3 {
pub fn init_vesting(
ctx: Context<InitVesting>,
recipient: Pubkey,
_recipient: anchor_lang::prelude::Pubkey,
amount: u64,
start_at: u64,
end_at: u64,
Expand Down

0 comments on commit 79f1b96

Please sign in to comment.