From e19524c33a028a982e5df37c0e8aadf45618ff8f Mon Sep 17 00:00:00 2001 From: Arni Hod Date: Sun, 4 Aug 2024 09:21:46 +0300 Subject: [PATCH] refactor: streamline class info object to match contract class --- .../src/execution/contract_class.rs | 53 ++++++++---------- .../blockifier/src/transaction/test_utils.rs | 12 +++-- .../native_blockifier/src/py_transaction.rs | 26 +++++---- crates/papyrus_execution/src/lib.rs | 54 ++++--------------- 4 files changed, 50 insertions(+), 95 deletions(-) diff --git a/crates/blockifier/src/execution/contract_class.rs b/crates/blockifier/src/execution/contract_class.rs index 26cc6ee46a..e029f0f272 100644 --- a/crates/blockifier/src/execution/contract_class.rs +++ b/crates/blockifier/src/execution/contract_class.rs @@ -34,7 +34,7 @@ use super::execution_utils::poseidon_hash_many_cost; use crate::abi::abi_utils::selector_from_name; use crate::abi::constants::{self, CONSTRUCTOR_ENTRY_POINT_NAME}; use crate::execution::entry_point::CallEntryPoint; -use crate::execution::errors::{ContractClassError, PreExecutionError}; +use crate::execution::errors::PreExecutionError; use crate::execution::execution_utils::sn_api_to_cairo_vm_program; use crate::fee::eth_gas_constants; use crate::transaction::errors::TransactionExecutionError; @@ -43,8 +43,6 @@ use crate::transaction::errors::TransactionExecutionError; #[path = "contract_class_test.rs"] pub mod test; -pub type ContractClassResult = Result; - /// Represents a runnable Starknet contract class (meaning, the program is runnable by the VM). #[derive(Clone, Debug, Eq, PartialEq, derive_more::From)] pub enum ContractClass { @@ -483,10 +481,9 @@ fn convert_entry_points_v1(external: Vec) -> Vec for ClassInfo { @@ -499,7 +496,7 @@ impl TryFrom for ClassInfo { abi_length, } = class_info; - Ok(Self { + Ok(Self::V1 { contract_class: casm_contract_class.try_into()?, sierra_program_length, abi_length, @@ -509,19 +506,31 @@ impl TryFrom for ClassInfo { impl ClassInfo { pub fn bytecode_length(&self) -> usize { - self.contract_class.bytecode_length() + match self { + ClassInfo::V0 { contract_class, .. } => contract_class.bytecode_length(), + ClassInfo::V1 { contract_class, .. } => contract_class.bytecode_length(), + } } pub fn contract_class(&self) -> ContractClass { - self.contract_class.clone() + match self { + ClassInfo::V0 { contract_class, .. } => ContractClass::V0(contract_class.clone()), + ClassInfo::V1 { contract_class, .. } => ContractClass::V1(contract_class.clone()), + } } pub fn sierra_program_length(&self) -> usize { - self.sierra_program_length + match self { + ClassInfo::V0 { .. } => 0, + ClassInfo::V1 { sierra_program_length, .. } => *sierra_program_length, + } } pub fn abi_length(&self) -> usize { - self.abi_length + match self { + ClassInfo::V0 { abi_length, .. } => *abi_length, + ClassInfo::V1 { abi_length, .. } => *abi_length, + } } pub fn code_size(&self) -> usize { @@ -530,24 +539,4 @@ impl ClassInfo { * 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, - }) - } - } } diff --git a/crates/blockifier/src/transaction/test_utils.rs b/crates/blockifier/src/transaction/test_utils.rs index 621a69052c..b448230709 100644 --- a/crates/blockifier/src/transaction/test_utils.rs +++ b/crates/blockifier/src/transaction/test_utils.rs @@ -300,11 +300,13 @@ pub fn l1_resource_bounds(max_amount: u64, max_price: u128) -> DeprecatedResourc } pub fn calculate_class_info_for_testing(contract_class: ContractClass) -> ClassInfo { - let sierra_program_length = match contract_class { - ContractClass::V0(_) => 0, - ContractClass::V1(_) => 100, - }; - ClassInfo::new(&contract_class, sierra_program_length, 100).unwrap() + let abi_length = 100; + match contract_class { + ContractClass::V0(contract_class) => ClassInfo::V0 { contract_class, abi_length }, + ContractClass::V1(contract_class) => { + ClassInfo::V1 { contract_class, sierra_program_length: 100, abi_length } + } + } } pub fn emit_n_events_tx( diff --git a/crates/native_blockifier/src/py_transaction.rs b/crates/native_blockifier/src/py_transaction.rs index 443ed57c90..4697bd8ba9 100644 --- a/crates/native_blockifier/src/py_transaction.rs +++ b/crates/native_blockifier/src/py_transaction.rs @@ -1,11 +1,6 @@ use std::collections::BTreeMap; -use blockifier::execution::contract_class::{ - ClassInfo, - ContractClass, - ContractClassV0, - ContractClassV1, -}; +use blockifier::execution::contract_class::{ClassInfo, ContractClassV0, ContractClassV1}; use blockifier::transaction::account_transaction::AccountTransaction; use blockifier::transaction::transaction_execution::Transaction; use blockifier::transaction::transaction_types::TransactionType; @@ -165,21 +160,24 @@ impl PyClassInfo { py_class_info: PyClassInfo, tx: &starknet_api::transaction::DeclareTransaction, ) -> NativeBlockifierResult { - let contract_class: ContractClass = match tx { + let class_info = match tx { starknet_api::transaction::DeclareTransaction::V0(_) | starknet_api::transaction::DeclareTransaction::V1(_) => { - ContractClassV0::try_from_json_string(&py_class_info.raw_contract_class)?.into() + let contract_class = + ContractClassV0::try_from_json_string(&py_class_info.raw_contract_class)?; + ClassInfo::V0 { contract_class, abi_length: py_class_info.abi_length } } starknet_api::transaction::DeclareTransaction::V2(_) | starknet_api::transaction::DeclareTransaction::V3(_) => { - ContractClassV1::try_from_json_string(&py_class_info.raw_contract_class)?.into() + let contract_class = + ContractClassV1::try_from_json_string(&py_class_info.raw_contract_class)?; + ClassInfo::V1 { + contract_class, + sierra_program_length: py_class_info.sierra_program_length, + abi_length: py_class_info.abi_length, + } } }; - let class_info = ClassInfo::new( - &contract_class, - py_class_info.sierra_program_length, - py_class_info.abi_length, - )?; Ok(class_info) } } diff --git a/crates/papyrus_execution/src/lib.rs b/crates/papyrus_execution/src/lib.rs index f08fadcebd..c8dba3a9ff 100644 --- a/crates/papyrus_execution/src/lib.rs +++ b/crates/papyrus_execution/src/lib.rs @@ -29,7 +29,7 @@ use blockifier::blockifier::block::{pre_process_block, BlockInfo, BlockNumberHas use blockifier::bouncer::BouncerConfig; use blockifier::context::{BlockContext, ChainInfo, FeeTokenAddresses, TransactionContext}; use blockifier::execution::call_info::CallExecution; -use blockifier::execution::contract_class::{ClassInfo, ContractClass as BlockifierContractClass}; +use blockifier::execution::contract_class::ClassInfo; use blockifier::execution::entry_point::{ CallEntryPoint, CallType as BlockifierCallType, @@ -162,12 +162,6 @@ impl SerializeConfig for ExecutionConfig { /// The error type for the execution module. #[derive(thiserror::Error, Debug)] pub enum ExecutionError { - #[error("Bad declare tx: {tx:?}. error: {err:?}")] - BadDeclareTransaction { - tx: DeclareTransaction, - #[source] - err: blockifier::execution::errors::ContractClassError, - }, #[error("Execution config file does not contain a configuration for all blocks")] ConfigContentError, #[error(transparent)] @@ -400,8 +394,6 @@ pub type AbiSize = usize; /// The size of the sierra program. pub type SierraSize = usize; -const DEPRECATED_CONTRACT_SIERRA_SIZE: SierraSize = 0; - /// The transaction input to be executed. // TODO(yair): This should use broadcasted transactions instead of regular transactions, but the // blockifier expects regular transactions. Consider changing the blockifier to use broadcasted txs. @@ -753,19 +745,15 @@ fn to_blockifier_tx( abi_length, only_query, ) => { - let class_v0 = BlockifierContractClass::V0(deprecated_class.try_into().map_err( + let contract_class = 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::V0 { contract_class, abi_length }; BlockifierTransaction::from_api( Transaction::Declare(DeclareTransaction::V0(declare_tx)), tx_hash, @@ -782,14 +770,8 @@ fn to_blockifier_tx( abi_length, only_query, ) => { - let class_v0 = BlockifierContractClass::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 contract_class = deprecated_class.try_into().map_err(BlockifierError::new)?; + let class_info = ClassInfo::V0 { contract_class, abi_length }; BlockifierTransaction::from_api( Transaction::Declare(DeclareTransaction::V1(declare_tx)), tx_hash, @@ -807,16 +789,8 @@ fn to_blockifier_tx( abi_length, only_query, ) => { - let class_v1 = BlockifierContractClass::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 { - tx: DeclareTransaction::V2(declare_tx.clone()), - err, - } - })?; + let contract_class = compiled_class.try_into().map_err(BlockifierError::new)?; + let class_info = ClassInfo::V1 { contract_class, sierra_program_length, abi_length }; BlockifierTransaction::from_api( Transaction::Declare(DeclareTransaction::V2(declare_tx)), tx_hash, @@ -834,16 +808,8 @@ fn to_blockifier_tx( abi_length, only_query, ) => { - let class_v1 = BlockifierContractClass::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 { - tx: DeclareTransaction::V3(declare_tx.clone()), - err, - } - })?; + let contract_class = compiled_class.try_into().map_err(BlockifierError::new)?; + let class_info = ClassInfo::V1 { contract_class, sierra_program_length, abi_length }; BlockifierTransaction::from_api( Transaction::Declare(DeclareTransaction::V3(declare_tx)), tx_hash,