Skip to content

Commit

Permalink
refactor(node): move building the p2p sync server to its own function
Browse files Browse the repository at this point in the history
  • Loading branch information
matan-starkware committed Sep 18, 2024
1 parent bbca06f commit 875d08a
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions crates/papyrus_node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,17 @@ async fn spawn_sync_client(
}
}

fn spawn_p2p_sync_server(
maybe_sync_server_channels: Option<P2PSyncServerChannels>,
storage_reader: StorageReader,
) -> JoinHandle<anyhow::Result<()>> {
let Some(p2p_sync_server_channels) = maybe_sync_server_channels else {
return tokio::spawn(pending());
};
let p2p_sync_server = P2PSyncServer::new(storage_reader.clone(), p2p_sync_server_channels);
tokio::spawn(async move { Ok(p2p_sync_server.run().await) })
}

async fn run_threads(config: NodeConfig) -> anyhow::Result<()> {
let (storage_reader, storage_writer) = open_storage(config.storage.clone())?;

Expand Down Expand Up @@ -289,15 +300,8 @@ async fn run_threads(config: NodeConfig) -> anyhow::Result<()> {
.await?;

// P2P Sync Server task.
let p2p_sync_server_future = match maybe_sync_server_channels {
Some(p2p_sync_server_channels) => {
let p2p_sync_server =
P2PSyncServer::new(storage_reader.clone(), p2p_sync_server_channels);
p2p_sync_server.run().boxed()
}
None => pending().boxed(),
};
let p2p_sync_server_handle = tokio::spawn(p2p_sync_server_future);
let p2p_sync_server_handle =
spawn_p2p_sync_server(maybe_sync_server_channels, storage_reader.clone());

// Sync task.
let sync_client_handle = spawn_sync_client(
Expand Down Expand Up @@ -330,7 +334,7 @@ async fn run_threads(config: NodeConfig) -> anyhow::Result<()> {
}
res = p2p_sync_server_handle => {
error!("P2P Sync server stopped");
res?
res??
}
res = network_handle => {
error!("Network stopped.");
Expand Down

0 comments on commit 875d08a

Please sign in to comment.