Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: block-miner automine checks #851

Merged
merged 2 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 4 additions & 12 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,14 +246,7 @@ impl ExecutorConfig {
.expect("spawning evm threads should not fail");
}

let auto_mine_enabled = if miner.block_time.is_none() {
tracing::info!("enabling auto mining");
true
} else {
false
};

let executor = Executor::new(storage, miner, auto_mine_enabled, relayer, evm_tx, self.num_evms);
let executor = Executor::new(storage, miner, relayer, evm_tx, self.num_evms);
Arc::new(executor)
}
}
Expand All @@ -271,12 +264,11 @@ pub struct MinerConfig {
impl MinerConfig {
pub fn init(&self, storage: Arc<StratusStorage>) -> Arc<BlockMiner> {
tracing::info!(config = ?self, "starting block miner");
let miner = Arc::new(BlockMiner::new(storage, self.block_time));

if let Some(block_time) = self.block_time {
Arc::clone(&miner).spawn_interval_miner(block_time);
let miner = Arc::new(BlockMiner::new(storage, self.block_time));
if miner.is_interval_miner_mode() {
Arc::clone(&miner).spawn_interval_miner();
}

miner
}
}
Expand Down
18 changes: 17 additions & 1 deletion src/eth/block_miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,30 @@ impl BlockMiner {
}
}

pub fn spawn_interval_miner(self: Arc<Self>, block_time: Duration) {
/// Spawns a new thread that keep mining blocks in the specified interval.
pub fn spawn_interval_miner(self: Arc<Self>) {
let Some(block_time) = self.block_time else {
tracing::error!("cannot spawn interval miner because it does not have a block time defined");
return;
};

tracing::info!(block_time = %humantime::Duration::from(block_time), "spawning interval miner");

let t = thread::Builder::new().name("interval-miner".into());
t.spawn(move || interval_miner(self, block_time))
.expect("spawning interval miner should not fail");
}

/// Checks if miner should run in interval miner mode.
pub fn is_interval_miner_mode(&self) -> bool {
self.block_time.is_some()
}

/// Checks if miner should run in automine mode.
pub fn is_automine_mode(&self) -> bool {
not(self.is_interval_miner_mode())
}

/// Mines external block and external transactions.
///
/// Local transactions are not allowed to be part of the block.
Expand Down
11 changes: 6 additions & 5 deletions src/eth/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub struct Executor {
miner: Mutex<Arc<BlockMiner>>,

/// Bool indicating whether to enable auto mining or not.
auto_mine_enabled: bool,
automine: bool,

/// Provider for sending rpc calls to substrate
relayer: Option<Arc<TransactionRelayer>>,
Expand All @@ -65,17 +65,18 @@ impl Executor {
pub fn new(
storage: Arc<StratusStorage>,
miner: Arc<BlockMiner>,
auto_mine_enabled: bool,
relayer: Option<Arc<TransactionRelayer>>,
evm_tx: crossbeam_channel::Sender<EvmTask>,
num_evms: usize,
) -> Self {
tracing::info!(%num_evms, "starting executor");
let automine = miner.is_automine_mode();
tracing::info!(%num_evms, %automine, "starting executor");

Self {
evm_tx,
num_evms,
miner: Mutex::new(miner),
auto_mine_enabled,
automine,
storage,
relayer,
}
Expand Down Expand Up @@ -285,7 +286,7 @@ impl Executor {
}

// auto mine needed for e2e contract tests
if self.auto_mine_enabled {
if self.automine {
let miner = self.miner.lock().await;
miner.mine_local_and_commit().await?;
}
Expand Down
Loading