Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Commit

Permalink
feat(execution): add the syscall resources to vm_resources (#1412)
Browse files Browse the repository at this point in the history
  • Loading branch information
OriStarkware committed Feb 6, 2024
1 parent 0371f3c commit 4160542
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,11 @@ pub fn finalize_execution(
.get_execution_resources(&vm)
.map_err(VirtualMachineError::RunnerError)?
.filter_unused_builtins();
// TODO(Ori, 14/2/2024): Rename `vm_resources`.
syscall_handler.resources.vm_resources += &vm_resources_without_inner_calls;
let versioned_constants = syscall_handler.context.versioned_constants();
syscall_handler.resources.vm_resources += &versioned_constants
.get_additional_os_syscall_resources(&syscall_handler.syscall_counter)?;

let full_call_vm_resources = &syscall_handler.resources.vm_resources - &previous_vm_resources;
Ok(CallInfo {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,11 @@ fn test_nested_library_call() {
calldata: calldata![stark_felt!(key), stark_felt!(value)],
..nested_storage_entry_point
};
let storage_entry_point_vm_resources =
VmExecutionResources { n_steps: 42, ..Default::default() };
let storage_entry_point_vm_resources = VmExecutionResources {
n_steps: 218,
n_memory_holes: 0,
builtin_instance_counter: HashMap::from([(RANGE_CHECK_BUILTIN_NAME.to_string(), 2)]),
};
let nested_storage_call_info = CallInfo {
call: nested_storage_entry_point,
execution: CallExecution::from_retdata(retdata![stark_felt!(value + 1)]),
Expand All @@ -145,9 +148,9 @@ fn test_nested_library_call() {
..Default::default()
};
let mut library_call_vm_resources = VmExecutionResources {
n_steps: 39,
builtin_instance_counter: HashMap::from([(RANGE_CHECK_BUILTIN_NAME.to_string(), 1)]),
..Default::default()
n_steps: 790,
n_memory_holes: 4,
builtin_instance_counter: HashMap::from([(RANGE_CHECK_BUILTIN_NAME.to_string(), 21)]),
};
library_call_vm_resources += &storage_entry_point_vm_resources;
let library_call_info = CallInfo {
Expand All @@ -167,7 +170,11 @@ fn test_nested_library_call() {
};

// Nested library call cost: library_call(inner) + library_call(library_call(inner)).
let mut main_call_vm_resources = VmExecutionResources { n_steps: 45, ..Default::default() };
let mut main_call_vm_resources = VmExecutionResources {
n_steps: 796,
n_memory_holes: 4,
builtin_instance_counter: HashMap::from([(RANGE_CHECK_BUILTIN_NAME.to_string(), 20)]),
};
main_call_vm_resources += &(&library_call_vm_resources * 2);
let expected_call_info = CallInfo {
call: main_entry_point.clone(),
Expand Down Expand Up @@ -218,7 +225,11 @@ fn test_call_contract() {
..trivial_external_entry_point
},
execution: expected_execution.clone(),
vm_resources: VmExecutionResources { n_steps: 42, ..Default::default() },
vm_resources: VmExecutionResources {
n_steps: 218,
n_memory_holes: 0,
builtin_instance_counter: HashMap::from([(RANGE_CHECK_BUILTIN_NAME.to_string(), 2)]),
},
storage_read_values: vec![StarkFelt::ZERO, stark_felt!(value)],
accessed_storage_keys: HashSet::from([StorageKey(patricia_key!(key))]),
..Default::default()
Expand All @@ -235,9 +246,9 @@ fn test_call_contract() {
},
execution: expected_execution,
vm_resources: VmExecutionResources {
n_steps: 81,
builtin_instance_counter: HashMap::from([(RANGE_CHECK_BUILTIN_NAME.to_string(), 1)]),
..Default::default()
n_steps: 1017,
n_memory_holes: 4,
builtin_instance_counter: HashMap::from([(RANGE_CHECK_BUILTIN_NAME.to_string(), 23)]),
},
..Default::default()
};
Expand Down
4 changes: 4 additions & 0 deletions crates/blockifier/src/execution/entry_point_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,11 @@ pub fn finalize_execution(
.get_execution_resources(&vm)
.map_err(VirtualMachineError::RunnerError)?
.filter_unused_builtins();
// TODO(Ori, 14/2/2024): Rename `vm_resources`.
syscall_handler.resources.vm_resources += &vm_resources_without_inner_calls;
let versioned_constants = syscall_handler.context.versioned_constants();
syscall_handler.resources.vm_resources += &versioned_constants
.get_additional_os_syscall_resources(&syscall_handler.syscall_counter)?;

let full_call_vm_resources = &syscall_handler.resources.vm_resources - &previous_vm_resources;
Ok(CallInfo {
Expand Down
16 changes: 8 additions & 8 deletions crates/blockifier/src/execution/syscalls/syscalls_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -542,9 +542,9 @@ fn test_nested_library_call() {
..nested_storage_entry_point
};
let storage_entry_point_vm_resources = VmExecutionResources {
n_steps: 143,
n_steps: 319,
n_memory_holes: 1,
builtin_instance_counter: HashMap::from([(RANGE_CHECK_BUILTIN_NAME.to_string(), 5)]),
builtin_instance_counter: HashMap::from([(RANGE_CHECK_BUILTIN_NAME.to_string(), 7)]),
};
let nested_storage_call_info = CallInfo {
call: nested_storage_entry_point,
Expand All @@ -559,9 +559,9 @@ fn test_nested_library_call() {
..Default::default()
};
let library_call_vm_resources = VmExecutionResources {
n_steps: 411,
n_memory_holes: 2,
builtin_instance_counter: HashMap::from([(RANGE_CHECK_BUILTIN_NAME.to_string(), 13)]),
n_steps: 1338,
n_memory_holes: 6,
builtin_instance_counter: HashMap::from([(RANGE_CHECK_BUILTIN_NAME.to_string(), 35)]),
};
let library_call_info = CallInfo {
call: library_entry_point,
Expand All @@ -588,9 +588,9 @@ fn test_nested_library_call() {
};

let main_call_vm_resources = VmExecutionResources {
n_steps: 765,
n_memory_holes: 4,
builtin_instance_counter: HashMap::from([(RANGE_CHECK_BUILTIN_NAME.to_string(), 23)]),
n_steps: 3370,
n_memory_holes: 16,
builtin_instance_counter: HashMap::from([(RANGE_CHECK_BUILTIN_NAME.to_string(), 87)]),
};
let expected_call_info = CallInfo {
call: main_entry_point.clone(),
Expand Down
6 changes: 2 additions & 4 deletions crates/blockifier/src/transaction/transaction_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@ pub fn calculate_tx_resources(
let l1_blob_gas_usage = usize_from_u128(gas_vector.blob_gas)
.expect("This conversion should not fail as the value is a converted usize.");
// Add additional Cairo resources needed for the OS to run the transaction.
let vm_and_syscall_usage = &execution_resources.vm_resources
+ &versioned_constants.get_additional_os_syscall_resources(&execution_resources.syscall_counter)?;
let total_vm_usage =
&vm_and_syscall_usage + &versioned_constants.get_additional_os_tx_resources(tx_type, calldata_length)?;
let total_vm_usage = &execution_resources.vm_resources
+ &versioned_constants.get_additional_os_tx_resources(tx_type, calldata_length)?;
let mut total_vm_usage = total_vm_usage.filter_unused_builtins();
// The segment arena" builtin is not part of SHARP (not in any proof layout).
// Each instance requires approximately 10 steps in the OS.
Expand Down
16 changes: 8 additions & 8 deletions crates/blockifier/src/transaction/transactions_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,9 +309,9 @@ fn default_invoke_tx_args(
range_check: 102,
n_steps: 4496,
vm_resources: VmExecutionResources {
n_steps: 62,
n_memory_holes: 0,
builtin_instance_counter: HashMap::from([(RANGE_CHECK_BUILTIN_NAME.to_string(), 1)]),
n_steps: 822,
n_memory_holes: 4,
builtin_instance_counter: HashMap::from([(RANGE_CHECK_BUILTIN_NAME.to_string(), 21)]),
},
validate_gas_consumed: 0,
execute_gas_consumed: 0,
Expand All @@ -323,9 +323,9 @@ fn default_invoke_tx_args(
range_check: 115,
n_steps: 4949,
vm_resources: VmExecutionResources {
n_steps: 284,
n_memory_holes: 1,
builtin_instance_counter: HashMap::from([(RANGE_CHECK_BUILTIN_NAME.to_string(), 7)]),
n_steps: 1106,
n_memory_holes: 5,
builtin_instance_counter: HashMap::from([(RANGE_CHECK_BUILTIN_NAME.to_string(), 28)]),
},
validate_gas_consumed: 14360, // The gas consumption results from parsing the input
// arguments.
Expand Down Expand Up @@ -1701,9 +1701,9 @@ fn test_l1_handler(#[values(false, true)] use_kzg_da: bool) {
..Default::default()
},
vm_resources: VmExecutionResources {
n_steps: 143,
n_steps: 232,
n_memory_holes: 1,
builtin_instance_counter: HashMap::from([(RANGE_CHECK_BUILTIN_NAME.to_string(), 5)]),
builtin_instance_counter: HashMap::from([(RANGE_CHECK_BUILTIN_NAME.to_string(), 6)]),
},
accessed_storage_keys: HashSet::from_iter(vec![accessed_storage_key]),
..Default::default()
Expand Down
5 changes: 3 additions & 2 deletions crates/blockifier/src/versioned_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use thiserror::Error;

use crate::execution::deprecated_syscalls::hint_processor::SyscallCounter;
use crate::execution::deprecated_syscalls::DeprecatedSyscallSelector;
use crate::execution::errors::PostExecutionError;
use crate::transaction::errors::TransactionExecutionError;
use crate::transaction::transaction_types::TransactionType;

Expand Down Expand Up @@ -114,7 +115,7 @@ impl VersionedConstants {
pub fn get_additional_os_syscall_resources(
&self,
syscall_counter: &SyscallCounter,
) -> Result<VmExecutionResources, TransactionExecutionError> {
) -> Result<VmExecutionResources, PostExecutionError> {
self.os_resources.get_additional_os_syscall_resources(syscall_counter)
}

Expand Down Expand Up @@ -178,7 +179,7 @@ impl OsResources {
pub fn get_additional_os_syscall_resources(
&self,
syscall_counter: &SyscallCounter,
) -> Result<VmExecutionResources, TransactionExecutionError> {
) -> Result<VmExecutionResources, PostExecutionError> {
let mut os_additional_vm_resources = VmExecutionResources::default();
for (syscall_selector, count) in syscall_counter {
let syscall_resources =
Expand Down

0 comments on commit 4160542

Please sign in to comment.