Skip to content

Commit

Permalink
Integrate chain_id and protocol_version in NetworkInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
leruaa committed Jan 25, 2023
1 parent 2ae88b0 commit 9f2d320
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 13 deletions.
3 changes: 3 additions & 0 deletions crates/interfaces/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ pub enum Error {

#[error(transparent)]
Provider(#[from] crate::provider::Error),

#[error(transparent)]
Network(#[from] reth_network_api::NetworkError),
}
2 changes: 2 additions & 0 deletions crates/net/network-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ pub trait PeersInfo: Send + Sync {
pub struct NetworkStatus {
/// The local node client version.
pub client_version: String,
/// The current ethereum protocol version
pub protocol_version: u64,
/// Information about the Ethereum Wire Protocol.
pub eth_protocol_info: EthProtocolInfo,
}
Expand Down
7 changes: 5 additions & 2 deletions crates/net/network/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ use std::{
net::SocketAddr,
pin::Pin,
sync::{
atomic::{AtomicUsize, Ordering},
atomic::{AtomicU64, AtomicUsize, Ordering},
Arc,
},
task::{Context, Poll},
Expand Down Expand Up @@ -218,6 +218,7 @@ where
peers_handle,
network_mode,
bandwidth_meter,
Arc::new(AtomicU64::new(chain_spec.chain.id())),
);

Ok(Self {
Expand Down Expand Up @@ -312,9 +313,11 @@ where
pub fn status(&self) -> NetworkStatus {
let sessions = self.swarm.sessions();
let status = sessions.status();
let hello_message = sessions.hello_message();

NetworkStatus {
client_version: sessions.hello_message().client_version,
client_version: hello_message.client_version,
protocol_version: hello_message.protocol_version as u64,
eth_protocol_info: EthProtocolInfo {
difficulty: status.total_difficulty,
head: status.blockhash,
Expand Down
12 changes: 11 additions & 1 deletion crates/net/network/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use reth_primitives::{NodeRecord, PeerId, TransactionSigned, TxHash, H256, U256}
use std::{
net::SocketAddr,
sync::{
atomic::{AtomicBool, AtomicUsize, Ordering},
atomic::{AtomicBool, AtomicU64, AtomicUsize, Ordering},
Arc,
},
};
Expand All @@ -39,6 +39,7 @@ pub struct NetworkHandle {

impl NetworkHandle {
/// Creates a single new instance.
#[allow(clippy::too_many_arguments)]
pub(crate) fn new(
num_active_peers: Arc<AtomicUsize>,
listener_address: Arc<Mutex<SocketAddr>>,
Expand All @@ -47,6 +48,7 @@ impl NetworkHandle {
peers: PeersHandle,
network_mode: NetworkMode,
bandwidth_meter: BandwidthMeter,
chain_id: Arc<AtomicU64>,
) -> Self {
let inner = NetworkInner {
num_active_peers,
Expand All @@ -57,6 +59,7 @@ impl NetworkHandle {
network_mode,
bandwidth_meter,
is_syncing: Arc::new(Default::default()),
chain_id,
};
Self { inner: Arc::new(inner) }
}
Expand Down Expand Up @@ -200,6 +203,11 @@ impl NetworkHandle {
pub fn bandwidth_meter(&self) -> &BandwidthMeter {
&self.inner.bandwidth_meter
}

/// Returns the chain id
pub fn chain_id(&self) -> u64 {
self.inner.chain_id.load(Ordering::Relaxed)
}
}

// === API Implementations ===
Expand Down Expand Up @@ -267,6 +275,8 @@ struct NetworkInner {
bandwidth_meter: BandwidthMeter,
/// Represents if the network is currently syncing.
is_syncing: Arc<AtomicBool>,
/// The chain id
chain_id: Arc<AtomicU64>,
}

/// Internal messages that can be passed to the [`NetworkManager`](crate::NetworkManager).
Expand Down
2 changes: 1 addition & 1 deletion crates/net/rpc-api/src/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use reth_rpc_types::{
pub trait EthApi {
/// Returns the protocol version encoded as a string.
#[method(name = "eth_protocolVersion")]
fn protocol_version(&self) -> Result<U64>;
async fn protocol_version(&self) -> Result<U64>;

/// Returns an object with data about the sync status or false.
#[method(name = "eth_syncing")]
Expand Down
26 changes: 19 additions & 7 deletions crates/net/rpc/src/eth/api/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
//! Provides everything related to `eth_` namespace

use async_trait::async_trait;
use reth_interfaces::Result;
use reth_network::NetworkHandle;
use reth_network_api::NetworkInfo;
use reth_primitives::U64;
use reth_provider::{BlockProvider, ChainInfo, StateProviderFactory};
use reth_rpc_types::Transaction;
Expand All @@ -12,9 +15,10 @@ mod server;
/// `Eth` API trait.
///
/// Defines core functionality of the `eth` API implementation.
#[async_trait]
pub trait EthApiSpec: Send + Sync {
/// Returns the current ethereum protocol version.
fn protocol_version(&self) -> U64;
async fn protocol_version(&self) -> Result<U64>;

/// Returns the chain id
fn chain_id(&self) -> U64;
Expand Down Expand Up @@ -43,17 +47,23 @@ where
Client: BlockProvider + StateProviderFactory + 'static,
{
/// Creates a new, shareable instance.
pub fn new(client: Arc<Client>, pool: Pool) -> Self {
let inner = EthApiInner { client, pool };
pub fn new(client: Arc<Client>, pool: Pool, network: NetworkHandle) -> Self {
let inner = EthApiInner { client, pool, network };
Self { inner: Arc::new(inner) }
}

/// Returns the inner `Client`
fn client(&self) -> &Arc<Client> {
&self.inner.client
}

/// Returns the inner `Client`
fn network(&self) -> &NetworkHandle {
&self.inner.network
}
}

#[async_trait]
impl<Pool, Client> EthApiSpec for EthApi<Pool, Client>
where
Pool: TransactionPool<Transaction = Transaction> + Clone + 'static,
Expand All @@ -62,13 +72,14 @@ where
/// Returns the current ethereum protocol version.
///
/// Note: This returns an `U64`, since this should return as hex string.
fn protocol_version(&self) -> U64 {
1u64.into()
async fn protocol_version(&self) -> Result<U64> {
let status = self.network().network_status().await?;
Ok(U64::from(status.protocol_version))
}

/// Returns the chain id
fn chain_id(&self) -> U64 {
todo!()
U64::from(self.network().chain_id())
}

/// Returns the current info for the chain
Expand All @@ -84,5 +95,6 @@ struct EthApiInner<Pool, Client> {
pool: Pool,
/// The client that can interact with the chain.
client: Arc<Client>,
// TODO needs network access to handle things like `eth_syncing`
/// An interface to interact with the network
network: NetworkHandle,
}
4 changes: 2 additions & 2 deletions crates/net/rpc/src/eth/api/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ where
Pool: TransactionPool + 'static,
Client: BlockProvider + StateProviderFactory + 'static,
{
fn protocol_version(&self) -> Result<U64> {
Ok(EthApiSpec::protocol_version(self))
async fn protocol_version(&self) -> Result<U64> {
EthApiSpec::protocol_version(self).await.to_rpc_result()
}

fn syncing(&self) -> Result<SyncStatus> {
Expand Down

0 comments on commit 9f2d320

Please sign in to comment.