From d5cc8ec820b87ed6dcb90548e2eb364178d8b35d Mon Sep 17 00:00:00 2001 From: Rodrigo Date: Tue, 5 Nov 2024 09:33:52 +0000 Subject: [PATCH] fix: compilation issues caused by previous refactor --- crates/blockifier/Cargo.toml | 2 +- .../src/execution/contract_class.rs | 9 +++ .../src/execution/native/contract_class.rs | 32 ++++++++++ crates/blockifier/src/test_utils.rs | 8 ++- crates/blockifier/src/test_utils/contracts.rs | 19 +++--- .../blockifier/src/test_utils/struct_impls.rs | 58 +++++++++---------- .../blockifier/src/transaction/test_utils.rs | 2 + crates/starknet_api/Cargo.toml | 1 + crates/starknet_api/src/contract_class.rs | 8 +++ 9 files changed, 94 insertions(+), 45 deletions(-) diff --git a/crates/blockifier/Cargo.toml b/crates/blockifier/Cargo.toml index 69bfac8392..4ec066ba4a 100644 --- a/crates/blockifier/Cargo.toml +++ b/crates/blockifier/Cargo.toml @@ -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 = [] diff --git a/crates/blockifier/src/execution/contract_class.rs b/crates/blockifier/src/execution/contract_class.rs index 33ca5e2024..0bd6fd73f2 100644 --- a/crates/blockifier/src/execution/contract_class.rs +++ b/crates/blockifier/src/execution/contract_class.rs @@ -1,3 +1,4 @@ +use core::panic; use std::collections::{HashMap, HashSet}; use std::ops::{Deref, Index}; use std::sync::Arc; @@ -77,6 +78,10 @@ impl TryFrom 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(raw_contract_class) => { + Self::V1Native(raw_contract_class.try_into()?) + } }; Ok(contract_class) @@ -544,6 +549,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"), } } @@ -574,6 +581,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 { diff --git a/crates/blockifier/src/execution/native/contract_class.rs b/crates/blockifier/src/execution/native/contract_class.rs index 529070283c..6c0d37afa9 100644 --- a/crates/blockifier/src/execution/native/contract_class.rs +++ b/crates/blockifier/src/execution/native/contract_class.rs @@ -2,12 +2,14 @@ use std::ops::Deref; use std::sync::Arc; use cairo_lang_sierra::ids::FunctionId; +use cairo_lang_starknet_classes::casm_contract_class::CasmContractClass; use cairo_lang_starknet_classes::contract_class::{ ContractClass as SierraContractClass, ContractEntryPoint as SierraContractEntryPoint, }; #[allow(unused_imports)] use cairo_native::executor::AotNativeExecutor; +use cairo_vm::types::errors::program_errors::ProgramError; use starknet_api::core::EntryPointSelector; use crate::execution::contract_class::{ContractClassV1, EntryPointsByType, HasSelector}; @@ -53,6 +55,36 @@ impl NativeContractClassV1 { } } +impl TryFrom for NativeContractClassV1 { + type Error = ProgramError; + + fn try_from(sierra_contract_class: SierraContractClass) -> Result { + let sierra_program = sierra_contract_class + .extract_sierra_program() + .expect("Cannot extract sierra program from sierra contract class"); + + // Compile the sierra program to native code and loads it into the process' + // memory space. + let native_context = cairo_native::context::NativeContext::new(); + 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); + + // 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)?; + + Ok(Self::new(executor, sierra_contract_class, casm)) + } +} + #[derive(Debug)] pub struct NativeContractClassV1Inner { pub executor: AotNativeExecutor, diff --git a/crates/blockifier/src/test_utils.rs b/crates/blockifier/src/test_utils.rs index 6bb2155af9..195cc83900 100644 --- a/crates/blockifier/src/test_utils.rs +++ b/crates/blockifier/src/test_utils.rs @@ -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; @@ -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!( diff --git a/crates/blockifier/src/test_utils/contracts.rs b/crates/blockifier/src/test_utils/contracts.rs index bab349ea8e..ddcda82fd7 100644 --- a/crates/blockifier/src/test_utils/contracts.rs +++ b/crates/blockifier/src/test_utils/contracts.rs @@ -1,8 +1,11 @@ use cairo_lang_starknet_classes::casm_contract_class::CasmContractClass; +#[cfg(feature = "cairo_native")] +use cairo_lang_starknet_classes::contract_class::ContractClass as SierraContractClass; 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; @@ -12,12 +15,8 @@ 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; -#[cfg(feature = "cairo_native")] use crate::test_utils::cairo_compile::starknet_compile; use crate::test_utils::cairo_compile::{cairo0_compile, cairo1_compile}; use crate::test_utils::struct_impls::LoadContractFromFile; @@ -159,15 +158,15 @@ 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() + ContractClass::V1Native(SierraContractClass::from_file(&self.get_compiled_path())) } } } diff --git a/crates/blockifier/src/test_utils/struct_impls.rs b/crates/blockifier/src/test_utils/struct_impls.rs index 28a60b465e..438b573c31 100644 --- a/crates/blockifier/src/test_utils/struct_impls.rs +++ b/crates/blockifier/src/test_utils/struct_impls.rs @@ -2,7 +2,9 @@ use std::sync::Arc; use cairo_lang_starknet_classes::casm_contract_class::CasmContractClass; #[cfg(feature = "cairo_native")] -use cairo_native::executor::AotNativeExecutor; +use cairo_lang_starknet_classes::contract_class::ContractClass as SierraContractClass; +#[cfg(feature = "cairo_native")] +use cairo_vm::types::errors::program_errors::ProgramError; use cairo_vm::vm::runners::cairo_runner::ExecutionResources; use serde_json::Value; use starknet_api::block::{BlockNumber, BlockTimestamp, NonzeroGasPrice}; @@ -10,7 +12,12 @@ 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}; @@ -18,14 +25,20 @@ 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::{ @@ -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 { @@ -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 { @@ -239,32 +257,10 @@ 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> { - // 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 { - 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, - )) - } - - 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()?; - let executor = compile_and_load(&sierra_program)?; - - 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)?; + pub fn try_from_json_string(raw_contract_class: &str) -> Result { + let sierra_contract_class: SierraContractClass = serde_json::from_str(raw_contract_class)?; - Ok(Self::new(executor, sierra_contract_class, casm)) + NativeContractClassV1::try_from(sierra_contract_class) } pub fn from_file(contract_path: &str) -> Self { diff --git a/crates/blockifier/src/transaction/test_utils.rs b/crates/blockifier/src/transaction/test_utils.rs index a2934a2206..dd74ddc5fd 100644 --- a/crates/blockifier/src/transaction/test_utils.rs +++ b/crates/blockifier/src/transaction/test_utils.rs @@ -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() } diff --git a/crates/starknet_api/Cargo.toml b/crates/starknet_api/Cargo.toml index b0697d0f18..1ab8abcbec 100644 --- a/crates/starknet_api/Cargo.toml +++ b/crates/starknet_api/Cargo.toml @@ -7,6 +7,7 @@ license-file.workspace = true description = "Starknet Rust types related to computation and execution." [features] +cairo_native = [] testing = [] [dependencies] diff --git a/crates/starknet_api/src/contract_class.rs b/crates/starknet_api/src/contract_class.rs index e633e3e4ae..76fad1f8c3 100644 --- a/crates/starknet_api/src/contract_class.rs +++ b/crates/starknet_api/src/contract_class.rs @@ -1,4 +1,6 @@ use cairo_lang_starknet_classes::casm_contract_class::CasmContractClass; +#[cfg(feature = "cairo_native")] +use cairo_lang_starknet_classes::contract_class::ContractClass as SierraContractClass; use serde::{Deserialize, Serialize}; use crate::core::CompiledClassHash; @@ -26,6 +28,8 @@ pub enum EntryPointType { pub enum ContractClass { V0(DeprecatedContractClass), V1(CasmContractClass), + #[cfg(feature = "cairo_native")] + V1Native(SierraContractClass), } impl ContractClass { @@ -35,6 +39,10 @@ impl ContractClass { ContractClass::V1(casm_contract_class) => { CompiledClassHash(casm_contract_class.compiled_class_hash()) } + #[cfg(feature = "cairo_native")] + ContractClass::V1Native(_sierra_contract_class) => { + unimplemented!() + } } } }