From 5af612911494b33c36dff54c9fa3cc326481e86c Mon Sep 17 00:00:00 2001 From: Yonatan Iluz Date: Tue, 19 Nov 2024 21:46:46 +0200 Subject: [PATCH] refactor(blockifier): remove execution_resources arg --- Cargo.lock | 1 - .../src/blockifier/stateful_validator.rs | 8 +-- crates/blockifier/src/execution/call_info.rs | 53 ++++++++++------- .../deprecated_entry_point_execution.rs | 36 +++++------- .../deprecated_syscalls/hint_processor.rs | 6 +- .../src/execution/deprecated_syscalls/mod.rs | 1 - .../blockifier/src/execution/entry_point.rs | 28 ++------- .../src/execution/entry_point_execution.rs | 25 +++----- .../src/execution/execution_utils.rs | 17 +----- .../execution/native/entry_point_execution.rs | 16 ++++-- .../src/execution/native/syscall_handler.rs | 18 ++---- .../src/execution/syscalls/hint_processor.rs | 12 +--- .../blockifier/src/execution/syscalls/mod.rs | 1 - .../syscalls/syscall_tests/library_call.rs | 20 ++----- crates/blockifier/src/fee/receipt.rs | 14 ++--- crates/blockifier/src/test_utils/prices.rs | 1 - .../blockifier/src/test_utils/struct_impls.rs | 3 +- .../src/transaction/account_transaction.rs | 57 ++++--------------- .../src/transaction/objects_test.rs | 34 +++++++---- .../src/transaction/transaction_execution.rs | 9 +-- .../src/transaction/transactions.rs | 32 ++++------- crates/papyrus_execution/src/lib.rs | 8 +-- 22 files changed, 139 insertions(+), 261 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8cac87e986..2a46eb4ea4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1500,7 +1500,6 @@ dependencies = [ "indexmap 2.6.0", "itertools 0.12.1", "keccak", - "lazy_static", "log", "num-bigint 0.4.6", "num-integer", diff --git a/crates/blockifier/src/blockifier/stateful_validator.rs b/crates/blockifier/src/blockifier/stateful_validator.rs index 8cf9ed410e..1d2a12ae3e 100644 --- a/crates/blockifier/src/blockifier/stateful_validator.rs +++ b/crates/blockifier/src/blockifier/stateful_validator.rs @@ -1,6 +1,5 @@ use std::sync::Arc; -use cairo_vm::vm::runners::cairo_runner::ExecutionResources; use starknet_api::core::{ContractAddress, Nonce}; use starknet_api::executable_transaction::AccountTransaction as ApiTransaction; use thiserror::Error; @@ -12,7 +11,7 @@ use crate::blockifier::transaction_executor::{ BLOCK_STATE_ACCESS_ERR, }; use crate::context::{BlockContext, TransactionContext}; -use crate::execution::call_info::{gas_for_fee_from_call_infos, CallInfo, ChargedResources}; +use crate::execution::call_info::CallInfo; use crate::fee::fee_checks::PostValidationReport; use crate::fee::receipt::TransactionReceipt; use crate::state::cached_state::CachedState; @@ -111,20 +110,16 @@ impl StatefulValidator { tx: &AccountTransaction, mut remaining_gas: u64, ) -> StatefulValidatorResult<(Option, TransactionReceipt)> { - let mut execution_resources = ExecutionResources::default(); let tx_context = Arc::new(self.tx_executor.block_context.to_tx_context(tx)); let limit_steps_by_resources = tx.enforce_fee(); let validate_call_info = tx.validate_tx( self.tx_executor.block_state.as_mut().expect(BLOCK_STATE_ACCESS_ERR), - &mut execution_resources, tx_context.clone(), &mut remaining_gas, limit_steps_by_resources, )?; - let gas_for_fee = gas_for_fee_from_call_infos(&validate_call_info, &None); - let tx_receipt = TransactionReceipt::from_account_tx( tx, &tx_context, @@ -134,7 +129,6 @@ impl StatefulValidator { .as_mut() .expect(BLOCK_STATE_ACCESS_ERR) .get_actual_state_changes()?, - &ChargedResources { vm_resources: execution_resources, gas_for_fee }, CallInfo::summarize_many( validate_call_info.iter(), &tx_context.block_context.versioned_constants, diff --git a/crates/blockifier/src/execution/call_info.rs b/crates/blockifier/src/execution/call_info.rs index b4a3708198..e078376b48 100644 --- a/crates/blockifier/src/execution/call_info.rs +++ b/crates/blockifier/src/execution/call_info.rs @@ -1,6 +1,6 @@ use std::collections::HashSet; use std::iter::Sum; -use std::ops::Add; +use std::ops::{Add, AddAssign}; use cairo_vm::vm::runners::cairo_runner::ExecutionResources; use serde::Serialize; @@ -73,6 +73,7 @@ pub struct EventSummary { #[derive(Clone, Debug, Default, PartialEq)] pub struct ExecutionSummary { + pub charged_resources: ChargedResources, pub executed_class_hashes: HashSet, pub visited_storage_entries: HashSet, pub l2_to_l1_payload_lengths: Vec, @@ -83,6 +84,7 @@ impl Add for ExecutionSummary { type Output = Self; fn add(mut self, other: Self) -> Self { + self.charged_resources += &other.charged_resources; self.executed_class_hashes.extend(other.executed_class_hashes); self.visited_storage_entries.extend(other.visited_storage_entries); self.l2_to_l1_payload_lengths.extend(other.l2_to_l1_payload_lengths); @@ -113,25 +115,22 @@ impl ChargedResources { } } -/// Returns the total gas_for_fee used in the given validate and execute calls. -pub fn gas_for_fee_from_call_infos( - validate: &Option, - execute: &Option, -) -> GasAmount { - let validate_gas_amount = validate - .as_ref() - .map(|call_info| call_info.charged_resources.gas_for_fee) - .unwrap_or(GasAmount(0)); - let execute_gas_amount = execute - .as_ref() - .map(|call_info| call_info.charged_resources.gas_for_fee) - .unwrap_or(GasAmount(0)); - validate_gas_amount.checked_add(execute_gas_amount).unwrap_or_else(|| { - panic!( - "Gas for fee overflowed: tried to add {execute_gas_amount} to \ - {validate_gas_amount}", - ) - }) +impl Add<&ChargedResources> for &ChargedResources { + type Output = ChargedResources; + + fn add(self, rhs: &ChargedResources) -> ChargedResources { + let mut new = self.clone(); + new.add_assign(rhs); + new + } +} + +impl AddAssign<&ChargedResources> for ChargedResources { + fn add_assign(&mut self, other: &Self) { + self.vm_resources += &other.vm_resources; + self.gas_for_fee = + self.gas_for_fee.checked_add(other.gas_for_fee).expect("Gas for fee overflowed."); + } } /// Represents the full effects of executing an entry point, including the inner calls it invoked. @@ -209,6 +208,9 @@ impl CallInfo { } ExecutionSummary { + // Note: the charged resourses of a call contains the inner call resources, unlike other + // fields such as events and messages, + charged_resources: self.charged_resources.clone(), executed_class_hashes, visited_storage_entries, l2_to_l1_payload_lengths, @@ -222,6 +224,17 @@ impl CallInfo { ) -> ExecutionSummary { call_infos.map(|call_info| call_info.summarize(versioned_constants)).sum() } + + pub fn summarize_charged_resources<'a>( + call_infos: impl Iterator, + ) -> ChargedResources { + // Note: the charged resourses of a call contains the inner call resources, unlike other + // fields such as events and messages, + call_infos.fold(ChargedResources::default(), |mut acc, inner_call| { + acc += &inner_call.charged_resources; + acc + }) + } } pub struct CallInfoIter<'a> { diff --git a/crates/blockifier/src/execution/deprecated_entry_point_execution.rs b/crates/blockifier/src/execution/deprecated_entry_point_execution.rs index 0d1b8029c8..fd4084682d 100644 --- a/crates/blockifier/src/execution/deprecated_entry_point_execution.rs +++ b/crates/blockifier/src/execution/deprecated_entry_point_execution.rs @@ -4,11 +4,12 @@ use cairo_vm::types::builtin_name::BuiltinName; use cairo_vm::types::layout_name::LayoutName; use cairo_vm::types::relocatable::{MaybeRelocatable, Relocatable}; use cairo_vm::vm::errors::vm_errors::VirtualMachineError; -use cairo_vm::vm::runners::cairo_runner::{CairoArg, CairoRunner, ExecutionResources}; +use cairo_vm::vm::runners::cairo_runner::{CairoArg, CairoRunner}; use starknet_api::abi::abi_utils::selector_from_name; use starknet_api::abi::constants::{CONSTRUCTOR_ENTRY_POINT_NAME, DEFAULT_ENTRY_POINT_SELECTOR}; use starknet_api::contract_class::EntryPointType; use starknet_api::core::EntryPointSelector; +use starknet_api::execution_resources::GasAmount; use starknet_api::hash::StarkHash; use super::execution_utils::SEGMENT_ARENA_BUILTIN_SIZE; @@ -45,11 +46,10 @@ pub fn execute_entry_point_call( call: CallEntryPoint, contract_class: ContractClassV0, state: &mut dyn State, - resources: &mut ExecutionResources, context: &mut EntryPointExecutionContext, ) -> EntryPointExecutionResult { let VmExecutionContext { mut runner, mut syscall_handler, initial_syscall_ptr, entry_point_pc } = - initialize_execution_context(&call, contract_class, state, resources, context)?; + initialize_execution_context(&call, contract_class, state, context)?; let (implicit_args, args) = prepare_call_arguments( &call, @@ -59,27 +59,16 @@ pub fn execute_entry_point_call( )?; let n_total_args = args.len(); - // Fix the VM resources, in order to calculate the usage of this run at the end. - let previous_resources = syscall_handler.resources.clone(); - // Execute. run_entry_point(&mut runner, &mut syscall_handler, entry_point_pc, args)?; - Ok(finalize_execution( - runner, - syscall_handler, - call, - previous_resources, - implicit_args, - n_total_args, - )?) + Ok(finalize_execution(runner, syscall_handler, call, implicit_args, n_total_args)?) } pub fn initialize_execution_context<'a>( call: &CallEntryPoint, contract_class: ContractClassV0, state: &'a mut dyn State, - resources: &'a mut ExecutionResources, context: &'a mut EntryPointExecutionContext, ) -> Result, PreExecutionError> { // Verify use of cairo0 builtins only. @@ -110,7 +99,6 @@ pub fn initialize_execution_context<'a>( let initial_syscall_ptr = runner.vm.add_memory_segment(); let syscall_handler = DeprecatedSyscallHintProcessor::new( state, - resources, context, initial_syscall_ptr, call.storage_address, @@ -231,7 +219,6 @@ pub fn finalize_execution( mut runner: CairoRunner, syscall_handler: DeprecatedSyscallHintProcessor<'_>, call: CallEntryPoint, - previous_resources: ExecutionResources, implicit_args: Vec, n_total_args: usize, ) -> Result { @@ -263,12 +250,17 @@ pub fn finalize_execution( .get_mut(&BuiltinName::segment_arena) .map_or_else(|| {}, |val| *val *= SEGMENT_ARENA_BUILTIN_SIZE); } - *syscall_handler.resources += &vm_resources_without_inner_calls; // Take into account the syscall resources of the current call. - *syscall_handler.resources += + vm_resources_without_inner_calls += &versioned_constants.get_additional_os_syscall_resources(&syscall_handler.syscall_counter); - let full_call_resources = &*syscall_handler.resources - &previous_resources; + let charged_resources_without_inner_calls = ChargedResources { + vm_resources: vm_resources_without_inner_calls, + gas_for_fee: GasAmount(0), + }; + let charged_resources = &charged_resources_without_inner_calls + + &CallInfo::summarize_charged_resources(syscall_handler.inner_calls.iter()); + Ok(CallInfo { call, execution: CallExecution { @@ -280,9 +272,7 @@ pub fn finalize_execution( }, inner_calls: syscall_handler.inner_calls, tracked_resource: TrackedResource::CairoSteps, - charged_resources: ChargedResources::from_execution_resources( - full_call_resources.filter_unused_builtins(), - ), + charged_resources, storage_read_values: syscall_handler.read_values, accessed_storage_keys: syscall_handler.accessed_keys, ..Default::default() diff --git a/crates/blockifier/src/execution/deprecated_syscalls/hint_processor.rs b/crates/blockifier/src/execution/deprecated_syscalls/hint_processor.rs index 332f037c7f..0ea7f175f6 100644 --- a/crates/blockifier/src/execution/deprecated_syscalls/hint_processor.rs +++ b/crates/blockifier/src/execution/deprecated_syscalls/hint_processor.rs @@ -14,7 +14,7 @@ use cairo_vm::types::relocatable::{MaybeRelocatable, Relocatable}; use cairo_vm::vm::errors::hint_errors::HintError; use cairo_vm::vm::errors::memory_errors::MemoryError; use cairo_vm::vm::errors::vm_errors::VirtualMachineError; -use cairo_vm::vm::runners::cairo_runner::{ExecutionResources, ResourceTracker, RunResources}; +use cairo_vm::vm::runners::cairo_runner::{ResourceTracker, RunResources}; use cairo_vm::vm::vm_core::VirtualMachine; use num_bigint::{BigUint, TryFromBigIntError}; use starknet_api::contract_class::EntryPointType; @@ -163,7 +163,6 @@ impl DeprecatedSyscallExecutionError { pub struct DeprecatedSyscallHintProcessor<'a> { // Input for execution. pub state: &'a mut dyn State, - pub resources: &'a mut ExecutionResources, pub context: &'a mut EntryPointExecutionContext, pub storage_address: ContractAddress, pub caller_address: ContractAddress, @@ -194,7 +193,6 @@ pub struct DeprecatedSyscallHintProcessor<'a> { impl<'a> DeprecatedSyscallHintProcessor<'a> { pub fn new( state: &'a mut dyn State, - resources: &'a mut ExecutionResources, context: &'a mut EntryPointExecutionContext, initial_syscall_ptr: Relocatable, storage_address: ContractAddress, @@ -202,7 +200,6 @@ impl<'a> DeprecatedSyscallHintProcessor<'a> { ) -> Self { DeprecatedSyscallHintProcessor { state, - resources, context, storage_address, caller_address, @@ -503,7 +500,6 @@ pub fn execute_inner_call( // Use `non_reverting_execute` since we don't support reverts here. let call_info = call.non_reverting_execute( syscall_handler.state, - syscall_handler.resources, syscall_handler.context, &mut remaining_gas, )?; diff --git a/crates/blockifier/src/execution/deprecated_syscalls/mod.rs b/crates/blockifier/src/execution/deprecated_syscalls/mod.rs index bded7975de..ff0741948f 100644 --- a/crates/blockifier/src/execution/deprecated_syscalls/mod.rs +++ b/crates/blockifier/src/execution/deprecated_syscalls/mod.rs @@ -347,7 +347,6 @@ pub fn deploy( let mut remaining_gas = syscall_handler.context.gas_costs().default_initial_gas_cost; let call_info = execute_deployment( syscall_handler.state, - syscall_handler.resources, syscall_handler.context, ctor_context, request.constructor_calldata, diff --git a/crates/blockifier/src/execution/entry_point.rs b/crates/blockifier/src/execution/entry_point.rs index 63960ae0b2..048023e0c1 100644 --- a/crates/blockifier/src/execution/entry_point.rs +++ b/crates/blockifier/src/execution/entry_point.rs @@ -3,7 +3,7 @@ use std::cmp::min; use std::collections::HashMap; use std::sync::Arc; -use cairo_vm::vm::runners::cairo_runner::{ExecutionResources, ResourceTracker, RunResources}; +use cairo_vm::vm::runners::cairo_runner::{ResourceTracker, RunResources}; use num_traits::{Inv, Zero}; use serde::Serialize; use starknet_api::abi::abi_utils::selector_from_name; @@ -116,7 +116,6 @@ impl CallEntryPoint { pub fn execute( mut self, state: &mut dyn State, - resources: &mut ExecutionResources, context: &mut EntryPointExecutionContext, remaining_gas: &mut u64, ) -> EntryPointExecutionResult { @@ -158,25 +157,17 @@ impl CallEntryPoint { )); // This is the last operation of this function. - execute_entry_point_call_wrapper( - self, - contract_class, - state, - resources, - context, - remaining_gas, - ) + execute_entry_point_call_wrapper(self, contract_class, state, context, remaining_gas) } /// Similar to `execute`, but returns an error if the outer call is reverted. pub fn non_reverting_execute( self, state: &mut dyn State, - resources: &mut ExecutionResources, context: &mut EntryPointExecutionContext, remaining_gas: &mut u64, ) -> EntryPointExecutionResult { - let execution_result = self.execute(state, resources, context, remaining_gas); + let execution_result = self.execute(state, context, remaining_gas); if let Ok(call_info) = &execution_result { // If the execution of the outer call failed, revert the transction. if call_info.execution.failed { @@ -409,7 +400,6 @@ impl EntryPointExecutionContext { pub fn execute_constructor_entry_point( state: &mut dyn State, - resources: &mut ExecutionResources, context: &mut EntryPointExecutionContext, ctor_context: ConstructorContext, calldata: Calldata, @@ -438,15 +428,9 @@ pub fn execute_constructor_entry_point( initial_gas: *remaining_gas, }; - constructor_call.non_reverting_execute(state, resources, context, remaining_gas).map_err( - |error| { - ConstructorEntryPointExecutionError::new( - error, - &ctor_context, - Some(constructor_selector), - ) - }, - ) + constructor_call.non_reverting_execute(state, context, remaining_gas).map_err(|error| { + ConstructorEntryPointExecutionError::new(error, &ctor_context, Some(constructor_selector)) + }) } pub fn handle_empty_constructor( diff --git a/crates/blockifier/src/execution/entry_point_execution.rs b/crates/blockifier/src/execution/entry_point_execution.rs index 1b40aa7b57..a60b5638ab 100644 --- a/crates/blockifier/src/execution/entry_point_execution.rs +++ b/crates/blockifier/src/execution/entry_point_execution.rs @@ -7,7 +7,7 @@ use cairo_vm::vm::errors::cairo_run_errors::CairoRunError; use cairo_vm::vm::errors::memory_errors::MemoryError; use cairo_vm::vm::errors::vm_errors::VirtualMachineError; use cairo_vm::vm::runners::builtin_runner::BuiltinRunner; -use cairo_vm::vm::runners::cairo_runner::{CairoArg, CairoRunner, ExecutionResources}; +use cairo_vm::vm::runners::cairo_runner::{CairoArg, CairoRunner}; use cairo_vm::vm::security::verify_secure_runner; use num_traits::{ToPrimitive, Zero}; use starknet_api::execution_resources::GasAmount; @@ -59,7 +59,6 @@ pub fn execute_entry_point_call( call: CallEntryPoint, contract_class: ContractClassV1, state: &mut dyn State, - resources: &mut ExecutionResources, context: &mut EntryPointExecutionContext, ) -> EntryPointExecutionResult { // Fetch the class hash from `call`. @@ -75,7 +74,7 @@ pub fn execute_entry_point_call( initial_syscall_ptr, entry_point, program_extra_data_length, - } = initialize_execution_context(call, &contract_class, state, resources, context)?; + } = initialize_execution_context(call, &contract_class, state, context)?; let args = prepare_call_arguments( &syscall_handler.call, @@ -86,9 +85,6 @@ pub fn execute_entry_point_call( )?; let n_total_args = args.len(); - // Fix the resources, in order to calculate the usage of this run at the end. - let previous_resources = syscall_handler.resources.clone(); - // Execute. let bytecode_length = contract_class.bytecode_length(); let program_segment_size = bytecode_length + program_extra_data_length; @@ -106,7 +102,6 @@ pub fn execute_entry_point_call( Ok(finalize_execution( runner, syscall_handler, - previous_resources, n_total_args, program_extra_data_length, tracked_resource, @@ -149,7 +144,6 @@ pub fn initialize_execution_context<'a>( call: CallEntryPoint, contract_class: &'a ContractClassV1, state: &'a mut dyn State, - resources: &'a mut ExecutionResources, context: &'a mut EntryPointExecutionContext, ) -> Result, PreExecutionError> { let entry_point = contract_class.get_entry_point(&call)?; @@ -177,7 +171,6 @@ pub fn initialize_execution_context<'a>( let initial_syscall_ptr = runner.vm.add_memory_segment(); let syscall_handler = SyscallHintProcessor::new( state, - resources, context, initial_syscall_ptr, call, @@ -396,7 +389,6 @@ fn to_gas_for_fee( pub fn finalize_execution( mut runner: CairoRunner, mut syscall_handler: SyscallHintProcessor<'_>, - previous_resources: ExecutionResources, n_total_args: usize, program_extra_data_length: usize, tracked_resource: TrackedResource, @@ -418,7 +410,7 @@ pub fn finalize_execution( let call_result = get_call_result(&runner, &syscall_handler)?; - // Take into account the VM execution resources of the current call, without inner calls. + // Take into account the resources of the current call, without inner calls. // Has to happen after marking holes in segments as accessed. let mut vm_resources_without_inner_calls = runner .get_execution_resources() @@ -431,19 +423,20 @@ pub fn finalize_execution( .get_mut(&BuiltinName::segment_arena) .map_or_else(|| {}, |val| *val *= SEGMENT_ARENA_BUILTIN_SIZE); } - *syscall_handler.resources += &vm_resources_without_inner_calls; // Take into account the syscall resources of the current call. - *syscall_handler.resources += + vm_resources_without_inner_calls += &versioned_constants.get_additional_os_syscall_resources(&syscall_handler.syscall_counter); syscall_handler.finalize(); - let full_call_resources = &*syscall_handler.resources - &previous_resources; - let charged_resources = ChargedResources { - vm_resources: full_call_resources.filter_unused_builtins(), + let charged_resources_without_inner_calls = ChargedResources { + vm_resources: vm_resources_without_inner_calls, // TODO(tzahi): Replace with a computed value. gas_for_fee: GasAmount(0), }; + let charged_resources = &charged_resources_without_inner_calls + + &CallInfo::summarize_charged_resources(syscall_handler.inner_calls.iter()); + Ok(CallInfo { call: syscall_handler.call, execution: CallExecution { diff --git a/crates/blockifier/src/execution/execution_utils.rs b/crates/blockifier/src/execution/execution_utils.rs index 75ef489b0c..d2c0b9aeaf 100644 --- a/crates/blockifier/src/execution/execution_utils.rs +++ b/crates/blockifier/src/execution/execution_utils.rs @@ -54,7 +54,6 @@ pub fn execute_entry_point_call_wrapper( mut call: CallEntryPoint, contract_class: RunnableContractClass, state: &mut dyn State, - resources: &mut ExecutionResources, context: &mut EntryPointExecutionContext, remaining_gas: &mut u64, ) -> EntryPointExecutionResult { @@ -82,7 +81,7 @@ pub fn execute_entry_point_call_wrapper( }; let orig_call = call.clone(); - let res = execute_entry_point_call(call, contract_class, state, resources, context); + let res = execute_entry_point_call(call, contract_class, state, context); let current_tracked_resource = context.tracked_resource_stack.pop().expect("Unexpected empty tracked resource."); @@ -123,7 +122,6 @@ pub fn execute_entry_point_call( call: CallEntryPoint, contract_class: RunnableContractClass, state: &mut dyn State, - resources: &mut ExecutionResources, context: &mut EntryPointExecutionContext, ) -> EntryPointExecutionResult { match contract_class { @@ -132,18 +130,11 @@ pub fn execute_entry_point_call( call, contract_class, state, - resources, context, ) } RunnableContractClass::V1(contract_class) => { - entry_point_execution::execute_entry_point_call( - call, - contract_class, - state, - resources, - context, - ) + entry_point_execution::execute_entry_point_call(call, contract_class, state, context) } #[cfg(feature = "cairo_native")] RunnableContractClass::V1Native(contract_class) => { @@ -154,7 +145,6 @@ pub fn execute_entry_point_call( call, contract_class.casm(), state, - resources, context, ) } else { @@ -162,7 +152,6 @@ pub fn execute_entry_point_call( call, contract_class, state, - resources, context, ) } @@ -309,7 +298,6 @@ impl ReadOnlySegments { /// Returns the call info of the deployed class' constructor execution. pub fn execute_deployment( state: &mut dyn State, - resources: &mut ExecutionResources, context: &mut EntryPointExecutionContext, ctor_context: ConstructorContext, constructor_calldata: Calldata, @@ -336,7 +324,6 @@ pub fn execute_deployment( execute_constructor_entry_point( state, - resources, context, ctor_context, constructor_calldata, diff --git a/crates/blockifier/src/execution/native/entry_point_execution.rs b/crates/blockifier/src/execution/native/entry_point_execution.rs index 02f4f45b12..5cd0c7289c 100644 --- a/crates/blockifier/src/execution/native/entry_point_execution.rs +++ b/crates/blockifier/src/execution/native/entry_point_execution.rs @@ -21,13 +21,12 @@ pub fn execute_entry_point_call( call: CallEntryPoint, contract_class: NativeContractClassV1, state: &mut dyn State, - resources: &mut ExecutionResources, context: &mut EntryPointExecutionContext, ) -> EntryPointExecutionResult { let entry_point = contract_class.get_entry_point(&call)?; let mut syscall_handler: NativeSyscallHandler<'_> = - NativeSyscallHandler::new(call, state, resources, context); + NativeSyscallHandler::new(call, state, context); let gas_costs = &syscall_handler.context.versioned_constants().os_constants.gas_costs; let builtin_costs = BuiltinCosts { @@ -82,6 +81,14 @@ fn create_callinfo( let gas_consumed = syscall_handler.call.initial_gas - remaining_gas; + let charged_resources_without_inner_calls = ChargedResources { + vm_resources: ExecutionResources::default(), + // TODO(tzahi): Replace with a computed value. + gas_for_fee: GasAmount(0), + }; + let charged_resources = &charged_resources_without_inner_calls + + &CallInfo::summarize_charged_resources(syscall_handler.inner_calls.iter()); + Ok(CallInfo { call: syscall_handler.call, execution: CallExecution { @@ -91,10 +98,7 @@ fn create_callinfo( failed: call_result.failure_flag, gas_consumed, }, - charged_resources: ChargedResources { - vm_resources: ExecutionResources::default(), - gas_for_fee: GasAmount(gas_consumed), - }, + charged_resources, inner_calls: syscall_handler.inner_calls, storage_read_values: syscall_handler.read_values, accessed_storage_keys: syscall_handler.accessed_keys, diff --git a/crates/blockifier/src/execution/native/syscall_handler.rs b/crates/blockifier/src/execution/native/syscall_handler.rs index 126e88d6d0..2f643d3879 100644 --- a/crates/blockifier/src/execution/native/syscall_handler.rs +++ b/crates/blockifier/src/execution/native/syscall_handler.rs @@ -19,7 +19,6 @@ use cairo_native::starknet::{ U256, }; use cairo_native::starknet_stub::u256_to_biguint; -use cairo_vm::vm::runners::cairo_runner::ExecutionResources; use starknet_api::contract_class::EntryPointType; use starknet_api::core::{ calculate_contract_address, @@ -64,7 +63,6 @@ use crate::transaction::objects::TransactionInfo; pub struct NativeSyscallHandler<'state> { // Input for execution. pub state: &'state mut dyn State, - pub resources: &'state mut ExecutionResources, pub context: &'state mut EntryPointExecutionContext, pub call: CallEntryPoint, @@ -88,13 +86,11 @@ impl<'state> NativeSyscallHandler<'state> { pub fn new( call: CallEntryPoint, state: &'state mut dyn State, - resources: &'state mut ExecutionResources, context: &'state mut EntryPointExecutionContext, ) -> NativeSyscallHandler<'state> { NativeSyscallHandler { state, call, - resources, context, events: Vec::new(), l2_to_l1_messages: Vec::new(), @@ -113,7 +109,7 @@ impl<'state> NativeSyscallHandler<'state> { remaining_gas: &mut u64, ) -> SyscallResult { let call_info = entry_point - .execute(self.state, self.resources, self.context, remaining_gas) + .execute(self.state, self.context, remaining_gas) .map_err(|e| self.handle_error(remaining_gas, e.into()))?; let retdata = call_info.execution.retdata.clone(); @@ -358,15 +354,9 @@ impl<'state> StarknetSyscallHandler for &mut NativeSyscallHandler<'state> { caller_address: deployer_address, }; - let call_info = execute_deployment( - self.state, - self.resources, - self.context, - ctor_context, - calldata, - remaining_gas, - ) - .map_err(|err| self.handle_error(remaining_gas, err.into()))?; + let call_info = + execute_deployment(self.state, self.context, ctor_context, calldata, remaining_gas) + .map_err(|err| self.handle_error(remaining_gas, err.into()))?; let constructor_retdata = call_info.execution.retdata.0[..].to_vec(); diff --git a/crates/blockifier/src/execution/syscalls/hint_processor.rs b/crates/blockifier/src/execution/syscalls/hint_processor.rs index 04d42f7e87..ce944a716b 100644 --- a/crates/blockifier/src/execution/syscalls/hint_processor.rs +++ b/crates/blockifier/src/execution/syscalls/hint_processor.rs @@ -11,7 +11,7 @@ use cairo_vm::types::relocatable::{MaybeRelocatable, Relocatable}; use cairo_vm::vm::errors::hint_errors::HintError; use cairo_vm::vm::errors::memory_errors::MemoryError; use cairo_vm::vm::errors::vm_errors::VirtualMachineError; -use cairo_vm::vm::runners::cairo_runner::{ExecutionResources, ResourceTracker, RunResources}; +use cairo_vm::vm::runners::cairo_runner::{ResourceTracker, RunResources}; use cairo_vm::vm::vm_core::VirtualMachine; use starknet_api::contract_class::EntryPointType; use starknet_api::core::{ClassHash, ContractAddress, EntryPointSelector}; @@ -215,7 +215,6 @@ pub const INVALID_ARGUMENT: &str = pub struct SyscallHintProcessor<'a> { // Input for execution. pub state: &'a mut dyn State, - pub resources: &'a mut ExecutionResources, pub context: &'a mut EntryPointExecutionContext, pub call: CallEntryPoint, @@ -257,7 +256,6 @@ pub struct SyscallHintProcessor<'a> { impl<'a> SyscallHintProcessor<'a> { pub fn new( state: &'a mut dyn State, - resources: &'a mut ExecutionResources, context: &'a mut EntryPointExecutionContext, initial_syscall_ptr: Relocatable, call: CallEntryPoint, @@ -274,7 +272,6 @@ impl<'a> SyscallHintProcessor<'a> { ); SyscallHintProcessor { state, - resources, context, call, inner_calls: vec![], @@ -809,12 +806,7 @@ pub fn execute_inner_call( ) -> SyscallResult { let revert_idx = syscall_handler.context.revert_infos.0.len(); - let call_info = call.execute( - syscall_handler.state, - syscall_handler.resources, - syscall_handler.context, - remaining_gas, - )?; + let call_info = call.execute(syscall_handler.state, syscall_handler.context, remaining_gas)?; let mut raw_retdata = call_info.execution.retdata.0.clone(); let failed = call_info.execution.failed; diff --git a/crates/blockifier/src/execution/syscalls/mod.rs b/crates/blockifier/src/execution/syscalls/mod.rs index 64c65f7d84..d09e266e66 100644 --- a/crates/blockifier/src/execution/syscalls/mod.rs +++ b/crates/blockifier/src/execution/syscalls/mod.rs @@ -269,7 +269,6 @@ pub fn deploy( }; let call_info = execute_deployment( syscall_handler.state, - syscall_handler.resources, syscall_handler.context, ctor_context, request.constructor_calldata, diff --git a/crates/blockifier/src/execution/syscalls/syscall_tests/library_call.rs b/crates/blockifier/src/execution/syscalls/syscall_tests/library_call.rs index a44a0459f0..aa651400bf 100644 --- a/crates/blockifier/src/execution/syscalls/syscall_tests/library_call.rs +++ b/crates/blockifier/src/execution/syscalls/syscall_tests/library_call.rs @@ -182,10 +182,7 @@ fn test_nested_library_call(test_contract: FeatureContract, expected_gas: u64) { }; let first_storage_entry_point_resources = if_native(&test_contract)( - ChargedResources { - vm_resources: ExecutionResources::default(), - gas_for_fee: GasAmount(26990), - }, + ChargedResources { vm_resources: ExecutionResources::default(), gas_for_fee: GasAmount(0) }, ChargedResources::from_execution_resources(ExecutionResources { n_steps: 244, n_memory_holes: 0, @@ -193,10 +190,7 @@ fn test_nested_library_call(test_contract: FeatureContract, expected_gas: u64) { }), ); let storage_entry_point_resources = if_native(&test_contract)( - ChargedResources { - vm_resources: ExecutionResources::default(), - gas_for_fee: GasAmount(26990), - }, + ChargedResources { vm_resources: ExecutionResources::default(), gas_for_fee: GasAmount(0) }, first_storage_entry_point_resources.clone(), ); @@ -221,10 +215,7 @@ fn test_nested_library_call(test_contract: FeatureContract, expected_gas: u64) { }; let library_call_resources = if_native(&test_contract)( - ChargedResources { - vm_resources: ExecutionResources::default(), - gas_for_fee: GasAmount(187970), - }, + ChargedResources { vm_resources: ExecutionResources::default(), gas_for_fee: GasAmount(0) }, ChargedResources::from_execution_resources( &get_syscall_resources(SyscallSelector::LibraryCall) + &ExecutionResources { @@ -263,10 +254,7 @@ fn test_nested_library_call(test_contract: FeatureContract, expected_gas: u64) { }; let main_call_resources = if_native(&test_contract)( - ChargedResources { - vm_resources: ExecutionResources::default(), - gas_for_fee: GasAmount(515110), - }, + ChargedResources { vm_resources: ExecutionResources::default(), gas_for_fee: GasAmount(0) }, ChargedResources::from_execution_resources( &(&get_syscall_resources(SyscallSelector::LibraryCall) * 3) + &ExecutionResources { diff --git a/crates/blockifier/src/fee/receipt.rs b/crates/blockifier/src/fee/receipt.rs index 0188ba7de9..97805f48b8 100644 --- a/crates/blockifier/src/fee/receipt.rs +++ b/crates/blockifier/src/fee/receipt.rs @@ -3,7 +3,7 @@ use starknet_api::execution_resources::{GasAmount, GasVector}; use starknet_api::transaction::fields::Fee; use crate::context::TransactionContext; -use crate::execution::call_info::{ChargedResources, ExecutionSummary}; +use crate::execution::call_info::ExecutionSummary; use crate::fee::resources::{ ComputationResources, StarknetResources, @@ -29,7 +29,6 @@ struct TransactionReceiptParameters<'a> { sender_address: Option, l1_handler_payload_size: Option, execution_summary_without_fee_transfer: ExecutionSummary, - charged_resources: &'a ChargedResources, tx_type: TransactionType, reverted_steps: usize, } @@ -57,11 +56,10 @@ impl TransactionReceipt { sender_address, l1_handler_payload_size, execution_summary_without_fee_transfer, - charged_resources, tx_type, reverted_steps, } = tx_receipt_params; - + let charged_resources = execution_summary_without_fee_transfer.charged_resources.clone(); let starknet_resources = StarknetResources::new( calldata_length, signature_length, @@ -73,7 +71,7 @@ impl TransactionReceipt { // Transaction overhead ('additional') resources are computed in VM resources no matter what // the tracked resources of the transaction are. - let cairo_resources = (&charged_resources.vm_resources + let total_vm_resources = (&charged_resources.vm_resources + &tx_context.block_context.versioned_constants.get_additional_os_tx_resources( tx_type, &starknet_resources, @@ -84,7 +82,7 @@ impl TransactionReceipt { let tx_resources = TransactionResources { starknet_resources, computation: ComputationResources { - vm_resources: cairo_resources, + vm_resources: total_vm_resources, n_reverted_steps: reverted_steps, sierra_gas: charged_resources.gas_for_fee, reverted_sierra_gas: GasAmount(0), // TODO(tzahi): compute value. @@ -116,7 +114,6 @@ impl TransactionReceipt { tx_context: &'a TransactionContext, l1_handler_payload_size: usize, execution_summary_without_fee_transfer: ExecutionSummary, - charged_resources: &'a ChargedResources, state_changes: &'a StateChanges, ) -> Self { Self::from_params(TransactionReceiptParameters { @@ -128,7 +125,6 @@ impl TransactionReceipt { sender_address: None, // L1 handlers have no sender address. l1_handler_payload_size: Some(l1_handler_payload_size), execution_summary_without_fee_transfer, - charged_resources, tx_type: TransactionType::L1Handler, reverted_steps: 0, }) @@ -139,7 +135,6 @@ impl TransactionReceipt { account_tx: &'a AccountTransaction, tx_context: &'a TransactionContext, state_changes: &'a StateChanges, - charged_resources: &'a ChargedResources, execution_summary_without_fee_transfer: ExecutionSummary, reverted_steps: usize, ) -> Self { @@ -152,7 +147,6 @@ impl TransactionReceipt { sender_address: Some(tx_context.tx_info.sender_address()), l1_handler_payload_size: None, execution_summary_without_fee_transfer, - charged_resources, tx_type: account_tx.tx_type(), reverted_steps, }) diff --git a/crates/blockifier/src/test_utils/prices.rs b/crates/blockifier/src/test_utils/prices.rs index f2a282c040..47fba5e7eb 100644 --- a/crates/blockifier/src/test_utils/prices.rs +++ b/crates/blockifier/src/test_utils/prices.rs @@ -70,7 +70,6 @@ fn fee_transfer_resources( fee_transfer_call .execute( state, - &mut ExecutionResources::default(), &mut EntryPointExecutionContext::new( Arc::new(block_context.to_tx_context(&account_invoke_tx(InvokeTxArgs::default()))), ExecutionMode::Execute, diff --git a/crates/blockifier/src/test_utils/struct_impls.rs b/crates/blockifier/src/test_utils/struct_impls.rs index 2d21fa1564..581263a05c 100644 --- a/crates/blockifier/src/test_utils/struct_impls.rs +++ b/crates/blockifier/src/test_utils/struct_impls.rs @@ -9,7 +9,6 @@ use cairo_lang_starknet_classes::casm_contract_class::CasmContractClass; use cairo_lang_starknet_classes::contract_class::ContractClass as SierraContractClass; #[cfg(feature = "cairo_native")] use cairo_native::executor::AotContractExecutor; -use cairo_vm::vm::runners::cairo_runner::ExecutionResources; use serde_json::Value; use starknet_api::block::{BlockNumber, BlockTimestamp, NonzeroGasPrice}; use starknet_api::contract_address; @@ -87,7 +86,7 @@ impl CallEntryPoint { limit_steps_by_resources, ); let mut remaining_gas = self.initial_gas; - self.execute(state, &mut ExecutionResources::default(), &mut context, &mut remaining_gas) + self.execute(state, &mut context, &mut remaining_gas) } /// Executes the call directly in validate mode, without account context. Limits the number of diff --git a/crates/blockifier/src/transaction/account_transaction.rs b/crates/blockifier/src/transaction/account_transaction.rs index cacef31ee4..479e2d78e7 100644 --- a/crates/blockifier/src/transaction/account_transaction.rs +++ b/crates/blockifier/src/transaction/account_transaction.rs @@ -1,6 +1,5 @@ use std::sync::Arc; -use cairo_vm::vm::runners::cairo_runner::ExecutionResources; use starknet_api::abi::abi_utils::selector_from_name; use starknet_api::block::GasPriceVector; use starknet_api::calldata; @@ -28,7 +27,7 @@ use starknet_api::transaction::{constants, TransactionHash, TransactionVersion}; use starknet_types_core::felt::Felt; use crate::context::{BlockContext, TransactionContext}; -use crate::execution::call_info::{gas_for_fee_from_call_infos, CallInfo, ChargedResources}; +use crate::execution::call_info::CallInfo; use crate::execution::contract_class::RunnableContractClass; use crate::execution::entry_point::{CallEntryPoint, CallType, EntryPointExecutionContext}; use crate::execution::stack_trace::{ @@ -406,14 +405,13 @@ impl AccountTransaction { fn handle_validate_tx( &self, state: &mut dyn State, - resources: &mut ExecutionResources, tx_context: Arc, remaining_gas: &mut u64, validate: bool, limit_steps_by_resources: bool, ) -> TransactionExecutionResult> { if validate { - self.validate_tx(state, resources, tx_context, remaining_gas, limit_steps_by_resources) + self.validate_tx(state, tx_context, remaining_gas, limit_steps_by_resources) } else { Ok(None) } @@ -500,12 +498,7 @@ impl AccountTransaction { let mut context = EntryPointExecutionContext::new_invoke(tx_context, true); Ok(fee_transfer_call - .execute( - state, - &mut ExecutionResources::default(), - &mut context, - &mut remaining_gas_for_fee_transfer, - ) + .execute(state, &mut context, &mut remaining_gas_for_fee_transfer) .map_err(TransactionFeeError::ExecuteFeeTransferError)?) } @@ -544,16 +537,13 @@ impl AccountTransaction { fn run_execute( &self, state: &mut S, - resources: &mut ExecutionResources, context: &mut EntryPointExecutionContext, remaining_gas: &mut u64, ) -> TransactionExecutionResult> { match &self.tx { - Transaction::Declare(tx) => tx.run_execute(state, resources, context, remaining_gas), - Transaction::DeployAccount(tx) => { - tx.run_execute(state, resources, context, remaining_gas) - } - Transaction::Invoke(tx) => tx.run_execute(state, resources, context, remaining_gas), + Transaction::Declare(tx) => tx.run_execute(state, context, remaining_gas), + Transaction::DeployAccount(tx) => tx.run_execute(state, context, remaining_gas), + Transaction::Invoke(tx) => tx.run_execute(state, context, remaining_gas), } } @@ -565,7 +555,6 @@ impl AccountTransaction { validate: bool, charge_fee: bool, ) -> TransactionExecutionResult { - let mut resources = ExecutionResources::default(); let validate_call_info: Option; let execute_call_info: Option; if matches!(&self.tx, Transaction::DeployAccount(_)) { @@ -574,11 +563,9 @@ impl AccountTransaction { // validation context. let mut execution_context = EntryPointExecutionContext::new_validate(tx_context.clone(), charge_fee); - execute_call_info = - self.run_execute(state, &mut resources, &mut execution_context, remaining_gas)?; + execute_call_info = self.run_execute(state, &mut execution_context, remaining_gas)?; validate_call_info = self.handle_validate_tx( state, - &mut resources, tx_context.clone(), remaining_gas, validate, @@ -589,23 +576,18 @@ impl AccountTransaction { EntryPointExecutionContext::new_invoke(tx_context.clone(), charge_fee); validate_call_info = self.handle_validate_tx( state, - &mut resources, tx_context.clone(), remaining_gas, validate, charge_fee, )?; - execute_call_info = - self.run_execute(state, &mut resources, &mut execution_context, remaining_gas)?; + execute_call_info = self.run_execute(state, &mut execution_context, remaining_gas)?; } - let gas_for_fee = gas_for_fee_from_call_infos(&validate_call_info, &execute_call_info); - let tx_receipt = TransactionReceipt::from_account_tx( self, &tx_context, &state.get_actual_state_changes()?, - &ChargedResources { vm_resources: resources, gas_for_fee }, CallInfo::summarize_many( validate_call_info.iter().chain(execute_call_info.iter()), &tx_context.block_context.versioned_constants, @@ -633,13 +615,11 @@ impl AccountTransaction { validate: bool, charge_fee: bool, ) -> TransactionExecutionResult { - let mut validate_resources = ExecutionResources::default(); let mut execution_context = EntryPointExecutionContext::new_invoke(tx_context.clone(), charge_fee); // Run the validation, and if execution later fails, only keep the validation diff. let validate_call_info = self.handle_validate_tx( state, - &mut validate_resources, tx_context.clone(), remaining_gas, validate, @@ -658,16 +638,10 @@ impl AccountTransaction { // Create copies of state and validate_resources for the execution. // Both will be rolled back if the execution is reverted or committed upon success. - let mut execution_resources = validate_resources.clone(); let mut execution_state = TransactionalState::create_transactional(state); - let execution_result = self.run_execute( - &mut execution_state, - &mut execution_resources, - &mut execution_context, - remaining_gas, - ); - let gas_for_fee = gas_for_fee_from_call_infos(&validate_call_info, &None); + let execution_result = + self.run_execute(&mut execution_state, &mut execution_context, remaining_gas); // Pre-compute cost in case of revert. // TODO(tzahi): add reverted_l2_gas to the receipt. @@ -677,7 +651,6 @@ impl AccountTransaction { self, &tx_context, &validate_state_changes, - &ChargedResources { vm_resources: validate_resources, gas_for_fee }, CallInfo::summarize_many( validate_call_info.iter(), &tx_context.block_context.versioned_constants, @@ -696,13 +669,6 @@ impl AccountTransaction { validate_state_changes, execution_state.get_actual_state_changes()?, ]), - &ChargedResources { - vm_resources: execution_resources, - gas_for_fee: gas_for_fee_from_call_infos( - &validate_call_info, - &execute_call_info, - ), - }, CallInfo::summarize_many( validate_call_info.iter().chain(execute_call_info.iter()), &tx_context.block_context.versioned_constants, @@ -904,7 +870,6 @@ impl ValidatableTransaction for AccountTransaction { fn validate_tx( &self, state: &mut dyn State, - resources: &mut ExecutionResources, tx_context: Arc, remaining_gas: &mut u64, limit_steps_by_resources: bool, @@ -933,7 +898,7 @@ impl ValidatableTransaction for AccountTransaction { // Note that we allow a revert here and we handle it bellow to get a better error message. let validate_call_info = validate_call - .execute(state, resources, &mut context, remaining_gas) + .execute(state, &mut context, remaining_gas) .map_err(|error| TransactionExecutionError::ValidateTransactionError { error, class_hash, diff --git a/crates/blockifier/src/transaction/objects_test.rs b/crates/blockifier/src/transaction/objects_test.rs index c1c339227c..d736adb89c 100644 --- a/crates/blockifier/src/transaction/objects_test.rs +++ b/crates/blockifier/src/transaction/objects_test.rs @@ -1,5 +1,6 @@ use rstest::rstest; use starknet_api::core::{ClassHash, ContractAddress, EthAddress}; +use starknet_api::execution_resources::GasAmount; use starknet_api::state::StorageKey; use starknet_api::transaction::L2ToL1Payload; use starknet_api::{class_hash, contract_address, storage_key}; @@ -8,6 +9,7 @@ use starknet_types_core::felt::Felt; use crate::execution::call_info::{ CallExecution, CallInfo, + ChargedResources, EventSummary, ExecutionSummary, MessageToL1, @@ -20,6 +22,7 @@ use crate::versioned_constants::VersionedConstants; #[derive(Debug, Default)] pub struct TestExecutionSummary { + pub gas_for_fee: GasAmount, pub num_of_events: usize, pub num_of_messages: usize, pub class_hash: ClassHash, @@ -29,6 +32,7 @@ pub struct TestExecutionSummary { impl TestExecutionSummary { pub fn new( + gas_for_fee: u64, num_of_events: usize, num_of_messages: usize, class_hash: ClassHash, @@ -36,6 +40,7 @@ impl TestExecutionSummary { storage_key: &str, ) -> Self { TestExecutionSummary { + gas_for_fee: GasAmount(gas_for_fee), num_of_events, num_of_messages, class_hash, @@ -64,6 +69,10 @@ impl TestExecutionSummary { .collect(), ..Default::default() }, + charged_resources: ChargedResources { + gas_for_fee: self.gas_for_fee, + ..Default::default() + }, accessed_storage_keys: vec![self.storage_key].into_iter().collect(), ..Default::default() } @@ -159,7 +168,7 @@ fn test_events_counter_in_tx_execution_info_with_inner_call_info(#[case] n_execu }; assert_eq!( - tx_execution_info.summarize(&VersionedConstants::latest_constants()).event_summary.n_events, + tx_execution_info.summarize(VersionedConstants::latest_constants()).event_summary.n_events, n_execute_events + n_fee_transfer_events + n_execution_events @@ -170,9 +179,9 @@ fn test_events_counter_in_tx_execution_info_with_inner_call_info(#[case] n_execu #[rstest] #[case( - TestExecutionSummary::new(1, 2, class_hash!("0x1"), "0x1", "0x1"), - TestExecutionSummary::new(2, 3, class_hash!("0x2"), "0x2", "0x2"), - TestExecutionSummary::new(3, 4, class_hash!("0x3"), "0x3", "0x3") + TestExecutionSummary::new(10, 1, 2, class_hash!("0x1"), "0x1", "0x1"), + TestExecutionSummary::new(20, 2, 3, class_hash!("0x2"), "0x2", "0x2"), + TestExecutionSummary::new(30, 3, 4, class_hash!("0x3"), "0x3", "0x3") )] fn test_summarize( #[case] validate_params: TestExecutionSummary, @@ -191,6 +200,12 @@ fn test_summarize( }; let expected_summary = ExecutionSummary { + charged_resources: ChargedResources { + gas_for_fee: validate_params.gas_for_fee + + execute_params.gas_for_fee + + fee_transfer_params.gas_for_fee, + ..Default::default() + }, executed_class_hashes: vec![ validate_params.class_hash, execute_params.class_hash, @@ -220,12 +235,9 @@ fn test_summarize( }, }; - // Call the summarize method - let actual_summary = tx_execution_info.summarize(&VersionedConstants::latest_constants()); + // Call the summarize method. + let actual_summary = tx_execution_info.summarize(VersionedConstants::latest_constants()); - // Compare the actual result with the expected result - assert_eq!(actual_summary.executed_class_hashes, expected_summary.executed_class_hashes); - assert_eq!(actual_summary.visited_storage_entries, expected_summary.visited_storage_entries); - assert_eq!(actual_summary.event_summary, expected_summary.event_summary); - assert_eq!(actual_summary.l2_to_l1_payload_lengths, expected_summary.l2_to_l1_payload_lengths); + // Compare the actual result with the expected result. + assert_eq!(actual_summary, expected_summary); } diff --git a/crates/blockifier/src/transaction/transaction_execution.rs b/crates/blockifier/src/transaction/transaction_execution.rs index dcf0cf0e41..2887c924b1 100644 --- a/crates/blockifier/src/transaction/transaction_execution.rs +++ b/crates/blockifier/src/transaction/transaction_execution.rs @@ -1,6 +1,5 @@ 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::{ @@ -15,7 +14,7 @@ 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::{gas_for_fee_from_call_infos, CallInfo, ChargedResources}; +use crate::execution::call_info::CallInfo; use crate::execution::entry_point::EntryPointExecutionContext; use crate::fee::receipt::TransactionReceipt; use crate::state::cached_state::TransactionalState; @@ -146,14 +145,11 @@ impl ExecutableTransaction for L1HandlerTransaction { ) -> TransactionExecutionResult { let tx_context = Arc::new(block_context.to_tx_context(self)); let limit_steps_by_resources = false; - let mut execution_resources = ExecutionResources::default(); let mut context = EntryPointExecutionContext::new_invoke(tx_context.clone(), limit_steps_by_resources); let mut remaining_gas = tx_context.initial_sierra_gas(); - let execute_call_info = - self.run_execute(state, &mut execution_resources, &mut context, &mut remaining_gas)?; + let execute_call_info = self.run_execute(state, &mut context, &mut remaining_gas)?; let l1_handler_payload_size = self.payload_size(); - let gas_for_fee = gas_for_fee_from_call_infos(&None, &execute_call_info); let TransactionReceipt { fee: actual_fee, da_gas, @@ -163,7 +159,6 @@ impl ExecutableTransaction for L1HandlerTransaction { &tx_context, l1_handler_payload_size, CallInfo::summarize_many(execute_call_info.iter(), &block_context.versioned_constants), - &ChargedResources { vm_resources: execution_resources, gas_for_fee }, &state.get_actual_state_changes()?, ); diff --git a/crates/blockifier/src/transaction/transactions.rs b/crates/blockifier/src/transaction/transactions.rs index 2fe3372b49..d570ebe6c4 100644 --- a/crates/blockifier/src/transaction/transactions.rs +++ b/crates/blockifier/src/transaction/transactions.rs @@ -1,6 +1,5 @@ use std::sync::Arc; -use cairo_vm::vm::runners::cairo_runner::ExecutionResources; use starknet_api::abi::abi_utils::selector_from_name; use starknet_api::contract_class::EntryPointType; use starknet_api::core::{ClassHash, CompiledClassHash, ContractAddress}; @@ -104,7 +103,6 @@ pub trait Executable { fn run_execute( &self, state: &mut S, - resources: &mut ExecutionResources, context: &mut EntryPointExecutionContext, remaining_gas: &mut u64, ) -> TransactionExecutionResult>; @@ -115,7 +113,6 @@ pub trait ValidatableTransaction { fn validate_tx( &self, state: &mut dyn State, - resources: &mut ExecutionResources, tx_context: Arc, remaining_gas: &mut u64, limit_steps_by_resources: bool, @@ -136,7 +133,6 @@ impl Executable for L1HandlerTransaction { fn run_execute( &self, state: &mut S, - resources: &mut ExecutionResources, context: &mut EntryPointExecutionContext, remaining_gas: &mut u64, ) -> TransactionExecutionResult> { @@ -156,15 +152,14 @@ impl Executable for L1HandlerTransaction { initial_gas: *remaining_gas, }; - execute_call - .non_reverting_execute(state, resources, context, remaining_gas) - .map(Some) - .map_err(|error| TransactionExecutionError::ExecutionError { + execute_call.non_reverting_execute(state, context, remaining_gas).map(Some).map_err( + |error| TransactionExecutionError::ExecutionError { error, class_hash, storage_address, selector, - }) + }, + ) } } @@ -188,7 +183,6 @@ impl Executable for DeclareTransaction { fn run_execute( &self, state: &mut S, - _resources: &mut ExecutionResources, context: &mut EntryPointExecutionContext, _remaining_gas: &mut u64, ) -> TransactionExecutionResult> { @@ -264,7 +258,6 @@ impl Executable for DeployAccountTransaction { fn run_execute( &self, state: &mut S, - resources: &mut ExecutionResources, context: &mut EntryPointExecutionContext, remaining_gas: &mut u64, ) -> TransactionExecutionResult> { @@ -277,7 +270,6 @@ impl Executable for DeployAccountTransaction { }; let call_info = execute_deployment( state, - resources, context, constructor_context, self.constructor_calldata(), @@ -325,7 +317,6 @@ impl Executable for InvokeTransaction { fn run_execute( &self, state: &mut S, - resources: &mut ExecutionResources, context: &mut EntryPointExecutionContext, remaining_gas: &mut u64, ) -> TransactionExecutionResult> { @@ -350,13 +341,14 @@ impl Executable for InvokeTransaction { initial_gas: *remaining_gas, }; - let call_info = execute_call - .non_reverting_execute(state, resources, context, remaining_gas) - .map_err(|error| TransactionExecutionError::ExecutionError { - error, - class_hash, - storage_address, - selector: entry_point_selector, + let call_info = + execute_call.non_reverting_execute(state, context, remaining_gas).map_err(|error| { + TransactionExecutionError::ExecutionError { + error, + class_hash, + storage_address, + selector: entry_point_selector, + } })?; Ok(Some(call_info)) } diff --git a/crates/papyrus_execution/src/lib.rs b/crates/papyrus_execution/src/lib.rs index 5941c9b4b5..4e7fada6bd 100644 --- a/crates/papyrus_execution/src/lib.rs +++ b/crates/papyrus_execution/src/lib.rs @@ -44,7 +44,6 @@ use blockifier::transaction::transactions::ExecutableTransaction; use blockifier::versioned_constants::{VersionedConstants, VersionedConstantsError}; use cairo_lang_starknet_classes::casm_contract_class::CasmContractClass; use cairo_vm::types::builtin_name::BuiltinName; -use cairo_vm::vm::runners::cairo_runner::ExecutionResources; use execution_utils::{get_trace_constructor, induced_state_diff}; use objects::{PriceUnit, TransactionSimulationOutput}; use papyrus_config::dumping::{ser_param, SerializeConfig}; @@ -272,12 +271,7 @@ pub fn execute_call( ); let res = call_entry_point - .execute( - &mut cached_state, - &mut ExecutionResources::default(), - &mut context, - &mut remaining_gas, - ) + .execute(&mut cached_state, &mut context, &mut remaining_gas) .map_err(|error| { if let Some(class_hash) = cached_state.state.missing_compiled_class.get() { ExecutionError::MissingCompiledClass { class_hash }