Skip to content

Commit f15e902

Browse files
committed
utils: Log assert failures for easier debugging
1 parent 23789d8 commit f15e902

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

core/rust/utils/src/assertions.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use solana_program::{
22
account_info::AccountInfo,
33
entrypoint::ProgramResult,
4+
log::sol_log,
45
program_error::ProgramError,
56
program_pack::{IsInitialized, Pack},
67
pubkey::Pubkey,
@@ -9,6 +10,13 @@ use solana_program::{
910

1011
pub fn assert_signer(account_info: &AccountInfo) -> ProgramResult {
1112
if !account_info.is_signer {
13+
sol_log(
14+
format!(
15+
"signer assertion failed for {key}: not signer",
16+
key = account_info.key,
17+
)
18+
.as_str(),
19+
);
1220
Err(ProgramError::MissingRequiredSignature)
1321
} else {
1422
Ok(())
@@ -21,6 +29,13 @@ pub fn assert_initialized<T: Pack + IsInitialized>(
2129
) -> Result<T, ProgramError> {
2230
let account: T = T::unpack_unchecked(&account_info.data.borrow())?;
2331
if !account.is_initialized() {
32+
sol_log(
33+
format!(
34+
"initialized assertion failed for {key}: not initialized",
35+
key = account_info.key,
36+
)
37+
.as_str(),
38+
);
2439
Err(error.into())
2540
} else {
2641
Ok(account)
@@ -33,6 +48,15 @@ pub fn assert_owned_by(
3348
error: impl Into<ProgramError>,
3449
) -> ProgramResult {
3550
if account.owner != owner {
51+
sol_log(
52+
format!(
53+
"owner assertion failed for {key}: expected {expected}, got {actual}",
54+
key = account.key,
55+
expected = owner,
56+
actual = account.owner
57+
)
58+
.as_str(),
59+
);
3660
Err(error.into())
3761
} else {
3862
Ok(())
@@ -47,6 +71,17 @@ pub fn assert_derivation(
4771
) -> Result<u8, ProgramError> {
4872
let (key, bump) = Pubkey::find_program_address(path, program_id);
4973
if key != *account.key {
74+
sol_log(
75+
format!(
76+
"derivation assertion failed for {actual_key}:\n
77+
\x20 expected {key} with program {program_id}, path {path:?}",
78+
actual_key = account.key,
79+
key = key,
80+
program_id = program_id,
81+
path = path,
82+
)
83+
.as_str(),
84+
);
5085
return Err(error.into());
5186
}
5287
Ok(bump)
@@ -58,6 +93,15 @@ pub fn assert_rent_exempt(
5893
error: impl Into<ProgramError>,
5994
) -> ProgramResult {
6095
if !rent.is_exempt(account_info.lamports(), account_info.data_len()) {
96+
sol_log(
97+
format!(
98+
"rent exempt assertion failed for {key}: has {balance} lamports, requires at least {min} lamports",
99+
key = account_info.key,
100+
balance = account_info.lamports(),
101+
min = rent.minimum_balance(account_info.data_len()),
102+
)
103+
.as_str(),
104+
);
61105
Err(error.into())
62106
} else {
63107
Ok(())

0 commit comments

Comments
 (0)