From 76a1e577c23a8f1bd62bd47b4010fc0f81fc173c Mon Sep 17 00:00:00 2001 From: Shahak Shama Date: Wed, 13 Nov 2024 11:29:24 +0200 Subject: [PATCH] feat(sync): add StateSyncClient and SyncBlock --- Cargo.lock | 11 ++++++++ Cargo.toml | 1 + crates/starknet_state_sync_types/Cargo.toml | 16 +++++++++++ .../src/communication.rs | 28 +++++++++++++++++++ .../starknet_state_sync_types/src/errors.rs | 6 ++++ crates/starknet_state_sync_types/src/lib.rs | 3 ++ .../src/state_sync_types.rs | 21 ++++++++++++++ 7 files changed, 86 insertions(+) create mode 100644 crates/starknet_state_sync_types/Cargo.toml create mode 100644 crates/starknet_state_sync_types/src/communication.rs create mode 100644 crates/starknet_state_sync_types/src/errors.rs create mode 100644 crates/starknet_state_sync_types/src/lib.rs create mode 100644 crates/starknet_state_sync_types/src/state_sync_types.rs diff --git a/Cargo.lock b/Cargo.lock index c23bd611598..1c0bf762953 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10517,6 +10517,17 @@ dependencies = [ "validator", ] +[[package]] +name = "starknet_state_sync_types" +version = "0.0.0" +dependencies = [ + "async-trait", + "serde", + "starknet_api", + "starknet_sequencer_infra", + "thiserror", +] + [[package]] name = "starknet_task_executor" version = "0.0.0" diff --git a/Cargo.toml b/Cargo.toml index 53bfa5ade96..ada270fb5a0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -48,6 +48,7 @@ members = [ "crates/starknet_sequencer_infra", "crates/starknet_sequencer_node", "crates/starknet_sierra_compile", + "crates/starknet_state_sync_types", "crates/starknet_task_executor", "workspace_tests", ] diff --git a/crates/starknet_state_sync_types/Cargo.toml b/crates/starknet_state_sync_types/Cargo.toml new file mode 100644 index 00000000000..28e72f14026 --- /dev/null +++ b/crates/starknet_state_sync_types/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "starknet_state_sync_types" +version.workspace = true +edition.workspace = true +license.workspace = true +repository.workspace = true + +[lints] +workspace = true + +[dependencies] +async-trait.workspace = true +serde = { workspace = true, features = ["derive"] } +starknet_api.workspace = true +starknet_sequencer_infra.workspace = true +thiserror.workspace = true diff --git a/crates/starknet_state_sync_types/src/communication.rs b/crates/starknet_state_sync_types/src/communication.rs new file mode 100644 index 00000000000..daf12e66426 --- /dev/null +++ b/crates/starknet_state_sync_types/src/communication.rs @@ -0,0 +1,28 @@ +use async_trait::async_trait; +use starknet_api::block::BlockNumber; +use starknet_sequencer_infra::component_client::ClientError; +use thiserror::Error; + +use crate::errors::StateSyncError; +use crate::state_sync_types::SyncBlock; + +#[async_trait] +pub trait StateSyncClient: Send + Sync { + async fn get_block( + &self, + block_number: BlockNumber, + ) -> StateSyncClientResult>; + + // TODO: Add state reader methods for gateway. +} + +#[derive(Clone, Debug, Error)] +pub enum StateSyncClientError { + #[error(transparent)] + ClientError(#[from] ClientError), + #[error(transparent)] + StateSyncError(#[from] StateSyncError), +} +pub type StateSyncClientResult = Result; + +// TODO: Add client types and request/response enums diff --git a/crates/starknet_state_sync_types/src/errors.rs b/crates/starknet_state_sync_types/src/errors.rs new file mode 100644 index 00000000000..956fe539cb6 --- /dev/null +++ b/crates/starknet_state_sync_types/src/errors.rs @@ -0,0 +1,6 @@ +use serde::{Deserialize, Serialize}; +use thiserror::Error; + +// This error is defined even though it's empty to be compatible with the other components. +#[derive(Debug, Error, Serialize, Deserialize, Clone)] +pub enum StateSyncError {} diff --git a/crates/starknet_state_sync_types/src/lib.rs b/crates/starknet_state_sync_types/src/lib.rs new file mode 100644 index 00000000000..b88bd60954f --- /dev/null +++ b/crates/starknet_state_sync_types/src/lib.rs @@ -0,0 +1,3 @@ +pub mod communication; +pub mod errors; +pub mod state_sync_types; diff --git a/crates/starknet_state_sync_types/src/state_sync_types.rs b/crates/starknet_state_sync_types/src/state_sync_types.rs new file mode 100644 index 00000000000..8968526623d --- /dev/null +++ b/crates/starknet_state_sync_types/src/state_sync_types.rs @@ -0,0 +1,21 @@ +use std::collections::HashMap; + +use starknet_api::block::BlockHash; +use starknet_api::core::ClassHash; +use starknet_api::deprecated_contract_class::ContractClass as DeprecatedContractClass; +use starknet_api::state::{ContractClass, ThinStateDiff}; +use starknet_api::transaction::TransactionHash; + +use crate::errors::StateSyncError; + +pub type StateSyncResult = Result; + +pub struct SyncBlock { + pub block_hash: BlockHash, + pub parent_block_hash: BlockHash, + pub state_diff: ThinStateDiff, + pub classes: HashMap, + // TODO: Consider removing deprecated classes. + pub deprecated_classes: HashMap, + pub transaction_hashes: Vec, +}