|
1 | 1 | // TODO(#3293) Remove this file once the copied code is upstreamed to blockifier.
|
2 | 2 | //! Module containing copied code to be upstreamed to blockifier
|
3 | 3 |
|
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; |
7 | 5 | 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; |
14 | 8 | 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; |
176 | 12 |
|
177 | 13 | #[expect(clippy::trivially_copy_pass_by_ref)]
|
178 | 14 | // Reason copied: Required by `finalize_execution`
|
|
0 commit comments