-
Notifications
You must be signed in to change notification settings - Fork 531
feature: mpl-utils: Log assert failures for easier debugging
#1163
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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, | ||
|
|
@@ -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(()) | ||
|
|
@@ -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", | ||
KartikSoneji marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| key = account_info.key, | ||
| ); | ||
| Err(error.into()) | ||
| } else { | ||
| Ok(account) | ||
|
|
@@ -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}", | ||
|
||
| key = account.key, | ||
| expected = owner, | ||
| actual = account.owner | ||
| ); | ||
| Err(error.into()) | ||
| } else { | ||
| Ok(()) | ||
|
|
@@ -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 | ||
|
||
| \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) | ||
|
|
@@ -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", | ||
KartikSoneji marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| key = account_info.key, | ||
| balance = account_info.lamports(), | ||
| min = rent.minimum_balance(account_info.data_len()), | ||
| ); | ||
| Err(error.into()) | ||
| } else { | ||
| Ok(()) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.