Skip to content

Commit

Permalink
fix: compilation issues caused by previous refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigo-pino committed Nov 7, 2024
1 parent 020bde6 commit ce5b163
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 46 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/blockifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ description = "The transaction-executing component in the Starknet sequencer."
workspace = true

[features]
cairo_native = ["dep:cairo-native"]
cairo_native = ["dep:cairo-native", "starknet_api/cairo_native"]
jemalloc = ["dep:tikv-jemallocator"]
testing = ["rand", "rstest", "starknet_api/testing"]
transaction_serde = []
Expand Down
11 changes: 11 additions & 0 deletions crates/blockifier/src/execution/contract_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ impl TryFrom<ContractClass> for RunnableContractClass {
let contract_class: Self = match raw_contract_class {
ContractClass::V0(raw_contract_class) => Self::V0(raw_contract_class.try_into()?),
ContractClass::V1(raw_contract_class) => Self::V1(raw_contract_class.try_into()?),
#[cfg(feature = "cairo_native")]
ContractClass::V1Native => {
panic!(
"Native conversion from contract class to runnable contract class is not \
implemented yet"
)
}
};

Ok(contract_class)
Expand Down Expand Up @@ -544,6 +551,8 @@ impl ClassInfo {
match &self.contract_class {
ContractClass::V0(contract_class) => contract_class.bytecode_length(),
ContractClass::V1(contract_class) => contract_class.bytecode.len(),
#[cfg(feature = "cairo_native")]
ContractClass::V1Native => panic!("Native contract have no bytecode length info"),
}
}

Expand Down Expand Up @@ -574,6 +583,8 @@ impl ClassInfo {
let (contract_class_version, condition) = match contract_class {
ContractClass::V0(_) => (0, sierra_program_length == 0),
ContractClass::V1(_) => (1, sierra_program_length > 0),
#[cfg(feature = "cairo_native")]
ContractClass::V1Native => (1, sierra_program_length > 0),
};

if condition {
Expand Down
1 change: 0 additions & 1 deletion crates/blockifier/src/execution/native/contract_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use cairo_lang_starknet_classes::contract_class::{
ContractClass as SierraContractClass,
ContractEntryPoint as SierraContractEntryPoint,
};
#[allow(unused_imports)]
use cairo_native::executor::AotNativeExecutor;
use starknet_api::core::EntryPointSelector;

Expand Down
2 changes: 1 addition & 1 deletion crates/blockifier/src/execution/stack_trace_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ Error in contract (contract address: {test_contract_address_2_felt:#064x}, class
CairoVersion::Cairo0 => expected_trace_cairo0,
CairoVersion::Cairo1 => expected_trace_cairo1,
#[cfg(feature = "cairo_native")]
CairoVersion::Native => panic!("Cairo Native contracts are not supported"),
CairoVersion::Native => panic!("Cairo Native is not yet supported"),
};

assert_eq!(tx_execution_error.to_string(), expected_trace);
Expand Down
10 changes: 6 additions & 4 deletions crates/blockifier/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ use starknet_api::execution_resources::{GasAmount, GasVector};
use starknet_api::hash::StarkHash;
use starknet_api::state::StorageKey;
use starknet_api::transaction::{
Calldata, ContractAddressSalt, Fee, GasVectorComputationMode, TransactionVersion,
Calldata,
ContractAddressSalt,
Fee,
GasVectorComputationMode,
TransactionVersion,
};
use starknet_api::{contract_address, felt};
use starknet_types_core::felt::Felt;
Expand Down Expand Up @@ -90,7 +94,7 @@ impl CairoVersion {
Self::Cairo0 => Self::Cairo1,
Self::Cairo1 => Self::Cairo0,
#[cfg(feature = "cairo_native")]
Self::Native => todo!("who should be your other?"),
Self::Native => panic!("There is no other version for native"),
}
}
}
Expand Down Expand Up @@ -321,8 +325,6 @@ macro_rules! check_tx_execution_error_for_custom_hint {
#[macro_export]
macro_rules! check_tx_execution_error_for_invalid_scenario {
($cairo_version:expr, $error:expr, $validate_constructor:expr $(,)?) => {
use $crate::transaction::errors::TransactionExecutionError;

match $cairo_version {
CairoVersion::Cairo0 => {
$crate::check_tx_execution_error_inner!(
Expand Down
2 changes: 2 additions & 0 deletions crates/blockifier/src/test_utils/cairo_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ pub fn cairo1_compile(
casm_output.stdout
}

/// Compile Cairo1 Contract into their Sierra version using the compiler version set in the
/// Cargo.toml
pub fn starknet_compile(
path: String,
git_tag_override: Option<String>,
Expand Down
27 changes: 16 additions & 11 deletions crates/blockifier/src/test_utils/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use cairo_lang_starknet_classes::casm_contract_class::CasmContractClass;
use starknet_api::contract_class::{ContractClass, EntryPointType};
use starknet_api::core::{ClassHash, CompiledClassHash, ContractAddress, EntryPointSelector};
use starknet_api::deprecated_contract_class::{
ContractClass as DeprecatedContractClass, EntryPointOffset,
ContractClass as DeprecatedContractClass,
EntryPointOffset,
};
use starknet_api::{class_hash, contract_address, felt};
use starknet_types_core::felt::Felt;
Expand All @@ -12,8 +13,6 @@ use strum_macros::EnumIter;
use crate::abi::abi_utils::selector_from_name;
use crate::abi::constants::CONSTRUCTOR_ENTRY_POINT_NAME;
use crate::execution::contract_class::RunnableContractClass;
use crate::execution::contract_class::{ContractClass, ContractClassV0, ContractClassV1};
use crate::execution::contract_class::{ContractClass, ContractClassV0, ContractClassV1};
use crate::execution::entry_point::CallEntryPoint;
#[cfg(feature = "cairo_native")]
use crate::execution::native::contract_class::NativeContractClassV1;
Expand Down Expand Up @@ -159,20 +158,26 @@ impl FeatureContract {

pub fn get_class(&self) -> ContractClass {
match self.cairo_version() {
CairoVersion::Cairo0 => ContractClass::V0(
DeprecatedContractClass::from_file(&self.get_compiled_path()).into(),
),
CairoVersion::Cairo0 => {
ContractClass::V0(DeprecatedContractClass::from_file(&self.get_compiled_path()))
}
CairoVersion::Cairo1 => {
ContractClass::V1(CasmContractClass::from_file(&self.get_compiled_path()).into())
ContractClass::V1(CasmContractClass::from_file(&self.get_compiled_path()))
}
#[cfg(feature = "cairo_native")]
CairoVersion::Native => {
NativeContractClassV1::from_file(&self.get_compiled_path()).into()
}
CairoVersion::Native => ContractClass::V1Native,
}
}

pub fn get_runnable_class(&self) -> RunnableContractClass {
// todo(rodrigo): patch because the ContractClass -> RunnableContractClass for Native
// contracts is not fully implemented
#[cfg(feature = "cairo_native")]
if CairoVersion::Native == self.cairo_version() {
let native_contract_class = NativeContractClassV1::from_file(&self.get_compiled_path());
return RunnableContractClass::V1Native(native_contract_class);
}

self.get_class().try_into().unwrap()
}

Expand Down Expand Up @@ -258,7 +263,7 @@ impl FeatureContract {
CairoVersion::Cairo0 => ERC20_CAIRO0_CONTRACT_SOURCE_PATH,
CairoVersion::Cairo1 => ERC20_CAIRO1_CONTRACT_SOURCE_PATH,
#[cfg(feature = "cairo_native")]
CairoVersion::Native => todo!("ERC20 cannot be tested with Native"),
CairoVersion::Native => todo!("ERC20 contract is not supported by Native yet"),
}
.into()
} else {
Expand Down
77 changes: 49 additions & 28 deletions crates/blockifier/src/test_utils/struct_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use std::sync::Arc;

use cairo_lang_starknet_classes::casm_contract_class::CasmContractClass;
#[cfg(feature = "cairo_native")]
use cairo_lang_starknet_classes::contract_class::ContractClass as SierraContractClass;
#[cfg(feature = "cairo_native")]
use cairo_native::executor::AotNativeExecutor;
use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
use serde_json::Value;
Expand All @@ -10,22 +12,33 @@ use starknet_api::contract_address;
use starknet_api::core::{ChainId, ClassHash};
use starknet_api::deprecated_contract_class::ContractClass as DeprecatedContractClass;

use super::update_json_value;
use super::{
update_json_value,
TEST_ERC20_CONTRACT_ADDRESS,
TEST_ERC20_CONTRACT_ADDRESS2,
TEST_SEQUENCER_ADDRESS,
};
use crate::blockifier::block::{BlockInfo, GasPrices};
use crate::bouncer::{BouncerConfig, BouncerWeights, BuiltinCount};
use crate::context::{BlockContext, ChainInfo, FeeTokenAddresses, TransactionContext};
use crate::execution::call_info::{CallExecution, CallInfo, Retdata};
use crate::execution::common_hints::ExecutionMode;
use crate::execution::contract_class::{ContractClassV0, ContractClassV1};
use crate::execution::entry_point::{
CallEntryPoint, EntryPointExecutionContext, EntryPointExecutionResult,
CallEntryPoint,
EntryPointExecutionContext,
EntryPointExecutionResult,
};
#[cfg(feature = "cairo_native")]
use crate::execution::native::contract_class::NativeContractClassV1;
use crate::state::state_api::State;
use crate::test_utils::{
get_raw_contract_class, CURRENT_BLOCK_NUMBER, CURRENT_BLOCK_TIMESTAMP,
DEFAULT_ETH_L1_DATA_GAS_PRICE, DEFAULT_ETH_L1_GAS_PRICE, DEFAULT_STRK_L1_DATA_GAS_PRICE,
get_raw_contract_class,
CURRENT_BLOCK_NUMBER,
CURRENT_BLOCK_TIMESTAMP,
DEFAULT_ETH_L1_DATA_GAS_PRICE,
DEFAULT_ETH_L1_GAS_PRICE,
DEFAULT_STRK_L1_DATA_GAS_PRICE,
DEFAULT_STRK_L1_GAS_PRICE,
};
use crate::transaction::objects::{
Expand All @@ -34,7 +47,10 @@ use crate::transaction::objects::{
TransactionInfo,
};
use crate::versioned_constants::{
GasCosts, OsConstants, VersionedConstants, VERSIONED_CONSTANTS_LATEST_JSON,
GasCosts,
OsConstants,
VersionedConstants,
VERSIONED_CONSTANTS_LATEST_JSON,
};

impl CallEntryPoint {
Expand Down Expand Up @@ -214,6 +230,8 @@ pub trait LoadContractFromFile: serde::de::DeserializeOwned {

impl LoadContractFromFile for CasmContractClass {}
impl LoadContractFromFile for DeprecatedContractClass {}
#[cfg(feature = "cairo_native")]
impl LoadContractFromFile for SierraContractClass {}

impl ContractClassV0 {
pub fn from_file(contract_path: &str) -> Self {
Expand All @@ -239,36 +257,39 @@ impl BouncerWeights {
impl NativeContractClassV1 {
/// Convenience function to construct a NativeContractClassV1 from a raw contract class.
/// If control over the compilation is desired use [Self::new] instead.
pub fn try_from_json_string(
raw_contract_class: &str,
) -> Result<Self, Box<dyn std::error::Error>> {
// Compile the Sierra Program to native code and loads it into the process'
// memory space.
fn compile_and_load(
sierra_program: &cairo_lang_sierra::program::Program,
) -> Result<AotNativeExecutor, cairo_native::error::Error> {
let native_context = cairo_native::context::NativeContext::new();
let native_program = native_context.compile(sierra_program, false)?;
Ok(AotNativeExecutor::from_native_module(
native_program,
cairo_native::OptLevel::Default,
))
}
pub fn try_from_json_string(raw_sierra_contract_class: &str) -> Self {
let sierra_contract_class: SierraContractClass =
serde_json::from_str(raw_sierra_contract_class).unwrap();

let sierra_contract_class: cairo_lang_starknet_classes::contract_class::ContractClass =
serde_json::from_str(raw_contract_class)?;
let sierra_program = sierra_contract_class
.extract_sierra_program()
.expect("Cannot extract sierra program from sierra contract class");

let sierra_program = sierra_contract_class.extract_sierra_program()?;
let executor = compile_and_load(&sierra_program)?;
// Create a Cairo Native MLIR context. This structure handles MLIR initialization and
// compiles Sierra programs into an MLIR module
let native_context = cairo_native::context::NativeContext::new();
// Compile the sierra program into a MLIR module
let native_program = native_context
.compile(&sierra_program, false)
.expect("Cannot compile sierra program into native program");
let executor =
AotNativeExecutor::from_native_module(native_program, cairo_native::OptLevel::Default);

let casm_contract_class = cairo_lang_starknet_classes::casm_contract_class::CasmContractClass::from_contract_class(sierra_contract_class.clone(), false, usize::MAX)?;
let casm = ContractClassV1::try_from(casm_contract_class)?;
// Compile the sierra contract class into casm
let casm_contract_class = CasmContractClass::from_contract_class(
sierra_contract_class.clone(),
false,
usize::MAX,
)
.expect("Cannot compile sierra contract class into casm contract class");
let casm = ContractClassV1::try_from(casm_contract_class)
.expect("Cannot get ContractClassV1 from CasmContractClass");

Ok(Self::new(executor, sierra_contract_class, casm))
NativeContractClassV1::new(executor, sierra_contract_class, casm)
}

pub fn from_file(contract_path: &str) -> Self {
let raw_contract_class = get_raw_contract_class(contract_path);
Self::try_from_json_string(&raw_contract_class).unwrap()
Self::try_from_json_string(&raw_contract_class)
}
}
2 changes: 2 additions & 0 deletions crates/blockifier/src/transaction/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,8 @@ pub fn calculate_class_info_for_testing(contract_class: ContractClass) -> ClassI
let sierra_program_length = match contract_class {
ContractClass::V0(_) => 0,
ContractClass::V1(_) => 100,
#[cfg(feature = "cairo_native")]
ContractClass::V1Native => 100,
};
ClassInfo::new(&contract_class, sierra_program_length, 100).unwrap()
}
Expand Down
2 changes: 2 additions & 0 deletions crates/starknet_api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ license-file.workspace = true
description = "Starknet Rust types related to computation and execution."

[features]
cairo_native = ["dep:cairo-native"]
testing = []

[dependencies]
bitvec.workspace = true
cairo-lang-starknet-classes.workspace = true
cairo-lang-runner.workspace = true
cairo-native = { workspace = true, optional = true }
derive_more.workspace = true
hex.workspace = true
indexmap = { workspace = true, features = ["serde"] }
Expand Down
6 changes: 6 additions & 0 deletions crates/starknet_api/src/contract_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ pub enum EntryPointType {
pub enum ContractClass {
V0(DeprecatedContractClass),
V1(CasmContractClass),
#[cfg(feature = "cairo_native")]
V1Native,
}

impl ContractClass {
Expand All @@ -35,6 +37,10 @@ impl ContractClass {
ContractClass::V1(casm_contract_class) => {
CompiledClassHash(casm_contract_class.compiled_class_hash())
}
#[cfg(feature = "cairo_native")]
ContractClass::V1Native => {
panic!("Native contract compiled class hash is not yet implmented")
}
}
}
}
Expand Down

0 comments on commit ce5b163

Please sign in to comment.