From 810f2a0247e1c247f086ee27f3eb0d759921a583 Mon Sep 17 00:00:00 2001 From: Abel Marnk Date: Sun, 7 Sep 2025 14:22:40 +0100 Subject: [PATCH 1/3] Clarify panic behaviour in docs --- README.md | 7 ++++--- harness/src/lib.rs | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index b63286be..47b6fb14 100644 --- a/README.md +++ b/README.md @@ -27,12 +27,13 @@ Four main API methods are offered: * `process_instruction`: Process an instruction and return the result. * `process_and_validate_instruction`: Process an instruction and perform a - series of checks on the result, panicking if any checks fail. + series of checks on the result. By default, failing checks cause a panic, + but this behavior is configurable through `Mollusk::config`. * `process_instruction_chain`: Process a chain of instructions and return the result. * `process_and_validate_instruction_chain`: Process a chain of instructions - and perform a series of checks on each result, panicking if any checks - fail. + and perform a series of checks on each result. By default, failing checks + cause a panic, but this behavior is configurable through `Mollusk::config`. ## Single Instructions diff --git a/harness/src/lib.rs b/harness/src/lib.rs index ebb7d38c..eae272c2 100644 --- a/harness/src/lib.rs +++ b/harness/src/lib.rs @@ -27,12 +27,13 @@ //! //! * `process_instruction`: Process an instruction and return the result. //! * `process_and_validate_instruction`: Process an instruction and perform a -//! series of checks on the result, panicking if any checks fail. +//! series of checks on the result. By default, failing checks cause a panic, +//! but this behavior is configurable through `Mollusk::config`. //! * `process_instruction_chain`: Process a chain of instructions and return //! the result. //! * `process_and_validate_instruction_chain`: Process a chain of instructions -//! and perform a series of checks on each result, panicking if any checks -//! fail. +//! and perform a series of checks on each result. By default, failing checks +//! cause a panic, but this behavior is configurable through `Mollusk::config`. //! //! ## Single Instructions //! From 6fab5a167a0ab64402bdff4b7ef651e0e1484338 Mon Sep 17 00:00:00 2001 From: Abel Marnk Date: Sun, 7 Sep 2025 14:32:58 +0100 Subject: [PATCH 2/3] Add custom CheckType --- result/src/check.rs | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/result/src/check.rs b/result/src/check.rs index 43fab210..30067a51 100644 --- a/result/src/check.rs +++ b/result/src/check.rs @@ -2,10 +2,10 @@ use { crate::{ - config::{compare, throw, CheckContext, Config}, + config::{CheckContext, Config, compare, throw}, types::{InstructionResult, ProgramResult}, }, - solana_account::ReadableAccount, + solana_account::{Account, ReadableAccount}, solana_instruction::error::InstructionError, solana_program_error::ProgramError, solana_pubkey::Pubkey, @@ -22,8 +22,10 @@ enum CheckType<'a> { ReturnData(&'a [u8]), /// Check a resulting account after executing the instruction. ResultingAccount(AccountCheck<'a>), - /// Check that all accounts are rent exempt + /// Check that all accounts are rent exempt. AllRentExempt, + /// Custom check for accounts. + Custom((Box bool>, &'static str)), } pub struct Check<'a> { @@ -75,10 +77,18 @@ impl<'a> Check<'a> { AccountCheckBuilder::new(pubkey) } - /// Check that all resulting accounts are rent exempt + /// Check that all resulting accounts are rent exempt. pub fn all_rent_exempt() -> Self { Check::new(CheckType::AllRentExempt) } + + /// Custom check for accounts. + pub fn custom(function: F, function_name: &'static str) -> Self + where + F: Fn(&[(Pubkey, Account)]) -> bool + 'static, + { + Check::new(CheckType::Custom((Box::new(function), function_name))) + } } enum AccountStateCheck { @@ -298,8 +308,12 @@ impl InstructionResult { } } } + CheckType::Custom((function, function_name))=>{ + println!("Calling {}", *function_name); + pass &= function(&self.resulting_accounts); + } } } pass } -} +} \ No newline at end of file From 2380daf6b9de854706c4ba0c76f39a0b87aca0b0 Mon Sep 17 00:00:00 2001 From: Abel Marnk Date: Sun, 7 Sep 2025 16:07:26 +0100 Subject: [PATCH 3/3] Fixed fmt and clippy errors --- README.md | 3 ++- harness/src/lib.rs | 3 ++- result/src/check.rs | 14 ++++++++------ 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 47b6fb14..6b56264f 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,8 @@ Four main API methods are offered: the result. * `process_and_validate_instruction_chain`: Process a chain of instructions and perform a series of checks on each result. By default, failing checks - cause a panic, but this behavior is configurable through `Mollusk::config`. + cause a panic, but this behavior is configurable through + `Mollusk::config`. ## Single Instructions diff --git a/harness/src/lib.rs b/harness/src/lib.rs index eae272c2..32850b96 100644 --- a/harness/src/lib.rs +++ b/harness/src/lib.rs @@ -33,7 +33,8 @@ //! the result. //! * `process_and_validate_instruction_chain`: Process a chain of instructions //! and perform a series of checks on each result. By default, failing checks -//! cause a panic, but this behavior is configurable through `Mollusk::config`. +//! cause a panic, but this behavior is configurable through +//! `Mollusk::config`. //! //! ## Single Instructions //! diff --git a/result/src/check.rs b/result/src/check.rs index 30067a51..7db96ff8 100644 --- a/result/src/check.rs +++ b/result/src/check.rs @@ -2,7 +2,7 @@ use { crate::{ - config::{CheckContext, Config, compare, throw}, + config::{compare, throw, CheckContext, Config}, types::{InstructionResult, ProgramResult}, }, solana_account::{Account, ReadableAccount}, @@ -11,6 +11,8 @@ use { solana_pubkey::Pubkey, }; +type AccountCheckFn = dyn Fn(&[(Pubkey, Account)]) -> bool; + enum CheckType<'a> { /// Check the number of compute units consumed by the instruction. ComputeUnitsConsumed(u64), @@ -25,7 +27,7 @@ enum CheckType<'a> { /// Check that all accounts are rent exempt. AllRentExempt, /// Custom check for accounts. - Custom((Box bool>, &'static str)), + Custom((Box, &'static str)), } pub struct Check<'a> { @@ -308,12 +310,12 @@ impl InstructionResult { } } } - CheckType::Custom((function, function_name))=>{ - println!("Calling {}", *function_name); - pass &= function(&self.resulting_accounts); + CheckType::Custom((function, function_name)) => { + println!("Calling {}", *function_name); + pass &= function(&self.resulting_accounts); } } } pass } -} \ No newline at end of file +}