feat: add for peers functions (#5351)

This commit is contained in:
Matthias Seitz
2023-11-08 15:46:07 +01:00
committed by GitHub
parent 80be039c77
commit 0e2346597b
8 changed files with 97 additions and 22 deletions

View File

@ -83,8 +83,32 @@ pub trait Peers: PeersInfo {
/// Adds a peer to the known peer set, with the given kind.
fn add_peer_kind(&self, peer: PeerId, kind: PeerKind, addr: SocketAddr);
/// Returns the rpc [PeerInfo] for all connected [PeerKind::Trusted] peers.
async fn get_trusted_peers(&self) -> Result<Vec<PeerInfo>, NetworkError> {
self.get_peers_by_kind(PeerKind::Trusted).await
}
/// Returns the rpc [PeerInfo] for all connected [PeerKind::Basic] peers.
async fn get_basic_peers(&self) -> Result<Vec<PeerInfo>, NetworkError> {
self.get_peers_by_kind(PeerKind::Basic).await
}
/// Returns the rpc [PeerInfo] for all connected peers with the given kind.
async fn get_peers_by_kind(&self, kind: PeerKind) -> Result<Vec<PeerInfo>, NetworkError>;
/// Returns the rpc [PeerInfo] for all connected peers.
async fn get_peers(&self) -> Result<Vec<PeerInfo>, NetworkError>;
async fn get_all_peers(&self) -> Result<Vec<PeerInfo>, NetworkError>;
/// Returns the rpc [PeerInfo] for the given peer id.
///
/// Returns `None` if the peer is not connected.
async fn get_peer_by_id(&self, peer_id: PeerId) -> Result<Option<PeerInfo>, NetworkError>;
/// Returns the rpc [PeerInfo] for the given peers if they are connected.
///
/// Note: This only returns peers that are connected, unconnected peers are ignored but keeping
/// the order in which they were requested.
async fn get_peers_by_id(&self, peer_ids: Vec<PeerId>) -> Result<Vec<PeerInfo>, NetworkError>;
/// Removes a peer from the peer set that corresponds to given kind.
fn remove_peer(&self, peer: PeerId, kind: PeerKind);

View File

@ -72,7 +72,19 @@ impl PeersInfo for NoopNetwork {
impl Peers for NoopNetwork {
fn add_peer_kind(&self, _peer: PeerId, _kind: PeerKind, _addr: SocketAddr) {}
async fn get_peers(&self) -> Result<Vec<PeerInfo>, NetworkError> {
async fn get_peers_by_kind(&self, _kind: PeerKind) -> Result<Vec<PeerInfo>, NetworkError> {
Ok(vec![])
}
async fn get_all_peers(&self) -> Result<Vec<PeerInfo>, NetworkError> {
Ok(vec![])
}
async fn get_peer_by_id(&self, _peer_id: PeerId) -> Result<Option<PeerInfo>, NetworkError> {
Ok(None)
}
async fn get_peers_by_id(&self, _peer_id: Vec<PeerId>) -> Result<Vec<PeerInfo>, NetworkError> {
Ok(vec![])
}