From bd3fad882c3419ad51bc9466abd146f63a859776 Mon Sep 17 00:00:00 2001 From: Noa Oved <104720318+noaov1@users.noreply.github.com> Date: Sun, 3 Nov 2024 21:53:13 +0200 Subject: [PATCH] refactor(blockifier): use raw contract class in class info (#1730) --- Cargo.lock | 1 - .../blockifier/transaction_executor_test.rs | 2 +- .../src/concurrency/worker_logic_test.rs | 2 +- .../src/execution/contract_class.rs | 19 +++--- crates/blockifier/src/fee/receipt_test.rs | 2 +- crates/blockifier/src/test_utils/contracts.rs | 20 ++++-- .../transaction/account_transactions_test.rs | 7 +-- .../blockifier/src/transaction/test_utils.rs | 14 ++--- .../src/transaction/transactions.rs | 13 ++-- .../src/transaction/transactions_test.rs | 9 ++- crates/blockifier_reexecution/Cargo.toml | 1 - .../src/state_reader/compile.rs | 30 ++++----- .../src/state_reader/test_state_reader.rs | 8 ++- .../native_blockifier/src/py_transaction.rs | 14 ++--- crates/papyrus_execution/src/lib.rs | 62 +++++++++---------- crates/starknet_api/src/contract_class.rs | 2 +- .../src/deprecated_contract_class.rs | 6 ++ 17 files changed, 107 insertions(+), 105 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f30eea6912..2e1a7412ad 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1537,7 +1537,6 @@ dependencies = [ "blockifier", "cairo-lang-starknet-classes", "cairo-lang-utils", - "cairo-vm", "clap", "flate2", "indexmap 2.6.0", diff --git a/crates/blockifier/src/blockifier/transaction_executor_test.rs b/crates/blockifier/src/blockifier/transaction_executor_test.rs index 785d5c2164..4259c207d6 100644 --- a/crates/blockifier/src/blockifier/transaction_executor_test.rs +++ b/crates/blockifier/src/blockifier/transaction_executor_test.rs @@ -127,7 +127,7 @@ fn test_declare( version: tx_version, resource_bounds: l1_resource_bounds(0_u8.into(), DEFAULT_STRK_L1_GAS_PRICE.into()), }, - calculate_class_info_for_testing(declared_contract.get_runnable_class()), + calculate_class_info_for_testing(declared_contract.get_class()), ) .into(); tx_executor_test_body(state, block_context, tx, expected_bouncer_weights); diff --git a/crates/blockifier/src/concurrency/worker_logic_test.rs b/crates/blockifier/src/concurrency/worker_logic_test.rs index da2c4b5ebe..4b7ed1a6cf 100644 --- a/crates/blockifier/src/concurrency/worker_logic_test.rs +++ b/crates/blockifier/src/concurrency/worker_logic_test.rs @@ -552,7 +552,7 @@ fn test_deploy_before_declare( let account_address_1 = account_contract.get_instance_address(1); let test_contract = FeatureContract::TestContract(cairo_version); let test_class_hash = test_contract.get_class_hash(); - let test_class_info = calculate_class_info_for_testing(test_contract.get_runnable_class()); + let test_class_info = calculate_class_info_for_testing(test_contract.get_class()); let test_compiled_class_hash = test_contract.get_compiled_class_hash(); let declare_tx = declare_tx( declare_tx_args! { diff --git a/crates/blockifier/src/execution/contract_class.rs b/crates/blockifier/src/execution/contract_class.rs index 8edc409be9..e65cd08787 100644 --- a/crates/blockifier/src/execution/contract_class.rs +++ b/crates/blockifier/src/execution/contract_class.rs @@ -524,7 +524,7 @@ fn convert_entry_points_v1(external: &[CasmContractEntryPoint]) -> Vec for ClassInfo { abi_length, } = class_info; - Ok(Self { contract_class: contract_class.try_into()?, sierra_program_length, abi_length }) + Ok(Self { contract_class: contract_class.clone(), sierra_program_length, abi_length }) } } impl ClassInfo { pub fn bytecode_length(&self) -> usize { - self.contract_class.bytecode_length() + match &self.contract_class { + ContractClass::V0(contract_class) => contract_class.bytecode_length(), + ContractClass::V1(contract_class) => contract_class.bytecode.len(), + } } - pub fn contract_class(&self) -> RunnableContractClass { + pub fn contract_class(&self) -> ContractClass { self.contract_class.clone() } @@ -568,15 +571,13 @@ impl ClassInfo { } pub fn new( - contract_class: &RunnableContractClass, + contract_class: &ContractClass, sierra_program_length: usize, abi_length: usize, ) -> ContractClassResult { let (contract_class_version, condition) = match contract_class { - RunnableContractClass::V0(_) => (0, sierra_program_length == 0), - RunnableContractClass::V1(_) => (1, sierra_program_length > 0), - #[cfg(feature = "cairo_native")] - RunnableContractClass::V1Native(_) => (1, sierra_program_length > 0), + ContractClass::V0(_) => (0, sierra_program_length == 0), + ContractClass::V1(_) => (1, sierra_program_length > 0), }; if condition { diff --git a/crates/blockifier/src/fee/receipt_test.rs b/crates/blockifier/src/fee/receipt_test.rs index ebe9a979ee..671b8c3285 100644 --- a/crates/blockifier/src/fee/receipt_test.rs +++ b/crates/blockifier/src/fee/receipt_test.rs @@ -69,7 +69,7 @@ fn test_calculate_tx_gas_usage_basic<'a>( // Declare. for cairo_version in [CairoVersion::Cairo0, CairoVersion::Cairo1] { - let empty_contract = FeatureContract::Empty(cairo_version).get_runnable_class(); + let empty_contract = FeatureContract::Empty(cairo_version).get_class(); let class_info = calculate_class_info_for_testing(empty_contract); let declare_tx_starknet_resources = StarknetResources::new( 0, diff --git a/crates/blockifier/src/test_utils/contracts.rs b/crates/blockifier/src/test_utils/contracts.rs index 101a595481..62b400060d 100644 --- a/crates/blockifier/src/test_utils/contracts.rs +++ b/crates/blockifier/src/test_utils/contracts.rs @@ -1,4 +1,5 @@ -use starknet_api::contract_class::EntryPointType; +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, @@ -11,9 +12,10 @@ 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::{ContractClassV0, ContractClassV1, RunnableContractClass}; +use crate::execution::contract_class::RunnableContractClass; use crate::execution::entry_point::CallEntryPoint; use crate::test_utils::cairo_compile::{cairo0_compile, cairo1_compile}; +use crate::test_utils::struct_impls::LoadContractFromFile; use crate::test_utils::{get_raw_contract_class, CairoVersion}; // This file contains featured contracts, used for tests. Use the function 'test_state' in @@ -148,13 +150,21 @@ impl FeatureContract { contract_address!(self.get_integer_base() + instance_id_as_u32 + ADDRESS_BIT) } - pub fn get_runnable_class(&self) -> RunnableContractClass { + pub fn get_class(&self) -> ContractClass { match self.cairo_version() { - CairoVersion::Cairo0 => ContractClassV0::from_file(&self.get_compiled_path()).into(), - CairoVersion::Cairo1 => ContractClassV1::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())) + } } } + pub fn get_runnable_class(&self) -> RunnableContractClass { + self.get_class().try_into().unwrap() + } + // TODO(Arni, 1/1/2025): Remove this function, and use the get_class function instead. pub fn get_deprecated_contract_class(&self) -> DeprecatedContractClass { let mut raw_contract_class: serde_json::Value = diff --git a/crates/blockifier/src/transaction/account_transactions_test.rs b/crates/blockifier/src/transaction/account_transactions_test.rs index 259a8b5abc..3d39a48b54 100644 --- a/crates/blockifier/src/transaction/account_transactions_test.rs +++ b/crates/blockifier/src/transaction/account_transactions_test.rs @@ -47,7 +47,6 @@ use crate::abi::abi_utils::{ use crate::check_tx_execution_error_for_invalid_scenario; use crate::context::{BlockContext, TransactionContext}; use crate::execution::call_info::CallInfo; -use crate::execution::contract_class::{ContractClassV1, RunnableContractClass}; use crate::execution::entry_point::EntryPointExecutionContext; use crate::execution::syscalls::SyscallSelector; use crate::fee::fee_utils::{get_fee_by_gas_vector, get_sequencer_balance_keys}; @@ -441,7 +440,7 @@ fn test_max_fee_limit_validate( let grindy_validate_account = FeatureContract::AccountWithLongValidate(CairoVersion::Cairo1); let grindy_class_hash = grindy_validate_account.get_class_hash(); let block_info = &block_context.block_info; - let class_info = calculate_class_info_for_testing(grindy_validate_account.get_runnable_class()); + let class_info = calculate_class_info_for_testing(grindy_validate_account.get_class()); // Declare the grindy-validation account. let account_tx = declare_tx( @@ -748,7 +747,7 @@ fn test_fail_declare(block_context: BlockContext, max_fee: Fee) { let TestInitData { mut state, account_address, mut nonce_manager, .. } = create_test_init_data(chain_info, CairoVersion::Cairo0); let class_hash = class_hash!(0xdeadeadeaf72_u128); - let contract_class = RunnableContractClass::V1(ContractClassV1::empty_for_testing()); + let contract_class = FeatureContract::Empty(CairoVersion::Cairo1).get_class(); let next_nonce = nonce_manager.next(account_address); // Cannot fail executing a declare tx unless it's V2 or above, and already declared. @@ -758,7 +757,7 @@ fn test_fail_declare(block_context: BlockContext, max_fee: Fee) { sender_address: account_address, ..Default::default() }; - state.set_contract_class(class_hash, contract_class.clone()).unwrap(); + state.set_contract_class(class_hash, contract_class.clone().try_into().unwrap()).unwrap(); state.set_compiled_class_hash(class_hash, declare_tx.compiled_class_hash).unwrap(); let class_info = calculate_class_info_for_testing(contract_class); let declare_account_tx = AccountTransaction::Declare( diff --git a/crates/blockifier/src/transaction/test_utils.rs b/crates/blockifier/src/transaction/test_utils.rs index 01c7573cea..e8a8936760 100644 --- a/crates/blockifier/src/transaction/test_utils.rs +++ b/crates/blockifier/src/transaction/test_utils.rs @@ -1,5 +1,6 @@ use rstest::fixture; use starknet_api::block::GasPrice; +use starknet_api::contract_class::ContractClass; use starknet_api::core::{ClassHash, ContractAddress, Nonce}; use starknet_api::execution_resources::GasAmount; use starknet_api::test_utils::deploy_account::DeployAccountTxArgs; @@ -25,7 +26,7 @@ use strum::IntoEnumIterator; use crate::abi::abi_utils::get_fee_token_var_address; use crate::context::{BlockContext, ChainInfo}; -use crate::execution::contract_class::{ClassInfo, RunnableContractClass}; +use crate::execution::contract_class::ClassInfo; use crate::state::cached_state::CachedState; use crate::state::state_api::State; use crate::test_utils::contracts::FeatureContract; @@ -259,8 +260,7 @@ pub fn create_account_tx_for_validate_test( } }; let class_hash = declared_contract.get_class_hash(); - let class_info = - calculate_class_info_for_testing(declared_contract.get_runnable_class()); + let class_info = calculate_class_info_for_testing(declared_contract.get_class()); declare_tx( declare_tx_args! { max_fee, @@ -375,12 +375,10 @@ pub fn create_all_resource_bounds( }) } -pub fn calculate_class_info_for_testing(contract_class: RunnableContractClass) -> ClassInfo { +pub fn calculate_class_info_for_testing(contract_class: ContractClass) -> ClassInfo { let sierra_program_length = match contract_class { - RunnableContractClass::V0(_) => 0, - RunnableContractClass::V1(_) => 100, - #[cfg(feature = "cairo_native")] - RunnableContractClass::V1Native(_) => 100, + ContractClass::V0(_) => 0, + ContractClass::V1(_) => 100, }; ClassInfo::new(&contract_class, sierra_program_length, 100).unwrap() } diff --git a/crates/blockifier/src/transaction/transactions.rs b/crates/blockifier/src/transaction/transactions.rs index 3d0b494f5f..ad8b13df78 100644 --- a/crates/blockifier/src/transaction/transactions.rs +++ b/crates/blockifier/src/transaction/transactions.rs @@ -1,7 +1,7 @@ use std::sync::Arc; use cairo_vm::vm::runners::cairo_runner::ExecutionResources; -use starknet_api::contract_class::EntryPointType; +use starknet_api::contract_class::{ContractClass, EntryPointType}; use starknet_api::core::{ClassHash, CompiledClassHash, ContractAddress, Nonce}; use starknet_api::executable_transaction::L1HandlerTransaction; use starknet_api::transaction::{ @@ -19,7 +19,7 @@ use starknet_api::transaction::{ use crate::abi::abi_utils::selector_from_name; use crate::context::{BlockContext, TransactionContext}; use crate::execution::call_info::CallInfo; -use crate::execution::contract_class::{ClassInfo, RunnableContractClass}; +use crate::execution::contract_class::ClassInfo; use crate::execution::entry_point::{ CallEntryPoint, CallType, @@ -154,7 +154,8 @@ impl DeclareTransaction { ) -> TransactionExecutionResult { let declare_version = declare_tx.version(); // Verify contract class version. - if !is_cairo1(&class_info.contract_class()) { + // TODO(Noa): Avoid the unnecessary conversion. + if !is_cairo1(&class_info.contract_class().try_into()?) { if declare_version > TransactionVersion::ONE { Err(TransactionExecutionError::ContractClassVersionMismatch { declare_version, @@ -213,7 +214,7 @@ impl DeclareTransaction { self.tx_hash } - pub fn contract_class(&self) -> RunnableContractClass { + pub fn contract_class(&self) -> ContractClass { self.class_info.contract_class() } @@ -230,7 +231,7 @@ impl DeclareTransaction { match state.get_compiled_contract_class(class_hash) { Err(StateError::UndeclaredClassHash(_)) => { // Class is undeclared; declare it. - state.set_contract_class(class_hash, self.contract_class())?; + state.set_contract_class(class_hash, self.contract_class().try_into()?)?; if let Some(compiled_class_hash) = compiled_class_hash { state.set_compiled_class_hash(class_hash, compiled_class_hash)?; } @@ -264,7 +265,7 @@ impl Executable for DeclareTransaction { // We allow redeclaration of the class for backward compatibility. // In the past, we allowed redeclaration of Cairo 0 contracts since there was // no class commitment (so no need to check if the class is already declared). - state.set_contract_class(class_hash, self.contract_class())?; + state.set_contract_class(class_hash, self.contract_class().try_into()?)?; } } starknet_api::transaction::DeclareTransaction::V2(DeclareTransactionV2 { diff --git a/crates/blockifier/src/transaction/transactions_test.rs b/crates/blockifier/src/transaction/transactions_test.rs index 6de3e13f2a..ed9ac027de 100644 --- a/crates/blockifier/src/transaction/transactions_test.rs +++ b/crates/blockifier/src/transaction/transactions_test.rs @@ -977,8 +977,7 @@ fn test_max_fee_exceeds_balance( assert_resource_bounds_exceed_balance_failure(state, block_context, invalid_tx); // Declare. let contract_to_declare = FeatureContract::Empty(CairoVersion::Cairo1); - let class_info = - calculate_class_info_for_testing(contract_to_declare.get_runnable_class()); + let class_info = calculate_class_info_for_testing(contract_to_declare.get_class()); let invalid_tx = declare_tx( declare_tx_args! { class_hash: contract_to_declare.get_class_hash(), @@ -1476,7 +1475,7 @@ fn test_declare_tx( let state = &mut test_state(chain_info, BALANCE, &[(account, 1)]); let class_hash = empty_contract.get_class_hash(); let compiled_class_hash = empty_contract.get_compiled_class_hash(); - let class_info = calculate_class_info_for_testing(empty_contract.get_runnable_class()); + let class_info = calculate_class_info_for_testing(empty_contract.get_class()); let sender_address = account.get_instance_address(0); let mut nonce_manager = NonceManager::default(); let state_changes_for_fee = declare_expected_state_changes_count(tx_version); @@ -1605,7 +1604,7 @@ fn test_declare_tx( // Verify class declaration. let contract_class_from_state = state.get_compiled_contract_class(class_hash).unwrap(); - assert_eq!(contract_class_from_state, class_info.contract_class()); + assert_eq!(contract_class_from_state, class_info.contract_class().try_into().unwrap()); // Checks that redeclaring the same contract fails. let account_tx2 = declare_tx( @@ -1638,7 +1637,7 @@ fn test_declare_tx_v0(default_l1_resource_bounds: ValidResourceBounds) { let state = &mut test_state(chain_info, BALANCE, &[(account, 1)]); let class_hash = empty_contract.get_class_hash(); let compiled_class_hash = empty_contract.get_compiled_class_hash(); - let class_info = calculate_class_info_for_testing(empty_contract.get_runnable_class()); + let class_info = calculate_class_info_for_testing(empty_contract.get_class()); let sender_address = account.get_instance_address(0); let mut nonce_manager = NonceManager::default(); diff --git a/crates/blockifier_reexecution/Cargo.toml b/crates/blockifier_reexecution/Cargo.toml index de662f4994..528997ecd9 100644 --- a/crates/blockifier_reexecution/Cargo.toml +++ b/crates/blockifier_reexecution/Cargo.toml @@ -12,7 +12,6 @@ blockifier_regression_https_testing = [] blockifier.workspace = true cairo-lang-starknet-classes.workspace = true cairo-lang-utils.workspace = true -cairo-vm.workspace = true clap = { workspace = true, features = ["cargo", "derive"] } flate2.workspace = true indexmap = { workspace = true, features = ["serde"] } diff --git a/crates/blockifier_reexecution/src/state_reader/compile.rs b/crates/blockifier_reexecution/src/state_reader/compile.rs index 7422a6e31c..4088dcea60 100644 --- a/crates/blockifier_reexecution/src/state_reader/compile.rs +++ b/crates/blockifier_reexecution/src/state_reader/compile.rs @@ -4,22 +4,20 @@ use std::collections::HashMap; use std::io::{self, Read}; -use std::sync::Arc; -use blockifier::execution::contract_class::{ - ContractClassV0, - ContractClassV0Inner, - RunnableContractClass, -}; use blockifier::state::state_api::StateResult; use cairo_lang_starknet_classes::contract_class::ContractEntryPoints; use cairo_lang_utils::bigint::BigUintAsHex; -use cairo_vm::types::program::Program; use flate2::bufread; use serde::Deserialize; -use starknet_api::contract_class::EntryPointType; +use starknet_api::contract_class::{ContractClass, EntryPointType}; use starknet_api::core::EntryPointSelector; -use starknet_api::deprecated_contract_class::{EntryPointOffset, EntryPointV0}; +use starknet_api::deprecated_contract_class::{ + ContractClass as DeprecatedContractClass, + EntryPointOffset, + EntryPointV0, + Program, +}; use starknet_api::hash::StarkHash; use starknet_core::types::{ CompressedLegacyContractClass, @@ -75,9 +73,7 @@ pub fn decode_reader(bytes: Vec) -> io::Result { } /// Compile a FlattenedSierraClass to a ContractClass V1 (casm) using cairo_lang_starknet_classes. -pub fn sierra_to_contact_class_v1( - sierra: FlattenedSierraClass, -) -> StateResult { +pub fn sierra_to_contact_class_v1(sierra: FlattenedSierraClass) -> StateResult { let middle_sierra: MiddleSierraContractClass = { let v = serde_json::to_value(sierra).map_err(serde_err_to_state_err); serde_json::from_value(v?).map_err(serde_err_to_state_err)? @@ -96,17 +92,17 @@ pub fn sierra_to_contact_class_v1( false, usize::MAX, ) + // TODO(Aviv): Reconsider the unwrap. .unwrap(); - Ok(RunnableContractClass::V1(casm.try_into().unwrap())) + Ok(ContractClass::V1(casm)) } /// Compile a CompressedLegacyContractClass to a ContractClass V0 using cairo_lang_starknet_classes. pub fn legacy_to_contract_class_v0( legacy: CompressedLegacyContractClass, -) -> StateResult { +) -> StateResult { let as_str = decode_reader(legacy.program).unwrap(); - let program = Program::from_bytes(as_str.as_bytes(), None).unwrap(); + let program: Program = serde_json::from_str(&as_str).unwrap(); let entry_points_by_type = map_entry_points_by_type_legacy(legacy.entry_points_by_type); - let inner = Arc::new(ContractClassV0Inner { program, entry_points_by_type }); - Ok(RunnableContractClass::V0(ContractClassV0(inner))) + Ok((DeprecatedContractClass { program, entry_points_by_type, abi: None }).into()) } diff --git a/crates/blockifier_reexecution/src/state_reader/test_state_reader.rs b/crates/blockifier_reexecution/src/state_reader/test_state_reader.rs index 92d3cc8297..9d65e471a3 100644 --- a/crates/blockifier_reexecution/src/state_reader/test_state_reader.rs +++ b/crates/blockifier_reexecution/src/state_reader/test_state_reader.rs @@ -80,8 +80,12 @@ impl StateReader for TestStateReader { class_hash: ClassHash, ) -> StateResult { match self.get_contract_class(&class_hash)? { - StarknetContractClass::Sierra(sierra) => sierra_to_contact_class_v1(sierra), - StarknetContractClass::Legacy(legacy) => legacy_to_contract_class_v0(legacy), + StarknetContractClass::Sierra(sierra) => { + Ok(sierra_to_contact_class_v1(sierra).unwrap().try_into().unwrap()) + } + StarknetContractClass::Legacy(legacy) => { + Ok(legacy_to_contract_class_v0(legacy).unwrap().try_into().unwrap()) + } } } diff --git a/crates/native_blockifier/src/py_transaction.rs b/crates/native_blockifier/src/py_transaction.rs index 8e04dd5dbf..5ac4b56df8 100644 --- a/crates/native_blockifier/src/py_transaction.rs +++ b/crates/native_blockifier/src/py_transaction.rs @@ -1,17 +1,13 @@ use std::collections::BTreeMap; -use blockifier::execution::contract_class::{ - ClassInfo, - ContractClassV0, - ContractClassV1, - RunnableContractClass, -}; +use blockifier::execution::contract_class::ClassInfo; use blockifier::transaction::account_transaction::AccountTransaction; use blockifier::transaction::transaction_execution::Transaction; use blockifier::transaction::transaction_types::TransactionType; use pyo3::exceptions::PyValueError; use pyo3::prelude::*; use starknet_api::block::GasPrice; +use starknet_api::contract_class::ContractClass; use starknet_api::execution_resources::GasAmount; use starknet_api::transaction::{ DeprecatedResourceBoundsMapping, @@ -170,14 +166,14 @@ impl PyClassInfo { py_class_info: PyClassInfo, tx: &starknet_api::transaction::DeclareTransaction, ) -> NativeBlockifierResult { - let contract_class: RunnableContractClass = match tx { + let contract_class = match tx { starknet_api::transaction::DeclareTransaction::V0(_) | starknet_api::transaction::DeclareTransaction::V1(_) => { - ContractClassV0::try_from_json_string(&py_class_info.raw_contract_class)?.into() + ContractClass::V0(serde_json::from_str(&py_class_info.raw_contract_class)?) } starknet_api::transaction::DeclareTransaction::V2(_) | starknet_api::transaction::DeclareTransaction::V3(_) => { - ContractClassV1::try_from_json_string(&py_class_info.raw_contract_class)?.into() + ContractClass::V1(serde_json::from_str(&py_class_info.raw_contract_class)?) } }; let class_info = ClassInfo::new( diff --git a/crates/papyrus_execution/src/lib.rs b/crates/papyrus_execution/src/lib.rs index 228b698cf5..cf393a232e 100644 --- a/crates/papyrus_execution/src/lib.rs +++ b/crates/papyrus_execution/src/lib.rs @@ -27,7 +27,7 @@ use blockifier::blockifier::block::{pre_process_block, BlockInfo, GasPrices}; use blockifier::bouncer::BouncerConfig; use blockifier::context::{BlockContext, ChainInfo, FeeTokenAddresses, TransactionContext}; use blockifier::execution::call_info::CallExecution; -use blockifier::execution::contract_class::{ClassInfo, RunnableContractClass}; +use blockifier::execution::contract_class::ClassInfo; use blockifier::execution::entry_point::{ CallEntryPoint, CallType as BlockifierCallType, @@ -790,19 +790,16 @@ fn to_blockifier_tx( abi_length, only_query, ) => { - let class_v0 = RunnableContractClass::V0(deprecated_class.try_into().map_err( - |e: cairo_vm::types::errors::program_errors::ProgramError| { - ExecutionError::TransactionExecutionError { - transaction_index, - execution_error: e.to_string(), - } - }, - )?); - let class_info = ClassInfo::new(&class_v0, DEPRECATED_CONTRACT_SIERRA_SIZE, abi_length) - .map_err(|err| ExecutionError::BadDeclareTransaction { - tx: DeclareTransaction::V0(declare_tx.clone()), - err, - })?; + let class_info = ClassInfo::new( + &deprecated_class.into(), + DEPRECATED_CONTRACT_SIERRA_SIZE, + abi_length, + ) + .map_err(|err| ExecutionError::BadDeclareTransaction { + tx: DeclareTransaction::V0(declare_tx.clone()), + err, + })?; + BlockifierTransaction::from_api( Transaction::Declare(DeclareTransaction::V0(declare_tx)), tx_hash, @@ -819,14 +816,15 @@ fn to_blockifier_tx( abi_length, only_query, ) => { - let class_v0 = RunnableContractClass::V0( - deprecated_class.try_into().map_err(BlockifierError::new)?, - ); - let class_info = ClassInfo::new(&class_v0, DEPRECATED_CONTRACT_SIERRA_SIZE, abi_length) - .map_err(|err| ExecutionError::BadDeclareTransaction { - tx: DeclareTransaction::V1(declare_tx.clone()), - err, - })?; + let class_info = ClassInfo::new( + &deprecated_class.into(), + DEPRECATED_CONTRACT_SIERRA_SIZE, + abi_length, + ) + .map_err(|err| ExecutionError::BadDeclareTransaction { + tx: DeclareTransaction::V1(declare_tx.clone()), + err, + })?; BlockifierTransaction::from_api( Transaction::Declare(DeclareTransaction::V1(declare_tx)), tx_hash, @@ -844,15 +842,13 @@ fn to_blockifier_tx( abi_length, only_query, ) => { - let class_v1 = - RunnableContractClass::V1(compiled_class.try_into().map_err(BlockifierError::new)?); let class_info = - ClassInfo::new(&class_v1, sierra_program_length, abi_length).map_err(|err| { - ExecutionError::BadDeclareTransaction { + ClassInfo::new(&compiled_class.into(), sierra_program_length, abi_length).map_err( + |err| ExecutionError::BadDeclareTransaction { tx: DeclareTransaction::V2(declare_tx.clone()), err, - } - })?; + }, + )?; BlockifierTransaction::from_api( Transaction::Declare(DeclareTransaction::V2(declare_tx)), tx_hash, @@ -870,15 +866,13 @@ fn to_blockifier_tx( abi_length, only_query, ) => { - let class_v1 = - RunnableContractClass::V1(compiled_class.try_into().map_err(BlockifierError::new)?); let class_info = - ClassInfo::new(&class_v1, sierra_program_length, abi_length).map_err(|err| { - ExecutionError::BadDeclareTransaction { + ClassInfo::new(&compiled_class.into(), sierra_program_length, abi_length).map_err( + |err| ExecutionError::BadDeclareTransaction { tx: DeclareTransaction::V3(declare_tx.clone()), err, - } - })?; + }, + )?; BlockifierTransaction::from_api( Transaction::Declare(DeclareTransaction::V3(declare_tx)), tx_hash, diff --git a/crates/starknet_api/src/contract_class.rs b/crates/starknet_api/src/contract_class.rs index 6470347578..e633e3e4ae 100644 --- a/crates/starknet_api/src/contract_class.rs +++ b/crates/starknet_api/src/contract_class.rs @@ -22,7 +22,7 @@ pub enum EntryPointType { } /// Represents a raw Starknet contract class. -#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)] +#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize, derive_more::From)] pub enum ContractClass { V0(DeprecatedContractClass), V1(CasmContractClass), diff --git a/crates/starknet_api/src/deprecated_contract_class.rs b/crates/starknet_api/src/deprecated_contract_class.rs index 81b56ffaf2..5ea52037af 100644 --- a/crates/starknet_api/src/deprecated_contract_class.rs +++ b/crates/starknet_api/src/deprecated_contract_class.rs @@ -26,6 +26,12 @@ pub struct ContractClass { pub entry_points_by_type: HashMap>, } +impl ContractClass { + pub fn bytecode_length(&self) -> usize { + self.program.data.as_array().expect("The program data must be an array.").len() + } +} + /// A [ContractClass](`crate::deprecated_contract_class::ContractClass`) abi entry. // Using untagged so the serialization will be sorted by the keys (the default behavior of Serde for // untagged enums). We care about the order of the fields in the serialization because it affects