feat: add Web3 namespace RPC handler (#990)

This commit is contained in:
Aurélien
2023-01-23 22:53:15 +01:00
committed by GitHub
parent e505c5f670
commit 6f047a5de0
5 changed files with 47 additions and 4 deletions

View File

@ -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,
}

View File

@ -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,

View File

@ -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")]

View File

@ -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;

View File

@ -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()
}
}