Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(starknet_state_sync): pass new internal blocks from state sync to p2p sync client #2631

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions crates/papyrus_node/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::process::exit;
use std::sync::Arc;
use std::time::Duration;

use futures::StreamExt;
use papyrus_base_layer::ethereum_base_layer_contract::EthereumBaseLayerConfig;
use papyrus_common::metrics::COLLECT_PROFILING_METRICS;
use papyrus_common::pending_classes::PendingClasses;
Expand Down Expand Up @@ -304,6 +305,7 @@ async fn spawn_sync_client(
storage_reader,
storage_writer,
p2p_sync_client_channels,
futures::stream::pending().boxed(),
);
tokio::spawn(async move { Ok(p2p_sync.run().await?) })
}
Expand Down
1 change: 1 addition & 0 deletions crates/papyrus_p2p_sync/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ papyrus_storage.workspace = true
rand.workspace = true
serde.workspace = true
starknet_api.workspace = true
starknet_state_sync_types.workspace = true
starknet-types-core.workspace = true
thiserror.workspace = true
tokio.workspace = true
Expand Down
8 changes: 7 additions & 1 deletion crates/papyrus_p2p_sync/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use std::time::Duration;

use class::ClassStreamBuilder;
use futures::channel::mpsc::SendError;
use futures::stream::BoxStream;
use futures::Stream;
use header::HeaderStreamBuilder;
use papyrus_common::pending_classes::ApiContractClass;
Expand All @@ -40,11 +41,13 @@ use serde::{Deserialize, Serialize};
use starknet_api::block::BlockNumber;
use starknet_api::core::ClassHash;
use starknet_api::transaction::FullTransaction;
use starknet_state_sync_types::state_sync_types::SyncBlock;
use state_diff::StateDiffStreamBuilder;
use stream_builder::{DataStreamBuilder, DataStreamResult};
use tokio_stream::StreamExt;
use tracing::instrument;
use transaction::TransactionStreamFactory;

const STEP: u64 = 1;
const ALLOWED_SIGNATURES_LENGTH: usize = 1;

Expand Down Expand Up @@ -226,6 +229,8 @@ pub struct P2PSyncClient {
storage_reader: StorageReader,
storage_writer: StorageWriter,
p2p_sync_channels: P2PSyncClientChannels,
#[allow(dead_code)]
internal_blocks_receiver: BoxStream<'static, (BlockNumber, SyncBlock)>,
}

