Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(blockifier): cairo version enum has only two variants #2352

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ use crate::test_utils::syscall::build_recurse_calldata;
use crate::test_utils::{
create_calldata,
trivial_external_entry_point_new,
CairoVersion,
CompilerBasedVersion,
BALANCE,
};
Expand Down Expand Up @@ -122,17 +121,17 @@ fn test_call_contract(outer_contract: FeatureContract, inner_contract: FeatureCo
#[rstest]
fn test_tracked_resources(
#[values(
CompilerBasedVersion::CairoVersion(CairoVersion::Cairo0),
CompilerBasedVersion::CairoVersion(RunnableContractVersion::Cairo0),
CompilerBasedVersion::OldCairo1,
CompilerBasedVersion::CairoVersion(CairoVersion::Cairo1),
CompilerBasedVersion::CairoVersion(CairoVersion::Native)
CompilerBasedVersion::CairoVersion(RunnableContractVersion::Cairo1Casm),
CompilerBasedVersion::CairoVersion(RunnableContractVersion::Cairo1Native)
)]
outer_version: CompilerBasedVersion,
#[values(
CompilerBasedVersion::CairoVersion(CairoVersion::Cairo0),
CompilerBasedVersion::CairoVersion(RunnableContractVersion::Cairo0),
CompilerBasedVersion::OldCairo1,
CompilerBasedVersion::CairoVersion(CairoVersion::Cairo1),
CompilerBasedVersion::CairoVersion(CairoVersion::Native)
CompilerBasedVersion::CairoVersion(RunnableContractVersion::Cairo1Casm),
CompilerBasedVersion::CairoVersion(RunnableContractVersion::Cairo1Native)
)]
inner_version: CompilerBasedVersion,
) {
Expand All @@ -144,15 +143,15 @@ fn test_tracked_resources(
#[rstest]
fn test_tracked_resources(
#[values(
CompilerBasedVersion::CairoVersion(CairoVersion::Cairo0),
CompilerBasedVersion::CairoVersion(RunnableContractVersion::Cairo0),
CompilerBasedVersion::OldCairo1,
CompilerBasedVersion::CairoVersion(CairoVersion::Cairo1)
CompilerBasedVersion::CairoVersion(RunnableContractVersion::Cairo1Casm)
)]
outer_version: CompilerBasedVersion,
#[values(
CompilerBasedVersion::CairoVersion(CairoVersion::Cairo0),
CompilerBasedVersion::CairoVersion(RunnableContractVersion::Cairo0),
CompilerBasedVersion::OldCairo1,
CompilerBasedVersion::CairoVersion(CairoVersion::Cairo1)
CompilerBasedVersion::CairoVersion(RunnableContractVersion::Cairo1Casm)
)]
inner_version: CompilerBasedVersion,
) {
Expand Down Expand Up @@ -192,15 +191,31 @@ fn test_tracked_resources_fn(
assert_eq!(execution.inner_calls.first().unwrap().tracked_resource, expected_inner_resource);
}

#[test_case(CompilerBasedVersion::CairoVersion(CairoVersion::Cairo0), CompilerBasedVersion::CairoVersion(CairoVersion::Cairo1); "Cairo0_and_Cairo1")]
#[test_case(CompilerBasedVersion::OldCairo1, CompilerBasedVersion::CairoVersion(CairoVersion::Cairo1); "OldCairo1_and_Cairo1")]
#[test_case(
CompilerBasedVersion::CairoVersion(RunnableContractVersion::Cairo0),
CompilerBasedVersion::CairoVersion(RunnableContractVersion::Cairo1Casm);
"Cairo0_and_Cairo1"
)]
#[test_case(
CompilerBasedVersion::OldCairo1,
CompilerBasedVersion::CairoVersion(RunnableContractVersion::Cairo1Casm);
"OldCairo1_and_Cairo1"
)]
#[cfg_attr(
feature = "cairo_native",
test_case(CompilerBasedVersion::CairoVersion(CairoVersion::Cairo0), CompilerBasedVersion::CairoVersion(CairoVersion::Native); "Cairo0_and_Native")
feature = "cairo_native",
test_case(
CompilerBasedVersion::CairoVersion(RunnableContractVersion::Cairo0),
CompilerBasedVersion::CairoVersion(RunnableContractVersion::Cairo1Native);
"Cairo0_and_Native"
)
)]
#[cfg_attr(
feature = "cairo_native",
test_case(CompilerBasedVersion::OldCairo1, CompilerBasedVersion::CairoVersion(CairoVersion::Native); "OldCairo1_and_Native")
feature = "cairo_native",
test_case(
CompilerBasedVersion::OldCairo1,
CompilerBasedVersion::CairoVersion(RunnableContractVersion::Cairo1Native);
"OldCairo1_and_Native"
)
)]
fn test_tracked_resources_nested(
cairo_steps_contract_version: CompilerBasedVersion,
Expand Down
25 changes: 6 additions & 19 deletions crates/blockifier/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,6 @@ pub const ERC20_CONTRACT_PATH: &str = "./ERC20/ERC20_Cairo0/ERC20_without_some_s
pub enum CairoVersion {
Cairo0,
Cairo1,
// TODO: Delete the native variant; use RunnableContractVersion when distinction between
// Cairo1 compilation methods is required.
#[cfg(feature = "cairo_native")]
Native,
}

impl Default for CairoVersion {
Expand All @@ -82,30 +78,21 @@ impl CairoVersion {
match self {
Self::Cairo0 => Self::Cairo1,
Self::Cairo1 => Self::Cairo0,
#[cfg(feature = "cairo_native")]
Self::Native => panic!("There is no other version for native"),
}
}
}

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum CompilerBasedVersion {
CairoVersion(CairoVersion),
CairoVersion(RunnableContractVersion),
OldCairo1,
}

impl CompilerBasedVersion {
pub fn get_test_contract(&self) -> FeatureContract {
match self {
Self::CairoVersion(CairoVersion::Cairo0) => {
FeatureContract::TestContract(RunnableContractVersion::Cairo0)
}
Self::CairoVersion(CairoVersion::Cairo1) => {
FeatureContract::TestContract(RunnableContractVersion::Cairo1Casm)
}
#[cfg(feature = "cairo_native")]
Self::CairoVersion(CairoVersion::Native) => {
FeatureContract::TestContract(RunnableContractVersion::Cairo1Native)
Self::CairoVersion(runnable_version) => {
FeatureContract::TestContract(*runnable_version)
}
Self::OldCairo1 => FeatureContract::CairoStepsTestContract,
}
Expand All @@ -115,12 +102,12 @@ impl CompilerBasedVersion {
/// and the transaction info into account).
pub fn own_tracked_resource(&self) -> TrackedResource {
match self {
Self::CairoVersion(CairoVersion::Cairo0) | Self::OldCairo1 => {
Self::CairoVersion(RunnableContractVersion::Cairo0) | Self::OldCairo1 => {
TrackedResource::CairoSteps
}
Self::CairoVersion(CairoVersion::Cairo1) => TrackedResource::SierraGas,
Self::CairoVersion(RunnableContractVersion::Cairo1Casm) => TrackedResource::SierraGas,
#[cfg(feature = "cairo_native")]
Self::CairoVersion(CairoVersion::Native) => TrackedResource::SierraGas,
Self::CairoVersion(RunnableContractVersion::Cairo1Native) => TrackedResource::SierraGas,
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/blockifier/src/test_utils/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ impl From<RunnableContractVersion> for CairoVersion {
RunnableContractVersion::Cairo0 => Self::Cairo0,
RunnableContractVersion::Cairo1Casm => Self::Cairo1,
#[cfg(feature = "cairo_native")]
RunnableContractVersion::Cairo1Native => Self::Native,
RunnableContractVersion::Cairo1Native => Self::Cairo1,
}
}
}
Expand Down
22 changes: 13 additions & 9 deletions crates/blockifier/src/transaction/account_transactions_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ use crate::test_utils::{
create_trivial_calldata,
get_syscall_resources,
get_tx_resources,
CairoVersion,
CompilerBasedVersion,
BALANCE,
DEFAULT_L1_DATA_GAS_MAX_AMOUNT,
Expand Down Expand Up @@ -1802,10 +1801,10 @@ fn test_concurrent_fee_transfer_when_sender_is_sequencer(
/// history.
#[rstest]
#[case(&[
CompilerBasedVersion::CairoVersion(CairoVersion::Cairo1),
CompilerBasedVersion::CairoVersion(CairoVersion::Cairo1),
CompilerBasedVersion::CairoVersion(CairoVersion::Cairo0),
CompilerBasedVersion::CairoVersion(CairoVersion::Cairo1)
CompilerBasedVersion::CairoVersion(RunnableContractVersion::Cairo1Casm),
CompilerBasedVersion::CairoVersion(RunnableContractVersion::Cairo1Casm),
CompilerBasedVersion::CairoVersion(RunnableContractVersion::Cairo0),
CompilerBasedVersion::CairoVersion(RunnableContractVersion::Cairo1Casm)
])]
// TODO(Tzahi, 1/12/2024): Add a case with OldCairo1 instead of Cairo0.
fn test_initial_gas(
Expand Down Expand Up @@ -1854,18 +1853,21 @@ fn test_initial_gas(
curr_initial_gas = execute_call_info.call.initial_gas;

match (prev_version, version, started_vm_mode) {
(CompilerBasedVersion::CairoVersion(CairoVersion::Cairo0), _, _) => {
(CompilerBasedVersion::CairoVersion(RunnableContractVersion::Cairo0), _, _) => {
assert_eq!(started_vm_mode, true);
assert_eq!(curr_initial_gas, prev_initial_gas);
}
(
_,
CompilerBasedVersion::CairoVersion(CairoVersion::Cairo0)
CompilerBasedVersion::CairoVersion(RunnableContractVersion::Cairo0)
| CompilerBasedVersion::OldCairo1,
false,
) => {
// First time we are in VM mode.
assert_eq!(prev_version, &CompilerBasedVersion::CairoVersion(CairoVersion::Cairo1));
assert_eq!(
prev_version,
&CompilerBasedVersion::CairoVersion(RunnableContractVersion::Cairo1Casm)
);
assert_eq!(
curr_initial_gas,
block_context.versioned_constants.default_initial_gas_cost()
Expand All @@ -1876,7 +1878,9 @@ fn test_initial_gas(
// prev_version is a non Cairo0 contract, thus it consumes gas from the initial
// gas.
assert!(curr_initial_gas < prev_initial_gas);
if version == &CompilerBasedVersion::CairoVersion(CairoVersion::Cairo1) {
if version
== &CompilerBasedVersion::CairoVersion(RunnableContractVersion::Cairo1Casm)
{
assert!(execute_call_info.execution.gas_consumed > 0);
} else {
assert!(execute_call_info.execution.gas_consumed == 0);
Expand Down
Loading