diff --git a/Cargo.lock b/Cargo.lock index c23bd61159..1c0bf76295 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 53bfa5ade9..ada270fb5a 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 0000000000..28e72f1402 --- /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 0000000000..d65cd30dcf --- /dev/null +++ b/crates/starknet_state_sync_types/src/communication.rs @@ -0,0 +1,30 @@ +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 { + /// Request for a block at a specific height. + /// If the block doesn't exist, or if the sync didn't download it yet, returns None. + 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 0000000000..956fe539cb --- /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 0000000000..b88bd60954 --- /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 0000000000..dc44144cd0 --- /dev/null +++ b/crates/starknet_state_sync_types/src/state_sync_types.rs @@ -0,0 +1,22 @@ +use serde::{Deserialize, Serialize}; +use starknet_api::block::BlockHash; +use starknet_api::state::ThinStateDiff; +use starknet_api::transaction::TransactionHash; + +use crate::errors::StateSyncError; + +pub type StateSyncResult = Result; + +/// A block that came from the state sync. +/// Contains all the data needed to update the state of the system about this block. +/// +/// Blocks that came from the state sync are trusted. Therefore, SyncBlock doesn't contain data +/// needed for verifying the block +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct SyncBlock { + pub block_hash: BlockHash, + pub parent_block_hash: BlockHash, + pub state_diff: ThinStateDiff, + // TODO: decide if we want full classes here. + pub transaction_hashes: Vec, +}