From 7a0a1f94026cda9ca23b990d78114fb2425d18d9 Mon Sep 17 00:00:00 2001 From: avivg-starkware Date: Tue, 12 Nov 2024 10:43:44 +0200 Subject: [PATCH] refactor(blockifier): switch use of blockifier classinfo by starknet_api classinfo (#1858) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit …api classinfo --- .../src/execution/contract_class.rs | 75 +------------------ crates/blockifier/src/execution/errors.rs | 12 --- crates/blockifier/src/test_utils/declare.rs | 2 +- .../blockifier/src/transaction/test_utils.rs | 3 +- .../src/transaction/transaction_execution.rs | 2 +- .../src/transaction/transactions.rs | 4 +- .../src/state_reader/errors.rs | 3 - .../state_reader/reexecution_state_reader.rs | 2 +- crates/native_blockifier/src/errors.rs | 2 - .../native_blockifier/src/py_transaction.rs | 3 +- crates/papyrus_execution/src/lib.rs | 5 +- 11 files changed, 9 insertions(+), 104 deletions(-) diff --git a/crates/blockifier/src/execution/contract_class.rs b/crates/blockifier/src/execution/contract_class.rs index df332829f2..6641c81fce 100644 --- a/crates/blockifier/src/execution/contract_class.rs +++ b/crates/blockifier/src/execution/contract_class.rs @@ -34,11 +34,10 @@ use starknet_types_core::felt::Felt; use crate::abi::constants::{self}; use crate::execution::entry_point::CallEntryPoint; -use crate::execution::errors::{ContractClassError, PreExecutionError}; +use crate::execution::errors::PreExecutionError; use crate::execution::execution_utils::{poseidon_hash_many_cost, sn_api_to_cairo_vm_program}; #[cfg(feature = "cairo_native")] use crate::execution::native::contract_class::NativeContractClassV1; -use crate::fee::eth_gas_constants; use crate::transaction::errors::TransactionExecutionError; use crate::versioned_constants::CompilerVersion; @@ -46,8 +45,6 @@ use crate::versioned_constants::CompilerVersion; #[path = "contract_class_test.rs"] pub mod test; -pub type ContractClassResult = Result; - pub trait HasSelector { fn selector(&self) -> &EntryPointSelector; } @@ -517,76 +514,6 @@ fn convert_entry_points_v1(external: &[CasmContractEntryPoint]) -> Vec for ClassInfo { - type Error = ProgramError; - - fn try_from(class_info: starknet_api::contract_class::ClassInfo) -> Result { - let starknet_api::contract_class::ClassInfo { - contract_class, - sierra_program_length, - abi_length, - } = class_info; - - Ok(Self { contract_class: contract_class.clone(), sierra_program_length, abi_length }) - } -} - -impl ClassInfo { - pub fn bytecode_length(&self) -> usize { - 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) -> ContractClass { - self.contract_class.clone() - } - - pub fn sierra_program_length(&self) -> usize { - self.sierra_program_length - } - - pub fn abi_length(&self) -> usize { - self.abi_length - } - - pub fn code_size(&self) -> usize { - (self.bytecode_length() + self.sierra_program_length()) - // We assume each felt is a word. - * eth_gas_constants::WORD_WIDTH - + self.abi_length() - } - - pub fn new( - contract_class: &ContractClass, - sierra_program_length: usize, - abi_length: usize, - ) -> ContractClassResult { - let (contract_class_version, condition) = match contract_class { - ContractClass::V0(_) => (0, sierra_program_length == 0), - ContractClass::V1(_) => (1, sierra_program_length > 0), - }; - - if condition { - Ok(Self { contract_class: contract_class.clone(), sierra_program_length, abi_length }) - } else { - Err(ContractClassError::ContractClassVersionSierraProgramLengthMismatch { - contract_class_version, - sierra_program_length, - }) - } - } -} - // TODO(Yoni): organize this file. #[derive(Clone, Debug, Default, Eq, PartialEq)] /// Modelled after [cairo_lang_starknet_classes::contract_class::ContractEntryPoints]. diff --git a/crates/blockifier/src/execution/errors.rs b/crates/blockifier/src/execution/errors.rs index 208daefccc..3c7d79d4de 100644 --- a/crates/blockifier/src/execution/errors.rs +++ b/crates/blockifier/src/execution/errors.rs @@ -132,15 +132,3 @@ impl ConstructorEntryPointExecutionError { } } } - -#[derive(Debug, Error)] -pub enum ContractClassError { - #[error( - "Sierra program length must be > 0 for Cairo1, and == 0 for Cairo0. Got: \ - {sierra_program_length:?} for contract class version {contract_class_version:?}" - )] - ContractClassVersionSierraProgramLengthMismatch { - contract_class_version: u8, - sierra_program_length: usize, - }, -} diff --git a/crates/blockifier/src/test_utils/declare.rs b/crates/blockifier/src/test_utils/declare.rs index ecd48cf6be..c4b37bb842 100644 --- a/crates/blockifier/src/test_utils/declare.rs +++ b/crates/blockifier/src/test_utils/declare.rs @@ -1,6 +1,6 @@ +use starknet_api::contract_class::ClassInfo; use starknet_api::test_utils::declare::DeclareTxArgs; -use crate::execution::contract_class::ClassInfo; use crate::transaction::account_transaction::AccountTransaction; use crate::transaction::transactions::DeclareTransaction; diff --git a/crates/blockifier/src/transaction/test_utils.rs b/crates/blockifier/src/transaction/test_utils.rs index 535cd5a14b..2203da5221 100644 --- a/crates/blockifier/src/transaction/test_utils.rs +++ b/crates/blockifier/src/transaction/test_utils.rs @@ -1,6 +1,6 @@ use rstest::fixture; use starknet_api::block::GasPrice; -use starknet_api::contract_class::ContractClass; +use starknet_api::contract_class::{ClassInfo, ContractClass}; use starknet_api::core::{ClassHash, ContractAddress, Nonce}; use starknet_api::execution_resources::GasAmount; use starknet_api::test_utils::deploy_account::DeployAccountTxArgs; @@ -28,7 +28,6 @@ use strum::IntoEnumIterator; use crate::abi::abi_utils::get_fee_token_var_address; use crate::context::{BlockContext, ChainInfo}; -use crate::execution::contract_class::ClassInfo; use crate::state::cached_state::CachedState; use crate::state::state_api::State; use crate::test_utils::contracts::FeatureContract; diff --git a/crates/blockifier/src/transaction/transaction_execution.rs b/crates/blockifier/src/transaction/transaction_execution.rs index 0bbbf36f35..381d926b55 100644 --- a/crates/blockifier/src/transaction/transaction_execution.rs +++ b/crates/blockifier/src/transaction/transaction_execution.rs @@ -1,6 +1,7 @@ use std::sync::Arc; use cairo_vm::vm::runners::cairo_runner::ExecutionResources; +use starknet_api::contract_class::ClassInfo; use starknet_api::core::{calculate_contract_address, ContractAddress, Nonce}; use starknet_api::executable_transaction::L1HandlerTransaction; use starknet_api::transaction::fields::Fee; @@ -9,7 +10,6 @@ use starknet_api::transaction::{Transaction as StarknetApiTransaction, Transacti use crate::bouncer::verify_tx_weights_within_max_capacity; use crate::context::BlockContext; use crate::execution::call_info::CallInfo; -use crate::execution::contract_class::ClassInfo; use crate::execution::entry_point::EntryPointExecutionContext; use crate::fee::receipt::TransactionReceipt; use crate::state::cached_state::TransactionalState; diff --git a/crates/blockifier/src/transaction/transactions.rs b/crates/blockifier/src/transaction/transactions.rs index 63444009d4..ba6ca80adf 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::{ContractClass, EntryPointType}; +use starknet_api::contract_class::{ClassInfo, ContractClass, EntryPointType}; use starknet_api::core::{ClassHash, CompiledClassHash, ContractAddress, Nonce}; use starknet_api::executable_transaction::{ DeclareTransaction as ExecutableDeclareTx, @@ -26,7 +26,6 @@ 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; use crate::execution::entry_point::{ CallEntryPoint, CallType, @@ -201,7 +200,6 @@ impl DeclareTransaction { ) -> Result { let starknet_api::executable_transaction::DeclareTransaction { tx, tx_hash, class_info } = declare_tx; - let class_info = class_info.try_into()?; Self::create(tx, tx_hash, class_info, only_query) } diff --git a/crates/blockifier_reexecution/src/state_reader/errors.rs b/crates/blockifier_reexecution/src/state_reader/errors.rs index 8d15f1de1c..e2d5a0c1dc 100644 --- a/crates/blockifier_reexecution/src/state_reader/errors.rs +++ b/crates/blockifier_reexecution/src/state_reader/errors.rs @@ -1,4 +1,3 @@ -use blockifier::execution::errors::ContractClassError; use blockifier::state::errors::StateError; use blockifier::transaction::errors::TransactionExecutionError; use blockifier::versioned_constants::VersionedConstantsError; @@ -22,6 +21,4 @@ pub enum ReexecutionError { TransactionExecutionError(#[from] TransactionExecutionError), #[error(transparent)] VersionedConstants(#[from] VersionedConstantsError), - #[error(transparent)] - ContractClassError(#[from] ContractClassError), } diff --git a/crates/blockifier_reexecution/src/state_reader/reexecution_state_reader.rs b/crates/blockifier_reexecution/src/state_reader/reexecution_state_reader.rs index 9ef01c8710..c500344c84 100644 --- a/crates/blockifier_reexecution/src/state_reader/reexecution_state_reader.rs +++ b/crates/blockifier_reexecution/src/state_reader/reexecution_state_reader.rs @@ -1,9 +1,9 @@ -use blockifier::execution::contract_class::ClassInfo; use blockifier::state::state_api::StateResult; use blockifier::test_utils::MAX_FEE; use blockifier::transaction::transaction_execution::Transaction as BlockifierTransaction; use papyrus_execution::DEPRECATED_CONTRACT_SIERRA_SIZE; use starknet_api::block::{BlockHash, BlockNumber}; +use starknet_api::contract_class::ClassInfo; use starknet_api::core::ClassHash; use starknet_api::transaction::{Transaction, TransactionHash}; use starknet_core::types::ContractClass as StarknetContractClass; diff --git a/crates/native_blockifier/src/errors.rs b/crates/native_blockifier/src/errors.rs index bdf6c79443..2128f41748 100644 --- a/crates/native_blockifier/src/errors.rs +++ b/crates/native_blockifier/src/errors.rs @@ -1,7 +1,6 @@ use blockifier::blockifier::stateful_validator::StatefulValidatorError; use blockifier::blockifier::transaction_executor::TransactionExecutorError; use blockifier::bouncer::BuiltinCount; -use blockifier::execution::errors::ContractClassError; use blockifier::state::errors::StateError; use blockifier::transaction::errors::{ ParseError, @@ -67,7 +66,6 @@ macro_rules! native_blockifier_errors { } native_blockifier_errors!( - (ContractClassError, ContractClassError, PyContractClassError), (NativeBlockifierInputError, NativeBlockifierInputError, PyNativeBlockifierInputError), (ProgramError, ProgramError, PyProgramError), (Pyo3Error, PyErr, PyPyo3Error), diff --git a/crates/native_blockifier/src/py_transaction.rs b/crates/native_blockifier/src/py_transaction.rs index e1d6360898..3349bcfc5e 100644 --- a/crates/native_blockifier/src/py_transaction.rs +++ b/crates/native_blockifier/src/py_transaction.rs @@ -1,13 +1,12 @@ use std::collections::BTreeMap; -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::contract_class::{ClassInfo, ContractClass}; use starknet_api::execution_resources::GasAmount; use starknet_api::transaction::fields::{ DeprecatedResourceBoundsMapping, diff --git a/crates/papyrus_execution/src/lib.rs b/crates/papyrus_execution/src/lib.rs index 5b31eb58d6..01cd9b7a23 100644 --- a/crates/papyrus_execution/src/lib.rs +++ b/crates/papyrus_execution/src/lib.rs @@ -27,7 +27,6 @@ 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; use blockifier::execution::entry_point::{ CallEntryPoint, CallType as BlockifierCallType, @@ -54,7 +53,7 @@ use papyrus_storage::header::HeaderStorageReader; use papyrus_storage::{StorageError, StorageReader}; use serde::{Deserialize, Serialize}; use starknet_api::block::{BlockHashAndNumber, BlockNumber, NonzeroGasPrice, StarknetVersion}; -use starknet_api::contract_class::EntryPointType; +use starknet_api::contract_class::{ClassInfo, EntryPointType}; use starknet_api::core::{ChainId, ClassHash, ContractAddress, EntryPointSelector}; use starknet_api::data_availability::L1DataAvailabilityMode; use starknet_api::deprecated_contract_class::ContractClass as DeprecatedContractClass; @@ -164,7 +163,7 @@ pub enum ExecutionError { BadDeclareTransaction { tx: DeclareTransaction, #[source] - err: blockifier::execution::errors::ContractClassError, + err: StarknetApiError, }, #[error("Execution config file does not contain a configuration for all blocks")] ConfigContentError,