-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
### Description Sets up an `rpc` crate. ### Metadata #7
- Loading branch information
Showing
11 changed files
with
433 additions
and
225 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
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
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,39 @@ | ||
[package] | ||
name = "op-rpc" | ||
description = "Consensus RPC for Rollup Nodes" | ||
version = "0.0.0" | ||
edition.workspace = true | ||
rust-version.workspace = true | ||
authors.workspace = true | ||
license.workspace = true | ||
repository.workspace = true | ||
keywords.workspace = true | ||
categories.workspace = true | ||
|
||
[dependencies] | ||
# op-alloy | ||
op-alloy-rpc-types.workspace = true | ||
op-alloy-rpc-jsonrpsee = { workspace = true, features = ["client"] } | ||
|
||
# Alloy | ||
alloy-eips.workspace = true | ||
|
||
# Misc | ||
tracing.workspace = true | ||
async-trait.workspace = true | ||
jsonrpsee = { workspace = true, features = ["jsonrpsee-core", "client-core", "server-core", "macros"] } | ||
|
||
# `reth` feature flag dependencies | ||
alloy = { workspace = true, optional = true } | ||
reqwest = { workspace = true, features = ["rustls-tls-native-roots"], optional = true } | ||
thiserror = { workspace = true, optional = true } | ||
serde_json = { workspace = true, optional = true } | ||
|
||
[features] | ||
default = ["reth"] | ||
reth = [ | ||
"dep:alloy", | ||
"dep:reqwest", | ||
"dep:serde_json", | ||
"dep:thiserror", | ||
] |
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,10 @@ | ||
//! Consensus RPC for Rollup Nodes | ||
|
||
#![doc(issue_tracker_base_url = "https://github.com/ithacaxyz/op-rs/issues/")] | ||
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))] | ||
#![cfg_attr(not(test), warn(unused_crate_dependencies))] | ||
|
||
pub mod rollup; | ||
|
||
#[cfg(feature = "reth")] | ||
pub mod sequencer; |
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,48 @@ | ||
//! Contains the RPC Definition | ||
|
||
use alloy_eips::BlockNumberOrTag; | ||
use async_trait::async_trait; | ||
use jsonrpsee::core::RpcResult; | ||
use op_alloy_rpc_jsonrpsee::traits::RollupNodeServer; | ||
use op_alloy_rpc_types::{ | ||
config::RollupConfig, output::OutputResponse, safe_head::SafeHeadResponse, sync::SyncStatus, | ||
}; | ||
use tracing::trace; | ||
|
||
/// An implementation of the [`RollupNodeServer`] trait. | ||
#[derive(Debug, Clone)] | ||
pub struct RollupNodeRpc { | ||
/// The version of the node. | ||
version: String, | ||
} | ||
|
||
#[async_trait] | ||
impl RollupNodeServer for RollupNodeRpc { | ||
async fn op_output_at_block( | ||
&self, | ||
block_number: BlockNumberOrTag, | ||
) -> RpcResult<OutputResponse> { | ||
trace!("op_output_at_block: {:?}", block_number); | ||
unimplemented!() | ||
} | ||
|
||
async fn op_safe_head_at_l1_block( | ||
&self, | ||
block_number: BlockNumberOrTag, | ||
) -> RpcResult<SafeHeadResponse> { | ||
trace!("op_safe_head_at_l1_block: {:?}", block_number); | ||
unimplemented!() | ||
} | ||
|
||
async fn op_sync_status(&self) -> RpcResult<SyncStatus> { | ||
unimplemented!() | ||
} | ||
|
||
async fn op_rollup_config(&self) -> RpcResult<RollupConfig> { | ||
unimplemented!() | ||
} | ||
|
||
async fn op_version(&self) -> RpcResult<String> { | ||
Ok(self.version.clone()) | ||
} | ||
} |
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,104 @@ | ||
//! Optimism reth RPC Extension used to forward raw transactions to the sequencer. | ||
|
||
use std::sync::{ | ||
atomic::{self, AtomicUsize}, | ||
Arc, | ||
}; | ||
|
||
use alloy::primitives::hex; | ||
use reqwest::Client; | ||
use serde_json::json; | ||
use tracing::warn; | ||
|
||
/// Error type when interacting with the Sequencer | ||
#[derive(Debug, thiserror::Error)] | ||
pub enum SequencerClientError { | ||
/// Wrapper around an [`reqwest::Error`]. | ||
#[error(transparent)] | ||
HttpError(#[from] reqwest::Error), | ||
/// Thrown when serializing transaction to forward to sequencer | ||
#[error("invalid sequencer transaction")] | ||
InvalidSequencerTransaction, | ||
} | ||
|
||
/// A client to interact with a Sequencer | ||
#[derive(Debug, Clone)] | ||
pub struct SequencerClient { | ||
inner: Arc<SequencerClientInner>, | ||
} | ||
|
||
impl SequencerClient { | ||
/// Creates a new [`SequencerClient`]. | ||
pub fn new(sequencer_endpoint: impl Into<String>) -> Self { | ||
let client = Client::builder().use_rustls_tls().build().unwrap(); | ||
Self::with_client(sequencer_endpoint, client) | ||
} | ||
|
||
/// Creates a new [`SequencerClient`]. | ||
pub fn with_client(sequencer_endpoint: impl Into<String>, http_client: Client) -> Self { | ||
let inner = SequencerClientInner { | ||
sequencer_endpoint: sequencer_endpoint.into(), | ||
http_client, | ||
id: AtomicUsize::new(0), | ||
}; | ||
Self { inner: Arc::new(inner) } | ||
} | ||
|
||
/// Returns the network of the client | ||
pub fn endpoint(&self) -> &str { | ||
&self.inner.sequencer_endpoint | ||
} | ||
|
||
/// Returns the client | ||
pub fn http_client(&self) -> &Client { | ||
&self.inner.http_client | ||
} | ||
|
||
/// Returns the next id for the request | ||
fn next_request_id(&self) -> usize { | ||
self.inner.id.fetch_add(1, atomic::Ordering::SeqCst) | ||
} | ||
|
||
/// Forwards a transaction to the sequencer endpoint. | ||
pub async fn forward_raw_transaction(&self, tx: &[u8]) -> Result<(), SequencerClientError> { | ||
let body = serde_json::to_string(&json!({ | ||
"jsonrpc": "2.0", | ||
"method": "eth_sendRawTransaction", | ||
"params": [format!("0x{}", hex::encode(tx))], | ||
"id": self.next_request_id() | ||
})) | ||
.map_err(|_| { | ||
warn!( | ||
target = "rpc::eth", | ||
"Failed to serialize transaction for forwarding to sequencer" | ||
); | ||
SequencerClientError::InvalidSequencerTransaction | ||
})?; | ||
|
||
self.http_client() | ||
.post(self.endpoint()) | ||
.header(reqwest::header::CONTENT_TYPE, "application/json") | ||
.body(body) | ||
.send() | ||
.await | ||
.inspect_err(|err| { | ||
warn!( | ||
target = "rpc::eth", | ||
%err, | ||
"Failed to forward transaction to sequencer", | ||
); | ||
})?; | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
#[derive(Debug, Default)] | ||
struct SequencerClientInner { | ||
/// The endpoint of the sequencer | ||
sequencer_endpoint: String, | ||
/// The HTTP client | ||
http_client: Client, | ||
/// Keeps track of unique request ids | ||
id: AtomicUsize, | ||
} |
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