From 002f19a08f01fe90310b5b6a4d29a268e403ffd6 Mon Sep 17 00:00:00 2001 From: xgreenx Date: Sun, 15 Oct 2023 12:05:39 +0200 Subject: [PATCH] Mark reserved peers as explicit for gossipsub to avoid reputation decreasing --- crates/services/p2p/src/config.rs | 2 +- crates/services/p2p/src/gossipsub/config.rs | 7 ++++++- crates/services/p2p/src/service.rs | 2 +- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/crates/services/p2p/src/config.rs b/crates/services/p2p/src/config.rs index 1c4bafc5e21..04aca17e795 100644 --- a/crates/services/p2p/src/config.rs +++ b/crates/services/p2p/src/config.rs @@ -301,7 +301,7 @@ pub(crate) fn build_transport( (transport, connection_state) } -fn peer_ids_set_from(multiaddr: &[Multiaddr]) -> HashSet { +pub fn peer_ids_set_from(multiaddr: &[Multiaddr]) -> HashSet { multiaddr .iter() // Safety: as is the case with `bootstrap_nodes` it is assumed that `reserved_nodes` [`Multiadr`] diff --git a/crates/services/p2p/src/gossipsub/config.rs b/crates/services/p2p/src/gossipsub/config.rs index 6901a8277b1..c703ac23064 100644 --- a/crates/services/p2p/src/gossipsub/config.rs +++ b/crates/services/p2p/src/gossipsub/config.rs @@ -175,7 +175,7 @@ fn initialize_peer_score_thresholds() -> PeerScoreThresholds { /// Given a `P2pConfig` containing `GossipsubConfig` creates a Gossipsub Behaviour pub(crate) fn build_gossipsub_behaviour(p2p_config: &Config) -> Gossipsub { - if p2p_config.metrics { + let mut gossipsub = if p2p_config.metrics { // Move to Metrics related feature flag let mut p2p_registry = Registry::default(); @@ -208,7 +208,12 @@ pub(crate) fn build_gossipsub_behaviour(p2p_config: &Config) -> Gossipsub { initialize_gossipsub(&mut gossipsub, p2p_config); gossipsub + }; + for peer_id in crate::config::peer_ids_set_from(&p2p_config.reserved_nodes) { + gossipsub.add_explicit_peer(&peer_id); } + + gossipsub } fn initialize_gossipsub(gossipsub: &mut Gossipsub, p2p_config: &Config) { diff --git a/crates/services/p2p/src/service.rs b/crates/services/p2p/src/service.rs index 621721fa71f..bbb6af26c57 100644 --- a/crates/services/p2p/src/service.rs +++ b/crates/services/p2p/src/service.rs @@ -201,8 +201,8 @@ where should_continue = true; match next_service_request { Some(TaskRequest::BroadcastTransaction(transaction)) => { - let broadcast = GossipsubBroadcastRequest::NewTx(transaction); let tx_id = transaction.id(&self.chain_id); + let broadcast = GossipsubBroadcastRequest::NewTx(transaction); let result = self.p2p_service.publish_message(broadcast); if let Err(e) = result { tracing::error!("Got an error during transaction {} broadcasting {}", tx_id, e);