Skip to content

Commit

Permalink
feat: add Web3 namespace RPC handler (paradigmxyz#990)
Browse files Browse the repository at this point in the history
  • Loading branch information
leruaa authored Jan 23, 2023
1 parent e505c5f commit 6f047a5
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 4 deletions.
4 changes: 2 additions & 2 deletions crates/net/network-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
2 changes: 1 addition & 1 deletion crates/net/network/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion crates/net/rpc-api/src/web3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>;
async fn client_version(&self) -> Result<String>;

/// Returns sha3 of the given data.
#[method(name = "web3_sha3")]
Expand Down
2 changes: 2 additions & 0 deletions crates/net/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
40 changes: 40 additions & 0 deletions crates/net/rpc/src/web3.rs
Original file line number Diff line number Diff line change
@@ -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<String> {
let status = self.network.network_status().await.to_rpc_result()?;
Ok(status.client_version)
}

fn sha3(&self, input: Bytes) -> RpcResult<H256> {
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()
}
}

0 comments on commit 6f047a5

Please sign in to comment.