feat: refactoring get_status() to return NetworkStatus (#997)

This commit is contained in:
Aurélien
2023-01-24 14:38:21 +01:00
committed by GitHub
parent 7905100f6d
commit 1c93b2c528
3 changed files with 26 additions and 26 deletions

View File

@@ -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::{
@@ -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.

View File

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

View File

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