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): define ChargedResources in CallInfo #1722

Merged
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
19 changes: 18 additions & 1 deletion crates/blockifier/src/execution/call_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::ops::Add;
use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
use serde::Serialize;
use starknet_api::core::{ClassHash, EthAddress};
use starknet_api::execution_resources::GasAmount;
use starknet_api::state::StorageKey;
use starknet_api::transaction::{EventContent, L2ToL1Payload};
use starknet_types_core::felt::Felt;
Expand Down Expand Up @@ -95,16 +96,32 @@ impl Sum for ExecutionSummary {
}
}

/// L2 resources counted for fee charge.
/// When all execution will be using gas (no VM mode), this should be removed, and the gas_consumed
/// field should be used for fee collection.
#[cfg_attr(feature = "transaction_serde", derive(serde::Deserialize))]
#[derive(Clone, Debug, Default, Serialize, Eq, PartialEq)]
pub struct ChargedResources {
pub vm_resources: ExecutionResources, // Counted in CairoSteps mode calls.
pub gas_for_fee: GasAmount, // Counted in SierraGas mode calls.
}

impl ChargedResources {
pub fn from_execution_resources(resources: ExecutionResources) -> Self {
Self { vm_resources: resources, ..Default::default() }
}
}

/// Represents the full effects of executing an entry point, including the inner calls it invoked.
#[cfg_attr(any(test, feature = "testing"), derive(Clone))]
#[cfg_attr(feature = "transaction_serde", derive(serde::Deserialize))]
#[derive(Debug, Default, Eq, PartialEq, Serialize)]
pub struct CallInfo {
pub call: CallEntryPoint,
pub execution: CallExecution,
pub resources: ExecutionResources,
pub inner_calls: Vec<CallInfo>,
pub tracked_resource: TrackedResource,
pub charged_resources: ChargedResources,

// Additional information gathered during execution.
pub storage_read_values: Vec<Felt>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use starknet_api::hash::StarkHash;
use super::execution_utils::SEGMENT_ARENA_BUILTIN_SIZE;
use crate::abi::abi_utils::selector_from_name;
use crate::abi::constants::{CONSTRUCTOR_ENTRY_POINT_NAME, DEFAULT_ENTRY_POINT_SELECTOR};
use crate::execution::call_info::{CallExecution, CallInfo};
use crate::execution::call_info::{CallExecution, CallInfo, ChargedResources};
use crate::execution::contract_class::{ContractClassV0, TrackedResource};
use crate::execution::deprecated_syscalls::hint_processor::DeprecatedSyscallHintProcessor;
use crate::execution::entry_point::{
Expand Down Expand Up @@ -278,9 +278,11 @@ pub fn finalize_execution(
failed: false,
gas_consumed: 0,
},
resources: full_call_resources.filter_unused_builtins(),
inner_calls: syscall_handler.inner_calls,
tracked_resource: TrackedResource::CairoSteps,
charged_resources: ChargedResources::from_execution_resources(
full_call_resources.filter_unused_builtins(),
),
storage_read_values: syscall_handler.read_values,
accessed_storage_keys: syscall_handler.accessed_keys,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use test_case::test_case;

use crate::abi::abi_utils::selector_from_name;
use crate::context::ChainInfo;
use crate::execution::call_info::{CallExecution, CallInfo, OrderedEvent};
use crate::execution::call_info::{CallExecution, CallInfo, ChargedResources, OrderedEvent};
use crate::execution::common_hints::ExecutionMode;
use crate::execution::deprecated_syscalls::DeprecatedSyscallSelector;
use crate::execution::entry_point::{CallEntryPoint, CallType};
Expand Down Expand Up @@ -157,7 +157,9 @@ fn test_nested_library_call() {
let nested_storage_call_info = CallInfo {
call: nested_storage_entry_point,
execution: CallExecution::from_retdata(retdata![felt!(value + 1)]),
resources: storage_entry_point_resources.clone(),
charged_resources: ChargedResources::from_execution_resources(
storage_entry_point_resources.clone(),
),
storage_read_values: vec![felt!(value + 1)],
accessed_storage_keys: HashSet::from([storage_key!(key + 1)]),
..Default::default()
Expand All @@ -172,14 +174,18 @@ fn test_nested_library_call() {
let library_call_info = CallInfo {
call: library_entry_point,
execution: CallExecution::from_retdata(retdata![felt!(value + 1)]),
resources: library_call_resources.clone(),
charged_resources: ChargedResources::from_execution_resources(
library_call_resources.clone(),
),
inner_calls: vec![nested_storage_call_info],
..Default::default()
};
let storage_call_info = CallInfo {
call: storage_entry_point,
execution: CallExecution::from_retdata(retdata![felt!(value)]),
resources: storage_entry_point_resources.clone(),
charged_resources: ChargedResources::from_execution_resources(
storage_entry_point_resources.clone(),
),
storage_read_values: vec![felt!(value)],
accessed_storage_keys: HashSet::from([storage_key!(key)]),
..Default::default()
Expand All @@ -196,7 +202,7 @@ fn test_nested_library_call() {
let expected_call_info = CallInfo {
call: main_entry_point.clone(),
execution: CallExecution::from_retdata(retdata![felt!(0_u8)]),
resources: main_call_resources,
charged_resources: ChargedResources::from_execution_resources(main_call_resources),
inner_calls: vec![library_call_info, storage_call_info],
..Default::default()
};
Expand Down Expand Up @@ -241,11 +247,11 @@ fn test_call_contract() {
..trivial_external_entry_point
},
execution: expected_execution.clone(),
resources: ExecutionResources {
charged_resources: ChargedResources::from_execution_resources(ExecutionResources {
n_steps: 222,
n_memory_holes: 0,
builtin_instance_counter: HashMap::from([(BuiltinName::range_check, 2)]),
},
}),
storage_read_values: vec![value],
accessed_storage_keys: HashSet::from([storage_key!(key_int)]),
..Default::default()
Expand All @@ -259,12 +265,14 @@ fn test_call_contract() {
..trivial_external_entry_point
},
execution: expected_execution,
resources: &get_syscall_resources(DeprecatedSyscallSelector::CallContract)
+ &ExecutionResources {
n_steps: 261,
n_memory_holes: 0,
builtin_instance_counter: HashMap::from([(BuiltinName::range_check, 3)]),
},
charged_resources: ChargedResources::from_execution_resources(
&get_syscall_resources(DeprecatedSyscallSelector::CallContract)
+ &ExecutionResources {
n_steps: 261,
n_memory_holes: 0,
builtin_instance_counter: HashMap::from([(BuiltinName::range_check, 3)]),
},
),
..Default::default()
};

Expand Down
4 changes: 2 additions & 2 deletions crates/blockifier/src/execution/entry_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ impl EntryPointExecutionContext {
}

/// From the total amount of steps available for execution, deduct the steps consumed during
/// validation and the overhead steps required for fee transfer.
/// validation and the overhead steps required, among the rest, for fee transfer.
/// Returns the remaining steps (after the subtraction).
pub fn subtract_validation_and_overhead_steps(
&mut self,
Expand All @@ -370,7 +370,7 @@ impl EntryPointExecutionContext {
) -> usize {
let validate_steps = validate_call_info
.as_ref()
.map(|call_info| call_info.resources.n_steps)
.map(|call_info| call_info.charged_resources.vm_resources.n_steps)
.unwrap_or_default();

let overhead_steps =
Expand Down
10 changes: 8 additions & 2 deletions crates/blockifier/src/execution/entry_point_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ use cairo_vm::vm::runners::builtin_runner::BuiltinRunner;
use cairo_vm::vm::runners::cairo_runner::{CairoArg, CairoRunner, ExecutionResources};
use cairo_vm::vm::security::verify_secure_runner;
use num_traits::{ToPrimitive, Zero};
use starknet_api::execution_resources::GasAmount;
use starknet_types_core::felt::Felt;

use crate::execution::call_info::{CallExecution, CallInfo, Retdata};
use crate::execution::call_info::{CallExecution, CallInfo, ChargedResources, Retdata};
use crate::execution::contract_class::{ContractClassV1, EntryPointV1, TrackedResource};
use crate::execution::entry_point::{
CallEntryPoint,
Expand Down Expand Up @@ -408,6 +409,11 @@ pub fn finalize_execution(
syscall_handler.finalize();

let full_call_resources = &*syscall_handler.resources - &previous_resources;
let charged_resources = ChargedResources {
vm_resources: full_call_resources.filter_unused_builtins(),
// TODO(tzahi): Replace with a computed value.
gas_for_fee: GasAmount(0),
};
Ok(CallInfo {
call: syscall_handler.call,
execution: CallExecution {
Expand All @@ -417,9 +423,9 @@ pub fn finalize_execution(
failed: call_result.failed,
gas_consumed: call_result.gas_consumed,
},
resources: full_call_resources.filter_unused_builtins(),
inner_calls: syscall_handler.inner_calls,
tracked_resource,
charged_resources,
storage_read_values: syscall_handler.read_values,
accessed_storage_keys: syscall_handler.accessed_keys,
})
Expand Down
8 changes: 6 additions & 2 deletions crates/blockifier/src/execution/entry_point_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -525,8 +525,12 @@ fn test_cairo1_entry_point_segment_arena() {
};

assert_eq!(
entry_point_call.execute_directly(&mut state).unwrap().resources.builtin_instance_counter
[&BuiltinName::segment_arena],
entry_point_call
.execute_directly(&mut state)
.unwrap()
.charged_resources
.vm_resources
.builtin_instance_counter[&BuiltinName::segment_arena],
// Note: the number of segment_arena instances should not depend on the compiler or VM
// version. Do not manually fix this then when upgrading them - it might be a bug.
2
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use cairo_native::execution_result::ContractExecutionResult;
use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
use num_traits::ToPrimitive;
use starknet_api::execution_resources::GasAmount;

use crate::execution::call_info::{CallExecution, CallInfo, Retdata};
use crate::execution::call_info::{CallExecution, CallInfo, ChargedResources, Retdata};
use crate::execution::contract_class::TrackedResource;
use crate::execution::entry_point::{
CallEntryPoint,
Expand Down Expand Up @@ -72,7 +73,10 @@ fn create_callinfo(
},
// todo(rodrigo): execution resources rely heavily on how the VM work, therefore
// the dummy values
resources: ExecutionResources::default(),
charged_resources: ChargedResources {
vm_resources: ExecutionResources::default(),
gas_for_fee: GasAmount(gas_consumed),
},
inner_calls: syscall_handler.inner_calls,
storage_read_values: syscall_handler.read_values,
accessed_storage_keys: syscall_handler.accessed_keys,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use test_case::test_case;

use crate::abi::abi_utils::selector_from_name;
use crate::context::ChainInfo;
use crate::execution::call_info::{CallExecution, CallInfo};
use crate::execution::call_info::{CallExecution, CallInfo, ChargedResources};
use crate::execution::entry_point::{CallEntryPoint, CallType};
use crate::execution::syscalls::syscall_tests::constants::{
REQUIRED_GAS_LIBRARY_CALL_TEST,
Expand Down Expand Up @@ -158,7 +158,9 @@ fn test_nested_library_call(test_contract: FeatureContract, expected_gas: u64) {
gas_consumed: REQUIRED_GAS_STORAGE_READ_WRITE_TEST,
..CallExecution::default()
},
resources: storage_entry_point_resources.clone(),
charged_resources: ChargedResources::from_execution_resources(
storage_entry_point_resources.clone(),
),
tracked_resource,
storage_read_values: vec![felt!(value + 1)],
accessed_storage_keys: HashSet::from([storage_key!(key + 1)]),
Expand All @@ -179,7 +181,7 @@ fn test_nested_library_call(test_contract: FeatureContract, expected_gas: u64) {
gas_consumed: REQUIRED_GAS_LIBRARY_CALL_TEST,
..CallExecution::default()
},
resources: library_call_resources,
charged_resources: ChargedResources::from_execution_resources(library_call_resources),
inner_calls: vec![nested_storage_call_info],
tracked_resource,
..Default::default()
Expand All @@ -192,7 +194,9 @@ fn test_nested_library_call(test_contract: FeatureContract, expected_gas: u64) {
gas_consumed: REQUIRED_GAS_STORAGE_READ_WRITE_TEST,
..CallExecution::default()
},
resources: storage_entry_point_resources,
charged_resources: ChargedResources::from_execution_resources(
storage_entry_point_resources,
),
storage_read_values: vec![felt!(value)],
accessed_storage_keys: HashSet::from([storage_key!(key)]),
tracked_resource,
Expand All @@ -212,7 +216,7 @@ fn test_nested_library_call(test_contract: FeatureContract, expected_gas: u64) {
gas_consumed: expected_gas,
..CallExecution::default()
},
resources: main_call_resources,
charged_resources: ChargedResources::from_execution_resources(main_call_resources),
inner_calls: vec![library_call_info, storage_call_info],
tracked_resource,
..Default::default()
Expand Down
3 changes: 2 additions & 1 deletion crates/blockifier/src/test_utils/prices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,6 @@ fn fee_transfer_resources(
&mut remaining_gas,
)
.unwrap()
.resources
.charged_resources
.vm_resources
}
21 changes: 14 additions & 7 deletions crates/blockifier/src/transaction/transactions_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ use crate::context::{BlockContext, ChainInfo, FeeTokenAddresses, TransactionCont
use crate::execution::call_info::{
CallExecution,
CallInfo,
ChargedResources,
ExecutionSummary,
MessageToL1,
OrderedEvent,
Expand Down Expand Up @@ -237,7 +238,7 @@ fn expected_validate_call_info(
initial_gas,
},
// The account contract we use for testing has trivial `validate` functions.
resources,
charged_resources: ChargedResources::from_execution_resources(resources),
execution: CallExecution { retdata, gas_consumed, ..Default::default() },
tracked_resource,
..Default::default()
Expand Down Expand Up @@ -305,7 +306,9 @@ fn expected_fee_transfer_call_info(
events: vec![expected_fee_transfer_event],
..Default::default()
},
resources: Prices::FeeTransfer(account_address, *fee_type).into(),
charged_resources: ChargedResources::from_execution_resources(
Prices::FeeTransfer(account_address, *fee_type).into(),
),
// We read sender and recipient balance - Uint256(BALANCE, 0) then Uint256(0, 0).
storage_read_values: vec![felt!(BALANCE.0), felt!(0_u8), felt!(0_u8), felt!(0_u8)],
accessed_storage_keys: HashSet::from_iter(vec![
Expand All @@ -328,7 +331,7 @@ fn get_expected_cairo_resources(
versioned_constants.get_additional_os_tx_resources(tx_type, starknet_resources, false);
for call_info in call_infos {
if let Some(call_info) = &call_info {
expected_cairo_resources += &call_info.resources
expected_cairo_resources += &call_info.charged_resources.vm_resources
};
}

Expand Down Expand Up @@ -513,11 +516,15 @@ fn test_invoke_tx(
gas_consumed: expected_arguments.execute_gas_consumed,
..Default::default()
},
resources: expected_arguments.resources,
charged_resources: ChargedResources::from_execution_resources(expected_arguments.resources),
inner_calls: vec![CallInfo {
call: expected_return_result_call,
execution: CallExecution::from_retdata(expected_return_result_retdata),
resources: ExecutionResources { n_steps: 23, n_memory_holes: 0, ..Default::default() },
charged_resources: ChargedResources::from_execution_resources(ExecutionResources {
n_steps: 23,
n_memory_holes: 0,
..Default::default()
}),
..Default::default()
}],
tracked_resource,
Expand Down Expand Up @@ -2246,11 +2253,11 @@ fn test_l1_handler(#[values(false, true)] use_kzg_da: bool) {
gas_consumed: 6820,
..Default::default()
},
resources: ExecutionResources {
charged_resources: ChargedResources::from_execution_resources(ExecutionResources {
n_steps: 158,
n_memory_holes: 0,
builtin_instance_counter: HashMap::from([(BuiltinName::range_check, 6)]),
},
}),
accessed_storage_keys: HashSet::from_iter(vec![accessed_storage_key]),
tracked_resource: test_contract
.get_runnable_class()
Expand Down
2 changes: 1 addition & 1 deletion crates/papyrus_execution/src/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ impl TryFrom<(CallInfo, GasVector)> for FunctionInvocation {
})
.collect(),
execution_resources: vm_resources_to_execution_resources(
call_info.resources,
call_info.charged_resources.vm_resources,
gas_vector,
)?,
})
Expand Down
Loading