diff --git a/crates/blockifier/src/execution/call_info.rs b/crates/blockifier/src/execution/call_info.rs index 948699544d..1b61ee54db 100644 --- a/crates/blockifier/src/execution/call_info.rs +++ b/crates/blockifier/src/execution/call_info.rs @@ -212,8 +212,8 @@ impl CallInfo { } ExecutionSummary { - // Note: the charged resourses of a call contains the inner call resources, unlike other - // fields such as events and messages, + // Note: the charged resources 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, @@ -229,13 +229,13 @@ impl CallInfo { call_infos.map(|call_info| call_info.summarize(versioned_constants)).sum() } - pub fn summarize_charged_resources<'a>( + pub fn summarize_vm_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; + ) -> ExecutionResources { + // Note: the vm resources (and entire charged resources) of a call contains the inner call + // resources, unlike other fields such as events and messages. + call_infos.fold(ExecutionResources::default(), |mut acc, inner_call| { + acc += &inner_call.charged_resources.vm_resources; acc }) } diff --git a/crates/blockifier/src/execution/deprecated_entry_point_execution.rs b/crates/blockifier/src/execution/deprecated_entry_point_execution.rs index bee02bedd5..efe78e0905 100644 --- a/crates/blockifier/src/execution/deprecated_entry_point_execution.rs +++ b/crates/blockifier/src/execution/deprecated_entry_point_execution.rs @@ -254,12 +254,9 @@ pub fn finalize_execution( vm_resources_without_inner_calls += &versioned_constants.get_additional_os_syscall_resources(&syscall_handler.syscall_counter); - 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()); + let vm_resources = &vm_resources_without_inner_calls + + &CallInfo::summarize_vm_resources(syscall_handler.inner_calls.iter()); + let charged_resources = ChargedResources { vm_resources, gas_for_fee: GasAmount(0) }; Ok(CallInfo { call, diff --git a/crates/blockifier/src/execution/entry_point_execution.rs b/crates/blockifier/src/execution/entry_point_execution.rs index 3a814f3835..7f2c352b31 100644 --- a/crates/blockifier/src/execution/entry_point_execution.rs +++ b/crates/blockifier/src/execution/entry_point_execution.rs @@ -324,20 +324,6 @@ fn maybe_fill_holes( Ok(()) } -/// Calculates the gas consumed in the current call. -pub fn gas_consumed_without_inner_calls( - tracked_resource: &TrackedResource, - gas_consumed: u64, - inner_calls: &[CallInfo], -) -> GasAmount { - GasAmount(match tracked_resource { - TrackedResource::CairoSteps => 0, - TrackedResource::SierraGas => gas_consumed - .checked_sub(inner_calls.iter().map(|call| call.execution.gas_consumed).sum::()) - .expect("gas_consumed unexpectedly underflowed."), - }) -} - pub fn finalize_execution( mut runner: CairoRunner, mut syscall_handler: SyscallHintProcessor<'_>, @@ -387,18 +373,10 @@ pub fn finalize_execution( syscall_handler.finalize(); - let charged_resources_without_inner_calls = ChargedResources { - vm_resources: vm_resources_without_inner_calls, - gas_for_fee: gas_consumed_without_inner_calls( - &tracked_resource, - call_result.gas_consumed, - &syscall_handler.base.inner_calls, - ), - }; - - let charged_resources = &charged_resources_without_inner_calls - + &CallInfo::summarize_charged_resources(syscall_handler.base.inner_calls.iter()); - + let vm_resources = &vm_resources_without_inner_calls + + &CallInfo::summarize_vm_resources(syscall_handler.base.inner_calls.iter()); + let charged_resources = + ChargedResources { vm_resources, gas_for_fee: GasAmount(call_result.gas_consumed) }; let syscall_handler_base = syscall_handler.base; Ok(CallInfo { call: syscall_handler_base.call, diff --git a/crates/blockifier/src/execution/entry_point_execution_test.rs b/crates/blockifier/src/execution/entry_point_execution_test.rs index 2fe0d29542..461df39f2f 100644 --- a/crates/blockifier/src/execution/entry_point_execution_test.rs +++ b/crates/blockifier/src/execution/entry_point_execution_test.rs @@ -7,10 +7,9 @@ use starknet_api::execution_resources::GasAmount; use starknet_api::transaction::fields::Calldata; use crate::context::ChainInfo; -use crate::execution::call_info::{CallExecution, CallInfo, ChargedResources}; +use crate::execution::call_info::{CallInfo, ChargedResources}; use crate::execution::contract_class::TrackedResource; use crate::execution::entry_point::CallEntryPoint; -use crate::execution::entry_point_execution::gas_consumed_without_inner_calls; use crate::test_utils::contracts::FeatureContract; use crate::test_utils::initial_test_state::test_state; use crate::test_utils::syscall::build_recurse_calldata; @@ -22,59 +21,6 @@ use crate::test_utils::{ BALANCE, }; -#[test] -/// Verifies that every call from the inner most to the outer has the expected gas_for_fee for the -/// following topology (marked as TrackedResource(gas_consumed)): -// Gas(8) -> Gas(3) -> VM(2) -> VM(1) -// \ -> VM(4) -// Expected values are 1 -> 1 -> 0 -> 0. -// \-> 0. -fn test_gas_for_fee() { - // First branch - 3 nested calls. - let mut inner_calls = vec![]; - for (tracked_resource, gas_consumed, expected_gas_for_fee) in [ - (TrackedResource::CairoSteps, 1, 0), - (TrackedResource::CairoSteps, 2, 0), - (TrackedResource::SierraGas, 3, 1), - ] { - assert_eq!( - gas_consumed_without_inner_calls(&tracked_resource, gas_consumed, &inner_calls).0, - expected_gas_for_fee - ); - inner_calls = vec![CallInfo { - execution: CallExecution { gas_consumed, ..Default::default() }, - tracked_resource, - inner_calls, - charged_resources: ChargedResources { - gas_for_fee: GasAmount(expected_gas_for_fee), - ..Default::default() - }, - ..Default::default() - }]; - } - - // Second branch - 1 call. - let (tracked_resource, gas_consumed, expected_gas_for_fee) = - (TrackedResource::CairoSteps, 4, 0); - assert_eq!( - gas_consumed_without_inner_calls(&tracked_resource, gas_consumed, &[]).0, - expected_gas_for_fee - ); - - inner_calls.push(CallInfo { - execution: CallExecution { gas_consumed, ..Default::default() }, - tracked_resource, - charged_resources: ChargedResources { - gas_for_fee: GasAmount(expected_gas_for_fee), - ..Default::default() - }, - ..Default::default() - }); - - // Outer call. - assert_eq!(gas_consumed_without_inner_calls(&TrackedResource::SierraGas, 8, &inner_calls).0, 1); -} - /// Asserts that the charged resources of a call is consistent with the inner calls in its subtree. fn assert_charged_resource_as_expected_rec(call_info: &CallInfo) { let inner_calls = &call_info.inner_calls; diff --git a/crates/blockifier/src/execution/native/entry_point_execution.rs b/crates/blockifier/src/execution/native/entry_point_execution.rs index 811314022b..035a3520d9 100644 --- a/crates/blockifier/src/execution/native/entry_point_execution.rs +++ b/crates/blockifier/src/execution/native/entry_point_execution.rs @@ -1,8 +1,8 @@ use cairo_native::execution_result::ContractExecutionResult; use cairo_native::utils::BuiltinCosts; -use cairo_vm::vm::runners::cairo_runner::ExecutionResources; use num_rational::Ratio; use stacker; +use starknet_api::execution_resources::GasAmount; use crate::execution::call_info::{CallExecution, CallInfo, ChargedResources, Retdata}; use crate::execution::contract_class::TrackedResource; @@ -11,7 +11,6 @@ use crate::execution::entry_point::{ EntryPointExecutionContext, EntryPointExecutionResult, }; -use crate::execution::entry_point_execution::gas_consumed_without_inner_calls; use crate::execution::errors::{EntryPointExecutionError, PostExecutionError, PreExecutionError}; use crate::execution::native::contract_class::NativeCompiledClassV1; use crate::execution::native::syscall_handler::NativeSyscallHandler; @@ -120,17 +119,8 @@ fn create_callinfo( } let gas_consumed = syscall_handler.base.call.initial_gas - remaining_gas; - - let charged_resources_without_inner_calls = ChargedResources { - vm_resources: ExecutionResources::default(), - gas_for_fee: gas_consumed_without_inner_calls( - &TrackedResource::SierraGas, - gas_consumed, - &syscall_handler.base.inner_calls, - ), - }; - let charged_resources = &charged_resources_without_inner_calls - + &CallInfo::summarize_charged_resources(syscall_handler.base.inner_calls.iter()); + let vm_resources = CallInfo::summarize_vm_resources(syscall_handler.base.inner_calls.iter()); + let charged_resources = ChargedResources { vm_resources, gas_for_fee: GasAmount(gas_consumed) }; Ok(CallInfo { call: syscall_handler.base.call,