mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-13 05:51:02 +00:00
Split Peerset into PeerStore & ProtocolControllers (#13611)
This commit is contained in:
@@ -257,8 +257,8 @@ impl<B: BlockT> Protocol<B> {
|
||||
}
|
||||
|
||||
/// Returns the list of reserved peers.
|
||||
pub fn reserved_peers(&self) -> impl Iterator<Item = &PeerId> {
|
||||
self.behaviour.reserved_peers(HARDCODED_PEERSETS_SYNC)
|
||||
pub fn reserved_peers(&self, pending_response: oneshot::Sender<Vec<PeerId>>) {
|
||||
self.behaviour.reserved_peers(HARDCODED_PEERSETS_SYNC, pending_response);
|
||||
}
|
||||
|
||||
/// Adds a `PeerId` to the list of reserved peers for syncing purposes.
|
||||
@@ -310,39 +310,13 @@ impl<B: BlockT> Protocol<B> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Notify the protocol that we have learned about the existence of nodes on the default set.
|
||||
/// Notify the protocol that we have learned about the existence of some peer.
|
||||
///
|
||||
/// Can be called multiple times with the same `PeerId`s.
|
||||
pub fn add_default_set_discovered_nodes(&mut self, peer_ids: impl Iterator<Item = PeerId>) {
|
||||
for peer_id in peer_ids {
|
||||
self.peerset_handle.add_to_peers_set(HARDCODED_PEERSETS_SYNC, peer_id);
|
||||
}
|
||||
}
|
||||
|
||||
/// Add a peer to a peers set.
|
||||
pub fn add_to_peers_set(&self, protocol: ProtocolName, peer: PeerId) {
|
||||
if let Some(index) = self.notification_protocols.iter().position(|p| *p == protocol) {
|
||||
self.peerset_handle.add_to_peers_set(sc_peerset::SetId::from(index), peer);
|
||||
} else {
|
||||
error!(
|
||||
target: "sub-libp2p",
|
||||
"add_to_peers_set with unknown protocol: {}",
|
||||
protocol
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Remove a peer from a peers set.
|
||||
pub fn remove_from_peers_set(&self, protocol: ProtocolName, peer: PeerId) {
|
||||
if let Some(index) = self.notification_protocols.iter().position(|p| *p == protocol) {
|
||||
self.peerset_handle.remove_from_peers_set(sc_peerset::SetId::from(index), peer);
|
||||
} else {
|
||||
error!(
|
||||
target: "sub-libp2p",
|
||||
"remove_from_peers_set with unknown protocol: {}",
|
||||
protocol
|
||||
);
|
||||
}
|
||||
/// Can be called multiple times with the same `PeerId`.
|
||||
pub fn add_known_peer(&mut self, peer_id: PeerId) {
|
||||
// TODO: get rid of this function and call `Peerset`/`PeerStore` directly
|
||||
// from `NetworkWorker`.
|
||||
self.peerset_handle.add_known_peer(peer_id);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ use crate::{
|
||||
|
||||
use bytes::BytesMut;
|
||||
use fnv::FnvHashMap;
|
||||
use futures::prelude::*;
|
||||
use futures::{channel::oneshot, prelude::*};
|
||||
use libp2p::{
|
||||
core::{ConnectedPoint, Endpoint, Multiaddr},
|
||||
swarm::{
|
||||
@@ -35,7 +35,7 @@ use libp2p::{
|
||||
},
|
||||
PeerId,
|
||||
};
|
||||
use log::{error, trace, warn};
|
||||
use log::{debug, error, info, trace, warn};
|
||||
use parking_lot::RwLock;
|
||||
use rand::distributions::{Distribution as _, Uniform};
|
||||
use sc_peerset::DropReason;
|
||||
@@ -231,6 +231,9 @@ enum PeerState {
|
||||
/// If `Some`, any dial attempts to this peer are delayed until the given `Instant`.
|
||||
backoff_until: Option<Instant>,
|
||||
|
||||
/// Incoming index tracking this connection.
|
||||
incoming_index: sc_peerset::IncomingIndex,
|
||||
|
||||
/// List of connections with this peer, and their state.
|
||||
connections: SmallVec<[(ConnectionId, ConnectionState); crate::MAX_CONNECTIONS_PER_PEER]>,
|
||||
},
|
||||
@@ -493,7 +496,7 @@ impl Notifications {
|
||||
|
||||
// Incoming => Disabled.
|
||||
// Ongoing opening requests from the remote are rejected.
|
||||
PeerState::Incoming { mut connections, backoff_until } => {
|
||||
PeerState::Incoming { mut connections, backoff_until, .. } => {
|
||||
let inc = if let Some(inc) = self
|
||||
.incoming
|
||||
.iter_mut()
|
||||
@@ -536,8 +539,12 @@ impl Notifications {
|
||||
}
|
||||
|
||||
/// Returns the list of reserved peers.
|
||||
pub fn reserved_peers(&self, set_id: sc_peerset::SetId) -> impl Iterator<Item = &PeerId> {
|
||||
self.peerset.reserved_peers(set_id)
|
||||
pub fn reserved_peers(
|
||||
&self,
|
||||
set_id: sc_peerset::SetId,
|
||||
pending_response: oneshot::Sender<Vec<PeerId>>,
|
||||
) {
|
||||
self.peerset.reserved_peers(set_id, pending_response);
|
||||
}
|
||||
|
||||
/// Returns the state of the peerset manager, for debugging purposes.
|
||||
@@ -686,65 +693,34 @@ impl Notifications {
|
||||
};
|
||||
}
|
||||
},
|
||||
|
||||
// Incoming => Enabled
|
||||
PeerState::Incoming { mut connections, .. } => {
|
||||
trace!(target: "sub-libp2p", "PSM => Connect({}, {:?}): Enabling connections.",
|
||||
occ_entry.key().0, set_id);
|
||||
if let Some(inc) = self
|
||||
.incoming
|
||||
.iter_mut()
|
||||
.find(|i| i.peer_id == occ_entry.key().0 && i.set_id == set_id && i.alive)
|
||||
{
|
||||
inc.alive = false;
|
||||
} else {
|
||||
error!(
|
||||
target: "sub-libp2p",
|
||||
"State mismatch in libp2p: no entry in incoming for incoming peer",
|
||||
)
|
||||
}
|
||||
|
||||
debug_assert!(connections
|
||||
.iter()
|
||||
.any(|(_, s)| matches!(s, ConnectionState::OpenDesiredByRemote)));
|
||||
for (connec_id, connec_state) in connections
|
||||
.iter_mut()
|
||||
.filter(|(_, s)| matches!(s, ConnectionState::OpenDesiredByRemote))
|
||||
{
|
||||
trace!(target: "sub-libp2p", "Handler({:?}, {:?}) <= Open({:?})",
|
||||
occ_entry.key(), *connec_id, set_id);
|
||||
self.events.push_back(ToSwarm::NotifyHandler {
|
||||
peer_id: occ_entry.key().0,
|
||||
handler: NotifyHandler::One(*connec_id),
|
||||
event: NotifsHandlerIn::Open { protocol_index: set_id.into() },
|
||||
});
|
||||
*connec_state = ConnectionState::Opening;
|
||||
}
|
||||
|
||||
*occ_entry.into_mut() = PeerState::Enabled { connections };
|
||||
// Incoming => Incoming
|
||||
st @ PeerState::Incoming { .. } => {
|
||||
debug!(
|
||||
target: "sub-libp2p",
|
||||
"PSM => Connect({}, {:?}): Ignoring obsolete connect, we are awaiting accept/reject.",
|
||||
occ_entry.key().0, set_id
|
||||
);
|
||||
*occ_entry.into_mut() = st;
|
||||
},
|
||||
|
||||
// Other states are kept as-is.
|
||||
st @ PeerState::Enabled { .. } => {
|
||||
warn!(target: "sub-libp2p",
|
||||
debug!(target: "sub-libp2p",
|
||||
"PSM => Connect({}, {:?}): Already connected.",
|
||||
occ_entry.key().0, set_id);
|
||||
*occ_entry.into_mut() = st;
|
||||
debug_assert!(false);
|
||||
},
|
||||
st @ PeerState::DisabledPendingEnable { .. } => {
|
||||
warn!(target: "sub-libp2p",
|
||||
debug!(target: "sub-libp2p",
|
||||
"PSM => Connect({}, {:?}): Already pending enabling.",
|
||||
occ_entry.key().0, set_id);
|
||||
*occ_entry.into_mut() = st;
|
||||
debug_assert!(false);
|
||||
},
|
||||
st @ PeerState::Requested { .. } | st @ PeerState::PendingRequest { .. } => {
|
||||
warn!(target: "sub-libp2p",
|
||||
debug!(target: "sub-libp2p",
|
||||
"PSM => Connect({}, {:?}): Duplicate request.",
|
||||
occ_entry.key().0, set_id);
|
||||
*occ_entry.into_mut() = st;
|
||||
debug_assert!(false);
|
||||
},
|
||||
|
||||
PeerState::Poisoned => {
|
||||
@@ -847,10 +823,12 @@ impl Notifications {
|
||||
|
||||
// Invalid state transitions.
|
||||
st @ PeerState::Incoming { .. } => {
|
||||
error!(target: "sub-libp2p", "PSM => Drop({}, {:?}): Not enabled (Incoming).",
|
||||
entry.key().0, set_id);
|
||||
info!(
|
||||
target: "sub-libp2p",
|
||||
"PSM => Drop({}, {:?}): Ignoring obsolete disconnect, we are awaiting accept/reject.",
|
||||
entry.key().0, set_id,
|
||||
);
|
||||
*entry.into_mut() = st;
|
||||
debug_assert!(false);
|
||||
},
|
||||
PeerState::Poisoned => {
|
||||
error!(target: "sub-libp2p", "State of {:?} is poisoned", entry.key());
|
||||
@@ -895,7 +873,24 @@ impl Notifications {
|
||||
|
||||
match mem::replace(state, PeerState::Poisoned) {
|
||||
// Incoming => Enabled
|
||||
PeerState::Incoming { mut connections, .. } => {
|
||||
PeerState::Incoming { mut connections, incoming_index, .. } => {
|
||||
if index < incoming_index {
|
||||
warn!(
|
||||
target: "sub-libp2p",
|
||||
"PSM => Accept({:?}, {}, {:?}): Ignoring obsolete incoming index, we are already awaiting {:?}.",
|
||||
index, incoming.peer_id, incoming.set_id, incoming_index
|
||||
);
|
||||
return
|
||||
} else if index > incoming_index {
|
||||
error!(
|
||||
target: "sub-libp2p",
|
||||
"PSM => Accept({:?}, {}, {:?}): Ignoring incoming index from the future, we are awaiting {:?}.",
|
||||
index, incoming.peer_id, incoming.set_id, incoming_index
|
||||
);
|
||||
debug_assert!(false);
|
||||
return
|
||||
}
|
||||
|
||||
trace!(target: "sub-libp2p", "PSM => Accept({:?}, {}, {:?}): Enabling connections.",
|
||||
index, incoming.peer_id, incoming.set_id);
|
||||
|
||||
@@ -955,7 +950,24 @@ impl Notifications {
|
||||
|
||||
match mem::replace(state, PeerState::Poisoned) {
|
||||
// Incoming => Disabled
|
||||
PeerState::Incoming { mut connections, backoff_until } => {
|
||||
PeerState::Incoming { mut connections, backoff_until, incoming_index } => {
|
||||
if index < incoming_index {
|
||||
warn!(
|
||||
target: "sub-libp2p",
|
||||
"PSM => Reject({:?}, {}, {:?}): Ignoring obsolete incoming index, we are already awaiting {:?}.",
|
||||
index, incoming.peer_id, incoming.set_id, incoming_index
|
||||
);
|
||||
return
|
||||
} else if index > incoming_index {
|
||||
error!(
|
||||
target: "sub-libp2p",
|
||||
"PSM => Reject({:?}, {}, {:?}): Ignoring incoming index from the future, we are awaiting {:?}.",
|
||||
index, incoming.peer_id, incoming.set_id, incoming_index
|
||||
);
|
||||
debug_assert!(false);
|
||||
return
|
||||
}
|
||||
|
||||
trace!(target: "sub-libp2p", "PSM => Reject({:?}, {}, {:?}): Rejecting connections.",
|
||||
index, incoming.peer_id, incoming.set_id);
|
||||
|
||||
@@ -1195,7 +1207,7 @@ impl NetworkBehaviour for Notifications {
|
||||
},
|
||||
|
||||
// Incoming => Incoming | Disabled | Backoff | Ø
|
||||
PeerState::Incoming { mut connections, backoff_until } => {
|
||||
PeerState::Incoming { mut connections, backoff_until, incoming_index } => {
|
||||
trace!(
|
||||
target: "sub-libp2p",
|
||||
"Libp2p => Disconnected({}, {:?}, {:?}): OpenDesiredByRemote.",
|
||||
@@ -1269,8 +1281,11 @@ impl NetworkBehaviour for Notifications {
|
||||
*entry.get_mut() =
|
||||
PeerState::Disabled { connections, backoff_until };
|
||||
} else {
|
||||
*entry.get_mut() =
|
||||
PeerState::Incoming { connections, backoff_until };
|
||||
*entry.get_mut() = PeerState::Incoming {
|
||||
connections,
|
||||
backoff_until,
|
||||
incoming_index,
|
||||
};
|
||||
}
|
||||
},
|
||||
|
||||
@@ -1489,7 +1504,7 @@ impl NetworkBehaviour for Notifications {
|
||||
|
||||
match mem::replace(entry.get_mut(), PeerState::Poisoned) {
|
||||
// Incoming => Incoming
|
||||
PeerState::Incoming { mut connections, backoff_until } => {
|
||||
PeerState::Incoming { mut connections, backoff_until, incoming_index } => {
|
||||
debug_assert!(connections
|
||||
.iter()
|
||||
.any(|(_, s)| matches!(s, ConnectionState::OpenDesiredByRemote)));
|
||||
@@ -1517,7 +1532,8 @@ impl NetworkBehaviour for Notifications {
|
||||
debug_assert!(false);
|
||||
}
|
||||
|
||||
*entry.into_mut() = PeerState::Incoming { connections, backoff_until };
|
||||
*entry.into_mut() =
|
||||
PeerState::Incoming { connections, backoff_until, incoming_index };
|
||||
},
|
||||
|
||||
PeerState::Enabled { mut connections } => {
|
||||
@@ -1582,8 +1598,11 @@ impl NetworkBehaviour for Notifications {
|
||||
incoming_id,
|
||||
});
|
||||
|
||||
*entry.into_mut() =
|
||||
PeerState::Incoming { connections, backoff_until };
|
||||
*entry.into_mut() = PeerState::Incoming {
|
||||
connections,
|
||||
backoff_until,
|
||||
incoming_index: incoming_id,
|
||||
};
|
||||
} else {
|
||||
// Connections in `OpeningThenClosing` and `Closing` state can be
|
||||
// in a Closed phase, and as such can emit `OpenDesiredByRemote`
|
||||
@@ -2087,6 +2106,7 @@ mod tests {
|
||||
use super::*;
|
||||
use crate::protocol::notifications::handler::tests::*;
|
||||
use libp2p::swarm::AddressRecord;
|
||||
use sc_peerset::IncomingIndex;
|
||||
use std::{collections::HashSet, iter};
|
||||
|
||||
impl PartialEq for ConnectionState {
|
||||
@@ -2279,7 +2299,7 @@ mod tests {
|
||||
NotifsHandlerOut::OpenDesiredByRemote { protocol_index: 0 },
|
||||
);
|
||||
|
||||
if let Some(&PeerState::Incoming { ref connections, backoff_until: None }) =
|
||||
if let Some(&PeerState::Incoming { ref connections, backoff_until: None, .. }) =
|
||||
notif.peers.get(&(peer, 0.into()))
|
||||
{
|
||||
assert_eq!(connections.len(), 1);
|
||||
@@ -2424,8 +2444,10 @@ mod tests {
|
||||
NotifsHandlerOut::OpenDesiredByRemote { protocol_index: 0 },
|
||||
);
|
||||
|
||||
// attempt to connect to the peer and verify that the peer state is `Enabled`
|
||||
notif.peerset_report_connect(peer, set_id);
|
||||
// attempt to connect to the peer and verify that the peer state is `Enabled`;
|
||||
// we rely on implementation detail that incoming indices are counted from 0
|
||||
// to not mock the `Peerset`
|
||||
notif.peerset_report_accept(IncomingIndex(0));
|
||||
assert!(std::matches!(notif.peers.get(&(peer, set_id)), Some(&PeerState::Enabled { .. })));
|
||||
}
|
||||
|
||||
@@ -2502,7 +2524,9 @@ mod tests {
|
||||
conn,
|
||||
NotifsHandlerOut::OpenDesiredByRemote { protocol_index: 0 },
|
||||
);
|
||||
notif.peerset_report_connect(peer, set_id);
|
||||
// we rely on the implementation detail that incoming indices are counted from 0
|
||||
// to not mock the `Peerset`
|
||||
notif.peerset_report_accept(IncomingIndex(0));
|
||||
assert!(std::matches!(notif.peers.get(&(peer, set_id)), Some(&PeerState::Enabled { .. })));
|
||||
|
||||
// disconnect peer and verify that the state is `Disabled`
|
||||
@@ -2859,7 +2883,9 @@ mod tests {
|
||||
);
|
||||
assert!(std::matches!(notif.peers.get(&(peer, set_id)), Some(&PeerState::Incoming { .. })));
|
||||
|
||||
notif.peerset_report_connect(peer, set_id);
|
||||
// We rely on the implementation detail that incoming indices are counted
|
||||
// from 0 to not mock the `Peerset`.
|
||||
notif.peerset_report_accept(sc_peerset::IncomingIndex(0));
|
||||
assert!(std::matches!(notif.peers.get(&(peer, set_id)), Some(&PeerState::Enabled { .. })));
|
||||
|
||||
// open new substream
|
||||
@@ -2948,7 +2974,6 @@ mod tests {
|
||||
|
||||
// check peer information
|
||||
assert_eq!(notif.open_peers().collect::<Vec<_>>(), vec![&peer],);
|
||||
assert_eq!(notif.reserved_peers(set_id).collect::<Vec<_>>(), Vec::<&PeerId>::new(),);
|
||||
assert_eq!(notif.num_discovered_peers(), 0usize);
|
||||
|
||||
// close the other connection and verify that notification replacement event is emitted
|
||||
@@ -3703,7 +3728,9 @@ mod tests {
|
||||
);
|
||||
assert!(std::matches!(notif.peers.get(&(peer, set_id)), Some(&PeerState::Incoming { .. })));
|
||||
|
||||
notif.peerset_report_connect(peer, set_id);
|
||||
// we rely on the implementation detail that incoming indices are counted from 0
|
||||
// to not mock the `Peerset`
|
||||
notif.peerset_report_accept(IncomingIndex(0));
|
||||
assert!(std::matches!(notif.peers.get(&(peer, set_id)), Some(&PeerState::Enabled { .. })));
|
||||
|
||||
let event = conn_yielder.open_substream(peer, 0, connected, vec![1, 2, 3, 4]);
|
||||
@@ -3834,7 +3861,6 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
#[cfg(debug_assertions)]
|
||||
fn peerset_report_connect_with_disabled_pending_enable_peer() {
|
||||
let (mut notif, _peerset) = development_notifs();
|
||||
@@ -3872,11 +3898,15 @@ mod tests {
|
||||
Some(&PeerState::DisabledPendingEnable { .. })
|
||||
));
|
||||
|
||||
// duplicate "connect" must not change the state
|
||||
notif.peerset_report_connect(peer, set_id);
|
||||
assert!(std::matches!(
|
||||
notif.peers.get(&(peer, set_id)),
|
||||
Some(&PeerState::DisabledPendingEnable { .. })
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
#[cfg(debug_assertions)]
|
||||
fn peerset_report_connect_with_requested_peer() {
|
||||
let (mut notif, _peerset) = development_notifs();
|
||||
@@ -3887,11 +3917,12 @@ mod tests {
|
||||
notif.peerset_report_connect(peer, set_id);
|
||||
assert!(std::matches!(notif.peers.get(&(peer, set_id)), Some(&PeerState::Requested)));
|
||||
|
||||
// Duplicate "connect" must not change the state.
|
||||
notif.peerset_report_connect(peer, set_id);
|
||||
assert!(std::matches!(notif.peers.get(&(peer, set_id)), Some(&PeerState::Requested)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
#[cfg(debug_assertions)]
|
||||
fn peerset_report_connect_with_pending_requested() {
|
||||
let (mut notif, _peerset) = development_notifs();
|
||||
@@ -3940,11 +3971,50 @@ mod tests {
|
||||
Some(&PeerState::PendingRequest { .. })
|
||||
));
|
||||
|
||||
// duplicate "connect" must not change the state
|
||||
notif.peerset_report_connect(peer, set_id);
|
||||
assert!(std::matches!(
|
||||
notif.peers.get(&(peer, set_id)),
|
||||
Some(&PeerState::PendingRequest { .. })
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(debug_assertions)]
|
||||
fn peerset_report_connect_with_incoming_peer() {
|
||||
let (mut notif, _peerset) = development_notifs();
|
||||
let peer = PeerId::random();
|
||||
let set_id = sc_peerset::SetId::from(0);
|
||||
let conn = ConnectionId::new_unchecked(0);
|
||||
let connected = ConnectedPoint::Listener {
|
||||
local_addr: Multiaddr::empty(),
|
||||
send_back_addr: Multiaddr::empty(),
|
||||
};
|
||||
|
||||
notif.on_swarm_event(FromSwarm::ConnectionEstablished(
|
||||
libp2p::swarm::behaviour::ConnectionEstablished {
|
||||
peer_id: peer,
|
||||
connection_id: conn,
|
||||
endpoint: &connected,
|
||||
failed_addresses: &[],
|
||||
other_established: 0usize,
|
||||
},
|
||||
));
|
||||
assert!(std::matches!(notif.peers.get(&(peer, set_id)), Some(&PeerState::Disabled { .. })));
|
||||
|
||||
// remote opens a substream, verify that peer state is updated to `Incoming`
|
||||
notif.on_connection_handler_event(
|
||||
peer,
|
||||
conn,
|
||||
NotifsHandlerOut::OpenDesiredByRemote { protocol_index: 0 },
|
||||
);
|
||||
assert!(std::matches!(notif.peers.get(&(peer, set_id)), Some(&PeerState::Incoming { .. })));
|
||||
|
||||
notif.peerset_report_connect(peer, set_id);
|
||||
assert!(std::matches!(notif.peers.get(&(peer, set_id)), Some(&PeerState::Incoming { .. })));
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
#[cfg(debug_assertions)]
|
||||
fn peerset_report_disconnect_with_incoming_peer() {
|
||||
let (mut notif, _peerset) = development_notifs();
|
||||
@@ -3973,10 +4043,10 @@ mod tests {
|
||||
conn,
|
||||
NotifsHandlerOut::OpenDesiredByRemote { protocol_index: 0 },
|
||||
);
|
||||
|
||||
assert!(std::matches!(notif.peers.get(&(peer, set_id)), Some(&PeerState::Incoming { .. })));
|
||||
|
||||
notif.peerset_report_disconnect(peer, set_id);
|
||||
assert!(std::matches!(notif.peers.get(&(peer, set_id)), Some(&PeerState::Incoming { .. })));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -307,8 +307,20 @@ fn reconnect_after_disconnect() {
|
||||
_ => {},
|
||||
}
|
||||
|
||||
// Due to the bug in `Notifications`, the disconnected node does not always detect that
|
||||
// it was disconnected. The closed inbound substream is tolerated by design, and the
|
||||
// closed outbound substream is not detected until something is sent into it.
|
||||
// See [PR #13396](https://github.com/paritytech/substrate/pull/13396).
|
||||
// This happens if the disconnecting node reconnects to it fast enough.
|
||||
// In this case the disconnected node does not transit via `ServiceState::NotConnected`
|
||||
// and stays in `ServiceState::FirstConnec`.
|
||||
// TODO: update this once the fix is finally merged.
|
||||
if service1_state == ServiceState::ConnectedAgain &&
|
||||
service2_state == ServiceState::ConnectedAgain
|
||||
service2_state == ServiceState::ConnectedAgain ||
|
||||
service1_state == ServiceState::ConnectedAgain &&
|
||||
service2_state == ServiceState::FirstConnec ||
|
||||
service1_state == ServiceState::FirstConnec &&
|
||||
service2_state == ServiceState::ConnectedAgain
|
||||
{
|
||||
break
|
||||
}
|
||||
|
||||
@@ -620,8 +620,11 @@ where
|
||||
}
|
||||
|
||||
/// Returns the list of reserved peers.
|
||||
pub fn reserved_peers(&self) -> impl Iterator<Item = &PeerId> {
|
||||
self.network_service.behaviour().user_protocol().reserved_peers()
|
||||
fn reserved_peers(&self, pending_response: oneshot::Sender<Vec<PeerId>>) {
|
||||
self.network_service
|
||||
.behaviour()
|
||||
.user_protocol()
|
||||
.reserved_peers(pending_response);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -882,40 +885,6 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
fn add_to_peers_set(
|
||||
&self,
|
||||
protocol: ProtocolName,
|
||||
peers: HashSet<Multiaddr>,
|
||||
) -> Result<(), String> {
|
||||
let peers = self.split_multiaddr_and_peer_id(peers)?;
|
||||
|
||||
for (peer_id, addr) in peers.into_iter() {
|
||||
// Make sure the local peer ID is never added to the PSM.
|
||||
if peer_id == self.local_peer_id {
|
||||
return Err("Local peer ID cannot be added as a reserved peer.".to_string())
|
||||
}
|
||||
|
||||
if !addr.is_empty() {
|
||||
let _ = self
|
||||
.to_worker
|
||||
.unbounded_send(ServiceToWorkerMsg::AddKnownAddress(peer_id, addr));
|
||||
}
|
||||
let _ = self
|
||||
.to_worker
|
||||
.unbounded_send(ServiceToWorkerMsg::AddToPeersSet(protocol.clone(), peer_id));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn remove_from_peers_set(&self, protocol: ProtocolName, peers: Vec<PeerId>) {
|
||||
for peer_id in peers.into_iter() {
|
||||
let _ = self
|
||||
.to_worker
|
||||
.unbounded_send(ServiceToWorkerMsg::RemoveFromPeersSet(protocol.clone(), peer_id));
|
||||
}
|
||||
}
|
||||
|
||||
fn sync_num_connected(&self) -> usize {
|
||||
self.num_connected.load(Ordering::Relaxed)
|
||||
}
|
||||
@@ -1128,8 +1097,6 @@ enum ServiceToWorkerMsg {
|
||||
SetPeersetReserved(ProtocolName, HashSet<PeerId>),
|
||||
AddSetReserved(ProtocolName, PeerId),
|
||||
RemoveSetReserved(ProtocolName, PeerId),
|
||||
AddToPeersSet(ProtocolName, PeerId),
|
||||
RemoveFromPeersSet(ProtocolName, PeerId),
|
||||
EventStream(out_events::Sender),
|
||||
Request {
|
||||
target: PeerId,
|
||||
@@ -1306,16 +1273,6 @@ where
|
||||
.remove_set_reserved_peer(protocol, peer_id),
|
||||
ServiceToWorkerMsg::AddKnownAddress(peer_id, addr) =>
|
||||
self.network_service.behaviour_mut().add_known_address(peer_id, addr),
|
||||
ServiceToWorkerMsg::AddToPeersSet(protocol, peer_id) => self
|
||||
.network_service
|
||||
.behaviour_mut()
|
||||
.user_protocol_mut()
|
||||
.add_to_peers_set(protocol, peer_id),
|
||||
ServiceToWorkerMsg::RemoveFromPeersSet(protocol, peer_id) => self
|
||||
.network_service
|
||||
.behaviour_mut()
|
||||
.user_protocol_mut()
|
||||
.remove_from_peers_set(protocol, peer_id),
|
||||
ServiceToWorkerMsg::EventStream(sender) => self.event_streams.push(sender),
|
||||
ServiceToWorkerMsg::Request {
|
||||
target,
|
||||
@@ -1349,8 +1306,7 @@ where
|
||||
.user_protocol_mut()
|
||||
.set_notification_handshake(protocol, handshake),
|
||||
ServiceToWorkerMsg::ReservedPeers { pending_response } => {
|
||||
let _ =
|
||||
pending_response.send(self.reserved_peers().map(ToOwned::to_owned).collect());
|
||||
self.reserved_peers(pending_response);
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -1454,16 +1410,10 @@ where
|
||||
.behaviour_mut()
|
||||
.add_self_reported_address_to_dht(&peer_id, &protocols, addr);
|
||||
}
|
||||
self.network_service
|
||||
.behaviour_mut()
|
||||
.user_protocol_mut()
|
||||
.add_default_set_discovered_nodes(iter::once(peer_id));
|
||||
self.network_service.behaviour_mut().user_protocol_mut().add_known_peer(peer_id);
|
||||
},
|
||||
SwarmEvent::Behaviour(BehaviourOut::Discovered(peer_id)) => {
|
||||
self.network_service
|
||||
.behaviour_mut()
|
||||
.user_protocol_mut()
|
||||
.add_default_set_discovered_nodes(iter::once(peer_id));
|
||||
self.network_service.behaviour_mut().user_protocol_mut().add_known_peer(peer_id);
|
||||
},
|
||||
SwarmEvent::Behaviour(BehaviourOut::RandomKademliaStarted) => {
|
||||
if let Some(metrics) = self.metrics.as_ref() {
|
||||
|
||||
@@ -156,10 +156,6 @@ pub trait NetworkPeers {
|
||||
/// Disconnect from a node as soon as possible.
|
||||
///
|
||||
/// This triggers the same effects as if the connection had closed itself spontaneously.
|
||||
///
|
||||
/// See also [`NetworkPeers::remove_from_peers_set`], which has the same effect but also
|
||||
/// prevents the local node from re-establishing an outgoing substream to this peer until it
|
||||
/// is added again.
|
||||
fn disconnect_peer(&self, who: PeerId, protocol: ProtocolName);
|
||||
|
||||
/// Connect to unreserved peers and allow unreserved peers to connect for syncing purposes.
|
||||
@@ -216,26 +212,6 @@ pub trait NetworkPeers {
|
||||
/// Remove peers from a peer set.
|
||||
fn remove_peers_from_reserved_set(&self, protocol: ProtocolName, peers: Vec<PeerId>);
|
||||
|
||||
/// Add a peer to a set of peers.
|
||||
///
|
||||
/// If the set has slots available, it will try to open a substream with this peer.
|
||||
///
|
||||
/// Each `Multiaddr` must end with a `/p2p/` component containing the `PeerId`. It can also
|
||||
/// consist of only `/p2p/<peerid>`.
|
||||
///
|
||||
/// Returns an `Err` if one of the given addresses is invalid or contains an
|
||||
/// invalid peer ID (which includes the local peer ID).
|
||||
fn add_to_peers_set(
|
||||
&self,
|
||||
protocol: ProtocolName,
|
||||
peers: HashSet<Multiaddr>,
|
||||
) -> Result<(), String>;
|
||||
|
||||
/// Remove peers from a peer set.
|
||||
///
|
||||
/// If we currently have an open substream with this peer, it will soon be closed.
|
||||
fn remove_from_peers_set(&self, protocol: ProtocolName, peers: Vec<PeerId>);
|
||||
|
||||
/// Returns the number of peers in the sync peer set we're connected to.
|
||||
fn sync_num_connected(&self) -> usize;
|
||||
}
|
||||
@@ -259,6 +235,10 @@ where
|
||||
}
|
||||
|
||||
fn report_peer(&self, who: PeerId, cost_benefit: ReputationChange) {
|
||||
// TODO: when we get rid of `Peerset`, we'll likely need to add some kind of async
|
||||
// interface to `PeerStore`, otherwise we'll have trouble calling functions accepting
|
||||
// `&mut self` via `Arc`.
|
||||
// See https://github.com/paritytech/substrate/issues/14170.
|
||||
T::report_peer(self, who, cost_benefit)
|
||||
}
|
||||
|
||||
@@ -302,18 +282,6 @@ where
|
||||
T::remove_peers_from_reserved_set(self, protocol, peers)
|
||||
}
|
||||
|
||||
fn add_to_peers_set(
|
||||
&self,
|
||||
protocol: ProtocolName,
|
||||
peers: HashSet<Multiaddr>,
|
||||
) -> Result<(), String> {
|
||||
T::add_to_peers_set(self, protocol, peers)
|
||||
}
|
||||
|
||||
fn remove_from_peers_set(&self, protocol: ProtocolName, peers: Vec<PeerId>) {
|
||||
T::remove_from_peers_set(self, protocol, peers)
|
||||
}
|
||||
|
||||
fn sync_num_connected(&self) -> usize {
|
||||
T::sync_num_connected(self)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user