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
68 changes: 68 additions & 0 deletions crates/optimism/rpc/src/historical.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//! Client support for optimism historical RPC requests.

use crate::sequencer::Error;
use alloy_json_rpc::{RpcRecv, RpcSend};
use alloy_rpc_client::RpcClient;
use std::sync::Arc;
use tracing::warn;

/// A client that can be used to forward RPC requests for historical data to an endpoint.
///
/// This is intended to be used for OP-Mainnet pre-bedrock data, allowing users to query historical
/// state.
#[derive(Debug, Clone)]
pub struct HistoricalRpcClient {
inner: Arc<HistoricalRpcClientInner>,
}

impl HistoricalRpcClient {
/// Constructs a new historical RPC client with the given endpoint URL.
pub async fn new(endpoint: &str) -> Result<Self, Error> {
let client = RpcClient::new_http(
endpoint.parse::<reqwest::Url>().map_err(|err| Error::InvalidUrl(err.to_string()))?,
);

Ok(Self {
inner: Arc::new(HistoricalRpcClientInner {
historical_endpoint: endpoint.to_string(),
client,
}),
})
}

/// Returns a reference to the underlying RPC client
fn client(&self) -> &RpcClient {
&self.inner.client
}

/// Forwards a JSON-RPC request to the historical endpoint
pub async fn request<Params: RpcSend, Resp: RpcRecv>(
&self,
method: &str,
params: Params,
) -> Result<Resp, Error> {
let resp =
self.client().request::<Params, Resp>(method.to_string(), params).await.inspect_err(
|err| {
warn!(
target: "rpc::historical",
%err,
"HTTP request to historical endpoint failed"
);
},
)?;

Ok(resp)
}

/// Returns the configured historical endpoint URL
pub fn endpoint(&self) -> &str {
&self.inner.historical_endpoint
}
}

#[derive(Debug)]
struct HistoricalRpcClientInner {
historical_endpoint: String,
client: RpcClient,
}
1 change: 1 addition & 0 deletions crates/optimism/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
pub mod engine;
pub mod error;
pub mod eth;
pub mod historical;
pub mod miner;
pub mod sequencer;
pub mod witness;
Expand Down
Loading