From 368152ec4944589f5cdc6cc7d6c747e826242de8 Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 29 Nov 2022 11:50:10 -0300 Subject: [PATCH 01/57] Expose write_binary_memory method --- src/cairo_runner.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/cairo_runner.rs b/src/cairo_runner.rs index ebf00915..a685bc93 100644 --- a/src/cairo_runner.rs +++ b/src/cairo_runner.rs @@ -5,7 +5,7 @@ use crate::{ }; use cairo_rs::{ bigint, - cairo_run::write_output, + cairo_run::{write_binary_memory, write_output}, hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor, serde::deserialize_program::Member, types::{ @@ -26,7 +26,14 @@ use pyo3::{ prelude::*, types::PyIterator, }; -use std::{any::Any, borrow::BorrowMut, collections::HashMap, iter::zip, path::PathBuf, rc::Rc}; +use std::{ + any::Any, + borrow::BorrowMut, + collections::HashMap, + iter::zip, + path::{Path, PathBuf}, + rc::Rc, +}; const MEMORY_GET_SEGMENT_USED_SIZE_MSG: &str = "Failed to segment used size"; const FAILED_TO_GET_INITIAL_FP: &str = "Failed to get initial segment"; @@ -210,6 +217,10 @@ impl PyCairoRunner { write_output(&mut self.inner, &mut (*self.pyvm.vm).borrow_mut()).map_err(to_py_error) } + pub fn write_binary_memory(&mut self, name: String) -> PyResult<()> { + write_binary_memory(&self.inner.relocated_memory, &Path::new(&name)).map_err(to_py_error) + } + pub fn add_segment(&self) -> PyRelocatable { (*self.pyvm.vm).borrow_mut().add_memory_segment().into() } From 28c9cff72957e92f0e2997cf9a2813eb11bfb7eb Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 29 Nov 2022 11:57:34 -0300 Subject: [PATCH 02/57] Add patches and build script --- scripts/memory_comparator/build_envs.sh | 19 + .../output-memory-cairo-lang.patch | 51 ++ .../output-memory-cairo-rs-py.patch | 521 ++++++++++++++++++ .../patch-memory-cairo-lang.patch | 51 ++ .../patch-memory-cairo-rs-py.patch | 521 ++++++++++++++++++ 5 files changed, 1163 insertions(+) create mode 100755 scripts/memory_comparator/build_envs.sh create mode 100644 scripts/memory_comparator/output-memory-cairo-lang.patch create mode 100644 scripts/memory_comparator/output-memory-cairo-rs-py.patch create mode 100644 scripts/memory_comparator/patch-memory-cairo-lang.patch create mode 100644 scripts/memory_comparator/patch-memory-cairo-rs-py.patch diff --git a/scripts/memory_comparator/build_envs.sh b/scripts/memory_comparator/build_envs.sh new file mode 100755 index 00000000..7b262db3 --- /dev/null +++ b/scripts/memory_comparator/build_envs.sh @@ -0,0 +1,19 @@ +#!/bin/sh + +set -e +set -o pipefail + +# This is not reaaaaally a robust way to find it, but you need to be actively +# trying to break it for this to fail :) +SCRIPT_DIR=$(dirname $0) + +python3.9 -m venv --upgrade-deps ${SCRIPT_DIR}/cairo-lang ${SCRIPT_DIR}/cairo-rs-py +${SCRIPT_DIR}/cairo-lang/bin/pip install cairo-lang==0.10.1 +${SCRIPT_DIR}/cairo-rs-py/bin/pip install maturin==0.14.1 cairo-lang==0.10.1 +${SCRIPT_DIR}/cairo-rs-py/bin/maturin build --manifest-path ${SCRIPT_DIR}/../Cargo.toml --release --strip --interpreter 3.9 --no-default-features --features extension +${SCRIPT_DIR}/cairo-rs-py/bin/pip install ${SCRIPT_DIR}/../target/wheels/cairo_rs_py-*.whl +patch --directory ${SCRIPT_DIR}/cairo-rs-py/lib/python3.9/site-packages/ --strip 2 < ${SCRIPT_DIR}/output-memory-cairo-rs-py.patch +patch --directory ${SCRIPT_DIR}/cairo-langlib/python3.9/site-packages/ --strip 2 < ${SCRIPT_DIR}/output-memory-cairo-lang.patch + +${SCRIPT_DIR}/cairo-rs-py/bin/cairo-run --version +${SCRIPT_DIR}/cairo-rs-py/bin/starknet --version diff --git a/scripts/memory_comparator/output-memory-cairo-lang.patch b/scripts/memory_comparator/output-memory-cairo-lang.patch new file mode 100644 index 00000000..95d26179 --- /dev/null +++ b/scripts/memory_comparator/output-memory-cairo-lang.patch @@ -0,0 +1,51 @@ +diff --git a/src/starkware/starknet/business_logic/execution/execute_entry_point.py b/src/starkware/starknet/business_logic/execution/execute_entry_point.py +index 62d4290..f4a0d8b 100644 +--- a/src/starkware/starknet/business_logic/execution/execute_entry_point.py ++++ b/src/starkware/starknet/business_logic/execution/execute_entry_point.py +@@ -9,6 +9,8 @@ from starkware.cairo.lang.vm.relocatable import RelocatableValue + from starkware.cairo.lang.vm.security import SecurityError + from starkware.cairo.lang.vm.utils import ResourcesError + from starkware.cairo.lang.vm.vm_exceptions import HintException, VmException, VmExceptionBase ++from starkware.cairo.lang.vm.cairo_run import write_binary_memory ++import math + from starkware.starknet.business_logic.execution.execute_entry_point_base import ( + ExecuteEntryPointBase, + ) +@@ -274,6 +276,10 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): + assert isinstance(args_ptr, RelocatableValue) # Downcast. + runner.mark_as_accessed(address=args_ptr, size=len(entry_points_args)) + ++ f = open("memory_files/execute_entry_point.memory", "wb") ++ field_bytes = math.ceil(contract_class.program.prime.bit_length() / 8) ++ runner.relocate() ++ write_binary_memory(f, runner.relocated_memory, field_bytes) + return runner, syscall_handler + + def _get_selected_entry_point( +diff --git a/src/starkware/starknet/core/os/class_hash.py b/src/starkware/starknet/core/os/class_hash.py +index 132fb21..321c63a 100644 +--- a/src/starkware/starknet/core/os/class_hash.py ++++ b/src/starkware/starknet/core/os/class_hash.py +@@ -3,9 +3,11 @@ import dataclasses + import itertools + import json + import os ++import math + from contextvars import ContextVar + from functools import lru_cache + from typing import Callable, List, Optional ++from starkware.cairo.lang.vm.cairo_run import write_binary_memory + + import cachetools + +@@ -92,6 +94,10 @@ def compute_class_hash_inner( + use_full_name=True, + verify_secure=False, + ) ++ f = open("memory_files/class_hash.memory", "wb") ++ field_bytes = math.ceil(program.prime.bit_length() / 8) ++ runner.relocate() ++ write_binary_memory(f, runner.relocated_memory, field_bytes) + _, class_hash = runner.get_return_values(2) + return class_hash + diff --git a/scripts/memory_comparator/output-memory-cairo-rs-py.patch b/scripts/memory_comparator/output-memory-cairo-rs-py.patch new file mode 100644 index 00000000..d2ae4aed --- /dev/null +++ b/scripts/memory_comparator/output-memory-cairo-rs-py.patch @@ -0,0 +1,521 @@ +diff --git a/src/starkware/starknet/business_logic/execution/execute_entry_point.py b/src/starkware/starknet/business_logic/execution/execute_entry_point.py +index 62d4290..c3cdd7e 100644 +--- a/src/starkware/starknet/business_logic/execution/execute_entry_point.py ++++ b/src/starkware/starknet/business_logic/execution/execute_entry_point.py +@@ -41,6 +41,7 @@ from starkware.starkware_utils.error_handling import ( + stark_assert, + wrap_with_stark_exception, + ) ++import cairo_rs_py + + logger = logging.getLogger(__name__) + +@@ -182,7 +183,8 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): + + # Run the specified contract entry point with given calldata. + with wrap_with_stark_exception(code=StarknetErrorCode.SECURITY_ERROR): +- runner = CairoFunctionRunner(program=contract_class.program, layout="all") ++ runner = cairo_rs_py.CairoRunner(program=contract_class.program.dumps(), entrypoint=None, layout="all", proof_mode=False) ++ runner.initialize_function_runner() + os_context = os_utils.prepare_os_context(runner=runner) + + validate_contract_deployed(state=state, contract_address=self.contract_address) +@@ -205,24 +207,24 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): + os_context, + len(self.calldata), + # Allocate and mark the segment as read-only (to mark every input array as read-only). +- syscall_handler._allocate_segment(segments=runner.segments, data=self.calldata), ++ syscall_handler._allocate_segment(segments=runner, data=self.calldata), + ] + + try: + runner.run_from_entrypoint( + entry_point.offset, +- *entry_points_args, ++ entry_points_args, + hint_locals={ + "syscall_handler": syscall_handler, + }, +- static_locals={ +- "__find_element_max_size": 2**20, +- "__squash_dict_max_size": 2**20, +- "__keccak_max_size": 2**20, +- "__usort_max_size": 2**20, +- "__chained_ec_op_max_len": 1000, +- }, +- run_resources=tx_execution_context.run_resources, ++ # static_locals={ ++ # "__find_element_max_size": 2**20, ++ # "__squash_dict_max_size": 2**20, ++ # "__keccak_max_size": 2**20, ++ # "__usort_max_size": 2**20, ++ # "__chained_ec_op_max_len": 1000, ++ # }, ++ # run_resources=tx_execution_context.run_resources, + verify_secure=True, + ) + except VmException as exception: +@@ -267,13 +269,14 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): + ) + + # When execution starts the stack holds entry_points_args + [ret_fp, ret_pc]. +- args_ptr = runner.initial_fp - (len(entry_points_args) + 2) ++ args_ptr = runner.get_initial_fp() - (len(entry_points_args) + 2) + + # The arguments are touched by the OS and should not be counted as holes, mark them + # as accessed. +- assert isinstance(args_ptr, RelocatableValue) # Downcast. ++ # assert isinstance(args_ptr, RelocatableValue) # Downcast. + runner.mark_as_accessed(address=args_ptr, size=len(entry_points_args)) +- ++ runner.relocate() ++ runner.write_binary_memory("memory_files/execute_entry_point.rs.memory") + return runner, syscall_handler + + def _get_selected_entry_point( +@@ -355,4 +358,4 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): + raise NotImplementedError(f"Call type {self.call_type} not implemented.") + + # Extract pre-fetched contract code from carried state. +- return get_deployed_class_hash_at_address(state=state, contract_address=code_address) ++ return get_deployed_class_hash_at_address(state=state, contract_address=code_address) +\ No newline at end of file +diff --git a/src/starkware/starknet/business_logic/transaction/fee.py b/src/starkware/starknet/business_logic/transaction/fee.py +index 9acaa73..9bbb9f5 100644 +--- a/src/starkware/starknet/business_logic/transaction/fee.py ++++ b/src/starkware/starknet/business_logic/transaction/fee.py +@@ -67,9 +67,9 @@ def calculate_l1_gas_by_cairo_usage( + """ + cairo_resource_fee_weights = general_config.cairo_resource_fee_weights + cairo_resource_names = set(cairo_resource_usage.keys()) +- assert cairo_resource_names.issubset( +- cairo_resource_fee_weights.keys() +- ), "Cairo resource names must be contained in fee weights dict." ++ # assert cairo_resource_names.issubset( ++ # cairo_resource_fee_weights.keys() ++ # ), "Cairo resource names must be contained in fee weights dict." + + # Convert Cairo usage to L1 gas usage. + cairo_l1_gas_usage = max( +diff --git a/src/starkware/starknet/business_logic/utils.py b/src/starkware/starknet/business_logic/utils.py +index f63bc9f..15660fe 100644 +--- a/src/starkware/starknet/business_logic/utils.py ++++ b/src/starkware/starknet/business_logic/utils.py +@@ -48,7 +48,14 @@ def get_return_values(runner: CairoFunctionRunner) -> List[int]: + exception_types=[Exception], + ): + ret_data_size, ret_data_ptr = runner.get_return_values(2) +- values = runner.memory.get_range(ret_data_ptr, ret_data_size) ++ ++ try: ++ # CAIRO-RS VERSION ++ values = runner.get_range(ret_data_ptr, ret_data_size) ++ except: ++ # ORIGINAL VERSION ++ values = runner.memory.get_range(ret_data_ptr, ret_data_size) ++ + + stark_assert( + all(isinstance(value, int) for value in values), +diff --git a/src/starkware/starknet/core/os/class_hash.py b/src/starkware/starknet/core/os/class_hash.py +index 132fb21..3cf48a8 100644 +--- a/src/starkware/starknet/core/os/class_hash.py ++++ b/src/starkware/starknet/core/os/class_hash.py +@@ -5,9 +5,10 @@ import json + import os + from contextvars import ContextVar + from functools import lru_cache +-from typing import Callable, List, Optional ++from typing import Any, Callable, Dict, List, Optional, Tuple + + import cachetools ++import cairo_rs_py + + from starkware.cairo.common.cairo_function_runner import CairoFunctionRunner + from starkware.cairo.common.structs import CairoStructFactory, CairoStructProxy +@@ -23,6 +24,10 @@ from starkware.cairo.lang.vm.crypto import pedersen_hash + from starkware.python.utils import from_bytes + from starkware.starknet.public.abi import starknet_keccak + from starkware.starknet.services.api.contract_class import ContractClass, EntryPointType ++# Added Imports ++from starkware.cairo.lang.vm.relocatable import MaybeRelocatable, RelocatableValue ++from starkware.cairo.lang.vm.vm_exceptions import SecurityError, VmException ++from starkware.python.utils import safe_zip + + CAIRO_FILE = os.path.join(os.path.dirname(__file__), "contracts.cairo") + +@@ -77,22 +82,24 @@ def compute_class_hash_inner( + contract_class_struct = get_contract_class_struct( + identifiers=program.identifiers, contract_class=contract_class + ) +- runner = CairoFunctionRunner(program) + +- hash_builtin = HashBuiltinRunner( +- name="custom_hasher", included=True, ratio=32, hash_func=hash_func +- ) +- runner.builtin_runners["hash_builtin"] = hash_builtin +- hash_builtin.initialize_segments(runner) ++ runner = cairo_rs_py.CairoRunner(program=program.dumps(), entrypoint=None, layout="all", proof_mode=False) ++ runner.initialize_function_runner() ++ hash_ptr = runner.add_additional_hash_builtin() ++ + +- runner.run( ++ run_function_runner( ++ runner, ++ program, + "starkware.starknet.core.os.contracts.class_hash", +- hash_ptr=hash_builtin.base, ++ hash_ptr=hash_ptr, + contract_class=contract_class_struct, + use_full_name=True, + verify_secure=False, + ) + _, class_hash = runner.get_return_values(2) ++ runner.relocate() ++ runner.write_binary_memory("memory_files/class_hash.rs.memory") + return class_hash + + +@@ -194,3 +201,103 @@ def get_contract_class_struct( + bytecode_length=len(contract_class.program.data), + bytecode_ptr=contract_class.program.data, + ) ++ ++def run_function_runner( ++ runner, ++ program, ++ func_name: str, ++ *args, ++ hint_locals: Optional[Dict[str, Any]] = None, ++ static_locals: Optional[Dict[str, Any]] = None, ++ verify_secure: Optional[bool] = None, ++ trace_on_failure: bool = False, ++ apply_modulo_to_args: Optional[bool] = None, ++ use_full_name: bool = False, ++ verify_implicit_args_segment: bool = False, ++ **kwargs, ++ ) -> Tuple[Tuple[MaybeRelocatable, ...], Tuple[MaybeRelocatable, ...]]: ++ """ ++ Runs func_name(*args). ++ args are converted to Cairo-friendly ones using gen_arg. ++ ++ Returns the return values of the function, splitted into 2 tuples of implicit values and ++ explicit values. Structs will be flattened to a sequence of felts as part of the returned ++ tuple. ++ ++ Additional params: ++ verify_secure - Run verify_secure_runner to do extra verifications. ++ trace_on_failure - Run the tracer in case of failure to help debugging. ++ apply_modulo_to_args - Apply modulo operation on integer arguments. ++ use_full_name - Treat 'func_name' as a fully qualified identifier name, rather than a ++ relative one. ++ verify_implicit_args_segment - For each implicit argument, verify that the argument and the ++ return value are in the same segment. ++ """ ++ assert isinstance(program, Program) ++ entrypoint = program.get_label(func_name, full_name_lookup=use_full_name) ++ ++ #Construct Fu ++ structs_factory = CairoStructFactory.from_program(program=program) ++ func = ScopedName.from_string(scope=func_name) ++ ++ full_args_struct = structs_factory.build_func_args(func=func) ++ all_args = full_args_struct(*args, **kwargs) ++ ++ try: ++ runner.run_from_entrypoint( ++ entrypoint, ++ all_args, ++ typed_args=True, ++ hint_locals=hint_locals, ++ static_locals=static_locals, ++ verify_secure=verify_secure, ++ apply_modulo_to_args=apply_modulo_to_args, ++ ) ++ except (VmException, SecurityError, AssertionError) as ex: ++ if trace_on_failure: ++ print( ++ f"""\ ++Got {type(ex).__name__} exception during the execution of {func_name}: ++{str(ex)} ++""" ++ ) ++ #trace_runner(runner=runner) ++ raise ++ ++ # The number of implicit arguments is identical to the number of implicit return values. ++ n_implicit_ret_vals = structs_factory.get_implicit_args_length(func=func) ++ n_explicit_ret_vals = structs_factory.get_explicit_return_values_length(func=func) ++ n_ret_vals = n_explicit_ret_vals + n_implicit_ret_vals ++ implicit_retvals = tuple( ++ runner.get_range( ++ runner.get_ap() - n_ret_vals, n_implicit_ret_vals ++ ) ++ ) ++ ++ explicit_retvals = tuple( ++ runner.get_range( ++ runner.get_ap() - n_explicit_ret_vals, n_explicit_ret_vals ++ ) ++ ) ++ ++ # Verify the memory segments of the implicit arguments. ++ if verify_implicit_args_segment: ++ implicit_args = all_args[:n_implicit_ret_vals] ++ for implicit_arg, implicit_retval in safe_zip(implicit_args, implicit_retvals): ++ assert isinstance( ++ implicit_arg, RelocatableValue ++ ), f"Implicit arguments must be RelocatableValues, {implicit_arg} is not." ++ assert isinstance(implicit_retval, RelocatableValue), ( ++ f"Argument {implicit_arg} is a RelocatableValue, but the returned value " ++ f"{implicit_retval} is not." ++ ) ++ assert implicit_arg.segment_index == implicit_retval.segment_index, ( ++ f"Implicit argument {implicit_arg} is not on the same segment as the returned " ++ f"{implicit_retval}." ++ ) ++ assert implicit_retval.offset >= implicit_arg.offset, ( ++ f"The offset of the returned implicit argument {implicit_retval} is less than " ++ f"the offset of the input {implicit_arg}." ++ ) ++ ++ return implicit_retvals, explicit_retvals +diff --git a/src/starkware/starknet/core/os/os_utils.py b/src/starkware/starknet/core/os/os_utils.py +index 20bd521..0ea99f4 100644 +--- a/src/starkware/starknet/core/os/os_utils.py ++++ b/src/starkware/starknet/core/os/os_utils.py +@@ -43,18 +43,23 @@ def update_builtin_pointers( + + return return_builtins + +- + def prepare_os_context(runner: CairoFunctionRunner) -> List[MaybeRelocatable]: +- syscall_segment = runner.segments.add() +- os_context: List[MaybeRelocatable] = [syscall_segment] +- +- for builtin in runner.program.builtins: +- builtin_runner = runner.builtin_runners[f"{builtin}_builtin"] +- os_context.extend(builtin_runner.initial_stack()) ++ # CAIRO-RS VERSION ++ try: ++ syscall_segment = runner.add_segment() ++ os_context: List[MaybeRelocatable] = [syscall_segment] ++ os_context.extend(runner.get_program_builtins_initial_stack()) ++ # ORIGINAL VERSION ++ except: ++ syscall_segment = runner.segments.add() ++ os_context: List[MaybeRelocatable] = [syscall_segment] ++ ++ for builtin in runner.program.builtins: ++ builtin_runner = runner.builtin_runners[f"{builtin}_builtin"] ++ os_context.extend(builtin_runner.initial_stack()) + + return os_context + +- + def validate_and_process_os_context( + runner: CairoFunctionRunner, + syscall_handler: syscall_utils.BusinessLogicSysCallHandler, +@@ -64,14 +69,23 @@ def validate_and_process_os_context( + Validates and processes an OS context that was returned by a transaction. + Returns the syscall processor object containing the accumulated syscall information. + """ +- # The returned values are os_context, retdata_size, retdata_ptr. +- os_context_end = runner.vm.run_context.ap - 2 +- stack_ptr = os_context_end +- for builtin in runner.program.builtins[::-1]: +- builtin_runner = runner.builtin_runners[f"{builtin}_builtin"] ++ # CAIRO-RS VERSION ++ try: ++ os_context_end = runner.get_ap() - 2 ++ stack_ptr = os_context_end ++ # The returned values are os_context, retdata_size, retdata_ptr. ++ stack_ptr = runner.get_builtins_final_stack(stack_ptr) ++ # ORIGINAL VERSION ++ except: ++ os_context_end = runner.vm.run_context.ap - 2 ++ ++ stack_ptr = os_context_end + +- with wrap_with_stark_exception(code=StarknetErrorCode.SECURITY_ERROR): +- stack_ptr = builtin_runner.final_stack(runner=runner, pointer=stack_ptr) ++ for builtin in runner.program.builtins[::-1]: ++ builtin_runner = runner.builtin_runners[f"{builtin}_builtin"] ++ ++ with wrap_with_stark_exception(code=StarknetErrorCode.SECURITY_ERROR): ++ stack_ptr = builtin_runner.final_stack(runner=runner, pointer=stack_ptr) + + final_os_context_ptr = stack_ptr - 1 + assert final_os_context_ptr + len(initial_os_context) == os_context_end +@@ -81,9 +95,19 @@ def validate_and_process_os_context( + runner=runner, ptr_offset=SYSCALL_PTR_OFFSET, os_context=initial_os_context + ) + +- segment_utils.validate_segment_pointers( +- segments=runner.segments, +- segment_base_ptr=syscall_base_ptr, +- segment_stop_ptr=syscall_stop_ptr, +- ) ++ # ORIGINAL VERSION ++ try: ++ segment_utils.validate_segment_pointers( ++ segments=runner, ++ segment_base_ptr=syscall_base_ptr, ++ segment_stop_ptr=syscall_stop_ptr, ++ ) ++ # CAIRO-RS VERSION ++ except: ++ segment_utils.validate_segment_pointers( ++ segments=runner.segments, ++ segment_base_ptr=syscall_base_ptr, ++ segment_stop_ptr=syscall_stop_ptr, ++ ) ++ + syscall_handler.post_run(runner=runner, syscall_stop_ptr=syscall_stop_ptr) +diff --git a/src/starkware/starknet/core/os/segment_utils.py b/src/starkware/starknet/core/os/segment_utils.py +index 1d09414..33f5c26 100644 +--- a/src/starkware/starknet/core/os/segment_utils.py ++++ b/src/starkware/starknet/core/os/segment_utils.py +@@ -8,7 +8,7 @@ from starkware.starknet.definitions.error_codes import StarknetErrorCode + from starkware.starknet.public.abi import SYSCALL_PTR_OFFSET + from starkware.starkware_utils.error_handling import stark_assert, wrap_with_stark_exception + +- ++# LAMBDA MODIFIED + def get_os_segment_ptr_range( + runner: CairoFunctionRunner, ptr_offset: int, os_context: List[MaybeRelocatable] + ) -> Tuple[MaybeRelocatable, MaybeRelocatable]: +@@ -21,10 +21,23 @@ def get_os_segment_ptr_range( + ), f"Illegal OS ptr offset; must be one of: {allowed_offsets}." + + # The returned values are os_context, retdata_size, retdata_ptr. +- os_context_end = runner.vm.run_context.ap - 2 ++ # CAIRO-RS VERSION ++ try: ++ os_context_end = runner.get_ap() - 2 ++ except: ++ # ORIGINAL VERSION ++ os_context_end = runner.vm.run_context.ap - 2 ++ + final_os_context_ptr = os_context_end - len(os_context) + +- return os_context[ptr_offset], runner.vm_memory[final_os_context_ptr + ptr_offset] ++ # CAIRO-RS VERSION ++ try: ++ return os_context[ptr_offset], runner.get(final_os_context_ptr + ptr_offset) ++ # ORIGINAL VERSION ++ except: ++ return os_context[ptr_offset], runner.vm_memory[final_os_context_ptr + ptr_offset] ++ ++ + + + def get_os_segment_stop_ptr( +@@ -61,14 +74,19 @@ def validate_segment_pointers( + segment_base_ptr: MaybeRelocatable, + segment_stop_ptr: MaybeRelocatable, + ): +- assert isinstance(segment_base_ptr, RelocatableValue) ++ # assert isinstance(segment_base_ptr, RelocatableValue) + assert ( + segment_base_ptr.offset == 0 + ), f"Segment base pointer must be zero; got {segment_base_ptr.offset}." + +- expected_stop_ptr = segment_base_ptr + segments.get_segment_used_size( +- segment_index=segment_base_ptr.segment_index +- ) ++ # CAIRO-RS VERSION ++ try: ++ expected_stop_ptr = segment_base_ptr + segments.get_segment_used_size( ++ index=segment_base_ptr.segment_index) ++ # ORIGINAL VERSION ++ except: ++ expected_stop_ptr = segment_base_ptr + segments.get_segment_used_size( ++ segment_index=segment_base_ptr.segment_index) + + stark_assert( + expected_stop_ptr == segment_stop_ptr, +diff --git a/src/starkware/starknet/core/os/syscall_utils.py b/src/starkware/starknet/core/os/syscall_utils.py +index e44635b..c129e50 100644 +--- a/src/starkware/starknet/core/os/syscall_utils.py ++++ b/src/starkware/starknet/core/os/syscall_utils.py +@@ -458,7 +458,6 @@ class SysCallHandlerBase(ABC): + ) -> List[int]: + """ + Returns the call retdata. +- + syscall_name can be "call_contract", "delegate_call", "delegate_l1_handler", "library_call" + or "library_call_l1_handler". + """ +@@ -589,7 +588,16 @@ class BusinessLogicSysCallHandler(SysCallHandlerBase): + def _allocate_segment( + self, segments: MemorySegmentManager, data: Iterable[MaybeRelocatable] + ) -> RelocatableValue: +- segment_start = segments.add() ++ # FIXME: Here "segments" in really a Runner under the hood. ++ # May want to change the variable names. ++ ++ # CAIRO-RS VERSION ++ try: ++ segment_start = segments.add_segment() ++ # ORIGINAL VERSION ++ except: ++ segment_start = segments.add() ++ + segment_end = segments.write_arg(ptr=segment_start, arg=data) + self.read_only_segments.append((segment_start, segment_end - segment_start)) + return segment_start +@@ -631,10 +639,10 @@ class BusinessLogicSysCallHandler(SysCallHandlerBase): + args_struct_def: StructDefinition = syscall_info.syscall_request_struct.struct_definition_ + for arg, (arg_name, arg_def) in safe_zip(request, args_struct_def.members.items()): + expected_type = get_runtime_type(arg_def.cairo_type) +- assert isinstance(arg, expected_type), ( +- f"Argument {arg_name} to syscall {syscall_name} is of unexpected type. " +- f"Expected: value of type {expected_type}; got: {arg}." +- ) ++ # assert isinstance(arg, expected_type), ( ++ # f"Argument {arg_name} to syscall {syscall_name} is of unexpected type. " ++ # f"Expected: value of type {expected_type}; got: {arg}." ++ # ) + + return request + +@@ -902,10 +910,20 @@ class BusinessLogicSysCallHandler(SysCallHandlerBase): + Validates that there were no out of bounds writes to read-only segments and marks + them as accessed. + """ +- segments = runner.segments ++ # ORIGINAL VERSION ++ try: ++ segments = runner.segments ++ # CAIRO-RS VERSION ++ except: ++ segments = runner + + for segment_ptr, segment_size in self.read_only_segments: +- used_size = segments.get_segment_used_size(segment_index=segment_ptr.segment_index) ++ # CAIRO-RS VERSION ++ try: ++ used_size = segments.get_segment_used_size(index=segment_ptr.segment_index) ++ # ORIGINAL VERSION ++ except: ++ used_size = segments.get_segment_used_size(segment_index=segment_ptr.segment_index) + stark_assert( + used_size == segment_size, + code=StarknetErrorCode.SECURITY_ERROR, +@@ -1041,7 +1059,6 @@ class OsSysCallHandler(SysCallHandlerBase): + def start_tx(self, tx_info_ptr: RelocatableValue): + """ + Called when starting the execution of a transaction. +- + 'tx_info_ptr' is a pointer to the TxInfo struct corresponding to said transaction. + """ + assert self.tx_info_ptr is None +@@ -1089,4 +1106,4 @@ class OsSysCallHandler(SysCallHandlerBase): + Called when skipping the execution of a transaction. + It replaces a call to start_tx and end_tx. + """ +- next(self.tx_execution_info_iterator) ++ next(self.tx_execution_info_iterator) +\ No newline at end of file diff --git a/scripts/memory_comparator/patch-memory-cairo-lang.patch b/scripts/memory_comparator/patch-memory-cairo-lang.patch new file mode 100644 index 00000000..95d26179 --- /dev/null +++ b/scripts/memory_comparator/patch-memory-cairo-lang.patch @@ -0,0 +1,51 @@ +diff --git a/src/starkware/starknet/business_logic/execution/execute_entry_point.py b/src/starkware/starknet/business_logic/execution/execute_entry_point.py +index 62d4290..f4a0d8b 100644 +--- a/src/starkware/starknet/business_logic/execution/execute_entry_point.py ++++ b/src/starkware/starknet/business_logic/execution/execute_entry_point.py +@@ -9,6 +9,8 @@ from starkware.cairo.lang.vm.relocatable import RelocatableValue + from starkware.cairo.lang.vm.security import SecurityError + from starkware.cairo.lang.vm.utils import ResourcesError + from starkware.cairo.lang.vm.vm_exceptions import HintException, VmException, VmExceptionBase ++from starkware.cairo.lang.vm.cairo_run import write_binary_memory ++import math + from starkware.starknet.business_logic.execution.execute_entry_point_base import ( + ExecuteEntryPointBase, + ) +@@ -274,6 +276,10 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): + assert isinstance(args_ptr, RelocatableValue) # Downcast. + runner.mark_as_accessed(address=args_ptr, size=len(entry_points_args)) + ++ f = open("memory_files/execute_entry_point.memory", "wb") ++ field_bytes = math.ceil(contract_class.program.prime.bit_length() / 8) ++ runner.relocate() ++ write_binary_memory(f, runner.relocated_memory, field_bytes) + return runner, syscall_handler + + def _get_selected_entry_point( +diff --git a/src/starkware/starknet/core/os/class_hash.py b/src/starkware/starknet/core/os/class_hash.py +index 132fb21..321c63a 100644 +--- a/src/starkware/starknet/core/os/class_hash.py ++++ b/src/starkware/starknet/core/os/class_hash.py +@@ -3,9 +3,11 @@ import dataclasses + import itertools + import json + import os ++import math + from contextvars import ContextVar + from functools import lru_cache + from typing import Callable, List, Optional ++from starkware.cairo.lang.vm.cairo_run import write_binary_memory + + import cachetools + +@@ -92,6 +94,10 @@ def compute_class_hash_inner( + use_full_name=True, + verify_secure=False, + ) ++ f = open("memory_files/class_hash.memory", "wb") ++ field_bytes = math.ceil(program.prime.bit_length() / 8) ++ runner.relocate() ++ write_binary_memory(f, runner.relocated_memory, field_bytes) + _, class_hash = runner.get_return_values(2) + return class_hash + diff --git a/scripts/memory_comparator/patch-memory-cairo-rs-py.patch b/scripts/memory_comparator/patch-memory-cairo-rs-py.patch new file mode 100644 index 00000000..d2ae4aed --- /dev/null +++ b/scripts/memory_comparator/patch-memory-cairo-rs-py.patch @@ -0,0 +1,521 @@ +diff --git a/src/starkware/starknet/business_logic/execution/execute_entry_point.py b/src/starkware/starknet/business_logic/execution/execute_entry_point.py +index 62d4290..c3cdd7e 100644 +--- a/src/starkware/starknet/business_logic/execution/execute_entry_point.py ++++ b/src/starkware/starknet/business_logic/execution/execute_entry_point.py +@@ -41,6 +41,7 @@ from starkware.starkware_utils.error_handling import ( + stark_assert, + wrap_with_stark_exception, + ) ++import cairo_rs_py + + logger = logging.getLogger(__name__) + +@@ -182,7 +183,8 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): + + # Run the specified contract entry point with given calldata. + with wrap_with_stark_exception(code=StarknetErrorCode.SECURITY_ERROR): +- runner = CairoFunctionRunner(program=contract_class.program, layout="all") ++ runner = cairo_rs_py.CairoRunner(program=contract_class.program.dumps(), entrypoint=None, layout="all", proof_mode=False) ++ runner.initialize_function_runner() + os_context = os_utils.prepare_os_context(runner=runner) + + validate_contract_deployed(state=state, contract_address=self.contract_address) +@@ -205,24 +207,24 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): + os_context, + len(self.calldata), + # Allocate and mark the segment as read-only (to mark every input array as read-only). +- syscall_handler._allocate_segment(segments=runner.segments, data=self.calldata), ++ syscall_handler._allocate_segment(segments=runner, data=self.calldata), + ] + + try: + runner.run_from_entrypoint( + entry_point.offset, +- *entry_points_args, ++ entry_points_args, + hint_locals={ + "syscall_handler": syscall_handler, + }, +- static_locals={ +- "__find_element_max_size": 2**20, +- "__squash_dict_max_size": 2**20, +- "__keccak_max_size": 2**20, +- "__usort_max_size": 2**20, +- "__chained_ec_op_max_len": 1000, +- }, +- run_resources=tx_execution_context.run_resources, ++ # static_locals={ ++ # "__find_element_max_size": 2**20, ++ # "__squash_dict_max_size": 2**20, ++ # "__keccak_max_size": 2**20, ++ # "__usort_max_size": 2**20, ++ # "__chained_ec_op_max_len": 1000, ++ # }, ++ # run_resources=tx_execution_context.run_resources, + verify_secure=True, + ) + except VmException as exception: +@@ -267,13 +269,14 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): + ) + + # When execution starts the stack holds entry_points_args + [ret_fp, ret_pc]. +- args_ptr = runner.initial_fp - (len(entry_points_args) + 2) ++ args_ptr = runner.get_initial_fp() - (len(entry_points_args) + 2) + + # The arguments are touched by the OS and should not be counted as holes, mark them + # as accessed. +- assert isinstance(args_ptr, RelocatableValue) # Downcast. ++ # assert isinstance(args_ptr, RelocatableValue) # Downcast. + runner.mark_as_accessed(address=args_ptr, size=len(entry_points_args)) +- ++ runner.relocate() ++ runner.write_binary_memory("memory_files/execute_entry_point.rs.memory") + return runner, syscall_handler + + def _get_selected_entry_point( +@@ -355,4 +358,4 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): + raise NotImplementedError(f"Call type {self.call_type} not implemented.") + + # Extract pre-fetched contract code from carried state. +- return get_deployed_class_hash_at_address(state=state, contract_address=code_address) ++ return get_deployed_class_hash_at_address(state=state, contract_address=code_address) +\ No newline at end of file +diff --git a/src/starkware/starknet/business_logic/transaction/fee.py b/src/starkware/starknet/business_logic/transaction/fee.py +index 9acaa73..9bbb9f5 100644 +--- a/src/starkware/starknet/business_logic/transaction/fee.py ++++ b/src/starkware/starknet/business_logic/transaction/fee.py +@@ -67,9 +67,9 @@ def calculate_l1_gas_by_cairo_usage( + """ + cairo_resource_fee_weights = general_config.cairo_resource_fee_weights + cairo_resource_names = set(cairo_resource_usage.keys()) +- assert cairo_resource_names.issubset( +- cairo_resource_fee_weights.keys() +- ), "Cairo resource names must be contained in fee weights dict." ++ # assert cairo_resource_names.issubset( ++ # cairo_resource_fee_weights.keys() ++ # ), "Cairo resource names must be contained in fee weights dict." + + # Convert Cairo usage to L1 gas usage. + cairo_l1_gas_usage = max( +diff --git a/src/starkware/starknet/business_logic/utils.py b/src/starkware/starknet/business_logic/utils.py +index f63bc9f..15660fe 100644 +--- a/src/starkware/starknet/business_logic/utils.py ++++ b/src/starkware/starknet/business_logic/utils.py +@@ -48,7 +48,14 @@ def get_return_values(runner: CairoFunctionRunner) -> List[int]: + exception_types=[Exception], + ): + ret_data_size, ret_data_ptr = runner.get_return_values(2) +- values = runner.memory.get_range(ret_data_ptr, ret_data_size) ++ ++ try: ++ # CAIRO-RS VERSION ++ values = runner.get_range(ret_data_ptr, ret_data_size) ++ except: ++ # ORIGINAL VERSION ++ values = runner.memory.get_range(ret_data_ptr, ret_data_size) ++ + + stark_assert( + all(isinstance(value, int) for value in values), +diff --git a/src/starkware/starknet/core/os/class_hash.py b/src/starkware/starknet/core/os/class_hash.py +index 132fb21..3cf48a8 100644 +--- a/src/starkware/starknet/core/os/class_hash.py ++++ b/src/starkware/starknet/core/os/class_hash.py +@@ -5,9 +5,10 @@ import json + import os + from contextvars import ContextVar + from functools import lru_cache +-from typing import Callable, List, Optional ++from typing import Any, Callable, Dict, List, Optional, Tuple + + import cachetools ++import cairo_rs_py + + from starkware.cairo.common.cairo_function_runner import CairoFunctionRunner + from starkware.cairo.common.structs import CairoStructFactory, CairoStructProxy +@@ -23,6 +24,10 @@ from starkware.cairo.lang.vm.crypto import pedersen_hash + from starkware.python.utils import from_bytes + from starkware.starknet.public.abi import starknet_keccak + from starkware.starknet.services.api.contract_class import ContractClass, EntryPointType ++# Added Imports ++from starkware.cairo.lang.vm.relocatable import MaybeRelocatable, RelocatableValue ++from starkware.cairo.lang.vm.vm_exceptions import SecurityError, VmException ++from starkware.python.utils import safe_zip + + CAIRO_FILE = os.path.join(os.path.dirname(__file__), "contracts.cairo") + +@@ -77,22 +82,24 @@ def compute_class_hash_inner( + contract_class_struct = get_contract_class_struct( + identifiers=program.identifiers, contract_class=contract_class + ) +- runner = CairoFunctionRunner(program) + +- hash_builtin = HashBuiltinRunner( +- name="custom_hasher", included=True, ratio=32, hash_func=hash_func +- ) +- runner.builtin_runners["hash_builtin"] = hash_builtin +- hash_builtin.initialize_segments(runner) ++ runner = cairo_rs_py.CairoRunner(program=program.dumps(), entrypoint=None, layout="all", proof_mode=False) ++ runner.initialize_function_runner() ++ hash_ptr = runner.add_additional_hash_builtin() ++ + +- runner.run( ++ run_function_runner( ++ runner, ++ program, + "starkware.starknet.core.os.contracts.class_hash", +- hash_ptr=hash_builtin.base, ++ hash_ptr=hash_ptr, + contract_class=contract_class_struct, + use_full_name=True, + verify_secure=False, + ) + _, class_hash = runner.get_return_values(2) ++ runner.relocate() ++ runner.write_binary_memory("memory_files/class_hash.rs.memory") + return class_hash + + +@@ -194,3 +201,103 @@ def get_contract_class_struct( + bytecode_length=len(contract_class.program.data), + bytecode_ptr=contract_class.program.data, + ) ++ ++def run_function_runner( ++ runner, ++ program, ++ func_name: str, ++ *args, ++ hint_locals: Optional[Dict[str, Any]] = None, ++ static_locals: Optional[Dict[str, Any]] = None, ++ verify_secure: Optional[bool] = None, ++ trace_on_failure: bool = False, ++ apply_modulo_to_args: Optional[bool] = None, ++ use_full_name: bool = False, ++ verify_implicit_args_segment: bool = False, ++ **kwargs, ++ ) -> Tuple[Tuple[MaybeRelocatable, ...], Tuple[MaybeRelocatable, ...]]: ++ """ ++ Runs func_name(*args). ++ args are converted to Cairo-friendly ones using gen_arg. ++ ++ Returns the return values of the function, splitted into 2 tuples of implicit values and ++ explicit values. Structs will be flattened to a sequence of felts as part of the returned ++ tuple. ++ ++ Additional params: ++ verify_secure - Run verify_secure_runner to do extra verifications. ++ trace_on_failure - Run the tracer in case of failure to help debugging. ++ apply_modulo_to_args - Apply modulo operation on integer arguments. ++ use_full_name - Treat 'func_name' as a fully qualified identifier name, rather than a ++ relative one. ++ verify_implicit_args_segment - For each implicit argument, verify that the argument and the ++ return value are in the same segment. ++ """ ++ assert isinstance(program, Program) ++ entrypoint = program.get_label(func_name, full_name_lookup=use_full_name) ++ ++ #Construct Fu ++ structs_factory = CairoStructFactory.from_program(program=program) ++ func = ScopedName.from_string(scope=func_name) ++ ++ full_args_struct = structs_factory.build_func_args(func=func) ++ all_args = full_args_struct(*args, **kwargs) ++ ++ try: ++ runner.run_from_entrypoint( ++ entrypoint, ++ all_args, ++ typed_args=True, ++ hint_locals=hint_locals, ++ static_locals=static_locals, ++ verify_secure=verify_secure, ++ apply_modulo_to_args=apply_modulo_to_args, ++ ) ++ except (VmException, SecurityError, AssertionError) as ex: ++ if trace_on_failure: ++ print( ++ f"""\ ++Got {type(ex).__name__} exception during the execution of {func_name}: ++{str(ex)} ++""" ++ ) ++ #trace_runner(runner=runner) ++ raise ++ ++ # The number of implicit arguments is identical to the number of implicit return values. ++ n_implicit_ret_vals = structs_factory.get_implicit_args_length(func=func) ++ n_explicit_ret_vals = structs_factory.get_explicit_return_values_length(func=func) ++ n_ret_vals = n_explicit_ret_vals + n_implicit_ret_vals ++ implicit_retvals = tuple( ++ runner.get_range( ++ runner.get_ap() - n_ret_vals, n_implicit_ret_vals ++ ) ++ ) ++ ++ explicit_retvals = tuple( ++ runner.get_range( ++ runner.get_ap() - n_explicit_ret_vals, n_explicit_ret_vals ++ ) ++ ) ++ ++ # Verify the memory segments of the implicit arguments. ++ if verify_implicit_args_segment: ++ implicit_args = all_args[:n_implicit_ret_vals] ++ for implicit_arg, implicit_retval in safe_zip(implicit_args, implicit_retvals): ++ assert isinstance( ++ implicit_arg, RelocatableValue ++ ), f"Implicit arguments must be RelocatableValues, {implicit_arg} is not." ++ assert isinstance(implicit_retval, RelocatableValue), ( ++ f"Argument {implicit_arg} is a RelocatableValue, but the returned value " ++ f"{implicit_retval} is not." ++ ) ++ assert implicit_arg.segment_index == implicit_retval.segment_index, ( ++ f"Implicit argument {implicit_arg} is not on the same segment as the returned " ++ f"{implicit_retval}." ++ ) ++ assert implicit_retval.offset >= implicit_arg.offset, ( ++ f"The offset of the returned implicit argument {implicit_retval} is less than " ++ f"the offset of the input {implicit_arg}." ++ ) ++ ++ return implicit_retvals, explicit_retvals +diff --git a/src/starkware/starknet/core/os/os_utils.py b/src/starkware/starknet/core/os/os_utils.py +index 20bd521..0ea99f4 100644 +--- a/src/starkware/starknet/core/os/os_utils.py ++++ b/src/starkware/starknet/core/os/os_utils.py +@@ -43,18 +43,23 @@ def update_builtin_pointers( + + return return_builtins + +- + def prepare_os_context(runner: CairoFunctionRunner) -> List[MaybeRelocatable]: +- syscall_segment = runner.segments.add() +- os_context: List[MaybeRelocatable] = [syscall_segment] +- +- for builtin in runner.program.builtins: +- builtin_runner = runner.builtin_runners[f"{builtin}_builtin"] +- os_context.extend(builtin_runner.initial_stack()) ++ # CAIRO-RS VERSION ++ try: ++ syscall_segment = runner.add_segment() ++ os_context: List[MaybeRelocatable] = [syscall_segment] ++ os_context.extend(runner.get_program_builtins_initial_stack()) ++ # ORIGINAL VERSION ++ except: ++ syscall_segment = runner.segments.add() ++ os_context: List[MaybeRelocatable] = [syscall_segment] ++ ++ for builtin in runner.program.builtins: ++ builtin_runner = runner.builtin_runners[f"{builtin}_builtin"] ++ os_context.extend(builtin_runner.initial_stack()) + + return os_context + +- + def validate_and_process_os_context( + runner: CairoFunctionRunner, + syscall_handler: syscall_utils.BusinessLogicSysCallHandler, +@@ -64,14 +69,23 @@ def validate_and_process_os_context( + Validates and processes an OS context that was returned by a transaction. + Returns the syscall processor object containing the accumulated syscall information. + """ +- # The returned values are os_context, retdata_size, retdata_ptr. +- os_context_end = runner.vm.run_context.ap - 2 +- stack_ptr = os_context_end +- for builtin in runner.program.builtins[::-1]: +- builtin_runner = runner.builtin_runners[f"{builtin}_builtin"] ++ # CAIRO-RS VERSION ++ try: ++ os_context_end = runner.get_ap() - 2 ++ stack_ptr = os_context_end ++ # The returned values are os_context, retdata_size, retdata_ptr. ++ stack_ptr = runner.get_builtins_final_stack(stack_ptr) ++ # ORIGINAL VERSION ++ except: ++ os_context_end = runner.vm.run_context.ap - 2 ++ ++ stack_ptr = os_context_end + +- with wrap_with_stark_exception(code=StarknetErrorCode.SECURITY_ERROR): +- stack_ptr = builtin_runner.final_stack(runner=runner, pointer=stack_ptr) ++ for builtin in runner.program.builtins[::-1]: ++ builtin_runner = runner.builtin_runners[f"{builtin}_builtin"] ++ ++ with wrap_with_stark_exception(code=StarknetErrorCode.SECURITY_ERROR): ++ stack_ptr = builtin_runner.final_stack(runner=runner, pointer=stack_ptr) + + final_os_context_ptr = stack_ptr - 1 + assert final_os_context_ptr + len(initial_os_context) == os_context_end +@@ -81,9 +95,19 @@ def validate_and_process_os_context( + runner=runner, ptr_offset=SYSCALL_PTR_OFFSET, os_context=initial_os_context + ) + +- segment_utils.validate_segment_pointers( +- segments=runner.segments, +- segment_base_ptr=syscall_base_ptr, +- segment_stop_ptr=syscall_stop_ptr, +- ) ++ # ORIGINAL VERSION ++ try: ++ segment_utils.validate_segment_pointers( ++ segments=runner, ++ segment_base_ptr=syscall_base_ptr, ++ segment_stop_ptr=syscall_stop_ptr, ++ ) ++ # CAIRO-RS VERSION ++ except: ++ segment_utils.validate_segment_pointers( ++ segments=runner.segments, ++ segment_base_ptr=syscall_base_ptr, ++ segment_stop_ptr=syscall_stop_ptr, ++ ) ++ + syscall_handler.post_run(runner=runner, syscall_stop_ptr=syscall_stop_ptr) +diff --git a/src/starkware/starknet/core/os/segment_utils.py b/src/starkware/starknet/core/os/segment_utils.py +index 1d09414..33f5c26 100644 +--- a/src/starkware/starknet/core/os/segment_utils.py ++++ b/src/starkware/starknet/core/os/segment_utils.py +@@ -8,7 +8,7 @@ from starkware.starknet.definitions.error_codes import StarknetErrorCode + from starkware.starknet.public.abi import SYSCALL_PTR_OFFSET + from starkware.starkware_utils.error_handling import stark_assert, wrap_with_stark_exception + +- ++# LAMBDA MODIFIED + def get_os_segment_ptr_range( + runner: CairoFunctionRunner, ptr_offset: int, os_context: List[MaybeRelocatable] + ) -> Tuple[MaybeRelocatable, MaybeRelocatable]: +@@ -21,10 +21,23 @@ def get_os_segment_ptr_range( + ), f"Illegal OS ptr offset; must be one of: {allowed_offsets}." + + # The returned values are os_context, retdata_size, retdata_ptr. +- os_context_end = runner.vm.run_context.ap - 2 ++ # CAIRO-RS VERSION ++ try: ++ os_context_end = runner.get_ap() - 2 ++ except: ++ # ORIGINAL VERSION ++ os_context_end = runner.vm.run_context.ap - 2 ++ + final_os_context_ptr = os_context_end - len(os_context) + +- return os_context[ptr_offset], runner.vm_memory[final_os_context_ptr + ptr_offset] ++ # CAIRO-RS VERSION ++ try: ++ return os_context[ptr_offset], runner.get(final_os_context_ptr + ptr_offset) ++ # ORIGINAL VERSION ++ except: ++ return os_context[ptr_offset], runner.vm_memory[final_os_context_ptr + ptr_offset] ++ ++ + + + def get_os_segment_stop_ptr( +@@ -61,14 +74,19 @@ def validate_segment_pointers( + segment_base_ptr: MaybeRelocatable, + segment_stop_ptr: MaybeRelocatable, + ): +- assert isinstance(segment_base_ptr, RelocatableValue) ++ # assert isinstance(segment_base_ptr, RelocatableValue) + assert ( + segment_base_ptr.offset == 0 + ), f"Segment base pointer must be zero; got {segment_base_ptr.offset}." + +- expected_stop_ptr = segment_base_ptr + segments.get_segment_used_size( +- segment_index=segment_base_ptr.segment_index +- ) ++ # CAIRO-RS VERSION ++ try: ++ expected_stop_ptr = segment_base_ptr + segments.get_segment_used_size( ++ index=segment_base_ptr.segment_index) ++ # ORIGINAL VERSION ++ except: ++ expected_stop_ptr = segment_base_ptr + segments.get_segment_used_size( ++ segment_index=segment_base_ptr.segment_index) + + stark_assert( + expected_stop_ptr == segment_stop_ptr, +diff --git a/src/starkware/starknet/core/os/syscall_utils.py b/src/starkware/starknet/core/os/syscall_utils.py +index e44635b..c129e50 100644 +--- a/src/starkware/starknet/core/os/syscall_utils.py ++++ b/src/starkware/starknet/core/os/syscall_utils.py +@@ -458,7 +458,6 @@ class SysCallHandlerBase(ABC): + ) -> List[int]: + """ + Returns the call retdata. +- + syscall_name can be "call_contract", "delegate_call", "delegate_l1_handler", "library_call" + or "library_call_l1_handler". + """ +@@ -589,7 +588,16 @@ class BusinessLogicSysCallHandler(SysCallHandlerBase): + def _allocate_segment( + self, segments: MemorySegmentManager, data: Iterable[MaybeRelocatable] + ) -> RelocatableValue: +- segment_start = segments.add() ++ # FIXME: Here "segments" in really a Runner under the hood. ++ # May want to change the variable names. ++ ++ # CAIRO-RS VERSION ++ try: ++ segment_start = segments.add_segment() ++ # ORIGINAL VERSION ++ except: ++ segment_start = segments.add() ++ + segment_end = segments.write_arg(ptr=segment_start, arg=data) + self.read_only_segments.append((segment_start, segment_end - segment_start)) + return segment_start +@@ -631,10 +639,10 @@ class BusinessLogicSysCallHandler(SysCallHandlerBase): + args_struct_def: StructDefinition = syscall_info.syscall_request_struct.struct_definition_ + for arg, (arg_name, arg_def) in safe_zip(request, args_struct_def.members.items()): + expected_type = get_runtime_type(arg_def.cairo_type) +- assert isinstance(arg, expected_type), ( +- f"Argument {arg_name} to syscall {syscall_name} is of unexpected type. " +- f"Expected: value of type {expected_type}; got: {arg}." +- ) ++ # assert isinstance(arg, expected_type), ( ++ # f"Argument {arg_name} to syscall {syscall_name} is of unexpected type. " ++ # f"Expected: value of type {expected_type}; got: {arg}." ++ # ) + + return request + +@@ -902,10 +910,20 @@ class BusinessLogicSysCallHandler(SysCallHandlerBase): + Validates that there were no out of bounds writes to read-only segments and marks + them as accessed. + """ +- segments = runner.segments ++ # ORIGINAL VERSION ++ try: ++ segments = runner.segments ++ # CAIRO-RS VERSION ++ except: ++ segments = runner + + for segment_ptr, segment_size in self.read_only_segments: +- used_size = segments.get_segment_used_size(segment_index=segment_ptr.segment_index) ++ # CAIRO-RS VERSION ++ try: ++ used_size = segments.get_segment_used_size(index=segment_ptr.segment_index) ++ # ORIGINAL VERSION ++ except: ++ used_size = segments.get_segment_used_size(segment_index=segment_ptr.segment_index) + stark_assert( + used_size == segment_size, + code=StarknetErrorCode.SECURITY_ERROR, +@@ -1041,7 +1059,6 @@ class OsSysCallHandler(SysCallHandlerBase): + def start_tx(self, tx_info_ptr: RelocatableValue): + """ + Called when starting the execution of a transaction. +- + 'tx_info_ptr' is a pointer to the TxInfo struct corresponding to said transaction. + """ + assert self.tx_info_ptr is None +@@ -1089,4 +1106,4 @@ class OsSysCallHandler(SysCallHandlerBase): + Called when skipping the execution of a transaction. + It replaces a call to start_tx and end_tx. + """ +- next(self.tx_execution_info_iterator) ++ next(self.tx_execution_info_iterator) +\ No newline at end of file From d6f45ffc6667ed6fb97c1e1026ee5b9cdaaf67f5 Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 29 Nov 2022 13:10:18 -0300 Subject: [PATCH 03/57] Draft steps on Makefile --- Makefile | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index c85791ed..6c48e079 100644 --- a/Makefile +++ b/Makefile @@ -100,4 +100,16 @@ compare_trace: $(CAIRO_RS_TRACE) $(CAIRO_TRACE) compare_memory: $(CAIRO_RS_MEM) $(CAIRO_MEM) cd tests; ./compare_vm_state.sh memory - +compare_memory_devnet: +# Set up the virtual envs with the patched starknet files + scripts/memory_comparator/build_envs.sh +# Clone the starknet-devnet from github +# Set up the starknet-devnet in each env +# Create the folder where we will store the memory outputs +# Run each test one by one in each env and run the memory comparator +# Notes: +# Steps: +# 1- Run test in both envs +# 2- Run memory comparator +# 3- Cleanup memory files +# Repeat for each non-failing test From 2f9e06002475abace85c54a52a25ceda97cf34bb Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 29 Nov 2022 13:16:56 -0300 Subject: [PATCH 04/57] Remove duplicate patch files --- .../patch-memory-cairo-lang.patch | 51 -- .../patch-memory-cairo-rs-py.patch | 521 ------------------ 2 files changed, 572 deletions(-) delete mode 100644 scripts/memory_comparator/patch-memory-cairo-lang.patch delete mode 100644 scripts/memory_comparator/patch-memory-cairo-rs-py.patch diff --git a/scripts/memory_comparator/patch-memory-cairo-lang.patch b/scripts/memory_comparator/patch-memory-cairo-lang.patch deleted file mode 100644 index 95d26179..00000000 --- a/scripts/memory_comparator/patch-memory-cairo-lang.patch +++ /dev/null @@ -1,51 +0,0 @@ -diff --git a/src/starkware/starknet/business_logic/execution/execute_entry_point.py b/src/starkware/starknet/business_logic/execution/execute_entry_point.py -index 62d4290..f4a0d8b 100644 ---- a/src/starkware/starknet/business_logic/execution/execute_entry_point.py -+++ b/src/starkware/starknet/business_logic/execution/execute_entry_point.py -@@ -9,6 +9,8 @@ from starkware.cairo.lang.vm.relocatable import RelocatableValue - from starkware.cairo.lang.vm.security import SecurityError - from starkware.cairo.lang.vm.utils import ResourcesError - from starkware.cairo.lang.vm.vm_exceptions import HintException, VmException, VmExceptionBase -+from starkware.cairo.lang.vm.cairo_run import write_binary_memory -+import math - from starkware.starknet.business_logic.execution.execute_entry_point_base import ( - ExecuteEntryPointBase, - ) -@@ -274,6 +276,10 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): - assert isinstance(args_ptr, RelocatableValue) # Downcast. - runner.mark_as_accessed(address=args_ptr, size=len(entry_points_args)) - -+ f = open("memory_files/execute_entry_point.memory", "wb") -+ field_bytes = math.ceil(contract_class.program.prime.bit_length() / 8) -+ runner.relocate() -+ write_binary_memory(f, runner.relocated_memory, field_bytes) - return runner, syscall_handler - - def _get_selected_entry_point( -diff --git a/src/starkware/starknet/core/os/class_hash.py b/src/starkware/starknet/core/os/class_hash.py -index 132fb21..321c63a 100644 ---- a/src/starkware/starknet/core/os/class_hash.py -+++ b/src/starkware/starknet/core/os/class_hash.py -@@ -3,9 +3,11 @@ import dataclasses - import itertools - import json - import os -+import math - from contextvars import ContextVar - from functools import lru_cache - from typing import Callable, List, Optional -+from starkware.cairo.lang.vm.cairo_run import write_binary_memory - - import cachetools - -@@ -92,6 +94,10 @@ def compute_class_hash_inner( - use_full_name=True, - verify_secure=False, - ) -+ f = open("memory_files/class_hash.memory", "wb") -+ field_bytes = math.ceil(program.prime.bit_length() / 8) -+ runner.relocate() -+ write_binary_memory(f, runner.relocated_memory, field_bytes) - _, class_hash = runner.get_return_values(2) - return class_hash - diff --git a/scripts/memory_comparator/patch-memory-cairo-rs-py.patch b/scripts/memory_comparator/patch-memory-cairo-rs-py.patch deleted file mode 100644 index d2ae4aed..00000000 --- a/scripts/memory_comparator/patch-memory-cairo-rs-py.patch +++ /dev/null @@ -1,521 +0,0 @@ -diff --git a/src/starkware/starknet/business_logic/execution/execute_entry_point.py b/src/starkware/starknet/business_logic/execution/execute_entry_point.py -index 62d4290..c3cdd7e 100644 ---- a/src/starkware/starknet/business_logic/execution/execute_entry_point.py -+++ b/src/starkware/starknet/business_logic/execution/execute_entry_point.py -@@ -41,6 +41,7 @@ from starkware.starkware_utils.error_handling import ( - stark_assert, - wrap_with_stark_exception, - ) -+import cairo_rs_py - - logger = logging.getLogger(__name__) - -@@ -182,7 +183,8 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): - - # Run the specified contract entry point with given calldata. - with wrap_with_stark_exception(code=StarknetErrorCode.SECURITY_ERROR): -- runner = CairoFunctionRunner(program=contract_class.program, layout="all") -+ runner = cairo_rs_py.CairoRunner(program=contract_class.program.dumps(), entrypoint=None, layout="all", proof_mode=False) -+ runner.initialize_function_runner() - os_context = os_utils.prepare_os_context(runner=runner) - - validate_contract_deployed(state=state, contract_address=self.contract_address) -@@ -205,24 +207,24 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): - os_context, - len(self.calldata), - # Allocate and mark the segment as read-only (to mark every input array as read-only). -- syscall_handler._allocate_segment(segments=runner.segments, data=self.calldata), -+ syscall_handler._allocate_segment(segments=runner, data=self.calldata), - ] - - try: - runner.run_from_entrypoint( - entry_point.offset, -- *entry_points_args, -+ entry_points_args, - hint_locals={ - "syscall_handler": syscall_handler, - }, -- static_locals={ -- "__find_element_max_size": 2**20, -- "__squash_dict_max_size": 2**20, -- "__keccak_max_size": 2**20, -- "__usort_max_size": 2**20, -- "__chained_ec_op_max_len": 1000, -- }, -- run_resources=tx_execution_context.run_resources, -+ # static_locals={ -+ # "__find_element_max_size": 2**20, -+ # "__squash_dict_max_size": 2**20, -+ # "__keccak_max_size": 2**20, -+ # "__usort_max_size": 2**20, -+ # "__chained_ec_op_max_len": 1000, -+ # }, -+ # run_resources=tx_execution_context.run_resources, - verify_secure=True, - ) - except VmException as exception: -@@ -267,13 +269,14 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): - ) - - # When execution starts the stack holds entry_points_args + [ret_fp, ret_pc]. -- args_ptr = runner.initial_fp - (len(entry_points_args) + 2) -+ args_ptr = runner.get_initial_fp() - (len(entry_points_args) + 2) - - # The arguments are touched by the OS and should not be counted as holes, mark them - # as accessed. -- assert isinstance(args_ptr, RelocatableValue) # Downcast. -+ # assert isinstance(args_ptr, RelocatableValue) # Downcast. - runner.mark_as_accessed(address=args_ptr, size=len(entry_points_args)) -- -+ runner.relocate() -+ runner.write_binary_memory("memory_files/execute_entry_point.rs.memory") - return runner, syscall_handler - - def _get_selected_entry_point( -@@ -355,4 +358,4 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): - raise NotImplementedError(f"Call type {self.call_type} not implemented.") - - # Extract pre-fetched contract code from carried state. -- return get_deployed_class_hash_at_address(state=state, contract_address=code_address) -+ return get_deployed_class_hash_at_address(state=state, contract_address=code_address) -\ No newline at end of file -diff --git a/src/starkware/starknet/business_logic/transaction/fee.py b/src/starkware/starknet/business_logic/transaction/fee.py -index 9acaa73..9bbb9f5 100644 ---- a/src/starkware/starknet/business_logic/transaction/fee.py -+++ b/src/starkware/starknet/business_logic/transaction/fee.py -@@ -67,9 +67,9 @@ def calculate_l1_gas_by_cairo_usage( - """ - cairo_resource_fee_weights = general_config.cairo_resource_fee_weights - cairo_resource_names = set(cairo_resource_usage.keys()) -- assert cairo_resource_names.issubset( -- cairo_resource_fee_weights.keys() -- ), "Cairo resource names must be contained in fee weights dict." -+ # assert cairo_resource_names.issubset( -+ # cairo_resource_fee_weights.keys() -+ # ), "Cairo resource names must be contained in fee weights dict." - - # Convert Cairo usage to L1 gas usage. - cairo_l1_gas_usage = max( -diff --git a/src/starkware/starknet/business_logic/utils.py b/src/starkware/starknet/business_logic/utils.py -index f63bc9f..15660fe 100644 ---- a/src/starkware/starknet/business_logic/utils.py -+++ b/src/starkware/starknet/business_logic/utils.py -@@ -48,7 +48,14 @@ def get_return_values(runner: CairoFunctionRunner) -> List[int]: - exception_types=[Exception], - ): - ret_data_size, ret_data_ptr = runner.get_return_values(2) -- values = runner.memory.get_range(ret_data_ptr, ret_data_size) -+ -+ try: -+ # CAIRO-RS VERSION -+ values = runner.get_range(ret_data_ptr, ret_data_size) -+ except: -+ # ORIGINAL VERSION -+ values = runner.memory.get_range(ret_data_ptr, ret_data_size) -+ - - stark_assert( - all(isinstance(value, int) for value in values), -diff --git a/src/starkware/starknet/core/os/class_hash.py b/src/starkware/starknet/core/os/class_hash.py -index 132fb21..3cf48a8 100644 ---- a/src/starkware/starknet/core/os/class_hash.py -+++ b/src/starkware/starknet/core/os/class_hash.py -@@ -5,9 +5,10 @@ import json - import os - from contextvars import ContextVar - from functools import lru_cache --from typing import Callable, List, Optional -+from typing import Any, Callable, Dict, List, Optional, Tuple - - import cachetools -+import cairo_rs_py - - from starkware.cairo.common.cairo_function_runner import CairoFunctionRunner - from starkware.cairo.common.structs import CairoStructFactory, CairoStructProxy -@@ -23,6 +24,10 @@ from starkware.cairo.lang.vm.crypto import pedersen_hash - from starkware.python.utils import from_bytes - from starkware.starknet.public.abi import starknet_keccak - from starkware.starknet.services.api.contract_class import ContractClass, EntryPointType -+# Added Imports -+from starkware.cairo.lang.vm.relocatable import MaybeRelocatable, RelocatableValue -+from starkware.cairo.lang.vm.vm_exceptions import SecurityError, VmException -+from starkware.python.utils import safe_zip - - CAIRO_FILE = os.path.join(os.path.dirname(__file__), "contracts.cairo") - -@@ -77,22 +82,24 @@ def compute_class_hash_inner( - contract_class_struct = get_contract_class_struct( - identifiers=program.identifiers, contract_class=contract_class - ) -- runner = CairoFunctionRunner(program) - -- hash_builtin = HashBuiltinRunner( -- name="custom_hasher", included=True, ratio=32, hash_func=hash_func -- ) -- runner.builtin_runners["hash_builtin"] = hash_builtin -- hash_builtin.initialize_segments(runner) -+ runner = cairo_rs_py.CairoRunner(program=program.dumps(), entrypoint=None, layout="all", proof_mode=False) -+ runner.initialize_function_runner() -+ hash_ptr = runner.add_additional_hash_builtin() -+ - -- runner.run( -+ run_function_runner( -+ runner, -+ program, - "starkware.starknet.core.os.contracts.class_hash", -- hash_ptr=hash_builtin.base, -+ hash_ptr=hash_ptr, - contract_class=contract_class_struct, - use_full_name=True, - verify_secure=False, - ) - _, class_hash = runner.get_return_values(2) -+ runner.relocate() -+ runner.write_binary_memory("memory_files/class_hash.rs.memory") - return class_hash - - -@@ -194,3 +201,103 @@ def get_contract_class_struct( - bytecode_length=len(contract_class.program.data), - bytecode_ptr=contract_class.program.data, - ) -+ -+def run_function_runner( -+ runner, -+ program, -+ func_name: str, -+ *args, -+ hint_locals: Optional[Dict[str, Any]] = None, -+ static_locals: Optional[Dict[str, Any]] = None, -+ verify_secure: Optional[bool] = None, -+ trace_on_failure: bool = False, -+ apply_modulo_to_args: Optional[bool] = None, -+ use_full_name: bool = False, -+ verify_implicit_args_segment: bool = False, -+ **kwargs, -+ ) -> Tuple[Tuple[MaybeRelocatable, ...], Tuple[MaybeRelocatable, ...]]: -+ """ -+ Runs func_name(*args). -+ args are converted to Cairo-friendly ones using gen_arg. -+ -+ Returns the return values of the function, splitted into 2 tuples of implicit values and -+ explicit values. Structs will be flattened to a sequence of felts as part of the returned -+ tuple. -+ -+ Additional params: -+ verify_secure - Run verify_secure_runner to do extra verifications. -+ trace_on_failure - Run the tracer in case of failure to help debugging. -+ apply_modulo_to_args - Apply modulo operation on integer arguments. -+ use_full_name - Treat 'func_name' as a fully qualified identifier name, rather than a -+ relative one. -+ verify_implicit_args_segment - For each implicit argument, verify that the argument and the -+ return value are in the same segment. -+ """ -+ assert isinstance(program, Program) -+ entrypoint = program.get_label(func_name, full_name_lookup=use_full_name) -+ -+ #Construct Fu -+ structs_factory = CairoStructFactory.from_program(program=program) -+ func = ScopedName.from_string(scope=func_name) -+ -+ full_args_struct = structs_factory.build_func_args(func=func) -+ all_args = full_args_struct(*args, **kwargs) -+ -+ try: -+ runner.run_from_entrypoint( -+ entrypoint, -+ all_args, -+ typed_args=True, -+ hint_locals=hint_locals, -+ static_locals=static_locals, -+ verify_secure=verify_secure, -+ apply_modulo_to_args=apply_modulo_to_args, -+ ) -+ except (VmException, SecurityError, AssertionError) as ex: -+ if trace_on_failure: -+ print( -+ f"""\ -+Got {type(ex).__name__} exception during the execution of {func_name}: -+{str(ex)} -+""" -+ ) -+ #trace_runner(runner=runner) -+ raise -+ -+ # The number of implicit arguments is identical to the number of implicit return values. -+ n_implicit_ret_vals = structs_factory.get_implicit_args_length(func=func) -+ n_explicit_ret_vals = structs_factory.get_explicit_return_values_length(func=func) -+ n_ret_vals = n_explicit_ret_vals + n_implicit_ret_vals -+ implicit_retvals = tuple( -+ runner.get_range( -+ runner.get_ap() - n_ret_vals, n_implicit_ret_vals -+ ) -+ ) -+ -+ explicit_retvals = tuple( -+ runner.get_range( -+ runner.get_ap() - n_explicit_ret_vals, n_explicit_ret_vals -+ ) -+ ) -+ -+ # Verify the memory segments of the implicit arguments. -+ if verify_implicit_args_segment: -+ implicit_args = all_args[:n_implicit_ret_vals] -+ for implicit_arg, implicit_retval in safe_zip(implicit_args, implicit_retvals): -+ assert isinstance( -+ implicit_arg, RelocatableValue -+ ), f"Implicit arguments must be RelocatableValues, {implicit_arg} is not." -+ assert isinstance(implicit_retval, RelocatableValue), ( -+ f"Argument {implicit_arg} is a RelocatableValue, but the returned value " -+ f"{implicit_retval} is not." -+ ) -+ assert implicit_arg.segment_index == implicit_retval.segment_index, ( -+ f"Implicit argument {implicit_arg} is not on the same segment as the returned " -+ f"{implicit_retval}." -+ ) -+ assert implicit_retval.offset >= implicit_arg.offset, ( -+ f"The offset of the returned implicit argument {implicit_retval} is less than " -+ f"the offset of the input {implicit_arg}." -+ ) -+ -+ return implicit_retvals, explicit_retvals -diff --git a/src/starkware/starknet/core/os/os_utils.py b/src/starkware/starknet/core/os/os_utils.py -index 20bd521..0ea99f4 100644 ---- a/src/starkware/starknet/core/os/os_utils.py -+++ b/src/starkware/starknet/core/os/os_utils.py -@@ -43,18 +43,23 @@ def update_builtin_pointers( - - return return_builtins - -- - def prepare_os_context(runner: CairoFunctionRunner) -> List[MaybeRelocatable]: -- syscall_segment = runner.segments.add() -- os_context: List[MaybeRelocatable] = [syscall_segment] -- -- for builtin in runner.program.builtins: -- builtin_runner = runner.builtin_runners[f"{builtin}_builtin"] -- os_context.extend(builtin_runner.initial_stack()) -+ # CAIRO-RS VERSION -+ try: -+ syscall_segment = runner.add_segment() -+ os_context: List[MaybeRelocatable] = [syscall_segment] -+ os_context.extend(runner.get_program_builtins_initial_stack()) -+ # ORIGINAL VERSION -+ except: -+ syscall_segment = runner.segments.add() -+ os_context: List[MaybeRelocatable] = [syscall_segment] -+ -+ for builtin in runner.program.builtins: -+ builtin_runner = runner.builtin_runners[f"{builtin}_builtin"] -+ os_context.extend(builtin_runner.initial_stack()) - - return os_context - -- - def validate_and_process_os_context( - runner: CairoFunctionRunner, - syscall_handler: syscall_utils.BusinessLogicSysCallHandler, -@@ -64,14 +69,23 @@ def validate_and_process_os_context( - Validates and processes an OS context that was returned by a transaction. - Returns the syscall processor object containing the accumulated syscall information. - """ -- # The returned values are os_context, retdata_size, retdata_ptr. -- os_context_end = runner.vm.run_context.ap - 2 -- stack_ptr = os_context_end -- for builtin in runner.program.builtins[::-1]: -- builtin_runner = runner.builtin_runners[f"{builtin}_builtin"] -+ # CAIRO-RS VERSION -+ try: -+ os_context_end = runner.get_ap() - 2 -+ stack_ptr = os_context_end -+ # The returned values are os_context, retdata_size, retdata_ptr. -+ stack_ptr = runner.get_builtins_final_stack(stack_ptr) -+ # ORIGINAL VERSION -+ except: -+ os_context_end = runner.vm.run_context.ap - 2 -+ -+ stack_ptr = os_context_end - -- with wrap_with_stark_exception(code=StarknetErrorCode.SECURITY_ERROR): -- stack_ptr = builtin_runner.final_stack(runner=runner, pointer=stack_ptr) -+ for builtin in runner.program.builtins[::-1]: -+ builtin_runner = runner.builtin_runners[f"{builtin}_builtin"] -+ -+ with wrap_with_stark_exception(code=StarknetErrorCode.SECURITY_ERROR): -+ stack_ptr = builtin_runner.final_stack(runner=runner, pointer=stack_ptr) - - final_os_context_ptr = stack_ptr - 1 - assert final_os_context_ptr + len(initial_os_context) == os_context_end -@@ -81,9 +95,19 @@ def validate_and_process_os_context( - runner=runner, ptr_offset=SYSCALL_PTR_OFFSET, os_context=initial_os_context - ) - -- segment_utils.validate_segment_pointers( -- segments=runner.segments, -- segment_base_ptr=syscall_base_ptr, -- segment_stop_ptr=syscall_stop_ptr, -- ) -+ # ORIGINAL VERSION -+ try: -+ segment_utils.validate_segment_pointers( -+ segments=runner, -+ segment_base_ptr=syscall_base_ptr, -+ segment_stop_ptr=syscall_stop_ptr, -+ ) -+ # CAIRO-RS VERSION -+ except: -+ segment_utils.validate_segment_pointers( -+ segments=runner.segments, -+ segment_base_ptr=syscall_base_ptr, -+ segment_stop_ptr=syscall_stop_ptr, -+ ) -+ - syscall_handler.post_run(runner=runner, syscall_stop_ptr=syscall_stop_ptr) -diff --git a/src/starkware/starknet/core/os/segment_utils.py b/src/starkware/starknet/core/os/segment_utils.py -index 1d09414..33f5c26 100644 ---- a/src/starkware/starknet/core/os/segment_utils.py -+++ b/src/starkware/starknet/core/os/segment_utils.py -@@ -8,7 +8,7 @@ from starkware.starknet.definitions.error_codes import StarknetErrorCode - from starkware.starknet.public.abi import SYSCALL_PTR_OFFSET - from starkware.starkware_utils.error_handling import stark_assert, wrap_with_stark_exception - -- -+# LAMBDA MODIFIED - def get_os_segment_ptr_range( - runner: CairoFunctionRunner, ptr_offset: int, os_context: List[MaybeRelocatable] - ) -> Tuple[MaybeRelocatable, MaybeRelocatable]: -@@ -21,10 +21,23 @@ def get_os_segment_ptr_range( - ), f"Illegal OS ptr offset; must be one of: {allowed_offsets}." - - # The returned values are os_context, retdata_size, retdata_ptr. -- os_context_end = runner.vm.run_context.ap - 2 -+ # CAIRO-RS VERSION -+ try: -+ os_context_end = runner.get_ap() - 2 -+ except: -+ # ORIGINAL VERSION -+ os_context_end = runner.vm.run_context.ap - 2 -+ - final_os_context_ptr = os_context_end - len(os_context) - -- return os_context[ptr_offset], runner.vm_memory[final_os_context_ptr + ptr_offset] -+ # CAIRO-RS VERSION -+ try: -+ return os_context[ptr_offset], runner.get(final_os_context_ptr + ptr_offset) -+ # ORIGINAL VERSION -+ except: -+ return os_context[ptr_offset], runner.vm_memory[final_os_context_ptr + ptr_offset] -+ -+ - - - def get_os_segment_stop_ptr( -@@ -61,14 +74,19 @@ def validate_segment_pointers( - segment_base_ptr: MaybeRelocatable, - segment_stop_ptr: MaybeRelocatable, - ): -- assert isinstance(segment_base_ptr, RelocatableValue) -+ # assert isinstance(segment_base_ptr, RelocatableValue) - assert ( - segment_base_ptr.offset == 0 - ), f"Segment base pointer must be zero; got {segment_base_ptr.offset}." - -- expected_stop_ptr = segment_base_ptr + segments.get_segment_used_size( -- segment_index=segment_base_ptr.segment_index -- ) -+ # CAIRO-RS VERSION -+ try: -+ expected_stop_ptr = segment_base_ptr + segments.get_segment_used_size( -+ index=segment_base_ptr.segment_index) -+ # ORIGINAL VERSION -+ except: -+ expected_stop_ptr = segment_base_ptr + segments.get_segment_used_size( -+ segment_index=segment_base_ptr.segment_index) - - stark_assert( - expected_stop_ptr == segment_stop_ptr, -diff --git a/src/starkware/starknet/core/os/syscall_utils.py b/src/starkware/starknet/core/os/syscall_utils.py -index e44635b..c129e50 100644 ---- a/src/starkware/starknet/core/os/syscall_utils.py -+++ b/src/starkware/starknet/core/os/syscall_utils.py -@@ -458,7 +458,6 @@ class SysCallHandlerBase(ABC): - ) -> List[int]: - """ - Returns the call retdata. -- - syscall_name can be "call_contract", "delegate_call", "delegate_l1_handler", "library_call" - or "library_call_l1_handler". - """ -@@ -589,7 +588,16 @@ class BusinessLogicSysCallHandler(SysCallHandlerBase): - def _allocate_segment( - self, segments: MemorySegmentManager, data: Iterable[MaybeRelocatable] - ) -> RelocatableValue: -- segment_start = segments.add() -+ # FIXME: Here "segments" in really a Runner under the hood. -+ # May want to change the variable names. -+ -+ # CAIRO-RS VERSION -+ try: -+ segment_start = segments.add_segment() -+ # ORIGINAL VERSION -+ except: -+ segment_start = segments.add() -+ - segment_end = segments.write_arg(ptr=segment_start, arg=data) - self.read_only_segments.append((segment_start, segment_end - segment_start)) - return segment_start -@@ -631,10 +639,10 @@ class BusinessLogicSysCallHandler(SysCallHandlerBase): - args_struct_def: StructDefinition = syscall_info.syscall_request_struct.struct_definition_ - for arg, (arg_name, arg_def) in safe_zip(request, args_struct_def.members.items()): - expected_type = get_runtime_type(arg_def.cairo_type) -- assert isinstance(arg, expected_type), ( -- f"Argument {arg_name} to syscall {syscall_name} is of unexpected type. " -- f"Expected: value of type {expected_type}; got: {arg}." -- ) -+ # assert isinstance(arg, expected_type), ( -+ # f"Argument {arg_name} to syscall {syscall_name} is of unexpected type. " -+ # f"Expected: value of type {expected_type}; got: {arg}." -+ # ) - - return request - -@@ -902,10 +910,20 @@ class BusinessLogicSysCallHandler(SysCallHandlerBase): - Validates that there were no out of bounds writes to read-only segments and marks - them as accessed. - """ -- segments = runner.segments -+ # ORIGINAL VERSION -+ try: -+ segments = runner.segments -+ # CAIRO-RS VERSION -+ except: -+ segments = runner - - for segment_ptr, segment_size in self.read_only_segments: -- used_size = segments.get_segment_used_size(segment_index=segment_ptr.segment_index) -+ # CAIRO-RS VERSION -+ try: -+ used_size = segments.get_segment_used_size(index=segment_ptr.segment_index) -+ # ORIGINAL VERSION -+ except: -+ used_size = segments.get_segment_used_size(segment_index=segment_ptr.segment_index) - stark_assert( - used_size == segment_size, - code=StarknetErrorCode.SECURITY_ERROR, -@@ -1041,7 +1059,6 @@ class OsSysCallHandler(SysCallHandlerBase): - def start_tx(self, tx_info_ptr: RelocatableValue): - """ - Called when starting the execution of a transaction. -- - 'tx_info_ptr' is a pointer to the TxInfo struct corresponding to said transaction. - """ - assert self.tx_info_ptr is None -@@ -1089,4 +1106,4 @@ class OsSysCallHandler(SysCallHandlerBase): - Called when skipping the execution of a transaction. - It replaces a call to start_tx and end_tx. - """ -- next(self.tx_execution_info_iterator) -+ next(self.tx_execution_info_iterator) -\ No newline at end of file From 2a20de6f37d27a5d71e9a5628410be091bd08854 Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 29 Nov 2022 13:31:21 -0300 Subject: [PATCH 05/57] Add target to .PHONY --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 6c48e079..017776a6 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -.PHONY: deps deps-macos deps-default-version build run check test clippy clean run-python-test full-test run-comparer-tracer compare_trace_memory compare_trace compare_memory +.PHONY: deps deps-macos deps-default-version build run check test clippy clean run-python-test full-test run-comparer-tracer compare_trace_memory compare_trace compare_memory compare_memory_devnet TEST_DIR=cairo_programs TEST_FILES:=$(wildcard $(TEST_DIR)/*.cairo) From 33d1b1eeee914b4a96e8e71cde0736c67d7f9926 Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 29 Nov 2022 13:43:44 -0300 Subject: [PATCH 06/57] Fix paths in build_envs script --- scripts/memory_comparator/build_envs.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/scripts/memory_comparator/build_envs.sh b/scripts/memory_comparator/build_envs.sh index 7b262db3..c8f0d05f 100755 --- a/scripts/memory_comparator/build_envs.sh +++ b/scripts/memory_comparator/build_envs.sh @@ -5,15 +5,15 @@ set -o pipefail # This is not reaaaaally a robust way to find it, but you need to be actively # trying to break it for this to fail :) -SCRIPT_DIR=$(dirname $0) +SCRIPT_DIR="scripts/memory_comparator" python3.9 -m venv --upgrade-deps ${SCRIPT_DIR}/cairo-lang ${SCRIPT_DIR}/cairo-rs-py ${SCRIPT_DIR}/cairo-lang/bin/pip install cairo-lang==0.10.1 ${SCRIPT_DIR}/cairo-rs-py/bin/pip install maturin==0.14.1 cairo-lang==0.10.1 -${SCRIPT_DIR}/cairo-rs-py/bin/maturin build --manifest-path ${SCRIPT_DIR}/../Cargo.toml --release --strip --interpreter 3.9 --no-default-features --features extension -${SCRIPT_DIR}/cairo-rs-py/bin/pip install ${SCRIPT_DIR}/../target/wheels/cairo_rs_py-*.whl +${SCRIPT_DIR}/cairo-rs-py/bin/maturin build --manifest-path Cargo.toml --release --strip --interpreter 3.9 --no-default-features --features extension +${SCRIPT_DIR}/cairo-rs-py/bin/pip install target/wheels/cairo_rs_py-*.whl patch --directory ${SCRIPT_DIR}/cairo-rs-py/lib/python3.9/site-packages/ --strip 2 < ${SCRIPT_DIR}/output-memory-cairo-rs-py.patch -patch --directory ${SCRIPT_DIR}/cairo-langlib/python3.9/site-packages/ --strip 2 < ${SCRIPT_DIR}/output-memory-cairo-lang.patch +patch --directory ${SCRIPT_DIR}/cairo-lang/lib/python3.9/site-packages/ --strip 2 < ${SCRIPT_DIR}/output-memory-cairo-lang.patch ${SCRIPT_DIR}/cairo-rs-py/bin/cairo-run --version ${SCRIPT_DIR}/cairo-rs-py/bin/starknet --version From 876b8d23183a9627afbbf0274f92d591bf0cb96b Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 29 Nov 2022 13:56:05 -0300 Subject: [PATCH 07/57] Add starknet setup --- Makefile | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Makefile b/Makefile index 017776a6..7500aa1f 100644 --- a/Makefile +++ b/Makefile @@ -104,7 +104,16 @@ compare_memory_devnet: # Set up the virtual envs with the patched starknet files scripts/memory_comparator/build_envs.sh # Clone the starknet-devnet from github + git clone git@github.com:Shard-Labs/starknet-devnet.git # Set up the starknet-devnet in each env +# cairo-rs-py + . scripts/memory_comparator/cairo-rs-py/bin/activate && \ + pip install starknet-devnet && \ + cd starknet-devnet; scripts/install_dev_tools.sh +# cairo-lang + . scripts/memory_comparator/cairo-lang/bin/activate && \ + pip install starknet-devnet && \ + cd starknet-devnet; scripts/install_dev_tools.sh # Create the folder where we will store the memory outputs # Run each test one by one in each env and run the memory comparator # Notes: From 5d6d67802237a050f98f40a0db9b06312826d97b Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 29 Nov 2022 13:59:49 -0300 Subject: [PATCH 08/57] Add more setup --- Makefile | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Makefile b/Makefile index 7500aa1f..ebcfc789 100644 --- a/Makefile +++ b/Makefile @@ -115,6 +115,10 @@ compare_memory_devnet: pip install starknet-devnet && \ cd starknet-devnet; scripts/install_dev_tools.sh # Create the folder where we will store the memory outputs + mkdir starknet-devnet/memory_files +# Compile test files + . scripts/memory_comparator/cairo-lang/bin/activate && \ + cd starknet-devnet; scripts/compile_contracts.sh # Run each test one by one in each env and run the memory comparator # Notes: # Steps: From f1373b6bbc0b4ab002578a65f7352ca4a50e0381 Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 29 Nov 2022 14:09:40 -0300 Subject: [PATCH 09/57] Fix --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index ebcfc789..dee09609 100644 --- a/Makefile +++ b/Makefile @@ -115,7 +115,7 @@ compare_memory_devnet: pip install starknet-devnet && \ cd starknet-devnet; scripts/install_dev_tools.sh # Create the folder where we will store the memory outputs - mkdir starknet-devnet/memory_files + cd starknet-devnet; mkdir memory_files # Compile test files . scripts/memory_comparator/cairo-lang/bin/activate && \ cd starknet-devnet; scripts/compile_contracts.sh From 0ced51371f544dbec39a75b14fb4148b63eddfa2 Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 29 Nov 2022 14:41:22 -0300 Subject: [PATCH 10/57] Start run_tests_compare_memory script --- .../memory_comparator/run_tests_compare_memory.sh | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 scripts/memory_comparator/run_tests_compare_memory.sh diff --git a/scripts/memory_comparator/run_tests_compare_memory.sh b/scripts/memory_comparator/run_tests_compare_memory.sh new file mode 100644 index 00000000..fd1daf09 --- /dev/null +++ b/scripts/memory_comparator/run_tests_compare_memory.sh @@ -0,0 +1,14 @@ +#!/usr/bin/env sh +# Please run this script from cairo-rs-py directory +memory_outputs_path= "memory_files" +# We need to be inside starknet-devnet in order to run poetry +cd starknet-devnet +for file in test/test*.py; do + # Run tests in cairo-rs-py env + . scripts/memory_comparator/cairo-rs-py/bin/activate + poetry run pytest $file + # Run tests in cairo-lang env + . scripts/memory_comparator/cairo-rs-py/bin/activate + poetry run pytest $file + break +done \ No newline at end of file From 228f2d6121c49c6b3cea402882cebbeacc936995 Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 29 Nov 2022 15:11:58 -0300 Subject: [PATCH 11/57] Add files to make clean --- Makefile | 2 ++ scripts/memory_comparator/run_tests_compare_memory.sh | 10 ++++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index dee09609..8c73e244 100644 --- a/Makefile +++ b/Makefile @@ -75,6 +75,8 @@ clean: rm -f $(TEST_DIR)/*.memory rm -f $(TEST_DIR)/*.trace rm -rf cairo-rs-py-env + rm -rf starknet-devnet + rm -rf scripts/memory_comparator/cairo* run-python-test: $(COMPILED_TESTS) PYENV_VERSION=pypy3.7-7.3.9 . cairo-rs-py-env/bin/activate && \ diff --git a/scripts/memory_comparator/run_tests_compare_memory.sh b/scripts/memory_comparator/run_tests_compare_memory.sh index fd1daf09..1591dee9 100644 --- a/scripts/memory_comparator/run_tests_compare_memory.sh +++ b/scripts/memory_comparator/run_tests_compare_memory.sh @@ -1,14 +1,16 @@ #!/usr/bin/env sh # Please run this script from cairo-rs-py directory -memory_outputs_path= "memory_files" + # We need to be inside starknet-devnet in order to run poetry cd starknet-devnet for file in test/test*.py; do # Run tests in cairo-rs-py env - . scripts/memory_comparator/cairo-rs-py/bin/activate + . ../scripts/memory_comparator/cairo-rs-py/bin/activate poetry run pytest $file # Run tests in cairo-lang env - . scripts/memory_comparator/cairo-rs-py/bin/activate + . ../scripts/memory_comparator/cairo-rs-py/bin/activate poetry run pytest $file break -done \ No newline at end of file +done + +cd .. \ No newline at end of file From cf84748fa506de42dd78a3ba46c3c1333c3e0e58 Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 29 Nov 2022 16:35:00 -0300 Subject: [PATCH 12/57] Move env patching to makefile --- Makefile | 5 ++++- scripts/memory_comparator/build_envs.sh | 2 -- .../run_tests_compare_memory.sh | 19 ++++++++++++------- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index 8c73e244..5ac344f2 100644 --- a/Makefile +++ b/Makefile @@ -103,7 +103,7 @@ compare_memory: $(CAIRO_RS_MEM) $(CAIRO_MEM) cd tests; ./compare_vm_state.sh memory compare_memory_devnet: -# Set up the virtual envs with the patched starknet files +# Set up the virtual envs scripts/memory_comparator/build_envs.sh # Clone the starknet-devnet from github git clone git@github.com:Shard-Labs/starknet-devnet.git @@ -121,6 +121,9 @@ compare_memory_devnet: # Compile test files . scripts/memory_comparator/cairo-lang/bin/activate && \ cd starknet-devnet; scripts/compile_contracts.sh +# Patch both envs + patch --directory scripts/memory_comparator/cairo-rs-py/lib/python3.9/site-packages/ --strip 2 < scripts/memory_comparator/output-memory-cairo-rs-py.patch + patch --directory scripts/memory_comparator/cairo-lang/lib/python3.9/site-packages/ --strip 2 < scripts/memory_comparator/output-memory-cairo-lang.patch # Run each test one by one in each env and run the memory comparator # Notes: # Steps: diff --git a/scripts/memory_comparator/build_envs.sh b/scripts/memory_comparator/build_envs.sh index c8f0d05f..07af5c42 100755 --- a/scripts/memory_comparator/build_envs.sh +++ b/scripts/memory_comparator/build_envs.sh @@ -12,8 +12,6 @@ ${SCRIPT_DIR}/cairo-lang/bin/pip install cairo-lang==0.10.1 ${SCRIPT_DIR}/cairo-rs-py/bin/pip install maturin==0.14.1 cairo-lang==0.10.1 ${SCRIPT_DIR}/cairo-rs-py/bin/maturin build --manifest-path Cargo.toml --release --strip --interpreter 3.9 --no-default-features --features extension ${SCRIPT_DIR}/cairo-rs-py/bin/pip install target/wheels/cairo_rs_py-*.whl -patch --directory ${SCRIPT_DIR}/cairo-rs-py/lib/python3.9/site-packages/ --strip 2 < ${SCRIPT_DIR}/output-memory-cairo-rs-py.patch -patch --directory ${SCRIPT_DIR}/cairo-lang/lib/python3.9/site-packages/ --strip 2 < ${SCRIPT_DIR}/output-memory-cairo-lang.patch ${SCRIPT_DIR}/cairo-rs-py/bin/cairo-run --version ${SCRIPT_DIR}/cairo-rs-py/bin/starknet --version diff --git a/scripts/memory_comparator/run_tests_compare_memory.sh b/scripts/memory_comparator/run_tests_compare_memory.sh index 1591dee9..fde9e3ed 100644 --- a/scripts/memory_comparator/run_tests_compare_memory.sh +++ b/scripts/memory_comparator/run_tests_compare_memory.sh @@ -4,13 +4,18 @@ # We need to be inside starknet-devnet in order to run poetry cd starknet-devnet for file in test/test*.py; do - # Run tests in cairo-rs-py env - . ../scripts/memory_comparator/cairo-rs-py/bin/activate - poetry run pytest $file - # Run tests in cairo-lang env - . ../scripts/memory_comparator/cairo-rs-py/bin/activate - poetry run pytest $file - break + # Skip problematic files + if ! ([ "$file" = "test/test_account.py" ] || [ "$file" = "test/invalid_file.py" ]); then + # Run tests in cairo-rs-py env + . ../scripts/memory_comparator/cairo-rs-py/bin/activate + poetry run pytest $file + # Run tests in cairo-lang env + . ../scripts/memory_comparator/cairo-lang/bin/activate + poetry run pytest $file + break + # Cleanup memory files + rm memory_files/*.memory + fi done cd .. \ No newline at end of file From 64aa87172f10515d507a4d0e3814fee5cc7dc092 Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 29 Nov 2022 16:48:48 -0300 Subject: [PATCH 13/57] Add memory comparison to Makefile --- .../run_tests_compare_memory.sh | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/scripts/memory_comparator/run_tests_compare_memory.sh b/scripts/memory_comparator/run_tests_compare_memory.sh index fde9e3ed..231c204a 100644 --- a/scripts/memory_comparator/run_tests_compare_memory.sh +++ b/scripts/memory_comparator/run_tests_compare_memory.sh @@ -1,21 +1,31 @@ #!/usr/bin/env sh # Please run this script from cairo-rs-py directory - +exit_code=0 # We need to be inside starknet-devnet in order to run poetry cd starknet-devnet for file in test/test*.py; do # Skip problematic files - if ! ([ "$file" = "test/test_account.py" ] || [ "$file" = "test/invalid_file.py" ]); then + if ! ([ "$file" = "test/test_account.py" ] || [ "$file" = "test/test_estimate_fee.py" ] || ["$file" = "test/test_rpc_estimate_fee.py"]); then # Run tests in cairo-rs-py env . ../scripts/memory_comparator/cairo-rs-py/bin/activate poetry run pytest $file # Run tests in cairo-lang env . ../scripts/memory_comparator/cairo-lang/bin/activate poetry run pytest $file - break + # Compare memory outputs + class_hash_path="memory_files/class_hash" + execute_entry_point_path="memory_files/execute_entry_point" + memory_comparator_path="../tests/memory_comparator.py" + if ! $memory_comparator_path $class_hash_path.memory $class_hash_path.rs.memory; then + echo "Memory differs for last class_hash on test $file" + exit_code=1 + fi + if ! $memory_comparator_path $execute_entry_point_path.memory $execute_entry_point_path.rs.memory; then + echo "Memory differs for last execute_entry_point on test $file" + exit_code=1 + fi # Cleanup memory files rm memory_files/*.memory fi done - -cd .. \ No newline at end of file +cd .. From 7592c350fbf9241e0f06341547c74e4153fe3801 Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 29 Nov 2022 17:16:10 -0300 Subject: [PATCH 14/57] Finish script --- scripts/memory_comparator/run_tests_compare_memory.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/memory_comparator/run_tests_compare_memory.sh b/scripts/memory_comparator/run_tests_compare_memory.sh index 231c204a..296a6e30 100644 --- a/scripts/memory_comparator/run_tests_compare_memory.sh +++ b/scripts/memory_comparator/run_tests_compare_memory.sh @@ -5,7 +5,7 @@ exit_code=0 cd starknet-devnet for file in test/test*.py; do # Skip problematic files - if ! ([ "$file" = "test/test_account.py" ] || [ "$file" = "test/test_estimate_fee.py" ] || ["$file" = "test/test_rpc_estimate_fee.py"]); then + if ! ([ "$file" = "test/test_account.py" ] || [ "$file" = "test/test_estimate_fee.py" ] || ["$file" = "test/test_rpc_estimate_fee.py"] || ["$file" = "test/test_fee_token.py"] || ["$file" = "test/test_postman.py"] || || ["$file" = "test/testnet_deployment.py"]); then # Run tests in cairo-rs-py env . ../scripts/memory_comparator/cairo-rs-py/bin/activate poetry run pytest $file @@ -28,4 +28,4 @@ for file in test/test*.py; do rm memory_files/*.memory fi done -cd .. +exit "${exit_code}" From c48c3ff02aca0682f08c762a91679d0ce50d9587 Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 29 Nov 2022 17:18:45 -0300 Subject: [PATCH 15/57] Complete makefile target --- Makefile | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 5ac344f2..71e94983 100644 --- a/Makefile +++ b/Makefile @@ -125,9 +125,4 @@ compare_memory_devnet: patch --directory scripts/memory_comparator/cairo-rs-py/lib/python3.9/site-packages/ --strip 2 < scripts/memory_comparator/output-memory-cairo-rs-py.patch patch --directory scripts/memory_comparator/cairo-lang/lib/python3.9/site-packages/ --strip 2 < scripts/memory_comparator/output-memory-cairo-lang.patch # Run each test one by one in each env and run the memory comparator -# Notes: -# Steps: -# 1- Run test in both envs -# 2- Run memory comparator -# 3- Cleanup memory files -# Repeat for each non-failing test + ./scripts/memory_comparator/run_tests_compare_memory.sh From 0528a421a27c1adee6326ccea68d075a2c8bcc04 Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 29 Nov 2022 17:24:29 -0300 Subject: [PATCH 16/57] Add compare-devnet-memory workflow --- .github/workflows/compare_memory_devnet.yml | 30 +++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 .github/workflows/compare_memory_devnet.yml diff --git a/.github/workflows/compare_memory_devnet.yml b/.github/workflows/compare_memory_devnet.yml new file mode 100644 index 00000000..2e6f672c --- /dev/null +++ b/.github/workflows/compare_memory_devnet.yml @@ -0,0 +1,30 @@ +name: compare_devnet_memory + +on: + push: + branches: [ main ] + pull_request: + branches: [ '*' ] + +env: + CARGO_TERM_COLOR: always + + jobs: + compare_memory_devnet: + runs-on: ubuntu-20.04 + steps: + - name: Install Rust 1.61.0 + uses: actions-rs/toolchain@v1 + with: + toolchain: 1.61.0 + override: true + components: rustfmt, clippy + - uses: actions/checkout@v3 + - name: Setup Pyenv + uses: "gabrielfalcao/pyenv-action@v9" + with: + default: 3.9 + - name: Install test dependencies + run: make deps-default-version + - name: Run devnet tests & compare memory outputs + run: make compare_memory_devnet \ No newline at end of file From fc15bd1ef7b740890545f883062adecb89ccf931 Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 29 Nov 2022 17:28:42 -0300 Subject: [PATCH 17/57] Fix syntax --- Makefile | 2 +- scripts/memory_comparator/run_tests_compare_memory.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 71e94983..2cd48b0d 100644 --- a/Makefile +++ b/Makefile @@ -125,4 +125,4 @@ compare_memory_devnet: patch --directory scripts/memory_comparator/cairo-rs-py/lib/python3.9/site-packages/ --strip 2 < scripts/memory_comparator/output-memory-cairo-rs-py.patch patch --directory scripts/memory_comparator/cairo-lang/lib/python3.9/site-packages/ --strip 2 < scripts/memory_comparator/output-memory-cairo-lang.patch # Run each test one by one in each env and run the memory comparator - ./scripts/memory_comparator/run_tests_compare_memory.sh + . ./scripts/memory_comparator/run_tests_compare_memory.sh diff --git a/scripts/memory_comparator/run_tests_compare_memory.sh b/scripts/memory_comparator/run_tests_compare_memory.sh index 296a6e30..758285fb 100644 --- a/scripts/memory_comparator/run_tests_compare_memory.sh +++ b/scripts/memory_comparator/run_tests_compare_memory.sh @@ -5,7 +5,7 @@ exit_code=0 cd starknet-devnet for file in test/test*.py; do # Skip problematic files - if ! ([ "$file" = "test/test_account.py" ] || [ "$file" = "test/test_estimate_fee.py" ] || ["$file" = "test/test_rpc_estimate_fee.py"] || ["$file" = "test/test_fee_token.py"] || ["$file" = "test/test_postman.py"] || || ["$file" = "test/testnet_deployment.py"]); then + if ! ([ "$file" = "test/test_account.py" ] || [ "$file" = "test/test_estimate_fee.py" ] || ["$file" = "test/test_rpc_estimate_fee.py"] || ["$file" = "test/test_fee_token.py"] || ["$file" = "test/test_postman.py"] || ["$file" = "test/testnet_deployment.py"]); then # Run tests in cairo-rs-py env . ../scripts/memory_comparator/cairo-rs-py/bin/activate poetry run pytest $file From 1cbb0245b348302399b4960ef9e821b172e49419 Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 29 Nov 2022 17:30:41 -0300 Subject: [PATCH 18/57] Fix workflow file --- .github/workflows/compare_memory_devnet.yml | 39 ++++++++++----------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/.github/workflows/compare_memory_devnet.yml b/.github/workflows/compare_memory_devnet.yml index 2e6f672c..544a0f1f 100644 --- a/.github/workflows/compare_memory_devnet.yml +++ b/.github/workflows/compare_memory_devnet.yml @@ -8,23 +8,22 @@ on: env: CARGO_TERM_COLOR: always - - jobs: - compare_memory_devnet: - runs-on: ubuntu-20.04 - steps: - - name: Install Rust 1.61.0 - uses: actions-rs/toolchain@v1 - with: - toolchain: 1.61.0 - override: true - components: rustfmt, clippy - - uses: actions/checkout@v3 - - name: Setup Pyenv - uses: "gabrielfalcao/pyenv-action@v9" - with: - default: 3.9 - - name: Install test dependencies - run: make deps-default-version - - name: Run devnet tests & compare memory outputs - run: make compare_memory_devnet \ No newline at end of file +jobs: + compare_memory_devnet: + runs-on: ubuntu-20.04 + steps: + - name: Install Rust 1.61.0 + uses: actions-rs/toolchain@v1 + with: + toolchain: 1.61.0 + override: true + components: rustfmt, clippy + - uses: actions/checkout@v3 + - name: Setup Pyenv + uses: "gabrielfalcao/pyenv-action@v9" + with: + default: 3.9 + - name: Install test dependencies + run: make deps-default-version + - name: Run devnet tests & compare memory outputs + run: make compare_memory_devnet From 058f4378a64463d76a19f088d58c49eec0f071fd Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 29 Nov 2022 17:40:20 -0300 Subject: [PATCH 19/57] Clippy fix --- src/cairo_runner.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cairo_runner.rs b/src/cairo_runner.rs index a685bc93..de89b608 100644 --- a/src/cairo_runner.rs +++ b/src/cairo_runner.rs @@ -218,7 +218,7 @@ impl PyCairoRunner { } pub fn write_binary_memory(&mut self, name: String) -> PyResult<()> { - write_binary_memory(&self.inner.relocated_memory, &Path::new(&name)).map_err(to_py_error) + write_binary_memory(&self.inner.relocated_memory, Path::new(&name)).map_err(to_py_error) } pub fn add_segment(&self) -> PyRelocatable { From ca867620578466c4457b1cc1169060b0fdb761b2 Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 29 Nov 2022 17:43:56 -0300 Subject: [PATCH 20/57] Remove ilegal option --- scripts/memory_comparator/build_envs.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/scripts/memory_comparator/build_envs.sh b/scripts/memory_comparator/build_envs.sh index 07af5c42..81638062 100755 --- a/scripts/memory_comparator/build_envs.sh +++ b/scripts/memory_comparator/build_envs.sh @@ -1,7 +1,6 @@ #!/bin/sh set -e -set -o pipefail # This is not reaaaaally a robust way to find it, but you need to be actively # trying to break it for this to fail :) From cc2e8a050a3eb46520bab455be96becdf172692f Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 29 Nov 2022 17:59:07 -0300 Subject: [PATCH 21/57] Add python setup to workflow --- .github/workflows/compare_memory_devnet.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/compare_memory_devnet.yml b/.github/workflows/compare_memory_devnet.yml index 544a0f1f..62b416f4 100644 --- a/.github/workflows/compare_memory_devnet.yml +++ b/.github/workflows/compare_memory_devnet.yml @@ -19,11 +19,11 @@ jobs: override: true components: rustfmt, clippy - uses: actions/checkout@v3 - - name: Setup Pyenv - uses: "gabrielfalcao/pyenv-action@v9" + - name: Python3 Build + uses: actions/setup-python@v4 with: - default: 3.9 + python-version: '3.9' - name: Install test dependencies - run: make deps-default-version + run: pip install ecdsa fastecdsa sympy cairo-lang==0.9.1 maturin - name: Run devnet tests & compare memory outputs run: make compare_memory_devnet From 47043228f4a68c2183d3ba2df2694db9a6a6cfee Mon Sep 17 00:00:00 2001 From: Federica Date: Wed, 30 Nov 2022 14:49:34 -0300 Subject: [PATCH 22/57] Fix stuff --- .github/workflows/compare_memory_devnet.yml | 6 ++++- Makefile | 25 ++++++++++++++++++- .../run_tests_compare_memory.sh | 4 +-- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/.github/workflows/compare_memory_devnet.yml b/.github/workflows/compare_memory_devnet.yml index 62b416f4..12c5afd3 100644 --- a/.github/workflows/compare_memory_devnet.yml +++ b/.github/workflows/compare_memory_devnet.yml @@ -23,7 +23,11 @@ jobs: uses: actions/setup-python@v4 with: python-version: '3.9' + - name: Clone starknet-devnet + uses: actions/checkout@master + with: + repository: Shard-Labs/starknet-devnet - name: Install test dependencies run: pip install ecdsa fastecdsa sympy cairo-lang==0.9.1 maturin - name: Run devnet tests & compare memory outputs - run: make compare_memory_devnet + run: make compare_memory_devnet_ci diff --git a/Makefile b/Makefile index 2cd48b0d..6775bb82 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -.PHONY: deps deps-macos deps-default-version build run check test clippy clean run-python-test full-test run-comparer-tracer compare_trace_memory compare_trace compare_memory compare_memory_devnet +.PHONY: deps deps-macos deps-default-version build run check test clippy clean run-python-test full-test run-comparer-tracer compare_trace_memory compare_trace compare_memory compare_memory_devnet compare_memory_devnet_ci TEST_DIR=cairo_programs TEST_FILES:=$(wildcard $(TEST_DIR)/*.cairo) @@ -126,3 +126,26 @@ compare_memory_devnet: patch --directory scripts/memory_comparator/cairo-lang/lib/python3.9/site-packages/ --strip 2 < scripts/memory_comparator/output-memory-cairo-lang.patch # Run each test one by one in each env and run the memory comparator . ./scripts/memory_comparator/run_tests_compare_memory.sh + +compare_memory_devnet_ci: +# Set up the virtual envs + scripts/memory_comparator/build_envs.sh +# Set up the starknet-devnet in each env +# cairo-rs-py + . scripts/memory_comparator/cairo-rs-py/bin/activate && \ + pip install starknet-devnet && \ + cd starknet-devnet; scripts/install_dev_tools.sh +# cairo-lang + . scripts/memory_comparator/cairo-lang/bin/activate && \ + pip install starknet-devnet && \ + cd starknet-devnet; scripts/install_dev_tools.sh +# Create the folder where we will store the memory outputs + cd starknet-devnet; mkdir memory_files +# Compile test files + . scripts/memory_comparator/cairo-lang/bin/activate && \ + cd starknet-devnet; scripts/compile_contracts.sh +# Patch both envs + patch --directory scripts/memory_comparator/cairo-rs-py/lib/python3.9/site-packages/ --strip 2 < scripts/memory_comparator/output-memory-cairo-rs-py.patch + patch --directory scripts/memory_comparator/cairo-lang/lib/python3.9/site-packages/ --strip 2 < scripts/memory_comparator/output-memory-cairo-lang.patch +# Run each test one by one in each env and run the memory comparator + . ./scripts/memory_comparator/run_tests_compare_memory.sh diff --git a/scripts/memory_comparator/run_tests_compare_memory.sh b/scripts/memory_comparator/run_tests_compare_memory.sh index 758285fb..599c651a 100644 --- a/scripts/memory_comparator/run_tests_compare_memory.sh +++ b/scripts/memory_comparator/run_tests_compare_memory.sh @@ -3,9 +3,9 @@ exit_code=0 # We need to be inside starknet-devnet in order to run poetry cd starknet-devnet -for file in test/test*.py; do +for file in test/test_*.py; do # Skip problematic files - if ! ([ "$file" = "test/test_account.py" ] || [ "$file" = "test/test_estimate_fee.py" ] || ["$file" = "test/test_rpc_estimate_fee.py"] || ["$file" = "test/test_fee_token.py"] || ["$file" = "test/test_postman.py"] || ["$file" = "test/testnet_deployment.py"]); then + if ! ([ "$file" == "test/test_account.py" ] || [ "$file" == "test/test_estimate_fee.py" ] || ["$file" == "test/test_rpc_estimate_fee.py"] || ["$file" == "test/test_fee_token.py"] || ["$file" == "test/test_postman.py"] || ["$file" == "test/testnet_deployment.py"]); then # Run tests in cairo-rs-py env . ../scripts/memory_comparator/cairo-rs-py/bin/activate poetry run pytest $file From c33c942b08eddbb07918adfba0567a2c0d142b7b Mon Sep 17 00:00:00 2001 From: Federica Date: Wed, 30 Nov 2022 14:55:51 -0300 Subject: [PATCH 23/57] Fix workflow --- .github/workflows/compare_memory_devnet.yml | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/.github/workflows/compare_memory_devnet.yml b/.github/workflows/compare_memory_devnet.yml index 12c5afd3..a61b2f5f 100644 --- a/.github/workflows/compare_memory_devnet.yml +++ b/.github/workflows/compare_memory_devnet.yml @@ -23,10 +23,14 @@ jobs: uses: actions/setup-python@v4 with: python-version: '3.9' - - name: Clone starknet-devnet - uses: actions/checkout@master + - name: Checkout + uses: actions/checkout@v3 + - name: Checkout starknet-devnet + uses: actions/checkout@v3 with: repository: Shard-Labs/starknet-devnet + path: cairo-rs-py + - name: Install test dependencies run: pip install ecdsa fastecdsa sympy cairo-lang==0.9.1 maturin - name: Run devnet tests & compare memory outputs From a15914c0c77481719d4c57f4c07dadfbb895b4fe Mon Sep 17 00:00:00 2001 From: Federica Date: Wed, 30 Nov 2022 15:06:44 -0300 Subject: [PATCH 24/57] Debugging --- .github/workflows/compare_memory_devnet.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/compare_memory_devnet.yml b/.github/workflows/compare_memory_devnet.yml index a61b2f5f..0aab664f 100644 --- a/.github/workflows/compare_memory_devnet.yml +++ b/.github/workflows/compare_memory_devnet.yml @@ -30,7 +30,8 @@ jobs: with: repository: Shard-Labs/starknet-devnet path: cairo-rs-py - + - name: Check directory + run: ls - name: Install test dependencies run: pip install ecdsa fastecdsa sympy cairo-lang==0.9.1 maturin - name: Run devnet tests & compare memory outputs From b8042bf4e15643ff1034dd5033958ad4e955f469 Mon Sep 17 00:00:00 2001 From: Federica Date: Wed, 30 Nov 2022 15:21:08 -0300 Subject: [PATCH 25/57] Debugging --- .github/workflows/compare_memory_devnet.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/compare_memory_devnet.yml b/.github/workflows/compare_memory_devnet.yml index 0aab664f..5cad3589 100644 --- a/.github/workflows/compare_memory_devnet.yml +++ b/.github/workflows/compare_memory_devnet.yml @@ -29,7 +29,6 @@ jobs: uses: actions/checkout@v3 with: repository: Shard-Labs/starknet-devnet - path: cairo-rs-py - name: Check directory run: ls - name: Install test dependencies From e097100841df3cdf431f6e40cca54489e3d0e035 Mon Sep 17 00:00:00 2001 From: Federica Date: Wed, 30 Nov 2022 15:26:21 -0300 Subject: [PATCH 26/57] Debugging --- .github/workflows/compare_memory_devnet.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/compare_memory_devnet.yml b/.github/workflows/compare_memory_devnet.yml index 5cad3589..f429e7d5 100644 --- a/.github/workflows/compare_memory_devnet.yml +++ b/.github/workflows/compare_memory_devnet.yml @@ -29,6 +29,7 @@ jobs: uses: actions/checkout@v3 with: repository: Shard-Labs/starknet-devnet + path: . - name: Check directory run: ls - name: Install test dependencies From f083415b4333207b80414e5e1f18c5fd98c39b22 Mon Sep 17 00:00:00 2001 From: Federica Date: Wed, 30 Nov 2022 15:30:58 -0300 Subject: [PATCH 27/57] Debug --- scripts/memory_comparator/run_tests_compare_memory.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/memory_comparator/run_tests_compare_memory.sh b/scripts/memory_comparator/run_tests_compare_memory.sh index 599c651a..9f33cc01 100644 --- a/scripts/memory_comparator/run_tests_compare_memory.sh +++ b/scripts/memory_comparator/run_tests_compare_memory.sh @@ -5,7 +5,7 @@ exit_code=0 cd starknet-devnet for file in test/test_*.py; do # Skip problematic files - if ! ([ "$file" == "test/test_account.py" ] || [ "$file" == "test/test_estimate_fee.py" ] || ["$file" == "test/test_rpc_estimate_fee.py"] || ["$file" == "test/test_fee_token.py"] || ["$file" == "test/test_postman.py"] || ["$file" == "test/testnet_deployment.py"]); then + if ! ([ "$file" == "test/test_account.py" ] || [ "$file" == "test/test_estimate_fee.py" ] || [ "$file" == "test/test_rpc_estimate_fee.py" ] || [ "$file" == "test/test_fee_token.py" ] || [ "$file" == "test/test_postman.py" ] || [ "$file" == "test/testnet_deployment.py" ]); then # Run tests in cairo-rs-py env . ../scripts/memory_comparator/cairo-rs-py/bin/activate poetry run pytest $file From 1108e7d2f60a92b37c3fdd756e6e8c24f6f4ac12 Mon Sep 17 00:00:00 2001 From: Federica Date: Wed, 30 Nov 2022 15:35:44 -0300 Subject: [PATCH 28/57] Debug --- .github/workflows/compare_memory_devnet.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/compare_memory_devnet.yml b/.github/workflows/compare_memory_devnet.yml index f429e7d5..a0c995a5 100644 --- a/.github/workflows/compare_memory_devnet.yml +++ b/.github/workflows/compare_memory_devnet.yml @@ -29,7 +29,7 @@ jobs: uses: actions/checkout@v3 with: repository: Shard-Labs/starknet-devnet - path: . + path: starknet-devnet - name: Check directory run: ls - name: Install test dependencies From d708d5560b4f638fd443f7ad39f4bbe1f1c4d6df Mon Sep 17 00:00:00 2001 From: Federica Date: Wed, 30 Nov 2022 15:46:28 -0300 Subject: [PATCH 29/57] Fix bash syntax --- scripts/memory_comparator/run_tests_compare_memory.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/memory_comparator/run_tests_compare_memory.sh b/scripts/memory_comparator/run_tests_compare_memory.sh index 9f33cc01..4521c3e7 100644 --- a/scripts/memory_comparator/run_tests_compare_memory.sh +++ b/scripts/memory_comparator/run_tests_compare_memory.sh @@ -5,7 +5,7 @@ exit_code=0 cd starknet-devnet for file in test/test_*.py; do # Skip problematic files - if ! ([ "$file" == "test/test_account.py" ] || [ "$file" == "test/test_estimate_fee.py" ] || [ "$file" == "test/test_rpc_estimate_fee.py" ] || [ "$file" == "test/test_fee_token.py" ] || [ "$file" == "test/test_postman.py" ] || [ "$file" == "test/testnet_deployment.py" ]); then + if ! ([ "$file" = "test/test_account.py" ] || [ "$file" = "test/test_estimate_fee.py" ] || [ "$file" = "test/test_rpc_estimate_fee.py" ] || [ "$file" = "test/test_fee_token.py" ] || [ "$file" = "test/test_postman.py" ] || [ "$file" = "test/testnet_deployment.py" ]); then # Run tests in cairo-rs-py env . ../scripts/memory_comparator/cairo-rs-py/bin/activate poetry run pytest $file From 24485e0a61f1bd82796be73577169e3c593c728c Mon Sep 17 00:00:00 2001 From: Federica Date: Wed, 30 Nov 2022 15:48:27 -0300 Subject: [PATCH 30/57] Remove debug step --- .github/workflows/compare_memory_devnet.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/compare_memory_devnet.yml b/.github/workflows/compare_memory_devnet.yml index a0c995a5..baa968fc 100644 --- a/.github/workflows/compare_memory_devnet.yml +++ b/.github/workflows/compare_memory_devnet.yml @@ -30,8 +30,6 @@ jobs: with: repository: Shard-Labs/starknet-devnet path: starknet-devnet - - name: Check directory - run: ls - name: Install test dependencies run: pip install ecdsa fastecdsa sympy cairo-lang==0.9.1 maturin - name: Run devnet tests & compare memory outputs From 4d4233a268ae038a5ffa26d3618985b6808c1031 Mon Sep 17 00:00:00 2001 From: Federica Date: Wed, 30 Nov 2022 18:24:01 -0300 Subject: [PATCH 31/57] Add success message --- scripts/memory_comparator/run_tests_compare_memory.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/scripts/memory_comparator/run_tests_compare_memory.sh b/scripts/memory_comparator/run_tests_compare_memory.sh index 4521c3e7..d979dc60 100644 --- a/scripts/memory_comparator/run_tests_compare_memory.sh +++ b/scripts/memory_comparator/run_tests_compare_memory.sh @@ -19,6 +19,8 @@ for file in test/test_*.py; do if ! $memory_comparator_path $class_hash_path.memory $class_hash_path.rs.memory; then echo "Memory differs for last class_hash on test $file" exit_code=1 + else + echo "Memory comparison successful" fi if ! $memory_comparator_path $execute_entry_point_path.memory $execute_entry_point_path.rs.memory; then echo "Memory differs for last execute_entry_point on test $file" From 1164c2fda868dfacf4b60734823247f52c5d9446 Mon Sep 17 00:00:00 2001 From: Federica Date: Wed, 30 Nov 2022 18:26:59 -0300 Subject: [PATCH 32/57] Insert bug --- src/cairo_runner.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cairo_runner.rs b/src/cairo_runner.rs index de89b608..61be692d 100644 --- a/src/cairo_runner.rs +++ b/src/cairo_runner.rs @@ -425,7 +425,7 @@ impl PyCairoRunner { stack }; - let return_fp = MaybeRelocatable::from(bigint!(0)); + let return_fp = MaybeRelocatable::from(bigint!(1)); let end = self .inner From 9b941b51ec52664ed577b16c497feef3434feb16 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 1 Dec 2022 12:49:47 -0300 Subject: [PATCH 33/57] Replace memory_comparator --- .../memory_comparator/memory_comparator.py | 57 +++++++++++++++++++ .../run_tests_compare_memory.sh | 4 +- 2 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 scripts/memory_comparator/memory_comparator.py diff --git a/scripts/memory_comparator/memory_comparator.py b/scripts/memory_comparator/memory_comparator.py new file mode 100644 index 00000000..ebcae76d --- /dev/null +++ b/scripts/memory_comparator/memory_comparator.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python3 + +import sys + +def main(): + filename1 = sys.argv[1] + filename2 = sys.argv[2] + cairo_mem = {} + cairo_rs_mem = {} + name = filename2.split("/")[-1] + with open(filename1, 'rb') as f: + cairo_raw = f.read() + assert len(cairo_raw) % 40 == 0, f'{filename1}: malformed memory file from Cairo VM' + chunks = len(cairo_raw) // 40 + for i in range(0, chunks): + chunk = cairo_raw[i*40:(i+1)*40] + k, v = int.from_bytes(chunk[:8], 'little'), int.from_bytes(chunk[8:], 'little') + assert k not in cairo_mem, f'{filename1}: address {k} has two values' + cairo_mem[k] = v + assert len(cairo_mem) * 40 == len(cairo_raw), f'{filename1}: {len(cairo_mem) * 40} != {len(cairo_raw)}' + + with open(filename2, 'rb') as f: + cairo_rs_raw = f.read() + assert len(cairo_rs_raw) % 40 == 0, f'{filename2}: malformed memory file from cairo-rs' + chunks = len(cairo_rs_raw) // 40 + for i in range(0, chunks): + chunk = cairo_rs_raw[i*40:(i+1)*40] + k, v = int.from_bytes(chunk[:8], 'little'), int.from_bytes(chunk[8:], 'little') + assert k not in cairo_rs_mem, f'{filename2}: address {k} has two values' + cairo_rs_mem[k] = v + assert len(cairo_rs_mem) * 40 == len(cairo_rs_raw), f'{filename2}: {len(cairo_rs_mem) * 40} != {len(cairo_rs_raw)}' + + assert len(cairo_mem) == len(cairo_rs_mem), f'{filename2}: len(cairo_mem)={len(cairo_mem)} len(cairo_mem)={len(cairo_rs_mem)}' + if cairo_mem != cairo_rs_mem: + print(f'Mismatch between {filename1} (Cairo) and {filename2} (cairo_rs)') + print('keys in Cairo but not cairo-rs:') + for k in cairo_mem: + if k in cairo_rs_mem: + continue + print(f'{k}:{v}') + print('keys in cairo_rs but not Cairo:') + for k in cairo_rs_mem: + if k in cairo_mem: + continue + print(f'{k}:{v}') + print('mismatched values (Cairo <-> cairo_rs)):') + for k in cairo_rs_mem: + if k not in cairo_mem: + continue + if cairo_rs_mem[k] == cairo_mem[k]: + continue + print(f'{k}:({cairo_mem[k]} <-> {cairo_rs_mem[k]})') + exit(1) + + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/scripts/memory_comparator/run_tests_compare_memory.sh b/scripts/memory_comparator/run_tests_compare_memory.sh index d979dc60..d80841e1 100644 --- a/scripts/memory_comparator/run_tests_compare_memory.sh +++ b/scripts/memory_comparator/run_tests_compare_memory.sh @@ -5,7 +5,7 @@ exit_code=0 cd starknet-devnet for file in test/test_*.py; do # Skip problematic files - if ! ([ "$file" = "test/test_account.py" ] || [ "$file" = "test/test_estimate_fee.py" ] || [ "$file" = "test/test_rpc_estimate_fee.py" ] || [ "$file" = "test/test_fee_token.py" ] || [ "$file" = "test/test_postman.py" ] || [ "$file" = "test/testnet_deployment.py" ]); then + if ! ([ "$file" = "test/test_account.py" ] || [ "$file" = "test/test_estimate_fee.py" ] || [ "$file" = "test/test_rpc_estimate_fee.py" ] || [ "$file" = "test/test_fee_token.py" ] || [ "$file" = "test/test_postman.py" ] || [ "$file" = "test/testnet_deployment.py" ] || [ "$file" = "test/test_api_specifications.py" ]); then # Run tests in cairo-rs-py env . ../scripts/memory_comparator/cairo-rs-py/bin/activate poetry run pytest $file @@ -15,7 +15,7 @@ for file in test/test_*.py; do # Compare memory outputs class_hash_path="memory_files/class_hash" execute_entry_point_path="memory_files/execute_entry_point" - memory_comparator_path="../tests/memory_comparator.py" + memory_comparator_path="../scripts/memory_comparator/memory_comparator.py" if ! $memory_comparator_path $class_hash_path.memory $class_hash_path.rs.memory; then echo "Memory differs for last class_hash on test $file" exit_code=1 From 3b13b36c769f23d950830c811e84314aa3634448 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 1 Dec 2022 13:32:45 -0300 Subject: [PATCH 34/57] Make new comparator executable --- scripts/memory_comparator/memory_comparator.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 scripts/memory_comparator/memory_comparator.py diff --git a/scripts/memory_comparator/memory_comparator.py b/scripts/memory_comparator/memory_comparator.py old mode 100644 new mode 100755 From 9b2ee59c7809f3f9c572767280795b9715185cdc Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 1 Dec 2022 13:34:45 -0300 Subject: [PATCH 35/57] Fix indentation --- .../memory_comparator/memory_comparator.py | 82 +++++++++---------- 1 file changed, 41 insertions(+), 41 deletions(-) diff --git a/scripts/memory_comparator/memory_comparator.py b/scripts/memory_comparator/memory_comparator.py index ebcae76d..537f5735 100755 --- a/scripts/memory_comparator/memory_comparator.py +++ b/scripts/memory_comparator/memory_comparator.py @@ -8,49 +8,49 @@ def main(): cairo_mem = {} cairo_rs_mem = {} name = filename2.split("/")[-1] - with open(filename1, 'rb') as f: - cairo_raw = f.read() - assert len(cairo_raw) % 40 == 0, f'{filename1}: malformed memory file from Cairo VM' - chunks = len(cairo_raw) // 40 - for i in range(0, chunks): - chunk = cairo_raw[i*40:(i+1)*40] - k, v = int.from_bytes(chunk[:8], 'little'), int.from_bytes(chunk[8:], 'little') - assert k not in cairo_mem, f'{filename1}: address {k} has two values' - cairo_mem[k] = v - assert len(cairo_mem) * 40 == len(cairo_raw), f'{filename1}: {len(cairo_mem) * 40} != {len(cairo_raw)}' + with open(filename1, 'rb') as f: + cairo_raw = f.read() + assert len(cairo_raw) % 40 == 0, f'{filename1}: malformed memory file from Cairo VM' + chunks = len(cairo_raw) // 40 + for i in range(0, chunks): + chunk = cairo_raw[i*40:(i+1)*40] + k, v = int.from_bytes(chunk[:8], 'little'), int.from_bytes(chunk[8:], 'little') + assert k not in cairo_mem, f'{filename1}: address {k} has two values' + cairo_mem[k] = v + assert len(cairo_mem) * 40 == len(cairo_raw), f'{filename1}: {len(cairo_mem) * 40} != {len(cairo_raw)}' - with open(filename2, 'rb') as f: - cairo_rs_raw = f.read() - assert len(cairo_rs_raw) % 40 == 0, f'{filename2}: malformed memory file from cairo-rs' - chunks = len(cairo_rs_raw) // 40 - for i in range(0, chunks): - chunk = cairo_rs_raw[i*40:(i+1)*40] - k, v = int.from_bytes(chunk[:8], 'little'), int.from_bytes(chunk[8:], 'little') - assert k not in cairo_rs_mem, f'{filename2}: address {k} has two values' - cairo_rs_mem[k] = v - assert len(cairo_rs_mem) * 40 == len(cairo_rs_raw), f'{filename2}: {len(cairo_rs_mem) * 40} != {len(cairo_rs_raw)}' + with open(filename2, 'rb') as f: + cairo_rs_raw = f.read() + assert len(cairo_rs_raw) % 40 == 0, f'{filename2}: malformed memory file from cairo-rs' + chunks = len(cairo_rs_raw) // 40 + for i in range(0, chunks): + chunk = cairo_rs_raw[i*40:(i+1)*40] + k, v = int.from_bytes(chunk[:8], 'little'), int.from_bytes(chunk[8:], 'little') + assert k not in cairo_rs_mem, f'{filename2}: address {k} has two values' + cairo_rs_mem[k] = v + assert len(cairo_rs_mem) * 40 == len(cairo_rs_raw), f'{filename2}: {len(cairo_rs_mem) * 40} != {len(cairo_rs_raw)}' - assert len(cairo_mem) == len(cairo_rs_mem), f'{filename2}: len(cairo_mem)={len(cairo_mem)} len(cairo_mem)={len(cairo_rs_mem)}' - if cairo_mem != cairo_rs_mem: - print(f'Mismatch between {filename1} (Cairo) and {filename2} (cairo_rs)') - print('keys in Cairo but not cairo-rs:') - for k in cairo_mem: - if k in cairo_rs_mem: - continue - print(f'{k}:{v}') - print('keys in cairo_rs but not Cairo:') - for k in cairo_rs_mem: - if k in cairo_mem: - continue - print(f'{k}:{v}') - print('mismatched values (Cairo <-> cairo_rs)):') - for k in cairo_rs_mem: - if k not in cairo_mem: - continue - if cairo_rs_mem[k] == cairo_mem[k]: - continue - print(f'{k}:({cairo_mem[k]} <-> {cairo_rs_mem[k]})') - exit(1) + assert len(cairo_mem) == len(cairo_rs_mem), f'{filename2}: len(cairo_mem)={len(cairo_mem)} len(cairo_mem)={len(cairo_rs_mem)}' + if cairo_mem != cairo_rs_mem: + print(f'Mismatch between {filename1} (Cairo) and {filename2} (cairo_rs)') + print('keys in Cairo but not cairo-rs:') + for k in cairo_mem: + if k in cairo_rs_mem: + continue + print(f'{k}:{v}') + print('keys in cairo_rs but not Cairo:') + for k in cairo_rs_mem: + if k in cairo_mem: + continue + print(f'{k}:{v}') + print('mismatched values (Cairo <-> cairo_rs)):') + for k in cairo_rs_mem: + if k not in cairo_mem: + continue + if cairo_rs_mem[k] == cairo_mem[k]: + continue + print(f'{k}:({cairo_mem[k]} <-> {cairo_rs_mem[k]})') + exit(1) if __name__ == '__main__': From 11c66ecacc9bc0dfb7782b0bc278aca802a4752c Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 1 Dec 2022 13:41:18 -0300 Subject: [PATCH 36/57] Filter out file which doesnt produce memory outputs in execute_entry_point --- scripts/memory_comparator/run_tests_compare_memory.sh | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/scripts/memory_comparator/run_tests_compare_memory.sh b/scripts/memory_comparator/run_tests_compare_memory.sh index d80841e1..eb18500a 100644 --- a/scripts/memory_comparator/run_tests_compare_memory.sh +++ b/scripts/memory_comparator/run_tests_compare_memory.sh @@ -22,9 +22,11 @@ for file in test/test_*.py; do else echo "Memory comparison successful" fi - if ! $memory_comparator_path $execute_entry_point_path.memory $execute_entry_point_path.rs.memory; then - echo "Memory differs for last execute_entry_point on test $file" - exit_code=1 + if ! ([ "$file" = "test/test_account_predeployed.py" ]); then + if ! $memory_comparator_path $execute_entry_point_path.memory $execute_entry_point_path.rs.memory; then + echo "Memory differs for last execute_entry_point on test $file" + exit_code=1 + fi fi # Cleanup memory files rm memory_files/*.memory From 80e7f91585e0e247967812dadfcd957a97abc011 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 1 Dec 2022 13:43:12 -0300 Subject: [PATCH 37/57] Filter out file which doesnt produce memory outputs --- scripts/memory_comparator/run_tests_compare_memory.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scripts/memory_comparator/run_tests_compare_memory.sh b/scripts/memory_comparator/run_tests_compare_memory.sh index eb18500a..b69ad6c5 100644 --- a/scripts/memory_comparator/run_tests_compare_memory.sh +++ b/scripts/memory_comparator/run_tests_compare_memory.sh @@ -5,7 +5,7 @@ exit_code=0 cd starknet-devnet for file in test/test_*.py; do # Skip problematic files - if ! ([ "$file" = "test/test_account.py" ] || [ "$file" = "test/test_estimate_fee.py" ] || [ "$file" = "test/test_rpc_estimate_fee.py" ] || [ "$file" = "test/test_fee_token.py" ] || [ "$file" = "test/test_postman.py" ] || [ "$file" = "test/testnet_deployment.py" ] || [ "$file" = "test/test_api_specifications.py" ]); then + if ! ([ "$file" = "test/test_account.py" ] || [ "$file" = "test/test_estimate_fee.py" ] || [ "$file" = "test/test_rpc_estimate_fee.py" ] || [ "$file" = "test/test_fee_token.py" ] || [ "$file" = "test/test_postman.py" ] || [ "$file" = "test/testnet_deployment.py" ] || [ "$file" = "test/testnet_deploy.py" ] || [ "$file" = "test/test_api_specifications.py" ]); then # Run tests in cairo-rs-py env . ../scripts/memory_comparator/cairo-rs-py/bin/activate poetry run pytest $file @@ -22,6 +22,7 @@ for file in test/test_*.py; do else echo "Memory comparison successful" fi + # Some tests do not use execute_entry_point and dont generate memory files there if ! ([ "$file" = "test/test_account_predeployed.py" ]); then if ! $memory_comparator_path $execute_entry_point_path.memory $execute_entry_point_path.rs.memory; then echo "Memory differs for last execute_entry_point on test $file" From 831c2dabf8c8345748062b6716394dc94103875a Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 1 Dec 2022 13:49:23 -0300 Subject: [PATCH 38/57] Filter out file which doesnt produce memory outputs --- .../memory_comparator/run_tests_compare_memory.sh | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/scripts/memory_comparator/run_tests_compare_memory.sh b/scripts/memory_comparator/run_tests_compare_memory.sh index b69ad6c5..3cb9e188 100644 --- a/scripts/memory_comparator/run_tests_compare_memory.sh +++ b/scripts/memory_comparator/run_tests_compare_memory.sh @@ -16,17 +16,21 @@ for file in test/test_*.py; do class_hash_path="memory_files/class_hash" execute_entry_point_path="memory_files/execute_entry_point" memory_comparator_path="../scripts/memory_comparator/memory_comparator.py" - if ! $memory_comparator_path $class_hash_path.memory $class_hash_path.rs.memory; then - echo "Memory differs for last class_hash on test $file" - exit_code=1 - else - echo "Memory comparison successful" + # Some tests do not use class_hash and dont generate memory files there + if ! ([ "$file" = "test/test_dump.py" ]); then + if ! $memory_comparator_path $class_hash_path.memory $class_hash_path.rs.memory; then + echo "Memory differs for last class_hash on test $file" + exit_code=1 + else + echo "Memory comparison successful" fi # Some tests do not use execute_entry_point and dont generate memory files there if ! ([ "$file" = "test/test_account_predeployed.py" ]); then if ! $memory_comparator_path $execute_entry_point_path.memory $execute_entry_point_path.rs.memory; then echo "Memory differs for last execute_entry_point on test $file" exit_code=1 + else + echo "Memory comparison successful" fi fi # Cleanup memory files From e537487ec3a3aaeec2e8675863d6c3eac144e7d4 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 1 Dec 2022 13:54:43 -0300 Subject: [PATCH 39/57] Exclude more files --- scripts/memory_comparator/run_tests_compare_memory.sh | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/scripts/memory_comparator/run_tests_compare_memory.sh b/scripts/memory_comparator/run_tests_compare_memory.sh index 3cb9e188..16e0bd02 100644 --- a/scripts/memory_comparator/run_tests_compare_memory.sh +++ b/scripts/memory_comparator/run_tests_compare_memory.sh @@ -4,8 +4,9 @@ exit_code=0 # We need to be inside starknet-devnet in order to run poetry cd starknet-devnet for file in test/test_*.py; do - # Skip problematic files - if ! ([ "$file" = "test/test_account.py" ] || [ "$file" = "test/test_estimate_fee.py" ] || [ "$file" = "test/test_rpc_estimate_fee.py" ] || [ "$file" = "test/test_fee_token.py" ] || [ "$file" = "test/test_postman.py" ] || [ "$file" = "test/testnet_deployment.py" ] || [ "$file" = "test/testnet_deploy.py" ] || [ "$file" = "test/test_api_specifications.py" ]); then + # Skip files that dont run entrypoints and dont produce memory outputs + # As a temporary measure, some tests that do produce memory outputs are being skipped while the team works on fixing some error format mismatches + if ! ([ "$file" = "test/test_account.py" ] || [ "$file" = "test/test_estimate_fee.py" ] || [ "$file" = "test/test_rpc_estimate_fee.py" ] || [ "$file" = "test/test_fee_token.py" ] || [ "$file" = "test/test_postman.py" ] || [ "$file" = "test/testnet_deployment.py" ] || [ "$file" = "test/testnet_deploy.py" ] || [ "$file" = "test/test_api_specifications.py" ] || [ "$file" = "test/test_fork_cli_params.py" ]); then # Run tests in cairo-rs-py env . ../scripts/memory_comparator/cairo-rs-py/bin/activate poetry run pytest $file @@ -23,6 +24,7 @@ for file in test/test_*.py; do exit_code=1 else echo "Memory comparison successful" + fi fi # Some tests do not use execute_entry_point and dont generate memory files there if ! ([ "$file" = "test/test_account_predeployed.py" ]); then From a7988f5e47d47ca0de46228be5f42de03096f2e9 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 1 Dec 2022 13:57:09 -0300 Subject: [PATCH 40/57] Remove bug --- src/cairo_runner.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cairo_runner.rs b/src/cairo_runner.rs index 61be692d..de89b608 100644 --- a/src/cairo_runner.rs +++ b/src/cairo_runner.rs @@ -425,7 +425,7 @@ impl PyCairoRunner { stack }; - let return_fp = MaybeRelocatable::from(bigint!(1)); + let return_fp = MaybeRelocatable::from(bigint!(0)); let end = self .inner From deb8d09144c342f026a5525d95dc222c6d544b8a Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 1 Dec 2022 16:46:37 -0300 Subject: [PATCH 41/57] Update patch file --- .../output-memory-cairo-rs-py.patch | 665 +++++++++--------- 1 file changed, 330 insertions(+), 335 deletions(-) diff --git a/scripts/memory_comparator/output-memory-cairo-rs-py.patch b/scripts/memory_comparator/output-memory-cairo-rs-py.patch index d2ae4aed..64fe8902 100644 --- a/scripts/memory_comparator/output-memory-cairo-rs-py.patch +++ b/scripts/memory_comparator/output-memory-cairo-rs-py.patch @@ -1,521 +1,516 @@ diff --git a/src/starkware/starknet/business_logic/execution/execute_entry_point.py b/src/starkware/starknet/business_logic/execution/execute_entry_point.py -index 62d4290..c3cdd7e 100644 +index 719208f..62d4290 100644 --- a/src/starkware/starknet/business_logic/execution/execute_entry_point.py +++ b/src/starkware/starknet/business_logic/execution/execute_entry_point.py -@@ -41,6 +41,7 @@ from starkware.starkware_utils.error_handling import ( +@@ -41,7 +41,6 @@ from starkware.starkware_utils.error_handling import ( stark_assert, wrap_with_stark_exception, ) -+import cairo_rs_py +-import cairo_rs_py logger = logging.getLogger(__name__) -@@ -182,7 +183,8 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): +@@ -183,8 +182,7 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): # Run the specified contract entry point with given calldata. with wrap_with_stark_exception(code=StarknetErrorCode.SECURITY_ERROR): -- runner = CairoFunctionRunner(program=contract_class.program, layout="all") -+ runner = cairo_rs_py.CairoRunner(program=contract_class.program.dumps(), entrypoint=None, layout="all", proof_mode=False) -+ runner.initialize_function_runner() +- runner = cairo_rs_py.CairoRunner(program=contract_class.program.dumps(), entrypoint=None, layout="all", proof_mode=False) +- runner.initialize_function_runner() ++ runner = CairoFunctionRunner(program=contract_class.program, layout="all") os_context = os_utils.prepare_os_context(runner=runner) validate_contract_deployed(state=state, contract_address=self.contract_address) -@@ -205,24 +207,24 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): +@@ -207,24 +205,24 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): os_context, len(self.calldata), # Allocate and mark the segment as read-only (to mark every input array as read-only). -- syscall_handler._allocate_segment(segments=runner.segments, data=self.calldata), -+ syscall_handler._allocate_segment(segments=runner, data=self.calldata), +- syscall_handler._allocate_segment(segments=runner, data=self.calldata), ++ syscall_handler._allocate_segment(segments=runner.segments, data=self.calldata), ] try: runner.run_from_entrypoint( entry_point.offset, -- *entry_points_args, -+ entry_points_args, +- entry_points_args, ++ *entry_points_args, hint_locals={ "syscall_handler": syscall_handler, }, -- static_locals={ -- "__find_element_max_size": 2**20, -- "__squash_dict_max_size": 2**20, -- "__keccak_max_size": 2**20, -- "__usort_max_size": 2**20, -- "__chained_ec_op_max_len": 1000, -- }, -- run_resources=tx_execution_context.run_resources, -+ # static_locals={ -+ # "__find_element_max_size": 2**20, -+ # "__squash_dict_max_size": 2**20, -+ # "__keccak_max_size": 2**20, -+ # "__usort_max_size": 2**20, -+ # "__chained_ec_op_max_len": 1000, -+ # }, -+ # run_resources=tx_execution_context.run_resources, +- # static_locals={ +- # "__find_element_max_size": 2**20, +- # "__squash_dict_max_size": 2**20, +- # "__keccak_max_size": 2**20, +- # "__usort_max_size": 2**20, +- # "__chained_ec_op_max_len": 1000, +- # }, +- # run_resources=tx_execution_context.run_resources, ++ static_locals={ ++ "__find_element_max_size": 2**20, ++ "__squash_dict_max_size": 2**20, ++ "__keccak_max_size": 2**20, ++ "__usort_max_size": 2**20, ++ "__chained_ec_op_max_len": 1000, ++ }, ++ run_resources=tx_execution_context.run_resources, verify_secure=True, ) except VmException as exception: -@@ -267,13 +269,14 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): - ) - - # When execution starts the stack holds entry_points_args + [ret_fp, ret_pc]. -- args_ptr = runner.initial_fp - (len(entry_points_args) + 2) -+ args_ptr = runner.get_initial_fp() - (len(entry_points_args) + 2) +@@ -273,10 +271,9 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): # The arguments are touched by the OS and should not be counted as holes, mark them # as accessed. -- assert isinstance(args_ptr, RelocatableValue) # Downcast. -+ # assert isinstance(args_ptr, RelocatableValue) # Downcast. +- # assert isinstance(args_ptr, RelocatableValue) # Downcast. ++ assert isinstance(args_ptr, RelocatableValue) # Downcast. runner.mark_as_accessed(address=args_ptr, size=len(entry_points_args)) -- -+ runner.relocate() -+ runner.write_binary_memory("memory_files/execute_entry_point.rs.memory") +- runner.relocate() +- runner.write_binary_memory("memory_files/execute_entry_point.rs.memory") ++ return runner, syscall_handler def _get_selected_entry_point( -@@ -355,4 +358,4 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): +@@ -358,4 +355,4 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): raise NotImplementedError(f"Call type {self.call_type} not implemented.") # Extract pre-fetched contract code from carried state. - return get_deployed_class_hash_at_address(state=state, contract_address=code_address) -+ return get_deployed_class_hash_at_address(state=state, contract_address=code_address) \ No newline at end of file ++ return get_deployed_class_hash_at_address(state=state, contract_address=code_address) diff --git a/src/starkware/starknet/business_logic/transaction/fee.py b/src/starkware/starknet/business_logic/transaction/fee.py -index 9acaa73..9bbb9f5 100644 +index 9bbb9f5..9acaa73 100644 --- a/src/starkware/starknet/business_logic/transaction/fee.py +++ b/src/starkware/starknet/business_logic/transaction/fee.py @@ -67,9 +67,9 @@ def calculate_l1_gas_by_cairo_usage( """ cairo_resource_fee_weights = general_config.cairo_resource_fee_weights cairo_resource_names = set(cairo_resource_usage.keys()) -- assert cairo_resource_names.issubset( -- cairo_resource_fee_weights.keys() -- ), "Cairo resource names must be contained in fee weights dict." -+ # assert cairo_resource_names.issubset( -+ # cairo_resource_fee_weights.keys() -+ # ), "Cairo resource names must be contained in fee weights dict." +- # assert cairo_resource_names.issubset( +- # cairo_resource_fee_weights.keys() +- # ), "Cairo resource names must be contained in fee weights dict." ++ assert cairo_resource_names.issubset( ++ cairo_resource_fee_weights.keys() ++ ), "Cairo resource names must be contained in fee weights dict." # Convert Cairo usage to L1 gas usage. cairo_l1_gas_usage = max( diff --git a/src/starkware/starknet/business_logic/utils.py b/src/starkware/starknet/business_logic/utils.py -index f63bc9f..15660fe 100644 +index 15660fe..f63bc9f 100644 --- a/src/starkware/starknet/business_logic/utils.py +++ b/src/starkware/starknet/business_logic/utils.py -@@ -48,7 +48,14 @@ def get_return_values(runner: CairoFunctionRunner) -> List[int]: +@@ -48,14 +48,7 @@ def get_return_values(runner: CairoFunctionRunner) -> List[int]: exception_types=[Exception], ): ret_data_size, ret_data_ptr = runner.get_return_values(2) -- values = runner.memory.get_range(ret_data_ptr, ret_data_size) -+ -+ try: -+ # CAIRO-RS VERSION -+ values = runner.get_range(ret_data_ptr, ret_data_size) -+ except: -+ # ORIGINAL VERSION -+ values = runner.memory.get_range(ret_data_ptr, ret_data_size) -+ +- +- try: +- # CAIRO-RS VERSION +- values = runner.get_range(ret_data_ptr, ret_data_size) +- except: +- # ORIGINAL VERSION +- values = runner.memory.get_range(ret_data_ptr, ret_data_size) +- ++ values = runner.memory.get_range(ret_data_ptr, ret_data_size) stark_assert( all(isinstance(value, int) for value in values), diff --git a/src/starkware/starknet/core/os/class_hash.py b/src/starkware/starknet/core/os/class_hash.py -index 132fb21..3cf48a8 100644 +index 3cf48a8..132fb21 100644 --- a/src/starkware/starknet/core/os/class_hash.py +++ b/src/starkware/starknet/core/os/class_hash.py -@@ -5,9 +5,10 @@ import json +@@ -5,10 +5,9 @@ import json import os from contextvars import ContextVar from functools import lru_cache --from typing import Callable, List, Optional -+from typing import Any, Callable, Dict, List, Optional, Tuple +-from typing import Any, Callable, Dict, List, Optional, Tuple ++from typing import Callable, List, Optional import cachetools -+import cairo_rs_py +-import cairo_rs_py from starkware.cairo.common.cairo_function_runner import CairoFunctionRunner from starkware.cairo.common.structs import CairoStructFactory, CairoStructProxy -@@ -23,6 +24,10 @@ from starkware.cairo.lang.vm.crypto import pedersen_hash +@@ -24,10 +23,6 @@ from starkware.cairo.lang.vm.crypto import pedersen_hash from starkware.python.utils import from_bytes from starkware.starknet.public.abi import starknet_keccak from starkware.starknet.services.api.contract_class import ContractClass, EntryPointType -+# Added Imports -+from starkware.cairo.lang.vm.relocatable import MaybeRelocatable, RelocatableValue -+from starkware.cairo.lang.vm.vm_exceptions import SecurityError, VmException -+from starkware.python.utils import safe_zip +-# Added Imports +-from starkware.cairo.lang.vm.relocatable import MaybeRelocatable, RelocatableValue +-from starkware.cairo.lang.vm.vm_exceptions import SecurityError, VmException +-from starkware.python.utils import safe_zip CAIRO_FILE = os.path.join(os.path.dirname(__file__), "contracts.cairo") -@@ -77,22 +82,24 @@ def compute_class_hash_inner( +@@ -82,24 +77,22 @@ def compute_class_hash_inner( contract_class_struct = get_contract_class_struct( identifiers=program.identifiers, contract_class=contract_class ) -- runner = CairoFunctionRunner(program) - -- hash_builtin = HashBuiltinRunner( -- name="custom_hasher", included=True, ratio=32, hash_func=hash_func -- ) -- runner.builtin_runners["hash_builtin"] = hash_builtin -- hash_builtin.initialize_segments(runner) -+ runner = cairo_rs_py.CairoRunner(program=program.dumps(), entrypoint=None, layout="all", proof_mode=False) -+ runner.initialize_function_runner() -+ hash_ptr = runner.add_additional_hash_builtin() -+ ++ runner = CairoFunctionRunner(program) -- runner.run( -+ run_function_runner( -+ runner, -+ program, +- runner = cairo_rs_py.CairoRunner(program=program.dumps(), entrypoint=None, layout="all", proof_mode=False) +- runner.initialize_function_runner() +- hash_ptr = runner.add_additional_hash_builtin() +- ++ hash_builtin = HashBuiltinRunner( ++ name="custom_hasher", included=True, ratio=32, hash_func=hash_func ++ ) ++ runner.builtin_runners["hash_builtin"] = hash_builtin ++ hash_builtin.initialize_segments(runner) + +- run_function_runner( +- runner, +- program, ++ runner.run( "starkware.starknet.core.os.contracts.class_hash", -- hash_ptr=hash_builtin.base, -+ hash_ptr=hash_ptr, +- hash_ptr=hash_ptr, ++ hash_ptr=hash_builtin.base, contract_class=contract_class_struct, use_full_name=True, verify_secure=False, ) _, class_hash = runner.get_return_values(2) -+ runner.relocate() -+ runner.write_binary_memory("memory_files/class_hash.rs.memory") +- runner.relocate() +- runner.write_binary_memory("memory_files/class_hash.rs.memory") return class_hash -@@ -194,3 +201,103 @@ def get_contract_class_struct( +@@ -201,103 +194,3 @@ def get_contract_class_struct( bytecode_length=len(contract_class.program.data), bytecode_ptr=contract_class.program.data, ) -+ -+def run_function_runner( -+ runner, -+ program, -+ func_name: str, -+ *args, -+ hint_locals: Optional[Dict[str, Any]] = None, -+ static_locals: Optional[Dict[str, Any]] = None, -+ verify_secure: Optional[bool] = None, -+ trace_on_failure: bool = False, -+ apply_modulo_to_args: Optional[bool] = None, -+ use_full_name: bool = False, -+ verify_implicit_args_segment: bool = False, -+ **kwargs, -+ ) -> Tuple[Tuple[MaybeRelocatable, ...], Tuple[MaybeRelocatable, ...]]: -+ """ -+ Runs func_name(*args). -+ args are converted to Cairo-friendly ones using gen_arg. -+ -+ Returns the return values of the function, splitted into 2 tuples of implicit values and -+ explicit values. Structs will be flattened to a sequence of felts as part of the returned -+ tuple. -+ -+ Additional params: -+ verify_secure - Run verify_secure_runner to do extra verifications. -+ trace_on_failure - Run the tracer in case of failure to help debugging. -+ apply_modulo_to_args - Apply modulo operation on integer arguments. -+ use_full_name - Treat 'func_name' as a fully qualified identifier name, rather than a -+ relative one. -+ verify_implicit_args_segment - For each implicit argument, verify that the argument and the -+ return value are in the same segment. -+ """ -+ assert isinstance(program, Program) -+ entrypoint = program.get_label(func_name, full_name_lookup=use_full_name) -+ -+ #Construct Fu -+ structs_factory = CairoStructFactory.from_program(program=program) -+ func = ScopedName.from_string(scope=func_name) -+ -+ full_args_struct = structs_factory.build_func_args(func=func) -+ all_args = full_args_struct(*args, **kwargs) -+ -+ try: -+ runner.run_from_entrypoint( -+ entrypoint, -+ all_args, -+ typed_args=True, -+ hint_locals=hint_locals, -+ static_locals=static_locals, -+ verify_secure=verify_secure, -+ apply_modulo_to_args=apply_modulo_to_args, -+ ) -+ except (VmException, SecurityError, AssertionError) as ex: -+ if trace_on_failure: -+ print( -+ f"""\ -+Got {type(ex).__name__} exception during the execution of {func_name}: -+{str(ex)} -+""" -+ ) -+ #trace_runner(runner=runner) -+ raise -+ -+ # The number of implicit arguments is identical to the number of implicit return values. -+ n_implicit_ret_vals = structs_factory.get_implicit_args_length(func=func) -+ n_explicit_ret_vals = structs_factory.get_explicit_return_values_length(func=func) -+ n_ret_vals = n_explicit_ret_vals + n_implicit_ret_vals -+ implicit_retvals = tuple( -+ runner.get_range( -+ runner.get_ap() - n_ret_vals, n_implicit_ret_vals -+ ) -+ ) -+ -+ explicit_retvals = tuple( -+ runner.get_range( -+ runner.get_ap() - n_explicit_ret_vals, n_explicit_ret_vals -+ ) -+ ) -+ -+ # Verify the memory segments of the implicit arguments. -+ if verify_implicit_args_segment: -+ implicit_args = all_args[:n_implicit_ret_vals] -+ for implicit_arg, implicit_retval in safe_zip(implicit_args, implicit_retvals): -+ assert isinstance( -+ implicit_arg, RelocatableValue -+ ), f"Implicit arguments must be RelocatableValues, {implicit_arg} is not." -+ assert isinstance(implicit_retval, RelocatableValue), ( -+ f"Argument {implicit_arg} is a RelocatableValue, but the returned value " -+ f"{implicit_retval} is not." -+ ) -+ assert implicit_arg.segment_index == implicit_retval.segment_index, ( -+ f"Implicit argument {implicit_arg} is not on the same segment as the returned " -+ f"{implicit_retval}." -+ ) -+ assert implicit_retval.offset >= implicit_arg.offset, ( -+ f"The offset of the returned implicit argument {implicit_retval} is less than " -+ f"the offset of the input {implicit_arg}." -+ ) -+ -+ return implicit_retvals, explicit_retvals +- +-def run_function_runner( +- runner, +- program, +- func_name: str, +- *args, +- hint_locals: Optional[Dict[str, Any]] = None, +- static_locals: Optional[Dict[str, Any]] = None, +- verify_secure: Optional[bool] = None, +- trace_on_failure: bool = False, +- apply_modulo_to_args: Optional[bool] = None, +- use_full_name: bool = False, +- verify_implicit_args_segment: bool = False, +- **kwargs, +- ) -> Tuple[Tuple[MaybeRelocatable, ...], Tuple[MaybeRelocatable, ...]]: +- """ +- Runs func_name(*args). +- args are converted to Cairo-friendly ones using gen_arg. +- +- Returns the return values of the function, splitted into 2 tuples of implicit values and +- explicit values. Structs will be flattened to a sequence of felts as part of the returned +- tuple. +- +- Additional params: +- verify_secure - Run verify_secure_runner to do extra verifications. +- trace_on_failure - Run the tracer in case of failure to help debugging. +- apply_modulo_to_args - Apply modulo operation on integer arguments. +- use_full_name - Treat 'func_name' as a fully qualified identifier name, rather than a +- relative one. +- verify_implicit_args_segment - For each implicit argument, verify that the argument and the +- return value are in the same segment. +- """ +- assert isinstance(program, Program) +- entrypoint = program.get_label(func_name, full_name_lookup=use_full_name) +- +- #Construct Fu +- structs_factory = CairoStructFactory.from_program(program=program) +- func = ScopedName.from_string(scope=func_name) +- +- full_args_struct = structs_factory.build_func_args(func=func) +- all_args = full_args_struct(*args, **kwargs) +- +- try: +- runner.run_from_entrypoint( +- entrypoint, +- all_args, +- typed_args=True, +- hint_locals=hint_locals, +- static_locals=static_locals, +- verify_secure=verify_secure, +- apply_modulo_to_args=apply_modulo_to_args, +- ) +- except (VmException, SecurityError, AssertionError) as ex: +- if trace_on_failure: +- print( +- f"""\ +-Got {type(ex).__name__} exception during the execution of {func_name}: +-{str(ex)} +-""" +- ) +- #trace_runner(runner=runner) +- raise +- +- # The number of implicit arguments is identical to the number of implicit return values. +- n_implicit_ret_vals = structs_factory.get_implicit_args_length(func=func) +- n_explicit_ret_vals = structs_factory.get_explicit_return_values_length(func=func) +- n_ret_vals = n_explicit_ret_vals + n_implicit_ret_vals +- implicit_retvals = tuple( +- runner.get_range( +- runner.get_ap() - n_ret_vals, n_implicit_ret_vals +- ) +- ) +- +- explicit_retvals = tuple( +- runner.get_range( +- runner.get_ap() - n_explicit_ret_vals, n_explicit_ret_vals +- ) +- ) +- +- # Verify the memory segments of the implicit arguments. +- if verify_implicit_args_segment: +- implicit_args = all_args[:n_implicit_ret_vals] +- for implicit_arg, implicit_retval in safe_zip(implicit_args, implicit_retvals): +- assert isinstance( +- implicit_arg, RelocatableValue +- ), f"Implicit arguments must be RelocatableValues, {implicit_arg} is not." +- assert isinstance(implicit_retval, RelocatableValue), ( +- f"Argument {implicit_arg} is a RelocatableValue, but the returned value " +- f"{implicit_retval} is not." +- ) +- assert implicit_arg.segment_index == implicit_retval.segment_index, ( +- f"Implicit argument {implicit_arg} is not on the same segment as the returned " +- f"{implicit_retval}." +- ) +- assert implicit_retval.offset >= implicit_arg.offset, ( +- f"The offset of the returned implicit argument {implicit_retval} is less than " +- f"the offset of the input {implicit_arg}." +- ) +- +- return implicit_retvals, explicit_retvals diff --git a/src/starkware/starknet/core/os/os_utils.py b/src/starkware/starknet/core/os/os_utils.py -index 20bd521..0ea99f4 100644 +index 0ea99f4..20bd521 100644 --- a/src/starkware/starknet/core/os/os_utils.py +++ b/src/starkware/starknet/core/os/os_utils.py -@@ -43,18 +43,23 @@ def update_builtin_pointers( +@@ -43,23 +43,18 @@ def update_builtin_pointers( return return_builtins -- ++ def prepare_os_context(runner: CairoFunctionRunner) -> List[MaybeRelocatable]: -- syscall_segment = runner.segments.add() -- os_context: List[MaybeRelocatable] = [syscall_segment] +- # CAIRO-RS VERSION +- try: +- syscall_segment = runner.add_segment() +- os_context: List[MaybeRelocatable] = [syscall_segment] +- os_context.extend(runner.get_program_builtins_initial_stack()) +- # ORIGINAL VERSION +- except: +- syscall_segment = runner.segments.add() +- os_context: List[MaybeRelocatable] = [syscall_segment] - -- for builtin in runner.program.builtins: -- builtin_runner = runner.builtin_runners[f"{builtin}_builtin"] -- os_context.extend(builtin_runner.initial_stack()) -+ # CAIRO-RS VERSION -+ try: -+ syscall_segment = runner.add_segment() -+ os_context: List[MaybeRelocatable] = [syscall_segment] -+ os_context.extend(runner.get_program_builtins_initial_stack()) -+ # ORIGINAL VERSION -+ except: -+ syscall_segment = runner.segments.add() -+ os_context: List[MaybeRelocatable] = [syscall_segment] +- for builtin in runner.program.builtins: +- builtin_runner = runner.builtin_runners[f"{builtin}_builtin"] +- os_context.extend(builtin_runner.initial_stack()) ++ syscall_segment = runner.segments.add() ++ os_context: List[MaybeRelocatable] = [syscall_segment] + -+ for builtin in runner.program.builtins: -+ builtin_runner = runner.builtin_runners[f"{builtin}_builtin"] -+ os_context.extend(builtin_runner.initial_stack()) ++ for builtin in runner.program.builtins: ++ builtin_runner = runner.builtin_runners[f"{builtin}_builtin"] ++ os_context.extend(builtin_runner.initial_stack()) return os_context -- ++ def validate_and_process_os_context( runner: CairoFunctionRunner, syscall_handler: syscall_utils.BusinessLogicSysCallHandler, -@@ -64,14 +69,23 @@ def validate_and_process_os_context( +@@ -69,23 +64,14 @@ def validate_and_process_os_context( Validates and processes an OS context that was returned by a transaction. Returns the syscall processor object containing the accumulated syscall information. """ -- # The returned values are os_context, retdata_size, retdata_ptr. -- os_context_end = runner.vm.run_context.ap - 2 -- stack_ptr = os_context_end -- for builtin in runner.program.builtins[::-1]: -- builtin_runner = runner.builtin_runners[f"{builtin}_builtin"] -+ # CAIRO-RS VERSION -+ try: -+ os_context_end = runner.get_ap() - 2 -+ stack_ptr = os_context_end -+ # The returned values are os_context, retdata_size, retdata_ptr. -+ stack_ptr = runner.get_builtins_final_stack(stack_ptr) -+ # ORIGINAL VERSION -+ except: -+ os_context_end = runner.vm.run_context.ap - 2 -+ -+ stack_ptr = os_context_end - -- with wrap_with_stark_exception(code=StarknetErrorCode.SECURITY_ERROR): -- stack_ptr = builtin_runner.final_stack(runner=runner, pointer=stack_ptr) -+ for builtin in runner.program.builtins[::-1]: -+ builtin_runner = runner.builtin_runners[f"{builtin}_builtin"] -+ -+ with wrap_with_stark_exception(code=StarknetErrorCode.SECURITY_ERROR): -+ stack_ptr = builtin_runner.final_stack(runner=runner, pointer=stack_ptr) +- # CAIRO-RS VERSION +- try: +- os_context_end = runner.get_ap() - 2 +- stack_ptr = os_context_end +- # The returned values are os_context, retdata_size, retdata_ptr. +- stack_ptr = runner.get_builtins_final_stack(stack_ptr) +- # ORIGINAL VERSION +- except: +- os_context_end = runner.vm.run_context.ap - 2 +- +- stack_ptr = os_context_end ++ # The returned values are os_context, retdata_size, retdata_ptr. ++ os_context_end = runner.vm.run_context.ap - 2 ++ stack_ptr = os_context_end ++ for builtin in runner.program.builtins[::-1]: ++ builtin_runner = runner.builtin_runners[f"{builtin}_builtin"] + +- for builtin in runner.program.builtins[::-1]: +- builtin_runner = runner.builtin_runners[f"{builtin}_builtin"] +- +- with wrap_with_stark_exception(code=StarknetErrorCode.SECURITY_ERROR): +- stack_ptr = builtin_runner.final_stack(runner=runner, pointer=stack_ptr) ++ with wrap_with_stark_exception(code=StarknetErrorCode.SECURITY_ERROR): ++ stack_ptr = builtin_runner.final_stack(runner=runner, pointer=stack_ptr) final_os_context_ptr = stack_ptr - 1 assert final_os_context_ptr + len(initial_os_context) == os_context_end -@@ -81,9 +95,19 @@ def validate_and_process_os_context( +@@ -95,19 +81,9 @@ def validate_and_process_os_context( runner=runner, ptr_offset=SYSCALL_PTR_OFFSET, os_context=initial_os_context ) -- segment_utils.validate_segment_pointers( -- segments=runner.segments, -- segment_base_ptr=syscall_base_ptr, -- segment_stop_ptr=syscall_stop_ptr, -- ) -+ # ORIGINAL VERSION -+ try: -+ segment_utils.validate_segment_pointers( -+ segments=runner, -+ segment_base_ptr=syscall_base_ptr, -+ segment_stop_ptr=syscall_stop_ptr, -+ ) -+ # CAIRO-RS VERSION -+ except: -+ segment_utils.validate_segment_pointers( -+ segments=runner.segments, -+ segment_base_ptr=syscall_base_ptr, -+ segment_stop_ptr=syscall_stop_ptr, -+ ) -+ +- # ORIGINAL VERSION +- try: +- segment_utils.validate_segment_pointers( +- segments=runner, +- segment_base_ptr=syscall_base_ptr, +- segment_stop_ptr=syscall_stop_ptr, +- ) +- # CAIRO-RS VERSION +- except: +- segment_utils.validate_segment_pointers( +- segments=runner.segments, +- segment_base_ptr=syscall_base_ptr, +- segment_stop_ptr=syscall_stop_ptr, +- ) +- ++ segment_utils.validate_segment_pointers( ++ segments=runner.segments, ++ segment_base_ptr=syscall_base_ptr, ++ segment_stop_ptr=syscall_stop_ptr, ++ ) syscall_handler.post_run(runner=runner, syscall_stop_ptr=syscall_stop_ptr) diff --git a/src/starkware/starknet/core/os/segment_utils.py b/src/starkware/starknet/core/os/segment_utils.py -index 1d09414..33f5c26 100644 +index 33f5c26..1d09414 100644 --- a/src/starkware/starknet/core/os/segment_utils.py +++ b/src/starkware/starknet/core/os/segment_utils.py @@ -8,7 +8,7 @@ from starkware.starknet.definitions.error_codes import StarknetErrorCode from starkware.starknet.public.abi import SYSCALL_PTR_OFFSET from starkware.starkware_utils.error_handling import stark_assert, wrap_with_stark_exception -- -+# LAMBDA MODIFIED +-# LAMBDA MODIFIED ++ def get_os_segment_ptr_range( runner: CairoFunctionRunner, ptr_offset: int, os_context: List[MaybeRelocatable] ) -> Tuple[MaybeRelocatable, MaybeRelocatable]: -@@ -21,10 +21,23 @@ def get_os_segment_ptr_range( +@@ -21,23 +21,10 @@ def get_os_segment_ptr_range( ), f"Illegal OS ptr offset; must be one of: {allowed_offsets}." # The returned values are os_context, retdata_size, retdata_ptr. -- os_context_end = runner.vm.run_context.ap - 2 -+ # CAIRO-RS VERSION -+ try: -+ os_context_end = runner.get_ap() - 2 -+ except: -+ # ORIGINAL VERSION -+ os_context_end = runner.vm.run_context.ap - 2 -+ +- # CAIRO-RS VERSION +- try: +- os_context_end = runner.get_ap() - 2 +- except: +- # ORIGINAL VERSION +- os_context_end = runner.vm.run_context.ap - 2 +- ++ os_context_end = runner.vm.run_context.ap - 2 final_os_context_ptr = os_context_end - len(os_context) -- return os_context[ptr_offset], runner.vm_memory[final_os_context_ptr + ptr_offset] -+ # CAIRO-RS VERSION -+ try: -+ return os_context[ptr_offset], runner.get(final_os_context_ptr + ptr_offset) -+ # ORIGINAL VERSION -+ except: -+ return os_context[ptr_offset], runner.vm_memory[final_os_context_ptr + ptr_offset] -+ -+ +- # CAIRO-RS VERSION +- try: +- return os_context[ptr_offset], runner.get(final_os_context_ptr + ptr_offset) +- # ORIGINAL VERSION +- except: +- return os_context[ptr_offset], runner.vm_memory[final_os_context_ptr + ptr_offset] +- +- ++ return os_context[ptr_offset], runner.vm_memory[final_os_context_ptr + ptr_offset] def get_os_segment_stop_ptr( -@@ -61,14 +74,19 @@ def validate_segment_pointers( +@@ -74,19 +61,14 @@ def validate_segment_pointers( segment_base_ptr: MaybeRelocatable, segment_stop_ptr: MaybeRelocatable, ): -- assert isinstance(segment_base_ptr, RelocatableValue) -+ # assert isinstance(segment_base_ptr, RelocatableValue) +- # assert isinstance(segment_base_ptr, RelocatableValue) ++ assert isinstance(segment_base_ptr, RelocatableValue) assert ( segment_base_ptr.offset == 0 ), f"Segment base pointer must be zero; got {segment_base_ptr.offset}." -- expected_stop_ptr = segment_base_ptr + segments.get_segment_used_size( -- segment_index=segment_base_ptr.segment_index -- ) -+ # CAIRO-RS VERSION -+ try: -+ expected_stop_ptr = segment_base_ptr + segments.get_segment_used_size( -+ index=segment_base_ptr.segment_index) -+ # ORIGINAL VERSION -+ except: -+ expected_stop_ptr = segment_base_ptr + segments.get_segment_used_size( -+ segment_index=segment_base_ptr.segment_index) +- # CAIRO-RS VERSION +- try: +- expected_stop_ptr = segment_base_ptr + segments.get_segment_used_size( +- index=segment_base_ptr.segment_index) +- # ORIGINAL VERSION +- except: +- expected_stop_ptr = segment_base_ptr + segments.get_segment_used_size( +- segment_index=segment_base_ptr.segment_index) ++ expected_stop_ptr = segment_base_ptr + segments.get_segment_used_size( ++ segment_index=segment_base_ptr.segment_index ++ ) stark_assert( expected_stop_ptr == segment_stop_ptr, diff --git a/src/starkware/starknet/core/os/syscall_utils.py b/src/starkware/starknet/core/os/syscall_utils.py -index e44635b..c129e50 100644 +index c129e50..e44635b 100644 --- a/src/starkware/starknet/core/os/syscall_utils.py +++ b/src/starkware/starknet/core/os/syscall_utils.py -@@ -458,7 +458,6 @@ class SysCallHandlerBase(ABC): +@@ -458,6 +458,7 @@ class SysCallHandlerBase(ABC): ) -> List[int]: """ Returns the call retdata. -- ++ syscall_name can be "call_contract", "delegate_call", "delegate_l1_handler", "library_call" or "library_call_l1_handler". """ -@@ -589,7 +588,16 @@ class BusinessLogicSysCallHandler(SysCallHandlerBase): +@@ -588,16 +589,7 @@ class BusinessLogicSysCallHandler(SysCallHandlerBase): def _allocate_segment( self, segments: MemorySegmentManager, data: Iterable[MaybeRelocatable] ) -> RelocatableValue: -- segment_start = segments.add() -+ # FIXME: Here "segments" in really a Runner under the hood. -+ # May want to change the variable names. -+ -+ # CAIRO-RS VERSION -+ try: -+ segment_start = segments.add_segment() -+ # ORIGINAL VERSION -+ except: -+ segment_start = segments.add() -+ +- # FIXME: Here "segments" in really a Runner under the hood. +- # May want to change the variable names. +- +- # CAIRO-RS VERSION +- try: +- segment_start = segments.add_segment() +- # ORIGINAL VERSION +- except: +- segment_start = segments.add() +- ++ segment_start = segments.add() segment_end = segments.write_arg(ptr=segment_start, arg=data) self.read_only_segments.append((segment_start, segment_end - segment_start)) return segment_start -@@ -631,10 +639,10 @@ class BusinessLogicSysCallHandler(SysCallHandlerBase): +@@ -639,10 +631,10 @@ class BusinessLogicSysCallHandler(SysCallHandlerBase): args_struct_def: StructDefinition = syscall_info.syscall_request_struct.struct_definition_ for arg, (arg_name, arg_def) in safe_zip(request, args_struct_def.members.items()): expected_type = get_runtime_type(arg_def.cairo_type) -- assert isinstance(arg, expected_type), ( -- f"Argument {arg_name} to syscall {syscall_name} is of unexpected type. " -- f"Expected: value of type {expected_type}; got: {arg}." -- ) -+ # assert isinstance(arg, expected_type), ( -+ # f"Argument {arg_name} to syscall {syscall_name} is of unexpected type. " -+ # f"Expected: value of type {expected_type}; got: {arg}." -+ # ) +- # assert isinstance(arg, expected_type), ( +- # f"Argument {arg_name} to syscall {syscall_name} is of unexpected type. " +- # f"Expected: value of type {expected_type}; got: {arg}." +- # ) ++ assert isinstance(arg, expected_type), ( ++ f"Argument {arg_name} to syscall {syscall_name} is of unexpected type. " ++ f"Expected: value of type {expected_type}; got: {arg}." ++ ) return request -@@ -902,10 +910,20 @@ class BusinessLogicSysCallHandler(SysCallHandlerBase): +@@ -910,20 +902,10 @@ class BusinessLogicSysCallHandler(SysCallHandlerBase): Validates that there were no out of bounds writes to read-only segments and marks them as accessed. """ -- segments = runner.segments -+ # ORIGINAL VERSION -+ try: -+ segments = runner.segments -+ # CAIRO-RS VERSION -+ except: -+ segments = runner +- # ORIGINAL VERSION +- try: +- segments = runner.segments +- # CAIRO-RS VERSION +- except: +- segments = runner ++ segments = runner.segments for segment_ptr, segment_size in self.read_only_segments: -- used_size = segments.get_segment_used_size(segment_index=segment_ptr.segment_index) -+ # CAIRO-RS VERSION -+ try: -+ used_size = segments.get_segment_used_size(index=segment_ptr.segment_index) -+ # ORIGINAL VERSION -+ except: -+ used_size = segments.get_segment_used_size(segment_index=segment_ptr.segment_index) +- # CAIRO-RS VERSION +- try: +- used_size = segments.get_segment_used_size(index=segment_ptr.segment_index) +- # ORIGINAL VERSION +- except: +- used_size = segments.get_segment_used_size(segment_index=segment_ptr.segment_index) ++ used_size = segments.get_segment_used_size(segment_index=segment_ptr.segment_index) stark_assert( used_size == segment_size, code=StarknetErrorCode.SECURITY_ERROR, -@@ -1041,7 +1059,6 @@ class OsSysCallHandler(SysCallHandlerBase): +@@ -1059,6 +1041,7 @@ class OsSysCallHandler(SysCallHandlerBase): def start_tx(self, tx_info_ptr: RelocatableValue): """ Called when starting the execution of a transaction. -- ++ 'tx_info_ptr' is a pointer to the TxInfo struct corresponding to said transaction. """ assert self.tx_info_ptr is None -@@ -1089,4 +1106,4 @@ class OsSysCallHandler(SysCallHandlerBase): +@@ -1106,4 +1089,4 @@ class OsSysCallHandler(SysCallHandlerBase): Called when skipping the execution of a transaction. It replaces a call to start_tx and end_tx. """ - next(self.tx_execution_info_iterator) -+ next(self.tx_execution_info_iterator) \ No newline at end of file ++ next(self.tx_execution_info_iterator) From 4850a57b2431643034f3e77e74e4aa82db795dbc Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 1 Dec 2022 16:58:53 -0300 Subject: [PATCH 42/57] Fix patch --- .../output-memory-cairo-rs-py.patch | 660 +++++++++--------- 1 file changed, 330 insertions(+), 330 deletions(-) diff --git a/scripts/memory_comparator/output-memory-cairo-rs-py.patch b/scripts/memory_comparator/output-memory-cairo-rs-py.patch index 64fe8902..895315e2 100644 --- a/scripts/memory_comparator/output-memory-cairo-rs-py.patch +++ b/scripts/memory_comparator/output-memory-cairo-rs-py.patch @@ -1,516 +1,516 @@ diff --git a/src/starkware/starknet/business_logic/execution/execute_entry_point.py b/src/starkware/starknet/business_logic/execution/execute_entry_point.py -index 719208f..62d4290 100644 +index 62d4290..719208f 100644 --- a/src/starkware/starknet/business_logic/execution/execute_entry_point.py +++ b/src/starkware/starknet/business_logic/execution/execute_entry_point.py -@@ -41,7 +41,6 @@ from starkware.starkware_utils.error_handling import ( +@@ -41,6 +41,7 @@ from starkware.starkware_utils.error_handling import ( stark_assert, wrap_with_stark_exception, ) --import cairo_rs_py ++import cairo_rs_py logger = logging.getLogger(__name__) -@@ -183,8 +182,7 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): +@@ -182,7 +183,8 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): # Run the specified contract entry point with given calldata. with wrap_with_stark_exception(code=StarknetErrorCode.SECURITY_ERROR): -- runner = cairo_rs_py.CairoRunner(program=contract_class.program.dumps(), entrypoint=None, layout="all", proof_mode=False) -- runner.initialize_function_runner() -+ runner = CairoFunctionRunner(program=contract_class.program, layout="all") +- runner = CairoFunctionRunner(program=contract_class.program, layout="all") ++ runner = cairo_rs_py.CairoRunner(program=contract_class.program.dumps(), entrypoint=None, layout="all", proof_mode=False) ++ runner.initialize_function_runner() os_context = os_utils.prepare_os_context(runner=runner) validate_contract_deployed(state=state, contract_address=self.contract_address) -@@ -207,24 +205,24 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): +@@ -205,24 +207,24 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): os_context, len(self.calldata), # Allocate and mark the segment as read-only (to mark every input array as read-only). -- syscall_handler._allocate_segment(segments=runner, data=self.calldata), -+ syscall_handler._allocate_segment(segments=runner.segments, data=self.calldata), +- syscall_handler._allocate_segment(segments=runner.segments, data=self.calldata), ++ syscall_handler._allocate_segment(segments=runner, data=self.calldata), ] try: runner.run_from_entrypoint( entry_point.offset, -- entry_points_args, -+ *entry_points_args, +- *entry_points_args, ++ entry_points_args, hint_locals={ "syscall_handler": syscall_handler, }, -- # static_locals={ -- # "__find_element_max_size": 2**20, -- # "__squash_dict_max_size": 2**20, -- # "__keccak_max_size": 2**20, -- # "__usort_max_size": 2**20, -- # "__chained_ec_op_max_len": 1000, -- # }, -- # run_resources=tx_execution_context.run_resources, -+ static_locals={ -+ "__find_element_max_size": 2**20, -+ "__squash_dict_max_size": 2**20, -+ "__keccak_max_size": 2**20, -+ "__usort_max_size": 2**20, -+ "__chained_ec_op_max_len": 1000, -+ }, -+ run_resources=tx_execution_context.run_resources, +- static_locals={ +- "__find_element_max_size": 2**20, +- "__squash_dict_max_size": 2**20, +- "__keccak_max_size": 2**20, +- "__usort_max_size": 2**20, +- "__chained_ec_op_max_len": 1000, +- }, +- run_resources=tx_execution_context.run_resources, ++ # static_locals={ ++ # "__find_element_max_size": 2**20, ++ # "__squash_dict_max_size": 2**20, ++ # "__keccak_max_size": 2**20, ++ # "__usort_max_size": 2**20, ++ # "__chained_ec_op_max_len": 1000, ++ # }, ++ # run_resources=tx_execution_context.run_resources, verify_secure=True, ) except VmException as exception: -@@ -273,10 +271,9 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): +@@ -271,9 +273,10 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): # The arguments are touched by the OS and should not be counted as holes, mark them # as accessed. -- # assert isinstance(args_ptr, RelocatableValue) # Downcast. -+ assert isinstance(args_ptr, RelocatableValue) # Downcast. +- assert isinstance(args_ptr, RelocatableValue) # Downcast. ++ # assert isinstance(args_ptr, RelocatableValue) # Downcast. runner.mark_as_accessed(address=args_ptr, size=len(entry_points_args)) -- runner.relocate() -- runner.write_binary_memory("memory_files/execute_entry_point.rs.memory") -+ +- ++ runner.relocate() ++ runner.write_binary_memory("memory_files/execute_entry_point.rs.memory") return runner, syscall_handler def _get_selected_entry_point( -@@ -358,4 +355,4 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): +@@ -355,4 +358,4 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): raise NotImplementedError(f"Call type {self.call_type} not implemented.") # Extract pre-fetched contract code from carried state. - return get_deployed_class_hash_at_address(state=state, contract_address=code_address) -\ No newline at end of file + return get_deployed_class_hash_at_address(state=state, contract_address=code_address) +\ No newline at end of file diff --git a/src/starkware/starknet/business_logic/transaction/fee.py b/src/starkware/starknet/business_logic/transaction/fee.py -index 9bbb9f5..9acaa73 100644 +index 9acaa73..9bbb9f5 100644 --- a/src/starkware/starknet/business_logic/transaction/fee.py +++ b/src/starkware/starknet/business_logic/transaction/fee.py @@ -67,9 +67,9 @@ def calculate_l1_gas_by_cairo_usage( """ cairo_resource_fee_weights = general_config.cairo_resource_fee_weights cairo_resource_names = set(cairo_resource_usage.keys()) -- # assert cairo_resource_names.issubset( -- # cairo_resource_fee_weights.keys() -- # ), "Cairo resource names must be contained in fee weights dict." -+ assert cairo_resource_names.issubset( -+ cairo_resource_fee_weights.keys() -+ ), "Cairo resource names must be contained in fee weights dict." +- assert cairo_resource_names.issubset( +- cairo_resource_fee_weights.keys() +- ), "Cairo resource names must be contained in fee weights dict." ++ # assert cairo_resource_names.issubset( ++ # cairo_resource_fee_weights.keys() ++ # ), "Cairo resource names must be contained in fee weights dict." # Convert Cairo usage to L1 gas usage. cairo_l1_gas_usage = max( diff --git a/src/starkware/starknet/business_logic/utils.py b/src/starkware/starknet/business_logic/utils.py -index 15660fe..f63bc9f 100644 +index f63bc9f..15660fe 100644 --- a/src/starkware/starknet/business_logic/utils.py +++ b/src/starkware/starknet/business_logic/utils.py -@@ -48,14 +48,7 @@ def get_return_values(runner: CairoFunctionRunner) -> List[int]: +@@ -48,7 +48,14 @@ def get_return_values(runner: CairoFunctionRunner) -> List[int]: exception_types=[Exception], ): ret_data_size, ret_data_ptr = runner.get_return_values(2) -- -- try: -- # CAIRO-RS VERSION -- values = runner.get_range(ret_data_ptr, ret_data_size) -- except: -- # ORIGINAL VERSION -- values = runner.memory.get_range(ret_data_ptr, ret_data_size) -- -+ values = runner.memory.get_range(ret_data_ptr, ret_data_size) +- values = runner.memory.get_range(ret_data_ptr, ret_data_size) ++ ++ try: ++ # CAIRO-RS VERSION ++ values = runner.get_range(ret_data_ptr, ret_data_size) ++ except: ++ # ORIGINAL VERSION ++ values = runner.memory.get_range(ret_data_ptr, ret_data_size) ++ stark_assert( all(isinstance(value, int) for value in values), diff --git a/src/starkware/starknet/core/os/class_hash.py b/src/starkware/starknet/core/os/class_hash.py -index 3cf48a8..132fb21 100644 +index 132fb21..3cf48a8 100644 --- a/src/starkware/starknet/core/os/class_hash.py +++ b/src/starkware/starknet/core/os/class_hash.py -@@ -5,10 +5,9 @@ import json +@@ -5,9 +5,10 @@ import json import os from contextvars import ContextVar from functools import lru_cache --from typing import Any, Callable, Dict, List, Optional, Tuple -+from typing import Callable, List, Optional +-from typing import Callable, List, Optional ++from typing import Any, Callable, Dict, List, Optional, Tuple import cachetools --import cairo_rs_py ++import cairo_rs_py from starkware.cairo.common.cairo_function_runner import CairoFunctionRunner from starkware.cairo.common.structs import CairoStructFactory, CairoStructProxy -@@ -24,10 +23,6 @@ from starkware.cairo.lang.vm.crypto import pedersen_hash +@@ -23,6 +24,10 @@ from starkware.cairo.lang.vm.crypto import pedersen_hash from starkware.python.utils import from_bytes from starkware.starknet.public.abi import starknet_keccak from starkware.starknet.services.api.contract_class import ContractClass, EntryPointType --# Added Imports --from starkware.cairo.lang.vm.relocatable import MaybeRelocatable, RelocatableValue --from starkware.cairo.lang.vm.vm_exceptions import SecurityError, VmException --from starkware.python.utils import safe_zip ++# Added Imports ++from starkware.cairo.lang.vm.relocatable import MaybeRelocatable, RelocatableValue ++from starkware.cairo.lang.vm.vm_exceptions import SecurityError, VmException ++from starkware.python.utils import safe_zip CAIRO_FILE = os.path.join(os.path.dirname(__file__), "contracts.cairo") -@@ -82,24 +77,22 @@ def compute_class_hash_inner( +@@ -77,22 +82,24 @@ def compute_class_hash_inner( contract_class_struct = get_contract_class_struct( identifiers=program.identifiers, contract_class=contract_class ) -+ runner = CairoFunctionRunner(program) +- runner = CairoFunctionRunner(program) + +- hash_builtin = HashBuiltinRunner( +- name="custom_hasher", included=True, ratio=32, hash_func=hash_func +- ) +- runner.builtin_runners["hash_builtin"] = hash_builtin +- hash_builtin.initialize_segments(runner) ++ runner = cairo_rs_py.CairoRunner(program=program.dumps(), entrypoint=None, layout="all", proof_mode=False) ++ runner.initialize_function_runner() ++ hash_ptr = runner.add_additional_hash_builtin() ++ -- runner = cairo_rs_py.CairoRunner(program=program.dumps(), entrypoint=None, layout="all", proof_mode=False) -- runner.initialize_function_runner() -- hash_ptr = runner.add_additional_hash_builtin() -- -+ hash_builtin = HashBuiltinRunner( -+ name="custom_hasher", included=True, ratio=32, hash_func=hash_func -+ ) -+ runner.builtin_runners["hash_builtin"] = hash_builtin -+ hash_builtin.initialize_segments(runner) - -- run_function_runner( -- runner, -- program, -+ runner.run( +- runner.run( ++ run_function_runner( ++ runner, ++ program, "starkware.starknet.core.os.contracts.class_hash", -- hash_ptr=hash_ptr, -+ hash_ptr=hash_builtin.base, +- hash_ptr=hash_builtin.base, ++ hash_ptr=hash_ptr, contract_class=contract_class_struct, use_full_name=True, verify_secure=False, ) _, class_hash = runner.get_return_values(2) -- runner.relocate() -- runner.write_binary_memory("memory_files/class_hash.rs.memory") ++ runner.relocate() ++ runner.write_binary_memory("memory_files/class_hash.rs.memory") return class_hash -@@ -201,103 +194,3 @@ def get_contract_class_struct( +@@ -194,3 +201,103 @@ def get_contract_class_struct( bytecode_length=len(contract_class.program.data), bytecode_ptr=contract_class.program.data, ) -- --def run_function_runner( -- runner, -- program, -- func_name: str, -- *args, -- hint_locals: Optional[Dict[str, Any]] = None, -- static_locals: Optional[Dict[str, Any]] = None, -- verify_secure: Optional[bool] = None, -- trace_on_failure: bool = False, -- apply_modulo_to_args: Optional[bool] = None, -- use_full_name: bool = False, -- verify_implicit_args_segment: bool = False, -- **kwargs, -- ) -> Tuple[Tuple[MaybeRelocatable, ...], Tuple[MaybeRelocatable, ...]]: -- """ -- Runs func_name(*args). -- args are converted to Cairo-friendly ones using gen_arg. -- -- Returns the return values of the function, splitted into 2 tuples of implicit values and -- explicit values. Structs will be flattened to a sequence of felts as part of the returned -- tuple. -- -- Additional params: -- verify_secure - Run verify_secure_runner to do extra verifications. -- trace_on_failure - Run the tracer in case of failure to help debugging. -- apply_modulo_to_args - Apply modulo operation on integer arguments. -- use_full_name - Treat 'func_name' as a fully qualified identifier name, rather than a -- relative one. -- verify_implicit_args_segment - For each implicit argument, verify that the argument and the -- return value are in the same segment. -- """ -- assert isinstance(program, Program) -- entrypoint = program.get_label(func_name, full_name_lookup=use_full_name) -- -- #Construct Fu -- structs_factory = CairoStructFactory.from_program(program=program) -- func = ScopedName.from_string(scope=func_name) -- -- full_args_struct = structs_factory.build_func_args(func=func) -- all_args = full_args_struct(*args, **kwargs) -- -- try: -- runner.run_from_entrypoint( -- entrypoint, -- all_args, -- typed_args=True, -- hint_locals=hint_locals, -- static_locals=static_locals, -- verify_secure=verify_secure, -- apply_modulo_to_args=apply_modulo_to_args, -- ) -- except (VmException, SecurityError, AssertionError) as ex: -- if trace_on_failure: -- print( -- f"""\ --Got {type(ex).__name__} exception during the execution of {func_name}: --{str(ex)} --""" -- ) -- #trace_runner(runner=runner) -- raise -- -- # The number of implicit arguments is identical to the number of implicit return values. -- n_implicit_ret_vals = structs_factory.get_implicit_args_length(func=func) -- n_explicit_ret_vals = structs_factory.get_explicit_return_values_length(func=func) -- n_ret_vals = n_explicit_ret_vals + n_implicit_ret_vals -- implicit_retvals = tuple( -- runner.get_range( -- runner.get_ap() - n_ret_vals, n_implicit_ret_vals -- ) -- ) -- -- explicit_retvals = tuple( -- runner.get_range( -- runner.get_ap() - n_explicit_ret_vals, n_explicit_ret_vals -- ) -- ) -- -- # Verify the memory segments of the implicit arguments. -- if verify_implicit_args_segment: -- implicit_args = all_args[:n_implicit_ret_vals] -- for implicit_arg, implicit_retval in safe_zip(implicit_args, implicit_retvals): -- assert isinstance( -- implicit_arg, RelocatableValue -- ), f"Implicit arguments must be RelocatableValues, {implicit_arg} is not." -- assert isinstance(implicit_retval, RelocatableValue), ( -- f"Argument {implicit_arg} is a RelocatableValue, but the returned value " -- f"{implicit_retval} is not." -- ) -- assert implicit_arg.segment_index == implicit_retval.segment_index, ( -- f"Implicit argument {implicit_arg} is not on the same segment as the returned " -- f"{implicit_retval}." -- ) -- assert implicit_retval.offset >= implicit_arg.offset, ( -- f"The offset of the returned implicit argument {implicit_retval} is less than " -- f"the offset of the input {implicit_arg}." -- ) -- -- return implicit_retvals, explicit_retvals ++ ++def run_function_runner( ++ runner, ++ program, ++ func_name: str, ++ *args, ++ hint_locals: Optional[Dict[str, Any]] = None, ++ static_locals: Optional[Dict[str, Any]] = None, ++ verify_secure: Optional[bool] = None, ++ trace_on_failure: bool = False, ++ apply_modulo_to_args: Optional[bool] = None, ++ use_full_name: bool = False, ++ verify_implicit_args_segment: bool = False, ++ **kwargs, ++ ) -> Tuple[Tuple[MaybeRelocatable, ...], Tuple[MaybeRelocatable, ...]]: ++ """ ++ Runs func_name(*args). ++ args are converted to Cairo-friendly ones using gen_arg. ++ ++ Returns the return values of the function, splitted into 2 tuples of implicit values and ++ explicit values. Structs will be flattened to a sequence of felts as part of the returned ++ tuple. ++ ++ Additional params: ++ verify_secure - Run verify_secure_runner to do extra verifications. ++ trace_on_failure - Run the tracer in case of failure to help debugging. ++ apply_modulo_to_args - Apply modulo operation on integer arguments. ++ use_full_name - Treat 'func_name' as a fully qualified identifier name, rather than a ++ relative one. ++ verify_implicit_args_segment - For each implicit argument, verify that the argument and the ++ return value are in the same segment. ++ """ ++ assert isinstance(program, Program) ++ entrypoint = program.get_label(func_name, full_name_lookup=use_full_name) ++ ++ #Construct Fu ++ structs_factory = CairoStructFactory.from_program(program=program) ++ func = ScopedName.from_string(scope=func_name) ++ ++ full_args_struct = structs_factory.build_func_args(func=func) ++ all_args = full_args_struct(*args, **kwargs) ++ ++ try: ++ runner.run_from_entrypoint( ++ entrypoint, ++ all_args, ++ typed_args=True, ++ hint_locals=hint_locals, ++ static_locals=static_locals, ++ verify_secure=verify_secure, ++ apply_modulo_to_args=apply_modulo_to_args, ++ ) ++ except (VmException, SecurityError, AssertionError) as ex: ++ if trace_on_failure: ++ print( ++ f"""\ ++Got {type(ex).__name__} exception during the execution of {func_name}: ++{str(ex)} ++""" ++ ) ++ #trace_runner(runner=runner) ++ raise ++ ++ # The number of implicit arguments is identical to the number of implicit return values. ++ n_implicit_ret_vals = structs_factory.get_implicit_args_length(func=func) ++ n_explicit_ret_vals = structs_factory.get_explicit_return_values_length(func=func) ++ n_ret_vals = n_explicit_ret_vals + n_implicit_ret_vals ++ implicit_retvals = tuple( ++ runner.get_range( ++ runner.get_ap() - n_ret_vals, n_implicit_ret_vals ++ ) ++ ) ++ ++ explicit_retvals = tuple( ++ runner.get_range( ++ runner.get_ap() - n_explicit_ret_vals, n_explicit_ret_vals ++ ) ++ ) ++ ++ # Verify the memory segments of the implicit arguments. ++ if verify_implicit_args_segment: ++ implicit_args = all_args[:n_implicit_ret_vals] ++ for implicit_arg, implicit_retval in safe_zip(implicit_args, implicit_retvals): ++ assert isinstance( ++ implicit_arg, RelocatableValue ++ ), f"Implicit arguments must be RelocatableValues, {implicit_arg} is not." ++ assert isinstance(implicit_retval, RelocatableValue), ( ++ f"Argument {implicit_arg} is a RelocatableValue, but the returned value " ++ f"{implicit_retval} is not." ++ ) ++ assert implicit_arg.segment_index == implicit_retval.segment_index, ( ++ f"Implicit argument {implicit_arg} is not on the same segment as the returned " ++ f"{implicit_retval}." ++ ) ++ assert implicit_retval.offset >= implicit_arg.offset, ( ++ f"The offset of the returned implicit argument {implicit_retval} is less than " ++ f"the offset of the input {implicit_arg}." ++ ) ++ ++ return implicit_retvals, explicit_retvals diff --git a/src/starkware/starknet/core/os/os_utils.py b/src/starkware/starknet/core/os/os_utils.py -index 0ea99f4..20bd521 100644 +index 20bd521..0ea99f4 100644 --- a/src/starkware/starknet/core/os/os_utils.py +++ b/src/starkware/starknet/core/os/os_utils.py -@@ -43,23 +43,18 @@ def update_builtin_pointers( +@@ -43,18 +43,23 @@ def update_builtin_pointers( return return_builtins -+ +- def prepare_os_context(runner: CairoFunctionRunner) -> List[MaybeRelocatable]: -- # CAIRO-RS VERSION -- try: -- syscall_segment = runner.add_segment() -- os_context: List[MaybeRelocatable] = [syscall_segment] -- os_context.extend(runner.get_program_builtins_initial_stack()) -- # ORIGINAL VERSION -- except: -- syscall_segment = runner.segments.add() -- os_context: List[MaybeRelocatable] = [syscall_segment] +- syscall_segment = runner.segments.add() +- os_context: List[MaybeRelocatable] = [syscall_segment] - -- for builtin in runner.program.builtins: -- builtin_runner = runner.builtin_runners[f"{builtin}_builtin"] -- os_context.extend(builtin_runner.initial_stack()) -+ syscall_segment = runner.segments.add() -+ os_context: List[MaybeRelocatable] = [syscall_segment] +- for builtin in runner.program.builtins: +- builtin_runner = runner.builtin_runners[f"{builtin}_builtin"] +- os_context.extend(builtin_runner.initial_stack()) ++ # CAIRO-RS VERSION ++ try: ++ syscall_segment = runner.add_segment() ++ os_context: List[MaybeRelocatable] = [syscall_segment] ++ os_context.extend(runner.get_program_builtins_initial_stack()) ++ # ORIGINAL VERSION ++ except: ++ syscall_segment = runner.segments.add() ++ os_context: List[MaybeRelocatable] = [syscall_segment] + -+ for builtin in runner.program.builtins: -+ builtin_runner = runner.builtin_runners[f"{builtin}_builtin"] -+ os_context.extend(builtin_runner.initial_stack()) ++ for builtin in runner.program.builtins: ++ builtin_runner = runner.builtin_runners[f"{builtin}_builtin"] ++ os_context.extend(builtin_runner.initial_stack()) return os_context -+ +- def validate_and_process_os_context( runner: CairoFunctionRunner, syscall_handler: syscall_utils.BusinessLogicSysCallHandler, -@@ -69,23 +64,14 @@ def validate_and_process_os_context( +@@ -64,14 +69,23 @@ def validate_and_process_os_context( Validates and processes an OS context that was returned by a transaction. Returns the syscall processor object containing the accumulated syscall information. """ -- # CAIRO-RS VERSION -- try: -- os_context_end = runner.get_ap() - 2 -- stack_ptr = os_context_end -- # The returned values are os_context, retdata_size, retdata_ptr. -- stack_ptr = runner.get_builtins_final_stack(stack_ptr) -- # ORIGINAL VERSION -- except: -- os_context_end = runner.vm.run_context.ap - 2 -- -- stack_ptr = os_context_end -+ # The returned values are os_context, retdata_size, retdata_ptr. -+ os_context_end = runner.vm.run_context.ap - 2 -+ stack_ptr = os_context_end -+ for builtin in runner.program.builtins[::-1]: -+ builtin_runner = runner.builtin_runners[f"{builtin}_builtin"] - -- for builtin in runner.program.builtins[::-1]: -- builtin_runner = runner.builtin_runners[f"{builtin}_builtin"] -- -- with wrap_with_stark_exception(code=StarknetErrorCode.SECURITY_ERROR): -- stack_ptr = builtin_runner.final_stack(runner=runner, pointer=stack_ptr) -+ with wrap_with_stark_exception(code=StarknetErrorCode.SECURITY_ERROR): -+ stack_ptr = builtin_runner.final_stack(runner=runner, pointer=stack_ptr) +- # The returned values are os_context, retdata_size, retdata_ptr. +- os_context_end = runner.vm.run_context.ap - 2 +- stack_ptr = os_context_end +- for builtin in runner.program.builtins[::-1]: +- builtin_runner = runner.builtin_runners[f"{builtin}_builtin"] ++ # CAIRO-RS VERSION ++ try: ++ os_context_end = runner.get_ap() - 2 ++ stack_ptr = os_context_end ++ # The returned values are os_context, retdata_size, retdata_ptr. ++ stack_ptr = runner.get_builtins_final_stack(stack_ptr) ++ # ORIGINAL VERSION ++ except: ++ os_context_end = runner.vm.run_context.ap - 2 ++ ++ stack_ptr = os_context_end + +- with wrap_with_stark_exception(code=StarknetErrorCode.SECURITY_ERROR): +- stack_ptr = builtin_runner.final_stack(runner=runner, pointer=stack_ptr) ++ for builtin in runner.program.builtins[::-1]: ++ builtin_runner = runner.builtin_runners[f"{builtin}_builtin"] ++ ++ with wrap_with_stark_exception(code=StarknetErrorCode.SECURITY_ERROR): ++ stack_ptr = builtin_runner.final_stack(runner=runner, pointer=stack_ptr) final_os_context_ptr = stack_ptr - 1 assert final_os_context_ptr + len(initial_os_context) == os_context_end -@@ -95,19 +81,9 @@ def validate_and_process_os_context( +@@ -81,9 +95,19 @@ def validate_and_process_os_context( runner=runner, ptr_offset=SYSCALL_PTR_OFFSET, os_context=initial_os_context ) -- # ORIGINAL VERSION -- try: -- segment_utils.validate_segment_pointers( -- segments=runner, -- segment_base_ptr=syscall_base_ptr, -- segment_stop_ptr=syscall_stop_ptr, -- ) -- # CAIRO-RS VERSION -- except: -- segment_utils.validate_segment_pointers( -- segments=runner.segments, -- segment_base_ptr=syscall_base_ptr, -- segment_stop_ptr=syscall_stop_ptr, -- ) -- -+ segment_utils.validate_segment_pointers( -+ segments=runner.segments, -+ segment_base_ptr=syscall_base_ptr, -+ segment_stop_ptr=syscall_stop_ptr, -+ ) +- segment_utils.validate_segment_pointers( +- segments=runner.segments, +- segment_base_ptr=syscall_base_ptr, +- segment_stop_ptr=syscall_stop_ptr, +- ) ++ # ORIGINAL VERSION ++ try: ++ segment_utils.validate_segment_pointers( ++ segments=runner, ++ segment_base_ptr=syscall_base_ptr, ++ segment_stop_ptr=syscall_stop_ptr, ++ ) ++ # CAIRO-RS VERSION ++ except: ++ segment_utils.validate_segment_pointers( ++ segments=runner.segments, ++ segment_base_ptr=syscall_base_ptr, ++ segment_stop_ptr=syscall_stop_ptr, ++ ) ++ syscall_handler.post_run(runner=runner, syscall_stop_ptr=syscall_stop_ptr) diff --git a/src/starkware/starknet/core/os/segment_utils.py b/src/starkware/starknet/core/os/segment_utils.py -index 33f5c26..1d09414 100644 +index 1d09414..33f5c26 100644 --- a/src/starkware/starknet/core/os/segment_utils.py +++ b/src/starkware/starknet/core/os/segment_utils.py @@ -8,7 +8,7 @@ from starkware.starknet.definitions.error_codes import StarknetErrorCode from starkware.starknet.public.abi import SYSCALL_PTR_OFFSET from starkware.starkware_utils.error_handling import stark_assert, wrap_with_stark_exception --# LAMBDA MODIFIED -+ +- ++# LAMBDA MODIFIED def get_os_segment_ptr_range( runner: CairoFunctionRunner, ptr_offset: int, os_context: List[MaybeRelocatable] ) -> Tuple[MaybeRelocatable, MaybeRelocatable]: -@@ -21,23 +21,10 @@ def get_os_segment_ptr_range( +@@ -21,10 +21,23 @@ def get_os_segment_ptr_range( ), f"Illegal OS ptr offset; must be one of: {allowed_offsets}." # The returned values are os_context, retdata_size, retdata_ptr. -- # CAIRO-RS VERSION -- try: -- os_context_end = runner.get_ap() - 2 -- except: -- # ORIGINAL VERSION -- os_context_end = runner.vm.run_context.ap - 2 -- -+ os_context_end = runner.vm.run_context.ap - 2 +- os_context_end = runner.vm.run_context.ap - 2 ++ # CAIRO-RS VERSION ++ try: ++ os_context_end = runner.get_ap() - 2 ++ except: ++ # ORIGINAL VERSION ++ os_context_end = runner.vm.run_context.ap - 2 ++ final_os_context_ptr = os_context_end - len(os_context) -- # CAIRO-RS VERSION -- try: -- return os_context[ptr_offset], runner.get(final_os_context_ptr + ptr_offset) -- # ORIGINAL VERSION -- except: -- return os_context[ptr_offset], runner.vm_memory[final_os_context_ptr + ptr_offset] -- -- -+ return os_context[ptr_offset], runner.vm_memory[final_os_context_ptr + ptr_offset] +- return os_context[ptr_offset], runner.vm_memory[final_os_context_ptr + ptr_offset] ++ # CAIRO-RS VERSION ++ try: ++ return os_context[ptr_offset], runner.get(final_os_context_ptr + ptr_offset) ++ # ORIGINAL VERSION ++ except: ++ return os_context[ptr_offset], runner.vm_memory[final_os_context_ptr + ptr_offset] ++ ++ def get_os_segment_stop_ptr( -@@ -74,19 +61,14 @@ def validate_segment_pointers( +@@ -61,14 +74,19 @@ def validate_segment_pointers( segment_base_ptr: MaybeRelocatable, segment_stop_ptr: MaybeRelocatable, ): -- # assert isinstance(segment_base_ptr, RelocatableValue) -+ assert isinstance(segment_base_ptr, RelocatableValue) +- assert isinstance(segment_base_ptr, RelocatableValue) ++ # assert isinstance(segment_base_ptr, RelocatableValue) assert ( segment_base_ptr.offset == 0 ), f"Segment base pointer must be zero; got {segment_base_ptr.offset}." -- # CAIRO-RS VERSION -- try: -- expected_stop_ptr = segment_base_ptr + segments.get_segment_used_size( -- index=segment_base_ptr.segment_index) -- # ORIGINAL VERSION -- except: -- expected_stop_ptr = segment_base_ptr + segments.get_segment_used_size( -- segment_index=segment_base_ptr.segment_index) -+ expected_stop_ptr = segment_base_ptr + segments.get_segment_used_size( -+ segment_index=segment_base_ptr.segment_index -+ ) +- expected_stop_ptr = segment_base_ptr + segments.get_segment_used_size( +- segment_index=segment_base_ptr.segment_index +- ) ++ # CAIRO-RS VERSION ++ try: ++ expected_stop_ptr = segment_base_ptr + segments.get_segment_used_size( ++ index=segment_base_ptr.segment_index) ++ # ORIGINAL VERSION ++ except: ++ expected_stop_ptr = segment_base_ptr + segments.get_segment_used_size( ++ segment_index=segment_base_ptr.segment_index) stark_assert( expected_stop_ptr == segment_stop_ptr, diff --git a/src/starkware/starknet/core/os/syscall_utils.py b/src/starkware/starknet/core/os/syscall_utils.py -index c129e50..e44635b 100644 +index e44635b..c129e50 100644 --- a/src/starkware/starknet/core/os/syscall_utils.py +++ b/src/starkware/starknet/core/os/syscall_utils.py -@@ -458,6 +458,7 @@ class SysCallHandlerBase(ABC): +@@ -458,7 +458,6 @@ class SysCallHandlerBase(ABC): ) -> List[int]: """ Returns the call retdata. -+ +- syscall_name can be "call_contract", "delegate_call", "delegate_l1_handler", "library_call" or "library_call_l1_handler". """ -@@ -588,16 +589,7 @@ class BusinessLogicSysCallHandler(SysCallHandlerBase): +@@ -589,7 +588,16 @@ class BusinessLogicSysCallHandler(SysCallHandlerBase): def _allocate_segment( self, segments: MemorySegmentManager, data: Iterable[MaybeRelocatable] ) -> RelocatableValue: -- # FIXME: Here "segments" in really a Runner under the hood. -- # May want to change the variable names. -- -- # CAIRO-RS VERSION -- try: -- segment_start = segments.add_segment() -- # ORIGINAL VERSION -- except: -- segment_start = segments.add() -- -+ segment_start = segments.add() +- segment_start = segments.add() ++ # FIXME: Here "segments" in really a Runner under the hood. ++ # May want to change the variable names. ++ ++ # CAIRO-RS VERSION ++ try: ++ segment_start = segments.add_segment() ++ # ORIGINAL VERSION ++ except: ++ segment_start = segments.add() ++ segment_end = segments.write_arg(ptr=segment_start, arg=data) self.read_only_segments.append((segment_start, segment_end - segment_start)) return segment_start -@@ -639,10 +631,10 @@ class BusinessLogicSysCallHandler(SysCallHandlerBase): +@@ -631,10 +639,10 @@ class BusinessLogicSysCallHandler(SysCallHandlerBase): args_struct_def: StructDefinition = syscall_info.syscall_request_struct.struct_definition_ for arg, (arg_name, arg_def) in safe_zip(request, args_struct_def.members.items()): expected_type = get_runtime_type(arg_def.cairo_type) -- # assert isinstance(arg, expected_type), ( -- # f"Argument {arg_name} to syscall {syscall_name} is of unexpected type. " -- # f"Expected: value of type {expected_type}; got: {arg}." -- # ) -+ assert isinstance(arg, expected_type), ( -+ f"Argument {arg_name} to syscall {syscall_name} is of unexpected type. " -+ f"Expected: value of type {expected_type}; got: {arg}." -+ ) +- assert isinstance(arg, expected_type), ( +- f"Argument {arg_name} to syscall {syscall_name} is of unexpected type. " +- f"Expected: value of type {expected_type}; got: {arg}." +- ) ++ # assert isinstance(arg, expected_type), ( ++ # f"Argument {arg_name} to syscall {syscall_name} is of unexpected type. " ++ # f"Expected: value of type {expected_type}; got: {arg}." ++ # ) return request -@@ -910,20 +902,10 @@ class BusinessLogicSysCallHandler(SysCallHandlerBase): +@@ -902,10 +910,20 @@ class BusinessLogicSysCallHandler(SysCallHandlerBase): Validates that there were no out of bounds writes to read-only segments and marks them as accessed. """ -- # ORIGINAL VERSION -- try: -- segments = runner.segments -- # CAIRO-RS VERSION -- except: -- segments = runner -+ segments = runner.segments +- segments = runner.segments ++ # ORIGINAL VERSION ++ try: ++ segments = runner.segments ++ # CAIRO-RS VERSION ++ except: ++ segments = runner for segment_ptr, segment_size in self.read_only_segments: -- # CAIRO-RS VERSION -- try: -- used_size = segments.get_segment_used_size(index=segment_ptr.segment_index) -- # ORIGINAL VERSION -- except: -- used_size = segments.get_segment_used_size(segment_index=segment_ptr.segment_index) -+ used_size = segments.get_segment_used_size(segment_index=segment_ptr.segment_index) +- used_size = segments.get_segment_used_size(segment_index=segment_ptr.segment_index) ++ # CAIRO-RS VERSION ++ try: ++ used_size = segments.get_segment_used_size(index=segment_ptr.segment_index) ++ # ORIGINAL VERSION ++ except: ++ used_size = segments.get_segment_used_size(segment_index=segment_ptr.segment_index) stark_assert( used_size == segment_size, code=StarknetErrorCode.SECURITY_ERROR, -@@ -1059,6 +1041,7 @@ class OsSysCallHandler(SysCallHandlerBase): +@@ -1041,7 +1059,6 @@ class OsSysCallHandler(SysCallHandlerBase): def start_tx(self, tx_info_ptr: RelocatableValue): """ Called when starting the execution of a transaction. -+ +- 'tx_info_ptr' is a pointer to the TxInfo struct corresponding to said transaction. """ assert self.tx_info_ptr is None -@@ -1106,4 +1089,4 @@ class OsSysCallHandler(SysCallHandlerBase): +@@ -1089,4 +1106,4 @@ class OsSysCallHandler(SysCallHandlerBase): Called when skipping the execution of a transaction. It replaces a call to start_tx and end_tx. """ - next(self.tx_execution_info_iterator) -\ No newline at end of file + next(self.tx_execution_info_iterator) +\ No newline at end of file From c257bcd321672cc2d5f126ed8c8b5666e9fbe25d Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 1 Dec 2022 17:38:30 -0300 Subject: [PATCH 43/57] Include more tests --- scripts/memory_comparator/run_tests_compare_memory.sh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/scripts/memory_comparator/run_tests_compare_memory.sh b/scripts/memory_comparator/run_tests_compare_memory.sh index 16e0bd02..ec922593 100644 --- a/scripts/memory_comparator/run_tests_compare_memory.sh +++ b/scripts/memory_comparator/run_tests_compare_memory.sh @@ -5,8 +5,7 @@ exit_code=0 cd starknet-devnet for file in test/test_*.py; do # Skip files that dont run entrypoints and dont produce memory outputs - # As a temporary measure, some tests that do produce memory outputs are being skipped while the team works on fixing some error format mismatches - if ! ([ "$file" = "test/test_account.py" ] || [ "$file" = "test/test_estimate_fee.py" ] || [ "$file" = "test/test_rpc_estimate_fee.py" ] || [ "$file" = "test/test_fee_token.py" ] || [ "$file" = "test/test_postman.py" ] || [ "$file" = "test/testnet_deployment.py" ] || [ "$file" = "test/testnet_deploy.py" ] || [ "$file" = "test/test_api_specifications.py" ] || [ "$file" = "test/test_fork_cli_params.py" ]); then + if! ( [ "$file" = "test/test_estimate_fee.py" ] || [ "$file" = "test/test_postman.py" ] || [ "$file" = "test/testnet_deployment.py" ] || [ "$file" = "test/testnet_deploy.py" ] || [ "$file" = "test/test_api_specifications.py" ] || [ "$file" = "test/test_fork_cli_params.py" ]); then # Run tests in cairo-rs-py env . ../scripts/memory_comparator/cairo-rs-py/bin/activate poetry run pytest $file From 4b00ac49feed93975d34c103999e293b30fb772d Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 1 Dec 2022 17:48:02 -0300 Subject: [PATCH 44/57] Fix syntax --- scripts/memory_comparator/run_tests_compare_memory.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/memory_comparator/run_tests_compare_memory.sh b/scripts/memory_comparator/run_tests_compare_memory.sh index ec922593..a4c564f7 100644 --- a/scripts/memory_comparator/run_tests_compare_memory.sh +++ b/scripts/memory_comparator/run_tests_compare_memory.sh @@ -5,7 +5,7 @@ exit_code=0 cd starknet-devnet for file in test/test_*.py; do # Skip files that dont run entrypoints and dont produce memory outputs - if! ( [ "$file" = "test/test_estimate_fee.py" ] || [ "$file" = "test/test_postman.py" ] || [ "$file" = "test/testnet_deployment.py" ] || [ "$file" = "test/testnet_deploy.py" ] || [ "$file" = "test/test_api_specifications.py" ] || [ "$file" = "test/test_fork_cli_params.py" ]); then + if! ([ "$file" = "test/test_estimate_fee.py" ] || [ "$file" = "test/test_postman.py" ] || [ "$file" = "test/testnet_deployment.py" ] || [ "$file" = "test/testnet_deploy.py" ] || [ "$file" = "test/test_api_specifications.py" ] || [ "$file" = "test/test_fork_cli_params.py" ]); then # Run tests in cairo-rs-py env . ../scripts/memory_comparator/cairo-rs-py/bin/activate poetry run pytest $file From b17141fa82f7a457bfe5b79bb3ffd1311abd5361 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 1 Dec 2022 17:50:41 -0300 Subject: [PATCH 45/57] Fix syntax --- scripts/memory_comparator/run_tests_compare_memory.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/memory_comparator/run_tests_compare_memory.sh b/scripts/memory_comparator/run_tests_compare_memory.sh index a4c564f7..ddc9f627 100644 --- a/scripts/memory_comparator/run_tests_compare_memory.sh +++ b/scripts/memory_comparator/run_tests_compare_memory.sh @@ -5,7 +5,7 @@ exit_code=0 cd starknet-devnet for file in test/test_*.py; do # Skip files that dont run entrypoints and dont produce memory outputs - if! ([ "$file" = "test/test_estimate_fee.py" ] || [ "$file" = "test/test_postman.py" ] || [ "$file" = "test/testnet_deployment.py" ] || [ "$file" = "test/testnet_deploy.py" ] || [ "$file" = "test/test_api_specifications.py" ] || [ "$file" = "test/test_fork_cli_params.py" ]); then + if ! ([ "$file" = "test/test_estimate_fee.py" ] || [ "$file" = "test/test_postman.py" ] || [ "$file" = "test/testnet_deployment.py" ] || [ "$file" = "test/testnet_deploy.py" ] || [ "$file" = "test/test_api_specifications.py" ] || [ "$file" = "test/test_fork_cli_params.py" ]); then # Run tests in cairo-rs-py env . ../scripts/memory_comparator/cairo-rs-py/bin/activate poetry run pytest $file From 8409d84db0da24365d0d41661d65a47daaadf969 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 1 Dec 2022 18:59:56 -0300 Subject: [PATCH 46/57] Add cairo_runner method + update patch cairo-rs-py --- .../output-memory-cairo-rs-py.patch | 19 +++++++------------ src/cairo_runner.rs | 10 +++++++++- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/scripts/memory_comparator/output-memory-cairo-rs-py.patch b/scripts/memory_comparator/output-memory-cairo-rs-py.patch index 895315e2..8190d61d 100644 --- a/scripts/memory_comparator/output-memory-cairo-rs-py.patch +++ b/scripts/memory_comparator/output-memory-cairo-rs-py.patch @@ -1,5 +1,5 @@ diff --git a/src/starkware/starknet/business_logic/execution/execute_entry_point.py b/src/starkware/starknet/business_logic/execution/execute_entry_point.py -index 62d4290..719208f 100644 +index 62d4290..fe04404 100644 --- a/src/starkware/starknet/business_logic/execution/execute_entry_point.py +++ b/src/starkware/starknet/business_logic/execution/execute_entry_point.py @@ -41,6 +41,7 @@ from starkware.starkware_utils.error_handling import ( @@ -55,7 +55,7 @@ index 62d4290..719208f 100644 verify_secure=True, ) except VmException as exception: -@@ -271,9 +273,10 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): +@@ -271,9 +273,11 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): # The arguments are touched by the OS and should not be counted as holes, mark them # as accessed. @@ -65,16 +65,10 @@ index 62d4290..719208f 100644 - + runner.relocate() + runner.write_binary_memory("memory_files/execute_entry_point.rs.memory") ++ runner.write_binary_trace("trace_files/execute_entry_point.rs.trace") return runner, syscall_handler def _get_selected_entry_point( -@@ -355,4 +358,4 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): - raise NotImplementedError(f"Call type {self.call_type} not implemented.") - - # Extract pre-fetched contract code from carried state. -- return get_deployed_class_hash_at_address(state=state, contract_address=code_address) -+ return get_deployed_class_hash_at_address(state=state, contract_address=code_address) -\ No newline at end of file diff --git a/src/starkware/starknet/business_logic/transaction/fee.py b/src/starkware/starknet/business_logic/transaction/fee.py index 9acaa73..9bbb9f5 100644 --- a/src/starkware/starknet/business_logic/transaction/fee.py @@ -113,7 +107,7 @@ index f63bc9f..15660fe 100644 stark_assert( all(isinstance(value, int) for value in values), diff --git a/src/starkware/starknet/core/os/class_hash.py b/src/starkware/starknet/core/os/class_hash.py -index 132fb21..3cf48a8 100644 +index 132fb21..00b769c 100644 --- a/src/starkware/starknet/core/os/class_hash.py +++ b/src/starkware/starknet/core/os/class_hash.py @@ -5,9 +5,10 @@ import json @@ -139,7 +133,7 @@ index 132fb21..3cf48a8 100644 CAIRO_FILE = os.path.join(os.path.dirname(__file__), "contracts.cairo") -@@ -77,22 +82,24 @@ def compute_class_hash_inner( +@@ -77,22 +82,25 @@ def compute_class_hash_inner( contract_class_struct = get_contract_class_struct( identifiers=program.identifiers, contract_class=contract_class ) @@ -169,10 +163,11 @@ index 132fb21..3cf48a8 100644 _, class_hash = runner.get_return_values(2) + runner.relocate() + runner.write_binary_memory("memory_files/class_hash.rs.memory") ++ runner.write_binary_trace("trace_files/class_hash.rs.trace") return class_hash -@@ -194,3 +201,103 @@ def get_contract_class_struct( +@@ -194,3 +202,103 @@ def get_contract_class_struct( bytecode_length=len(contract_class.program.data), bytecode_ptr=contract_class.program.data, ) diff --git a/src/cairo_runner.rs b/src/cairo_runner.rs index ed40c349..0303423c 100644 --- a/src/cairo_runner.rs +++ b/src/cairo_runner.rs @@ -6,7 +6,7 @@ use crate::{ }; use cairo_rs::{ bigint, - cairo_run::{write_binary_memory, write_output}, + cairo_run::{write_binary_memory, write_binary_trace, write_output}, hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor, serde::deserialize_program::Member, types::{ @@ -225,6 +225,14 @@ impl PyCairoRunner { write_binary_memory(&self.inner.relocated_memory, Path::new(&name)).map_err(to_py_error) } + pub fn write_binary_trace(&mut self, name: String) -> PyResult<()> { + write_binary_trace( + &self.inner.relocated_trace.as_ref().unwrap(), + Path::new(&name), + ) + .map_err(to_py_error) + } + pub fn add_segment(&self) -> PyRelocatable { (*self.pyvm.vm).borrow_mut().add_memory_segment().into() } From 8d7bd3930e3656881645b45deb351ad4323f21f7 Mon Sep 17 00:00:00 2001 From: Federica Date: Fri, 2 Dec 2022 11:31:02 -0300 Subject: [PATCH 47/57] Update cairo-lang patch --- .../output-memory-cairo-lang.patch | 31 ++++++++++++------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/scripts/memory_comparator/output-memory-cairo-lang.patch b/scripts/memory_comparator/output-memory-cairo-lang.patch index 95d26179..8be2ccaa 100644 --- a/scripts/memory_comparator/output-memory-cairo-lang.patch +++ b/scripts/memory_comparator/output-memory-cairo-lang.patch @@ -1,29 +1,32 @@ diff --git a/src/starkware/starknet/business_logic/execution/execute_entry_point.py b/src/starkware/starknet/business_logic/execution/execute_entry_point.py -index 62d4290..f4a0d8b 100644 +index 62d4290..e0395c4 100644 --- a/src/starkware/starknet/business_logic/execution/execute_entry_point.py +++ b/src/starkware/starknet/business_logic/execution/execute_entry_point.py @@ -9,6 +9,8 @@ from starkware.cairo.lang.vm.relocatable import RelocatableValue from starkware.cairo.lang.vm.security import SecurityError from starkware.cairo.lang.vm.utils import ResourcesError from starkware.cairo.lang.vm.vm_exceptions import HintException, VmException, VmExceptionBase -+from starkware.cairo.lang.vm.cairo_run import write_binary_memory ++from starkware.cairo.lang.vm.cairo_run import write_binary_memory, write_binary_trace +import math from starkware.starknet.business_logic.execution.execute_entry_point_base import ( ExecuteEntryPointBase, ) -@@ -274,6 +276,10 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): +@@ -274,6 +276,13 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): assert isinstance(args_ptr, RelocatableValue) # Downcast. runner.mark_as_accessed(address=args_ptr, size=len(entry_points_args)) -+ f = open("memory_files/execute_entry_point.memory", "wb") ++ memory_file = open("memory_files/execute_entry_point.memory", "wb") ++ trace_file = open("trace_files/execute_entry_point.trace", "wb") + field_bytes = math.ceil(contract_class.program.prime.bit_length() / 8) + runner.relocate() -+ write_binary_memory(f, runner.relocated_memory, field_bytes) ++ write_binary_memory(memory_file, runner.relocated_memory, field_bytes) ++ write_binary_trace(trace_file, runner.relocated_trace) ++ return runner, syscall_handler def _get_selected_entry_point( diff --git a/src/starkware/starknet/core/os/class_hash.py b/src/starkware/starknet/core/os/class_hash.py -index 132fb21..321c63a 100644 +index 132fb21..aa20ed8 100644 --- a/src/starkware/starknet/core/os/class_hash.py +++ b/src/starkware/starknet/core/os/class_hash.py @@ -3,9 +3,11 @@ import dataclasses @@ -34,18 +37,22 @@ index 132fb21..321c63a 100644 from contextvars import ContextVar from functools import lru_cache from typing import Callable, List, Optional -+from starkware.cairo.lang.vm.cairo_run import write_binary_memory ++from starkware.cairo.lang.vm.cairo_run import write_binary_memory, write_binary_trace import cachetools -@@ -92,6 +94,10 @@ def compute_class_hash_inner( - use_full_name=True, +@@ -93,6 +95,14 @@ def compute_class_hash_inner( verify_secure=False, ) -+ f = open("memory_files/class_hash.memory", "wb") + _, class_hash = runner.get_return_values(2) ++ ++ memory_file = open("memory_files/class_hash.memory", "wb") ++ trace_file = open("trace_files/class_hash.trace", "wb") + field_bytes = math.ceil(program.prime.bit_length() / 8) + runner.relocate() -+ write_binary_memory(f, runner.relocated_memory, field_bytes) - _, class_hash = runner.get_return_values(2) ++ write_binary_memory(memory_file, runner.relocated_memory, field_bytes) ++ write_binary_trace(trace_file, runner.relocated_trace) ++ return class_hash + From e568d0324855cee3e284a7792cad156d3f71f770 Mon Sep 17 00:00:00 2001 From: Federica Date: Fri, 2 Dec 2022 11:32:38 -0300 Subject: [PATCH 48/57] Create trace folder in makefile --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index b0b67c2c..3fc18e87 100644 --- a/Makefile +++ b/Makefile @@ -121,7 +121,7 @@ compare_memory_devnet: pip install starknet-devnet && \ cd starknet-devnet; scripts/install_dev_tools.sh # Create the folder where we will store the memory outputs - cd starknet-devnet; mkdir memory_files + cd starknet-devnet; mkdir memory_files; mkdir trace_files # Compile test files . scripts/memory_comparator/cairo-lang/bin/activate && \ cd starknet-devnet; scripts/compile_contracts.sh @@ -144,7 +144,7 @@ compare_memory_devnet_ci: pip install starknet-devnet && \ cd starknet-devnet; scripts/install_dev_tools.sh # Create the folder where we will store the memory outputs - cd starknet-devnet; mkdir memory_files + cd starknet-devnet; mkdir memory_files; mkdir trace_files # Compile test files . scripts/memory_comparator/cairo-lang/bin/activate && \ cd starknet-devnet; scripts/compile_contracts.sh From 93e4bf3a14fcb3f56a1734298cd8c3598ee8fd40 Mon Sep 17 00:00:00 2001 From: Federica Date: Fri, 2 Dec 2022 11:38:52 -0300 Subject: [PATCH 49/57] Add trace comparison --- .../run_tests_compare_memory.sh | 32 +++++++++++++++---- 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/scripts/memory_comparator/run_tests_compare_memory.sh b/scripts/memory_comparator/run_tests_compare_memory.sh index ddc9f627..d8b6816d 100644 --- a/scripts/memory_comparator/run_tests_compare_memory.sh +++ b/scripts/memory_comparator/run_tests_compare_memory.sh @@ -4,7 +4,7 @@ exit_code=0 # We need to be inside starknet-devnet in order to run poetry cd starknet-devnet for file in test/test_*.py; do - # Skip files that dont run entrypoints and dont produce memory outputs + # Skip files that dont run entrypoints and dont produce memory and trace outputs if ! ([ "$file" = "test/test_estimate_fee.py" ] || [ "$file" = "test/test_postman.py" ] || [ "$file" = "test/testnet_deployment.py" ] || [ "$file" = "test/testnet_deploy.py" ] || [ "$file" = "test/test_api_specifications.py" ] || [ "$file" = "test/test_fork_cli_params.py" ]); then # Run tests in cairo-rs-py env . ../scripts/memory_comparator/cairo-rs-py/bin/activate @@ -13,29 +13,49 @@ for file in test/test_*.py; do . ../scripts/memory_comparator/cairo-lang/bin/activate poetry run pytest $file # Compare memory outputs - class_hash_path="memory_files/class_hash" - execute_entry_point_path="memory_files/execute_entry_point" + class_hash_memory_path="memory_files/class_hash" + execute_entry_point_memory_path="memory_files/execute_entry_point" + class_hash_trace_path="trace_files/class_hash" + execute_entry_point_trace_path="trace_files/execute_entry_point" memory_comparator_path="../scripts/memory_comparator/memory_comparator.py" - # Some tests do not use class_hash and dont generate memory files there + # Some tests do not use class_hash and dont generate memory and trace files there if ! ([ "$file" = "test/test_dump.py" ]); then - if ! $memory_comparator_path $class_hash_path.memory $class_hash_path.rs.memory; then + # Compare memory outputs for class_hash + if ! $memory_comparator_path $class_hash_memory_path.memory $class_hash_memory_path.rs.memory; then echo "Memory differs for last class_hash on test $file" exit_code=1 else echo "Memory comparison successful" fi + # Compare trace outputs for class_hash + if ! diff -q $class_hash_trace_path.trace $class_hash_trace_path.rs.trace; then + echo "Traces differ for last class_hash on test $file" + exit_code=1 + else + echo "Trace comparison successful" + fi fi # Some tests do not use execute_entry_point and dont generate memory files there if ! ([ "$file" = "test/test_account_predeployed.py" ]); then - if ! $memory_comparator_path $execute_entry_point_path.memory $execute_entry_point_path.rs.memory; then + # Compare memory outputs for execute_entry_point + if ! $memory_comparator_path $execute_entry_point_memory_path.memory $execute_entry_point_memory_path.rs.memory; then echo "Memory differs for last execute_entry_point on test $file" exit_code=1 else echo "Memory comparison successful" fi + # Compare trace outputs for execute_entry_point + if ! diff -q $execute_entry_point_trace_path.trace $execute_entry_point_trace_path.rs.trace; then + echo "Traces differ for last execute_entry_point on test $file" + exit_code=1 + else + echo "Trace comparison successful" + fi fi # Cleanup memory files rm memory_files/*.memory + # Cleanup trace files + rm trace_files/*.trace fi done exit "${exit_code}" From 796e52aec39ddec214016fb9a086dd3cf6dae79a Mon Sep 17 00:00:00 2001 From: Federica Date: Fri, 2 Dec 2022 11:49:08 -0300 Subject: [PATCH 50/57] Remove unwrap --- src/cairo_runner.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/cairo_runner.rs b/src/cairo_runner.rs index 0303423c..6d4bcf85 100644 --- a/src/cairo_runner.rs +++ b/src/cairo_runner.rs @@ -227,7 +227,9 @@ impl PyCairoRunner { pub fn write_binary_trace(&mut self, name: String) -> PyResult<()> { write_binary_trace( - &self.inner.relocated_trace.as_ref().unwrap(), + self.inner.relocated_trace.as_ref().ok_or_else(|| { + PyTypeError::new_err("Cant write binary trace if trace is not enabled") + })?, Path::new(&name), ) .map_err(to_py_error) From d8d5a16c18b38f8b9af33a92644a9d72f8e9aa4a Mon Sep 17 00:00:00 2001 From: Federica Date: Fri, 2 Dec 2022 16:05:17 -0300 Subject: [PATCH 51/57] Compare all files instead of last file set --- .../output-memory-cairo-lang.patch | 55 +++++++++++--- .../output-memory-cairo-rs-py.patch | 73 ++++++++++++++----- .../run_tests_compare_memory.sh | 47 ++++-------- 3 files changed, 113 insertions(+), 62 deletions(-) diff --git a/scripts/memory_comparator/output-memory-cairo-lang.patch b/scripts/memory_comparator/output-memory-cairo-lang.patch index 8be2ccaa..8ec6eda3 100644 --- a/scripts/memory_comparator/output-memory-cairo-lang.patch +++ b/scripts/memory_comparator/output-memory-cairo-lang.patch @@ -1,5 +1,5 @@ diff --git a/src/starkware/starknet/business_logic/execution/execute_entry_point.py b/src/starkware/starknet/business_logic/execution/execute_entry_point.py -index 62d4290..e0395c4 100644 +index 62d4290..9929444 100644 --- a/src/starkware/starknet/business_logic/execution/execute_entry_point.py +++ b/src/starkware/starknet/business_logic/execution/execute_entry_point.py @@ -9,6 +9,8 @@ from starkware.cairo.lang.vm.relocatable import RelocatableValue @@ -11,22 +11,39 @@ index 62d4290..e0395c4 100644 from starkware.starknet.business_logic.execution.execute_entry_point_base import ( ExecuteEntryPointBase, ) -@@ -274,6 +276,13 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): +@@ -44,6 +46,8 @@ from starkware.starkware_utils.error_handling import ( + + logger = logging.getLogger(__name__) + ++call_number = 0 ++ + + class ExecuteEntryPoint(ExecuteEntryPointBase): + """ +@@ -171,6 +175,7 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): + Returns the corresponding CairoFunctionRunner and BusinessLogicSysCallHandler in order to + retrieve the execution information. + """ ++ global call_number + # Prepare input for Cairo function runner. + class_hash = self._get_code_class_hash(state=state) + contract_class = state.get_contract_class(class_hash=class_hash) +@@ -274,6 +279,13 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): assert isinstance(args_ptr, RelocatableValue) # Downcast. runner.mark_as_accessed(address=args_ptr, size=len(entry_points_args)) -+ memory_file = open("memory_files/execute_entry_point.memory", "wb") -+ trace_file = open("trace_files/execute_entry_point.trace", "wb") ++ memory_file = open("memory_files/execute_entry_point_{}.memory".format(call_number), "wb") ++ trace_file = open("trace_files/execute_entry_point_{}.trace".format(call_number), "wb") + field_bytes = math.ceil(contract_class.program.prime.bit_length() / 8) + runner.relocate() + write_binary_memory(memory_file, runner.relocated_memory, field_bytes) + write_binary_trace(trace_file, runner.relocated_trace) -+ ++ call_number += 1 return runner, syscall_handler def _get_selected_entry_point( diff --git a/src/starkware/starknet/core/os/class_hash.py b/src/starkware/starknet/core/os/class_hash.py -index 132fb21..aa20ed8 100644 +index 132fb21..46bd820 100644 --- a/src/starkware/starknet/core/os/class_hash.py +++ b/src/starkware/starknet/core/os/class_hash.py @@ -3,9 +3,11 @@ import dataclasses @@ -41,18 +58,36 @@ index 132fb21..aa20ed8 100644 import cachetools -@@ -93,6 +95,14 @@ def compute_class_hash_inner( +@@ -26,6 +28,8 @@ from starkware.starknet.services.api.contract_class import ContractClass, EntryP + + CAIRO_FILE = os.path.join(os.path.dirname(__file__), "contracts.cairo") + ++call_number = 0 ++ + class_hash_cache_ctx_var: ContextVar[Optional[cachetools.LRUCache]] = ContextVar( + "class_hash_cache", default=None + ) +@@ -73,6 +77,7 @@ def compute_class_hash( + def compute_class_hash_inner( + contract_class: ContractClass, hash_func: Callable[[int, int], int] + ) -> int: ++ global call_number + program = load_program() + contract_class_struct = get_contract_class_struct( + identifiers=program.identifiers, contract_class=contract_class +@@ -93,6 +98,15 @@ def compute_class_hash_inner( verify_secure=False, ) _, class_hash = runner.get_return_values(2) + -+ memory_file = open("memory_files/class_hash.memory", "wb") -+ trace_file = open("trace_files/class_hash.trace", "wb") ++ memory_file = open("memory_files/class_hash_{}.memory".format(call_number), "wb") ++ trace_file = open("trace_files/class_hash_{}.trace".format(call_number), "wb") + field_bytes = math.ceil(program.prime.bit_length() / 8) + runner.relocate() + write_binary_memory(memory_file, runner.relocated_memory, field_bytes) + write_binary_trace(trace_file, runner.relocated_trace) -+ ++ call_number += 1 ++ return class_hash diff --git a/scripts/memory_comparator/output-memory-cairo-rs-py.patch b/scripts/memory_comparator/output-memory-cairo-rs-py.patch index 8190d61d..421be37a 100644 --- a/scripts/memory_comparator/output-memory-cairo-rs-py.patch +++ b/scripts/memory_comparator/output-memory-cairo-rs-py.patch @@ -1,8 +1,8 @@ diff --git a/src/starkware/starknet/business_logic/execution/execute_entry_point.py b/src/starkware/starknet/business_logic/execution/execute_entry_point.py -index 62d4290..fe04404 100644 +index 62d4290..1978d9e 100644 --- a/src/starkware/starknet/business_logic/execution/execute_entry_point.py +++ b/src/starkware/starknet/business_logic/execution/execute_entry_point.py -@@ -41,6 +41,7 @@ from starkware.starkware_utils.error_handling import ( +@@ -41,9 +41,12 @@ from starkware.starkware_utils.error_handling import ( stark_assert, wrap_with_stark_exception, ) @@ -10,7 +10,28 @@ index 62d4290..fe04404 100644 logger = logging.getLogger(__name__) -@@ -182,7 +183,8 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): ++call_number = 0 ++ + + class ExecuteEntryPoint(ExecuteEntryPointBase): + """ +@@ -137,6 +140,7 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): + contract storage, etc.) is saved on the resources manager. + Returns a CallInfo object that represents the execution. + """ ++ global call_number + previous_cairo_usage = resources_manager.cairo_usage + + runner, syscall_handler = self._run( +@@ -171,6 +175,7 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): + Returns the corresponding CairoFunctionRunner and BusinessLogicSysCallHandler in order to + retrieve the execution information. + """ ++ global call_number + # Prepare input for Cairo function runner. + class_hash = self._get_code_class_hash(state=state) + contract_class = state.get_contract_class(class_hash=class_hash) +@@ -182,7 +187,8 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): # Run the specified contract entry point with given calldata. with wrap_with_stark_exception(code=StarknetErrorCode.SECURITY_ERROR): @@ -20,7 +41,7 @@ index 62d4290..fe04404 100644 os_context = os_utils.prepare_os_context(runner=runner) validate_contract_deployed(state=state, contract_address=self.contract_address) -@@ -205,24 +207,24 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): +@@ -205,24 +211,24 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): os_context, len(self.calldata), # Allocate and mark the segment as read-only (to mark every input array as read-only). @@ -55,7 +76,7 @@ index 62d4290..fe04404 100644 verify_secure=True, ) except VmException as exception: -@@ -271,9 +273,11 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): +@@ -271,9 +277,12 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): # The arguments are touched by the OS and should not be counted as holes, mark them # as accessed. @@ -64,8 +85,9 @@ index 62d4290..fe04404 100644 runner.mark_as_accessed(address=args_ptr, size=len(entry_points_args)) - + runner.relocate() -+ runner.write_binary_memory("memory_files/execute_entry_point.rs.memory") -+ runner.write_binary_trace("trace_files/execute_entry_point.rs.trace") ++ runner.write_binary_memory("memory_files/execute_entry_point_{}.rs.memory".format(call_number)) ++ runner.write_binary_trace("trace_files/execute_entry_point_{}.rs.trace".format(call_number)) ++ call_number += 1 return runner, syscall_handler def _get_selected_entry_point( @@ -107,7 +129,7 @@ index f63bc9f..15660fe 100644 stark_assert( all(isinstance(value, int) for value in values), diff --git a/src/starkware/starknet/core/os/class_hash.py b/src/starkware/starknet/core/os/class_hash.py -index 132fb21..00b769c 100644 +index 132fb21..9757bae 100644 --- a/src/starkware/starknet/core/os/class_hash.py +++ b/src/starkware/starknet/core/os/class_hash.py @@ -5,9 +5,10 @@ import json @@ -122,7 +144,7 @@ index 132fb21..00b769c 100644 from starkware.cairo.common.cairo_function_runner import CairoFunctionRunner from starkware.cairo.common.structs import CairoStructFactory, CairoStructProxy -@@ -23,6 +24,10 @@ from starkware.cairo.lang.vm.crypto import pedersen_hash +@@ -23,9 +24,15 @@ from starkware.cairo.lang.vm.crypto import pedersen_hash from starkware.python.utils import from_bytes from starkware.starknet.public.abi import starknet_keccak from starkware.starknet.services.api.contract_class import ContractClass, EntryPointType @@ -133,7 +155,17 @@ index 132fb21..00b769c 100644 CAIRO_FILE = os.path.join(os.path.dirname(__file__), "contracts.cairo") -@@ -77,22 +82,25 @@ def compute_class_hash_inner( ++call_number = 0 ++ + class_hash_cache_ctx_var: ContextVar[Optional[cachetools.LRUCache]] = ContextVar( + "class_hash_cache", default=None + ) +@@ -73,26 +80,31 @@ def compute_class_hash( + def compute_class_hash_inner( + contract_class: ContractClass, hash_func: Callable[[int, int], int] + ) -> int: ++ global call_number + program = load_program() contract_class_struct = get_contract_class_struct( identifiers=program.identifiers, contract_class=contract_class ) @@ -147,9 +179,9 @@ index 132fb21..00b769c 100644 + runner = cairo_rs_py.CairoRunner(program=program.dumps(), entrypoint=None, layout="all", proof_mode=False) + runner.initialize_function_runner() + hash_ptr = runner.add_additional_hash_builtin() -+ - runner.run( ++ + run_function_runner( + runner, + program, @@ -162,12 +194,13 @@ index 132fb21..00b769c 100644 ) _, class_hash = runner.get_return_values(2) + runner.relocate() -+ runner.write_binary_memory("memory_files/class_hash.rs.memory") -+ runner.write_binary_trace("trace_files/class_hash.rs.trace") ++ runner.write_binary_memory("memory_files/class_hash_{}.rs.memory".format(call_number)) ++ runner.write_binary_trace("trace_files/class_hash_{}.rs.trace".format(call_number)) ++ call_number += 1 return class_hash -@@ -194,3 +202,103 @@ def get_contract_class_struct( +@@ -194,3 +206,103 @@ def get_contract_class_struct( bytecode_length=len(contract_class.program.data), bytecode_ptr=contract_class.program.data, ) @@ -288,7 +321,7 @@ index 20bd521..0ea99f4 100644 - builtin_runner = runner.builtin_runners[f"{builtin}_builtin"] - os_context.extend(builtin_runner.initial_stack()) + # CAIRO-RS VERSION -+ try: ++ try: + syscall_segment = runner.add_segment() + os_context: List[MaybeRelocatable] = [syscall_segment] + os_context.extend(runner.get_program_builtins_initial_stack()) @@ -348,7 +381,7 @@ index 20bd521..0ea99f4 100644 - segment_stop_ptr=syscall_stop_ptr, - ) + # ORIGINAL VERSION -+ try: ++ try: + segment_utils.validate_segment_pointers( + segments=runner, + segment_base_ptr=syscall_base_ptr, @@ -416,7 +449,7 @@ index 1d09414..33f5c26 100644 - segment_index=segment_base_ptr.segment_index - ) + # CAIRO-RS VERSION -+ try: ++ try: + expected_stop_ptr = segment_base_ptr + segments.get_segment_used_size( + index=segment_base_ptr.segment_index) + # ORIGINAL VERSION @@ -447,7 +480,7 @@ index e44635b..c129e50 100644 + # May want to change the variable names. + + # CAIRO-RS VERSION -+ try: ++ try: + segment_start = segments.add_segment() + # ORIGINAL VERSION + except: @@ -477,7 +510,7 @@ index e44635b..c129e50 100644 """ - segments = runner.segments + # ORIGINAL VERSION -+ try: ++ try: + segments = runner.segments + # CAIRO-RS VERSION + except: @@ -489,7 +522,7 @@ index e44635b..c129e50 100644 + try: + used_size = segments.get_segment_used_size(index=segment_ptr.segment_index) + # ORIGINAL VERSION -+ except: ++ except: + used_size = segments.get_segment_used_size(segment_index=segment_ptr.segment_index) stark_assert( used_size == segment_size, diff --git a/scripts/memory_comparator/run_tests_compare_memory.sh b/scripts/memory_comparator/run_tests_compare_memory.sh index d8b6816d..6e883e22 100644 --- a/scripts/memory_comparator/run_tests_compare_memory.sh +++ b/scripts/memory_comparator/run_tests_compare_memory.sh @@ -13,45 +13,28 @@ for file in test/test_*.py; do . ../scripts/memory_comparator/cairo-lang/bin/activate poetry run pytest $file # Compare memory outputs - class_hash_memory_path="memory_files/class_hash" - execute_entry_point_memory_path="memory_files/execute_entry_point" - class_hash_trace_path="trace_files/class_hash" - execute_entry_point_trace_path="trace_files/execute_entry_point" + memory_dir="memory_files" memory_comparator_path="../scripts/memory_comparator/memory_comparator.py" - # Some tests do not use class_hash and dont generate memory and trace files there - if ! ([ "$file" = "test/test_dump.py" ]); then - # Compare memory outputs for class_hash - if ! $memory_comparator_path $class_hash_memory_path.memory $class_hash_memory_path.rs.memory; then - echo "Memory differs for last class_hash on test $file" + for mem_file in $(ls $memory_dir | grep .rs.memory$ | sed -E 's/\.rs.memory$//'); do + if ! $memory_comparator_path $memory_dir/$mem_file.memory $memory_dir/$mem_file.rs.memory; then + echo "Memory differs for $mem_file on test $file" exit_code=1 else - echo "Memory comparison successful" + echo "Memory comparison successful for $mem_file on test $file" fi - # Compare trace outputs for class_hash - if ! diff -q $class_hash_trace_path.trace $class_hash_trace_path.rs.trace; then - echo "Traces differ for last class_hash on test $file" + done + # Compare trace outputs + trace_dir="trace_files" + for trace_file in $(ls $trace_dir | grep .rs.trace$ | sed -E 's/\.rs.trace$//'); do + echo $trace_file + ls trace_files + if ! diff -q $trace_dir/$trace_file.trace $trace_dir/$trace_file.rs.trace; then + echo "Traces differs for $trace_file on test $file" exit_code=1 else - echo "Trace comparison successful" + echo "Trace comparison successful $trace_file on test $file" fi - fi - # Some tests do not use execute_entry_point and dont generate memory files there - if ! ([ "$file" = "test/test_account_predeployed.py" ]); then - # Compare memory outputs for execute_entry_point - if ! $memory_comparator_path $execute_entry_point_memory_path.memory $execute_entry_point_memory_path.rs.memory; then - echo "Memory differs for last execute_entry_point on test $file" - exit_code=1 - else - echo "Memory comparison successful" - fi - # Compare trace outputs for execute_entry_point - if ! diff -q $execute_entry_point_trace_path.trace $execute_entry_point_trace_path.rs.trace; then - echo "Traces differ for last execute_entry_point on test $file" - exit_code=1 - else - echo "Trace comparison successful" - fi - fi + done # Cleanup memory files rm memory_files/*.memory # Cleanup trace files From 4948025b1d50d3e966b214620bcfb48caeb0bec5 Mon Sep 17 00:00:00 2001 From: Federica Date: Fri, 2 Dec 2022 16:08:55 -0300 Subject: [PATCH 52/57] Remove debug prints --- scripts/memory_comparator/run_tests_compare_memory.sh | 2 -- 1 file changed, 2 deletions(-) diff --git a/scripts/memory_comparator/run_tests_compare_memory.sh b/scripts/memory_comparator/run_tests_compare_memory.sh index 6e883e22..db19a9d8 100644 --- a/scripts/memory_comparator/run_tests_compare_memory.sh +++ b/scripts/memory_comparator/run_tests_compare_memory.sh @@ -26,8 +26,6 @@ for file in test/test_*.py; do # Compare trace outputs trace_dir="trace_files" for trace_file in $(ls $trace_dir | grep .rs.trace$ | sed -E 's/\.rs.trace$//'); do - echo $trace_file - ls trace_files if ! diff -q $trace_dir/$trace_file.trace $trace_dir/$trace_file.rs.trace; then echo "Traces differs for $trace_file on test $file" exit_code=1 From 8436b78dfe94c46d9e509a43ab6cec237c9e9b4d Mon Sep 17 00:00:00 2001 From: Federica Date: Mon, 5 Dec 2022 16:12:12 -0300 Subject: [PATCH 53/57] Update patches --- .../output-memory-cairo-lang.patch | 16 +++++---- .../output-memory-cairo-rs-py.patch | 34 +++++++++---------- 2 files changed, 26 insertions(+), 24 deletions(-) diff --git a/scripts/memory_comparator/output-memory-cairo-lang.patch b/scripts/memory_comparator/output-memory-cairo-lang.patch index 8ec6eda3..579f5f28 100644 --- a/scripts/memory_comparator/output-memory-cairo-lang.patch +++ b/scripts/memory_comparator/output-memory-cairo-lang.patch @@ -1,5 +1,5 @@ diff --git a/src/starkware/starknet/business_logic/execution/execute_entry_point.py b/src/starkware/starknet/business_logic/execution/execute_entry_point.py -index 62d4290..9929444 100644 +index 09dfd89..3ae7c9a 100644 --- a/src/starkware/starknet/business_logic/execution/execute_entry_point.py +++ b/src/starkware/starknet/business_logic/execution/execute_entry_point.py @@ -9,6 +9,8 @@ from starkware.cairo.lang.vm.relocatable import RelocatableValue @@ -8,27 +8,29 @@ index 62d4290..9929444 100644 from starkware.cairo.lang.vm.vm_exceptions import HintException, VmException, VmExceptionBase +from starkware.cairo.lang.vm.cairo_run import write_binary_memory, write_binary_trace +import math + from starkware.python.utils import to_bytes from starkware.starknet.business_logic.execution.execute_entry_point_base import ( ExecuteEntryPointBase, - ) -@@ -44,6 +46,8 @@ from starkware.starkware_utils.error_handling import ( +@@ -45,8 +47,9 @@ from starkware.starkware_utils.error_handling import ( logger = logging.getLogger(__name__) +-FAULTY_CLASS_HASH = to_bytes(0x1A7820094FEAF82D53F53F214B81292D717E7BB9A92BB2488092CD306F3993F) +call_number = 0 -+ + ++FAULTY_CLASS_HASH = to_bytes(0x1A7820094FEAF82D53F53F214B81292D717E7BB9A92BB2488092CD306F3993F) class ExecuteEntryPoint(ExecuteEntryPointBase): """ -@@ -171,6 +175,7 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): +@@ -174,6 +177,7 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): Returns the corresponding CairoFunctionRunner and BusinessLogicSysCallHandler in order to retrieve the execution information. """ + global call_number # Prepare input for Cairo function runner. class_hash = self._get_code_class_hash(state=state) - contract_class = state.get_contract_class(class_hash=class_hash) -@@ -274,6 +279,13 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): + +@@ -284,6 +288,13 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): assert isinstance(args_ptr, RelocatableValue) # Downcast. runner.mark_as_accessed(address=args_ptr, size=len(entry_points_args)) diff --git a/scripts/memory_comparator/output-memory-cairo-rs-py.patch b/scripts/memory_comparator/output-memory-cairo-rs-py.patch index 421be37a..529d99d7 100644 --- a/scripts/memory_comparator/output-memory-cairo-rs-py.patch +++ b/scripts/memory_comparator/output-memory-cairo-rs-py.patch @@ -1,8 +1,8 @@ diff --git a/src/starkware/starknet/business_logic/execution/execute_entry_point.py b/src/starkware/starknet/business_logic/execution/execute_entry_point.py -index 62d4290..1978d9e 100644 +index 09dfd89..c5e75cb 100644 --- a/src/starkware/starknet/business_logic/execution/execute_entry_point.py +++ b/src/starkware/starknet/business_logic/execution/execute_entry_point.py -@@ -41,9 +41,12 @@ from starkware.starkware_utils.error_handling import ( +@@ -42,9 +42,12 @@ from starkware.starkware_utils.error_handling import ( stark_assert, wrap_with_stark_exception, ) @@ -12,10 +12,10 @@ index 62d4290..1978d9e 100644 +call_number = 0 + + FAULTY_CLASS_HASH = to_bytes(0x1A7820094FEAF82D53F53F214B81292D717E7BB9A92BB2488092CD306F3993F) - class ExecuteEntryPoint(ExecuteEntryPointBase): - """ -@@ -137,6 +140,7 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): + +@@ -140,6 +143,7 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): contract storage, etc.) is saved on the resources manager. Returns a CallInfo object that represents the execution. """ @@ -23,15 +23,15 @@ index 62d4290..1978d9e 100644 previous_cairo_usage = resources_manager.cairo_usage runner, syscall_handler = self._run( -@@ -171,6 +175,7 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): +@@ -174,6 +178,7 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): Returns the corresponding CairoFunctionRunner and BusinessLogicSysCallHandler in order to retrieve the execution information. """ + global call_number # Prepare input for Cairo function runner. class_hash = self._get_code_class_hash(state=state) - contract_class = state.get_contract_class(class_hash=class_hash) -@@ -182,7 +187,8 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): + +@@ -192,7 +197,8 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): # Run the specified contract entry point with given calldata. with wrap_with_stark_exception(code=StarknetErrorCode.SECURITY_ERROR): @@ -41,7 +41,7 @@ index 62d4290..1978d9e 100644 os_context = os_utils.prepare_os_context(runner=runner) validate_contract_deployed(state=state, contract_address=self.contract_address) -@@ -205,24 +211,24 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): +@@ -215,24 +221,24 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): os_context, len(self.calldata), # Allocate and mark the segment as read-only (to mark every input array as read-only). @@ -76,7 +76,7 @@ index 62d4290..1978d9e 100644 verify_secure=True, ) except VmException as exception: -@@ -271,9 +277,12 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): +@@ -281,9 +287,12 @@ class ExecuteEntryPoint(ExecuteEntryPointBase): # The arguments are touched by the OS and should not be counted as holes, mark them # as accessed. @@ -460,10 +460,10 @@ index 1d09414..33f5c26 100644 stark_assert( expected_stop_ptr == segment_stop_ptr, diff --git a/src/starkware/starknet/core/os/syscall_utils.py b/src/starkware/starknet/core/os/syscall_utils.py -index e44635b..c129e50 100644 +index 48645a0..c77466f 100644 --- a/src/starkware/starknet/core/os/syscall_utils.py +++ b/src/starkware/starknet/core/os/syscall_utils.py -@@ -458,7 +458,6 @@ class SysCallHandlerBase(ABC): +@@ -461,7 +461,6 @@ class SysCallHandlerBase(ABC): ) -> List[int]: """ Returns the call retdata. @@ -471,7 +471,7 @@ index e44635b..c129e50 100644 syscall_name can be "call_contract", "delegate_call", "delegate_l1_handler", "library_call" or "library_call_l1_handler". """ -@@ -589,7 +588,16 @@ class BusinessLogicSysCallHandler(SysCallHandlerBase): +@@ -592,7 +591,16 @@ class BusinessLogicSysCallHandler(SysCallHandlerBase): def _allocate_segment( self, segments: MemorySegmentManager, data: Iterable[MaybeRelocatable] ) -> RelocatableValue: @@ -489,7 +489,7 @@ index e44635b..c129e50 100644 segment_end = segments.write_arg(ptr=segment_start, arg=data) self.read_only_segments.append((segment_start, segment_end - segment_start)) return segment_start -@@ -631,10 +639,10 @@ class BusinessLogicSysCallHandler(SysCallHandlerBase): +@@ -634,10 +642,10 @@ class BusinessLogicSysCallHandler(SysCallHandlerBase): args_struct_def: StructDefinition = syscall_info.syscall_request_struct.struct_definition_ for arg, (arg_name, arg_def) in safe_zip(request, args_struct_def.members.items()): expected_type = get_runtime_type(arg_def.cairo_type) @@ -504,7 +504,7 @@ index e44635b..c129e50 100644 return request -@@ -902,10 +910,20 @@ class BusinessLogicSysCallHandler(SysCallHandlerBase): +@@ -905,10 +913,20 @@ class BusinessLogicSysCallHandler(SysCallHandlerBase): Validates that there were no out of bounds writes to read-only segments and marks them as accessed. """ @@ -527,7 +527,7 @@ index e44635b..c129e50 100644 stark_assert( used_size == segment_size, code=StarknetErrorCode.SECURITY_ERROR, -@@ -1041,7 +1059,6 @@ class OsSysCallHandler(SysCallHandlerBase): +@@ -1044,7 +1062,6 @@ class OsSysCallHandler(SysCallHandlerBase): def start_tx(self, tx_info_ptr: RelocatableValue): """ Called when starting the execution of a transaction. @@ -535,7 +535,7 @@ index e44635b..c129e50 100644 'tx_info_ptr' is a pointer to the TxInfo struct corresponding to said transaction. """ assert self.tx_info_ptr is None -@@ -1089,4 +1106,4 @@ class OsSysCallHandler(SysCallHandlerBase): +@@ -1092,4 +1109,4 @@ class OsSysCallHandler(SysCallHandlerBase): Called when skipping the execution of a transaction. It replaces a call to start_tx and end_tx. """ From b9ef2771b94bd876590a743c3c537bcf83660974 Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 6 Dec 2022 15:21:27 -0300 Subject: [PATCH 54/57] Set salt to constant value to avoid divergencies in contract addresses --- .../output-memory-cairo-lang.patch | 13 +++++++++++++ .../output-memory-cairo-rs-py.patch | 13 +++++++++++++ 2 files changed, 26 insertions(+) diff --git a/scripts/memory_comparator/output-memory-cairo-lang.patch b/scripts/memory_comparator/output-memory-cairo-lang.patch index 579f5f28..48edb776 100644 --- a/scripts/memory_comparator/output-memory-cairo-lang.patch +++ b/scripts/memory_comparator/output-memory-cairo-lang.patch @@ -44,6 +44,19 @@ index 09dfd89..3ae7c9a 100644 return runner, syscall_handler def _get_selected_entry_point( +diff --git a/src/starkware/starknet/cli/starknet_cli.py b/src/starkware/starknet/cli/starknet_cli.py +index ddca7c6..adccde5 100755 +--- a/src/starkware/starknet/cli/starknet_cli.py ++++ b/src/starkware/starknet/cli/starknet_cli.py +@@ -844,7 +844,7 @@ def get_salt(salt: Optional[str]) -> int: + If salt is None, returns a random salt. + """ + if salt is None: +- return fields.ContractAddressSalt.get_random_value() ++ return 1 + + return parse_hex_arg(arg=salt, arg_name="salt") + diff --git a/src/starkware/starknet/core/os/class_hash.py b/src/starkware/starknet/core/os/class_hash.py index 132fb21..46bd820 100644 --- a/src/starkware/starknet/core/os/class_hash.py diff --git a/scripts/memory_comparator/output-memory-cairo-rs-py.patch b/scripts/memory_comparator/output-memory-cairo-rs-py.patch index 529d99d7..a376efec 100644 --- a/scripts/memory_comparator/output-memory-cairo-rs-py.patch +++ b/scripts/memory_comparator/output-memory-cairo-rs-py.patch @@ -128,6 +128,19 @@ index f63bc9f..15660fe 100644 stark_assert( all(isinstance(value, int) for value in values), +diff --git a/src/starkware/starknet/cli/starknet_cli.py b/src/starkware/starknet/cli/starknet_cli.py +index ddca7c6..adccde5 100755 +--- a/src/starkware/starknet/cli/starknet_cli.py ++++ b/src/starkware/starknet/cli/starknet_cli.py +@@ -844,7 +844,7 @@ def get_salt(salt: Optional[str]) -> int: + If salt is None, returns a random salt. + """ + if salt is None: +- return fields.ContractAddressSalt.get_random_value() ++ return 1 + + return parse_hex_arg(arg=salt, arg_name="salt") + diff --git a/src/starkware/starknet/core/os/class_hash.py b/src/starkware/starknet/core/os/class_hash.py index 132fb21..9757bae 100644 --- a/src/starkware/starknet/core/os/class_hash.py From d089ae61f6268f0355074d9cad661839095daca8 Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 6 Dec 2022 15:49:28 -0300 Subject: [PATCH 55/57] Change constant salt to stable salt --- .../output-memory-cairo-lang.patch | 22 ++++++++++++++++--- .../output-memory-cairo-rs-py.patch | 22 ++++++++++++++++--- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/scripts/memory_comparator/output-memory-cairo-lang.patch b/scripts/memory_comparator/output-memory-cairo-lang.patch index 48edb776..a5cfec0b 100644 --- a/scripts/memory_comparator/output-memory-cairo-lang.patch +++ b/scripts/memory_comparator/output-memory-cairo-lang.patch @@ -45,15 +45,31 @@ index 09dfd89..3ae7c9a 100644 def _get_selected_entry_point( diff --git a/src/starkware/starknet/cli/starknet_cli.py b/src/starkware/starknet/cli/starknet_cli.py -index ddca7c6..adccde5 100755 +index ddca7c6..fbd4e89 100755 --- a/src/starkware/starknet/cli/starknet_cli.py +++ b/src/starkware/starknet/cli/starknet_cli.py -@@ -844,7 +844,7 @@ def get_salt(salt: Optional[str]) -> int: +@@ -60,6 +60,8 @@ from starkware.starknet.wallets.account import DEFAULT_ACCOUNT_DIR, Account + from starkware.starknet.wallets.starknet_context import StarknetContext + from starkware.starkware_utils.error_handling import StarkErrorCode + ++custom_salt = 0 ++ + NETWORKS = { + "alpha-goerli": "alpha4.starknet.io", + "alpha-goerli2": "alpha4-2.starknet.io", +@@ -839,12 +841,14 @@ def parse_hex_arg(arg: str, arg_name: str) -> int: + + + def get_salt(salt: Optional[str]) -> int: ++ global custom_salt ++ custom_salt +=1 + """ + Validates the given salt and returns it as an integer. If salt is None, returns a random salt. """ if salt is None: - return fields.ContractAddressSalt.get_random_value() -+ return 1 ++ return custom_salt return parse_hex_arg(arg=salt, arg_name="salt") diff --git a/scripts/memory_comparator/output-memory-cairo-rs-py.patch b/scripts/memory_comparator/output-memory-cairo-rs-py.patch index a376efec..ae769142 100644 --- a/scripts/memory_comparator/output-memory-cairo-rs-py.patch +++ b/scripts/memory_comparator/output-memory-cairo-rs-py.patch @@ -129,15 +129,31 @@ index f63bc9f..15660fe 100644 stark_assert( all(isinstance(value, int) for value in values), diff --git a/src/starkware/starknet/cli/starknet_cli.py b/src/starkware/starknet/cli/starknet_cli.py -index ddca7c6..adccde5 100755 +index ddca7c6..fbd4e89 100755 --- a/src/starkware/starknet/cli/starknet_cli.py +++ b/src/starkware/starknet/cli/starknet_cli.py -@@ -844,7 +844,7 @@ def get_salt(salt: Optional[str]) -> int: +@@ -60,6 +60,8 @@ from starkware.starknet.wallets.account import DEFAULT_ACCOUNT_DIR, Account + from starkware.starknet.wallets.starknet_context import StarknetContext + from starkware.starkware_utils.error_handling import StarkErrorCode + ++custom_salt = 0 ++ + NETWORKS = { + "alpha-goerli": "alpha4.starknet.io", + "alpha-goerli2": "alpha4-2.starknet.io", +@@ -839,12 +841,14 @@ def parse_hex_arg(arg: str, arg_name: str) -> int: + + + def get_salt(salt: Optional[str]) -> int: ++ global custom_salt ++ custom_salt +=1 + """ + Validates the given salt and returns it as an integer. If salt is None, returns a random salt. """ if salt is None: - return fields.ContractAddressSalt.get_random_value() -+ return 1 ++ return custom_salt return parse_hex_arg(arg=salt, arg_name="salt") From bf002df4475241203bc1eec5d7de35b4ba6c41ad Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 6 Dec 2022 19:05:01 -0300 Subject: [PATCH 56/57] Skip time-dependant test --- scripts/memory_comparator/run_tests_compare_memory.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/memory_comparator/run_tests_compare_memory.sh b/scripts/memory_comparator/run_tests_compare_memory.sh index db19a9d8..bf19a1b7 100644 --- a/scripts/memory_comparator/run_tests_compare_memory.sh +++ b/scripts/memory_comparator/run_tests_compare_memory.sh @@ -5,7 +5,7 @@ exit_code=0 cd starknet-devnet for file in test/test_*.py; do # Skip files that dont run entrypoints and dont produce memory and trace outputs - if ! ([ "$file" = "test/test_estimate_fee.py" ] || [ "$file" = "test/test_postman.py" ] || [ "$file" = "test/testnet_deployment.py" ] || [ "$file" = "test/testnet_deploy.py" ] || [ "$file" = "test/test_api_specifications.py" ] || [ "$file" = "test/test_fork_cli_params.py" ]); then + if ! ([ "$file" = "test/test_estimate_fee.py" ] || [ "$file" = "test/test_postman.py" ] || [ "$file" = "test/testnet_deployment.py" ] || [ "$file" = "test/testnet_deploy.py" ] || [ "$file" = "test/test_api_specifications.py" ] || [ "$file" = "test/test_fork_cli_params.py" ] || [ "$file" = "test/test_timestamps.py" ]); then # Run tests in cairo-rs-py env . ../scripts/memory_comparator/cairo-rs-py/bin/activate poetry run pytest $file From 7f8cfa71371b2c1acef4e5211a07c405ce5d24fd Mon Sep 17 00:00:00 2001 From: fmoletta <99273364+fmoletta@users.noreply.github.com> Date: Fri, 24 Feb 2023 17:01:51 -0300 Subject: [PATCH 57/57] Update scripts/memory_comparator/build_envs.sh Co-authored-by: Mario Rugiero --- scripts/memory_comparator/build_envs.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/memory_comparator/build_envs.sh b/scripts/memory_comparator/build_envs.sh index 81638062..f31fd8ea 100755 --- a/scripts/memory_comparator/build_envs.sh +++ b/scripts/memory_comparator/build_envs.sh @@ -7,8 +7,8 @@ set -e SCRIPT_DIR="scripts/memory_comparator" python3.9 -m venv --upgrade-deps ${SCRIPT_DIR}/cairo-lang ${SCRIPT_DIR}/cairo-rs-py -${SCRIPT_DIR}/cairo-lang/bin/pip install cairo-lang==0.10.1 -${SCRIPT_DIR}/cairo-rs-py/bin/pip install maturin==0.14.1 cairo-lang==0.10.1 +${SCRIPT_DIR}/cairo-lang/bin/pip install cairo-lang==0.10.3 +${SCRIPT_DIR}/cairo-rs-py/bin/pip install maturin==0.14.1 cairo-lang==0.10.3 ${SCRIPT_DIR}/cairo-rs-py/bin/maturin build --manifest-path Cargo.toml --release --strip --interpreter 3.9 --no-default-features --features extension ${SCRIPT_DIR}/cairo-rs-py/bin/pip install target/wheels/cairo_rs_py-*.whl