mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-01 03:07:56 +00:00
Attempt to relieve pressure on mpsc_network_worker (#13725)
* Attempt to relieve pressure on `mpsc_network_worker` `SyncingEngine` interacting with `NetworkWorker` can put a lot of strain on the channel if the number of inbound connections is high. This is because `SyncingEngine` is notified of each inbound substream which it then can either accept or reject and this causes a lot of message exchange on the already busy channel. Use a direct channel pair between `Protocol` and `SyncingEngine` to exchange notification events. It is a temporary change to alleviate the problems caused by syncing being an independent protocol and the fix will be removed once `NotificationService` is implemented. * Apply review comments * fixes * trigger ci * Fix tests Verify that both peers have a connection now that the validation goes through `SyncingEngine`. Depending on how the tasks are scheduled, one of them might not have the peer registered in `SyncingEngine` at which point the test won't make any progress because block announcement received from an unknown peer is discarded. Move polling of `ChainSync` at the end of the function so that if a block announcement causes a block request to be sent, that can be sent in the same call to `SyncingEngine::poll()`. --------- Co-authored-by: parity-processbot <>
This commit is contained in:
@@ -24,6 +24,7 @@ use crate::{
|
||||
|
||||
use bytes::Bytes;
|
||||
use codec::{DecodeAll, Encode};
|
||||
use futures::{channel::oneshot, stream::FuturesUnordered, StreamExt};
|
||||
use libp2p::{
|
||||
core::connection::ConnectionId,
|
||||
swarm::{
|
||||
@@ -35,11 +36,14 @@ use libp2p::{
|
||||
use log::{debug, error, warn};
|
||||
|
||||
use sc_network_common::{role::Roles, sync::message::BlockAnnouncesHandshake};
|
||||
use sc_utils::mpsc::TracingUnboundedSender;
|
||||
use sp_runtime::traits::Block as BlockT;
|
||||
|
||||
use std::{
|
||||
collections::{HashMap, HashSet, VecDeque},
|
||||
future::Future,
|
||||
iter,
|
||||
pin::Pin,
|
||||
task::Poll,
|
||||
};
|
||||
|
||||
@@ -68,6 +72,9 @@ mod rep {
|
||||
pub const BAD_MESSAGE: Rep = Rep::new(-(1 << 12), "Bad message");
|
||||
}
|
||||
|
||||
type PendingSyncSubstreamValidation =
|
||||
Pin<Box<dyn Future<Output = Result<(PeerId, Roles), PeerId>> + Send>>;
|
||||
|
||||
// Lock must always be taken in order declared here.
|
||||
pub struct Protocol<B: BlockT> {
|
||||
/// Pending list of messages to return from `poll` as a priority.
|
||||
@@ -87,6 +94,8 @@ pub struct Protocol<B: BlockT> {
|
||||
bad_handshake_substreams: HashSet<(PeerId, sc_peerset::SetId)>,
|
||||
/// Connected peers.
|
||||
peers: HashMap<PeerId, Roles>,
|
||||
sync_substream_validations: FuturesUnordered<PendingSyncSubstreamValidation>,
|
||||
tx: TracingUnboundedSender<crate::event::SyncEvent<B>>,
|
||||
_marker: std::marker::PhantomData<B>,
|
||||
}
|
||||
|
||||
@@ -96,6 +105,7 @@ impl<B: BlockT> Protocol<B> {
|
||||
roles: Roles,
|
||||
network_config: &config::NetworkConfiguration,
|
||||
block_announces_protocol: config::NonDefaultSetConfig,
|
||||
tx: TracingUnboundedSender<crate::event::SyncEvent<B>>,
|
||||
) -> error::Result<(Self, sc_peerset::PeersetHandle, Vec<(PeerId, Multiaddr)>)> {
|
||||
let mut known_addresses = Vec::new();
|
||||
|
||||
@@ -179,6 +189,8 @@ impl<B: BlockT> Protocol<B> {
|
||||
.collect(),
|
||||
bad_handshake_substreams: Default::default(),
|
||||
peers: HashMap::new(),
|
||||
sync_substream_validations: FuturesUnordered::new(),
|
||||
tx,
|
||||
// TODO: remove when `BlockAnnouncesHandshake` is moved away from `Protocol`
|
||||
_marker: Default::default(),
|
||||
};
|
||||
@@ -418,6 +430,23 @@ impl<B: BlockT> NetworkBehaviour for Protocol<B> {
|
||||
return Poll::Ready(NetworkBehaviourAction::CloseConnection { peer_id, connection }),
|
||||
};
|
||||
|
||||
while let Poll::Ready(Some(validation_result)) =
|
||||
self.sync_substream_validations.poll_next_unpin(cx)
|
||||
{
|
||||
match validation_result {
|
||||
Ok((peer, roles)) => {
|
||||
self.peers.insert(peer, roles);
|
||||
},
|
||||
Err(peer) => {
|
||||
log::debug!(
|
||||
target: "sub-libp2p",
|
||||
"`SyncingEngine` rejected stream"
|
||||
);
|
||||
self.behaviour.disconnect_peer(&peer, HARDCODED_PEERSETS_SYNC);
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
let outcome = match event {
|
||||
NotificationsOut::CustomProtocolOpen {
|
||||
peer_id,
|
||||
@@ -440,16 +469,29 @@ impl<B: BlockT> NetworkBehaviour for Protocol<B> {
|
||||
best_hash: handshake.best_hash,
|
||||
genesis_hash: handshake.genesis_hash,
|
||||
};
|
||||
self.peers.insert(peer_id, roles);
|
||||
|
||||
CustomMessageOutcome::NotificationStreamOpened {
|
||||
remote: peer_id,
|
||||
protocol: self.notification_protocols[usize::from(set_id)].clone(),
|
||||
negotiated_fallback,
|
||||
received_handshake: handshake.encode(),
|
||||
roles,
|
||||
notifications_sink,
|
||||
}
|
||||
let (tx, rx) = oneshot::channel();
|
||||
let _ = self.tx.unbounded_send(
|
||||
crate::SyncEvent::NotificationStreamOpened {
|
||||
remote: peer_id,
|
||||
received_handshake: handshake,
|
||||
sink: notifications_sink,
|
||||
tx,
|
||||
},
|
||||
);
|
||||
self.sync_substream_validations.push(Box::pin(async move {
|
||||
match rx.await {
|
||||
Ok(accepted) =>
|
||||
if accepted {
|
||||
Ok((peer_id, roles))
|
||||
} else {
|
||||
Err(peer_id)
|
||||
},
|
||||
Err(_) => Err(peer_id),
|
||||
}
|
||||
}));
|
||||
|
||||
CustomMessageOutcome::None
|
||||
},
|
||||
Ok(msg) => {
|
||||
debug!(
|
||||
@@ -469,15 +511,27 @@ impl<B: BlockT> NetworkBehaviour for Protocol<B> {
|
||||
let roles = handshake.roles;
|
||||
self.peers.insert(peer_id, roles);
|
||||
|
||||
CustomMessageOutcome::NotificationStreamOpened {
|
||||
remote: peer_id,
|
||||
protocol: self.notification_protocols[usize::from(set_id)]
|
||||
.clone(),
|
||||
negotiated_fallback,
|
||||
received_handshake,
|
||||
roles,
|
||||
notifications_sink,
|
||||
}
|
||||
let (tx, rx) = oneshot::channel();
|
||||
let _ = self.tx.unbounded_send(
|
||||
crate::SyncEvent::NotificationStreamOpened {
|
||||
remote: peer_id,
|
||||
received_handshake: handshake,
|
||||
sink: notifications_sink,
|
||||
tx,
|
||||
},
|
||||
);
|
||||
self.sync_substream_validations.push(Box::pin(async move {
|
||||
match rx.await {
|
||||
Ok(accepted) =>
|
||||
if accepted {
|
||||
Ok((peer_id, roles))
|
||||
} else {
|
||||
Err(peer_id)
|
||||
},
|
||||
Err(_) => Err(peer_id),
|
||||
}
|
||||
}));
|
||||
CustomMessageOutcome::None
|
||||
},
|
||||
Err(err2) => {
|
||||
log::debug!(
|
||||
@@ -535,6 +589,12 @@ impl<B: BlockT> NetworkBehaviour for Protocol<B> {
|
||||
NotificationsOut::CustomProtocolReplaced { peer_id, notifications_sink, set_id } =>
|
||||
if self.bad_handshake_substreams.contains(&(peer_id, set_id)) {
|
||||
CustomMessageOutcome::None
|
||||
} else if set_id == HARDCODED_PEERSETS_SYNC {
|
||||
let _ = self.tx.unbounded_send(crate::SyncEvent::NotificationSinkReplaced {
|
||||
remote: peer_id,
|
||||
sink: notifications_sink,
|
||||
});
|
||||
CustomMessageOutcome::None
|
||||
} else {
|
||||
CustomMessageOutcome::NotificationStreamReplaced {
|
||||
remote: peer_id,
|
||||
@@ -548,6 +608,12 @@ impl<B: BlockT> NetworkBehaviour for Protocol<B> {
|
||||
// handshake. The outer layers have never received an opening event about this
|
||||
// substream, and consequently shouldn't receive a closing event either.
|
||||
CustomMessageOutcome::None
|
||||
} else if set_id == HARDCODED_PEERSETS_SYNC {
|
||||
let _ = self.tx.unbounded_send(crate::SyncEvent::NotificationStreamClosed {
|
||||
remote: peer_id,
|
||||
});
|
||||
self.peers.remove(&peer_id);
|
||||
CustomMessageOutcome::None
|
||||
} else {
|
||||
CustomMessageOutcome::NotificationStreamClosed {
|
||||
remote: peer_id,
|
||||
@@ -558,6 +624,12 @@ impl<B: BlockT> NetworkBehaviour for Protocol<B> {
|
||||
NotificationsOut::Notification { peer_id, set_id, message } => {
|
||||
if self.bad_handshake_substreams.contains(&(peer_id, set_id)) {
|
||||
CustomMessageOutcome::None
|
||||
} else if set_id == HARDCODED_PEERSETS_SYNC {
|
||||
let _ = self.tx.unbounded_send(crate::SyncEvent::NotificationsReceived {
|
||||
remote: peer_id,
|
||||
messages: vec![message.freeze()],
|
||||
});
|
||||
CustomMessageOutcome::None
|
||||
} else {
|
||||
let protocol_name = self.notification_protocols[usize::from(set_id)].clone();
|
||||
CustomMessageOutcome::NotificationsReceived {
|
||||
|
||||
Reference in New Issue
Block a user