Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove copied code from cairo1_execution.rs #3131

Merged
merged 1 commit into from
Mar 31, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,125 +7,25 @@ use crate::runtime_extensions::common::get_relocated_vm_trace;
use blockifier::execution::contract_class::CompiledClassV1;
use blockifier::execution::entry_point::ExecutableCallEntryPoint;
use blockifier::execution::entry_point_execution::{
VmExecutionContext, finalize_execution, prepare_call_arguments,
};
use blockifier::execution::errors::PreExecutionError;
use blockifier::execution::execution_utils::{
ReadOnlySegments, write_felt, write_maybe_relocatable,
ExecutionRunnerMode, VmExecutionContext, finalize_execution,
initialize_execution_context_with_runner_mode, prepare_call_arguments,
};
use blockifier::execution::stack_trace::{
Cairo1RevertHeader, extract_trailing_cairo1_revert_trace,
};
use blockifier::execution::syscalls::hint_processor::SyscallHintProcessor;
use blockifier::versioned_constants::GasCosts;
use blockifier::{
execution::{
contract_class::EntryPointV1, entry_point::EntryPointExecutionContext,
errors::EntryPointExecutionError, execution_utils::Args,
},
state::state_api::State,
};
use cairo_vm::types::layout_name::LayoutName;
use cairo_vm::types::relocatable::MaybeRelocatable;
use cairo_vm::vm::errors::cairo_run_errors::CairoRunError;
use cairo_vm::{
hint_processor::hint_processor_definition::HintProcessor,
vm::runners::cairo_runner::{CairoArg, CairoRunner},
};
use runtime::{ExtendedRuntime, StarknetRuntime};
use starknet_types_core::felt::Felt;

// TODO(#2957) remove copied code
// Copied from https://github.com/starkware-libs/sequencer/blob/545761f29b859d06f125bd6c332b6182845734f0/crates/blockifier/src/execution/entry_point_execution.rs#L145
fn prepare_program_extra_data(
runner: &mut CairoRunner,
contract_class: &CompiledClassV1,
read_only_segments: &mut ReadOnlySegments,
gas_costs: &GasCosts,
) -> Result<usize, PreExecutionError> {
// Create the builtin cost segment, the builtin order should be the same as the price builtin
// array in the os in compiled_class.cairo in load_compiled_class_facts.
let builtin_price_array = [
gas_costs.builtins.pedersen,
gas_costs.builtins.bitwise,
gas_costs.builtins.ecop,
gas_costs.builtins.poseidon,
gas_costs.builtins.add_mod,
gas_costs.builtins.mul_mod,
];

let data = builtin_price_array
.iter()
.map(|&x| MaybeRelocatable::from(Felt::from(x)))
.collect::<Vec<_>>();
let builtin_cost_segment_start = read_only_segments.allocate(&mut runner.vm, &data)?;

// Put a pointer to the builtin cost segment at the end of the program (after the
// additional `ret` statement).
let mut ptr = (runner.vm.get_pc() + contract_class.bytecode_length())?;
// Push a `ret` opcode.
write_felt(
&mut runner.vm,
&mut ptr,
Felt::from(0x208b_7fff_7fff_7ffe_u128),
)?;
// Push a pointer to the builtin cost segment.
write_maybe_relocatable(&mut runner.vm, &mut ptr, builtin_cost_segment_start)?;

let program_extra_data_length = 2;
Ok(program_extra_data_length)
}

// TODO(#2957) remove copied code
// Copied from https://github.com/starkware-libs/sequencer/blob/0e1e92e0b90790e4bec20721c069c312d6a60a13/crates/blockifier/src/execution/entry_point_execution.rs#L98
fn initialize_execution_context<'a>(
call: ExecutableCallEntryPoint,
compiled_class: &'a CompiledClassV1,
state: &'a mut dyn State,
context: &'a mut EntryPointExecutionContext,
) -> Result<VmExecutionContext<'a>, PreExecutionError> {
let entry_point = compiled_class.get_entry_point(&call.type_and_selector())?;

// Instantiate Cairo runner.
let proof_mode = false;
// region: Modified blockifier code
let trace_enabled = true;
let mut runner = CairoRunner::new(
&compiled_class.0.program,
LayoutName::starknet,
proof_mode,
trace_enabled,
)?;
// endregion

runner.initialize_function_runner_cairo_1(&entry_point.builtins)?;
let mut read_only_segments = ReadOnlySegments::default();
let program_extra_data_length = prepare_program_extra_data(
&mut runner,
compiled_class,
&mut read_only_segments,
&context.versioned_constants().os_constants.gas_costs,
)?;

// Instantiate syscall handler.
let initial_syscall_ptr = runner.vm.add_memory_segment();
let syscall_handler = SyscallHintProcessor::new(
state,
context,
initial_syscall_ptr,
call,
&compiled_class.hints,
read_only_segments,
);

Ok(VmExecutionContext {
runner,
syscall_handler,
initial_syscall_ptr,
entry_point,
program_extra_data_length,
})
}

// blockifier/src/execution/cairo1_execution.rs:48 (execute_entry_point_call)
pub fn execute_entry_point_call_cairo1(
Expand All @@ -147,7 +47,13 @@ pub fn execute_entry_point_call_cairo1(
initial_syscall_ptr,
entry_point,
program_extra_data_length,
} = initialize_execution_context(call, compiled_class_v1, state, context)?;
} = initialize_execution_context_with_runner_mode(
call,
compiled_class_v1,
state,
context,
ExecutionRunnerMode::Tracing,
)?;

let args = prepare_call_arguments(
&syscall_handler.base.call,
Expand Down
Loading