From 907ee98ab8f048229661e506ffe89d2afdc20b41 Mon Sep 17 00:00:00 2001 From: Romain Ruetschi Date: Wed, 16 Oct 2024 15:35:53 +0200 Subject: [PATCH] Work around libp2p bug where a node cannot publish to a topic if restarted too quickly See https://github.com/libp2p/rust-libp2p/issues/5097 --- code/crates/gossip-consensus/src/lib.rs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/code/crates/gossip-consensus/src/lib.rs b/code/crates/gossip-consensus/src/lib.rs index f47bb7e41..0c2c24d80 100644 --- a/code/crates/gossip-consensus/src/lib.rs +++ b/code/crates/gossip-consensus/src/lib.rs @@ -10,7 +10,7 @@ use futures::StreamExt; use libp2p::metrics::{Metrics, Recorder}; use libp2p::request_response::InboundRequestId; use libp2p::swarm::{self, SwarmEvent}; -use libp2p::{gossipsub, identify, SwarmBuilder}; +use libp2p::{gossipsub, identify, quic, SwarmBuilder}; use libp2p_broadcast as broadcast; use tokio::sync::mpsc; use tracing::{debug, error, error_span, trace, Instrument}; @@ -66,9 +66,18 @@ pub struct Config { } impl Config { - fn apply(&self, cfg: swarm::Config) -> swarm::Config { + fn apply_to_swarm(&self, cfg: swarm::Config) -> swarm::Config { cfg.with_idle_connection_timeout(self.idle_connection_timeout) } + + fn apply_to_quic(&self, mut cfg: quic::Config) -> quic::Config { + // NOTE: This is set low due to quic transport not properly resetting + // connection state when reconnecting before connection timeout. + // See https://github.com/libp2p/rust-libp2p/issues/5097 + cfg.max_idle_timeout = 300; + cfg.keep_alive_interval = Duration::from_millis(100); + cfg + } } #[derive(Copy, Clone, Debug, PartialEq, Eq)] @@ -125,14 +134,14 @@ pub async fn spawn( .with_dns()? .with_bandwidth_metrics(registry) .with_behaviour(|kp| Behaviour::new_with_metrics(config.protocol, kp, registry))? - .with_swarm_config(|cfg| config.apply(cfg)) + .with_swarm_config(|cfg| config.apply_to_swarm(cfg)) .build()), TransportProtocol::Quic => Ok(builder - .with_quic() + .with_quic_config(|cfg| config.apply_to_quic(cfg)) .with_dns()? .with_bandwidth_metrics(registry) .with_behaviour(|kp| Behaviour::new_with_metrics(config.protocol, kp, registry))? - .with_swarm_config(|cfg| config.apply(cfg)) + .with_swarm_config(|cfg| config.apply_to_swarm(cfg)) .build()), } })?;