From b73834dd7be746ae09986d389076a18ca4702028 Mon Sep 17 00:00:00 2001 From: Kartik Soneji Date: Sat, 11 Nov 2023 12:20:55 +0530 Subject: [PATCH] utils: Log assert failures for easier debugging --- core/rust/utils/src/assertions.rs | 44 +++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/core/rust/utils/src/assertions.rs b/core/rust/utils/src/assertions.rs index cf5995eb67..0f3459ad2e 100644 --- a/core/rust/utils/src/assertions.rs +++ b/core/rust/utils/src/assertions.rs @@ -1,6 +1,7 @@ use solana_program::{ account_info::AccountInfo, entrypoint::ProgramResult, + log::sol_log, program_error::ProgramError, program_pack::{IsInitialized, Pack}, pubkey::Pubkey, @@ -9,6 +10,13 @@ use solana_program::{ pub fn assert_signer(account_info: &AccountInfo) -> ProgramResult { if !account_info.is_signer { + sol_log( + format!( + "signer assertion failed for {key}: not signer", + key = account_info.key, + ) + .as_str(), + ); Err(ProgramError::MissingRequiredSignature) } else { Ok(()) @@ -21,6 +29,13 @@ pub fn assert_initialized( ) -> Result { let account: T = T::unpack_unchecked(&account_info.data.borrow())?; if !account.is_initialized() { + sol_log( + format!( + "initialized assertion failed for {key}: not initialized", + key = account_info.key, + ) + .as_str(), + ); Err(error.into()) } else { Ok(account) @@ -33,6 +48,15 @@ pub fn assert_owned_by( error: impl Into, ) -> ProgramResult { if account.owner != owner { + sol_log( + format!( + "owner assertion failed for {key}: expected {expected}, got {actual}", + key = account.key, + expected = owner, + actual = account.owner + ) + .as_str(), + ); Err(error.into()) } else { Ok(()) @@ -47,6 +71,17 @@ pub fn assert_derivation( ) -> Result { let (key, bump) = Pubkey::find_program_address(path, program_id); if key != *account.key { + sol_log( + format!( + "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, + ) + .as_str(), + ); return Err(error.into()); } Ok(bump) @@ -58,6 +93,15 @@ pub fn assert_rent_exempt( error: impl Into, ) -> ProgramResult { if !rent.is_exempt(account_info.lamports(), account_info.data_len()) { + sol_log( + format!( + "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()), + ) + .as_str(), + ); Err(error.into()) } else { Ok(())