Skip to content

Commit

Permalink
feat(net/peer): set rpc added peer as static (#9201)
Browse files Browse the repository at this point in the history
Signed-off-by: jsvisa <delweng@gmail.com>
  • Loading branch information
jsvisa authored Jul 1, 2024
1 parent 9cd3778 commit d4fa9de
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
11 changes: 9 additions & 2 deletions crates/net/network-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ pub trait PeersInfo: Send + Sync {
pub trait Peers: PeersInfo {
/// Adds a peer to the peer set with UDP `SocketAddr`.
fn add_peer(&self, peer: PeerId, tcp_addr: SocketAddr) {
self.add_peer_kind(peer, PeerKind::Basic, tcp_addr, None);
self.add_peer_kind(peer, PeerKind::Static, tcp_addr, None);
}

/// Adds a peer to the peer set with TCP and UDP `SocketAddr`.
fn add_peer_with_udp(&self, peer: PeerId, tcp_addr: SocketAddr, udp_addr: SocketAddr) {
self.add_peer_kind(peer, PeerKind::Basic, tcp_addr, Some(udp_addr));
self.add_peer_kind(peer, PeerKind::Static, tcp_addr, Some(udp_addr));
}

/// Adds a trusted [`PeerId`] to the peer set.
Expand Down Expand Up @@ -163,6 +163,8 @@ pub enum PeerKind {
/// Basic peer kind.
#[default]
Basic,
/// Static peer, added via JSON-RPC.
Static,
/// Trusted peer.
Trusted,
}
Expand All @@ -173,6 +175,11 @@ impl PeerKind {
matches!(self, Self::Trusted)
}

/// Returns `true` if the peer is static.
pub const fn is_static(&self) -> bool {
matches!(self, Self::Static)
}

/// Returns `true` if the peer is basic.
pub const fn is_basic(&self) -> bool {
matches!(self, Self::Basic)
Expand Down
2 changes: 1 addition & 1 deletion crates/net/network/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ where
}
}
NetworkHandleMessage::RemovePeer(peer_id, kind) => {
self.swarm.state_mut().remove_peer(peer_id, kind);
self.swarm.state_mut().remove_peer_kind(peer_id, kind);
}
NetworkHandleMessage::DisconnectPeer(peer_id, reason) => {
self.swarm.sessions_mut().disconnect(peer_id, reason);
Expand Down
5 changes: 3 additions & 2 deletions crates/net/network/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,10 @@ where
self.peers_manager.add_peer_kind(peer_id, kind, addr, None)
}

pub(crate) fn remove_peer(&mut self, peer_id: PeerId, kind: PeerKind) {
/// Removes a peer and its address with the given kind from the peerset.
pub(crate) fn remove_peer_kind(&mut self, peer_id: PeerId, kind: PeerKind) {
match kind {
PeerKind::Basic => self.peers_manager.remove_peer(peer_id),
PeerKind::Basic | PeerKind::Static => self.peers_manager.remove_peer(peer_id),
PeerKind::Trusted => self.peers_manager.remove_peer_from_trusted_set(peer_id),
}
}
Expand Down

0 comments on commit d4fa9de

Please sign in to comment.