Skip to content

Commit

Permalink
chore: encapsulate importer init
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-aranha-cw committed Aug 12, 2024
1 parent 70d8833 commit 2c8febf
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 27 deletions.
26 changes: 15 additions & 11 deletions src/eth/follower/importer/importer_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"])]
Expand All @@ -32,19 +35,20 @@ pub struct ImporterConfig {
}

impl ImporterConfig {
pub fn init(
&self,
executor: Arc<Executor>,
miner: Arc<Miner>,
storage: Arc<StratusStorage>,
chain: Arc<BlockchainClient>,
) -> anyhow::Result<Arc<Importer>> {
pub async fn init(&self, executor: Arc<Executor>, miner: Arc<Miner>, storage: Arc<StratusStorage>) -> anyhow::Result<Option<Arc<dyn Consensus>>> {
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<Executor>, miner: Arc<Miner>, storage: Arc<StratusStorage>) -> anyhow::Result<Option<Arc<dyn Consensus>>> {
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, {
Expand All @@ -56,6 +60,6 @@ impl ImporterConfig {
}
});

Ok(importer)
Ok(Some(importer))
}
}
20 changes: 4 additions & 16 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Arc<dyn Consensus>> = 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
Expand Down

0 comments on commit 2c8febf

Please sign in to comment.