From d095a9d1392a7a3ad2bb9354fdc813d08edafb62 Mon Sep 17 00:00:00 2001 From: Yoni <78365039+Yoni-Starkware@users.noreply.github.com> Date: Thu, 19 Sep 2024 19:04:20 +0300 Subject: [PATCH] refactor(blockifier): delete transaction_utils (#899) --- .../src/execution/execution_utils.rs | 4 ++ .../src/execution/syscalls/hint_processor.rs | 2 +- .../blockifier/src/execution/syscalls/mod.rs | 2 +- crates/blockifier/src/transaction.rs | 1 - .../src/transaction/account_transaction.rs | 2 +- .../src/transaction/transaction_utils.rs | 40 ------------------- .../src/transaction/transactions.rs | 25 ++++++++++-- 7 files changed, 28 insertions(+), 48 deletions(-) delete mode 100644 crates/blockifier/src/transaction/transaction_utils.rs diff --git a/crates/blockifier/src/execution/execution_utils.rs b/crates/blockifier/src/execution/execution_utils.rs index 7824e13873..306d973f8b 100644 --- a/crates/blockifier/src/execution/execution_utils.rs +++ b/crates/blockifier/src/execution/execution_utils.rs @@ -71,6 +71,10 @@ pub fn execute_entry_point_call( } } +pub fn update_remaining_gas(remaining_gas: &mut u64, call_info: &CallInfo) { + *remaining_gas -= call_info.execution.gas_consumed; +} + pub fn read_execution_retdata( runner: &CairoRunner, retdata_size: MaybeRelocatable, diff --git a/crates/blockifier/src/execution/syscalls/hint_processor.rs b/crates/blockifier/src/execution/syscalls/hint_processor.rs index 4ca0d131e7..3753febac9 100644 --- a/crates/blockifier/src/execution/syscalls/hint_processor.rs +++ b/crates/blockifier/src/execution/syscalls/hint_processor.rs @@ -31,6 +31,7 @@ use crate::execution::execution_utils::{ felt_from_ptr, felt_range_from_ptr, max_fee_for_execution_info, + update_remaining_gas, write_maybe_relocatable, ReadOnlySegment, ReadOnlySegments, @@ -73,7 +74,6 @@ use crate::execution::syscalls::{ use crate::state::errors::StateError; use crate::state::state_api::State; use crate::transaction::objects::{CurrentTransactionInfo, TransactionInfo}; -use crate::transaction::transaction_utils::update_remaining_gas; pub type SyscallCounter = HashMap; diff --git a/crates/blockifier/src/execution/syscalls/mod.rs b/crates/blockifier/src/execution/syscalls/mod.rs index da6df5f2e4..9091f92ea1 100644 --- a/crates/blockifier/src/execution/syscalls/mod.rs +++ b/crates/blockifier/src/execution/syscalls/mod.rs @@ -43,12 +43,12 @@ use crate::execution::entry_point::{CallEntryPoint, CallType, ConstructorContext use crate::execution::execution_utils::{ execute_deployment, felt_from_ptr, + update_remaining_gas, write_felt, write_maybe_relocatable, ReadOnlySegment, }; use crate::execution::syscalls::hint_processor::{INVALID_INPUT_LENGTH_ERROR, OUT_OF_GAS_ERROR}; -use crate::transaction::transaction_utils::update_remaining_gas; use crate::versioned_constants::{EventLimits, VersionedConstants}; pub mod hint_processor; diff --git a/crates/blockifier/src/transaction.rs b/crates/blockifier/src/transaction.rs index 78ee679cae..4cf550f4c6 100644 --- a/crates/blockifier/src/transaction.rs +++ b/crates/blockifier/src/transaction.rs @@ -8,5 +8,4 @@ pub mod objects; pub mod test_utils; pub mod transaction_execution; pub mod transaction_types; -pub mod transaction_utils; pub mod transactions; diff --git a/crates/blockifier/src/transaction/account_transaction.rs b/crates/blockifier/src/transaction/account_transaction.rs index e53a5d84a9..15d30dd554 100644 --- a/crates/blockifier/src/transaction/account_transaction.rs +++ b/crates/blockifier/src/transaction/account_transaction.rs @@ -19,6 +19,7 @@ use crate::context::{BlockContext, TransactionContext}; use crate::execution::call_info::{CallInfo, Retdata}; use crate::execution::contract_class::ContractClass; use crate::execution::entry_point::{CallEntryPoint, CallType, EntryPointExecutionContext}; +use crate::execution::execution_utils::update_remaining_gas; use crate::fee::fee_checks::{FeeCheckReportFields, PostExecutionReport}; use crate::fee::fee_utils::{ get_fee_by_gas_vector, @@ -46,7 +47,6 @@ use crate::transaction::objects::{ TransactionPreValidationResult, }; use crate::transaction::transaction_types::TransactionType; -use crate::transaction::transaction_utils::update_remaining_gas; use crate::transaction::transactions::{ DeclareTransaction, DeployAccountTransaction, diff --git a/crates/blockifier/src/transaction/transaction_utils.rs b/crates/blockifier/src/transaction/transaction_utils.rs deleted file mode 100644 index 1f4edc40c4..0000000000 --- a/crates/blockifier/src/transaction/transaction_utils.rs +++ /dev/null @@ -1,40 +0,0 @@ -use starknet_api::transaction::TransactionVersion; - -use crate::execution::call_info::CallInfo; -use crate::execution::contract_class::ContractClass; -use crate::transaction::errors::TransactionExecutionError; - -pub fn update_remaining_gas(remaining_gas: &mut u64, call_info: &CallInfo) { - *remaining_gas -= call_info.execution.gas_consumed; -} - -pub fn verify_contract_class_version( - contract_class: &ContractClass, - declare_version: TransactionVersion, -) -> Result<(), TransactionExecutionError> { - match contract_class { - // TODO: Make TransactionVersion an enum and use match here. - ContractClass::V0(_) => { - if declare_version == TransactionVersion::ZERO - || declare_version == TransactionVersion::ONE - { - return Ok(()); - } - Err(TransactionExecutionError::ContractClassVersionMismatch { - declare_version, - cairo_version: 0, - }) - } - ContractClass::V1(_) => { - if declare_version == TransactionVersion::TWO - || declare_version == TransactionVersion::THREE - { - return Ok(()); - } - Err(TransactionExecutionError::ContractClassVersionMismatch { - declare_version, - cairo_version: 1, - }) - } - } -} diff --git a/crates/blockifier/src/transaction/transactions.rs b/crates/blockifier/src/transaction/transactions.rs index a9c20f1b8d..4d27c34269 100644 --- a/crates/blockifier/src/transaction/transactions.rs +++ b/crates/blockifier/src/transaction/transactions.rs @@ -25,7 +25,7 @@ use crate::execution::entry_point::{ ConstructorContext, EntryPointExecutionContext, }; -use crate::execution::execution_utils::execute_deployment; +use crate::execution::execution_utils::{execute_deployment, update_remaining_gas}; use crate::state::cached_state::TransactionalState; use crate::state::errors::StateError; use crate::state::state_api::{State, UpdatableState}; @@ -41,8 +41,6 @@ use crate::transaction::objects::{ TransactionInfo, TransactionInfoCreator, }; -use crate::transaction::transaction_utils::{update_remaining_gas, verify_contract_class_version}; - #[cfg(test)] #[path = "transactions_test.rs"] mod test; @@ -153,7 +151,26 @@ impl DeclareTransaction { only_query: bool, ) -> TransactionExecutionResult { let declare_version = declare_tx.version(); - verify_contract_class_version(&class_info.contract_class(), declare_version)?; + // Verify contract class version. + match &class_info.contract_class() { + // TODO: Make TransactionVersion an enum and use match here. + ContractClass::V0(_) => { + if declare_version > TransactionVersion::ONE { + Err(TransactionExecutionError::ContractClassVersionMismatch { + declare_version, + cairo_version: 0, + })? + } + } + ContractClass::V1(_) => { + if declare_version <= TransactionVersion::ONE { + Err(TransactionExecutionError::ContractClassVersionMismatch { + declare_version, + cairo_version: 1, + })? + } + } + } Ok(Self { tx: declare_tx, tx_hash, class_info, only_query }) }