From f8e8af4549dd79e50905f305520913a9ec7f8755 Mon Sep 17 00:00:00 2001 From: Krzysztof Lis Date: Tue, 17 Sep 2024 13:27:32 +0200 Subject: [PATCH] feat: allow one custom kad name in config --- crates/p2p/src/behaviour.rs | 5 +++++ crates/p2p/src/behaviour/builder.rs | 17 +++++------------ crates/p2p/src/bin/bootstrap/main.rs | 5 +++-- crates/p2p/src/builder.rs | 2 +- crates/p2p/src/lib.rs | 4 ++-- crates/p2p/src/main_loop.rs | 11 +++-------- crates/p2p/src/test_utils/peer.rs | 2 +- crates/p2p/src/tests.rs | 18 +++++++++--------- crates/pathfinder/src/bin/pathfinder/config.rs | 14 +++++++------- crates/pathfinder/src/bin/pathfinder/main.rs | 2 +- 10 files changed, 37 insertions(+), 43 deletions(-) diff --git a/crates/p2p/src/behaviour.rs b/crates/p2p/src/behaviour.rs index be3cbc511f..499ef71380 100644 --- a/crates/p2p/src/behaviour.rs +++ b/crates/p2p/src/behaviour.rs @@ -39,6 +39,7 @@ use crate::secret::Secret; use crate::sync::codec; use crate::Config; +/// The default kademlia protocol name for a given Starknet chain. pub fn kademlia_protocol_name(chain_id: ChainId) -> StreamProtocol { StreamProtocol::try_from_owned(format!("/starknet/kad/{}/1.0.0", chain_id.as_str())) .expect("Starts with /") @@ -724,6 +725,10 @@ impl Behaviour { }); } + pub fn kademlia(&self) -> &kad::Behaviour { + &self.inner.kademlia + } + pub fn kademlia_mut(&mut self) -> &mut kad::Behaviour { &mut self.inner.kademlia } diff --git a/crates/p2p/src/behaviour/builder.rs b/crates/p2p/src/behaviour/builder.rs index d79d33e2a0..94bb378d14 100644 --- a/crates/p2p/src/behaviour/builder.rs +++ b/crates/p2p/src/behaviour/builder.rs @@ -96,18 +96,11 @@ impl Builder { // This makes sure that the DHT we're implementing is incompatible with the // "default" IPFS DHT from libp2p. - let protocol_name = if cfg.kad_names.is_empty() { - kademlia_protocol_name(chain_id) - } else { - // TODO change config to use 1 protocol name - cfg.kad_names - .iter() - .cloned() - .map(StreamProtocol::try_from_owned) - .collect::, _>>() - .expect("valid protocol names") - .swap_remove(0) - }; + let protocol_name = cfg + .kad_name + .clone() + .map(|x| StreamProtocol::try_from_owned(x).expect("valid protocol name")) + .unwrap_or_else(|| kademlia_protocol_name(chain_id)); let mut kademlia_config = kad::Config::new(protocol_name); kademlia_config.set_record_ttl(Some(Duration::from_secs(0))); diff --git a/crates/p2p/src/bin/bootstrap/main.rs b/crates/p2p/src/bin/bootstrap/main.rs index 9ad159a99a..092834d439 100644 --- a/crates/p2p/src/bin/bootstrap/main.rs +++ b/crates/p2p/src/bin/bootstrap/main.rs @@ -9,7 +9,6 @@ use libp2p::core::upgrade; use libp2p::identity::Keypair; use libp2p::swarm::{Config, SwarmEvent}; use libp2p::{dns, identify, noise, Multiaddr, Swarm, Transport}; -use p2p::kademlia_protocol_name; use pathfinder_common::ChainId; use serde::Deserialize; use zeroize::Zeroizing; @@ -161,9 +160,11 @@ async fn main() -> anyhow::Result<()> { swarm.add_external_address(observed_addr); + let my_kad_names = swarm.behaviour().kademlia.protocol_names(); + if protocols .iter() - .any(|p| p.as_ref() == kademlia_protocol_name(chain_id)) + .any(|p| my_kad_names.contains(p)) { for addr in listen_addrs { swarm.behaviour_mut().kademlia.add_address(&peer_id, addr); diff --git a/crates/p2p/src/builder.rs b/crates/p2p/src/builder.rs index 73f31c0e61..4a428ae935 100644 --- a/crates/p2p/src/builder.rs +++ b/crates/p2p/src/builder.rs @@ -65,7 +65,7 @@ impl Builder { ( client, event_receiver, - MainLoop::new(swarm, command_receiver, event_sender, cfg, chain_id), + MainLoop::new(swarm, command_receiver, event_sender, cfg), ) } } diff --git a/crates/p2p/src/lib.rs b/crates/p2p/src/lib.rs index 2cddc3f823..f2f23c2774 100644 --- a/crates/p2p/src/lib.rs +++ b/crates/p2p/src/lib.rs @@ -65,8 +65,8 @@ pub struct Config { pub ip_whitelist: Vec, pub bootstrap: BootstrapConfig, pub inbound_connections_rate_limit: RateLimit, - /// Alternative protocol names for Kademlia - pub kad_names: Vec, + /// Custom protocol name for Kademlia + pub kad_name: Option, /// Request timeout for p2p-stream pub stream_timeout: Duration, /// Applies to each of the p2p-stream protocols separately diff --git a/crates/p2p/src/main_loop.rs b/crates/p2p/src/main_loop.rs index 441416dce8..7a25ce1f0a 100644 --- a/crates/p2p/src/main_loop.rs +++ b/crates/p2p/src/main_loop.rs @@ -24,7 +24,6 @@ use p2p_proto::state::StateDiffsResponse; use p2p_proto::transaction::TransactionsResponse; use p2p_proto::{ToProtobuf, TryFromProtobuf}; use p2p_stream::{self, OutboundRequestId}; -use pathfinder_common::ChainId; use tokio::sync::{mpsc, oneshot}; use tokio::time::Duration; @@ -47,7 +46,6 @@ pub struct MainLoop { // 2. update the sync head info of our peers using a different mechanism // request_sync_status: HashSetDelay, pending_queries: PendingQueries, - chain_id: ChainId, /// Ongoing Kademlia bootstrap query. ongoing_bootstrap: Option, _pending_test_queries: TestQueries, @@ -89,7 +87,6 @@ impl MainLoop { command_receiver: mpsc::Receiver, event_sender: mpsc::Sender, cfg: Config, - chain_id: ChainId, ) -> Self { Self { cfg, @@ -99,7 +96,6 @@ impl MainLoop { pending_dials: Default::default(), pending_sync_requests: Default::default(), pending_queries: Default::default(), - chain_id, ongoing_bootstrap: None, _pending_test_queries: Default::default(), } @@ -313,10 +309,9 @@ impl MainLoop { self.swarm.add_external_address(observed_addr); - if protocols - .iter() - .any(|p| p.as_ref() == behaviour::kademlia_protocol_name(self.chain_id)) - { + let my_kad_names = self.swarm.behaviour().kademlia().protocol_names(); + + if protocols.iter().any(|p| my_kad_names.contains(p)) { for addr in &listen_addrs { self.swarm .behaviour_mut() diff --git a/crates/p2p/src/test_utils/peer.rs b/crates/p2p/src/test_utils/peer.rs index 8ba7f8be1e..72c4defdb7 100644 --- a/crates/p2p/src/test_utils/peer.rs +++ b/crates/p2p/src/test_utils/peer.rs @@ -43,7 +43,7 @@ impl Config { max: 1000, interval: Duration::from_secs(1), }, - kad_names: Default::default(), + kad_name: Default::default(), stream_timeout: Duration::from_secs(10), max_concurrent_streams: 100, } diff --git a/crates/p2p/src/tests.rs b/crates/p2p/src/tests.rs index 35ea76fac1..1723a627b5 100644 --- a/crates/p2p/src/tests.rs +++ b/crates/p2p/src/tests.rs @@ -177,7 +177,7 @@ async fn periodic_bootstrap() { max: 1000, interval: Duration::from_secs(1), }, - kad_names: Default::default(), + kad_name: Default::default(), stream_timeout: Duration::from_secs(10), max_concurrent_streams: 100, }; @@ -329,7 +329,7 @@ async fn reconnect_too_quickly() { max: 1000, interval: Duration::from_secs(1), }, - kad_names: Default::default(), + kad_name: Default::default(), stream_timeout: Duration::from_secs(10), max_concurrent_streams: 100, }; @@ -434,7 +434,7 @@ async fn duplicate_connection() { max: 1000, interval: Duration::from_secs(1), }, - kad_names: Default::default(), + kad_name: Default::default(), stream_timeout: Duration::from_secs(10), max_concurrent_streams: 100, }; @@ -523,7 +523,7 @@ async fn outbound_peer_eviction() { max: 1000, interval: Duration::from_secs(1), }, - kad_names: Default::default(), + kad_name: Default::default(), stream_timeout: Duration::from_secs(10), max_concurrent_streams: 100, }; @@ -655,7 +655,7 @@ async fn inbound_peer_eviction() { max: 1000, interval: Duration::from_secs(1), }, - kad_names: Default::default(), + kad_name: Default::default(), stream_timeout: Duration::from_secs(10), max_concurrent_streams: 100, }; @@ -744,7 +744,7 @@ async fn evicted_peer_reconnection() { max: 1000, interval: Duration::from_secs(1), }, - kad_names: Default::default(), + kad_name: Default::default(), stream_timeout: Duration::from_secs(10), max_concurrent_streams: 100, }; @@ -838,7 +838,7 @@ async fn ip_whitelist() { max: 1000, interval: Duration::from_secs(1), }, - kad_names: Default::default(), + kad_name: Default::default(), stream_timeout: Duration::from_secs(10), max_concurrent_streams: 100, }; @@ -874,7 +874,7 @@ async fn ip_whitelist() { max: 1000, interval: Duration::from_secs(1), }, - kad_names: Default::default(), + kad_name: Default::default(), stream_timeout: Duration::from_secs(10), max_concurrent_streams: 100, }; @@ -911,7 +911,7 @@ async fn rate_limit() { max: 2, interval: RATE_LIMIT_INTERVAL, }, - kad_names: Default::default(), + kad_name: Default::default(), stream_timeout: Duration::from_secs(10), max_concurrent_streams: 100, }; diff --git a/crates/pathfinder/src/bin/pathfinder/config.rs b/crates/pathfinder/src/bin/pathfinder/config.rs index 17109ae3f2..36abcb42fd 100644 --- a/crates/pathfinder/src/bin/pathfinder/config.rs +++ b/crates/pathfinder/src/bin/pathfinder/config.rs @@ -473,13 +473,13 @@ Example: ip_whitelist: Vec, #[arg( - long = "p2p.experimental.kad-names", - long_help = "Comma separated list of custom Kademlia protocol names.", + long = "p2p.experimental.kad-name", + long_help = "Custom Kademlia protocol name.", value_name = "LIST", value_delimiter = ',', - env = "PATHFINDER_P2P_EXPERIMENTAL_KAD_NAMES" + env = "PATHFINDER_P2P_EXPERIMENTAL_KAD_NAME" )] - kad_names: Vec, + kad_name: Option, #[arg( long = "p2p.experimental.l1-checkpoint-override-json-path", @@ -731,7 +731,7 @@ pub struct P2PConfig { pub max_outbound_connections: usize, pub ip_whitelist: Vec, pub low_watermark: usize, - pub kad_names: Vec, + pub kad_name: Option, pub l1_checkpoint_override: Option, pub stream_timeout: Duration, pub max_concurrent_streams: usize, @@ -850,7 +850,7 @@ impl P2PConfig { .exit() } - if args.kad_names.iter().any(|x| !x.starts_with('/')) { + if args.kad_name.iter().any(|x| !x.starts_with('/')) { Cli::command() .error( ErrorKind::ValueValidation, @@ -878,7 +878,7 @@ impl P2PConfig { predefined_peers: parse_multiaddr_vec("p2p.predefined-peers", args.predefined_peers), ip_whitelist: args.ip_whitelist, low_watermark: 0, - kad_names: args.kad_names, + kad_name: args.kad_name, l1_checkpoint_override, stream_timeout: Duration::from_secs(args.stream_timeout.into()), max_concurrent_streams: args.max_concurrent_streams, diff --git a/crates/pathfinder/src/bin/pathfinder/main.rs b/crates/pathfinder/src/bin/pathfinder/main.rs index 55bec79b74..de86086f74 100644 --- a/crates/pathfinder/src/bin/pathfinder/main.rs +++ b/crates/pathfinder/src/bin/pathfinder/main.rs @@ -453,7 +453,7 @@ async fn start_p2p( max: 10, interval: Duration::from_secs(1), }, - kad_names: config.kad_names, + kad_name: config.kad_name, stream_timeout: config.stream_timeout, max_concurrent_streams: config.max_concurrent_streams, },