Skip to content

Commit

Permalink
πŸ§‘β€πŸ’» to_context_string() fn, debug output
Browse files Browse the repository at this point in the history
  • Loading branch information
lukacan committed Feb 16, 2024
1 parent 9943ab9 commit e8ee52f
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 29 deletions.
13 changes: 13 additions & 0 deletions crates/client/derive/display_ix/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ pub fn display_ix(input: TokenStream) -> TokenStream {

let display_impl = match &input.data {
Data::Enum(enum_data) => {
let to_context_string_match_arms = enum_data.variants.iter().map(|variant| {
let variant_name = &variant.ident;
quote! {
#enum_name::#variant_name (_) => String::from(stringify!(#variant_name)),
}
});
let display_match_arms = enum_data.variants.iter().map(|variant| {
let variant_name = &variant.ident;

Expand Down Expand Up @@ -41,6 +47,13 @@ pub fn display_ix(input: TokenStream) -> TokenStream {
}
}
}
impl #enum_name {
fn to_context_string(&self)->String{
match self {
#(#to_context_string_match_arms)*
}
}
}
}
}
_ => panic!("DisplayIx can only be derived for enums"),
Expand Down
12 changes: 6 additions & 6 deletions crates/client/derive/fuzz_test_executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub fn fuzz_test_executor(input: TokenStream) -> TokenStream {
#enum_name::#variant_name (ix) => {
let (mut signers, metas) =
if let Ok(acc) = ix.get_accounts(client, &mut accounts.borrow_mut())
.map_err(|e| e.with_origin(Origin::Instruction(self.to_string()))) {
.map_err(|e| e.with_origin(Origin::Instruction(self.to_context_string()))) {
acc
} else {
return Ok(());
Expand All @@ -25,7 +25,7 @@ pub fn fuzz_test_executor(input: TokenStream) -> TokenStream {
snaphot.capture_before(client).unwrap();
let data =
if let Ok(data) = ix.get_data(client, &mut accounts.borrow_mut())
.map_err(|e| e.with_origin(Origin::Instruction(self.to_string()))) {
.map_err(|e| e.with_origin(Origin::Instruction(self.to_context_string()))) {
data
} else {
return Ok(());
Expand All @@ -42,17 +42,17 @@ pub fn fuzz_test_executor(input: TokenStream) -> TokenStream {
transaction.sign(&sig, client.get_last_blockhash());

let res = client.process_transaction(transaction)
.map_err(|e| e.with_origin(Origin::Instruction(self.to_string())));
.map_err(|e| e.with_origin(Origin::Instruction(self.to_context_string())));

// this can return FuzzClientErrorWithOrigin
snaphot.capture_after(client).unwrap();
let (acc_before, acc_after) = snaphot.get_snapshot()
.map_err(|e| e.with_origin(Origin::Instruction(self.to_string()))).unwrap(); // we want to panic if we cannot unwrap to cause a crash
.map_err(|e| e.with_origin(Origin::Instruction(self.to_context_string()))).unwrap(); // we want to panic if we cannot unwrap to cause a crash

if let Err(e) = ix.check(acc_before, acc_after, data).map_err(|e| e.with_origin(Origin::Instruction(self.to_string()))) {
if let Err(e) = ix.check(acc_before, acc_after, data).map_err(|e| e.with_origin(Origin::Instruction(self.to_context_string()))) {
eprintln!(
"Custom check after the {} instruction did not pass with the error message: {}",
self, e
self.to_context_string(), e
);
eprintln!("Instruction data submitted to the instruction were:"); // TODO data does not implement Debug trait -> derive Debug trait on InitializeIx and automaticaly implement conversion from Initialize to InitializeIx
panic!("{}", e)
Expand Down
5 changes: 3 additions & 2 deletions crates/client/src/fuzzer/data_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ where
for ix in self.iter() {
eprintln!("{}", ix);
}
eprintln!("------ End of Instructions sequence ------ ");
}

for fuzz_ix in &mut self.iter() {
Expand Down Expand Up @@ -221,7 +222,7 @@ macro_rules! fuzz_trd {
///
/// # Examples
///
/// ```rust
/// ```rust,ignore
/// use trdelnik_client::fuzzing::show_account;
///
/// #[derive(Debug)]
Expand All @@ -238,7 +239,7 @@ macro_rules! fuzz_trd {
/// pre_ix: Self::IxSnapshot,
/// post_ix: Self::IxSnapshot,
/// ix_data: Self::IxData,
/// ) -> Result<(), &'static str> {} {
/// ) -> Result<(), FuzzingError> {
/// if let Some(escrow) = pre_ix.escrow{
/// show_account!(escrow);
/// }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ pub mod fuzz_example3_fuzz_instructions {
InitVesting(InitVesting),
WithdrawUnlocked(WithdrawUnlocked),
}
#[derive(Arbitrary, Clone)]
#[derive(Arbitrary, Clone, Debug)]
pub struct InitVesting {
pub accounts: InitVestingAccounts,
pub data: InitVestingData,
}
#[derive(Arbitrary, Clone)]
#[derive(Arbitrary, Clone, Debug)]
pub struct InitVestingAccounts {
pub sender: AccountId,
pub sender_token_account: AccountId,
Expand All @@ -21,7 +21,7 @@ pub mod fuzz_example3_fuzz_instructions {
pub token_program: AccountId,
pub system_program: AccountId,
}
#[derive(Arbitrary, Clone)]
#[derive(Arbitrary, Clone, Debug)]
pub struct InitVestingData {
pub recipient: AccountId,
pub _recipient: AccountId,
Expand All @@ -30,12 +30,12 @@ pub mod fuzz_example3_fuzz_instructions {
pub end_at: u64,
pub interval: u64,
}
#[derive(Arbitrary, Clone)]
#[derive(Arbitrary, Clone, Debug)]
pub struct WithdrawUnlocked {
pub accounts: WithdrawUnlockedAccounts,
pub data: WithdrawUnlockedData,
}
#[derive(Arbitrary, Clone)]
#[derive(Arbitrary, Clone, Debug)]
pub struct WithdrawUnlockedAccounts {
pub recipient: AccountId,
pub recipient_token_account: AccountId,
Expand All @@ -46,7 +46,7 @@ pub mod fuzz_example3_fuzz_instructions {
pub token_program: AccountId,
pub system_program: AccountId,
}
#[derive(Arbitrary, Clone)]
#[derive(Arbitrary, Clone, Debug)]
pub struct WithdrawUnlockedData {}
impl<'info> IxOps<'info> for InitVesting {
type IxData = fuzz_example3::instruction::InitVesting;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,22 @@ pub enum FuzzInstruction {
impl std::fmt::Display for FuzzInstruction {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
FuzzInstruction::InitVesting(_) => f.write_fmt(format_args!("InitVesting")),
FuzzInstruction::WithdrawUnlocked(_) => {
f.write_fmt(format_args!("WithdrawUnlocked"))
FuzzInstruction::InitVesting(ref content) => {
f.write_fmt(format_args!("InitVesting"))?;
f.write_fmt(format_args!("({0:#?})", content))
}
FuzzInstruction::WithdrawUnlocked(ref content) => {
f.write_fmt(format_args!("WithdrawUnlocked"))?;
f.write_fmt(format_args!("({0:#?})", content))
}
}
}
}
impl FuzzInstruction {
fn to_context_string(&self) -> String {
match self {
FuzzInstruction::InitVesting(_) => String::from("InitVesting"),
FuzzInstruction::WithdrawUnlocked(_) => String::from("WithdrawUnlocked"),
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl FuzzTestExecutor<FuzzAccounts> for FuzzInstruction {
= ix
.get_accounts(client, &mut accounts.borrow_mut())
.map_err(|e| {
e.with_origin(Origin::Instruction(self.to_string()))
e.with_origin(Origin::Instruction(self.to_context_string()))
})
{
acc
Expand All @@ -29,7 +29,7 @@ impl FuzzTestExecutor<FuzzAccounts> for FuzzInstruction {
= ix
.get_data(client, &mut accounts.borrow_mut())
.map_err(|e| {
e.with_origin(Origin::Instruction(self.to_string()))
e.with_origin(Origin::Instruction(self.to_context_string()))
})
{
data
Expand All @@ -50,24 +50,28 @@ impl FuzzTestExecutor<FuzzAccounts> for FuzzInstruction {
transaction.sign(&sig, client.get_last_blockhash());
let res = client
.process_transaction(transaction)
.map_err(|e| e.with_origin(Origin::Instruction(self.to_string())));
.map_err(|e| {
e.with_origin(Origin::Instruction(self.to_context_string()))
});
snaphot.capture_after(client).unwrap();
let (acc_before, acc_after) = snaphot
.get_snapshot()
.map_err(|e| e.with_origin(Origin::Instruction(self.to_string())))
.map_err(|e| {
e.with_origin(Origin::Instruction(self.to_context_string()))
})
.unwrap();
if let Err(e)
= ix
.check(acc_before, acc_after, data)
.map_err(|e| {
e.with_origin(Origin::Instruction(self.to_string()))
e.with_origin(Origin::Instruction(self.to_context_string()))
})
{
{
::std::io::_eprint(
format_args!(
"Custom check after the {0} instruction did not pass with the error message: {1}\n",
self, e,
self.to_context_string(), e,
),
);
};
Expand All @@ -91,7 +95,7 @@ impl FuzzTestExecutor<FuzzAccounts> for FuzzInstruction {
= ix
.get_accounts(client, &mut accounts.borrow_mut())
.map_err(|e| {
e.with_origin(Origin::Instruction(self.to_string()))
e.with_origin(Origin::Instruction(self.to_context_string()))
})
{
acc
Expand All @@ -104,7 +108,7 @@ impl FuzzTestExecutor<FuzzAccounts> for FuzzInstruction {
= ix
.get_data(client, &mut accounts.borrow_mut())
.map_err(|e| {
e.with_origin(Origin::Instruction(self.to_string()))
e.with_origin(Origin::Instruction(self.to_context_string()))
})
{
data
Expand All @@ -125,24 +129,28 @@ impl FuzzTestExecutor<FuzzAccounts> for FuzzInstruction {
transaction.sign(&sig, client.get_last_blockhash());
let res = client
.process_transaction(transaction)
.map_err(|e| e.with_origin(Origin::Instruction(self.to_string())));
.map_err(|e| {
e.with_origin(Origin::Instruction(self.to_context_string()))
});
snaphot.capture_after(client).unwrap();
let (acc_before, acc_after) = snaphot
.get_snapshot()
.map_err(|e| e.with_origin(Origin::Instruction(self.to_string())))
.map_err(|e| {
e.with_origin(Origin::Instruction(self.to_context_string()))
})
.unwrap();
if let Err(e)
= ix
.check(acc_before, acc_after, data)
.map_err(|e| {
e.with_origin(Origin::Instruction(self.to_string()))
e.with_origin(Origin::Instruction(self.to_context_string()))
})
{
{
::std::io::_eprint(
format_args!(
"Custom check after the {0} instruction did not pass with the error message: {1}\n",
self, e,
self.to_context_string(), e,
),
);
};
Expand Down

0 comments on commit e8ee52f

Please sign in to comment.