From ae176ed5831d387fc4225af251900767a903bd14 Mon Sep 17 00:00:00 2001 From: gabriel-aranha-cw Date: Mon, 12 Aug 2024 13:48:46 -0300 Subject: [PATCH] chore: make miner config more explicit and encapsulated --- src/bin/importer_offline.rs | 3 ++- src/eth/miner/miner_config.rs | 23 +++++++++++-------- src/main.rs | 5 +--- tests/test_import_external_snapshot_common.rs | 3 ++- 4 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/bin/importer_offline.rs b/src/bin/importer_offline.rs index e5e99d3c6..f8b501a9a 100644 --- a/src/bin/importer_offline.rs +++ b/src/bin/importer_offline.rs @@ -19,6 +19,7 @@ use itertools::Itertools; use stratus::config::ImporterOfflineConfig; use stratus::eth::executor::Executor; use stratus::eth::miner::Miner; +use stratus::eth::miner::MinerMode; use stratus::eth::primitives::Block; use stratus::eth::primitives::BlockNumber; use stratus::eth::primitives::ExternalBlock; @@ -53,7 +54,7 @@ async fn run(config: ImporterOfflineConfig) -> anyhow::Result<()> { // init services let rpc_storage = config.rpc_storage.init().await?; let storage = config.storage.init()?; - let miner = config.miner.init_external_mode(Arc::clone(&storage))?; + let miner = config.miner.init_with_mode(MinerMode::External, Arc::clone(&storage))?; let executor = config.executor.init(Arc::clone(&storage), Arc::clone(&miner)); // init block snapshots to export diff --git a/src/eth/miner/miner_config.rs b/src/eth/miner/miner_config.rs index fbdaffc87..43655539d 100644 --- a/src/eth/miner/miner_config.rs +++ b/src/eth/miner/miner_config.rs @@ -8,6 +8,8 @@ use display_json::DebugAsJson; use crate::eth::miner::Miner; use crate::eth::storage::StratusStorage; use crate::ext::parse_duration; +use crate::GlobalState; +use crate::NodeMode; // ----------------------------------------------------------------------------- // Config @@ -21,18 +23,21 @@ pub struct MinerConfig { } impl MinerConfig { - /// Inits [`BlockMiner`] with external mining mode, ignoring the configured value. - pub fn init_external_mode(&self, storage: Arc) -> anyhow::Result> { - self.init_with_mode(MinerMode::External, storage) - } - - /// Inits [`BlockMiner`] with the configured mining mode. + /// Inits [`Miner`] with the appropriate miner mode based on the node mode. pub fn init(&self, storage: Arc) -> anyhow::Result> { - self.init_with_mode(self.block_mode, storage) + tracing::info!(config = ?self, "creating block miner"); + + let mode = match GlobalState::get_node_mode() { + NodeMode::Follower => MinerMode::External, + NodeMode::Leader => self.block_mode, + }; + + self.init_with_mode(mode, storage) } - fn init_with_mode(&self, mode: MinerMode, storage: Arc) -> anyhow::Result> { - tracing::info!(config = ?self, "creating block miner"); + /// Inits [`Miner`] with a specific miner mode, regardless of node mode. + pub fn init_with_mode(&self, mode: MinerMode, storage: Arc) -> anyhow::Result> { + tracing::info!(config = ?self, mode = ?mode, "creating block miner with specific mode"); // create miner let miner = Miner::new(Arc::clone(&storage), mode); diff --git a/src/main.rs b/src/main.rs index acdf1ca71..619d094b3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,10 +23,7 @@ async fn run(config: StratusConfig) -> anyhow::Result<()> { let storage = config.storage.init()?; // Init miner - let miner = match GlobalState::get_node_mode() { - NodeMode::Follower => config.miner.init_external_mode(Arc::clone(&storage))?, - NodeMode::Leader => config.miner.init(Arc::clone(&storage))?, - }; + let miner = config.miner.init(Arc::clone(&storage))?; // Init executor let executor = config.executor.init(Arc::clone(&storage), Arc::clone(&miner)); diff --git a/tests/test_import_external_snapshot_common.rs b/tests/test_import_external_snapshot_common.rs index 98b17cebd..0ac64290b 100644 --- a/tests/test_import_external_snapshot_common.rs +++ b/tests/test_import_external_snapshot_common.rs @@ -9,6 +9,7 @@ use fancy_duration::AsFancyDuration; use itertools::Itertools; use stratus::alias::JsonValue; use stratus::config::IntegrationTestConfig; +use stratus::eth::miner::MinerMode; use stratus::eth::primitives::Account; use stratus::eth::primitives::Address; use stratus::eth::primitives::ExternalBlock; @@ -151,7 +152,7 @@ pub async fn execute_test( // init services let storage = Arc::new(StratusStorage::new(Box::::default(), Box::new(perm_storage))); - let miner = config.miner.init_external_mode(Arc::clone(&storage)).unwrap(); + let miner = config.miner.init_with_mode(MinerMode::External, Arc::clone(&storage)).unwrap(); let executor = config.executor.init(Arc::clone(&storage), Arc::clone(&miner)); // execute and mine