use crate::protocol::notifications::handler::{
self, NotificationsSink, NotifsHandlerIn, NotifsHandlerOut, NotifsHandlerProto,
};
use bytes::BytesMut;
use fnv::FnvHashMap;
use futures::prelude::*;
use libp2p::{
core::{connection::ConnectionId, Multiaddr, PeerId},
swarm::{
behaviour::{ConnectionClosed, ConnectionEstablished, DialFailure, FromSwarm},
handler::ConnectionHandler,
DialError, IntoConnectionHandler, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler,
PollParameters,
},
};
use log::{error, trace, warn};
use parking_lot::RwLock;
use rand::distributions::{Distribution as _, Uniform};
use sc_network_common::protocol::ProtocolName;
use sc_peerset::DropReason;
use smallvec::SmallVec;
use std::{
cmp,
collections::{hash_map::Entry, VecDeque},
mem,
pin::Pin,
sync::Arc,
task::{Context, Poll},
time::{Duration, Instant},
};
pub struct Notifications {
notif_protocols: Vec<handler::ProtocolConfig>,
peerset: sc_peerset::Peerset,
peers: FnvHashMap<(PeerId, sc_peerset::SetId), PeerState>,
delays: stream::FuturesUnordered<
Pin<Box<dyn Future<Output = (DelayId, PeerId, sc_peerset::SetId)> + Send>>,
>,
next_delay_id: DelayId,
incoming: SmallVec<[IncomingPeer; 6]>,
next_incoming_index: sc_peerset::IncomingIndex,
events: VecDeque<NetworkBehaviourAction<NotificationsOut, NotifsHandlerProto>>,
}
#[derive(Debug, Clone)]
pub struct ProtocolConfig {
pub name: ProtocolName,
pub fallback_names: Vec<ProtocolName>,
pub handshake: Vec<u8>,
pub max_notification_size: u64,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
struct DelayId(u64);
#[derive(Debug)]
enum PeerState {
Poisoned,
Backoff {
timer: DelayId,
timer_deadline: Instant,
},
PendingRequest {
timer: DelayId,
timer_deadline: Instant,
},
Requested,
Disabled {
backoff_until: Option<Instant>,
connections: SmallVec<[(ConnectionId, ConnectionState); crate::MAX_CONNECTIONS_PER_PEER]>,
},
DisabledPendingEnable {
timer: DelayId,
timer_deadline: Instant,
connections: SmallVec<[(ConnectionId, ConnectionState); crate::MAX_CONNECTIONS_PER_PEER]>,
},
Enabled {
connections: SmallVec<[(ConnectionId, ConnectionState); crate::MAX_CONNECTIONS_PER_PEER]>,
},
Incoming {
backoff_until: Option<Instant>,
connections: SmallVec<[(ConnectionId, ConnectionState); crate::MAX_CONNECTIONS_PER_PEER]>,
},
}
impl PeerState {
fn is_open(&self) -> bool {
self.get_open().is_some()
}
fn get_open(&self) -> Option<&NotificationsSink> {
match self {
Self::Enabled { connections, .. } => connections.iter().find_map(|(_, s)| match s {
ConnectionState::Open(s) => Some(s),
_ => None,
}),
_ => None,
}
}
}
#[derive(Debug)]
enum ConnectionState {
Closed,
Closing,
Opening,
OpeningThenClosing,
OpenDesiredByRemote,
Open(NotificationsSink),
}
#[derive(Debug)]
struct IncomingPeer {
peer_id: PeerId,
set_id: sc_peerset::SetId,
alive: bool,
incoming_id: sc_peerset::IncomingIndex,
}
#[derive(Debug)]
pub enum NotificationsOut {
CustomProtocolOpen {
peer_id: PeerId,
set_id: sc_peerset::SetId,
negotiated_fallback: Option<ProtocolName>,
received_handshake: Vec<u8>,
notifications_sink: NotificationsSink,
},
CustomProtocolReplaced {
peer_id: PeerId,
set_id: sc_peerset::SetId,
notifications_sink: NotificationsSink,
},
CustomProtocolClosed {
peer_id: PeerId,
set_id: sc_peerset::SetId,
},
Notification {
peer_id: PeerId,
set_id: sc_peerset::SetId,
message: BytesMut,
},
}
impl Notifications {
pub fn new(
peerset: sc_peerset::Peerset,
notif_protocols: impl Iterator<Item = ProtocolConfig>,
) -> Self {
let notif_protocols = notif_protocols
.map(|cfg| handler::ProtocolConfig {
name: cfg.name,
fallback_names: cfg.fallback_names,
handshake: Arc::new(RwLock::new(cfg.handshake)),
max_notification_size: cfg.max_notification_size,
})
.collect::<Vec<_>>();
assert!(!notif_protocols.is_empty());
Self {
notif_protocols,
peerset,
peers: FnvHashMap::default(),
delays: Default::default(),
next_delay_id: DelayId(0),
incoming: SmallVec::new(),
next_incoming_index: sc_peerset::IncomingIndex(0),
events: VecDeque::new(),
}
}
pub fn set_notif_protocol_handshake(
&mut self,
set_id: sc_peerset::SetId,
handshake_message: impl Into<Vec<u8>>,
) {
if let Some(p) = self.notif_protocols.get_mut(usize::from(set_id)) {
*p.handshake.write() = handshake_message.into();
} else {
log::error!(target: "sub-libp2p", "Unknown handshake change set: {:?}", set_id);
debug_assert!(false);
}
}
pub fn num_discovered_peers(&self) -> usize {
self.peerset.num_discovered_peers()
}
pub fn open_peers(&self) -> impl Iterator<Item = &PeerId> {
self.peers.iter().filter(|(_, state)| state.is_open()).map(|((id, _), _)| id)
}
pub fn is_open(&self, peer_id: &PeerId, set_id: sc_peerset::SetId) -> bool {
self.peers.get(&(*peer_id, set_id)).map(|p| p.is_open()).unwrap_or(false)
}
pub fn disconnect_peer(&mut self, peer_id: &PeerId, set_id: sc_peerset::SetId) {
trace!(target: "sub-libp2p", "External API => Disconnect({}, {:?})", peer_id, set_id);
self.disconnect_peer_inner(peer_id, set_id, None);
}
fn disconnect_peer_inner(
&mut self,
peer_id: &PeerId,
set_id: sc_peerset::SetId,
ban: Option<Duration>,
) {
let mut entry = if let Entry::Occupied(entry) = self.peers.entry((*peer_id, set_id)) {
entry
} else {
return
};
match mem::replace(entry.get_mut(), PeerState::Poisoned) {
st @ PeerState::Disabled { .. } => *entry.into_mut() = st,
st @ PeerState::Requested => *entry.into_mut() = st,
st @ PeerState::PendingRequest { .. } => *entry.into_mut() = st,
st @ PeerState::Backoff { .. } => *entry.into_mut() = st,
PeerState::DisabledPendingEnable { connections, timer_deadline, timer: _ } => {
trace!(target: "sub-libp2p", "PSM <= Dropped({}, {:?})", peer_id, set_id);
self.peerset.dropped(set_id, *peer_id, DropReason::Unknown);
let backoff_until = Some(if let Some(ban) = ban {
cmp::max(timer_deadline, Instant::now() + ban)
} else {
timer_deadline
});
*entry.into_mut() = PeerState::Disabled { connections, backoff_until }
},
PeerState::Enabled { mut connections } => {
trace!(target: "sub-libp2p", "PSM <= Dropped({}, {:?})", peer_id, set_id);
self.peerset.dropped(set_id, *peer_id, DropReason::Unknown);
if connections.iter().any(|(_, s)| matches!(s, ConnectionState::Open(_))) {
trace!(target: "sub-libp2p", "External API <= Closed({}, {:?})", peer_id, set_id);
let event =
NotificationsOut::CustomProtocolClosed { peer_id: *peer_id, set_id };
self.events.push_back(NetworkBehaviourAction::GenerateEvent(event));
}
for (connec_id, connec_state) in
connections.iter_mut().filter(|(_, s)| matches!(s, ConnectionState::Open(_)))
{
trace!(target: "sub-libp2p", "Handler({:?}, {:?}) <= Close({:?})", peer_id, *connec_id, set_id);
self.events.push_back(NetworkBehaviourAction::NotifyHandler {
peer_id: *peer_id,
handler: NotifyHandler::One(*connec_id),
event: NotifsHandlerIn::Close { protocol_index: set_id.into() },
});
*connec_state = ConnectionState::Closing;
}
for (connec_id, connec_state) in
connections.iter_mut().filter(|(_, s)| matches!(s, ConnectionState::Opening))
{
trace!(target: "sub-libp2p", "Handler({:?}, {:?}) <= Close({:?})", peer_id, *connec_id, set_id);
self.events.push_back(NetworkBehaviourAction::NotifyHandler {
peer_id: *peer_id,
handler: NotifyHandler::One(*connec_id),
event: NotifsHandlerIn::Close { protocol_index: set_id.into() },
});
*connec_state = ConnectionState::OpeningThenClosing;
}
debug_assert!(!connections
.iter()
.any(|(_, s)| matches!(s, ConnectionState::Open(_))));
debug_assert!(!connections
.iter()
.any(|(_, s)| matches!(s, ConnectionState::Opening)));
let backoff_until = ban.map(|dur| Instant::now() + dur);
*entry.into_mut() = PeerState::Disabled { connections, backoff_until }
},
PeerState::Incoming { mut connections, backoff_until } => {
let inc = if let Some(inc) = self
.incoming
.iter_mut()
.find(|i| i.peer_id == entry.key().0 && i.set_id == set_id && i.alive)
{
inc
} else {
error!(
target: "sub-libp2p",
"State mismatch in libp2p: no entry in incoming for incoming peer"
);
return
};
inc.alive = false;
for (connec_id, connec_state) in connections
.iter_mut()
.filter(|(_, s)| matches!(s, ConnectionState::OpenDesiredByRemote))
{
trace!(target: "sub-libp2p", "Handler({:?}, {:?}) <= Close({:?})", peer_id, *connec_id, set_id);
self.events.push_back(NetworkBehaviourAction::NotifyHandler {
peer_id: *peer_id,
handler: NotifyHandler::One(*connec_id),
event: NotifsHandlerIn::Close { protocol_index: set_id.into() },
});
*connec_state = ConnectionState::Closing;
}
let backoff_until = match (backoff_until, ban) {
(Some(a), Some(b)) => Some(cmp::max(a, Instant::now() + b)),
(Some(a), None) => Some(a),
(None, Some(b)) => Some(Instant::now() + b),
(None, None) => None,
};
debug_assert!(!connections
.iter()
.any(|(_, s)| matches!(s, ConnectionState::OpenDesiredByRemote)));
*entry.into_mut() = PeerState::Disabled { connections, backoff_until }
},
PeerState::Poisoned => {
error!(target: "sub-libp2p", "State of {:?} is poisoned", peer_id)
},
}
}
pub fn reserved_peers(&self, set_id: sc_peerset::SetId) -> impl Iterator<Item = &PeerId> {
self.peerset.reserved_peers(set_id)
}
pub fn write_notification(
&mut self,
target: &PeerId,
set_id: sc_peerset::SetId,
message: impl Into<Vec<u8>>,
) {
let notifs_sink = match self.peers.get(&(*target, set_id)).and_then(|p| p.get_open()) {
None => {
trace!(
target: "sub-libp2p",
"Tried to sent notification to {:?} without an open channel.",
target,
);
return
},
Some(sink) => sink,
};
let message = message.into();
trace!(
target: "sub-libp2p",
"External API => Notification({:?}, {:?}, {} bytes)",
target,
set_id,
message.len(),
);
trace!(target: "sub-libp2p", "Handler({:?}) <= Sync notification", target);
notifs_sink.send_sync_notification(message);
}
pub fn peerset_debug_info(&mut self) -> serde_json::Value {
self.peerset.debug_info()
}
fn peerset_report_connect(&mut self, peer_id: PeerId, set_id: sc_peerset::SetId) {
let handler = self.new_handler();
let mut occ_entry = match self.peers.entry((peer_id, set_id)) {
Entry::Occupied(entry) => entry,
Entry::Vacant(entry) => {
trace!(
target: "sub-libp2p",
"PSM => Connect({}, {:?}): Starting to connect",
entry.key().0,
set_id,
);
trace!(target: "sub-libp2p", "Libp2p <= Dial {}", entry.key().0);
self.events.push_back(NetworkBehaviourAction::Dial {
opts: entry.key().0.into(),
handler,
});
entry.insert(PeerState::Requested);
return
},
};
let now = Instant::now();
match mem::replace(occ_entry.get_mut(), PeerState::Poisoned) {
PeerState::Backoff { ref timer, ref timer_deadline } if *timer_deadline > now => {
let peer_id = occ_entry.key().0;
trace!(
target: "sub-libp2p",
"PSM => Connect({}, {:?}): Will start to connect at until {:?}",
peer_id,
set_id,
timer_deadline,
);
*occ_entry.into_mut() =
PeerState::PendingRequest { timer: *timer, timer_deadline: *timer_deadline };
},
PeerState::Backoff { .. } => {
trace!(
target: "sub-libp2p",
"PSM => Connect({}, {:?}): Starting to connect",
occ_entry.key().0,
set_id,
);
trace!(target: "sub-libp2p", "Libp2p <= Dial {:?}", occ_entry.key());
self.events.push_back(NetworkBehaviourAction::Dial {
opts: occ_entry.key().0.into(),
handler,
});
*occ_entry.into_mut() = PeerState::Requested;
},
PeerState::Disabled { connections, backoff_until: Some(ref backoff) }
if *backoff > now =>
{
let peer_id = occ_entry.key().0;
trace!(
target: "sub-libp2p",
"PSM => Connect({}, {:?}): But peer is backed-off until {:?}",
peer_id,
set_id,
backoff,
);
let delay_id = self.next_delay_id;
self.next_delay_id.0 += 1;
let delay = futures_timer::Delay::new(*backoff - now);
self.delays.push(
async move {
delay.await;
(delay_id, peer_id, set_id)
}
.boxed(),
);
*occ_entry.into_mut() = PeerState::DisabledPendingEnable {
connections,
timer: delay_id,
timer_deadline: *backoff,
};
},
PeerState::Disabled { mut connections, backoff_until } => {
debug_assert!(!connections
.iter()
.any(|(_, s)| { matches!(s, ConnectionState::Open(_)) }));
if let Some((connec_id, connec_state)) =
connections.iter_mut().find(|(_, s)| matches!(s, ConnectionState::Closed))
{
trace!(target: "sub-libp2p", "PSM => Connect({}, {:?}): Enabling connections.",
occ_entry.key().0, set_id);
trace!(target: "sub-libp2p", "Handler({:?}, {:?}) <= Open({:?})", peer_id, *connec_id, set_id);
self.events.push_back(NetworkBehaviourAction::NotifyHandler {
peer_id,
handler: NotifyHandler::One(*connec_id),
event: NotifsHandlerIn::Open { protocol_index: set_id.into() },
});
*connec_state = ConnectionState::Opening;
*occ_entry.into_mut() = PeerState::Enabled { connections };
} else {
debug_assert!(connections.iter().any(|(_, s)| {
matches!(s, ConnectionState::OpeningThenClosing | ConnectionState::Closing)
}));
trace!(
target: "sub-libp2p",
"PSM => Connect({}, {:?}): No connection in proper state. Delaying.",
occ_entry.key().0, set_id
);
let timer_deadline = {
let base = now + Duration::from_secs(5);
if let Some(backoff_until) = backoff_until {
cmp::max(base, backoff_until)
} else {
base
}
};
let delay_id = self.next_delay_id;
self.next_delay_id.0 += 1;
debug_assert!(timer_deadline > now);
let delay = futures_timer::Delay::new(timer_deadline - now);
self.delays.push(
async move {
delay.await;
(delay_id, peer_id, set_id)
}
.boxed(),
);
*occ_entry.into_mut() = PeerState::DisabledPendingEnable {
connections,
timer: delay_id,
timer_deadline,
};
}
},
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(NetworkBehaviourAction::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 };
},
st @ PeerState::Enabled { .. } => {
warn!(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",
"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",
"PSM => Connect({}, {:?}): Duplicate request.",
occ_entry.key().0, set_id);
*occ_entry.into_mut() = st;
debug_assert!(false);
},
PeerState::Poisoned => {
error!(target: "sub-libp2p", "State of {:?} is poisoned", occ_entry.key());
debug_assert!(false);
},
}
}
fn peerset_report_disconnect(&mut self, peer_id: PeerId, set_id: sc_peerset::SetId) {
let mut entry = match self.peers.entry((peer_id, set_id)) {
Entry::Occupied(entry) => entry,
Entry::Vacant(entry) => {
trace!(target: "sub-libp2p", "PSM => Drop({}, {:?}): Already disabled.",
entry.key().0, set_id);
return
},
};
match mem::replace(entry.get_mut(), PeerState::Poisoned) {
st @ PeerState::Disabled { .. } | st @ PeerState::Backoff { .. } => {
trace!(target: "sub-libp2p", "PSM => Drop({}, {:?}): Already disabled.",
entry.key().0, set_id);
*entry.into_mut() = st;
},
PeerState::DisabledPendingEnable { connections, timer_deadline, timer: _ } => {
debug_assert!(!connections.is_empty());
trace!(target: "sub-libp2p",
"PSM => Drop({}, {:?}): Interrupting pending enabling.",
entry.key().0, set_id);
*entry.into_mut() =
PeerState::Disabled { connections, backoff_until: Some(timer_deadline) };
},
PeerState::Enabled { mut connections } => {
trace!(target: "sub-libp2p", "PSM => Drop({}, {:?}): Disabling connections.",
entry.key().0, set_id);
debug_assert!(connections.iter().any(|(_, s)| matches!(
s,
ConnectionState::Opening | ConnectionState::Open(_)
)));
if connections.iter().any(|(_, s)| matches!(s, ConnectionState::Open(_))) {
trace!(target: "sub-libp2p", "External API <= Closed({}, {:?})", entry.key().0, set_id);
let event =
NotificationsOut::CustomProtocolClosed { peer_id: entry.key().0, set_id };
self.events.push_back(NetworkBehaviourAction::GenerateEvent(event));
}
for (connec_id, connec_state) in
connections.iter_mut().filter(|(_, s)| matches!(s, ConnectionState::Opening))
{
trace!(target: "sub-libp2p", "Handler({:?}, {:?}) <= Close({:?})",
entry.key(), *connec_id, set_id);
self.events.push_back(NetworkBehaviourAction::NotifyHandler {
peer_id: entry.key().0,
handler: NotifyHandler::One(*connec_id),
event: NotifsHandlerIn::Close { protocol_index: set_id.into() },
});
*connec_state = ConnectionState::OpeningThenClosing;
}
for (connec_id, connec_state) in
connections.iter_mut().filter(|(_, s)| matches!(s, ConnectionState::Open(_)))
{
trace!(target: "sub-libp2p", "Handler({:?}, {:?}) <= Close({:?})",
entry.key(), *connec_id, set_id);
self.events.push_back(NetworkBehaviourAction::NotifyHandler {
peer_id: entry.key().0,
handler: NotifyHandler::One(*connec_id),
event: NotifsHandlerIn::Close { protocol_index: set_id.into() },
});
*connec_state = ConnectionState::Closing;
}
*entry.into_mut() = PeerState::Disabled { connections, backoff_until: None }
},
PeerState::Requested => {
trace!(target: "sub-libp2p", "PSM => Drop({}, {:?}): Not yet connected.",
entry.key().0, set_id);
entry.remove();
},
PeerState::PendingRequest { timer, timer_deadline } => {
trace!(target: "sub-libp2p", "PSM => Drop({}, {:?}): Not yet connected",
entry.key().0, set_id);
*entry.into_mut() = PeerState::Backoff { timer, timer_deadline }
},
st @ PeerState::Incoming { .. } => {
error!(target: "sub-libp2p", "PSM => Drop({}, {:?}): Not enabled (Incoming).",
entry.key().0, set_id);
*entry.into_mut() = st;
debug_assert!(false);
},
PeerState::Poisoned => {
error!(target: "sub-libp2p", "State of {:?} is poisoned", entry.key());
debug_assert!(false);
},
}
}
fn peerset_report_accept(&mut self, index: sc_peerset::IncomingIndex) {
let incoming = if let Some(pos) = self.incoming.iter().position(|i| i.incoming_id == index)
{
self.incoming.remove(pos)
} else {
error!(target: "sub-libp2p", "PSM => Accept({:?}): Invalid index", index);
return
};
if !incoming.alive {
trace!(target: "sub-libp2p", "PSM => Accept({:?}, {}, {:?}): Obsolete incoming",
index, incoming.peer_id, incoming.set_id);
match self.peers.get_mut(&(incoming.peer_id, incoming.set_id)) {
Some(PeerState::DisabledPendingEnable { .. }) | Some(PeerState::Enabled { .. }) => {
},
_ => {
trace!(target: "sub-libp2p", "PSM <= Dropped({}, {:?})",
incoming.peer_id, incoming.set_id);
self.peerset.dropped(incoming.set_id, incoming.peer_id, DropReason::Unknown);
},
}
return
}
let state = match self.peers.get_mut(&(incoming.peer_id, incoming.set_id)) {
Some(s) => s,
None => {
debug_assert!(false);
return
},
};
match mem::replace(state, PeerState::Poisoned) {
PeerState::Incoming { mut connections, .. } => {
trace!(target: "sub-libp2p", "PSM => Accept({:?}, {}, {:?}): Enabling connections.",
index, incoming.peer_id, incoming.set_id);
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({:?})",
incoming.peer_id, *connec_id, incoming.set_id);
self.events.push_back(NetworkBehaviourAction::NotifyHandler {
peer_id: incoming.peer_id,
handler: NotifyHandler::One(*connec_id),
event: NotifsHandlerIn::Open { protocol_index: incoming.set_id.into() },
});
*connec_state = ConnectionState::Opening;
}
*state = PeerState::Enabled { connections };
},
peer => {
error!(target: "sub-libp2p",
"State mismatch in libp2p: Expected alive incoming. Got {:?}.",
peer);
debug_assert!(false);
},
}
}
fn peerset_report_reject(&mut self, index: sc_peerset::IncomingIndex) {
let incoming = if let Some(pos) = self.incoming.iter().position(|i| i.incoming_id == index)
{
self.incoming.remove(pos)
} else {
error!(target: "sub-libp2p", "PSM => Reject({:?}): Invalid index", index);
return
};
if !incoming.alive {
trace!(target: "sub-libp2p", "PSM => Reject({:?}, {}, {:?}): Obsolete incoming, \
ignoring", index, incoming.peer_id, incoming.set_id);
return
}
let state = match self.peers.get_mut(&(incoming.peer_id, incoming.set_id)) {
Some(s) => s,
None => {
debug_assert!(false);
return
},
};
match mem::replace(state, PeerState::Poisoned) {
PeerState::Incoming { mut connections, backoff_until } => {
trace!(target: "sub-libp2p", "PSM => Reject({:?}, {}, {:?}): Rejecting connections.",
index, incoming.peer_id, incoming.set_id);
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({:?}, {:?}) <= Close({:?})",
incoming.peer_id, connec_id, incoming.set_id);
self.events.push_back(NetworkBehaviourAction::NotifyHandler {
peer_id: incoming.peer_id,
handler: NotifyHandler::One(*connec_id),
event: NotifsHandlerIn::Close { protocol_index: incoming.set_id.into() },
});
*connec_state = ConnectionState::Closing;
}
*state = PeerState::Disabled { connections, backoff_until };
},
peer => error!(target: "sub-libp2p",
"State mismatch in libp2p: Expected alive incoming. Got {:?}.",
peer),
}
}
}
impl NetworkBehaviour for Notifications {
type ConnectionHandler = NotifsHandlerProto;
type OutEvent = NotificationsOut;
fn new_handler(&mut self) -> Self::ConnectionHandler {
NotifsHandlerProto::new(self.notif_protocols.clone())
}
fn addresses_of_peer(&mut self, _: &PeerId) -> Vec<Multiaddr> {
Vec::new()
}
fn on_swarm_event(&mut self, event: FromSwarm<Self::ConnectionHandler>) {
match event {
FromSwarm::ConnectionEstablished(ConnectionEstablished {
peer_id,
endpoint,
connection_id,
..
}) => {
for set_id in (0..self.notif_protocols.len()).map(sc_peerset::SetId::from) {
match self.peers.entry((peer_id, set_id)).or_insert(PeerState::Poisoned) {
st @ &mut PeerState::Requested |
st @ &mut PeerState::PendingRequest { .. } => {
trace!(target: "sub-libp2p",
"Libp2p => Connected({}, {:?}, {:?}): Connection was requested by PSM.",
peer_id, set_id, endpoint
);
trace!(target: "sub-libp2p", "Handler({:?}, {:?}) <= Open({:?})", peer_id, connection_id, set_id);
self.events.push_back(NetworkBehaviourAction::NotifyHandler {
peer_id,
handler: NotifyHandler::One(connection_id),
event: NotifsHandlerIn::Open { protocol_index: set_id.into() },
});
let mut connections = SmallVec::new();
connections.push((connection_id, ConnectionState::Opening));
*st = PeerState::Enabled { connections };
},
st @ &mut PeerState::Poisoned | st @ &mut PeerState::Backoff { .. } => {
let backoff_until =
if let PeerState::Backoff { timer_deadline, .. } = st {
Some(*timer_deadline)
} else {
None
};
trace!(target: "sub-libp2p",
"Libp2p => Connected({}, {:?}, {:?}, {:?}): Not requested by PSM, disabling.",
peer_id, set_id, endpoint, connection_id);
let mut connections = SmallVec::new();
connections.push((connection_id, ConnectionState::Closed));
*st = PeerState::Disabled { connections, backoff_until };
},
PeerState::Incoming { connections, .. } |
PeerState::Disabled { connections, .. } |
PeerState::DisabledPendingEnable { connections, .. } |
PeerState::Enabled { connections, .. } => {
trace!(target: "sub-libp2p",
"Libp2p => Connected({}, {:?}, {:?}, {:?}): Secondary connection. Leaving closed.",
peer_id, set_id, endpoint, connection_id);
connections.push((connection_id, ConnectionState::Closed));
},
}
}
},
FromSwarm::ConnectionClosed(ConnectionClosed { peer_id, connection_id, .. }) => {
for set_id in (0..self.notif_protocols.len()).map(sc_peerset::SetId::from) {
let mut entry = if let Entry::Occupied(entry) =
self.peers.entry((peer_id, set_id))
{
entry
} else {
error!(target: "sub-libp2p", "inject_connection_closed: State mismatch in the custom protos handler");
debug_assert!(false);
return
};
match mem::replace(entry.get_mut(), PeerState::Poisoned) {
PeerState::Disabled { mut connections, backoff_until } => {
trace!(target: "sub-libp2p", "Libp2p => Disconnected({}, {:?}, {:?}): Disabled.",
peer_id, set_id, connection_id);
if let Some(pos) =
connections.iter().position(|(c, _)| *c == connection_id)
{
connections.remove(pos);
} else {
debug_assert!(false);
error!(target: "sub-libp2p",
"inject_connection_closed: State mismatch in the custom protos handler");
}
if connections.is_empty() {
if let Some(until) = backoff_until {
let now = Instant::now();
if until > now {
let delay_id = self.next_delay_id;
self.next_delay_id.0 += 1;
let delay = futures_timer::Delay::new(until - now);
self.delays.push(
async move {
delay.await;
(delay_id, peer_id, set_id)
}
.boxed(),
);
*entry.get_mut() = PeerState::Backoff {
timer: delay_id,
timer_deadline: until,
};
} else {
entry.remove();
}
} else {
entry.remove();
}
} else {
*entry.get_mut() =
PeerState::Disabled { connections, backoff_until };
}
},
PeerState::DisabledPendingEnable {
mut connections,
timer_deadline,
timer,
} => {
trace!(
target: "sub-libp2p",
"Libp2p => Disconnected({}, {:?}, {:?}): Disabled but pending enable.",
peer_id, set_id, connection_id
);
if let Some(pos) =
connections.iter().position(|(c, _)| *c == connection_id)
{
connections.remove(pos);
} else {
error!(target: "sub-libp2p",
"inject_connection_closed: State mismatch in the custom protos handler");
debug_assert!(false);
}
if connections.is_empty() {
trace!(target: "sub-libp2p", "PSM <= Dropped({}, {:?})", peer_id, set_id);
self.peerset.dropped(set_id, peer_id, DropReason::Unknown);
*entry.get_mut() = PeerState::Backoff { timer, timer_deadline };
} else {
*entry.get_mut() = PeerState::DisabledPendingEnable {
connections,
timer_deadline,
timer,
};
}
},
PeerState::Incoming { mut connections, backoff_until } => {
trace!(
target: "sub-libp2p",
"Libp2p => Disconnected({}, {:?}, {:?}): OpenDesiredByRemote.",
peer_id, set_id, connection_id
);
debug_assert!(connections
.iter()
.any(|(_, s)| matches!(s, ConnectionState::OpenDesiredByRemote)));
if let Some(pos) =
connections.iter().position(|(c, _)| *c == connection_id)
{
connections.remove(pos);
} else {
error!(target: "sub-libp2p",
"inject_connection_closed: State mismatch in the custom protos handler");
debug_assert!(false);
}
let no_desired_left = !connections
.iter()
.any(|(_, s)| matches!(s, ConnectionState::OpenDesiredByRemote));
if no_desired_left {
if let Some(state) = self
.incoming
.iter_mut()
.find(|i| i.alive && i.set_id == set_id && i.peer_id == peer_id)
{
state.alive = false;
} else {
error!(target: "sub-libp2p", "State mismatch in libp2p: no entry in \
incoming corresponding to an incoming state in peers");
debug_assert!(false);
}
}
if connections.is_empty() {
if let Some(until) = backoff_until {
let now = Instant::now();
if until > now {
let delay_id = self.next_delay_id;
self.next_delay_id.0 += 1;
let delay = futures_timer::Delay::new(until - now);
self.delays.push(
async move {
delay.await;
(delay_id, peer_id, set_id)
}
.boxed(),
);
*entry.get_mut() = PeerState::Backoff {
timer: delay_id,
timer_deadline: until,
};
} else {
entry.remove();
}
} else {
entry.remove();
}
} else if no_desired_left {
*entry.get_mut() =
PeerState::Disabled { connections, backoff_until };
} else {
*entry.get_mut() =
PeerState::Incoming { connections, backoff_until };
}
},
PeerState::Enabled { mut connections } => {
trace!(
target: "sub-libp2p",
"Libp2p => Disconnected({}, {:?}, {:?}): Enabled.",
peer_id, set_id, connection_id
);
debug_assert!(connections.iter().any(|(_, s)| matches!(
s,
ConnectionState::Opening | ConnectionState::Open(_)
)));
if let Some(pos) =
connections.iter().position(|(c, _)| *c == connection_id)
{
let (_, state) = connections.remove(pos);
if let ConnectionState::Open(_) = state {
if let Some((replacement_pos, replacement_sink)) = connections
.iter()
.enumerate()
.find_map(|(num, (_, s))| match s {
ConnectionState::Open(s) => Some((num, s.clone())),
_ => None,
}) {
if pos <= replacement_pos {
trace!(
target: "sub-libp2p",
"External API <= Sink replaced({}, {:?})",
peer_id, set_id
);
let event = NotificationsOut::CustomProtocolReplaced {
peer_id,
set_id,
notifications_sink: replacement_sink,
};
self.events.push_back(
NetworkBehaviourAction::GenerateEvent(event),
);
}
} else {
trace!(
target: "sub-libp2p", "External API <= Closed({}, {:?})",
peer_id, set_id
);
let event = NotificationsOut::CustomProtocolClosed {
peer_id,
set_id,
};
self.events.push_back(
NetworkBehaviourAction::GenerateEvent(event),
);
}
}
} else {
error!(target: "sub-libp2p",
"inject_connection_closed: State mismatch in the custom protos handler");
debug_assert!(false);
}
if connections.is_empty() {
trace!(target: "sub-libp2p", "PSM <= Dropped({}, {:?})", peer_id, set_id);
self.peerset.dropped(set_id, peer_id, DropReason::Unknown);
let ban_dur = Uniform::new(5, 10).sample(&mut rand::thread_rng());
let delay_id = self.next_delay_id;
self.next_delay_id.0 += 1;
let delay = futures_timer::Delay::new(Duration::from_secs(ban_dur));
self.delays.push(
async move {
delay.await;
(delay_id, peer_id, set_id)
}
.boxed(),
);
*entry.get_mut() = PeerState::Backoff {
timer: delay_id,
timer_deadline: Instant::now() + Duration::from_secs(ban_dur),
};
} else if !connections.iter().any(|(_, s)| {
matches!(s, ConnectionState::Opening | ConnectionState::Open(_))
}) {
trace!(target: "sub-libp2p", "PSM <= Dropped({}, {:?})", peer_id, set_id);
self.peerset.dropped(set_id, peer_id, DropReason::Unknown);
*entry.get_mut() =
PeerState::Disabled { connections, backoff_until: None };
} else {
*entry.get_mut() = PeerState::Enabled { connections };
}
},
PeerState::Requested |
PeerState::PendingRequest { .. } |
PeerState::Backoff { .. } => {
error!(target: "sub-libp2p",
"`inject_connection_closed` called for unknown peer {}",
peer_id);
debug_assert!(false);
},
PeerState::Poisoned => {
error!(target: "sub-libp2p", "State of peer {} is poisoned", peer_id);
debug_assert!(false);
},
}
}
},
FromSwarm::DialFailure(DialFailure { peer_id, error, .. }) => {
if let DialError::Transport(errors) = error {
for (addr, error) in errors.iter() {
trace!(target: "sub-libp2p", "Libp2p => Reach failure for {:?} through {:?}: {:?}", peer_id, addr, error);
}
}
if let Some(peer_id) = peer_id {
trace!(target: "sub-libp2p", "Libp2p => Dial failure for {:?}", peer_id);
for set_id in (0..self.notif_protocols.len()).map(sc_peerset::SetId::from) {
if let Entry::Occupied(mut entry) = self.peers.entry((peer_id, set_id)) {
match mem::replace(entry.get_mut(), PeerState::Poisoned) {
st @ PeerState::Backoff { .. } => {
*entry.into_mut() = st;
},
st @ PeerState::Requested |
st @ PeerState::PendingRequest { .. } => {
trace!(target: "sub-libp2p", "PSM <= Dropped({}, {:?})", peer_id, set_id);
self.peerset.dropped(set_id, peer_id, DropReason::Unknown);
let now = Instant::now();
let ban_duration = match st {
PeerState::PendingRequest { timer_deadline, .. }
if timer_deadline > now =>
cmp::max(timer_deadline - now, Duration::from_secs(5)),
_ => Duration::from_secs(5),
};
let delay_id = self.next_delay_id;
self.next_delay_id.0 += 1;
let delay = futures_timer::Delay::new(ban_duration);
let peer_id = peer_id;
self.delays.push(
async move {
delay.await;
(delay_id, peer_id, set_id)
}
.boxed(),
);
*entry.into_mut() = PeerState::Backoff {
timer: delay_id,
timer_deadline: now + ban_duration,
};
},
st @ PeerState::Disabled { .. } |
st @ PeerState::Enabled { .. } |
st @ PeerState::DisabledPendingEnable { .. } |
st @ PeerState::Incoming { .. } => {
*entry.into_mut() = st;
},
PeerState::Poisoned => {
error!(target: "sub-libp2p", "State of {:?} is poisoned", peer_id);
debug_assert!(false);
},
}
}
}
}
},
FromSwarm::ListenerClosed(_) => {},
FromSwarm::ListenFailure(_) => {},
FromSwarm::ListenerError(_) => {},
FromSwarm::ExpiredExternalAddr(_) => {},
FromSwarm::NewListener(_) => {},
FromSwarm::ExpiredListenAddr(_) => {},
FromSwarm::NewExternalAddr(_) => {},
FromSwarm::AddressChange(_) => {},
FromSwarm::NewListenAddr(_) => {},
}
}
fn on_connection_handler_event(
&mut self,
peer_id: PeerId,
connection_id: ConnectionId,
event: <<Self::ConnectionHandler as IntoConnectionHandler>::Handler as
ConnectionHandler>::OutEvent,
) {
match event {
NotifsHandlerOut::OpenDesiredByRemote { protocol_index } => {
let set_id = sc_peerset::SetId::from(protocol_index);
trace!(target: "sub-libp2p",
"Handler({:?}, {:?}]) => OpenDesiredByRemote({:?})",
peer_id, connection_id, set_id);
let mut entry = if let Entry::Occupied(entry) = self.peers.entry((peer_id, set_id))
{
entry
} else {
error!(
target: "sub-libp2p",
"OpenDesiredByRemote: State mismatch in the custom protos handler"
);
debug_assert!(false);
return
};
match mem::replace(entry.get_mut(), PeerState::Poisoned) {
PeerState::Incoming { mut connections, backoff_until } => {
debug_assert!(connections
.iter()
.any(|(_, s)| matches!(s, ConnectionState::OpenDesiredByRemote)));
if let Some((_, connec_state)) =
connections.iter_mut().find(|(c, _)| *c == connection_id)
{
if let ConnectionState::Closed = *connec_state {
*connec_state = ConnectionState::OpenDesiredByRemote;
} else {
debug_assert!(matches!(
connec_state,
ConnectionState::OpeningThenClosing | ConnectionState::Closing
));
}
} else {
error!(
target: "sub-libp2p",
"OpenDesiredByRemote: State mismatch in the custom protos handler"
);
debug_assert!(false);
}
*entry.into_mut() = PeerState::Incoming { connections, backoff_until };
},
PeerState::Enabled { mut connections } => {
debug_assert!(connections.iter().any(|(_, s)| matches!(
s,
ConnectionState::Opening | ConnectionState::Open(_)
)));
if let Some((_, connec_state)) =
connections.iter_mut().find(|(c, _)| *c == connection_id)
{
if let ConnectionState::Closed = *connec_state {
trace!(target: "sub-libp2p", "Handler({:?}, {:?}) <= Open({:?})",
peer_id, connection_id, set_id);
self.events.push_back(NetworkBehaviourAction::NotifyHandler {
peer_id,
handler: NotifyHandler::One(connection_id),
event: NotifsHandlerIn::Open { protocol_index: set_id.into() },
});
*connec_state = ConnectionState::Opening;
} else {
debug_assert!(matches!(
connec_state,
ConnectionState::OpenDesiredByRemote |
ConnectionState::Closing | ConnectionState::Opening
));
}
} else {
error!(
target: "sub-libp2p",
"OpenDesiredByRemote: State mismatch in the custom protos handler"
);
debug_assert!(false);
}
*entry.into_mut() = PeerState::Enabled { connections };
},
PeerState::Disabled { mut connections, backoff_until } => {
if let Some((_, connec_state)) =
connections.iter_mut().find(|(c, _)| *c == connection_id)
{
if let ConnectionState::Closed = *connec_state {
*connec_state = ConnectionState::OpenDesiredByRemote;
let incoming_id = self.next_incoming_index;
self.next_incoming_index.0 += 1;
trace!(target: "sub-libp2p", "PSM <= Incoming({}, {:?}).",
peer_id, incoming_id);
self.peerset.incoming(set_id, peer_id, incoming_id);
self.incoming.push(IncomingPeer {
peer_id,
set_id,
alive: true,
incoming_id,
});
*entry.into_mut() =
PeerState::Incoming { connections, backoff_until };
} else {
debug_assert!(matches!(
connec_state,
ConnectionState::OpeningThenClosing | ConnectionState::Closing
));
*entry.into_mut() =
PeerState::Disabled { connections, backoff_until };
}
} else {
error!(
target: "sub-libp2p",
"OpenDesiredByRemote: State mismatch in the custom protos handler"
);
debug_assert!(false);
}
},
PeerState::DisabledPendingEnable { mut connections, timer, timer_deadline } => {
if let Some((_, connec_state)) =
connections.iter_mut().find(|(c, _)| *c == connection_id)
{
if let ConnectionState::Closed = *connec_state {
trace!(target: "sub-libp2p", "Handler({:?}, {:?}) <= Open({:?})",
peer_id, connection_id, set_id);
self.events.push_back(NetworkBehaviourAction::NotifyHandler {
peer_id,
handler: NotifyHandler::One(connection_id),
event: NotifsHandlerIn::Open { protocol_index: set_id.into() },
});
*connec_state = ConnectionState::Opening;
*entry.into_mut() = PeerState::Enabled { connections };
} else {
debug_assert!(matches!(
connec_state,
ConnectionState::OpeningThenClosing | ConnectionState::Closing
));
*entry.into_mut() = PeerState::DisabledPendingEnable {
connections,
timer,
timer_deadline,
};
}
} else {
error!(
target: "sub-libp2p",
"OpenDesiredByRemote: State mismatch in the custom protos handler"
);
debug_assert!(false);
}
},
state => {
error!(target: "sub-libp2p",
"OpenDesiredByRemote: Unexpected state in the custom protos handler: {:?}",
state);
debug_assert!(false);
},
};
},
NotifsHandlerOut::CloseDesired { protocol_index } => {
let set_id = sc_peerset::SetId::from(protocol_index);
trace!(target: "sub-libp2p",
"Handler({}, {:?}) => CloseDesired({:?})",
peer_id, connection_id, set_id);
let mut entry = if let Entry::Occupied(entry) = self.peers.entry((peer_id, set_id))
{
entry
} else {
error!(target: "sub-libp2p", "CloseDesired: State mismatch in the custom protos handler");
debug_assert!(false);
return
};
match mem::replace(entry.get_mut(), PeerState::Poisoned) {
PeerState::Enabled { mut connections } => {
debug_assert!(connections.iter().any(|(_, s)| matches!(
s,
ConnectionState::Opening | ConnectionState::Open(_)
)));
let pos = if let Some(pos) =
connections.iter().position(|(c, _)| *c == connection_id)
{
pos
} else {
error!(target: "sub-libp2p",
"CloseDesired: State mismatch in the custom protos handler");
debug_assert!(false);
return
};
if matches!(connections[pos].1, ConnectionState::Closing) {
*entry.into_mut() = PeerState::Enabled { connections };
return
}
debug_assert!(matches!(connections[pos].1, ConnectionState::Open(_)));
connections[pos].1 = ConnectionState::Closing;
trace!(target: "sub-libp2p", "Handler({}, {:?}) <= Close({:?})", peer_id, connection_id, set_id);
self.events.push_back(NetworkBehaviourAction::NotifyHandler {
peer_id,
handler: NotifyHandler::One(connection_id),
event: NotifsHandlerIn::Close { protocol_index: set_id.into() },
});
if let Some((replacement_pos, replacement_sink)) =
connections.iter().enumerate().find_map(|(num, (_, s))| match s {
ConnectionState::Open(s) => Some((num, s.clone())),
_ => None,
}) {
if pos <= replacement_pos {
trace!(target: "sub-libp2p", "External API <= Sink replaced({:?})", peer_id);
let event = NotificationsOut::CustomProtocolReplaced {
peer_id,
set_id,
notifications_sink: replacement_sink,
};
self.events.push_back(NetworkBehaviourAction::GenerateEvent(event));
}
*entry.into_mut() = PeerState::Enabled { connections };
} else {
if !connections
.iter()
.any(|(_, s)| matches!(s, ConnectionState::Opening))
{
trace!(target: "sub-libp2p", "PSM <= Dropped({}, {:?})", peer_id, set_id);
self.peerset.dropped(set_id, peer_id, DropReason::Refused);
*entry.into_mut() =
PeerState::Disabled { connections, backoff_until: None };
} else {
*entry.into_mut() = PeerState::Enabled { connections };
}
trace!(target: "sub-libp2p", "External API <= Closed({}, {:?})", peer_id, set_id);
let event = NotificationsOut::CustomProtocolClosed { peer_id, set_id };
self.events.push_back(NetworkBehaviourAction::GenerateEvent(event));
}
},
state @ PeerState::Disabled { .. } |
state @ PeerState::DisabledPendingEnable { .. } => {
*entry.into_mut() = state;
},
state => {
error!(target: "sub-libp2p",
"Unexpected state in the custom protos handler: {:?}",
state);
},
}
},
NotifsHandlerOut::CloseResult { protocol_index } => {
let set_id = sc_peerset::SetId::from(protocol_index);
trace!(target: "sub-libp2p",
"Handler({}, {:?}) => CloseResult({:?})",
peer_id, connection_id, set_id);
match self.peers.get_mut(&(peer_id, set_id)) {
Some(PeerState::Incoming { connections, .. }) |
Some(PeerState::DisabledPendingEnable { connections, .. }) |
Some(PeerState::Disabled { connections, .. }) |
Some(PeerState::Enabled { connections, .. }) => {
if let Some((_, connec_state)) = connections.iter_mut().find(|(c, s)| {
*c == connection_id && matches!(s, ConnectionState::Closing)
}) {
*connec_state = ConnectionState::Closed;
} else {
error!(target: "sub-libp2p",
"CloseResult: State mismatch in the custom protos handler");
debug_assert!(false);
}
},
state => {
error!(target: "sub-libp2p",
"CloseResult: Unexpected state in the custom protos handler: {:?}",
state);
debug_assert!(false);
},
}
},
NotifsHandlerOut::OpenResultOk {
protocol_index,
negotiated_fallback,
received_handshake,
notifications_sink,
..
} => {
let set_id = sc_peerset::SetId::from(protocol_index);
trace!(target: "sub-libp2p",
"Handler({}, {:?}) => OpenResultOk({:?})",
peer_id, connection_id, set_id);
match self.peers.get_mut(&(peer_id, set_id)) {
Some(PeerState::Enabled { connections, .. }) => {
debug_assert!(connections.iter().any(|(_, s)| matches!(
s,
ConnectionState::Opening | ConnectionState::Open(_)
)));
let any_open =
connections.iter().any(|(_, s)| matches!(s, ConnectionState::Open(_)));
if let Some((_, connec_state)) = connections.iter_mut().find(|(c, s)| {
*c == connection_id && matches!(s, ConnectionState::Opening)
}) {
if !any_open {
trace!(target: "sub-libp2p", "External API <= Open({}, {:?})", peer_id, set_id);
let event = NotificationsOut::CustomProtocolOpen {
peer_id,
set_id,
negotiated_fallback,
received_handshake,
notifications_sink: notifications_sink.clone(),
};
self.events.push_back(NetworkBehaviourAction::GenerateEvent(event));
}
*connec_state = ConnectionState::Open(notifications_sink);
} else if let Some((_, connec_state)) =
connections.iter_mut().find(|(c, s)| {
*c == connection_id &&
matches!(s, ConnectionState::OpeningThenClosing)
}) {
*connec_state = ConnectionState::Closing;
} else {
error!(target: "sub-libp2p",
"OpenResultOk State mismatch in the custom protos handler");
debug_assert!(false);
}
},
Some(PeerState::Incoming { connections, .. }) |
Some(PeerState::DisabledPendingEnable { connections, .. }) |
Some(PeerState::Disabled { connections, .. }) => {
if let Some((_, connec_state)) = connections.iter_mut().find(|(c, s)| {
*c == connection_id && matches!(s, ConnectionState::OpeningThenClosing)
}) {
*connec_state = ConnectionState::Closing;
} else {
error!(target: "sub-libp2p",
"OpenResultOk State mismatch in the custom protos handler");
debug_assert!(false);
}
},
state => {
error!(target: "sub-libp2p",
"OpenResultOk: Unexpected state in the custom protos handler: {:?}",
state);
debug_assert!(false);
},
}
},
NotifsHandlerOut::OpenResultErr { protocol_index } => {
let set_id = sc_peerset::SetId::from(protocol_index);
trace!(target: "sub-libp2p",
"Handler({:?}, {:?}) => OpenResultErr({:?})",
peer_id, connection_id, set_id);
let mut entry = if let Entry::Occupied(entry) = self.peers.entry((peer_id, set_id))
{
entry
} else {
error!(target: "sub-libp2p", "OpenResultErr: State mismatch in the custom protos handler");
debug_assert!(false);
return
};
match mem::replace(entry.get_mut(), PeerState::Poisoned) {
PeerState::Enabled { mut connections } => {
debug_assert!(connections.iter().any(|(_, s)| matches!(
s,
ConnectionState::Opening | ConnectionState::Open(_)
)));
if let Some((_, connec_state)) = connections.iter_mut().find(|(c, s)| {
*c == connection_id && matches!(s, ConnectionState::Opening)
}) {
*connec_state = ConnectionState::Closed;
} else if let Some((_, connec_state)) =
connections.iter_mut().find(|(c, s)| {
*c == connection_id &&
matches!(s, ConnectionState::OpeningThenClosing)
}) {
*connec_state = ConnectionState::Closing;
} else {
error!(target: "sub-libp2p",
"OpenResultErr: State mismatch in the custom protos handler");
debug_assert!(false);
}
if !connections.iter().any(|(_, s)| {
matches!(s, ConnectionState::Opening | ConnectionState::Open(_))
}) {
trace!(target: "sub-libp2p", "PSM <= Dropped({:?})", peer_id);
self.peerset.dropped(set_id, peer_id, DropReason::Refused);
let ban_dur = Uniform::new(5, 10).sample(&mut rand::thread_rng());
*entry.into_mut() = PeerState::Disabled {
connections,
backoff_until: Some(Instant::now() + Duration::from_secs(ban_dur)),
};
} else {
*entry.into_mut() = PeerState::Enabled { connections };
}
},
mut state @ PeerState::Incoming { .. } |
mut state @ PeerState::DisabledPendingEnable { .. } |
mut state @ PeerState::Disabled { .. } => {
match &mut state {
PeerState::Incoming { connections, .. } |
PeerState::Disabled { connections, .. } |
PeerState::DisabledPendingEnable { connections, .. } => {
if let Some((_, connec_state)) =
connections.iter_mut().find(|(c, s)| {
*c == connection_id &&
matches!(s, ConnectionState::OpeningThenClosing)
}) {
*connec_state = ConnectionState::Closing;
} else {
error!(target: "sub-libp2p",
"OpenResultErr: State mismatch in the custom protos handler");
debug_assert!(false);
}
},
_ => unreachable!(
"Match branches are the same as the one on which we
enter this block; qed"
),
};
*entry.into_mut() = state;
},
state => {
error!(target: "sub-libp2p",
"Unexpected state in the custom protos handler: {:?}",
state);
debug_assert!(false);
},
};
},
NotifsHandlerOut::Notification { protocol_index, message } => {
let set_id = sc_peerset::SetId::from(protocol_index);
if self.is_open(&peer_id, set_id) {
trace!(
target: "sub-libp2p",
"Handler({:?}) => Notification({}, {:?}, {} bytes)",
connection_id,
peer_id,
set_id,
message.len()
);
trace!(
target: "sub-libp2p",
"External API <= Message({}, {:?})",
peer_id,
set_id,
);
let event = NotificationsOut::Notification { peer_id, set_id, message };
self.events.push_back(NetworkBehaviourAction::GenerateEvent(event));
} else {
trace!(
target: "sub-libp2p",
"Handler({:?}) => Post-close notification({}, {:?}, {} bytes)",
connection_id,
peer_id,
set_id,
message.len()
);
}
},
}
}
fn poll(
&mut self,
cx: &mut Context,
_params: &mut impl PollParameters,
) -> Poll<NetworkBehaviourAction<Self::OutEvent, Self::ConnectionHandler>> {
if let Some(event) = self.events.pop_front() {
return Poll::Ready(event)
}
loop {
match futures::Stream::poll_next(Pin::new(&mut self.peerset), cx) {
Poll::Ready(Some(sc_peerset::Message::Accept(index))) => {
self.peerset_report_accept(index);
},
Poll::Ready(Some(sc_peerset::Message::Reject(index))) => {
self.peerset_report_reject(index);
},
Poll::Ready(Some(sc_peerset::Message::Connect { peer_id, set_id, .. })) => {
self.peerset_report_connect(peer_id, set_id);
},
Poll::Ready(Some(sc_peerset::Message::Drop { peer_id, set_id, .. })) => {
self.peerset_report_disconnect(peer_id, set_id);
},
Poll::Ready(None) => {
error!(target: "sub-libp2p", "Peerset receiver stream has returned None");
break
},
Poll::Pending => break,
}
}
while let Poll::Ready(Some((delay_id, peer_id, set_id))) =
Pin::new(&mut self.delays).poll_next(cx)
{
let handler = self.new_handler();
let peer_state = match self.peers.get_mut(&(peer_id, set_id)) {
Some(s) => s,
None => continue,
};
match peer_state {
PeerState::Backoff { timer, .. } if *timer == delay_id => {
trace!(target: "sub-libp2p", "Libp2p <= Clean up ban of {:?} from the state", peer_id);
self.peers.remove(&(peer_id, set_id));
},
PeerState::PendingRequest { timer, .. } if *timer == delay_id => {
trace!(target: "sub-libp2p", "Libp2p <= Dial {:?} now that ban has expired", peer_id);
self.events
.push_back(NetworkBehaviourAction::Dial { opts: peer_id.into(), handler });
*peer_state = PeerState::Requested;
},
PeerState::DisabledPendingEnable { connections, timer, timer_deadline }
if *timer == delay_id =>
{
if let Some((connec_id, connec_state)) =
connections.iter_mut().find(|(_, s)| matches!(s, ConnectionState::Closed))
{
trace!(target: "sub-libp2p", "Handler({}, {:?}) <= Open({:?}) (ban expired)",
peer_id, *connec_id, set_id);
self.events.push_back(NetworkBehaviourAction::NotifyHandler {
peer_id,
handler: NotifyHandler::One(*connec_id),
event: NotifsHandlerIn::Open { protocol_index: set_id.into() },
});
*connec_state = ConnectionState::Opening;
*peer_state = PeerState::Enabled { connections: mem::take(connections) };
} else {
*timer_deadline = Instant::now() + Duration::from_secs(5);
let delay = futures_timer::Delay::new(Duration::from_secs(5));
let timer = *timer;
self.delays.push(
async move {
delay.await;
(timer, peer_id, set_id)
}
.boxed(),
);
}
},
_ => {},
}
}
if let Some(event) = self.events.pop_front() {
return Poll::Ready(event)
}
Poll::Pending
}
}