Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement sudo_network_unstable_watch #1599

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions lib/src/network/basic_peering_strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,43 @@ where
}
}

/// Returns `true` if the peer belongs to the given chain.
///
/// Also returns `true` if the peer is banned.s
pub fn peer_in_chain(&self, peer_id: &PeerId, chain_id: &TChainId) -> bool {
let Some(&peer_id_index) = self.peer_ids_indices.get(peer_id) else {
// If the `PeerId` is unknown, it means it doesn't belong to any chain.
return false;
};

let Some(&chain_index) = self.chains_indices.get(chain_id) else {
// If the `TChainId` is unknown, it means that it doesn't have any peer.
return false;
};

self.peers_chains
.contains_key(&(peer_id_index, chain_index))
}

/// Returns the list of chains that the given peer belongs to.
///
/// Includes chains where the peer is banned.
pub fn peer_chains_unordered(
&'_ self,
peer_id: &PeerId,
) -> impl Iterator<Item = &'_ TChainId> + '_ {
let Some(&peer_id_index) = self.peer_ids_indices.get(peer_id) else {
// If the `PeerId` is unknown, it means it doesn't belong to any chain.
return either::Right(iter::empty());
};

either::Left(
self.peers_chains
.range((peer_id_index, usize::MIN)..(peer_id_index + 1, usize::MIN))
.map(|((_, chain_index), _)| &self.chains[*chain_index]),
)
}

/// Returns the list of all addresses that have been inserted for the given peer.
pub fn peer_addresses(&'_ self, peer_id: &PeerId) -> impl Iterator<Item = &'_ [u8]> + '_ {
let Some(&peer_id_index) = self.peer_ids_indices.get(peer_id) else {
Expand Down
47 changes: 47 additions & 0 deletions lib/src/network/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1118,6 +1118,53 @@ where
&self.inner[id].address
}

/// Returns the list of fully-established connections (i.e. whose handshake has finished)
/// with the given peer.
pub fn established_connections(
&'_ self,
peer_id: &PeerId,
) -> impl Iterator<Item = ConnectionId> + '_ {
let Some(&peer_index) = self.peers_by_peer_id.get(peer_id) else {
return either::Right(iter::empty());
};

either::Left(
self.connections_by_peer_id
.range(
(peer_index, ConnectionId::min_value())
..=(peer_index, ConnectionId::max_value()),
)
.filter(|(_, connection_id)| {
// TODO: what about shutting_down?
self.inner.connection_state(*connection_id).established
})
.map(|&(_, connection_id)| connection_id),
)
}

/// Returns the list of connections that are still in their handshaking phase but are expected
/// to reach the given peer.
pub fn potential_connections(
&'_ self,
peer_id: &PeerId,
) -> impl Iterator<Item = ConnectionId> + '_ {
let Some(&peer_index) = self.peers_by_peer_id.get(peer_id) else {
return either::Right(iter::empty());
};

either::Left(
self.connections_by_peer_id
.range(
(peer_index, ConnectionId::min_value())
..=(peer_index, ConnectionId::max_value()),
)
.filter(|(_, connection_id)| {
!self.inner.connection_state(*connection_id).established
})
.map(|&(_, connection_id)| connection_id),
)
}

/// Returns the number of connections with the given peer.
///
/// Both connections that have and have not finished their handshaking phase are considered.
Expand Down
Loading
Loading