Skip to content

Commit c20c2b2

Browse files
authored
Remove some copied code from copied_code.rs (#3589)
<!-- Reference any GitHub issues resolved by this PR --> Towards #3293 ## Introduced changes <!-- A brief description of the changes --> - ## Checklist <!-- Make sure all of these are complete --> - [ ] Linked relevant issue - [ ] Updated relevant documentation - [ ] Added relevant tests - [ ] Performed self-review of the code - [ ] Added changes to `CHANGELOG.md`
1 parent 9d69d05 commit c20c2b2

File tree

5 files changed

+11
-177
lines changed

5 files changed

+11
-177
lines changed

crates/forge-runner/src/running.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use anyhow::{Result, bail};
77
use blockifier::execution::call_info::CallInfo;
88
use blockifier::execution::contract_class::TrackedResource;
99
use blockifier::execution::entry_point::EntryPointExecutionContext;
10-
use blockifier::execution::entry_point_execution::prepare_call_arguments;
10+
use blockifier::execution::entry_point_execution::{prepare_call_arguments, run_entry_point};
1111
use blockifier::execution::errors::EntryPointExecutionError;
1212
use blockifier::state::cached_state::CachedState;
1313
use cairo_vm::Felt252;
@@ -53,7 +53,6 @@ mod syscall_handler;
5353
pub mod with_config;
5454

5555
use crate::debugging::{TraceVerbosity, build_debugging_trace};
56-
use crate::running::copied_code::run_entry_point;
5756
pub use hints::hints_to_params;
5857
use setup::VmExecutionContext;
5958
pub use syscall_handler::has_segment_arena;

crates/forge-runner/src/running/config_run.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use super::hints::hints_by_representation;
2-
use crate::running::copied_code::run_entry_point;
32
use crate::running::execution::finalize_execution;
43
use crate::running::setup::{
54
VmExecutionContext, build_test_call_and_entry_point, entry_point_initial_budget,
@@ -8,7 +7,7 @@ use crate::running::setup::{
87
use crate::{forge_config::ForgeTrackedResource, package_tests::TestDetails};
98
use anyhow::Result;
109
use blockifier::execution::contract_class::TrackedResource;
11-
use blockifier::execution::entry_point_execution::prepare_call_arguments;
10+
use blockifier::execution::entry_point_execution::{prepare_call_arguments, run_entry_point};
1211
use blockifier::state::{cached_state::CachedState, state_api::StateReader};
1312
use cheatnet::runtime_extensions::forge_config_extension::{
1413
ForgeConfigExtension, config::RawForgeConfig,

crates/forge-runner/src/running/copied_code.rs

Lines changed: 6 additions & 170 deletions
Original file line numberDiff line numberDiff line change
@@ -1,178 +1,14 @@
11
// TODO(#3293) Remove this file once the copied code is upstreamed to blockifier.
22
//! Module containing copied code to be upstreamed to blockifier
33
4-
use blockifier::blockifier_versioned_constants::GasCosts;
5-
use blockifier::execution::contract_class::{EntryPointV1, TrackedResource};
6-
use blockifier::execution::entry_point::EntryPointExecutionResult;
4+
use blockifier::execution::contract_class::TrackedResource;
75
use blockifier::execution::entry_point_execution::CallResult;
8-
use blockifier::execution::errors::{
9-
EntryPointExecutionError, PostExecutionError, PreExecutionError,
10-
};
11-
use blockifier::execution::execution_utils::{
12-
Args, ReadOnlySegments, read_execution_retdata, write_felt, write_maybe_relocatable,
13-
};
6+
use blockifier::execution::errors::PostExecutionError;
7+
use blockifier::execution::execution_utils::read_execution_retdata;
148
use blockifier::execution::syscalls::hint_processor::SyscallHintProcessor;
15-
use cairo_vm::hint_processor::hint_processor_definition::HintProcessor;
16-
use cairo_vm::types::builtin_name::BuiltinName;
17-
use cairo_vm::types::relocatable::{MaybeRelocatable, Relocatable};
18-
use cairo_vm::vm::errors::cairo_run_errors::CairoRunError;
19-
use cairo_vm::vm::errors::memory_errors::MemoryError;
20-
use cairo_vm::vm::errors::vm_errors::VirtualMachineError;
21-
use cairo_vm::vm::runners::builtin_runner::BuiltinRunner;
22-
use cairo_vm::vm::runners::cairo_runner::{CairoArg, CairoRunner};
23-
use cairo_vm::vm::security::verify_secure_runner;
24-
use num_traits::{ToPrimitive, Zero};
25-
use starknet_types_core::felt::Felt;
26-
27-
#[expect(clippy::needless_pass_by_value, clippy::result_large_err)]
28-
// Reason copied: Signature change
29-
/// Runs the runner from the given PC.
30-
pub(crate) fn run_entry_point<HP: HintProcessor>(
31-
runner: &mut CairoRunner,
32-
// Modified code
33-
// hint_processor: &mut SyscallHintProcessor<'_>,
34-
hint_processor: &mut HP,
35-
entry_point: EntryPointV1,
36-
args: Args,
37-
program_segment_size: usize,
38-
) -> EntryPointExecutionResult<()> {
39-
// Note that we run `verify_secure_runner` manually after filling the holes in the rc96 segment.
40-
let verify_secure = false;
41-
let args: Vec<&CairoArg> = args.iter().collect();
42-
runner.run_from_entrypoint(
43-
entry_point.pc(),
44-
&args,
45-
verify_secure,
46-
Some(program_segment_size),
47-
hint_processor,
48-
)?;
49-
50-
maybe_fill_holes(entry_point, runner)?;
51-
52-
verify_secure_runner(runner, false, Some(program_segment_size))
53-
.map_err(CairoRunError::VirtualMachine)?;
54-
55-
Ok(())
56-
}
57-
58-
#[expect(
59-
clippy::items_after_statements,
60-
clippy::needless_pass_by_value,
61-
clippy::result_large_err
62-
)]
63-
// Reason copied: Required by `run_entry_point`
64-
/// Fills the holes after running the entry point.
65-
/// Currently only fills the holes in the rc96 segment.
66-
fn maybe_fill_holes(
67-
entry_point: EntryPointV1,
68-
runner: &mut CairoRunner,
69-
) -> Result<(), EntryPointExecutionError> {
70-
let Some(rc96_offset) = entry_point
71-
.builtins
72-
.iter()
73-
.rev()
74-
.position(|name| *name == BuiltinName::range_check96)
75-
else {
76-
return Ok(());
77-
};
78-
let rc96_builtin_runner = runner
79-
.vm
80-
.get_builtin_runners()
81-
.iter()
82-
.find_map(|builtin| {
83-
if let BuiltinRunner::RangeCheck96(rc96_builtin_runner) = builtin {
84-
Some(rc96_builtin_runner)
85-
} else {
86-
None
87-
}
88-
})
89-
.expect("RangeCheck96 builtin runner not found.");
90-
91-
// 'EntryPointReturnValues' is returned after the implicits and its size is 5,
92-
// So the last implicit is at offset 5 + 1.
93-
const IMPLICITS_OFFSET: usize = 6;
94-
let rc_96_stop_ptr = (runner.vm.get_ap() - (IMPLICITS_OFFSET + rc96_offset))
95-
.map_err(|err| CairoRunError::VirtualMachine(VirtualMachineError::Math(err)))?;
96-
97-
let rc96_base = rc96_builtin_runner.base();
98-
let rc96_segment: isize = rc96_base
99-
.try_into()
100-
.expect("Builtin segment index must fit in isize.");
101-
102-
let Relocatable {
103-
segment_index: rc96_stop_segment,
104-
offset: stop_offset,
105-
} = runner
106-
.vm
107-
.get_relocatable(rc_96_stop_ptr)
108-
.map_err(CairoRunError::MemoryError)?;
109-
assert_eq!(rc96_stop_segment, rc96_segment);
110-
111-
// Update `segment_used_sizes` to include the holes.
112-
runner
113-
.vm
114-
.segments
115-
.segment_used_sizes
116-
.as_mut()
117-
.expect("Segments used sizes should be calculated at this point")[rc96_base] = stop_offset;
118-
119-
for offset in 0..stop_offset {
120-
match runner.vm.insert_value(
121-
Relocatable {
122-
segment_index: rc96_segment,
123-
offset,
124-
},
125-
Felt::zero(),
126-
) {
127-
// If the value is already set, ignore the error.
128-
Ok(()) | Err(MemoryError::InconsistentMemory(_)) => {}
129-
Err(err) => panic!("Unexpected error when filling holes: {err}."),
130-
}
131-
}
132-
133-
Ok(())
134-
}
135-
136-
// Reason copied: Private function
137-
pub(crate) fn prepare_program_extra_data(
138-
runner: &mut CairoRunner,
139-
// contract_class: &CompiledClassV1,
140-
bytecode_length: usize,
141-
read_only_segments: &mut ReadOnlySegments,
142-
gas_costs: &GasCosts,
143-
) -> Result<usize, PreExecutionError> {
144-
// Create the builtin cost segment, the builtin order should be the same as the price builtin
145-
// array in the os in compiled_class.cairo in load_compiled_class_facts.
146-
let builtin_price_array = [
147-
gas_costs.builtins.pedersen,
148-
gas_costs.builtins.bitwise,
149-
gas_costs.builtins.ecop,
150-
gas_costs.builtins.poseidon,
151-
gas_costs.builtins.add_mod,
152-
gas_costs.builtins.mul_mod,
153-
];
154-
155-
let data = builtin_price_array
156-
.iter()
157-
.map(|&x| MaybeRelocatable::from(Felt::from(x)))
158-
.collect::<Vec<_>>();
159-
let builtin_cost_segment_start = read_only_segments.allocate(&mut runner.vm, &data)?;
160-
161-
// Put a pointer to the builtin cost segment at the end of the program (after the
162-
// additional `ret` statement).
163-
let mut ptr = (runner.vm.get_pc() + bytecode_length)?;
164-
// Push a `ret` opcode.
165-
write_felt(
166-
&mut runner.vm,
167-
&mut ptr,
168-
Felt::from(0x208b_7fff_7fff_7ffe_u128),
169-
)?;
170-
// Push a pointer to the builtin cost segment.
171-
write_maybe_relocatable(&mut runner.vm, &mut ptr, builtin_cost_segment_start)?;
172-
173-
let program_extra_data_length = 2;
174-
Ok(program_extra_data_length)
175-
}
9+
use cairo_vm::types::relocatable::MaybeRelocatable;
10+
use cairo_vm::vm::runners::cairo_runner::CairoRunner;
11+
use num_traits::ToPrimitive;
17612

17713
#[expect(clippy::trivially_copy_pass_by_ref)]
17814
// Reason copied: Required by `finalize_execution`

crates/forge-runner/src/running/execution.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,10 @@ pub fn finalize_execution(
1919
program_extra_data_length: usize,
2020
tracked_resource: TrackedResource,
2121
) -> Result<CallInfo, PostExecutionError> {
22-
// region: Modified blockifier code
2322
finalize_runner(runner, n_total_args, program_extra_data_length)?;
2423
syscall_handler
2524
.read_only_segments
2625
.mark_as_accessed(runner)?;
27-
// endregion
2826

2927
let call_result = get_call_result(runner, syscall_handler, &tracked_resource)?;
3028

@@ -43,6 +41,7 @@ pub fn finalize_execution(
4341
);
4442

4543
let syscall_handler_base = &syscall_handler.base;
44+
// region: Modified blockifier code - added clones due to different function signature
4645
Ok(CallInfo {
4746
call: syscall_handler_base.call.clone().into(),
4847
execution: CallExecution {
@@ -59,4 +58,5 @@ pub fn finalize_execution(
5958
storage_access_tracker: syscall_handler_base.storage_access_tracker.clone(),
6059
builtin_counters: vm_resources_without_inner_calls.prover_builtins(),
6160
})
61+
// endregion
6262
}

crates/forge-runner/src/running/setup.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::package_tests::TestDetails;
2-
use crate::running::copied_code::prepare_program_extra_data;
32
use blockifier::execution::contract_class::EntryPointV1;
43
use blockifier::execution::entry_point::{EntryPointExecutionContext, ExecutableCallEntryPoint};
4+
use blockifier::execution::entry_point_execution::prepare_program_extra_data;
55
use blockifier::execution::errors::PreExecutionError;
66
use blockifier::execution::execution_utils::ReadOnlySegments;
77
use blockifier::execution::syscalls::hint_processor::SyscallHintProcessor;

0 commit comments

Comments
 (0)