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

Commit

Permalink
fix: offset segment arena instance counter for older vm versions (#2090)
Browse files Browse the repository at this point in the history
  • Loading branch information
avi-starkware committed Jul 16, 2024
1 parent 3580880 commit 39463c7
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 4 deletions.
1 change: 1 addition & 0 deletions crates/blockifier/resources/versioned_constants.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
]
},
"max_recursion_depth": 50,
"count_segment_arena_cells_not_instances": false,
"os_constants": {
"nop_entry_point_offset": -1,
"entry_point_type_external": 0,
Expand Down
1 change: 1 addition & 0 deletions crates/blockifier/resources/versioned_constants_13_0.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions crates/blockifier/resources/versioned_constants_13_1.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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};
Expand Down Expand Up @@ -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)?;
Expand Down
11 changes: 9 additions & 2 deletions crates/blockifier/src/execution/entry_point_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,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;
Expand Down Expand Up @@ -318,12 +319,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)?;
Expand Down
2 changes: 2 additions & 0 deletions crates/blockifier/src/execution/execution_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ use crate::transaction::objects::TransactionInfo;

pub type Args = Vec<CairoArg>;

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,
Expand Down
5 changes: 5 additions & 0 deletions crates/blockifier/src/versioned_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down

0 comments on commit 39463c7

Please sign in to comment.