Skip to content

Commit

Permalink
Fixing cheatcode panic prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
Radinyn committed Jul 5, 2023
1 parent 492928f commit ae1a1c5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 15 deletions.
4 changes: 2 additions & 2 deletions protostar-rust/src/cheatcodes_hint_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
43 changes: 30 additions & 13 deletions protostar-rust/src/running.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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()),
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

0 comments on commit ae1a1c5

Please sign in to comment.