Skip to content

Commit

Permalink
feat: BlockMiner::save_execution
Browse files Browse the repository at this point in the history
  • Loading branch information
dinhani-cw committed May 20, 2024
1 parent 499d503 commit 4b30850
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
15 changes: 15 additions & 0 deletions src/eth/block_miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::eth::primitives::Hash;
use crate::eth::primitives::Index;
use crate::eth::primitives::LocalTransactionExecution;
use crate::eth::primitives::LogMined;
use crate::eth::primitives::TransactionExecution;
use crate::eth::primitives::TransactionMined;
use crate::eth::storage::StratusStorage;
use crate::ext::not;
Expand All @@ -30,6 +31,9 @@ pub struct BlockMiner {
/// Time duration between blocks.
pub block_time: Option<Duration>,

/// Broadcasts pending transactions events.
pub notifier_pending_txs: broadcast::Sender<Hash>,

/// Broadcasts new mined blocks events.
pub notifier_blocks: broadcast::Sender<Block>,

Expand All @@ -44,6 +48,7 @@ impl BlockMiner {
Self {
storage,
block_time,
notifier_pending_txs: broadcast::channel(u16::MAX as usize).0,
notifier_blocks: broadcast::channel(u16::MAX as usize).0,
notifier_logs: broadcast::channel(u16::MAX as usize).0,
}
Expand Down Expand Up @@ -100,6 +105,16 @@ impl BlockMiner {
not(self.is_interval_miner_mode())
}

/// Persists a transaction execution.
pub async fn save_execution(&self, tx_execution: TransactionExecution) -> anyhow::Result<()> {
let tx_hash = tx_execution.hash();

self.storage.save_execution(tx_execution).await?;
let _ = self.notifier_pending_txs.send(tx_hash);

Ok(())
}

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

/// Mutex-wrapped miner for creating new blockchain blocks.
miner: Mutex<Arc<BlockMiner>>,
miner: Arc<BlockMiner>,

/// Bool indicating whether to enable auto mining or not.
automine: bool,
Expand Down Expand Up @@ -81,7 +81,7 @@ impl Executor {
Self {
evm_tx,
num_evms,
miner: Mutex::new(miner),
miner,
automine,
storage,
relayer,
Expand Down Expand Up @@ -154,7 +154,7 @@ impl Executor {
}

// persist state
storage.save_execution(TransactionExecution::External(tx)).await?;
self.miner.save_execution(TransactionExecution::External(tx)).await?;
}

// track block metrics
Expand Down Expand Up @@ -282,7 +282,7 @@ impl Executor {

// save execution to temporary storage (not working yet)
let tx_execution = TransactionExecution::new_local(tx_input.clone(), evm_result.clone());
if let Err(e) = self.storage.save_execution(tx_execution.clone()).await {
if let Err(e) = self.miner.save_execution(tx_execution.clone()).await {
if let Some(StorageError::Conflict(conflicts)) = e.downcast_ref::<StorageError>() {
tracing::warn!(?conflicts, "temporary storage conflict detected when saving execution");
continue;
Expand All @@ -295,8 +295,7 @@ impl Executor {

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

break tx_execution;
Expand Down

0 comments on commit 4b30850

Please sign in to comment.