Skip to content

Commit

Permalink
🔥 Remove AccountsSnapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
lukacan committed Nov 6, 2024
1 parent 061cbc9 commit 2cef53f
Show file tree
Hide file tree
Showing 74 changed files with 5,210 additions and 2,924 deletions.
465 changes: 350 additions & 115 deletions Cargo.lock

Large diffs are not rendered by default.

19 changes: 1 addition & 18 deletions crates/cli/src/howto.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,7 @@ For more info about Honggfuzz installation check https://github.com/rust-fuzz/ho
trident init
```

- Derive ***AccountsSnapshots*** for each account context in the program:

```rust
use trident_derive_accounts_snapshots::AccountsSnapshots;

#[derive(AccountsSnapshots, Accounts)]
pub struct InitializeContext<'info> {
// ...
}

```

- Link Account Context Aliases in the ***fuzz_instructions.rs*** with desired Snapshots

```rust
use hello_world::trident_fuzz_initialize_context_snapshot::InitializeContextAlias;
type InitializeFnSnapshot<'info> = InitializeContextAlias<'info>;
```
## Write Fuzz Test

- Implement the ***todo!*** placeholders in ***fuzz_instructions.rs*** based on the provided descriptions.

Expand Down
1 change: 0 additions & 1 deletion crates/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ description = "The trident_client crate helps you build and deploy an Anchor pro
trident-derive-displayix = { path = "../fuzz/derive/display_ix", version = "0.0.3" }
trident-derive-fuzz-test-executor = { path = "../fuzz/derive/fuzz_test_executor", version = "0.0.3" }
trident-fuzz = { path = "../fuzz", version = "0.2.0" }
trident-derive-accounts-snapshots = { path = "../fuzz/derive/accounts_snapshots", version = "0.0.2" }
# ANCHOR
# INFO: Anchor-spl is here as dependency only to activate the idl-build feature, so that
# users do not have to do it manually in their program's Cargo.toml
Expand Down
2 changes: 0 additions & 2 deletions crates/client/config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
{
"trident_fuzz": "0.2.0",
"trident_derive_accounts_snapshots": "0.0.2",
"trident_client": "0.8.0"
}
5 changes: 5 additions & 0 deletions crates/client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,21 @@ pub mod fuzzing {
/// anchor_lang
pub use anchor_lang;
pub use anchor_lang::solana_program::hash::Hash;
pub use anchor_lang::AccountDeserialize;
pub use anchor_lang::InstructionData;
pub use anchor_lang::Key;
pub use anchor_lang::ToAccountInfo;
pub use anchor_lang::ToAccountMetas;

/// solana_sdk
pub use solana_sdk;
pub use solana_sdk::account::AccountSharedData;
pub use solana_sdk::account::ReadableAccount;
pub use solana_sdk::account_info::AccountInfo;
pub use solana_sdk::entrypoint::ProcessInstruction;
pub use solana_sdk::instruction::AccountMeta;
pub use solana_sdk::instruction::Instruction;
pub use solana_sdk::native_token::LAMPORTS_PER_SOL;
pub use solana_sdk::pubkey::Pubkey;
pub use solana_sdk::signer::keypair::Keypair;
pub use solana_sdk::signer::Signer;
Expand Down Expand Up @@ -60,6 +64,7 @@ pub mod fuzzing {
pub use trident_fuzz::ix_ops::IxOps;
pub use trident_fuzz::program_test_client_blocking::ProgramTestClientBlocking;
pub use trident_fuzz::snapshot::Snapshot;
pub use trident_fuzz::snapshot::SnapshotAccount;
pub use trident_fuzz::transaction_executor::TransactionExecutor;

pub use std::cell::RefCell;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ pub fn generate_source_code(idls: &[Idl]) -> String {
let mut all_instruction_inputs: Vec<syn::ItemStruct> = Vec::new();
let mut all_instructions_ixops_impls: Vec<syn::ItemImpl> = Vec::new();
let mut all_fuzz_accounts: Vec<syn::FnArg> = Vec::new();
let mut all_snapshot_types: Vec<syn::ItemType> = Vec::new();

// Mappings for instructions and accounts
let mut instructions_mappings: HashMap<String, u8> = HashMap::new();
Expand All @@ -25,19 +24,13 @@ pub fn generate_source_code(idls: &[Idl]) -> String {
all_instructions.extend(get_instruction_variants(idl, &instructions_mappings));
all_instruction_inputs.extend(get_instruction_inputs(idl, &instructions_mappings));
all_instructions_ixops_impls.extend(get_instruction_ixops(idl, &instructions_mappings));
all_snapshot_types.extend(get_snapshot_types(idl, &instructions_mappings));
all_fuzz_accounts.extend(get_fuzz_accounts(idl, &accounts_mappings));
}

// Define the Rust module with all generated code
let module_definition = quote! {
use trident_client::fuzzing::*;

/// Link the relevant Account Context Alias from the program.
/// Aliases are generated by the `AccountsSnapshots` macro.
#(#all_snapshot_types)*


/// FuzzInstruction contains all available Instructions.
/// Below, the instruction arguments (accounts and data) are defined.
#[derive(Arbitrary, DisplayIx, FuzzTestExecutor)]
Expand Down Expand Up @@ -124,31 +117,6 @@ fn get_instruction_variants(
})
}

// Generate snapshot types for each instruction
fn get_snapshot_types(idl: &Idl, instruction_mappings: &HashMap<String, u8>) -> Vec<syn::ItemType> {
let program_name = idl.metadata.name.to_case(Case::UpperCamel);

idl.instructions
.iter()
.fold(Vec::new(), |mut snapshot_types, instruction| {
let mut instruction_name = instruction.name.to_case(Case::UpperCamel);
let count = instruction_mappings.get(&instruction_name).unwrap_or(&1);

// Append the program name if the instruction name is not unique
if *count > 1 {
instruction_name.push_str(&program_name);
}

let ix_snapshot: syn::Ident = format_ident!("{}Snapshot", &instruction_name);
let ix_alias: syn::Ident = format_ident!("{}Alias", &instruction_name);

let snapshot_type: syn::ItemType =
parse_quote! (type #ix_snapshot<'info> = #ix_alias<'info>;);
snapshot_types.push(snapshot_type);
snapshot_types
})
}

// Generate input structures for each instruction
fn get_instruction_inputs(
idl: &Idl,
Expand Down Expand Up @@ -255,7 +223,6 @@ fn get_instruction_ixops(

let instruction_ident_name_modified: syn::Ident =
format_ident!("{}", &instruction_name);
let ix_snapshot: syn::Ident = format_ident!("{}Snapshot", &instruction_name);

// Map arguments to their types
let parameters = instruction
Expand Down Expand Up @@ -299,7 +266,6 @@ fn get_instruction_ixops(
impl<'info> IxOps<'info> for #instruction_ident_name_modified {
type IxData = #module_name::instruction::#instruction_ident_name;
type IxAccounts = FuzzAccounts;
type IxSnapshot = #ix_snapshot<'info>;

/// Definition of the program ID that the Instruction is associated with.
fn get_program_id(&self) -> solana_sdk::pubkey::Pubkey {
Expand Down
4 changes: 2 additions & 2 deletions crates/client/src/test_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl TestGenerator {

update_gitignore(&self.root, CARGO_TARGET_DIR_DEFAULT_HFUZZ)?;
// update_gitignore(&self.root, CARGO_TARGET_DIR_DEFAULT_AFL)?;
initialize_package_metadata(&self.program_packages, &self.versions_config).await?;
// initialize_package_metadata(&self.program_packages, &self.versions_config).await?;
}

#[throws]
Expand All @@ -84,7 +84,7 @@ impl TestGenerator {
self.generate_source_codes().await?;
self.add_new_fuzz_test().await?;

update_package_metadata(&self.program_packages, &self.versions_config).await?;
// update_package_metadata(&self.program_packages, &self.versions_config).await?;
}

#[throws]
Expand Down
Loading

0 comments on commit 2cef53f

Please sign in to comment.