Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(blockifier): get sierra contract class from feature contract #2466

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
28 changes: 28 additions & 0 deletions crates/starknet_api/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod state_test;

use std::fmt::Debug;

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

#[cfg(any(test, feature = "testing"))]
impl From<cairo_lang_starknet_classes::contract_class::ContractClass> for SierraContractClass {
fn from(
cairo_lang_contract_class: cairo_lang_starknet_classes::contract_class::ContractClass,
) -> 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
Loading