Skip to content

Commit

Permalink
improve
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-aranha-cw committed Jul 30, 2024
1 parent 3b1709b commit 779be4d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 43 deletions.
61 changes: 20 additions & 41 deletions src/eth/importer/importer_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::time::Duration;
use clap::Parser;
use display_json::DebugAsJson;

use crate::config::StratusConfig;
use crate::eth::executor::Executor;
use crate::eth::importer::Importer;
use crate::eth::miner::Miner;
Expand Down Expand Up @@ -33,52 +32,32 @@ pub struct ImporterConfig {
}

impl ImporterConfig {
pub fn init_with_config(
config: &StratusConfig,
pub fn init(
&self,
executor: &Arc<Executor>,
miner: &Arc<Miner>,
storage: &Arc<StratusStorage>,
chain: Option<Arc<BlockchainClient>>,
) -> anyhow::Result<Option<Arc<Importer>>> {
if let Some(importer_config) = config.importer.as_ref() {
if let Some(chain) = chain {
return Ok(Some(importer_config.init(
Arc::clone(executor),
Arc::clone(miner),
Arc::clone(storage),
chain,
)?));
} else {
return Err(anyhow::anyhow!("chain is not initialized"));
}
}
Ok(None)
}

pub fn init(
&self,
executor: Arc<Executor>,
miner: Arc<Miner>,
storage: Arc<StratusStorage>,
chain: Arc<BlockchainClient>,
) -> anyhow::Result<Arc<Importer>> {
const TASK_NAME: &str = "importer::init";
tracing::info!(config = ?self, "creating importer");

let config = self.clone();

let importer = Importer::new(executor, miner, Arc::clone(&storage), chain, config.sync_interval);
let importer = Arc::new(importer);

spawn_named(TASK_NAME, {
let importer = Arc::clone(&importer);
async move {
if let Err(e) = importer.run_importer_online().await {
tracing::error!(reason = ?e, "importer-online failed");
if let Some(chain) = chain {
const TASK_NAME: &str = "importer::init";
tracing::info!(config = ?self, "creating importer");

let importer = Importer::new(Arc::clone(executor), Arc::clone(miner), Arc::clone(storage), chain, self.sync_interval);
let importer = Arc::new(importer);

spawn_named(TASK_NAME, {
let importer = Arc::clone(&importer);
async move {
if let Err(e) = importer.run_importer_online().await {
tracing::error!(reason = ?e, "importer-online failed");
}
}
}
});
});

Ok(importer)
Ok(Some(importer))
} else {
Err(anyhow::anyhow!("chain is not initialized"))
}
}
}
5 changes: 3 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::sync::Arc;
use stratus::config::StratusConfig;
use stratus::eth::consensus::simple_consensus::SimpleConsensus;
use stratus::eth::consensus::Consensus;
use stratus::eth::importer::ImporterConfig;
use stratus::eth::rpc::serve_rpc;
use stratus::infra::BlockchainClient;
use stratus::GlobalServices;
Expand All @@ -24,7 +23,9 @@ async fn run(config: StratusConfig) -> anyhow::Result<()> {

let consensus: Arc<dyn Consensus> = Arc::new(SimpleConsensus::new(Arc::clone(&storage), chain.clone()));

let _importer = ImporterConfig::init_with_config(&config, &executor, &miner, &storage, chain.clone())?;
if let Some(importer_config) = &config.importer {
importer_config.init(&executor, &miner, &storage, chain.clone())?;
}

// init rpc server
serve_rpc(
Expand Down

0 comments on commit 779be4d

Please sign in to comment.