Skip to content

Commit

Permalink
Fix parsing EntryPointExecutionError (#1420)
Browse files Browse the repository at this point in the history
<!-- Reference any GitHub issues resolved by this PR -->

Closes #1406

## Introduced changes

<!-- A brief description of the changes -->

- Fix `invalid entry point error` in segment arena test
- Fix `assert_panic` macro
- The error string from `CallContractFailure::Error` looks now like:
```rust
0x73686f7274737472696e67 ('shortstring')\n0x0 ('')\n0x800000000000011000000000000000000000000000000000000000000000000\n0x73686f7274737472696e6732 ('shortstring2')
```

## Checklist

<!-- Make sure all of these are complete -->

- [x] Linked relevant issue
- [x] Updated relevant documentation
- [x] Added relevant tests
- [x] Performed self-review of the code
- [x] Added changes to `CHANGELOG.md`
  • Loading branch information
drknzz authored Dec 20, 2023
1 parent 93b2a2a commit 0114151
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 15 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Argument `max-fee` in `account deploy` is now optional

#### Fixed

- Parsing panic data from call contract result

## [0.13.0] - 2023-12-14

### Forge
Expand Down Expand Up @@ -226,7 +230,7 @@ from now on the only officially supported cairo compiler version is 2

- `var` library function for reading environmental variables

### Fixed
#### Fixed
- Using any concrete `block_id` when using forking mode, would lead to crashes

## [0.7.0] - 2023-09-27
Expand Down Expand Up @@ -268,7 +272,7 @@ from now on the only officially supported cairo compiler version is 2
- printing failures summary at the end of an execution
- filtering tests now uses an absolute module tree path — it is possible to filter tests by module names, etc.

### Fixed
#### Fixed

- non-zero exit code is returned when any tests fail
- mock_call works with dispatchers if contract does not exists
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use anyhow::Result;
use blockifier::execution::execution_utils::stark_felt_to_felt;
use cairo_lang_runner::casm_run::format_next_item;
use std::sync::Arc;

use crate::constants::TEST_ADDRESS;
Expand All @@ -18,7 +20,6 @@ use blockifier::execution::{
};
use blockifier::state::errors::StateError;
use cairo_felt::Felt252;
use cairo_lang_runner::short_string::as_cairo_short_string;
use starknet_api::core::PatriciaKey;
use starknet_api::patricia_key;
use starknet_api::{
Expand Down Expand Up @@ -82,11 +83,19 @@ impl CallContractFailure {
.map(|data| Felt252::from_bytes_be(data.bytes()))
.collect();

let err_data_str = err_data
.iter()
.map(|x| as_cairo_short_string(x).unwrap())
.collect::<Vec<String>>()
.join("\n");
// blockifier/src/execution_utils:274 (format_panic_data) (modified)
let err_data_str = {
let mut felts = error_data.iter().map(|felt| stark_felt_to_felt(*felt));
let mut items = Vec::new();
while let Some(item) = format_next_item(&mut felts) {
items.push(item.quote_if_string());
}
if let [item] = &items[..] {
item.clone()
} else {
items.join("\n").to_string()
}
};

for invalid_calldata_msg in [
"Failed to deserialize param #",
Expand Down
1 change: 1 addition & 0 deletions crates/cheatnet/tests/builtins/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
mod panic_call;
mod segment_arena;
59 changes: 59 additions & 0 deletions crates/cheatnet/tests/builtins/panic_call.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use crate::common::state::create_cheatnet_state;
use crate::common::{deploy_contract, felt_selector_from_name, state::create_cached_state};
use crate::{assert_error, assert_panic};
use cairo_felt::Felt252;
use cheatnet::runtime_extensions::call_to_blockifier_runtime_extension::rpc::call_contract;
use conversions::felt252::FromShortString;
use num_traits::Bounded;

#[test]
fn call_contract_error() {
let mut cached_state = create_cached_state();
let (mut blockifier_state, mut cheatnet_state) = create_cheatnet_state(&mut cached_state);

let contract_address =
deploy_contract(&mut blockifier_state, &mut cheatnet_state, "PanicCall", &[]);

let selector = felt_selector_from_name("panic_call");

let output = call_contract(
&mut blockifier_state,
&mut cheatnet_state,
&contract_address,
&selector,
&[Felt252::from(420)],
)
.unwrap();

assert_error!(output, "0x496e70757420746f6f206c6f6e6720666f7220617267756d656e7473 ('Input too long for arguments')");
}

#[test]
fn call_contract_panic() {
let mut cached_state = create_cached_state();
let (mut blockifier_state, mut cheatnet_state) = create_cheatnet_state(&mut cached_state);

let contract_address =
deploy_contract(&mut blockifier_state, &mut cheatnet_state, "PanicCall", &[]);

let selector = felt_selector_from_name("panic_call");

let output = call_contract(
&mut blockifier_state,
&mut cheatnet_state,
&contract_address,
&selector,
&[],
)
.unwrap();

assert_panic!(
output,
vec![
Felt252::from_short_string("shortstring").unwrap(),
Felt252::from(0),
Felt252::max_value(),
Felt252::from_short_string("shortstring2").unwrap()
]
);
}
6 changes: 4 additions & 2 deletions crates/cheatnet/tests/builtins/segment_arena.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::assert_success;
use crate::common::state::create_cheatnet_state;
use crate::common::{deploy_contract, felt_selector_from_name, state::create_cached_state};
use cheatnet::runtime_extensions::call_to_blockifier_runtime_extension::rpc::call_contract;
Expand All @@ -19,7 +20,8 @@ fn segment_arena_simple() {
&contract_address,
&selector,
&[],
);
)
.unwrap();

assert!(matches!(output, Result::Ok(_)));
assert_success!(output, &[]);
}
4 changes: 2 additions & 2 deletions crates/cheatnet/tests/cheatcodes/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ fn deploy_missing_arguments_in_constructor() {

assert!(match output {
Err(CheatcodeError::Unrecoverable(EnhancedHintError::Hint(HintError::CustomHint(msg)))) =>
msg.as_ref() == "Failed to deserialize param #2",
msg.as_ref() == "0x4661696c656420746f20646573657269616c697a6520706172616d202332 ('Failed to deserialize param #2')",
_ => false,
});
}
Expand All @@ -270,7 +270,7 @@ fn deploy_too_many_arguments_in_constructor() {

assert!(match output {
Err(CheatcodeError::Unrecoverable(EnhancedHintError::Hint(HintError::CustomHint(msg)))) =>
msg.as_ref() == "Input too long for arguments",
msg.as_ref() == "0x496e70757420746f6f206c6f6e6720666f7220617267756d656e7473 ('Input too long for arguments')",
_ => false,
});
}
Expand Down
4 changes: 2 additions & 2 deletions crates/cheatnet/tests/common/assertions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ macro_rules! assert_panic {
assert!(
matches!(
$call_contract_output.result,
cheatnet::rpc::CallContractResult::Failure(
cheatnet::rpc::CallContractFailure::Panic { panic_data, .. }
cheatnet::runtime_extensions::call_to_blockifier_runtime_extension::rpc::CallContractResult::Failure(
cheatnet::runtime_extensions::call_to_blockifier_runtime_extension::rpc::CallContractFailure::Panic { panic_data, .. }
)
if panic_data == $expected_data
)
Expand Down
1 change: 1 addition & 0 deletions crates/cheatnet/tests/contracts/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ mod elect;
mod starknet;
mod warp;
mod segment_arena_user;
mod panic_call;
mod store_load;
17 changes: 17 additions & 0 deletions crates/cheatnet/tests/contracts/src/panic_call.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#[starknet::contract]
mod PanicCall {
#[storage]
struct Storage {}

#[external(v0)]
fn panic_call(ref self: ContractState) {
panic(
array![
'shortstring',
0,
0x800000000000011000000000000000000000000000000000000000000000000,
'shortstring2'
]
);
}
}
3 changes: 2 additions & 1 deletion crates/cheatnet/tests/contracts/src/segment_arena_user.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
mod SegmentArenaUser {
#[storage]
struct Storage {}
#[abi(embed_v0)]

#[external(v0)]
fn interface_function(ref self: ContractState) {
let felt_dict: Felt252Dict<felt252> = Default::default();
}
Expand Down

0 comments on commit 0114151

Please sign in to comment.