Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(blockifier): take gas_consumed for CallInfo directly from CallResult #3110

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions crates/blockifier/src/execution/call_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<Item = &'a CallInfo>,
) -> 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
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
30 changes: 4 additions & 26 deletions crates/blockifier/src/execution/entry_point_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<u64>())
.expect("gas_consumed unexpectedly underflowed."),
})
}

pub fn finalize_execution(
mut runner: CairoRunner,
mut syscall_handler: SyscallHintProcessor<'_>,
Expand Down Expand Up @@ -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,
Expand Down
56 changes: 1 addition & 55 deletions crates/blockifier/src/execution/entry_point_execution_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
16 changes: 3 additions & 13 deletions crates/blockifier/src/execution/native/entry_point_execution.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
Loading