Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions core/rust/utils/src/assertions.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use solana_program::{
account_info::AccountInfo,
entrypoint::ProgramResult,
msg,
program_error::ProgramError,
program_pack::{IsInitialized, Pack},
pubkey::Pubkey,
Expand All @@ -9,6 +10,7 @@ use solana_program::{

pub fn assert_signer(account_info: &AccountInfo) -> ProgramResult {
if !account_info.is_signer {
msg!("{key} is not a signer", key = account_info.key,);
Err(ProgramError::MissingRequiredSignature)
} else {
Ok(())
Expand All @@ -21,6 +23,7 @@ pub fn assert_initialized<T: Pack + IsInitialized>(
) -> Result<T, ProgramError> {
let account: T = T::unpack_unchecked(&account_info.data.borrow())?;
if !account.is_initialized() {
msg!("{key} is not initialized", key = account_info.key,);
Err(error.into())
} else {
Ok(account)
Expand All @@ -33,6 +36,12 @@ pub fn assert_owned_by(
error: impl Into<ProgramError>,
) -> ProgramResult {
if account.owner != owner {
msg!(
"invalid owner for {key}: expected {expected}, got {actual}",
key = account.key,
expected = owner,
actual = account.owner
);
Err(error.into())
} else {
Ok(())
Expand All @@ -47,6 +56,14 @@ pub fn assert_derivation(
) -> Result<u8, ProgramError> {
let (key, bump) = Pubkey::find_program_address(path, program_id);
if key != *account.key {
msg!(
"derivation assertion failed for {actual_key}:
\x20 expected {key} with program {program_id}, path {path:?}",
actual_key = account.key,
key = key,
program_id = program_id,
path = path,
);
return Err(error.into());
}
Ok(bump)
Expand All @@ -58,6 +75,12 @@ pub fn assert_rent_exempt(
error: impl Into<ProgramError>,
) -> ProgramResult {
if !rent.is_exempt(account_info.lamports(), account_info.data_len()) {
msg!(
"{key} is not rent-exempt: has {balance} lamports, requires {min} lamports",
key = account_info.key,
balance = account_info.lamports(),
min = rent.minimum_balance(account_info.data_len()),
);
Err(error.into())
} else {
Ok(())
Expand Down