mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
feat(rpc): net namespace (#363)
* feat(rpc): net namespace * add network_id field to debug * is_listening flag & peer count as hex * eth api trait * fix errors * Update crates/net/rpc/src/net/mod.rs Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de> * add trait bounds directly * fmt Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -3616,6 +3616,7 @@ dependencies = [
|
||||
"hex",
|
||||
"jsonrpsee",
|
||||
"reth-interfaces",
|
||||
"reth-network",
|
||||
"reth-primitives",
|
||||
"reth-provider",
|
||||
"reth-rpc-api",
|
||||
|
||||
@ -16,6 +16,7 @@ reth-rpc-api = { path = "../rpc-api" }
|
||||
reth-rpc-types = { path = "../rpc-types" }
|
||||
reth-provider = { path = "../../storage/provider"}
|
||||
reth-transaction-pool = { path = "../../transaction-pool" }
|
||||
reth-network = { path = "../network" }
|
||||
|
||||
# rpc
|
||||
jsonrpsee = { version = "0.16" }
|
||||
|
||||
@ -3,16 +3,31 @@
|
||||
use reth_interfaces::Result;
|
||||
use reth_primitives::{U256, U64};
|
||||
use reth_provider::{BlockProvider, StateProviderFactory};
|
||||
use reth_rpc_types::Transaction;
|
||||
use reth_transaction_pool::TransactionPool;
|
||||
use std::sync::Arc;
|
||||
|
||||
mod server;
|
||||
|
||||
/// `Eth` API trait.
|
||||
///
|
||||
/// Defines core functionality of the `eth` API implementation.
|
||||
pub trait EthApiSpec: Send + Sync {
|
||||
/// Returns the current ethereum protocol version.
|
||||
fn protocol_version(&self) -> U64;
|
||||
|
||||
/// Returns the best block number
|
||||
fn block_number(&self) -> Result<U256>;
|
||||
|
||||
/// Returns the chain id
|
||||
fn chain_id(&self) -> U64;
|
||||
}
|
||||
|
||||
/// `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`])
|
||||
/// These are implemented two-fold: Core functionality is implemented as [EthApiSpec]
|
||||
/// trait. 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).
|
||||
@ -37,18 +52,29 @@ where
|
||||
fn client(&self) -> &Arc<Client> {
|
||||
&self.inner.client
|
||||
}
|
||||
}
|
||||
|
||||
impl<Pool, Client> EthApiSpec for EthApi<Pool, Client>
|
||||
where
|
||||
Pool: TransactionPool<Transaction = Transaction> + Clone + 'static,
|
||||
Client: BlockProvider + StateProviderFactory + 'static,
|
||||
{
|
||||
/// Returns the current ethereum protocol version.
|
||||
///
|
||||
/// Note: This returns an `U64`, since this should return as hex string.
|
||||
pub fn protocol_version(&self) -> U64 {
|
||||
fn protocol_version(&self) -> U64 {
|
||||
1u64.into()
|
||||
}
|
||||
|
||||
/// Returns the best block number
|
||||
pub fn block_number(&self) -> Result<U256> {
|
||||
fn block_number(&self) -> Result<U256> {
|
||||
Ok(self.client().chain_info()?.best_number.into())
|
||||
}
|
||||
|
||||
/// Returns the chain id
|
||||
fn chain_id(&self) -> U64 {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
/// Container type `EthApi`
|
||||
|
||||
@ -16,14 +16,17 @@ use reth_rpc_types::{
|
||||
use reth_transaction_pool::TransactionPool;
|
||||
use serde_json::Value;
|
||||
|
||||
use super::EthApiSpec;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl<Pool, Client> EthApiServer for EthApi<Pool, Client>
|
||||
where
|
||||
Self: EthApiSpec,
|
||||
Pool: TransactionPool + 'static,
|
||||
Client: BlockProvider + StateProviderFactory + 'static,
|
||||
{
|
||||
fn protocol_version(&self) -> Result<U64> {
|
||||
Ok(self.protocol_version())
|
||||
Ok(EthApiSpec::protocol_version(self))
|
||||
}
|
||||
|
||||
fn syncing(&self) -> Result<SyncStatus> {
|
||||
@ -39,7 +42,7 @@ where
|
||||
}
|
||||
|
||||
fn block_number(&self) -> Result<U256> {
|
||||
self.block_number().with_message("Failed to read block number")
|
||||
EthApiSpec::block_number(self).with_message("Failed to read block number")
|
||||
}
|
||||
|
||||
async fn chain_id(&self) -> Result<Option<U64>> {
|
||||
|
||||
@ -3,5 +3,5 @@
|
||||
mod api;
|
||||
mod pubsub;
|
||||
|
||||
pub use api::EthApi;
|
||||
pub use api::{EthApi, EthApiSpec};
|
||||
pub use pubsub::EthPubSub;
|
||||
|
||||
@ -12,7 +12,9 @@
|
||||
//! Provides the implementation of all RPC interfaces.
|
||||
|
||||
mod eth;
|
||||
mod net;
|
||||
|
||||
pub use eth::{EthApi, EthPubSub};
|
||||
pub use eth::{EthApi, EthApiSpec, EthPubSub};
|
||||
pub use net::NetApi;
|
||||
|
||||
pub(crate) mod result;
|
||||
|
||||
36
crates/net/rpc/src/net/mod.rs
Normal file
36
crates/net/rpc/src/net/mod.rs
Normal file
@ -0,0 +1,36 @@
|
||||
use crate::eth::EthApiSpec;
|
||||
use jsonrpsee::core::RpcResult as Result;
|
||||
use reth_network::NetworkHandle;
|
||||
use reth_rpc_api::NetApiServer;
|
||||
use reth_rpc_types::PeerCount;
|
||||
|
||||
/// `Net` API implementation.
|
||||
///
|
||||
/// This type provides the functionality for handling `net` related requests.
|
||||
pub struct NetApi {
|
||||
/// An interface to interact with the network
|
||||
network: NetworkHandle,
|
||||
/// The implementation of `eth` API
|
||||
eth: Box<dyn EthApiSpec>,
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for NetApi {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("NetApi").finish_non_exhaustive()
|
||||
}
|
||||
}
|
||||
|
||||
/// Net rpc implementation
|
||||
impl NetApiServer for NetApi {
|
||||
fn version(&self) -> Result<String> {
|
||||
Ok(self.eth.chain_id().to_string())
|
||||
}
|
||||
|
||||
fn peer_count(&self) -> Result<PeerCount> {
|
||||
Ok(PeerCount::Hex(self.network.num_connected_peers().into()))
|
||||
}
|
||||
|
||||
fn is_listening(&self) -> Result<bool> {
|
||||
Ok(true)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user