Skip to content

Commit

Permalink
✏️ fixes for fuzz_example3 - remove unnecessary code parts
Browse files Browse the repository at this point in the history
  • Loading branch information
lukacan authored and lukacan committed Jan 11, 2024
1 parent 55943fb commit 2daf337
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ pub fn _withdraw_unlocked(ctx: Context<WithdrawUnlocked>) -> Result<()> {
b"ESCROW_PDA_AUTHORITY".as_ref(),
&[*ctx.bumps.get("escrow_pda_authority").unwrap()],
];
let token_acc_amount = ctx.accounts.escrow_token_account.amount;
msg!("withdrawal {}", escrow.withdrawal);
msg!("unlocked_amount {}", unlocked_amount);
msg!("token_acc_amount {}", token_acc_amount);

transfer(
CpiContext::new(
Expand Down
5 changes: 0 additions & 5 deletions examples/fuzz_example3/programs/fuzz_example3/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,16 @@ impl Escrow {
};

let duration = self.end_time.checked_sub(self.start_time)?;
msg!("duration {}", duration);
msg!("amount {}", self.amount);
msg!("interval {}", self.interval);

let interval_amount = self
.amount
.checked_mul(self.interval)?
.checked_div(duration)?;
msg!("interval_amount {}", interval_amount);

let nr_intervals = time
.checked_sub(self.start_time)?
.checked_div(self.interval)?
.checked_add(1)?;
msg!("nr_intervals {}", nr_intervals);

nr_intervals
.checked_mul(interval_amount)?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,51 +31,44 @@ impl<'info> InitVestingSnapshot<'info> {
let accounts = get_account_infos_option(accounts, metas)
.map_err(|_| FuzzingError::CannotGetAccounts)?;
let mut accounts_iter = accounts.into_iter();
// eprintln!("deserializign sender");
let sender: Option<Signer<'_>> = accounts_iter
.next()
.ok_or(FuzzingError::NotEnoughAccounts)?
.map(|acc| anchor_lang::accounts::signer::Signer::try_from(&acc))
.transpose()
.map_err(|_| FuzzingError::CannotDeserializeAccount)?;
// eprintln!("deserializign sender ata");
let sender_token_account: Option<anchor_lang::accounts::account::Account<TokenAccount>> =
accounts_iter
.next()
.ok_or(FuzzingError::NotEnoughAccounts)?
.map(|acc| anchor_lang::accounts::account::Account::try_from(&acc))
.transpose()
.map_err(|_| FuzzingError::CannotDeserializeAccount)?;
// eprintln!("deserializign escrow");
let escrow: Option<anchor_lang::accounts::account::Account<Escrow>> = accounts_iter
.next()
.ok_or(FuzzingError::NotEnoughAccounts)?
.map(|acc| anchor_lang::accounts::account::Account::try_from(&acc))
.transpose()
.map_err(|_| FuzzingError::CannotDeserializeAccount)?;
// eprintln!("deserializign escrow ata");
let escrow_token_account: Option<anchor_lang::accounts::account::Account<TokenAccount>> =
accounts_iter
.next()
.ok_or(FuzzingError::NotEnoughAccounts)?
.map(|acc| anchor_lang::accounts::account::Account::try_from(&acc))
.transpose()
.map_err(|_| FuzzingError::CannotDeserializeAccount)?;
// eprintln!("deserializign mint");
let mint: Option<anchor_lang::accounts::account::Account<Mint>> = accounts_iter
.next()
.ok_or(FuzzingError::NotEnoughAccounts)?
.map(|acc| anchor_lang::accounts::account::Account::try_from(&acc))
.transpose()
.map_err(|_| FuzzingError::CannotDeserializeAccount)?;
// eprintln!("deserializign token program");
let token_program: Option<anchor_lang::accounts::program::Program<Token>> = accounts_iter
.next()
.ok_or(FuzzingError::NotEnoughAccounts)?
.map(|acc| anchor_lang::accounts::program::Program::try_from(&acc))
.transpose()
.map_err(|_| FuzzingError::CannotDeserializeAccount)?;
// eprintln!("deserializign system program");
let system_program: Option<anchor_lang::accounts::program::Program<System>> = accounts_iter
.next()
.ok_or(FuzzingError::NotEnoughAccounts)?
Expand Down
40 changes: 12 additions & 28 deletions examples/fuzz_example3/trdelnik-tests/src/fuzz_instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ pub mod fuzz_example3_fuzz_instructions {
#[derive(Arbitrary, Clone)]
pub struct InitVestingData {
pub recipient: AccountId,
// the range is selected randomly
#[arbitrary(with = |u: &mut arbitrary::Unstructured| u.int_in_range(13581..=580743))]
pub amount: u64,
// we want start_at smaller than end_at
// and for testing purposes we can run tests with times from the past
Expand Down Expand Up @@ -250,47 +248,33 @@ pub mod fuzz_example3_fuzz_instructions {
) -> Result<(), &'static str> {
if let Some(escrow) = pre_ix.escrow {
let recipient = pre_ix.recipient.unwrap();
if let Some(recepient_token_account_pre) = pre_ix.recipient_token_account {
if let Some(recepient_token_account_post) = post_ix.recipient_token_account {
if let Some(recipient_token_account_pre) = pre_ix.recipient_token_account {
if let Some(recipient_token_account_post) = post_ix.recipient_token_account {
if escrow.recipient == *recipient.key {
if recepient_token_account_pre.amount
== recepient_token_account_post.amount
if recipient_token_account_pre.amount
== recipient_token_account_post.amount
{
// INFO Recipient was not able to withdraw
return Err("Recipient was not able to withdraw any funds");
} else if recepient_token_account_pre.amount + escrow.amount
!= recepient_token_account_post.amount
} else if recipient_token_account_pre.amount + escrow.amount
!= recipient_token_account_post.amount
{
if recepient_token_account_pre.amount + escrow.amount
> recepient_token_account_post.amount
if recipient_token_account_pre.amount + escrow.amount
> recipient_token_account_post.amount
{
// INFO The recipient was able to withdraw,
// but not as much as was initially intended.
eprintln!(
"Amount Mismatch (Recipient withdrew LESS) by: {}",
(recepient_token_account_pre.amount + escrow.amount)
- recepient_token_account_post.amount
);
return Err("Recipient withdrew LESS");
} else {
// INFO The recipient was able to withdraw,
// but more as was initially intended.
// This option is possible because the program uses one token accout with corresponding mint
// across multiple Escrow Transactions, this means that we can actually withdraw more
// if prior to Withdraw call, was sufficient amount transfered to the escrow token account.
// if prior to Withdraw call, was sufficient amount transferred to the escrow token account.
// (e.g. due to prior Initialization of different Escrow Transactions)
eprintln!(
"Amount Mismatch (Recipient withdrew MORE) by: {}",
recepient_token_account_post.amount
- (recepient_token_account_pre.amount + escrow.amount)
);
// For testing purposes inside debug use eprintln!()
return Err("Recipient withdrew MORE");
}
eprintln!("Before: {}", recepient_token_account_pre.amount);
eprintln!("After: {}", recepient_token_account_post.amount);
eprintln!(
"Expected: {}",
recepient_token_account_pre.amount + escrow.amount
);
return Err("Transfered amount mismatch");
}
}
}
Expand Down

0 comments on commit 2daf337

Please sign in to comment.