From 6f047a5de0c3b8ebfad5033b0139856a98cd0955 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien?= Date: Mon, 23 Jan 2023 22:53:15 +0100 Subject: [PATCH] feat: add Web3 namespace RPC handler (#990) --- crates/net/network-api/src/lib.rs | 4 ++-- crates/net/network/src/network.rs | 2 +- crates/net/rpc-api/src/web3.rs | 3 ++- crates/net/rpc/src/lib.rs | 2 ++ crates/net/rpc/src/web3.rs | 40 +++++++++++++++++++++++++++++++ 5 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 crates/net/rpc/src/web3.rs diff --git a/crates/net/network-api/src/lib.rs b/crates/net/network-api/src/lib.rs index 54454f2be7..8dfcd339d8 100644 --- a/crates/net/network-api/src/lib.rs +++ b/crates/net/network-api/src/lib.rs @@ -42,8 +42,8 @@ pub trait PeersInfo: Send + Sync { /// The status of the network being ran by the local node. #[derive(Serialize, Deserialize, Debug, Default)] pub struct NetworkStatus { - /// The local node client name. - pub client_name: String, + /// The local node client version. + pub client_version: String, /// Information about the Ethereum Wire Protocol. pub eth_protocol_info: EthProtocolInfo, } diff --git a/crates/net/network/src/network.rs b/crates/net/network/src/network.rs index 0d8e519b54..e5f5153bf0 100644 --- a/crates/net/network/src/network.rs +++ b/crates/net/network/src/network.rs @@ -235,7 +235,7 @@ impl NetworkInfo for NetworkHandle { let status = self.get_status().await?; Ok(NetworkStatus { - client_name: "Reth".to_string(), + client_version: "Reth".to_string(), eth_protocol_info: EthProtocolInfo { difficulty: status.total_difficulty, head: status.blockhash, diff --git a/crates/net/rpc-api/src/web3.rs b/crates/net/rpc-api/src/web3.rs index d64c16beed..fec593cbf6 100644 --- a/crates/net/rpc-api/src/web3.rs +++ b/crates/net/rpc-api/src/web3.rs @@ -4,10 +4,11 @@ use reth_primitives::{Bytes, H256}; /// Web3 rpc interface. #[cfg_attr(not(feature = "client"), rpc(server))] #[cfg_attr(feature = "client", rpc(server, client))] +#[async_trait::async_trait] pub trait Web3Api { /// Returns current client version. #[method(name = "web3_clientVersion")] - fn client_version(&self) -> Result; + async fn client_version(&self) -> Result; /// Returns sha3 of the given data. #[method(name = "web3_sha3")] diff --git a/crates/net/rpc/src/lib.rs b/crates/net/rpc/src/lib.rs index 9d2fba2554..33a69c9b35 100644 --- a/crates/net/rpc/src/lib.rs +++ b/crates/net/rpc/src/lib.rs @@ -15,10 +15,12 @@ mod admin; mod engine; mod eth; mod net; +mod web3; pub use admin::AdminApi; pub use engine::EngineApi; pub use eth::{EthApi, EthApiSpec, EthPubSub}; pub use net::NetApi; +pub use web3::Web3Api; pub(crate) mod result; diff --git a/crates/net/rpc/src/web3.rs b/crates/net/rpc/src/web3.rs new file mode 100644 index 0000000000..0f32b2e011 --- /dev/null +++ b/crates/net/rpc/src/web3.rs @@ -0,0 +1,40 @@ +use crate::result::ToRpcResult; +use async_trait::async_trait; +use jsonrpsee::core::RpcResult; +use reth_network::NetworkHandle; +use reth_network_api::NetworkInfo; +use reth_primitives::{keccak256, Bytes, H256}; +use reth_rpc_api::Web3ApiServer; + +/// `web3` API implementation. +/// +/// This type provides the functionality for handling `web3` related requests. +pub struct Web3Api { + /// An interface to interact with the network + network: NetworkHandle, +} + +impl Web3Api { + /// Creates a new instance of `Web3Api`. + pub fn new(network: NetworkHandle) -> Web3Api { + Web3Api { network } + } +} + +#[async_trait] +impl Web3ApiServer for Web3Api { + async fn client_version(&self) -> RpcResult { + let status = self.network.network_status().await.to_rpc_result()?; + Ok(status.client_version) + } + + fn sha3(&self, input: Bytes) -> RpcResult { + Ok(keccak256(input)) + } +} + +impl std::fmt::Debug for Web3Api { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Web3Api").finish_non_exhaustive() + } +}