Skip to content

Commit

Permalink
feat: refactoring get_status() to return NetworkStatus (paradigmxyz#997)
Browse files Browse the repository at this point in the history
  • Loading branch information
leruaa authored and literallymarvellous committed Feb 5, 2023
1 parent 631bd45 commit 5105b4d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 26 deletions.
18 changes: 15 additions & 3 deletions crates/net/network/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ use reth_eth_wire::{
DisconnectReason, Status,
};
use reth_net_common::bandwidth_meter::BandwidthMeter;
use reth_network_api::{EthProtocolInfo, NetworkStatus};
use reth_primitives::{PeerId, H256};
use reth_provider::BlockProvider;
use std::{
Expand Down Expand Up @@ -307,9 +308,20 @@ where
self.swarm.state().fetch_client()
}

/// Returns the current [`Status`] for the local node.
pub fn status(&self) -> Status {
self.swarm.sessions().status()
/// Returns the current [`NetworkStatus`] for the local node.
pub fn status(&self) -> NetworkStatus {
let sessions = self.swarm.sessions();
let status = sessions.status();

NetworkStatus {
client_version: sessions.hello_message().client_version,
eth_protocol_info: EthProtocolInfo {
difficulty: status.total_difficulty,
head: status.blockhash,
network: status.chain.id(),
genesis: status.genesis,
},
}
}

/// Event hook for an unexpected message from the peer.
Expand Down
29 changes: 6 additions & 23 deletions crates/net/network/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@ use crate::{
};
use async_trait::async_trait;
use parking_lot::Mutex;
use reth_eth_wire::{
DisconnectReason, NewBlock, NewPooledTransactionHashes, SharedTransactions, Status,
};
use reth_eth_wire::{DisconnectReason, NewBlock, NewPooledTransactionHashes, SharedTransactions};
use reth_interfaces::{
p2p::headers::client::StatusUpdater,
sync::{SyncState, SyncStateProvider, SyncStateUpdater},
};
use reth_net_common::bandwidth_meter::BandwidthMeter;
use reth_network_api::{EthProtocolInfo, NetworkError, NetworkInfo, NetworkStatus, PeersInfo};
use reth_network_api::{NetworkError, NetworkInfo, NetworkStatus, PeersInfo};
use reth_primitives::{NodeRecord, PeerId, TransactionSigned, TxHash, H256, U256};
use std::{
net::SocketAddr,
Expand Down Expand Up @@ -129,13 +127,6 @@ impl NetworkHandle {
self.send_message(NetworkHandleMessage::StatusUpdate { height, hash, total_difficulty });
}

/// Get the current status of the node.
pub async fn get_status(&self) -> Result<Status, oneshot::error::RecvError> {
let (tx, rx) = oneshot::channel();
let _ = self.manager().send(NetworkHandleMessage::GetStatus(tx));
rx.await
}

/// Announce a block over devp2p
///
/// Caution: in PoS this is a noop, since new block propagation will happen over devp2p
Expand Down Expand Up @@ -232,17 +223,9 @@ impl NetworkInfo for NetworkHandle {
}

async fn network_status(&self) -> Result<NetworkStatus, NetworkError> {
let status = self.get_status().await?;

Ok(NetworkStatus {
client_version: "Reth".to_string(),
eth_protocol_info: EthProtocolInfo {
difficulty: status.total_difficulty,
head: status.blockhash,
network: status.chain.id(),
genesis: status.genesis,
},
})
let (tx, rx) = oneshot::channel();
let _ = self.manager().send(NetworkHandleMessage::GetStatus(tx));
rx.await.map_err(Into::into)
}
}

Expand Down Expand Up @@ -317,7 +300,7 @@ pub(crate) enum NetworkHandleMessage {
/// Apply a status update.
StatusUpdate { height: u64, hash: H256, total_difficulty: U256 },
/// Get the currenet status
GetStatus(oneshot::Sender<Status>),
GetStatus(oneshot::Sender<NetworkStatus>),
/// Get PeerInfo from all the peers
GetPeerInfo(oneshot::Sender<Vec<PeerInfo>>),
/// Get PeerInfo for a specific peer
Expand Down
5 changes: 5 additions & 0 deletions crates/net/network/src/session/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ impl SessionManager {
self.status
}

/// Returns the session hello message.
pub(crate) fn hello_message(&self) -> HelloMessage {
self.hello_message.clone()
}

/// Spawns the given future onto a new task that is tracked in the `spawned_tasks` [`JoinSet`].
fn spawn<F>(&self, f: F)
where
Expand Down

0 comments on commit 5105b4d

Please sign in to comment.