Skip to content

Commit

Permalink
refactor(starknet_class_manager_types): make class hash output of `ad…
Browse files Browse the repository at this point in the history
…d_class` (#3494)

In untrusted flows each node needs to calculate class hash by itself.
Originally, I wanted the GW to first run
`__validate_declare__(class_hash)` and if successful - request compilation;
but it isn't worth optimizing on (`Declare`s are quite rare).
  • Loading branch information
elintul authored Jan 22, 2025
1 parent 785b84b commit 9aba93a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 25 deletions.
4 changes: 2 additions & 2 deletions crates/papyrus_p2p_sync/src/client/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ impl BlockData for (DeclaredClasses, DeprecatedDeclaredClasses, BlockNumber) {
) -> BoxFuture<'a, Result<(), P2pSyncClientError>> {
async move {
// TODO(noamsp): handle non fatal errors by reporting the peer instead of failing
for (class_hash, class) in self.0 {
class_manager_client.add_class(class_hash, class).await?;
for (_class_hash, class) in self.0 {
class_manager_client.add_class(class).await?;
}

for (class_hash, deprecated_class) in self.1 {
Expand Down
10 changes: 6 additions & 4 deletions crates/papyrus_p2p_sync/src/client/class_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use starknet_api::deprecated_contract_class::{
EntryPointV0,
};
use starknet_api::state::SierraContractClass;
use starknet_class_manager_types::MockClassManagerClient;
use starknet_class_manager_types::{ClassHashes, MockClassManagerClient};

use super::test_utils::{
random_header,
Expand Down Expand Up @@ -60,12 +60,14 @@ async fn class_basic_flow() {
let class_hash = state_diff.get_class_hash();
match class {
ApiContractClass::ContractClass(class) => {
let compiled_class_hash = state_diff.get_compiled_class_hash();
let executable_class_hash = state_diff.get_compiled_class_hash();
class_manager_client
.expect_add_class()
.times(1)
.with(eq(class_hash), eq(class.clone()))
.return_once(move |_, _| Ok(compiled_class_hash));
.with(eq(class.clone()))
.return_once(move |_| {
Ok(ClassHashes { class_hash, executable_class_hash })
});
}
ApiContractClass::DeprecatedContractClass(class) => {
class_manager_client
Expand Down
3 changes: 3 additions & 0 deletions crates/starknet_class_manager_types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ serde = { workspace = true, features = ["derive"] }
starknet_api.workspace = true
starknet_sequencer_infra.workspace = true
thiserror.workspace = true

[dev-dependencies]
starknet_api = { workspace = true, features = ["testing"] }
31 changes: 12 additions & 19 deletions crates/starknet_class_manager_types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,19 @@ pub type Class = SierraContractClass;
pub type ExecutableClass = ContractClass;
pub type ExecutableClassHash = CompiledClassHash;

#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct ClassHashes {
pub class_hash: ClassHash,
pub executable_class_hash: ExecutableClassHash,
}

/// Serves as the class manager's shared interface.
/// Requires `Send + Sync` to allow transferring and sharing resources (inputs, futures) across
/// threads.
#[cfg_attr(feature = "testing", automock)]
#[async_trait]
pub trait ClassManagerClient: Send + Sync {
// TODO(native): make generic in executable type.
async fn add_class(
&self,
class_id: ClassId,
class: Class,
) -> ClassManagerClientResult<ExecutableClassHash>;
async fn add_class(&self, class: Class) -> ClassManagerClientResult<ClassHashes>;

async fn get_executable(&self, class_id: ClassId) -> ClassManagerClientResult<ExecutableClass>;

Expand Down Expand Up @@ -87,15 +88,15 @@ pub enum ClassManagerClientError {

#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum ClassManagerRequest {
AddClass(ClassId, Class),
AddClass(Class),
AddDeprecatedClass(ClassId, DeprecatedClass),
GetExecutable(ClassId),
GetSierra(ClassId),
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum ClassManagerResponse {
AddClass(ClassManagerResult<ExecutableClassHash>),
AddClass(ClassManagerResult<ClassHashes>),
AddDeprecatedClass(ClassManagerResult<()>),
GetExecutable(ClassManagerResult<ExecutableClass>),
GetSierra(ClassManagerResult<Class>),
Expand All @@ -106,12 +107,8 @@ impl<ComponentClientType> ClassManagerClient for ComponentClientType
where
ComponentClientType: Send + Sync + ComponentClient<ClassManagerRequest, ClassManagerResponse>,
{
async fn add_class(
&self,
class_id: ClassId,
class: Class,
) -> ClassManagerClientResult<ExecutableClassHash> {
let request = ClassManagerRequest::AddClass(class_id, class);
async fn add_class(&self, class: Class) -> ClassManagerClientResult<ClassHashes> {
let request = ClassManagerRequest::AddClass(class);
handle_all_response_variants!(
ClassManagerResponse,
AddClass,
Expand Down Expand Up @@ -163,11 +160,7 @@ pub struct EmptyClassManagerClient;

#[async_trait]
impl ClassManagerClient for EmptyClassManagerClient {
async fn add_class(
&self,
_class_id: ClassId,
_class: Class,
) -> ClassManagerClientResult<ExecutableClassHash> {
async fn add_class(&self, _class: Class) -> ClassManagerClientResult<ClassHashes> {
Ok(Default::default())
}

Expand Down

0 comments on commit 9aba93a

Please sign in to comment.