Skip to content

Commit

Permalink
feat: allow one custom kad name in config
Browse files Browse the repository at this point in the history
  • Loading branch information
CHr15F0x committed Sep 19, 2024
1 parent 60757b2 commit f8e8af4
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 43 deletions.
5 changes: 5 additions & 0 deletions crates/p2p/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 /")
Expand Down Expand Up @@ -724,6 +725,10 @@ impl Behaviour {
});
}

pub fn kademlia(&self) -> &kad::Behaviour<MemoryStore> {
&self.inner.kademlia
}

pub fn kademlia_mut(&mut self) -> &mut kad::Behaviour<MemoryStore> {
&mut self.inner.kademlia
}
Expand Down
17 changes: 5 additions & 12 deletions crates/p2p/src/behaviour/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Result<Vec<_>, _>>()
.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)));
Expand Down
5 changes: 3 additions & 2 deletions crates/p2p/src/bin/bootstrap/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion crates/p2p/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)
}
}
4 changes: 2 additions & 2 deletions crates/p2p/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ pub struct Config {
pub ip_whitelist: Vec<IpNet>,
pub bootstrap: BootstrapConfig,
pub inbound_connections_rate_limit: RateLimit,
/// Alternative protocol names for Kademlia
pub kad_names: Vec<String>,
/// Custom protocol name for Kademlia
pub kad_name: Option<String>,
/// Request timeout for p2p-stream
pub stream_timeout: Duration,
/// Applies to each of the p2p-stream protocols separately
Expand Down
11 changes: 3 additions & 8 deletions crates/p2p/src/main_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -47,7 +46,6 @@ pub struct MainLoop {
// 2. update the sync head info of our peers using a different mechanism
// request_sync_status: HashSetDelay<PeerId>,
pending_queries: PendingQueries,
chain_id: ChainId,
/// Ongoing Kademlia bootstrap query.
ongoing_bootstrap: Option<QueryId>,
_pending_test_queries: TestQueries,
Expand Down Expand Up @@ -89,7 +87,6 @@ impl MainLoop {
command_receiver: mpsc::Receiver<Command>,
event_sender: mpsc::Sender<Event>,
cfg: Config,
chain_id: ChainId,
) -> Self {
Self {
cfg,
Expand All @@ -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(),
}
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion crates/p2p/src/test_utils/peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
18 changes: 9 additions & 9 deletions crates/p2p/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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,
};
Expand Down Expand Up @@ -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,
};
Expand Down Expand Up @@ -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,
};
Expand Down Expand Up @@ -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,
};
Expand Down Expand Up @@ -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,
};
Expand Down Expand Up @@ -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,
};
Expand Down Expand Up @@ -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,
};
Expand Down Expand Up @@ -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,
};
Expand Down
14 changes: 7 additions & 7 deletions crates/pathfinder/src/bin/pathfinder/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,13 +473,13 @@ Example:
ip_whitelist: Vec<IpNet>,

#[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<String>,
kad_name: Option<String>,

#[arg(
long = "p2p.experimental.l1-checkpoint-override-json-path",
Expand Down Expand Up @@ -731,7 +731,7 @@ pub struct P2PConfig {
pub max_outbound_connections: usize,
pub ip_whitelist: Vec<IpNet>,
pub low_watermark: usize,
pub kad_names: Vec<String>,
pub kad_name: Option<String>,
pub l1_checkpoint_override: Option<pathfinder_ethereum::EthereumStateUpdate>,
pub stream_timeout: Duration,
pub max_concurrent_streams: usize,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion crates/pathfinder/src/bin/pathfinder/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down

0 comments on commit f8e8af4

Please sign in to comment.