Skip to content

Commit

Permalink
chore: use BuiltinName enum in EntryPointV1 instead of string
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoni-Starkware committed Aug 5, 2024
1 parent 8844718 commit f0a1157
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 37 deletions.
26 changes: 13 additions & 13 deletions crates/blockifier/src/execution/contract_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ pub struct ContractClassV1Inner {
pub struct EntryPointV1 {
pub selector: EntryPointSelector,
pub offset: EntryPointOffset,
pub builtins: Vec<String>,
pub builtins: Vec<BuiltinName>,
}

impl EntryPointV1 {
Expand Down Expand Up @@ -403,15 +403,15 @@ impl TryFrom<CasmContractClass> for ContractClassV1 {
let mut entry_points_by_type = HashMap::new();
entry_points_by_type.insert(
EntryPointType::Constructor,
convert_entry_points_v1(class.entry_points_by_type.constructor)?,
convert_entry_points_v1(class.entry_points_by_type.constructor),
);
entry_points_by_type.insert(
EntryPointType::External,
convert_entry_points_v1(class.entry_points_by_type.external)?,
convert_entry_points_v1(class.entry_points_by_type.external),
);
entry_points_by_type.insert(
EntryPointType::L1Handler,
convert_entry_points_v1(class.entry_points_by_type.l1_handler)?,
convert_entry_points_v1(class.entry_points_by_type.l1_handler),
);

let bytecode_segment_lengths = class
Expand Down Expand Up @@ -452,17 +452,17 @@ fn hint_to_hint_params(hint: &cairo_lang_casm::hints::Hint) -> Result<HintParams
})
}

fn convert_entry_points_v1(
external: Vec<CasmContractEntryPoint>,
) -> Result<Vec<EntryPointV1>, ProgramError> {
fn convert_entry_points_v1(external: Vec<CasmContractEntryPoint>) -> Vec<EntryPointV1> {
external
.into_iter()
.map(|ep| -> Result<_, ProgramError> {
Ok(EntryPointV1 {
selector: EntryPointSelector(Felt::from(ep.selector)),
offset: EntryPointOffset(ep.offset),
builtins: ep.builtins.into_iter().map(|builtin| builtin + "_builtin").collect(),
})
.map(|ep| EntryPointV1 {
selector: EntryPointSelector(Felt::from(ep.selector)),
offset: EntryPointOffset(ep.offset),
builtins: ep
.builtins
.into_iter()
.map(|builtin| BuiltinName::from_str(&builtin).expect("Unrecognized builtin."))
.collect(),
})
.collect()
}
Expand Down
28 changes: 6 additions & 22 deletions crates/blockifier/src/execution/entry_point_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,20 +163,7 @@ pub fn initialize_execution_context<'a>(
trace_enabled,
)?;

// Initialize program with all builtins.
let program_builtins = [
BuiltinName::bitwise,
BuiltinName::ec_op,
BuiltinName::ecdsa,
BuiltinName::pedersen,
BuiltinName::poseidon,
BuiltinName::range_check,
BuiltinName::segment_arena,
BuiltinName::range_check96,
BuiltinName::add_mod,
BuiltinName::mul_mod,
];
runner.initialize_function_runner_cairo_1(&program_builtins)?;
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, contract_class, &mut read_only_segments)?;
Expand Down Expand Up @@ -239,16 +226,13 @@ pub fn prepare_call_arguments(

// Push builtins.
for builtin_name in &entrypoint.builtins {
if let Some(builtin) = runner
.vm
.get_builtin_runners()
.iter()
.find(|builtin| builtin.name().to_str_with_suffix() == builtin_name)
if let Some(builtin) =
runner.vm.get_builtin_runners().iter().find(|builtin| builtin.name() == *builtin_name)
{
args.extend(builtin.initial_stack().into_iter().map(CairoArg::Single));
continue;
}
if builtin_name == BuiltinName::segment_arena.to_str_with_suffix() {
if builtin_name == &BuiltinName::segment_arena {
let segment_arena = runner.vm.add_memory_segment();

// Write into segment_arena.
Expand All @@ -263,7 +247,7 @@ pub fn prepare_call_arguments(
args.push(CairoArg::Single(MaybeRelocatable::from(ptr)));
continue;
}
return Err(PreExecutionError::InvalidBuiltin(builtin_name.clone()));
return Err(PreExecutionError::InvalidBuiltin(*builtin_name));
}
// Push gas counter.
args.push(CairoArg::Single(MaybeRelocatable::from(Felt::from(call.initial_gas))));
Expand Down Expand Up @@ -319,7 +303,7 @@ fn maybe_fill_holes(
.builtins
.iter()
.rev()
.position(|name| name.as_str() == BuiltinName::range_check96.to_str_with_suffix())
.position(|name| *name == BuiltinName::range_check96)
else {
return Ok(());
};
Expand Down
4 changes: 2 additions & 2 deletions crates/blockifier/src/execution/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ pub enum PreExecutionError {
EntryPointNotFound(EntryPointSelector),
#[error("Fraud attempt blocked.")]
FraudAttempt,
#[error("Invalid builtin {0:?}.")]
InvalidBuiltin(String),
#[error("Invalid builtin {0}.")]
InvalidBuiltin(BuiltinName),
#[error("The constructor entry point must be named 'constructor'.")]
InvalidConstructorEntryPointName,
#[error(transparent)]
Expand Down

0 comments on commit f0a1157

Please sign in to comment.