Skip to content

Commit

Permalink
refactor(starknet_api): entry points by type field of sierra contact
Browse files Browse the repository at this point in the history
  • Loading branch information
AvivYossef-starkware committed Nov 27, 2024
1 parent 0131a00 commit 378185a
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 15 deletions.
1 change: 1 addition & 0 deletions crates/papyrus_common/src/class_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ fn entry_points_hash(
PoseidonHash(Poseidon::hash_array(
class
.entry_points_by_type
.to_hash_map()
.get(entry_point_type)
.unwrap_or(&vec![])
.iter()
Expand Down
6 changes: 6 additions & 0 deletions crates/papyrus_protobuf/src/converters/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use serde::Serialize;
use starknet_api::contract_class::EntryPointType;
use starknet_api::core::{ClassHash, EntryPointSelector};
use starknet_api::data_availability::DataAvailabilityMode;
use starknet_api::rpc_transaction::EntryPointByType;
use starknet_api::{deprecated_contract_class, state};
use starknet_types_core::felt::Felt;

Expand Down Expand Up @@ -245,6 +246,8 @@ impl TryFrom<protobuf::Cairo1Class> for state::SierraContractClass {
);
}

let entry_points_by_type = EntryPointByType::from_hash_map(entry_points_by_type);

Ok(state::SierraContractClass {
sierra_program,
entry_points_by_type,
Expand All @@ -264,6 +267,7 @@ impl From<state::SierraContractClass> for protobuf::Cairo1Class {
let entry_points = Some(protobuf::Cairo1EntryPoints {
constructors: value
.entry_points_by_type
.to_hash_map()
.get(&EntryPointType::Constructor)
.unwrap_or(&vec![])
.iter()
Expand All @@ -273,6 +277,7 @@ impl From<state::SierraContractClass> for protobuf::Cairo1Class {

externals: value
.entry_points_by_type
.to_hash_map()
.get(&EntryPointType::External)
.unwrap_or(&vec![])
.iter()
Expand All @@ -281,6 +286,7 @@ impl From<state::SierraContractClass> for protobuf::Cairo1Class {
.collect(),
l1_handlers: value
.entry_points_by_type
.to_hash_map()
.get(&EntryPointType::L1Handler)
.unwrap_or(&vec![])
.iter()
Expand Down
15 changes: 14 additions & 1 deletion crates/papyrus_rpc/src/v0_8/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use serde::{Deserialize, Serialize};
use starknet_api::block::BlockHash;
use starknet_api::contract_class::EntryPointType;
use starknet_api::core::{ClassHash, CompiledClassHash, ContractAddress, GlobalRoot, Nonce};
use starknet_api::rpc_transaction::EntryPointByType as starknet_api_EntryPointByType;
use starknet_api::state::{EntryPoint, StorageKey, ThinStateDiff as starknet_api_ThinStateDiff};
use starknet_client::reader::objects::state::{
DeclaredClassHashEntry as ClientDeclaredClassHashEntry,
Expand Down Expand Up @@ -173,6 +174,8 @@ pub struct StorageEntry {
pub key: StorageKey,
pub value: Felt,
}

// TODO(Aviv): Remove this and use sn_api::rpc_transaction::EntryPointByType.
#[derive(Debug, Clone, Default, Eq, PartialEq, Deserialize, Serialize)]
pub struct EntryPointByType {
#[serde(rename = "CONSTRUCTOR")]
Expand Down Expand Up @@ -206,6 +209,16 @@ impl EntryPointByType {
}
}

impl From<starknet_api_EntryPointByType> for EntryPointByType {
fn from(entry_points: starknet_api_EntryPointByType) -> Self {
Self {
constructor: entry_points.constructor.into_iter().map(EntryPoint::from).collect(),
external: entry_points.external.into_iter().map(EntryPoint::from).collect(),
l1handler: entry_points.l1handler.into_iter().map(EntryPoint::from).collect(),
}
}
}

#[derive(Debug, Clone, Default, Eq, PartialEq, Deserialize, Serialize)]
pub struct ContractClass {
pub sierra_program: Vec<Felt>,
Expand All @@ -219,7 +232,7 @@ impl From<starknet_api::state::SierraContractClass> for ContractClass {
Self {
sierra_program: class.sierra_program,
contract_class_version: CONTRACT_CLASS_VERSION.to_owned(),
entry_points_by_type: EntryPointByType::from_hash_map(class.entry_points_by_type),
entry_points_by_type: class.entry_points_by_type.into(),
abi: class.abi,
}
}
Expand Down
10 changes: 7 additions & 3 deletions crates/papyrus_storage/src/serialization/serializers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ use starknet_api::deprecated_contract_class::{
};
use starknet_api::execution_resources::{Builtin, ExecutionResources, GasAmount, GasVector};
use starknet_api::hash::{PoseidonHash, StarkHash};
use starknet_api::rpc_transaction::EntryPointByType;
use starknet_api::state::{
EntryPoint,
FunctionIndex,
Expand Down Expand Up @@ -243,6 +244,11 @@ auto_storage_serde! {
pub function_idx: FunctionIndex,
pub selector: EntryPointSelector,
}
pub struct EntryPointByType {
pub constructor: Vec<EntryPoint>,
pub external: Vec<EntryPoint>,
pub l1handler: Vec<EntryPoint>,
}
pub struct FunctionIndex(pub usize);
pub struct EntryPointOffset(pub usize);
pub struct EntryPointSelector(pub StarkHash);
Expand Down Expand Up @@ -981,9 +987,7 @@ impl StorageSerde for SierraContractClass {
&mut decompress_from_reader(bytes)?.as_slice(),
)?,
contract_class_version: String::deserialize_from(bytes)?,
entry_points_by_type: HashMap::<EntryPointType, Vec<EntryPoint>>::deserialize_from(
bytes,
)?,
entry_points_by_type: EntryPointByType::deserialize_from(bytes)?,
abi: String::deserialize_from(&mut decompress_from_reader(bytes)?.as_slice())?,
})
}
Expand Down
6 changes: 2 additions & 4 deletions crates/papyrus_storage/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@
#[path = "utils_test.rs"]
mod utils_test;

use std::collections::HashMap;
use std::fs::File;
use std::io::{BufWriter, Write};

use metrics::{absolute_counter, gauge};
use serde::Serialize;
use starknet_api::block::BlockNumber;
use starknet_api::contract_class::EntryPointType;
use starknet_api::core::{ChainId, ClassHash, CompiledClassHash};
use starknet_api::state::EntryPoint;
use starknet_api::rpc_transaction::EntryPointByType;
use starknet_types_core::felt::Felt;
use tracing::debug;

Expand All @@ -28,7 +26,7 @@ struct DumpDeclaredClass {
compiled_class_hash: CompiledClassHash,
sierra_program: Vec<Felt>,
contract_class_version: String,
entry_points_by_type: HashMap<EntryPointType, Vec<EntryPoint>>,
entry_points_by_type: EntryPointByType,
}

/// Dumps the declared_classes at a given block range from the storage to a file.
Expand Down
4 changes: 2 additions & 2 deletions crates/papyrus_storage/src/utils_test.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::collections::HashMap;
use std::fs;

use indexmap::indexmap;
Expand All @@ -9,6 +8,7 @@ use prometheus_parse::Value::{Counter, Gauge};
use starknet_api::block::BlockNumber;
use starknet_api::core::{ClassHash, CompiledClassHash};
use starknet_api::hash::StarkHash;
use starknet_api::rpc_transaction::EntryPointByType;
use starknet_api::state::{SierraContractClass, ThinStateDiff};
use starknet_types_core::felt::Felt;

Expand All @@ -33,7 +33,7 @@ fn test_dump_declared_classes() {
SierraContractClass {
sierra_program: vec![i_felt, i_felt],
contract_class_version: "0.1.0".to_string(),
entry_points_by_type: HashMap::new(),
entry_points_by_type: EntryPointByType::default(),
abi: "".to_string(),
},
));
Expand Down
4 changes: 3 additions & 1 deletion crates/papyrus_test_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ use starknet_api::hash::{PoseidonHash, StarkHash};
use starknet_api::rpc_transaction::{
ContractClass as RpcContractClass,
EntryPointByType as RpcEntryPointByType,
EntryPointByType,
RpcDeclareTransaction,
RpcDeclareTransactionV3,
RpcDeployAccountTransaction,
Expand Down Expand Up @@ -492,14 +493,15 @@ auto_impl_get_test_instance! {
V0_13_3 = 18,
V0_13_4 = 19,
}

pub struct Calldata(pub Arc<Vec<Felt>>);
pub struct ClassHash(pub StarkHash);
pub struct CompiledClassHash(pub StarkHash);
pub struct ContractAddressSalt(pub StarkHash);
pub struct SierraContractClass {
pub sierra_program: Vec<Felt>,
pub contract_class_version: String,
pub entry_points_by_type: HashMap<EntryPointType, Vec<EntryPoint>>,
pub entry_points_by_type: EntryPointByType,
pub abi: String,
}
pub struct DeprecatedContractClass {
Expand Down
27 changes: 27 additions & 0 deletions crates/starknet_api/src/rpc_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
#[path = "rpc_transaction_test.rs"]
mod rpc_transaction_test;

use std::collections::HashMap;

use serde::{Deserialize, Serialize};
use starknet_types_core::felt::Felt;

use crate::contract_class::EntryPointType;
use crate::core::{
calculate_contract_address,
ClassHash,
Expand Down Expand Up @@ -278,6 +281,7 @@ pub struct ContractClass {
pub abi: String,
}

// TODO(Aviv): remove duplication with sequencer/crates/papyrus_rpc/src/v0_8/state.rs
#[derive(Debug, Clone, Default, Eq, PartialEq, Deserialize, Serialize, Hash)]
pub struct EntryPointByType {
#[serde(rename = "CONSTRUCTOR")]
Expand All @@ -287,3 +291,26 @@ pub struct EntryPointByType {
#[serde(rename = "L1_HANDLER")]
pub l1handler: Vec<EntryPoint>,
}

impl EntryPointByType {
pub fn from_hash_map(entry_points_by_type: HashMap<EntryPointType, Vec<EntryPoint>>) -> Self {
macro_rules! get_entrypoint_by_type {
($variant:ident) => {
(*(entry_points_by_type.get(&EntryPointType::$variant).unwrap_or(&vec![]))).to_vec()
};
}

Self {
constructor: get_entrypoint_by_type!(Constructor),
external: get_entrypoint_by_type!(External),
l1handler: get_entrypoint_by_type!(L1Handler),
}
}
pub fn to_hash_map(&self) -> HashMap<EntryPointType, Vec<EntryPoint>> {
HashMap::from_iter([
(EntryPointType::Constructor, self.constructor.clone()),
(EntryPointType::External, self.external.clone()),
(EntryPointType::L1Handler, self.l1handler.clone()),
])
}
}
5 changes: 2 additions & 3 deletions crates/starknet_api/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@
#[path = "state_test.rs"]
mod state_test;

use std::collections::HashMap;
use std::fmt::Debug;

use indexmap::IndexMap;
use serde::{Deserialize, Serialize};
use starknet_types_core::felt::Felt;

use crate::block::{BlockHash, BlockNumber};
use crate::contract_class::EntryPointType;
use crate::core::{
ClassHash,
CompiledClassHash,
Expand All @@ -22,6 +20,7 @@ use crate::core::{
};
use crate::deprecated_contract_class::ContractClass as DeprecatedContractClass;
use crate::hash::StarkHash;
use crate::rpc_transaction::EntryPointByType;
use crate::{impl_from_through_intermediate, StarknetApiError};

pub type DeclaredClasses = IndexMap<ClassHash, SierraContractClass>;
Expand Down Expand Up @@ -214,7 +213,7 @@ impl_from_through_intermediate!(u128, StorageKey, u8, u16, u32, u64);
pub struct SierraContractClass {
pub sierra_program: Vec<Felt>,
pub contract_class_version: String,
pub entry_points_by_type: HashMap<EntryPointType, Vec<EntryPoint>>,
pub entry_points_by_type: EntryPointByType,
pub abi: String,
}

Expand Down
3 changes: 2 additions & 1 deletion crates/starknet_client/src/reader/objects/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use serde::{Deserialize, Serialize};
use starknet_api::block::BlockHash;
use starknet_api::contract_class::EntryPointType;
use starknet_api::core::{ClassHash, ContractAddress, GlobalRoot, Nonce};
use starknet_api::rpc_transaction::EntryPointByType;
use starknet_api::state::EntryPoint;
use starknet_types_core::felt::Felt;

Expand Down Expand Up @@ -68,7 +69,7 @@ impl From<ContractClass> for starknet_api::state::SierraContractClass {
Self {
sierra_program: class.sierra_program,
contract_class_version: class.contract_class_version,
entry_points_by_type: class.entry_points_by_type,
entry_points_by_type: EntryPointByType::from_hash_map(class.entry_points_by_type),
abi: class.abi,
}
}
Expand Down

0 comments on commit 378185a

Please sign in to comment.