diff --git a/crates/mempool/src/communication.rs b/crates/mempool/src/communication.rs index 166f99a74..e25ee7975 100644 --- a/crates/mempool/src/communication.rs +++ b/crates/mempool/src/communication.rs @@ -1,5 +1,6 @@ use async_trait::async_trait; use starknet_mempool_infra::component_definitions::ComponentRequestHandler; +use starknet_mempool_infra::component_runner::ComponentRunner; use starknet_mempool_infra::component_server::ComponentServer; use starknet_mempool_types::communication::{ MempoolRequest, MempoolRequestAndResponseSender, MempoolResponse, @@ -52,3 +53,6 @@ impl ComponentRequestHandler for MempoolCommuni } } } + +#[async_trait] +impl ComponentRunner for MempoolCommunicationWrapper {} diff --git a/crates/mempool_infra/src/component_runner.rs b/crates/mempool_infra/src/component_runner.rs index 7785bcb23..82f568522 100644 --- a/crates/mempool_infra/src/component_runner.rs +++ b/crates/mempool_infra/src/component_runner.rs @@ -11,6 +11,8 @@ pub enum ComponentStartError { /// Interface to start components. #[async_trait] pub trait ComponentRunner { - /// Start the component. Normally this function should never return. - async fn start(&mut self) -> Result<(), ComponentStartError>; + /// Start the component. By default do nothing. + async fn start(&mut self) -> Result<(), ComponentStartError> { + Ok(()) + } } diff --git a/crates/mempool_infra/src/component_server.rs b/crates/mempool_infra/src/component_server.rs index 17cb17bd0..a2945e417 100644 --- a/crates/mempool_infra/src/component_server.rs +++ b/crates/mempool_infra/src/component_server.rs @@ -21,7 +21,7 @@ use crate::component_runner::ComponentRunner; pub struct ComponentServer where - Component: ComponentRequestHandler, + Component: ComponentRequestHandler + ComponentRunner, Request: Send + Sync, Response: Send + Sync, { @@ -31,7 +31,7 @@ where impl ComponentServer where - Component: ComponentRequestHandler, + Component: ComponentRequestHandler + ComponentRunner, Request: Send + Sync, Response: Send + Sync, { @@ -52,11 +52,19 @@ pub trait ComponentServerStarter: Send + Sync { impl ComponentServerStarter for ComponentServer where - Component: ComponentRequestHandler + Send + Sync, + Component: ComponentRequestHandler + ComponentRunner + Send + Sync, Request: Send + Sync, Response: Send + Sync, { async fn start(&mut self) { + match self.component.start().await { + Ok(_) => info!("ComponentServer::start() completed."), + Err(err) => { + error!("ComponentServer::start() failed: {:?}", err); + return; + } + } + while let Some(request_and_res_tx) = self.rx.recv().await { let request = request_and_res_tx.request; let tx = request_and_res_tx.tx; diff --git a/crates/mempool_infra/tests/common/mod.rs b/crates/mempool_infra/tests/common/mod.rs index 71781ca53..3a81fadd9 100644 --- a/crates/mempool_infra/tests/common/mod.rs +++ b/crates/mempool_infra/tests/common/mod.rs @@ -1,7 +1,7 @@ use async_trait::async_trait; use serde::{Deserialize, Serialize}; use starknet_mempool_infra::component_client::ClientResult; - +use starknet_mempool_infra::component_runner::ComponentRunner; pub(crate) type ValueA = u32; pub(crate) type ValueB = u8; @@ -55,6 +55,9 @@ impl ComponentA { } } +#[async_trait] +impl ComponentRunner for ComponentA {} + pub(crate) struct ComponentB { value: ValueB, _a: Box, @@ -69,3 +72,7 @@ impl ComponentB { self.value } } + +#[async_trait] +impl ComponentRunner for ComponentB {} +