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

feat: read_transaction in InMemoryTemporaryStorage #1239

Merged
merged 1 commit into from
Jun 25, 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
2 changes: 1 addition & 1 deletion src/eth/primitives/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 10 additions & 3 deletions src/eth/primitives/pending_block.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
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;

/// Block that is being mined and receiving updates.
#[derive(DebugAsJson, Clone, Default, serde::Serialize)]
pub struct PendingBlock {
pub number: BlockNumber,
pub tx_executions: Vec<TransactionExecution>,
pub tx_executions: IndexMap<Hash, TransactionExecution>,
pub external_block: Option<ExternalBlock>,
}

Expand All @@ -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<LocalTransactionExecution>, Vec<ExternalTransactionExecution>) {
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);
Expand Down
2 changes: 1 addition & 1 deletion src/eth/storage/inmemory/inmemory_permanent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ impl PermanentStorage for InMemoryPermanentStorage {
}
}

fn read_mined_transaction(&self, hash: &Hash) -> anyhow::Result<Option<TransactionMined>> {
fn read_transaction(&self, hash: &Hash) -> anyhow::Result<Option<TransactionMined>> {
let state_lock = self.lock_read();

match state_lock.transactions.get(hash) {
Expand Down
12 changes: 11 additions & 1 deletion src/eth/storage/inmemory/inmemory_temporary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(())
}
Expand All @@ -206,6 +207,15 @@ impl TemporaryStorage for InMemoryTemporaryStorage {
Ok(finished_block)
}

fn read_transaction(&self, hash: &Hash) -> anyhow::Result<Option<TransactionExecution>> {
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
// -------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion src/eth/storage/permanent_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub trait PermanentStorage: Send + Sync + 'static {
fn read_block(&self, block_selection: &BlockSelection) -> anyhow::Result<Option<Block>>;

/// Retrieves a transaction from the storage.
fn read_mined_transaction(&self, hash: &Hash) -> anyhow::Result<Option<TransactionMined>>;
fn read_transaction(&self, hash: &Hash) -> anyhow::Result<Option<TransactionMined>>;

/// Retrieves logs from the storage.
fn read_logs(&self, filter: &LogFilter) -> anyhow::Result<Vec<LogMined>>;
Expand Down
2 changes: 1 addition & 1 deletion src/eth/storage/rocks/rocks_permanent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl PermanentStorage for RocksPermanentStorage {
Ok(self.state.read_block(selection))
}

fn read_mined_transaction(&self, hash: &Hash) -> anyhow::Result<Option<TransactionMined>> {
fn read_transaction(&self, hash: &Hash) -> anyhow::Result<Option<TransactionMined>> {
self.state.read_transaction(hash)
}

Expand Down
2 changes: 1 addition & 1 deletion src/eth/storage/stratus_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
})
}
Expand Down
4 changes: 4 additions & 0 deletions src/eth/storage/temporary_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<PendingBlock>;

/// Retrieves a transaction from the storage.
fn read_transaction(&self, hash: &Hash) -> anyhow::Result<Option<TransactionExecution>>;

// -------------------------------------------------------------------------
// Accounts and slots
// -------------------------------------------------------------------------
Expand Down