-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sync): add StateSync that sends requests to StateSyncRunner (#2072)
- Loading branch information
1 parent
24f5801
commit e97d2d4
Showing
6 changed files
with
86 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
[package] | ||
name = "starknet_state_sync" | ||
version.workspace = true | ||
edition.workspace = true | ||
license.workspace = true | ||
repository.workspace = true | ||
|
||
[lints] | ||
workspace = true | ||
|
||
[dependencies] | ||
async-trait.workspace = true | ||
futures.workspace = true | ||
starknet_sequencer_infra.workspace = true | ||
starknet_state_sync_types.workspace = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
pub mod runner; | ||
|
||
use async_trait::async_trait; | ||
use futures::channel::{mpsc, oneshot}; | ||
use futures::SinkExt; | ||
use starknet_sequencer_infra::component_definitions::ComponentRequestHandler; | ||
use starknet_state_sync_types::communication::{StateSyncRequest, StateSyncResponse}; | ||
use starknet_state_sync_types::errors::StateSyncError; | ||
|
||
use crate::runner::StateSyncRunner; | ||
|
||
// TODO(shahak): consider adding to config | ||
const BUFFER_SIZE: usize = 100000; | ||
|
||
pub fn create_state_sync_and_runner() -> (StateSync, StateSyncRunner) { | ||
let (request_sender, request_receiver) = mpsc::channel(BUFFER_SIZE); | ||
(StateSync { request_sender }, StateSyncRunner { request_receiver }) | ||
} | ||
|
||
pub struct StateSync { | ||
pub request_sender: mpsc::Sender<(StateSyncRequest, oneshot::Sender<StateSyncResponse>)>, | ||
} | ||
|
||
// TODO(shahak): Have StateSyncRunner call StateSync instead of the opposite once we stop supporting | ||
// papyrus executable and can move the storage into StateSync. | ||
#[async_trait] | ||
impl ComponentRequestHandler<StateSyncRequest, StateSyncResponse> for StateSync { | ||
async fn handle_request(&mut self, request: StateSyncRequest) -> StateSyncResponse { | ||
let (response_sender, response_receiver) = oneshot::channel(); | ||
if self.request_sender.send((request, response_sender)).await.is_err() { | ||
return StateSyncResponse::GetBlock(Err(StateSyncError::RunnerCommunicationError)); | ||
} | ||
response_receiver.await.unwrap_or_else(|_| { | ||
StateSyncResponse::GetBlock(Err(StateSyncError::RunnerCommunicationError)) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
use async_trait::async_trait; | ||
use futures::channel::{mpsc, oneshot}; | ||
use starknet_sequencer_infra::component_definitions::ComponentStarter; | ||
use starknet_sequencer_infra::errors::ComponentError; | ||
use starknet_state_sync_types::communication::{StateSyncRequest, StateSyncResponse}; | ||
|
||
pub struct StateSyncRunner { | ||
pub request_receiver: mpsc::Receiver<(StateSyncRequest, oneshot::Sender<StateSyncResponse>)>, | ||
} | ||
|
||
#[async_trait] | ||
impl ComponentStarter for StateSyncRunner { | ||
async fn start(&mut self) -> Result<(), ComponentError> { | ||
unimplemented!() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
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 {} | ||
pub enum StateSyncError { | ||
#[error("Communication error between StateSync and StateSyncRunner")] | ||
RunnerCommunicationError, | ||
} |