Skip to content

Commit

Permalink
feat(starknet_os): implement load_entry_points_to_contract_class_stru…
Browse files Browse the repository at this point in the history
…ct for load_inner_class hint (#4640)
  • Loading branch information
rotem-starkware authored Mar 4, 2025
1 parent a57860d commit c8ee9d7
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
pub mod implementation;
#[allow(dead_code)]
pub mod utils;
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use cairo_vm::types::relocatable::{MaybeRelocatable, Relocatable};
use cairo_vm::vm::vm_core::VirtualMachine;
use starknet_api::contract_class::EntryPointType;
use starknet_api::deprecated_contract_class::ContractClass;
use starknet_types_core::felt::Felt;

use crate::hints::error::OsHintResult;
use crate::hints::vars::CairoStruct;
use crate::vm_utils::{insert_value_to_nested_field, IdentifierGetter};

#[allow(clippy::too_many_arguments)]
/// Loads the entry points of a deprecated contract class to a contract class struct, given a
/// specific entry point type.
fn load_entry_points_to_contract_class_struct<IG: IdentifierGetter>(
deprecated_class: &ContractClass,
entry_point_type: &EntryPointType,
class_base: Relocatable,
var_type: CairoStruct,
vm: &mut VirtualMachine,
identifier_getter: &IG,
entry_points_field: String,
num_entry_points_field: String,
) -> OsHintResult {
let empty_vec = Vec::new();
let entry_points =
deprecated_class.entry_points_by_type.get(entry_point_type).unwrap_or(&empty_vec);

let flat_entry_point_data: Vec<MaybeRelocatable> = entry_points
.iter()
.flat_map(|entry_point| {
vec![
MaybeRelocatable::from(entry_point.selector.0),
MaybeRelocatable::from(Felt::from(entry_point.offset.0)),
]
})
.collect();

insert_value_to_nested_field(
class_base,
var_type,
vm,
&[num_entry_points_field],
identifier_getter,
Felt::from(entry_points.len()),
)?;

let flat_entry_point_data_base = vm.add_memory_segment();
vm.load_data(flat_entry_point_data_base, &flat_entry_point_data)?;
insert_value_to_nested_field(
class_base,
var_type,
vm,
&[entry_points_field],
identifier_getter,
flat_entry_point_data_base,
)?;

Ok(())
}
2 changes: 2 additions & 0 deletions crates/starknet_os/src/hints/vars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ impl Const {
Self::AliasContractAddress.fetch_as(constants)
}
}

#[derive(Copy, Clone)]
pub enum CairoStruct {
CompiledClassFact,
DeprecatedCompiledClass,
Expand Down
1 change: 0 additions & 1 deletion crates/starknet_os/src/vm_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ fn fetch_nested_fields_address<IG: IdentifierGetter>(
}

/// Inserts a value to a nested field of a cairo variable given a base address.
#[allow(dead_code)]
pub(crate) fn insert_value_to_nested_field<IG: IdentifierGetter, T: Into<MaybeRelocatable>>(
base_address: Relocatable,
var_type: CairoStruct,
Expand Down

0 comments on commit c8ee9d7

Please sign in to comment.