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 db5ec51
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 8 deletions.
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ ark-secp256k1 = "0.4.0"
ark-secp256r1 = "0.4.0"
assert_matches = "1.5.0"
cached = "0.44.0"
cairo-lang-casm = "2.7.0-rc.1"
cairo-lang-runner = "2.7.0-rc.1"
cairo-lang-starknet-classes = "2.7.0-rc.1"
cairo-lang-utils = "2.7.0-rc.1"
cairo-lang-casm = "2.7.0-rc.2"
cairo-lang-runner = "2.7.0-rc.2"
cairo-lang-starknet-classes = "2.7.0-rc.2"
cairo-lang-utils = "2.7.0-rc.2"
cairo-vm = "1.0.0-rc5"
criterion = "0.3"
derive_more = "0.99.17"
Expand Down
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": 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_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 db5ec51

Please sign in to comment.