Skip to content
This repository has been archived by the owner on Nov 5, 2023. It is now read-only.

Commit

Permalink
move optimism tx meta to new file
Browse files Browse the repository at this point in the history
  • Loading branch information
refcell committed Nov 2, 2023
1 parent 23e9440 commit 3190b1f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 46 deletions.
45 changes: 4 additions & 41 deletions crates/rpc/rpc/src/eth/api/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,43 +16,6 @@ use reth_rpc_types::{Index, RichBlock, TransactionReceipt};
use reth_rpc_types_compat::block::{from_block, uncle_block_from_header};
use reth_transaction_pool::TransactionPool;

#[cfg(feature = "optimism")]
use reth_primitives::U256;
#[cfg(feature = "optimism")]
use revm::L1BlockInfo;
#[cfg(feature = "optimism")]
use std::rc::Rc;

/// Optimism Transaction Metadata
///
/// Includes the L1 fee and data gas for the tx along with the L1
/// block info. In order to pass the [OptimismTxMeta] into the
/// async colored `build_transaction_receipt_with_block_receipts`
/// function, a reference counter for the L1 block info is
/// used so the L1 block info can be shared between receipts.
#[cfg(feature = "optimism")]
#[derive(Debug, Default, Clone)]
pub(crate) struct OptimismTxMeta {
/// The L1 block info.
pub(crate) l1_block_info: Option<Rc<L1BlockInfo>>,
/// The L1 fee for the block.
pub(crate) l1_fee: Option<U256>,
/// The L1 data gas for the block.
pub(crate) l1_data_gas: Option<U256>,
}

#[cfg(feature = "optimism")]
impl OptimismTxMeta {
/// Creates a new [OptimismTxMeta].
pub(crate) fn new(
l1_block_info: Option<Rc<L1BlockInfo>>,
l1_fee: Option<U256>,
l1_data_gas: Option<U256>,
) -> Self {
Self { l1_block_info, l1_fee, l1_data_gas }
}
}

impl<Provider, Pool, Network> EthApi<Provider, Pool, Network>
where
Provider:
Expand Down Expand Up @@ -150,7 +113,7 @@ where
)
})
.collect::<EthResult<Vec<_>>>();
return receipts.map(Some)
return receipts.map(Some);
}

Ok(None)
Expand All @@ -167,7 +130,7 @@ where

if block_id.is_pending() {
// Pending block can be fetched directly without need for caching
return Ok(self.provider().pending_block()?.map(|block| block.body.len()))
return Ok(self.provider().pending_block()?.map(|block| block.body.len()));
}

let block_hash = match self.provider().block_hash_for_id(block_id)? {
Expand All @@ -189,10 +152,10 @@ where
// Pending block can be fetched directly without need for caching
let maybe_pending = self.provider().pending_block()?;
return if maybe_pending.is_some() {
return Ok(maybe_pending)
return Ok(maybe_pending);
} else {
self.local_pending_block().await
}
};
}

let block_hash = match self.provider().block_hash_for_id(block_id)? {
Expand Down
10 changes: 6 additions & 4 deletions crates/rpc/rpc/src/eth/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ use tokio::sync::{oneshot, Mutex};
mod block;
mod call;
mod fees;
#[cfg(feature = "optimism")]
mod optimism;
mod pending_block;
mod server;
mod sign;
Expand Down Expand Up @@ -280,7 +282,7 @@ where
pub(crate) async fn local_pending_block(&self) -> EthResult<Option<SealedBlock>> {
let pending = self.pending_block_env_and_cfg()?;
if pending.origin.is_actual_pending() {
return Ok(pending.origin.into_actual_pending())
return Ok(pending.origin.into_actual_pending());
}

// no pending block from the CL yet, so we need to build it ourselves via txpool
Expand All @@ -295,21 +297,21 @@ where
pending.origin.header().hash == pending_block.block.parent_hash &&
now <= pending_block.expires_at
{
return Ok(Some(pending_block.block.clone()))
return Ok(Some(pending_block.block.clone()));
}
}

// if we're currently syncing, we're unable to build a pending block
if this.network().is_syncing() {
return Ok(None)
return Ok(None);
}

// we rebuild the block
let pending_block = match pending.build_block(this.provider(), this.pool()) {
Ok(block) => block,
Err(err) => {
tracing::debug!(target: "rpc", "Failed to build pending block: {:?}", err);
return Ok(None)
return Ok(None);
}
};

Expand Down
31 changes: 31 additions & 0 deletions crates/rpc/rpc/src/eth/api/optimism.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use reth_primitives::U256;
use revm::L1BlockInfo;
use std::rc::Rc;

/// Optimism Transaction Metadata
///
/// Includes the L1 fee and data gas for the tx along with the L1
/// block info. In order to pass the [OptimismTxMeta] into the
/// async colored `build_transaction_receipt_with_block_receipts`
/// function, a reference counter for the L1 block info is
/// used so the L1 block info can be shared between receipts.
#[derive(Debug, Default, Clone)]
pub(crate) struct OptimismTxMeta {
/// The L1 block info.
pub(crate) l1_block_info: Option<Rc<L1BlockInfo>>,
/// The L1 fee for the block.
pub(crate) l1_fee: Option<U256>,
/// The L1 data gas for the block.
pub(crate) l1_data_gas: Option<U256>,
}

impl OptimismTxMeta {
/// Creates a new [OptimismTxMeta].
pub(crate) fn new(
l1_block_info: Option<Rc<L1BlockInfo>>,
l1_fee: Option<U256>,
l1_data_gas: Option<U256>,
) -> Self {
Self { l1_block_info, l1_fee, l1_data_gas }
}
}
2 changes: 1 addition & 1 deletion crates/rpc/rpc/src/eth/api/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use revm::{
use revm_primitives::{db::DatabaseCommit, Env, ExecutionResult, ResultAndState, SpecId, State};

#[cfg(feature = "optimism")]
use crate::eth::api::block::OptimismTxMeta;
use crate::eth::api::optimism::OptimismTxMeta;
#[cfg(feature = "optimism")]
use reth_revm::optimism::RethL1BlockInfo;
#[cfg(feature = "optimism")]
Expand Down

0 comments on commit 3190b1f

Please sign in to comment.