Remove manual poll of the libp2p Swarm (#6550)

* remove manual poll for libp2p Swarm,

use tokio::select! instead
This commit is contained in:
João Oliveira
2024-10-30 11:26:26 +00:00
committed by GitHub
parent 8d7b3ddac7
commit 7105442840

View File

@@ -37,10 +37,7 @@ use slog::{crit, debug, info, o, trace, warn};
use std::num::{NonZeroU8, NonZeroUsize}; use std::num::{NonZeroU8, NonZeroUsize};
use std::path::PathBuf; use std::path::PathBuf;
use std::pin::Pin; use std::pin::Pin;
use std::{ use std::sync::Arc;
sync::Arc,
task::{Context, Poll},
};
use types::{ use types::{
consts::altair::SYNC_COMMITTEE_SUBNET_COUNT, EnrForkId, EthSpec, ForkContext, Slot, SubnetId, consts::altair::SYNC_COMMITTEE_SUBNET_COUNT, EnrForkId, EthSpec, ForkContext, Slot, SubnetId,
}; };
@@ -1794,12 +1791,45 @@ impl<E: EthSpec> Network<E> {
/* Networking polling */ /* Networking polling */
/// Poll the p2p networking stack. pub async fn next_event(&mut self) -> NetworkEvent<E> {
/// loop {
/// This will poll the swarm and do maintenance routines. tokio::select! {
pub fn poll_network(&mut self, cx: &mut Context) -> Poll<NetworkEvent<E>> { // Poll the libp2p `Swarm`.
while let Poll::Ready(Some(swarm_event)) = self.swarm.poll_next_unpin(cx) { // This will poll the swarm and do maintenance routines.
let maybe_event = match swarm_event { Some(event) = self.swarm.next() => {
if let Some(event) = self.parse_swarm_event(event) {
return event;
}
},
// perform gossipsub score updates when necessary
_ = self.update_gossipsub_scores.tick() => {
let this = self.swarm.behaviour_mut();
this.peer_manager.update_gossipsub_scores(&this.gossipsub);
}
// poll the gossipsub cache to clear expired messages
Some(result) = self.gossip_cache.next() => {
match result {
Err(e) => warn!(self.log, "Gossip cache error"; "error" => e),
Ok(expired_topic) => {
if let Some(v) = metrics::get_int_counter(
&metrics::GOSSIP_EXPIRED_LATE_PUBLISH_PER_TOPIC_KIND,
&[expired_topic.kind().as_ref()],
) {
v.inc()
};
}
}
}
}
}
}
fn parse_swarm_event(
&mut self,
event: SwarmEvent<BehaviourEvent<E>>,
) -> Option<NetworkEvent<E>> {
match event {
SwarmEvent::Behaviour(behaviour_event) => match behaviour_event { SwarmEvent::Behaviour(behaviour_event) => match behaviour_event {
// Handle sub-behaviour events. // Handle sub-behaviour events.
BehaviourEvent::Gossipsub(ge) => self.inject_gs_event(ge), BehaviourEvent::Gossipsub(ge) => self.inject_gs_event(ge),
@@ -1872,9 +1902,7 @@ impl<E: EthSpec> Network<E> {
// behaviour implementation. // behaviour implementation.
None None
} }
SwarmEvent::NewListenAddr { address, .. } => { SwarmEvent::NewListenAddr { address, .. } => Some(NetworkEvent::NewListenAddr(address)),
Some(NetworkEvent::NewListenAddr(address))
}
SwarmEvent::ExpiredListenAddr { address, .. } => { SwarmEvent::ExpiredListenAddr { address, .. } => {
debug!(self.log, "Listen address expired"; "address" => %address); debug!(self.log, "Listen address expired"; "address" => %address);
None None
@@ -1905,37 +1933,6 @@ impl<E: EthSpec> Network<E> {
// release notes more than compiler feedback // release notes more than compiler feedback
None None
} }
};
if let Some(ev) = maybe_event {
return Poll::Ready(ev);
} }
} }
// perform gossipsub score updates when necessary
while self.update_gossipsub_scores.poll_tick(cx).is_ready() {
let this = self.swarm.behaviour_mut();
this.peer_manager.update_gossipsub_scores(&this.gossipsub);
}
// poll the gossipsub cache to clear expired messages
while let Poll::Ready(Some(result)) = self.gossip_cache.poll_next_unpin(cx) {
match result {
Err(e) => warn!(self.log, "Gossip cache error"; "error" => e),
Ok(expired_topic) => {
if let Some(v) = metrics::get_int_counter(
&metrics::GOSSIP_EXPIRED_LATE_PUBLISH_PER_TOPIC_KIND,
&[expired_topic.kind().as_ref()],
) {
v.inc()
};
}
}
}
Poll::Pending
}
pub async fn next_event(&mut self) -> NetworkEvent<E> {
futures::future::poll_fn(|cx| self.poll_network(cx)).await
}
} }