From f0a11570f23430e6ab3b566ae49fd0e55eaf894d Mon Sep 17 00:00:00 2001 From: Yonatan Iluz Date: Mon, 5 Aug 2024 11:16:50 +0300 Subject: [PATCH] chore: use BuiltinName enum in EntryPointV1 instead of string --- .../src/execution/contract_class.rs | 26 ++++++++--------- .../src/execution/entry_point_execution.rs | 28 ++++--------------- crates/blockifier/src/execution/errors.rs | 4 +-- 3 files changed, 21 insertions(+), 37 deletions(-) diff --git a/crates/blockifier/src/execution/contract_class.rs b/crates/blockifier/src/execution/contract_class.rs index c6f1afe6128..4e182ed837d 100644 --- a/crates/blockifier/src/execution/contract_class.rs +++ b/crates/blockifier/src/execution/contract_class.rs @@ -347,7 +347,7 @@ pub struct ContractClassV1Inner { pub struct EntryPointV1 { pub selector: EntryPointSelector, pub offset: EntryPointOffset, - pub builtins: Vec, + pub builtins: Vec, } impl EntryPointV1 { @@ -403,15 +403,15 @@ impl TryFrom 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 @@ -452,17 +452,17 @@ fn hint_to_hint_params(hint: &cairo_lang_casm::hints::Hint) -> Result, -) -> Result, ProgramError> { +fn convert_entry_points_v1(external: Vec) -> Vec { 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() } diff --git a/crates/blockifier/src/execution/entry_point_execution.rs b/crates/blockifier/src/execution/entry_point_execution.rs index 9e5bae7245d..9d3f893bc1c 100644 --- a/crates/blockifier/src/execution/entry_point_execution.rs +++ b/crates/blockifier/src/execution/entry_point_execution.rs @@ -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)?; @@ -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. @@ -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)))); @@ -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(()); }; diff --git a/crates/blockifier/src/execution/errors.rs b/crates/blockifier/src/execution/errors.rs index cc0e87a7531..04bb87a484c 100644 --- a/crates/blockifier/src/execution/errors.rs +++ b/crates/blockifier/src/execution/errors.rs @@ -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)]