From dc6c2e416450f697861805cd53a703560ed8d1d8 Mon Sep 17 00:00:00 2001 From: AvivYossef-starkware Date: Tue, 26 Nov 2024 15:45:03 +0200 Subject: [PATCH] refactor(starknet_api): entry points by type field of sierra contact --- crates/papyrus_common/src/class_hash.rs | 1 + .../papyrus_protobuf/src/converters/class.rs | 6 +++++ crates/papyrus_rpc/src/v0_8/state.rs | 15 ++++++++++- .../src/serialization/serializers.rs | 10 ++++--- crates/papyrus_storage/src/utils.rs | 6 ++--- crates/papyrus_storage/src/utils_test.rs | 4 +-- crates/papyrus_test_utils/src/lib.rs | 4 ++- crates/starknet_api/src/rpc_transaction.rs | 27 +++++++++++++++++++ crates/starknet_api/src/state.rs | 5 ++-- .../src/reader/objects/state.rs | 3 ++- 10 files changed, 66 insertions(+), 15 deletions(-) diff --git a/crates/papyrus_common/src/class_hash.rs b/crates/papyrus_common/src/class_hash.rs index a1b1e91cad8..f464c2fc43a 100644 --- a/crates/papyrus_common/src/class_hash.rs +++ b/crates/papyrus_common/src/class_hash.rs @@ -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() diff --git a/crates/papyrus_protobuf/src/converters/class.rs b/crates/papyrus_protobuf/src/converters/class.rs index b7cca8aac17..2a34a5d219e 100644 --- a/crates/papyrus_protobuf/src/converters/class.rs +++ b/crates/papyrus_protobuf/src/converters/class.rs @@ -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; @@ -245,6 +246,8 @@ impl TryFrom 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, @@ -264,6 +267,7 @@ impl From 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() @@ -273,6 +277,7 @@ impl From for protobuf::Cairo1Class { externals: value .entry_points_by_type + .to_hash_map() .get(&EntryPointType::External) .unwrap_or(&vec![]) .iter() @@ -281,6 +286,7 @@ impl From for protobuf::Cairo1Class { .collect(), l1_handlers: value .entry_points_by_type + .to_hash_map() .get(&EntryPointType::L1Handler) .unwrap_or(&vec![]) .iter() diff --git a/crates/papyrus_rpc/src/v0_8/state.rs b/crates/papyrus_rpc/src/v0_8/state.rs index a50f705b235..7e0cd096e2e 100644 --- a/crates/papyrus_rpc/src/v0_8/state.rs +++ b/crates/papyrus_rpc/src/v0_8/state.rs @@ -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, @@ -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")] @@ -206,6 +209,16 @@ impl EntryPointByType { } } +impl From 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, @@ -219,7 +232,7 @@ impl From 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, } } diff --git a/crates/papyrus_storage/src/serialization/serializers.rs b/crates/papyrus_storage/src/serialization/serializers.rs index 6a76f7fc9b8..8daf2510c2b 100644 --- a/crates/papyrus_storage/src/serialization/serializers.rs +++ b/crates/papyrus_storage/src/serialization/serializers.rs @@ -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, @@ -243,6 +244,11 @@ auto_storage_serde! { pub function_idx: FunctionIndex, pub selector: EntryPointSelector, } + pub struct EntryPointByType { + pub constructor: Vec, + pub external: Vec, + pub l1handler: Vec, + } pub struct FunctionIndex(pub usize); pub struct EntryPointOffset(pub usize); pub struct EntryPointSelector(pub StarkHash); @@ -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::>::deserialize_from( - bytes, - )?, + entry_points_by_type: EntryPointByType::deserialize_from(bytes)?, abi: String::deserialize_from(&mut decompress_from_reader(bytes)?.as_slice())?, }) } diff --git a/crates/papyrus_storage/src/utils.rs b/crates/papyrus_storage/src/utils.rs index 2348137434d..157c7cbb7b1 100644 --- a/crates/papyrus_storage/src/utils.rs +++ b/crates/papyrus_storage/src/utils.rs @@ -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; @@ -28,7 +26,7 @@ struct DumpDeclaredClass { compiled_class_hash: CompiledClassHash, sierra_program: Vec, contract_class_version: String, - entry_points_by_type: HashMap>, + entry_points_by_type: EntryPointByType, } /// Dumps the declared_classes at a given block range from the storage to a file. diff --git a/crates/papyrus_storage/src/utils_test.rs b/crates/papyrus_storage/src/utils_test.rs index 33fede70617..8f69a6570bb 100644 --- a/crates/papyrus_storage/src/utils_test.rs +++ b/crates/papyrus_storage/src/utils_test.rs @@ -1,4 +1,3 @@ -use std::collections::HashMap; use std::fs; use indexmap::indexmap; @@ -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; @@ -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(), }, )); diff --git a/crates/papyrus_test_utils/src/lib.rs b/crates/papyrus_test_utils/src/lib.rs index 0a266f86203..92cd8d0f6dd 100644 --- a/crates/papyrus_test_utils/src/lib.rs +++ b/crates/papyrus_test_utils/src/lib.rs @@ -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, @@ -492,6 +493,7 @@ auto_impl_get_test_instance! { V0_13_3 = 18, V0_13_4 = 19, } + pub struct Calldata(pub Arc>); pub struct ClassHash(pub StarkHash); pub struct CompiledClassHash(pub StarkHash); @@ -499,7 +501,7 @@ auto_impl_get_test_instance! { pub struct SierraContractClass { pub sierra_program: Vec, pub contract_class_version: String, - pub entry_points_by_type: HashMap>, + pub entry_points_by_type: EntryPointByType, pub abi: String, } pub struct DeprecatedContractClass { diff --git a/crates/starknet_api/src/rpc_transaction.rs b/crates/starknet_api/src/rpc_transaction.rs index 76d3238e322..4607fdd45d5 100644 --- a/crates/starknet_api/src/rpc_transaction.rs +++ b/crates/starknet_api/src/rpc_transaction.rs @@ -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, @@ -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")] @@ -287,3 +291,26 @@ pub struct EntryPointByType { #[serde(rename = "L1_HANDLER")] pub l1handler: Vec, } + +impl EntryPointByType { + pub fn from_hash_map(entry_points_by_type: HashMap>) -> 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> { + HashMap::from_iter([ + (EntryPointType::Constructor, self.constructor.clone()), + (EntryPointType::External, self.external.clone()), + (EntryPointType::L1Handler, self.l1handler.clone()), + ]) + } +} diff --git a/crates/starknet_api/src/state.rs b/crates/starknet_api/src/state.rs index 3216ed90f1b..8bfb841bef4 100644 --- a/crates/starknet_api/src/state.rs +++ b/crates/starknet_api/src/state.rs @@ -2,7 +2,6 @@ #[path = "state_test.rs"] mod state_test; -use std::collections::HashMap; use std::fmt::Debug; use indexmap::IndexMap; @@ -10,7 +9,6 @@ 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, @@ -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; @@ -214,7 +213,7 @@ impl_from_through_intermediate!(u128, StorageKey, u8, u16, u32, u64); pub struct SierraContractClass { pub sierra_program: Vec, pub contract_class_version: String, - pub entry_points_by_type: HashMap>, + pub entry_points_by_type: EntryPointByType, pub abi: String, } diff --git a/crates/starknet_client/src/reader/objects/state.rs b/crates/starknet_client/src/reader/objects/state.rs index eb70e036f36..8fc5329042d 100644 --- a/crates/starknet_client/src/reader/objects/state.rs +++ b/crates/starknet_client/src/reader/objects/state.rs @@ -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; @@ -68,7 +69,7 @@ impl From 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, } }