From ae1a1c5ee899dea892c0fd5e61880b5457a739ad Mon Sep 17 00:00:00 2001 From: Radinyn Date: Wed, 5 Jul 2023 09:38:03 +0200 Subject: [PATCH] Fixing cheatcode panic prototype --- .../src/cheatcodes_hint_processor.rs | 4 +- protostar-rust/src/running.rs | 43 +++++++++++++------ .../declare_test/tests/test_declare.cairo | 5 +++ 3 files changed, 37 insertions(+), 15 deletions(-) diff --git a/protostar-rust/src/cheatcodes_hint_processor.rs b/protostar-rust/src/cheatcodes_hint_processor.rs index 5c695bb36..f4a97c4d7 100644 --- a/protostar-rust/src/cheatcodes_hint_processor.rs +++ b/protostar-rust/src/cheatcodes_hint_processor.rs @@ -254,12 +254,12 @@ fn execute_cheatcode_hint( ) }); - let sierra_path = starknet_artifacts.contracts.iter().find_map(|contract| { + let Some(sierra_path) = starknet_artifacts.contracts.iter().find_map(|contract| { if contract.contract_name == contract_value_as_short_str { return Some(contract.artifacts.sierra.clone()); } None - }).unwrap_or_else(|| panic!("Failed to find contract {contract_value_as_short_str} in starknet_artifacts.json")); + }) else {return Err(HintError::CustomHint("Failed to find contract {contract_value_as_short_str} in starknet_artifacts.json".into()))}; let sierra_path = current_dir.join(sierra_path); let file = std::fs::File::open(&sierra_path) diff --git a/protostar-rust/src/running.rs b/protostar-rust/src/running.rs index 30a11ab5b..74fe84b68 100644 --- a/protostar-rust/src/running.rs +++ b/protostar-rust/src/running.rs @@ -1,14 +1,17 @@ use std::collections::HashMap; -use anyhow::{Context, Result}; +use anyhow::Result; use blockifier::transaction::transaction_utils_for_protostar::create_state_with_trivial_validation_account; use cairo_vm::serde::deserialize_program::HintParams; -use itertools::chain; +use itertools::{chain, Itertools}; +use cairo_felt::Felt252; use cairo_lang_casm::hints::Hint; use cairo_lang_casm::instructions::Instruction; use cairo_lang_runner::casm_run::hint_to_hint_params; -use cairo_lang_runner::CairoHintProcessor as CoreCairoHintProcessor; +use cairo_lang_runner::{ + CairoHintProcessor as CoreCairoHintProcessor, RunResultValue, RunnerError, +}; use cairo_lang_runner::{RunResult, SierraCasmRunner, StarknetState}; use test_collector::TestConfig; @@ -69,14 +72,28 @@ pub(crate) fn run_from_test_config( original_cairo_hint_processor: core_cairo_hint_processor, blockifier_state: Some(create_state_with_trivial_validation_account()), }; - let result = runner - .run_function( - runner.find_function(config.name.as_str())?, - &mut cairo_hint_processor, - hints_dict, - instructions, - builtins, - ) - .with_context(|| format!("Failed to run the function `{}`.", config.name.as_str()))?; - Ok(result) + + // TODO(Radinyn) 1: Add custom class wrapping RunResult + match runner.run_function( + runner.find_function(config.name.as_str())?, + &mut cairo_hint_processor, + hints_dict, + instructions, + builtins, + ) { + Ok(result) => Ok(result), + // CairoRunError comes from VirtualMachineError which may come from HintException that originates in the cheatcode processor + Err(RunnerError::CairoRunError(_)) => Ok(RunResult { + gas_counter: None, + memory: vec![], + // TODO(Radinyn) 2: add the string during creating custom class instance (recover it from the CairoRunError) + value: RunResultValue::Panic( + vec![4417637, 6386787, 7300197, 2123122, 7499634] // "Cheatcode error" + .into_iter() + .map(Felt252::from) + .collect_vec(), + ), + }), + Err(err) => Err(err.into()), + } } diff --git a/protostar-rust/tests/data/declare_test/tests/test_declare.cairo b/protostar-rust/tests/data/declare_test/tests/test_declare.cairo index 21bbb3fb6..f6c5f1c8d 100644 --- a/protostar-rust/tests/data/declare_test/tests/test_declare.cairo +++ b/protostar-rust/tests/data/declare_test/tests/test_declare.cairo @@ -18,3 +18,8 @@ fn multiple_contracts() { assert(class_hash != class_hash2, 'class hashes neq'); } + +#[test] +fn non_existant_contract() { + let class_hash = declare('GoodbyeStarknet').unwrap(); +}