Description
The function crate_entry_code
has increased in it’s size to a point in which it has become very tedious and hard to read, and understand. The main objective of this refactor would be to separe the main parts of it into smaller functions to improve its readability, primarily to make it easier to debug if that was the case. So this does not imply any other action than moving code.
Currently, create_entry_code
can be divided into four groups (which must be in the order stated):
- Initialize arguments. This include:
- Initializing builtin segments.
- Initializing variables for the return types.
- Copy return values. This include:
- Serialize return values
- Copy serialized values to the output segment.
- Validate segment arena. This include:
- Call the hint that relocates all dictionaries
- Verify every segment in the SegmentArena is contiguos (in after each other).
- Finalize builder.
NOTE: group 3 is inside group 2, This is because of how the function was coded. Since both coping outputs and verifying the segment arena only happen during proof mode, the latter group is inside the former.
With these four steps performed in the function, we could take the first 3 and separate them into 3 different functions:
fn initialize_arguments(
ctx: &mut CasmBuilderconfig,
config: &Cairo1RunConfig,
...
);
fn copy_return_values_to_output(
ctx: &mut CasmBuilderconfig,
config: &Cairo1RunConfig,
...
);
fn validate_segment_arena(
ctx: &mut CasmBuilderconfig,
config: &Cairo1RunConfig,
...
);
Since the fourth step is to simply finalize the builder, it fine to leave it in the main function.
These three new functions must be called in the order stated above as it is a requirement for vm to be able to work properly.
NOTE: Since copy return values implies validating the SegmentArena, since both occur only in proof_mode. validate_segment_arena
is called inside copy_return_values_to_output
after copying return values.