Skip to content

Commit

Permalink
chore(op): remove optimism feature for LoadReceipts (#9504)
Browse files Browse the repository at this point in the history
Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
  • Loading branch information
emhane and mattsse authored Jul 16, 2024
1 parent 06bfce2 commit 4dbf47b
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 140 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/optimism/rpc/src/eth/receipt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ where
}
}

/// Applies OP specific fields to a receipt.
/// Applies OP specific fields to a receipt builder.
pub fn op_receipt_fields(
resp_builder: ReceiptBuilder,
tx: &TransactionSigned,
Expand Down
10 changes: 6 additions & 4 deletions crates/optimism/rpc/src/eth/transaction.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
//! Loads and formats OP transaction RPC response.
//! Loads and formats OP transaction RPC response.
use std::sync::Arc;

use reth_evm_optimism::RethL1BlockInfo;
use reth_primitives::TransactionSigned;
use reth_provider::BlockReaderIdExt;
use reth_provider::{BlockReaderIdExt, TransactionsProvider};
use reth_rpc_eth_api::{
helpers::{EthApiSpec, EthSigner, EthTransactions, LoadTransaction},
RawTransactionForwarder,
Expand All @@ -17,7 +19,7 @@ impl<Eth: EthTransactions> EthTransactions for OpEthApi<Eth> {
EthTransactions::provider(&self.inner)
}

fn raw_tx_forwarder(&self) -> Option<std::sync::Arc<dyn RawTransactionForwarder>> {
fn raw_tx_forwarder(&self) -> Option<Arc<dyn RawTransactionForwarder>> {
self.inner.raw_tx_forwarder()
}

Expand All @@ -29,7 +31,7 @@ impl<Eth: EthTransactions> EthTransactions for OpEthApi<Eth> {
impl<Eth: LoadTransaction> LoadTransaction for OpEthApi<Eth> {
type Pool = Eth::Pool;

fn provider(&self) -> impl reth_provider::TransactionsProvider {
fn provider(&self) -> impl TransactionsProvider {
LoadTransaction::provider(&self.inner)
}

Expand Down
2 changes: 0 additions & 2 deletions crates/rpc/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ reth-network-peers.workspace = true
reth-evm.workspace = true
reth-rpc-eth-types.workspace = true
reth-rpc-server-types.workspace = true
reth-evm-optimism = { workspace = true, optional = true }
reth-node-api.workspace = true

# eth
Expand Down Expand Up @@ -94,6 +93,5 @@ optimism = [
"reth-rpc-eth-api/optimism",
"reth-revm/optimism",
"jsonrpsee-types",
"reth-evm-optimism",
"reth-rpc-eth-types/optimism",
]
1 change: 0 additions & 1 deletion crates/rpc/rpc/src/eth/helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ mod fees;
#[cfg(feature = "optimism")]
pub mod optimism;
mod pending_block;
#[cfg(not(feature = "optimism"))]
mod receipt;
mod spec;
mod state;
Expand Down
133 changes: 2 additions & 131 deletions crates/rpc/rpc/src/eth/helpers/optimism.rs
Original file line number Diff line number Diff line change
@@ -1,138 +1,9 @@
//! Loads and formats OP transaction RPC response.
use jsonrpsee_types::error::ErrorObject;
use reth_evm_optimism::RethL1BlockInfo;
use reth_primitives::{Receipt, TransactionMeta, TransactionSigned};
use reth_provider::{BlockIdReader, ChainSpecProvider};
use reth_rpc_eth_api::helpers::LoadReceipt;
use reth_rpc_eth_types::{EthApiError, EthResult, EthStateCache, ReceiptBuilder};
use reth_rpc_eth_types::EthApiError;
use reth_rpc_server_types::result::internal_rpc_err;
use reth_rpc_types::{AnyTransactionReceipt, OptimismTransactionReceiptFields, ToRpcError};
use revm::L1BlockInfo;

use crate::EthApi;

/// L1 fee and data gas for a transaction, along with the L1 block info.
#[derive(Debug, Default, Clone)]
pub struct OptimismTxMeta {
/// The L1 block info.
pub l1_block_info: Option<L1BlockInfo>,
/// The L1 fee for the block.
pub l1_fee: Option<u128>,
/// The L1 data gas for the block.
pub l1_data_gas: Option<u128>,
}

impl OptimismTxMeta {
/// Creates a new [`OptimismTxMeta`].
pub const fn new(
l1_block_info: Option<L1BlockInfo>,
l1_fee: Option<u128>,
l1_data_gas: Option<u128>,
) -> Self {
Self { l1_block_info, l1_fee, l1_data_gas }
}
}

impl<Provider, Pool, Network, EvmConfig> EthApi<Provider, Pool, Network, EvmConfig>
where
Provider: BlockIdReader + ChainSpecProvider,
{
/// Builds [`OptimismTxMeta`] object using the provided [`TransactionSigned`], L1 block
/// info and block timestamp. The [`L1BlockInfo`] is used to calculate the l1 fee and l1 data
/// gas for the transaction. If the [`L1BlockInfo`] is not provided, the meta info will be
/// empty.
pub fn build_op_tx_meta(
&self,
tx: &TransactionSigned,
l1_block_info: Option<L1BlockInfo>,
block_timestamp: u64,
) -> EthResult<OptimismTxMeta> {
let Some(l1_block_info) = l1_block_info else { return Ok(OptimismTxMeta::default()) };

let (l1_fee, l1_data_gas) = if !tx.is_deposit() {
let envelope_buf = tx.envelope_encoded();

let inner_l1_fee = l1_block_info
.l1_tx_data_fee(
&self.inner.provider().chain_spec(),
block_timestamp,
&envelope_buf,
tx.is_deposit(),
)
.map_err(|_| OptimismEthApiError::L1BlockFeeError)?;
let inner_l1_data_gas = l1_block_info
.l1_data_gas(&self.inner.provider().chain_spec(), block_timestamp, &envelope_buf)
.map_err(|_| OptimismEthApiError::L1BlockGasError)?;
(
Some(inner_l1_fee.saturating_to::<u128>()),
Some(inner_l1_data_gas.saturating_to::<u128>()),
)
} else {
(None, None)
};

Ok(OptimismTxMeta::new(Some(l1_block_info), l1_fee, l1_data_gas))
}
}

impl<Provider, Pool, Network, EvmConfig> LoadReceipt for EthApi<Provider, Pool, Network, EvmConfig>
where
Self: Send + Sync,
Provider: BlockIdReader + ChainSpecProvider,
{
#[inline]
fn cache(&self) -> &EthStateCache {
self.inner.cache()
}

async fn build_transaction_receipt(
&self,
tx: TransactionSigned,
meta: TransactionMeta,
receipt: Receipt,
) -> EthResult<AnyTransactionReceipt> {
let (block, receipts) = self
.cache()
.get_block_and_receipts(meta.block_hash)
.await?
.ok_or(EthApiError::UnknownBlockNumber)?;

let block = block.unseal();
let l1_block_info = reth_evm_optimism::extract_l1_info(&block).ok();
let optimism_tx_meta = self.build_op_tx_meta(&tx, l1_block_info, block.timestamp)?;

let resp_builder = ReceiptBuilder::new(&tx, meta, &receipt, &receipts)?;
let resp_builder = op_receipt_fields(resp_builder, &tx, &receipt, optimism_tx_meta);

Ok(resp_builder.build())
}
}

/// Applies OP specific fields to a receipt.
fn op_receipt_fields(
resp_builder: ReceiptBuilder,
tx: &TransactionSigned,
receipt: &Receipt,
optimism_tx_meta: OptimismTxMeta,
) -> ReceiptBuilder {
let mut op_fields = OptimismTransactionReceiptFields::default();

if tx.is_deposit() {
op_fields.deposit_nonce = receipt.deposit_nonce.map(reth_primitives::U64::from);
op_fields.deposit_receipt_version =
receipt.deposit_receipt_version.map(reth_primitives::U64::from);
} else if let Some(l1_block_info) = optimism_tx_meta.l1_block_info {
op_fields.l1_fee = optimism_tx_meta.l1_fee;
op_fields.l1_gas_used = optimism_tx_meta.l1_data_gas.map(|dg| {
dg + l1_block_info.l1_fee_overhead.unwrap_or_default().saturating_to::<u128>()
});
op_fields.l1_fee_scalar = Some(f64::from(l1_block_info.l1_base_fee_scalar) / 1_000_000.0);
op_fields.l1_gas_price = Some(l1_block_info.l1_base_fee.saturating_to());
}

resp_builder.add_other_fields(op_fields.into())
}
use reth_rpc_types::ToRpcError;

/// Optimism specific errors, that extend [`EthApiError`].
#[derive(Debug, thiserror::Error)]
Expand Down

0 comments on commit 4dbf47b

Please sign in to comment.