From 70635b728252e13f270944e66a0ece1e2a980b6a Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Thu, 8 Dec 2022 13:55:56 +0100 Subject: [PATCH] feat(rpc): add eth_pubsub skeleton (#354) --- crates/net/rpc-api/src/eth_pubsub.rs | 4 +- crates/net/rpc/src/eth/api/mod.rs | 64 ++++++++++++++++++ .../src/eth/{eth_server.rs => api/server.rs} | 4 +- crates/net/rpc/src/eth/mod.rs | 67 ++----------------- crates/net/rpc/src/eth/pubsub.rs | 63 +++++++++++++++++ crates/net/rpc/src/lib.rs | 2 +- 6 files changed, 137 insertions(+), 67 deletions(-) create mode 100644 crates/net/rpc/src/eth/api/mod.rs rename crates/net/rpc/src/eth/{eth_server.rs => api/server.rs} (97%) create mode 100644 crates/net/rpc/src/eth/pubsub.rs diff --git a/crates/net/rpc-api/src/eth_pubsub.rs b/crates/net/rpc-api/src/eth_pubsub.rs index 4f6966a35f..7777c2579b 100644 --- a/crates/net/rpc-api/src/eth_pubsub.rs +++ b/crates/net/rpc-api/src/eth_pubsub.rs @@ -6,9 +6,9 @@ use reth_rpc_types::pubsub::{Kind, Params}; pub trait EthPubSubApi { /// Create an ethereum subscription. #[subscription( - name = "eth_subscribe" => "eth_subscription", + name = "eth_subscribe", unsubscribe = "eth_unsubscribe", item = reth_rpc_types::pubsub::SubscriptionResult )] - fn eth_subscribe(&self, kind: Kind, params: Option); + fn subscribe(&self, kind: Kind, params: Option); } diff --git a/crates/net/rpc/src/eth/api/mod.rs b/crates/net/rpc/src/eth/api/mod.rs new file mode 100644 index 0000000000..416004ec05 --- /dev/null +++ b/crates/net/rpc/src/eth/api/mod.rs @@ -0,0 +1,64 @@ +//! Provides everything related to `eth_` namespace + +use reth_interfaces::{ + provider::{BlockProvider, StateProviderFactory}, + Result, +}; +use reth_primitives::{Transaction, U256, U64}; +use reth_transaction_pool::TransactionPool; +use std::sync::Arc; + +mod server; + +/// `Eth` API implementation. +/// +/// This type provides the functionality for handling `eth_` related requests. +/// These are implemented two-fold: Core functionality is implemented as functions directly on this +/// type. Additionally, the required server implementations (e.g. [`reth_rpc_api::EthApiServer`]) +/// are implemented separately in submodules. The rpc handler implementation can then delegate to +/// the main impls. This way [`EthApi`] is not limited to [`jsonrpsee`] and can be used standalone +/// or in other network handlers (for example ipc). +#[derive(Debug, Clone)] +pub struct EthApi { + /// All nested fields bundled together. + inner: Arc>, +} + +impl EthApi +where + Pool: TransactionPool + Clone, + Client: BlockProvider + StateProviderFactory, +{ + /// Creates a new, shareable instance. + pub fn new(client: Arc, pool: Pool) -> Self { + let inner = EthApiInner { client, pool }; + Self { inner: Arc::new(inner) } + } + + /// Returns the inner `Client` + fn client(&self) -> &Arc { + &self.inner.client + } + + /// Returns the current ethereum protocol version. + /// + /// Note: This returns an `U64`, since this should return as hex string. + pub fn protocol_version(&self) -> U64 { + 1u64.into() + } + + /// Returns the best block number + pub fn block_number(&self) -> Result { + Ok(self.client().chain_info()?.best_number.into()) + } +} + +/// Container type `EthApi` +#[derive(Debug)] +struct EthApiInner { + /// The transaction pool. + pool: Pool, + /// The client that can interact with the chain. + client: Arc, + // TODO needs network access to handle things like `eth_syncing` +} diff --git a/crates/net/rpc/src/eth/eth_server.rs b/crates/net/rpc/src/eth/api/server.rs similarity index 97% rename from crates/net/rpc/src/eth/eth_server.rs rename to crates/net/rpc/src/eth/api/server.rs index 6d1e858833..0c9ea39bfe 100644 --- a/crates/net/rpc/src/eth/eth_server.rs +++ b/crates/net/rpc/src/eth/api/server.rs @@ -1,7 +1,7 @@ //! Implementation of the [`jsonrpsee`] generated [`reth_rpc_api::EthApiServer`] trait -//! implementation for handling RPC requests for he `eth_` namespace. +//! Handles RPC requests for he `eth_` namespace. -use crate::{eth::EthApi, result::ToRpcResult}; +use crate::{eth::api::EthApi, result::ToRpcResult}; use jsonrpsee::core::RpcResult as Result; use reth_interfaces::provider::{BlockProvider, StateProviderFactory}; use reth_primitives::{ diff --git a/crates/net/rpc/src/eth/mod.rs b/crates/net/rpc/src/eth/mod.rs index 7e06a30c24..186f8d754a 100644 --- a/crates/net/rpc/src/eth/mod.rs +++ b/crates/net/rpc/src/eth/mod.rs @@ -1,64 +1,7 @@ -//! Provides everything related to `eth_` namespace +//! `eth` namespace handler implementation. -use reth_interfaces::{ - provider::{BlockProvider, StateProviderFactory}, - Result, -}; -use reth_primitives::{Transaction, U256, U64}; -use reth_transaction_pool::TransactionPool; -use std::sync::Arc; +mod api; +mod pubsub; -mod eth_server; - -/// `Eth` API implementation. -/// -/// This type provides the functionality for handling `eth_` related requests. -/// These are implemented two-fold: Core functionality is implemented as functions directly on this -/// type. Additionally, the required server implementations (e.g. [`reth_rpc_api::EthApiServer`]) -/// are implemented separately in submodules. The rpc handler implementation can then delegate to -/// the main impls. This way [`EthApi`] is not limited to [`jsonrpsee`] and can be used standalone -/// or in other network handlers (for example ipc). -#[derive(Debug, Clone)] -pub struct EthApi { - /// All nested fields bundled together. - inner: Arc>, -} - -impl EthApi -where - Pool: TransactionPool + Clone, - Client: BlockProvider + StateProviderFactory, -{ - /// Creates a new, shareable instance. - pub fn new(client: Arc, pool: Pool) -> Self { - let inner = EthApiInner { client, pool }; - Self { inner: Arc::new(inner) } - } - - /// Returns the inner `Client` - fn client(&self) -> &Arc { - &self.inner.client - } - - /// Returns the current ethereum protocol version. - /// - /// Note: This returns an `U64`, since this should return as hex string. - pub fn protocol_version(&self) -> U64 { - 1u64.into() - } - - /// Returns the best block number - pub fn block_number(&self) -> Result { - Ok(self.client().chain_info()?.best_number.into()) - } -} - -/// Container type `EthApi` -#[derive(Debug)] -struct EthApiInner { - /// The transaction pool. - pool: Pool, - /// The client that can interact with the chain. - client: Arc, - // TODO needs network access to handle things like `eth_syncing` -} +pub use api::EthApi; +pub use pubsub::EthPubSub; diff --git a/crates/net/rpc/src/eth/pubsub.rs b/crates/net/rpc/src/eth/pubsub.rs new file mode 100644 index 0000000000..8bb3d5ac70 --- /dev/null +++ b/crates/net/rpc/src/eth/pubsub.rs @@ -0,0 +1,63 @@ +//! `eth_` PubSub RPC handler implementation + +use jsonrpsee::{types::SubscriptionResult, SubscriptionSink}; +use reth_interfaces::provider::BlockProvider; +use reth_rpc_api::EthPubSubApiServer; +use reth_rpc_types::pubsub::{Kind, Params}; +use reth_transaction_pool::TransactionPool; +use std::sync::Arc; + +/// `Eth` pubsub RPC implementation. +/// +/// This handles +#[derive(Debug, Clone)] +pub struct EthPubSub { + /// All nested fields bundled together. + inner: Arc>, +} + +// === impl EthPubSub === + +impl EthPubSub { + /// Creates a new, shareable instance. + pub fn new(client: Arc, pool: Pool) -> Self { + let inner = EthPubSubInner { client, pool }; + Self { inner: Arc::new(inner) } + } +} + +impl EthPubSubApiServer for EthPubSub +where + Pool: TransactionPool + Clone, + Client: BlockProvider + 'static, +{ + fn subscribe( + &self, + mut sink: SubscriptionSink, + _kind: Kind, + _params: Option, + ) -> SubscriptionResult { + sink.accept()?; + todo!() + } +} + +/// The actual handler for and accepted [`EthPubSub::subscribe`] call. +async fn handle_accepted( + _pool: Pool, + _client: Arc, + _accepted_sink: SubscriptionSink, + _kind: Kind, + _params: Option, +) { +} + +/// Container type `EthApi` +#[derive(Debug)] +struct EthPubSubInner { + /// The transaction pool. + pool: Pool, + /// The client that can interact with the chain. + client: Arc, + // TODO needs spawn access +} diff --git a/crates/net/rpc/src/lib.rs b/crates/net/rpc/src/lib.rs index bcc3e2ce7a..58c4624ee7 100644 --- a/crates/net/rpc/src/lib.rs +++ b/crates/net/rpc/src/lib.rs @@ -13,6 +13,6 @@ mod eth; -pub use eth::EthApi; +pub use eth::{EthApi, EthPubSub}; pub(crate) mod result;