Skip to content

Commit

Permalink
feat(sync): add StateSyncClient and SyncBlock (#2007)
Browse files Browse the repository at this point in the history
  • Loading branch information
ShahakShama authored Nov 14, 2024
1 parent 25eeca0 commit bcb6338
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]
Expand Down
16 changes: 16 additions & 0 deletions crates/starknet_state_sync_types/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
30 changes: 30 additions & 0 deletions crates/starknet_state_sync_types/src/communication.rs
Original file line number Diff line number Diff line change
@@ -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<Option<SyncBlock>>;

// 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<T> = Result<T, StateSyncClientError>;

// TODO: Add client types and request/response enums
6 changes: 6 additions & 0 deletions crates/starknet_state_sync_types/src/errors.rs
Original file line number Diff line number Diff line change
@@ -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 {}
3 changes: 3 additions & 0 deletions crates/starknet_state_sync_types/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod communication;
pub mod errors;
pub mod state_sync_types;
19 changes: 19 additions & 0 deletions crates/starknet_state_sync_types/src/state_sync_types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use serde::{Deserialize, Serialize};
use starknet_api::state::ThinStateDiff;
use starknet_api::transaction::TransactionHash;

use crate::errors::StateSyncError;

pub type StateSyncResult<T> = Result<T, StateSyncError>;

/// 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 state_diff: ThinStateDiff,
// TODO: decide if we want block hash, parent block hash and full classes here.
pub transaction_hashes: Vec<TransactionHash>,
}

0 comments on commit bcb6338

Please sign in to comment.