Skip to content

Commit

Permalink
feat(blockifier): get sierra contract class from feature contract
Browse files Browse the repository at this point in the history
  • Loading branch information
AvivYossef-starkware committed Dec 5, 2024
1 parent abbecf9 commit 3efd03b
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
17 changes: 17 additions & 0 deletions crates/blockifier/src/test_utils/contracts.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::HashMap;

use cairo_lang_starknet_classes::casm_contract_class::CasmContractClass;
use cairo_lang_starknet_classes::contract_class::ContractClass as CairoLangContractClass;
use itertools::Itertools;
use starknet_api::abi::abi_utils::selector_from_name;
use starknet_api::abi::constants::CONSTRUCTOR_ENTRY_POINT_NAME;
Expand All @@ -10,6 +11,7 @@ use starknet_api::deprecated_contract_class::{
ContractClass as DeprecatedContractClass,
EntryPointOffset,
};
use starknet_api::state::SierraContractClass;
use starknet_api::{class_hash, contract_address, felt};
use starknet_types_core::felt::Felt;
use strum::IntoEnumIterator;
Expand Down Expand Up @@ -205,6 +207,21 @@ impl FeatureContract {
self.get_class().try_into().unwrap()
}

pub fn get_raw_sierra(&self) -> String {
if self.cairo_version() == CairoVersion::Cairo0 {
panic!("The sierra contract is only available for Cairo1.");
}

get_raw_contract_class(&self.get_sierra_path())
}

pub fn get_sierra(&self) -> SierraContractClass {
let raw_sierra = self.get_raw_sierra();
let cairo_contract_class: CairoLangContractClass =
serde_json::from_str(&raw_sierra).unwrap();
SierraContractClass::from(cairo_contract_class)
}

pub fn get_raw_class(&self) -> String {
get_raw_contract_class(&self.get_compiled_path())
}
Expand Down
13 changes: 13 additions & 0 deletions crates/starknet_api/src/rpc_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod rpc_transaction_test;

use std::collections::HashMap;

use cairo_lang_starknet_classes::contract_class::ContractEntryPoints as CairoLangContractEntryPoints;
use serde::{Deserialize, Serialize};

use crate::contract_class::EntryPointType;
Expand Down Expand Up @@ -282,6 +283,18 @@ pub struct EntryPointByType {
pub l1handler: Vec<EntryPoint>,
}

// TODO(AVIV): Consider removing this conversion and using CairoLangContractEntryPoints instead of
// defining the EntryPointByType struct.
impl From<CairoLangContractEntryPoints> for EntryPointByType {
fn from(value: CairoLangContractEntryPoints) -> Self {
Self {
constructor: value.constructor.into_iter().map(EntryPoint::from).collect(),
external: value.external.into_iter().map(EntryPoint::from).collect(),
l1handler: value.l1_handler.into_iter().map(EntryPoint::from).collect(),
}
}
}

impl EntryPointByType {
pub fn from_hash_map(entry_points_by_type: HashMap<EntryPointType, Vec<EntryPoint>>) -> Self {
macro_rules! get_entrypoint_by_type {
Expand Down
29 changes: 29 additions & 0 deletions crates/starknet_api/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ mod state_test;

use std::fmt::Debug;

use cairo_lang_starknet_classes::contract_class::{
ContractClass as CairoLangContractClass,
ContractEntryPoint as CairoLangContractEntryPoint,
};
use indexmap::IndexMap;
use serde::{Deserialize, Serialize};
use starknet_types_core::felt::Felt;
Expand Down Expand Up @@ -228,13 +232,38 @@ impl Default for SierraContractClass {
}
}

#[cfg(any(test, feature = "testing"))]
impl From<CairoLangContractClass> for SierraContractClass {
fn from(cairo_lang_contract_class: CairoLangContractClass) -> Self {
Self {
sierra_program: cairo_lang_contract_class
.sierra_program
.into_iter()
.map(|big_uint_as_hex| Felt::from(big_uint_as_hex.value))
.collect(),
contract_class_version: cairo_lang_contract_class.contract_class_version,
entry_points_by_type: cairo_lang_contract_class.entry_points_by_type.into(),
abi: cairo_lang_contract_class.abi.map(|abi| abi.json()).unwrap_or_default(),
}
}
}

/// An entry point of a [ContractClass](`crate::state::ContractClass`).
#[derive(Debug, Default, Clone, Eq, PartialEq, Hash, Deserialize, Serialize, PartialOrd, Ord)]
pub struct EntryPoint {
pub function_idx: FunctionIndex,
pub selector: EntryPointSelector,
}

impl From<CairoLangContractEntryPoint> for EntryPoint {
fn from(entry_point: CairoLangContractEntryPoint) -> Self {
Self {
function_idx: FunctionIndex(entry_point.function_idx),
selector: EntryPointSelector(entry_point.selector.into()),
}
}
}

#[derive(
Debug, Copy, Clone, Default, Eq, PartialEq, Hash, Deserialize, Serialize, PartialOrd, Ord,
)]
Expand Down

0 comments on commit 3efd03b

Please sign in to comment.