diff --git a/crates/mempool_infra/src/component_server/definitions.rs b/crates/mempool_infra/src/component_server/definitions.rs index 4946205d7..2ac744569 100644 --- a/crates/mempool_infra/src/component_server/definitions.rs +++ b/crates/mempool_infra/src/component_server/definitions.rs @@ -1,5 +1,6 @@ use std::any::type_name; +use thiserror::Error; use tokio::sync::mpsc::Receiver; use tracing::info; @@ -26,3 +27,13 @@ pub async fn request_response_loop( tx.send(res).await.expect("Response connection should be open."); } } + +#[derive(Clone, Debug, Error)] +pub enum ReplaceComponentError { + #[error("Internal error.")] + InternalError, +} + +pub trait ComponentReplacer { + fn replace(&mut self, component: Component) -> Result<(), ReplaceComponentError>; +} diff --git a/crates/mempool_infra/src/component_server/empty_component_server.rs b/crates/mempool_infra/src/component_server/empty_component_server.rs index 663d0122b..e1651575e 100644 --- a/crates/mempool_infra/src/component_server/empty_component_server.rs +++ b/crates/mempool_infra/src/component_server/empty_component_server.rs @@ -3,6 +3,7 @@ use std::any::type_name; use async_trait::async_trait; use tracing::info; +use crate::component_server::{ComponentReplacer, ReplaceComponentError}; use crate::errors::{ComponentError, ComponentServerError}; use crate::starters::Startable; @@ -29,3 +30,10 @@ impl + Send + Sync> Startable(component: Component) -> EmptyServer { EmptyServer::new(component) } + +impl ComponentReplacer for EmptyServer { + fn replace(&mut self, component: Component) -> Result<(), ReplaceComponentError> { + self.component = component; + Ok(()) + } +} diff --git a/crates/mempool_infra/src/component_server/local_component_server.rs b/crates/mempool_infra/src/component_server/local_component_server.rs index 05c9d7cd1..5be38eaff 100644 --- a/crates/mempool_infra/src/component_server/local_component_server.rs +++ b/crates/mempool_infra/src/component_server/local_component_server.rs @@ -6,6 +6,7 @@ use tracing::error; use super::definitions::request_response_loop; use crate::component_definitions::{ComponentRequestAndResponseSender, ComponentRequestHandler}; +use crate::component_server::{ComponentReplacer, ReplaceComponentError}; use crate::errors::{ComponentError, ComponentServerError}; use crate::starters::Startable; @@ -181,3 +182,16 @@ where Self { component, rx, _local_server_type: PhantomData } } } + +impl ComponentReplacer + for BaseLocalComponentServer +where + Component: ComponentRequestHandler, + Request: Send + Sync, + Response: Send + Sync, +{ + fn replace(&mut self, component: Component) -> Result<(), ReplaceComponentError> { + self.component = component; + Ok(()) + } +}