Skip to content
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
35 changes: 32 additions & 3 deletions crates/scroll/rpc/src/eth/block.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
//! Loads and formats Scroll block RPC response.

use crate::{ScrollEthApi, ScrollEthApiError};
use crate::{RpcBlockHeaderMut, ScrollEthApi, ScrollEthApiError};

use reth_rpc_convert::RpcConvert;
use alloy_consensus::BlockHeader;
use alloy_eips::BlockId;
use reth_provider::HeaderProvider;
use reth_rpc_convert::{RpcConvert, RpcTypes};
use reth_rpc_eth_api::{
helpers::{EthBlocks, LoadBlock},
RpcNodeCore,
EthApiTypes, FromEthApiError, FullEthApiTypes, RpcBlock, RpcNodeCore,
};
use reth_rpc_eth_types::error::FromEvmError;

Expand All @@ -14,7 +17,33 @@ where
N: RpcNodeCore,
ScrollEthApiError: FromEvmError<N::Evm>,
Rpc: RpcConvert<Primitives = N::Primitives, Error = ScrollEthApiError>,
<<Self as EthApiTypes>::NetworkTypes as RpcTypes>::Header: RpcBlockHeaderMut,
{
async fn rpc_block(
&self,
block_id: BlockId,
full: bool,
) -> Result<Option<RpcBlock<Self::NetworkTypes>>, Self::Error>
where
Self: FullEthApiTypes,
{
let Some(block) = self.recovered_block(block_id).await? else { return Ok(None) };

let td = self
.provider()
.header_td_by_number(block.number())
.map_err(Self::Error::from_eth_err)?;

let mut block = block.clone_into_rpc_block(
full.into(),
|tx, tx_info| self.tx_resp_builder().fill(tx, tx_info),
|header, size| self.tx_resp_builder().convert_header(header, size),
)?;

*block.header.total_difficulty_mut() = td;

Ok(Some(block))
}
}

impl<N, Rpc> LoadBlock for ScrollEthApi<N, Rpc>
Expand Down
12 changes: 12 additions & 0 deletions crates/scroll/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,15 @@ pub mod sequencer;
pub use error::{ScrollEthApiError, SequencerClientError};
pub use eth::{ScrollEthApi, ScrollReceiptBuilder};
pub use sequencer::SequencerClient;

/// Gives mutable access to the fields of an RPC block header.
pub trait RpcBlockHeaderMut {
/// Mutable reference to the total difficulty.
fn total_difficulty_mut(&mut self) -> &mut Option<alloy_primitives::U256>;
}

impl RpcBlockHeaderMut for alloy_rpc_types_eth::Header {
fn total_difficulty_mut(&mut self) -> &mut Option<alloy_primitives::U256> {
&mut self.total_difficulty
}
}
Loading