Skip to content
Open
Changes from 2 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
29 changes: 29 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,10 @@ use solana_program::{

pub fn assert_signer(account_info: &AccountInfo) -> ProgramResult {
if !account_info.is_signer {
msg!(
"signer assertion failed for {key}: not signer",
key = account_info.key,
);
Err(ProgramError::MissingRequiredSignature)
} else {
Ok(())
Expand All @@ -21,6 +26,10 @@ 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!(
"initialized assertion failed for {key}: not initialized",
key = account_info.key,
);
Err(error.into())
} else {
Ok(account)
Expand All @@ -33,6 +42,12 @@ pub fn assert_owned_by(
error: impl Into<ProgramError>,
) -> ProgramResult {
if account.owner != owner {
msg!(
"owner assertion failed for {key}: expected {expected}, got {actual}",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"invalid owner for {key} ({actual} != {expected})"

key = account.key,
expected = owner,
actual = account.owner
);
Err(error.into())
} else {
Ok(())
Expand All @@ -47,6 +62,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}:\n
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"invalid derivation {actual_key} != {key}"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like \n is not needed, I'll remove that.

\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 +81,12 @@ pub fn assert_rent_exempt(
error: impl Into<ProgramError>,
) -> ProgramResult {
if !rent.is_exempt(account_info.lamports(), account_info.data_len()) {
msg!(
"rent exempt assertion failed for {key}: has {balance} lamports, requires at least {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