Skip to content

Commit

Permalink
feat: mine_*_and_commit methods (#839)
Browse files Browse the repository at this point in the history
  • Loading branch information
dinhani-cw authored May 14, 2024
1 parent fd0fc23 commit 8eadf77
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 12 deletions.
3 changes: 1 addition & 2 deletions src/bin/importer_online.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ pub async fn run_importer_online(
executor.reexecute_external(&block, &receipts).await?;

// mine block
let mined_block = miner.mine_external_mixed().await?;
miner.commit(mined_block).await?;
miner.mine_external_mixed_and_commit().await?;

#[cfg(feature = "metrics")]
metrics::inc_n_importer_online_transactions_total(receipts.len() as u64);
Expand Down
24 changes: 21 additions & 3 deletions src/eth/block_miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl BlockMiner {
}
}

/// Mines from external block and external transactions.
/// Mines external block and external transactions.
///
/// Local transactions are not allowed to be part of the block.
pub async fn mine_external(&self) -> anyhow::Result<Block> {
Expand All @@ -60,7 +60,13 @@ impl BlockMiner {
block_from_external(external_block, mined_txs)
}

/// Mines from external block and external transactions.
/// Same as [`Self::mine_external`], but automatically commits the block instead of returning it.
pub async fn mine_external_and_commit(&self) -> anyhow::Result<()> {
let block = self.mine_external().await?;
self.commit(block).await
}

/// Mines external block and external transactions.
///
/// Local transactions are allowed to be part of the block if failed, but not succesful ones.
pub async fn mine_external_mixed(&self) -> anyhow::Result<Block> {
Expand All @@ -87,7 +93,13 @@ impl BlockMiner {
Ok(block)
}

/// Mines from local transactions.
/// Same as [`Self::mine_external_mixed`], but automatically commits the block instead of returning it.
pub async fn mine_external_mixed_and_commit(&self) -> anyhow::Result<()> {
let block = self.mine_external_mixed().await?;
self.commit(block).await
}

/// Mines local transactions.
///
/// External transactions are not allowed to be part of the block.
pub async fn mine_local(&self) -> anyhow::Result<Block> {
Expand All @@ -106,6 +118,12 @@ impl BlockMiner {
}
}

/// Same as [`Self::mine_local`], but automatically commits the block instead of returning it.
pub async fn mine_local_and_commit(&self) -> anyhow::Result<()> {
let block = self.mine_local().await?;
self.commit(block).await
}

/// Persists a mined block to permanent storage and prepares new block.
pub async fn commit(&self, block: Block) -> anyhow::Result<()> {
let block_number = *block.number();
Expand Down
3 changes: 1 addition & 2 deletions src/eth/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,7 @@ impl Executor {

// TODO: remove automine
let miner = self.miner.lock().await;
let block = miner.mine_local().await?;
miner.commit(block).await?;
miner.mine_local_and_commit().await?;

break tx_execution;
}
Expand Down
4 changes: 1 addition & 3 deletions src/eth/rpc/rpc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,7 @@ async fn debug_set_head(params: Params<'_>, ctx: Arc<RpcContext>) -> anyhow::Res

#[cfg(feature = "dev")]
async fn evm_mine(_params: Params<'_>, ctx: Arc<RpcContext>) -> anyhow::Result<JsonValue, RpcError> {
let block = ctx.miner.mine_local().await?;
ctx.miner.commit(block).await?;

ctx.miner.mine_local_and_commit().await?;
Ok(serde_json::to_value(true).unwrap())
}

Expand Down
3 changes: 1 addition & 2 deletions tests/test_import_external_snapshot_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,7 @@ pub async fn execute_test(

// execute and mine
executor.reexecute_external(&block, &receipts).await.unwrap();
let mined_block = miner.mine_external().await.unwrap();
miner.commit(mined_block).await.unwrap();
miner.mine_external_and_commit().await.unwrap();

// get metrics from prometheus (sleep to ensure prometheus collected)
tokio::time::sleep(Duration::from_secs(5)).await;
Expand Down

0 comments on commit 8eadf77

Please sign in to comment.