impl P2PSyncClient {
Expand All @@ -234,8 +239,9 @@ impl P2PSyncClient {
storage_reader: StorageReader,
storage_writer: StorageWriter,
p2p_sync_channels: P2PSyncClientChannels,
internal_blocks_receiver: BoxStream<'static, (BlockNumber, SyncBlock)>,
) -> Self {
Self { config, storage_reader, storage_writer, p2p_sync_channels }
Self { config, storage_reader, storage_writer, p2p_sync_channels, internal_blocks_receiver }
}

#[instrument(skip(self), level = "debug", err)]
Expand Down
2 changes: 2 additions & 0 deletions crates/papyrus_p2p_sync/src/client/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ pub fn setup() -> TestArgs {
storage_reader.clone(),
storage_writer,
p2p_sync_channels,
futures::stream::pending().boxed(),
);
TestArgs {
p2p_sync,
Expand Down Expand Up @@ -194,6 +195,7 @@ pub async fn run_test(max_query_lengths: HashMap<DataType, u64>, actions: Vec<Ac
storage_reader.clone(),
storage_writer,
p2p_sync_channels,
futures::stream::pending().boxed(),
);

let mut headers_current_query_responses_manager = None;
Expand Down
20 changes: 16 additions & 4 deletions crates/starknet_state_sync/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ pub mod config;
pub mod runner;

use async_trait::async_trait;
use futures::channel::mpsc::{channel, Sender};
use futures::SinkExt;
use papyrus_storage::body::BodyStorageReader;
use papyrus_storage::state::StateStorageReader;
use papyrus_storage::StorageReader;
Expand All @@ -12,18 +14,23 @@ use starknet_state_sync_types::communication::{
StateSyncResponse,
StateSyncResult,
};
use starknet_state_sync_types::errors::StateSyncError;
use starknet_state_sync_types::state_sync_types::SyncBlock;

use crate::config::StateSyncConfig;
use crate::runner::StateSyncRunner;

const BUFFER_SIZE: usize = 100000;

pub fn create_state_sync_and_runner(config: StateSyncConfig) -> (StateSync, StateSyncRunner) {
let (state_sync_runner, storage_reader) = StateSyncRunner::new(config);
(StateSync { storage_reader }, state_sync_runner)
let (new_block_sender, new_block_receiver) = channel(BUFFER_SIZE);
let (state_sync_runner, storage_reader) = StateSyncRunner::new(config, new_block_receiver);
(StateSync { storage_reader, new_block_sender }, state_sync_runner)
}

pub struct StateSync {
storage_reader: StorageReader,
new_block_sender: Sender<(BlockNumber, SyncBlock)>,
}

// TODO(shahak): Have StateSyncRunner call StateSync instead of the opposite once we stop supporting
Expand All @@ -35,8 +42,13 @@ impl ComponentRequestHandler<StateSyncRequest, StateSyncResponse> for StateSync
StateSyncRequest::GetBlock(block_number) => {
StateSyncResponse::GetBlock(self.get_block(block_number))
}
StateSyncRequest::AddNewBlock(_block_number, _sync_block) => {
todo!()
StateSyncRequest::AddNewBlock(block_number, sync_block) => {
StateSyncResponse::AddNewBlock(
self.new_block_sender
.send((block_number, sync_block))
.await
.map_err(StateSyncError::from),
)
}
}
}
Expand Down
11 changes: 9 additions & 2 deletions crates/starknet_state_sync/src/runner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@
mod test;

use async_trait::async_trait;
use futures::channel::mpsc::Receiver;
use futures::future::BoxFuture;
use futures::FutureExt;
use futures::{FutureExt, StreamExt};
use papyrus_network::network_manager::{self, NetworkError};
use papyrus_p2p_sync::client::{P2PSyncClient, P2PSyncClientChannels, P2PSyncClientError};
use papyrus_p2p_sync::server::{P2PSyncServer, P2PSyncServerChannels};
use papyrus_p2p_sync::{Protocol, BUFFER_SIZE};
use papyrus_storage::{open_storage, StorageReader};
use starknet_api::block::BlockNumber;
use starknet_sequencer_infra::component_definitions::ComponentStarter;
use starknet_sequencer_infra::errors::ComponentError;
use starknet_state_sync_types::state_sync_types::SyncBlock;

use crate::config::StateSyncConfig;

Expand All @@ -37,7 +40,10 @@ impl ComponentStarter for StateSyncRunner {
}

impl StateSyncRunner {
pub fn new(config: StateSyncConfig) -> (Self, StorageReader) {
pub fn new(
config: StateSyncConfig,
new_block_receiver: Receiver<(BlockNumber, SyncBlock)>,
) -> (Self, StorageReader) {
let (storage_reader, storage_writer) =
open_storage(config.storage_config).expect("StateSyncRunner failed opening storage");

Expand Down Expand Up @@ -65,6 +71,7 @@ impl StateSyncRunner {
storage_reader.clone(),
storage_writer,
p2p_sync_client_channels,
new_block_receiver.boxed(),
);

let header_server_receiver = network_manager
Expand Down
1 change: 1 addition & 0 deletions crates/starknet_state_sync_types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ workspace = true

[dependencies]
async-trait.workspace = true
futures.workspace = true
papyrus_proc_macros.workspace = true
papyrus_storage.workspace = true
serde = { workspace = true, features = ["derive"] }
Expand Down
9 changes: 9 additions & 0 deletions crates/starknet_state_sync_types/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use futures::channel::mpsc::SendError;
use papyrus_storage::StorageError;
use serde::{Deserialize, Serialize};
use thiserror::Error;
Expand All @@ -10,10 +11,18 @@ pub enum StateSyncError {
// We put the string of the error instead.
#[error("Unexpected storage error: {0}")]
StorageError(String),
#[error("Error while sending SyncBlock from StateSync to P2pSyncClient")]
SendError(String),
}

impl From<StorageError> for StateSyncError {
fn from(error: StorageError) -> Self {
StateSyncError::StorageError(error.to_string())
}
}

impl From<SendError> for StateSyncError {
fn from(error: SendError) -> Self {
StateSyncError::SendError(error.to_string())
}
}
Loading