Skip to content

Commit

Permalink
🔧 Allow empty AccountsSnapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
lukacan committed Oct 13, 2024
1 parent 4aa51a6 commit 85a033a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 10 deletions.
3 changes: 2 additions & 1 deletion crates/cli/src/command/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{_discover, show_howto};

pub const ANCHOR_TOML: &str = "Anchor.toml";
pub const TRIDENT_TOML: &str = "Trident.toml";
pub const SKIP: &str = "\x1b[33mSkip\x1b[0m";

#[throws]
pub async fn init(force: bool) {
Expand All @@ -25,7 +26,7 @@ pub async fn init(force: bool) {
} else {
let root_path = Path::new(&root).join(TRIDENT_TOML);
if root_path.exists() {
println!("It looks like Trident is already initialized as the Trident.toml was found in {} directory.",root);
println!("{SKIP} It looks like Trident is already initialized as the Trident.toml was found in {} directory.",root);
} else {
generator.initialize().await?;
}
Expand Down
56 changes: 47 additions & 9 deletions crates/fuzz/derive/accounts_snapshots/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -716,13 +716,29 @@ fn generate(accs: &TridentAccountsStruct) -> proc_macro2::TokenStream {
quote! { #field_name }
});

quote! {
#[cfg(feature = "trident-fuzzing")]
pub mod #module_name{
#[cfg(target_os = "solana")]
compile_error!("Do not use fuzzing with Production Code");
use super::*;
impl<'info>trident_fuzz::fuzz_deserialize::FuzzDeserialize<'info> for #snapshot_name<'info> {
// CHECK IF STRUCT HAS ANY FIELDS
let has_fields = !accs.0.fields.is_empty();

// USE PHANTOMDATA IF NO FIELDS PRESENT
let struct_definition = if has_fields {
quote! {
pub struct #snapshot_name<'info> {
#(#snapshot_fields)*
}
}
} else {
quote! {
pub struct #snapshot_name<'info> {
#[allow(dead_code)]
_phantom: std::marker::PhantomData<&'info ()>,
}
}
};

// IF IT HAS FIELDS JUST FOLLOW STANDARD BEHAVIOR
let deserialize_impl = if has_fields {
quote! {
impl<'info> trident_fuzz::fuzz_deserialize::FuzzDeserialize<'info> for #snapshot_name<'info> {
fn deserialize_option(
_program_id: &anchor_lang::prelude::Pubkey,
accounts: &mut &'info [Option<AccountInfo<'info>>],
Expand All @@ -736,10 +752,32 @@ fn generate(accs: &TridentAccountsStruct) -> proc_macro2::TokenStream {
})
}
}
pub struct #snapshot_name<'info> {
#(#snapshot_fields)*
}
} else {
// IF IT HAS NO FIELDS RETURN THE PHANTOM DATA
quote! {
impl<'info> trident_fuzz::fuzz_deserialize::FuzzDeserialize<'info> for #snapshot_name<'info> {
fn deserialize_option(
_program_id: &anchor_lang::prelude::Pubkey,
_accounts: &mut &'info [Option<AccountInfo<'info>>],
) -> core::result::Result<Self, trident_fuzz::error::FuzzingError> {
Ok(Self { _phantom: std::marker::PhantomData })
}
}
}
};

quote! {
#[cfg(feature = "trident-fuzzing")]
pub mod #module_name {
#[cfg(target_os = "solana")]
compile_error!("Do not use fuzzing with Production Code");
use super::*;

#deserialize_impl

#struct_definition
}
}
}

Expand Down

0 comments on commit 85a033a

Please sign in to comment.