Skip to content

Commit

Permalink
chore: make miner config more explicit and encapsulated
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-aranha-cw committed Aug 12, 2024
1 parent 545b11d commit ae176ed
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 15 deletions.
3 changes: 2 additions & 1 deletion src/bin/importer_offline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
23 changes: 14 additions & 9 deletions src/eth/miner/miner_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<StratusStorage>) -> anyhow::Result<Arc<Miner>> {
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<StratusStorage>) -> anyhow::Result<Arc<Miner>> {
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<StratusStorage>) -> anyhow::Result<Arc<Miner>> {
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<StratusStorage>) -> anyhow::Result<Arc<Miner>> {
tracing::info!(config = ?self, mode = ?mode, "creating block miner with specific mode");

// create miner
let miner = Miner::new(Arc::clone(&storage), mode);
Expand Down
5 changes: 1 addition & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
3 changes: 2 additions & 1 deletion tests/test_import_external_snapshot_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -151,7 +152,7 @@ pub async fn execute_test(

// init services
let storage = Arc::new(StratusStorage::new(Box::<InMemoryTemporaryStorage>::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
Expand Down

0 comments on commit ae176ed

Please sign in to comment.