diff --git a/crates/client/tests/test_data/expected_source_codes/expected_accounts_snapshots.rs b/crates/client/tests/test_data/expected_source_codes/expected_accounts_snapshots.rs index f21787bf8..205983cb5 100644 --- a/crates/client/tests/test_data/expected_source_codes/expected_accounts_snapshots.rs +++ b/crates/client/tests/test_data/expected_source_codes/expected_accounts_snapshots.rs @@ -26,30 +26,38 @@ impl<'info> InitVestingSnapshot<'info> { accounts: &'info mut [Option], ) -> core::result::Result { let accounts = get_account_infos_option(accounts, metas) - .map_err(|_| FuzzingError::CannotGetAccounts)?; + .map_err(|_| FuzzingError::NotAbleToObtainAccountInfos)?; let mut accounts_iter = accounts.into_iter(); let sender: Signer<'_> = accounts_iter .next() - .ok_or(FuzzingError::NotEnoughAccounts)? + .ok_or(FuzzingError::NotEnoughAccounts("sender".to_string()))? .map(|acc| anchor_lang::accounts::signer::Signer::try_from(&acc)) - .ok_or(FuzzingError::AccountNotFound)? - .map_err(|_| FuzzingError::CannotDeserializeAccount)?; + .ok_or(FuzzingError::AccountNotFound("sender".to_string()))? + .map_err(|_| FuzzingError::CannotDeserializeAccount("sender".to_string()))?; let sender_token_account: anchor_lang::accounts::account::Account = accounts_iter .next() - .ok_or(FuzzingError::NotEnoughAccounts)? + .ok_or(FuzzingError::NotEnoughAccounts( + "sender_token_account".to_string(), + ))? .map(|acc| anchor_lang::accounts::account::Account::try_from(&acc)) - .ok_or(FuzzingError::AccountNotFound)? - .map_err(|_| FuzzingError::CannotDeserializeAccount)?; + .ok_or(FuzzingError::AccountNotFound( + "sender_token_account".to_string(), + ))? + .map_err(|_| { + FuzzingError::CannotDeserializeAccount("sender_token_account".to_string()) + })?; let escrow: Option> = accounts_iter .next() - .ok_or(FuzzingError::NotEnoughAccounts)? + .ok_or(FuzzingError::NotEnoughAccounts("escrow".to_string()))? .map(|acc| { if acc.key() != PROGRAM_ID { anchor_lang::accounts::account::Account::try_from(&acc) - .map_err(|e| e.to_string()) + .map_err(|_| FuzzingError::CannotDeserializeAccount("escrow".to_string())) } else { - Err("Optional account not provided".to_string()) + Err(FuzzingError::OptionalAccountNotProvided( + "escrow".to_string(), + )) } }) .transpose() @@ -57,28 +65,36 @@ impl<'info> InitVestingSnapshot<'info> { let escrow_token_account: anchor_lang::accounts::account::Account = accounts_iter .next() - .ok_or(FuzzingError::NotEnoughAccounts)? + .ok_or(FuzzingError::NotEnoughAccounts( + "escrow_token_account".to_string(), + ))? .map(|acc| anchor_lang::accounts::account::Account::try_from(&acc)) - .ok_or(FuzzingError::AccountNotFound)? - .map_err(|_| FuzzingError::CannotDeserializeAccount)?; + .ok_or(FuzzingError::AccountNotFound( + "escrow_token_account".to_string(), + ))? + .map_err(|_| { + FuzzingError::CannotDeserializeAccount("escrow_token_account".to_string()) + })?; let mint: anchor_lang::accounts::account::Account = accounts_iter .next() - .ok_or(FuzzingError::NotEnoughAccounts)? + .ok_or(FuzzingError::NotEnoughAccounts("mint".to_string()))? .map(|acc| anchor_lang::accounts::account::Account::try_from(&acc)) - .ok_or(FuzzingError::AccountNotFound)? - .map_err(|_| FuzzingError::CannotDeserializeAccount)?; + .ok_or(FuzzingError::AccountNotFound("mint".to_string()))? + .map_err(|_| FuzzingError::CannotDeserializeAccount("mint".to_string()))?; let token_program: anchor_lang::accounts::program::Program = accounts_iter .next() - .ok_or(FuzzingError::NotEnoughAccounts)? + .ok_or(FuzzingError::NotEnoughAccounts("token_program".to_string()))? .map(|acc| anchor_lang::accounts::program::Program::try_from(&acc)) - .ok_or(FuzzingError::AccountNotFound)? - .map_err(|_| FuzzingError::CannotDeserializeAccount)?; + .ok_or(FuzzingError::AccountNotFound("token_program".to_string()))? + .map_err(|_| FuzzingError::CannotDeserializeAccount("token_program".to_string()))?; let system_program: anchor_lang::accounts::program::Program = accounts_iter .next() - .ok_or(FuzzingError::NotEnoughAccounts)? + .ok_or(FuzzingError::NotEnoughAccounts( + "system_program".to_string(), + ))? .map(|acc| anchor_lang::accounts::program::Program::try_from(&acc)) - .ok_or(FuzzingError::AccountNotFound)? - .map_err(|_| FuzzingError::CannotDeserializeAccount)?; + .ok_or(FuzzingError::AccountNotFound("system_program".to_string()))? + .map_err(|_| FuzzingError::CannotDeserializeAccount("system_program".to_string()))?; Ok(Self { sender, sender_token_account, @@ -96,30 +112,38 @@ impl<'info> WithdrawUnlockedSnapshot<'info> { accounts: &'info mut [Option], ) -> core::result::Result { let accounts = get_account_infos_option(accounts, metas) - .map_err(|_| FuzzingError::CannotGetAccounts)?; + .map_err(|_| FuzzingError::NotAbleToObtainAccountInfos)?; let mut accounts_iter = accounts.into_iter(); let recipient: Signer<'_> = accounts_iter .next() - .ok_or(FuzzingError::NotEnoughAccounts)? + .ok_or(FuzzingError::NotEnoughAccounts("recipient".to_string()))? .map(|acc| anchor_lang::accounts::signer::Signer::try_from(&acc)) - .ok_or(FuzzingError::AccountNotFound)? - .map_err(|_| FuzzingError::CannotDeserializeAccount)?; + .ok_or(FuzzingError::AccountNotFound("recipient".to_string()))? + .map_err(|_| FuzzingError::CannotDeserializeAccount("recipient".to_string()))?; let recipient_token_account: anchor_lang::accounts::account::Account = accounts_iter .next() - .ok_or(FuzzingError::NotEnoughAccounts)? + .ok_or(FuzzingError::NotEnoughAccounts( + "recipient_token_account".to_string(), + ))? .map(|acc| anchor_lang::accounts::account::Account::try_from(&acc)) - .ok_or(FuzzingError::AccountNotFound)? - .map_err(|_| FuzzingError::CannotDeserializeAccount)?; + .ok_or(FuzzingError::AccountNotFound( + "recipient_token_account".to_string(), + ))? + .map_err(|_| { + FuzzingError::CannotDeserializeAccount("recipient_token_account".to_string()) + })?; let escrow: Option> = accounts_iter .next() - .ok_or(FuzzingError::NotEnoughAccounts)? + .ok_or(FuzzingError::NotEnoughAccounts("escrow".to_string()))? .map(|acc| { if acc.key() != PROGRAM_ID { anchor_lang::accounts::account::Account::try_from(&acc) - .map_err(|e| e.to_string()) + .map_err(|_| FuzzingError::CannotDeserializeAccount("escrow".to_string())) } else { - Err("Optional account not provided".to_string()) + Err(FuzzingError::OptionalAccountNotProvided( + "escrow".to_string(), + )) } }) .transpose() @@ -127,32 +151,44 @@ impl<'info> WithdrawUnlockedSnapshot<'info> { let escrow_token_account: anchor_lang::accounts::account::Account = accounts_iter .next() - .ok_or(FuzzingError::NotEnoughAccounts)? + .ok_or(FuzzingError::NotEnoughAccounts( + "escrow_token_account".to_string(), + ))? .map(|acc| anchor_lang::accounts::account::Account::try_from(&acc)) - .ok_or(FuzzingError::AccountNotFound)? - .map_err(|_| FuzzingError::CannotDeserializeAccount)?; + .ok_or(FuzzingError::AccountNotFound( + "escrow_token_account".to_string(), + ))? + .map_err(|_| { + FuzzingError::CannotDeserializeAccount("escrow_token_account".to_string()) + })?; let escrow_pda_authority = accounts_iter .next() - .ok_or(FuzzingError::NotEnoughAccounts)? - .ok_or(FuzzingError::AccountNotFound)?; + .ok_or(FuzzingError::NotEnoughAccounts( + "escrow_pda_authority".to_string(), + ))? + .ok_or(FuzzingError::AccountNotFound( + "escrow_pda_authority".to_string(), + ))?; let mint: anchor_lang::accounts::account::Account = accounts_iter .next() - .ok_or(FuzzingError::NotEnoughAccounts)? + .ok_or(FuzzingError::NotEnoughAccounts("mint".to_string()))? .map(|acc| anchor_lang::accounts::account::Account::try_from(&acc)) - .ok_or(FuzzingError::AccountNotFound)? - .map_err(|_| FuzzingError::CannotDeserializeAccount)?; + .ok_or(FuzzingError::AccountNotFound("mint".to_string()))? + .map_err(|_| FuzzingError::CannotDeserializeAccount("mint".to_string()))?; let token_program: anchor_lang::accounts::program::Program = accounts_iter .next() - .ok_or(FuzzingError::NotEnoughAccounts)? + .ok_or(FuzzingError::NotEnoughAccounts("token_program".to_string()))? .map(|acc| anchor_lang::accounts::program::Program::try_from(&acc)) - .ok_or(FuzzingError::AccountNotFound)? - .map_err(|_| FuzzingError::CannotDeserializeAccount)?; + .ok_or(FuzzingError::AccountNotFound("token_program".to_string()))? + .map_err(|_| FuzzingError::CannotDeserializeAccount("token_program".to_string()))?; let system_program: anchor_lang::accounts::program::Program = accounts_iter .next() - .ok_or(FuzzingError::NotEnoughAccounts)? + .ok_or(FuzzingError::NotEnoughAccounts( + "system_program".to_string(), + ))? .map(|acc| anchor_lang::accounts::program::Program::try_from(&acc)) - .ok_or(FuzzingError::AccountNotFound)? - .map_err(|_| FuzzingError::CannotDeserializeAccount)?; + .ok_or(FuzzingError::AccountNotFound("system_program".to_string()))? + .map_err(|_| FuzzingError::CannotDeserializeAccount("system_program".to_string()))?; Ok(Self { recipient, recipient_token_account, diff --git a/crates/client/tests/test_data/fuzzer_macros/fuzz_fuzz_test_executor.expanded.rs b/crates/client/tests/test_data/fuzzer_macros/fuzz_fuzz_test_executor.expanded.rs index 86e9a8d64..956311678 100644 --- a/crates/client/tests/test_data/fuzzer_macros/fuzz_fuzz_test_executor.expanded.rs +++ b/crates/client/tests/test_data/fuzzer_macros/fuzz_fuzz_test_executor.expanded.rs @@ -12,45 +12,29 @@ impl FuzzTestExecutor for FuzzInstruction { ) -> core::result::Result<(), Box> { match self { FuzzInstruction::InitVesting(ix) => { - let (mut signers, metas) = match ix - .get_accounts(client, &mut accounts.borrow_mut()) - .map_err(|e| { - FuzzingErrorWithOrigin::from(e) - .with_origin(Origin::Instruction(self.to_string())) - }) + 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())) + }) { - Ok(acc) => acc, - Err(e) => { - { - ::std::io::_eprint(format_args!("{0}\n", e)); - }; - ::core::panicking::panic("explicit panic") - } - }; - let mut snapshot = Snapshot::new(&metas, ix); - match snapshot.capture_before(client) { - Ok(_) => {} - Err(e) => { - { - ::std::io::_eprint(format_args!("{0}\n", e)); - }; - ::core::panicking::panic("explicit panic") - } + acc + } else { + return Ok(()); }; - let data = match ix - .get_data(client, &mut accounts.borrow_mut()) - .map_err(|e| { - FuzzingErrorWithOrigin::from(e) - .with_origin(Origin::Instruction(self.to_string())) - }) + let mut snaphot = Snapshot::new(&metas, ix); + 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())) + }) { - Ok(data) => data, - Err(e) => { - { - ::std::io::_eprint(format_args!("{0}\n", e)); - }; - ::core::panicking::panic("explicit panic") - } + data + } else { + return Ok(()); }; let ixx = Instruction { program_id, @@ -66,91 +50,66 @@ impl FuzzTestExecutor for FuzzInstruction { transaction.sign(&sig, client.get_last_blockhash()); let res = client .process_transaction(transaction) - .map_err(|e| { - FuzzClientErrorWithOrigin::from(e) - .with_origin(Origin::Instruction(self.to_string())) - }); - match snapshot.capture_after(client) { - Ok(_) => {} - Err(e) => { - { - ::std::io::_eprint(format_args!("{0}\n", e)); - }; - ::core::panicking::panic("explicit panic") - } - }; - match snapshot + .map_err(|e| e.with_origin(Origin::Instruction(self.to_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()))) + .unwrap(); + if let Err(e) + = ix + .check(acc_before, acc_after, data) + .map_err(|e| { + e.with_origin(Origin::Instruction(self.to_string())) + }) { - Ok((acc_before, acc_after)) => { - match ix - .check(acc_before, acc_after, data) - .map_err(|e| { - FuzzingErrorWithOrigin::from(e) - .with_origin(Origin::Instruction(self.to_string())) - }) - { - Ok(_) => {} - Err(e) => { - { - ::std::io::_eprint(format_args!("{0}\n", e)); - }; - ::core::panicking::panic("explicit panic") - } - }; - } - Err(e) => { - { - ::std::io::_eprint(format_args!("{0}\n", e)); - }; - ::core::panicking::panic("explicit panic") + { + ::std::io::_eprint( + format_args!( + "Custom check after the {0} instruction did not pass with the error message: {1}\n", + self, e, + ), + ); + }; + { + ::std::io::_eprint( + format_args!( + "Instruction data submitted to the instruction were:\n", + ), + ); + }; + { + ::core::panicking::panic_display(&e); } - }; + } if res.is_err() { return Ok(()); } } FuzzInstruction::WithdrawUnlocked(ix) => { - let (mut signers, metas) = match ix - .get_accounts(client, &mut accounts.borrow_mut()) - .map_err(|e| { - FuzzingErrorWithOrigin::from(e) - .with_origin(Origin::Instruction(self.to_string())) - }) + 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())) + }) { - Ok(acc) => acc, - Err(e) => { - { - ::std::io::_eprint(format_args!("{0}\n", e)); - }; - ::core::panicking::panic("explicit panic") - } - }; - let mut snapshot = Snapshot::new(&metas, ix); - match snapshot.capture_before(client) { - Ok(_) => {} - Err(e) => { - { - ::std::io::_eprint(format_args!("{0}\n", e)); - }; - ::core::panicking::panic("explicit panic") - } + acc + } else { + return Ok(()); }; - let data = match ix - .get_data(client, &mut accounts.borrow_mut()) - .map_err(|e| { - FuzzingErrorWithOrigin::from(e) - .with_origin(Origin::Instruction(self.to_string())) - }) + let mut snaphot = Snapshot::new(&metas, ix); + 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())) + }) { - Ok(data) => data, - Err(e) => { - { - ::std::io::_eprint(format_args!("{0}\n", e)); - }; - ::core::panicking::panic("explicit panic") - } + data + } else { + return Ok(()); }; let ixx = Instruction { program_id, @@ -166,47 +125,38 @@ impl FuzzTestExecutor for FuzzInstruction { transaction.sign(&sig, client.get_last_blockhash()); let res = client .process_transaction(transaction) - .map_err(|e| { - FuzzClientErrorWithOrigin::from(e) - .with_origin(Origin::Instruction(self.to_string())) - }); - match snapshot.capture_after(client) { - Ok(_) => {} - Err(e) => { - { - ::std::io::_eprint(format_args!("{0}\n", e)); - }; - ::core::panicking::panic("explicit panic") - } - }; - match snapshot + .map_err(|e| e.with_origin(Origin::Instruction(self.to_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()))) + .unwrap(); + if let Err(e) + = ix + .check(acc_before, acc_after, data) + .map_err(|e| { + e.with_origin(Origin::Instruction(self.to_string())) + }) { - Ok((acc_before, acc_after)) => { - match ix - .check(acc_before, acc_after, data) - .map_err(|e| { - FuzzingErrorWithOrigin::from(e) - .with_origin(Origin::Instruction(self.to_string())) - }) - { - Ok(_) => {} - Err(e) => { - { - ::std::io::_eprint(format_args!("{0}\n", e)); - }; - ::core::panicking::panic("explicit panic") - } - }; - } - Err(e) => { - { - ::std::io::_eprint(format_args!("{0}\n", e)); - }; - ::core::panicking::panic("explicit panic") + { + ::std::io::_eprint( + format_args!( + "Custom check after the {0} instruction did not pass with the error message: {1}\n", + self, e, + ), + ); + }; + { + ::std::io::_eprint( + format_args!( + "Instruction data submitted to the instruction were:\n", + ), + ); + }; + { + ::core::panicking::panic_display(&e); } - }; + } if res.is_err() { return Ok(()); }