Skip to content

Commit

Permalink
fix: fix some of the call contract tests
Browse files Browse the repository at this point in the history
  • Loading branch information
varex83 committed Oct 30, 2024
1 parent c04006b commit 2bac70d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 41 deletions.
8 changes: 4 additions & 4 deletions crates/blockifier/src/execution/native/syscall_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ impl<'state> StarknetSyscallHandler for &mut NativeSyscallHandler<'state> {
let contract_address = ContractAddress::try_from(address)
.map_err(|error| encode_str_as_felts(&error.to_string()))?;
if self.context.execution_mode == ExecutionMode::Validate
&& self.contract_address != contract_address
&& self.call.storage_address != contract_address
{
let err = SyscallExecutionError::InvalidSyscallInExecutionMode {
syscall_name: "call_contract".to_string(),
Expand All @@ -213,7 +213,7 @@ impl<'state> StarknetSyscallHandler for &mut NativeSyscallHandler<'state> {
entry_point_selector: EntryPointSelector(entry_point_selector),
calldata: wrapper_calldata,
storage_address: contract_address,
caller_address: self.contract_address,
caller_address: self.call.caller_address,
call_type: CallType::Call,
initial_gas: u64::try_from(*remaining_gas)
.expect("Failed to convert gas from u128 to u64."),
Expand All @@ -236,7 +236,7 @@ impl<'state> StarknetSyscallHandler for &mut NativeSyscallHandler<'state> {

let key = StorageKey::try_from(address).map_err(|e| encode_str_as_felts(&e.to_string()))?;

let read_result = self.state.get_storage_at(self.contract_address, key);
let read_result = self.state.get_storage_at(self.call.storage_address, key);
let value = read_result.map_err(|e| encode_str_as_felts(&e.to_string()))?;

self.accessed_keys.insert(key);
Expand All @@ -261,7 +261,7 @@ impl<'state> StarknetSyscallHandler for &mut NativeSyscallHandler<'state> {
let key = StorageKey::try_from(address).map_err(|e| encode_str_as_felts(&e.to_string()))?;
self.accessed_keys.insert(key);

let write_result = self.state.set_storage_at(self.contract_address, key, value);
let write_result = self.state.set_storage_at(self.call.storage_address, key, value);
write_result.map_err(|e| encode_str_as_felts(&e.to_string()))?;

Ok(())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,14 @@ fn test_call_contract_that_panics() {
);
}

#[test_case(
FeatureContract::TestContract(CairoVersion::Native),
FeatureContract::TestContract(CairoVersion::Native),
191870;
"Call Contract between two contracts using Native"
#[cfg_attr(
feature = "cairo_native",
test_case(
FeatureContract::TestContract(CairoVersion::Native),
FeatureContract::TestContract(CairoVersion::Native),
191870;
"Call Contract between two contracts using Native"
)
)]
#[test_case(
FeatureContract::TestContract(CairoVersion::Cairo1),
Expand Down Expand Up @@ -112,22 +115,50 @@ fn test_call_contract(
);
}

/// Cairo0 / Old Cairo1 / Cairo1 calls to Cairo0 / Old Cairo1/ Cairo1.
/// Cairo0 / Old Cairo1 / Cairo1 / Native calls to Cairo0 / Old Cairo1 / Cairo1 / Native.
#[cfg(feature = "cairo_native")]
#[rstest]
fn test_tracked_resources(
#[values(
CompilerBasedVersion::CairoVersion(CairoVersion::Cairo0),
CompilerBasedVersion::OldCairo1,
CompilerBasedVersion::CairoVersion(CairoVersion::Cairo1),
CompilerBasedVersion::CairoVersion(CairoVersion::Native),
CompilerBasedVersion::CairoVersion(CairoVersion::Native)
)]
outer_version: CompilerBasedVersion,
#[values(
CompilerBasedVersion::CairoVersion(CairoVersion::Cairo0),
CompilerBasedVersion::OldCairo1,
CompilerBasedVersion::CairoVersion(CairoVersion::Cairo1),
CompilerBasedVersion::CairoVersion(CairoVersion::Native),
CompilerBasedVersion::CairoVersion(CairoVersion::Native)
)]
inner_version: CompilerBasedVersion,
) {
test_tracked_resources_fn(outer_version, inner_version);
}

/// Cairo0 / Old Cairo1 / Cairo1 calls to Cairo0 / Old Cairo1 / Cairo1.
#[cfg(not(feature = "cairo_native"))]
#[rstest]
fn test_tracked_resources(
#[values(
CompilerBasedVersion::CairoVersion(CairoVersion::Cairo0),
CompilerBasedVersion::OldCairo1,
CompilerBasedVersion::CairoVersion(CairoVersion::Cairo1)
)]
outer_version: CompilerBasedVersion,
#[values(
CompilerBasedVersion::CairoVersion(CairoVersion::Cairo0),
CompilerBasedVersion::OldCairo1,
CompilerBasedVersion::CairoVersion(CairoVersion::Cairo1)
)]
inner_version: CompilerBasedVersion,
) {
test_tracked_resources_fn(outer_version, inner_version);
}

fn test_tracked_resources_fn(
outer_version: CompilerBasedVersion,
inner_version: CompilerBasedVersion,
) {
let outer_contract = outer_version.get_test_contract();
Expand Down
29 changes: 0 additions & 29 deletions crates/blockifier/src/test_utils/cairo_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,35 +168,6 @@ pub fn starknet_compile(
sierra_output.stdout
}

pub fn sierra_compile(
path: String,
git_tag_override: Option<String>,
cargo_nightly_arg: Option<String>,
) -> Vec<u8> {
prepare_cairo1_compiler_deps(git_tag_override);

let cairo1_compiler_path = local_cairo1_compiler_repo_path();

// Command args common to both compilation phases.
let mut base_compile_args = vec![
"run".into(),
format!("--manifest-path={}/Cargo.toml", cairo1_compiler_path.to_string_lossy()),
"--bin".into(),
];
// Add additional cargo arg if provided. Should be first arg (base command is `cargo`).
if let Some(nightly_version) = cargo_nightly_arg {
base_compile_args.insert(0, format!("+nightly-{nightly_version}"));
}

// Cairo -> Sierra.
let mut starknet_compile_commmand = Command::new("cargo");
starknet_compile_commmand.args(base_compile_args.clone());
starknet_compile_commmand.args(["starknet-compile", "--", "--single-file", &path]);
let sierra_output = run_and_verify_output(&mut starknet_compile_commmand);

sierra_output.stdout
}

/// Verifies that the required dependencies are available before compiling; panics if unavailable.
fn verify_cairo0_compiler_deps() {
// Python compiler. Verify correct version.
Expand Down

0 comments on commit 2bac70d

Please sign in to comment.