From 4b308503b7089aab39bf1fc2eb03228569ef1e2e Mon Sep 17 00:00:00 2001 From: Renato Dinhani Date: Mon, 20 May 2024 14:30:33 -0300 Subject: [PATCH] feat: BlockMiner::save_execution --- src/eth/block_miner.rs | 15 +++++++++++++++ src/eth/executor.rs | 11 +++++------ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/eth/block_miner.rs b/src/eth/block_miner.rs index 5b8a36474..8bfcadf2a 100644 --- a/src/eth/block_miner.rs +++ b/src/eth/block_miner.rs @@ -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; @@ -30,6 +31,9 @@ pub struct BlockMiner { /// Time duration between blocks. pub block_time: Option, + /// Broadcasts pending transactions events. + pub notifier_pending_txs: broadcast::Sender, + /// Broadcasts new mined blocks events. pub notifier_blocks: broadcast::Sender, @@ -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, } @@ -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. diff --git a/src/eth/executor.rs b/src/eth/executor.rs index 0cbf6e4dd..48f17a3ea 100644 --- a/src/eth/executor.rs +++ b/src/eth/executor.rs @@ -50,7 +50,7 @@ pub struct Executor { num_evms: usize, /// Mutex-wrapped miner for creating new blockchain blocks. - miner: Mutex>, + miner: Arc, /// Bool indicating whether to enable auto mining or not. automine: bool, @@ -81,7 +81,7 @@ impl Executor { Self { evm_tx, num_evms, - miner: Mutex::new(miner), + miner, automine, storage, relayer, @@ -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 @@ -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::() { tracing::warn!(?conflicts, "temporary storage conflict detected when saving execution"); continue; @@ -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;