forked from paradigmxyz/reth
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(op): fixes impl of eth api for OP (paradigmxyz#9377)
- Loading branch information
1 parent
eb5902b
commit ca7a949
Showing
12 changed files
with
417 additions
and
91 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
//! RPC errors specific to OP. | ||
use jsonrpsee::types::ErrorObject; | ||
use reth_rpc_eth_types::EthApiError; | ||
use reth_rpc_server_types::result::internal_rpc_err; | ||
use reth_rpc_types::ToRpcError; | ||
|
||
/// Optimism specific errors, that extend [`EthApiError`]. | ||
#[derive(Debug, thiserror::Error)] | ||
pub enum OpEthApiError { | ||
/// Thrown when calculating L1 gas fee. | ||
#[error("failed to calculate l1 gas fee")] | ||
L1BlockFeeError, | ||
/// Thrown when calculating L1 gas used | ||
#[error("failed to calculate l1 gas used")] | ||
L1BlockGasError, | ||
} | ||
|
||
impl ToRpcError for OpEthApiError { | ||
fn to_rpc_error(&self) -> ErrorObject<'static> { | ||
match self { | ||
Self::L1BlockFeeError | Self::L1BlockGasError => internal_rpc_err(self.to_string()), | ||
} | ||
} | ||
} | ||
|
||
impl From<OpEthApiError> for EthApiError { | ||
fn from(err: OpEthApiError) -> Self { | ||
Self::other(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
//! Loads and formats OP block RPC response. | ||
use reth_primitives::TransactionMeta; | ||
use reth_provider::{BlockReaderIdExt, HeaderProvider}; | ||
use reth_rpc_eth_api::helpers::{EthApiSpec, EthBlocks, LoadBlock, LoadReceipt, LoadTransaction}; | ||
use reth_rpc_eth_types::{EthResult, EthStateCache, ReceiptBuilder}; | ||
use reth_rpc_types::{AnyTransactionReceipt, BlockId}; | ||
|
||
use crate::{op_receipt_fields, OpEthApi}; | ||
|
||
impl<Eth> EthBlocks for OpEthApi<Eth> | ||
where | ||
Eth: EthBlocks + EthApiSpec + LoadTransaction, | ||
{ | ||
fn provider(&self) -> impl HeaderProvider { | ||
EthBlocks::provider(&self.inner) | ||
} | ||
|
||
async fn block_receipts( | ||
&self, | ||
block_id: BlockId, | ||
) -> EthResult<Option<Vec<AnyTransactionReceipt>>> | ||
where | ||
Self: LoadReceipt, | ||
{ | ||
if let Some((block, receipts)) = self.load_block_and_receipts(block_id).await? { | ||
let block_number = block.number; | ||
let base_fee = block.base_fee_per_gas; | ||
let block_hash = block.hash(); | ||
let excess_blob_gas = block.excess_blob_gas; | ||
let timestamp = block.timestamp; | ||
let block = block.unseal(); | ||
|
||
let l1_block_info = reth_evm_optimism::extract_l1_info(&block).ok(); | ||
|
||
let receipts = block | ||
.body | ||
.into_iter() | ||
.zip(receipts.iter()) | ||
.enumerate() | ||
.map(|(idx, (ref tx, receipt))| { | ||
let meta = TransactionMeta { | ||
tx_hash: tx.hash, | ||
index: idx as u64, | ||
block_hash, | ||
block_number, | ||
base_fee, | ||
excess_blob_gas, | ||
timestamp, | ||
}; | ||
|
||
let optimism_tx_meta = | ||
self.build_op_tx_meta(tx, l1_block_info.clone(), timestamp)?; | ||
|
||
ReceiptBuilder::new(tx, meta, receipt, &receipts).map(|builder| { | ||
op_receipt_fields(builder, tx, receipt, optimism_tx_meta).build() | ||
}) | ||
}) | ||
.collect::<EthResult<Vec<_>>>(); | ||
return receipts.map(Some) | ||
} | ||
|
||
Ok(None) | ||
} | ||
} | ||
|
||
impl<Eth: LoadBlock> LoadBlock for OpEthApi<Eth> { | ||
fn provider(&self) -> impl BlockReaderIdExt { | ||
LoadBlock::provider(&self.inner) | ||
} | ||
|
||
fn cache(&self) -> &EthStateCache { | ||
self.inner.cache() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
//! Loads OP pending block for a RPC response. | ||
use reth_primitives::{ | ||
revm_primitives::{BlockEnv, ExecutionResult}, | ||
BlockNumber, Receipt, TransactionSignedEcRecovered, B256, | ||
}; | ||
use reth_provider::{ChainSpecProvider, ExecutionOutcome}; | ||
use reth_rpc_eth_api::helpers::LoadPendingBlock; | ||
use reth_rpc_eth_types::PendingBlock; | ||
|
||
use crate::OpEthApi; | ||
|
||
impl<Eth> LoadPendingBlock for OpEthApi<Eth> | ||
where | ||
Eth: LoadPendingBlock, | ||
{ | ||
#[inline] | ||
fn provider( | ||
&self, | ||
) -> impl reth_provider::BlockReaderIdExt | ||
+ reth_provider::EvmEnvProvider | ||
+ reth_provider::ChainSpecProvider | ||
+ reth_provider::StateProviderFactory { | ||
self.inner.provider() | ||
} | ||
|
||
#[inline] | ||
fn pool(&self) -> impl reth_transaction_pool::TransactionPool { | ||
self.inner.pool() | ||
} | ||
|
||
#[inline] | ||
fn pending_block(&self) -> &tokio::sync::Mutex<Option<PendingBlock>> { | ||
self.inner.pending_block() | ||
} | ||
|
||
#[inline] | ||
fn evm_config(&self) -> &impl reth_evm::ConfigureEvm { | ||
self.inner.evm_config() | ||
} | ||
|
||
fn assemble_receipt( | ||
&self, | ||
tx: &TransactionSignedEcRecovered, | ||
result: ExecutionResult, | ||
cumulative_gas_used: u64, | ||
) -> Receipt { | ||
Receipt { | ||
tx_type: tx.tx_type(), | ||
success: result.is_success(), | ||
cumulative_gas_used, | ||
logs: result.into_logs().into_iter().map(Into::into).collect(), | ||
deposit_nonce: None, | ||
deposit_receipt_version: None, | ||
} | ||
} | ||
|
||
fn receipts_root( | ||
&self, | ||
_block_env: &BlockEnv, | ||
execution_outcome: &ExecutionOutcome, | ||
block_number: BlockNumber, | ||
) -> B256 { | ||
execution_outcome | ||
.optimism_receipts_root_slow( | ||
block_number, | ||
self.provider().chain_spec().as_ref(), | ||
_block_env.timestamp.to::<u64>(), | ||
) | ||
.expect("Block is present") | ||
} | ||
} |
Oops, something went wrong.