diff --git a/src/eth/primitives/block.rs b/src/eth/primitives/block.rs index 7cf2c3c94..c2c126284 100644 --- a/src/eth/primitives/block.rs +++ b/src/eth/primitives/block.rs @@ -45,7 +45,7 @@ impl Block { Block::new(BlockNumber::ZERO, UnixTime::from(1702568764)) } - /// Pushes a single transaction execution to the blocks transactions + /// Pushes a single transaction execution to the blocks transactions. pub fn push_execution(&mut self, input: TransactionInput, evm_result: EvmExecutionResult) { let transaction_index = (self.transactions.len() as u64).into(); self.transactions.push(TransactionMined { diff --git a/src/eth/primitives/pending_block.rs b/src/eth/primitives/pending_block.rs index f15e76f57..a5585b228 100644 --- a/src/eth/primitives/pending_block.rs +++ b/src/eth/primitives/pending_block.rs @@ -1,8 +1,10 @@ use display_json::DebugAsJson; +use indexmap::IndexMap; use crate::eth::primitives::BlockNumber; use crate::eth::primitives::ExternalBlock; use crate::eth::primitives::ExternalTransactionExecution; +use crate::eth::primitives::Hash; use crate::eth::primitives::LocalTransactionExecution; use crate::eth::primitives::TransactionExecution; @@ -10,7 +12,7 @@ use crate::eth::primitives::TransactionExecution; #[derive(DebugAsJson, Clone, Default, serde::Serialize)] pub struct PendingBlock { pub number: BlockNumber, - pub tx_executions: Vec, + pub tx_executions: IndexMap, pub external_block: Option, } @@ -20,12 +22,17 @@ impl PendingBlock { Self { number, ..Default::default() } } - /// Split transactions executions in local and external executions. + /// Adds a transaction execution to the block. + pub fn push_transaction(&mut self, tx: TransactionExecution) { + self.tx_executions.insert(tx.hash(), tx); + } + + /// Splits transactions executions in local and external executions. pub fn split_transactions(&self) -> (Vec, Vec) { let mut local_txs = Vec::with_capacity(self.tx_executions.len()); let mut external_txs = Vec::with_capacity(self.tx_executions.len()); - for tx in self.tx_executions.clone() { + for tx in self.tx_executions.values().cloned() { match tx { TransactionExecution::Local(tx) => { local_txs.push(tx); diff --git a/src/eth/storage/inmemory/inmemory_permanent.rs b/src/eth/storage/inmemory/inmemory_permanent.rs index 15cf4b265..abc070513 100644 --- a/src/eth/storage/inmemory/inmemory_permanent.rs +++ b/src/eth/storage/inmemory/inmemory_permanent.rs @@ -199,7 +199,7 @@ impl PermanentStorage for InMemoryPermanentStorage { } } - fn read_mined_transaction(&self, hash: &Hash) -> anyhow::Result> { + fn read_transaction(&self, hash: &Hash) -> anyhow::Result> { let state_lock = self.lock_read(); match state_lock.transactions.get(hash) { diff --git a/src/eth/storage/inmemory/inmemory_temporary.rs b/src/eth/storage/inmemory/inmemory_temporary.rs index 84ffdf12e..f9c3e36d2 100644 --- a/src/eth/storage/inmemory/inmemory_temporary.rs +++ b/src/eth/storage/inmemory/inmemory_temporary.rs @@ -16,6 +16,7 @@ use crate::eth::primitives::EvmExecution; use crate::eth::primitives::ExecutionConflicts; use crate::eth::primitives::ExecutionConflictsBuilder; use crate::eth::primitives::ExternalBlock; +use crate::eth::primitives::Hash; use crate::eth::primitives::PendingBlock; use crate::eth::primitives::Slot; use crate::eth::primitives::SlotIndex; @@ -184,7 +185,7 @@ impl TemporaryStorage for InMemoryTemporaryStorage { } // save execution - states.head.require_active_block_mut()?.tx_executions.push(tx); + states.head.require_active_block_mut()?.push_transaction(tx); Ok(()) } @@ -206,6 +207,15 @@ impl TemporaryStorage for InMemoryTemporaryStorage { Ok(finished_block) } + fn read_transaction(&self, hash: &Hash) -> anyhow::Result> { + let states = self.lock_read(); + let Some(ref pending_block) = states.head.block else { return Ok(None) }; + match pending_block.tx_executions.get(hash) { + Some(tx) => Ok(Some(tx.clone())), + None => Ok(None), + } + } + // ------------------------------------------------------------------------- // Accounts and Slots // ------------------------------------------------------------------------- diff --git a/src/eth/storage/permanent_storage.rs b/src/eth/storage/permanent_storage.rs index ac619202c..9fa438fa0 100644 --- a/src/eth/storage/permanent_storage.rs +++ b/src/eth/storage/permanent_storage.rs @@ -35,7 +35,7 @@ pub trait PermanentStorage: Send + Sync + 'static { fn read_block(&self, block_selection: &BlockSelection) -> anyhow::Result>; /// Retrieves a transaction from the storage. - fn read_mined_transaction(&self, hash: &Hash) -> anyhow::Result>; + fn read_transaction(&self, hash: &Hash) -> anyhow::Result>; /// Retrieves logs from the storage. fn read_logs(&self, filter: &LogFilter) -> anyhow::Result>; diff --git a/src/eth/storage/rocks/rocks_permanent.rs b/src/eth/storage/rocks/rocks_permanent.rs index eac2d48df..c53411722 100644 --- a/src/eth/storage/rocks/rocks_permanent.rs +++ b/src/eth/storage/rocks/rocks_permanent.rs @@ -92,7 +92,7 @@ impl PermanentStorage for RocksPermanentStorage { Ok(self.state.read_block(selection)) } - fn read_mined_transaction(&self, hash: &Hash) -> anyhow::Result> { + fn read_transaction(&self, hash: &Hash) -> anyhow::Result> { self.state.read_transaction(hash) } diff --git a/src/eth/storage/stratus_storage.rs b/src/eth/storage/stratus_storage.rs index 3b6df8e85..2c8640a5c 100644 --- a/src/eth/storage/stratus_storage.rs +++ b/src/eth/storage/stratus_storage.rs @@ -351,7 +351,7 @@ impl StratusStorage { Span::with(|s| s.rec_str("hash", hash)); tracing::debug!(storage = %label::PERM, %hash, "reading transaction"); - timed(|| self.perm.read_mined_transaction(hash)).with(|m| { + timed(|| self.perm.read_transaction(hash)).with(|m| { metrics::inc_storage_read_mined_transaction(m.elapsed, label::PERM, m.result.is_ok()); }) } diff --git a/src/eth/storage/temporary_storage.rs b/src/eth/storage/temporary_storage.rs index e86d4af9a..c26ded5a0 100644 --- a/src/eth/storage/temporary_storage.rs +++ b/src/eth/storage/temporary_storage.rs @@ -4,6 +4,7 @@ use crate::eth::primitives::BlockNumber; use crate::eth::primitives::EvmExecution; use crate::eth::primitives::ExecutionConflicts; use crate::eth::primitives::ExternalBlock; +use crate::eth::primitives::Hash; use crate::eth::primitives::PendingBlock; use crate::eth::primitives::Slot; use crate::eth::primitives::SlotIndex; @@ -34,6 +35,9 @@ pub trait TemporaryStorage: Send + Sync + 'static { /// Finishes the mining of the active block and starts a new block. fn finish_block(&self) -> anyhow::Result; + /// Retrieves a transaction from the storage. + fn read_transaction(&self, hash: &Hash) -> anyhow::Result>; + // ------------------------------------------------------------------------- // Accounts and slots // -------------------------------------------------------------------------