From 9d77c75a09a0f06918b3afabbca4862e90a60861 Mon Sep 17 00:00:00 2001 From: avi-starkware Date: Tue, 16 Jul 2024 16:24:43 +0300 Subject: [PATCH] fix: offset segment arena instance counter for older vm versions (#2090) --- crates/blockifier/resources/versioned_constants.json | 1 + .../resources/versioned_constants_13_0.json | 1 + .../resources/versioned_constants_13_1.json | 1 + .../resources/versioned_constants_13_1_1.json | 1 + .../execution/deprecated_entry_point_execution.rs | 12 ++++++++++-- .../src/execution/entry_point_execution.rs | 11 +++++++++-- crates/blockifier/src/execution/execution_utils.rs | 2 ++ crates/blockifier/src/versioned_constants.rs | 5 +++++ 8 files changed, 30 insertions(+), 4 deletions(-) diff --git a/crates/blockifier/resources/versioned_constants.json b/crates/blockifier/resources/versioned_constants.json index 0ec062c975..46cfab6338 100644 --- a/crates/blockifier/resources/versioned_constants.json +++ b/crates/blockifier/resources/versioned_constants.json @@ -25,6 +25,7 @@ }, "disable_cairo0_redeclaration": true, "max_recursion_depth": 50, + "count_segment_arena_cells_not_instances": false, "os_constants": { "block_hash_contract_address": 1, "call_contract_gas_cost": { diff --git a/crates/blockifier/resources/versioned_constants_13_0.json b/crates/blockifier/resources/versioned_constants_13_0.json index 1531f6fc13..ff8f797307 100644 --- a/crates/blockifier/resources/versioned_constants_13_0.json +++ b/crates/blockifier/resources/versioned_constants_13_0.json @@ -5,6 +5,7 @@ }, "invoke_tx_max_n_steps": 3000000, "max_recursion_depth": 50, + "count_segment_arena_cells_not_instances": true, "os_constants": { "nop_entry_point_offset": -1, "entry_point_type_external": 0, diff --git a/crates/blockifier/resources/versioned_constants_13_1.json b/crates/blockifier/resources/versioned_constants_13_1.json index 4e28f7b711..0b8de5265d 100644 --- a/crates/blockifier/resources/versioned_constants_13_1.json +++ b/crates/blockifier/resources/versioned_constants_13_1.json @@ -24,6 +24,7 @@ ] }, "max_recursion_depth": 50, + "count_segment_arena_cells_not_instances": true, "os_constants": { "nop_entry_point_offset": -1, "entry_point_type_external": 0, diff --git a/crates/blockifier/resources/versioned_constants_13_1_1.json b/crates/blockifier/resources/versioned_constants_13_1_1.json index 791dac8b9b..48832c863d 100644 --- a/crates/blockifier/resources/versioned_constants_13_1_1.json +++ b/crates/blockifier/resources/versioned_constants_13_1_1.json @@ -24,6 +24,7 @@ ] }, "max_recursion_depth": 50, + "count_segment_arena_cells_not_instances": true, "os_constants": { "nop_entry_point_offset": -1, "entry_point_type_external": 0, diff --git a/crates/blockifier/src/execution/deprecated_entry_point_execution.rs b/crates/blockifier/src/execution/deprecated_entry_point_execution.rs index 73b2c7b855..7442918ca4 100644 --- a/crates/blockifier/src/execution/deprecated_entry_point_execution.rs +++ b/crates/blockifier/src/execution/deprecated_entry_point_execution.rs @@ -1,3 +1,4 @@ +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; @@ -6,6 +7,7 @@ use starknet_api::core::EntryPointSelector; use starknet_api::deprecated_contract_class::EntryPointType; 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}; @@ -227,12 +229,18 @@ pub fn finalize_execution( // Take into account the VM execution resources of the current call, without inner calls. // Has to happen after marking holes in segments as accessed. - let vm_resources_without_inner_calls = runner + let mut vm_resources_without_inner_calls = runner .get_execution_resources() .map_err(VirtualMachineError::RunnerError)? .filter_unused_builtins(); - *syscall_handler.resources += &vm_resources_without_inner_calls; let versioned_constants = syscall_handler.context.versioned_constants(); + if versioned_constants.count_segment_arena_cells_not_instances { + vm_resources_without_inner_calls + .builtin_instance_counter + .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 += &versioned_constants .get_additional_os_syscall_resources(&syscall_handler.syscall_counter)?; diff --git a/crates/blockifier/src/execution/entry_point_execution.rs b/crates/blockifier/src/execution/entry_point_execution.rs index 648f188c8d..d7deda0ee8 100644 --- a/crates/blockifier/src/execution/entry_point_execution.rs +++ b/crates/blockifier/src/execution/entry_point_execution.rs @@ -21,6 +21,7 @@ use crate::execution::entry_point::{ use crate::execution::errors::{EntryPointExecutionError, PostExecutionError, PreExecutionError}; use crate::execution::execution_utils::{ read_execution_retdata, write_felt, write_maybe_relocatable, Args, ReadOnlySegments, + SEGMENT_ARENA_BUILTIN_SIZE, }; use crate::execution::syscalls::hint_processor::SyscallHintProcessor; use crate::state::state_api::State; @@ -391,12 +392,18 @@ pub fn finalize_execution( // Take into account the VM execution resources of the current call, without inner calls. // Has to happen after marking holes in segments as accessed. - let vm_resources_without_inner_calls = runner + let mut vm_resources_without_inner_calls = runner .get_execution_resources() .map_err(VirtualMachineError::RunnerError)? .filter_unused_builtins(); - *syscall_handler.resources += &vm_resources_without_inner_calls; let versioned_constants = syscall_handler.context.versioned_constants(); + if versioned_constants.count_segment_arena_cells_not_instances { + vm_resources_without_inner_calls + .builtin_instance_counter + .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 += &versioned_constants .get_additional_os_syscall_resources(&syscall_handler.syscall_counter)?; diff --git a/crates/blockifier/src/execution/execution_utils.rs b/crates/blockifier/src/execution/execution_utils.rs index 326e1a6337..8934b133dc 100644 --- a/crates/blockifier/src/execution/execution_utils.rs +++ b/crates/blockifier/src/execution/execution_utils.rs @@ -34,6 +34,8 @@ use crate::transaction::objects::TransactionInfo; pub type Args = Vec; +pub const SEGMENT_ARENA_BUILTIN_SIZE: usize = 3; + /// Executes a specific call to a contract entry point and returns its output. pub fn execute_entry_point_call( call: CallEntryPoint, diff --git a/crates/blockifier/src/versioned_constants.rs b/crates/blockifier/src/versioned_constants.rs index 0114144755..699e17f886 100644 --- a/crates/blockifier/src/versioned_constants.rs +++ b/crates/blockifier/src/versioned_constants.rs @@ -48,6 +48,11 @@ pub struct VersionedConstants { pub l2_resource_gas_costs: L2ResourceGasCosts, pub max_recursion_depth: usize, pub validate_max_n_steps: u32, + // BACKWARD COMPATIBILITY: If true, the segment_arena builtin instance counter will be + // multiplied by 3. This offsets a bug in the old vm where the counter counted the number of + // cells used by instances of the builtin, instead of the number of instances. + #[serde(default)] + pub count_segment_arena_cells_not_instances: bool, // Transactions settings. #[serde(default)]