diff --git a/src/eth/follower/importer/importer_config.rs b/src/eth/follower/importer/importer_config.rs index b72229994..eaf5e458f 100644 --- a/src/eth/follower/importer/importer_config.rs +++ b/src/eth/follower/importer/importer_config.rs @@ -5,12 +5,15 @@ use clap::Parser; use display_json::DebugAsJson; use crate::eth::executor::Executor; +use crate::eth::follower::consensus::Consensus; use crate::eth::follower::importer::Importer; use crate::eth::miner::Miner; use crate::eth::storage::StratusStorage; use crate::ext::parse_duration; use crate::ext::spawn_named; use crate::infra::BlockchainClient; +use crate::GlobalState; +use crate::NodeMode; #[derive(Default, Parser, DebugAsJson, Clone, serde::Serialize)] #[group(requires_all = ["external_rpc"])] @@ -32,19 +35,20 @@ pub struct ImporterConfig { } impl ImporterConfig { - pub fn init( - &self, - executor: Arc, - miner: Arc, - storage: Arc, - chain: Arc, - ) -> anyhow::Result> { + pub async fn init(&self, executor: Arc, miner: Arc, storage: Arc) -> anyhow::Result>> { + match GlobalState::get_node_mode() { + NodeMode::Follower => self.init_follower(executor, miner, storage).await, + NodeMode::Leader => Ok(None), + } + } + + async fn init_follower(&self, executor: Arc, miner: Arc, storage: Arc) -> anyhow::Result>> { const TASK_NAME: &str = "importer::init"; - tracing::info!(config = ?self, "creating importer"); + tracing::info!(config = ?self, "creating importer for follower node"); - let config = self.clone(); + let chain = Arc::new(BlockchainClient::new_http_ws(&self.external_rpc, self.external_rpc_ws.as_deref(), self.external_rpc_timeout).await?); - let importer = Importer::new(executor, miner, Arc::clone(&storage), chain, config.sync_interval); + let importer = Importer::new(executor, Arc::clone(&miner), Arc::clone(&storage), Arc::clone(&chain), self.sync_interval); let importer = Arc::new(importer); spawn_named(TASK_NAME, { @@ -56,6 +60,6 @@ impl ImporterConfig { } }); - Ok(importer) + Ok(Some(importer)) } } diff --git a/src/main.rs b/src/main.rs index 619d094b3..c11f80421 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,7 @@ use std::sync::Arc; use stratus::config::StratusConfig; -use stratus::eth::follower::consensus::Consensus; use stratus::eth::rpc::serve_rpc; -use stratus::infra::BlockchainClient; use stratus::GlobalServices; use stratus::GlobalState; use stratus::NodeMode; @@ -29,20 +27,10 @@ async fn run(config: StratusConfig) -> anyhow::Result<()> { let executor = config.executor.init(Arc::clone(&storage), Arc::clone(&miner)); // Init importer - let consensus: Option> = match GlobalState::get_node_mode() { - NodeMode::Follower => { - let importer_config = config.importer.as_ref().ok_or(anyhow::anyhow!("importer config is not set"))?; - let chain = Arc::new( - BlockchainClient::new_http_ws( - importer_config.external_rpc.as_ref(), - importer_config.external_rpc_ws.as_deref(), - importer_config.external_rpc_timeout, - ) - .await?, - ); - Some(importer_config.init(Arc::clone(&executor), Arc::clone(&miner), Arc::clone(&storage), Arc::clone(&chain))?) - } - NodeMode::Leader => None, + let consensus = if let Some(importer_config) = &config.importer { + importer_config.init(Arc::clone(&executor), Arc::clone(&miner), Arc::clone(&storage)).await? + } else { + None }; // Init RPC server