Skip to content

Commit 643d4ff

Browse files
kad: Allow connecting to more than one DHT network (#473)
Needed for IPFS support in substrate, because we need to connect to polkadot & IPFS DHTs simultaneously. Close #471.
1 parent bb21357 commit 643d4ff

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

src/config.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ pub struct ConfigBuilder {
9393
identify: Option<identify::Config>,
9494

9595
/// Kademlia protocol config.
96-
kademlia: Option<kademlia::Config>,
96+
kademlia: Vec<kademlia::Config>,
9797

9898
/// Bitswap protocol config.
9999
bitswap: Option<bitswap::Config>,
@@ -149,7 +149,7 @@ impl ConfigBuilder {
149149
keypair: None,
150150
ping: None,
151151
identify: None,
152-
kademlia: None,
152+
kademlia: Vec::new(),
153153
bitswap: None,
154154
mdns: None,
155155
executor: None,
@@ -219,7 +219,7 @@ impl ConfigBuilder {
219219

220220
/// Enable IPFS Kademlia protocol.
221221
pub fn with_libp2p_kademlia(mut self, config: kademlia::Config) -> Self {
222-
self.kademlia = Some(config);
222+
self.kademlia.push(config);
223223
self
224224
}
225225

@@ -307,7 +307,7 @@ impl ConfigBuilder {
307307
websocket: self.websocket.take(),
308308
ping: self.ping.take(),
309309
identify: self.identify.take(),
310-
kademlia: self.kademlia.take(),
310+
kademlia: self.kademlia,
311311
bitswap: self.bitswap.take(),
312312
max_parallel_dials: self.max_parallel_dials,
313313
executor: self.executor.map_or(Arc::new(DefaultExecutor {}), |executor| executor),
@@ -349,7 +349,7 @@ pub struct Litep2pConfig {
349349
pub(crate) identify: Option<identify::Config>,
350350

351351
/// Kademlia protocol configuration, if enabled.
352-
pub(crate) kademlia: Option<kademlia::Config>,
352+
pub(crate) kademlia: Vec<kademlia::Config>,
353353

354354
/// Bitswap protocol configuration, if enabled.
355355
pub(crate) bitswap: Option<bitswap::Config>,

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,8 @@ impl Litep2p {
259259
}));
260260
}
261261

262-
// start kademlia protocol event loop if enabled
263-
if let Some(kademlia_config) = litep2p_config.kademlia.take() {
262+
// start kademlia protocol event loops
263+
for kademlia_config in litep2p_config.kademlia.into_iter() {
264264
tracing::debug!(
265265
target: LOG_TARGET,
266266
protocol_names = ?kademlia_config.protocol_names,

src/protocol/libp2p/kademlia/mod.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,9 @@ impl Kademlia {
240240
self.routing_table.on_connection_established(Key::from(peer), endpoint);
241241

242242
let Some(actions) = self.pending_dials.remove(&peer) else {
243-
entry.insert(PeerContext::new());
243+
// Note that we do not add peer entry if we don't have any pending actions.
244+
// This is done to not populate `self.peers` with peers that don't support
245+
// our Kademlia protocol.
244246
return Ok(());
245247
};
246248

@@ -343,6 +345,7 @@ impl Kademlia {
343345
let pending_action = &mut self
344346
.peers
345347
.get_mut(&peer)
348+
// If we opened an outbound substream, we must have pending actions for the peer.
346349
.ok_or(Error::PeerDoesntExist(peer))?
347350
.pending_actions
348351
.remove(&substream_id);
@@ -412,6 +415,10 @@ impl Kademlia {
412415
async fn on_inbound_substream(&mut self, peer: PeerId, substream: Substream) {
413416
tracing::trace!(target: LOG_TARGET, ?peer, "inbound substream opened");
414417

418+
// Ensure peer entry exists to treat peer as [`ConnectionType::Connected`].
419+
// when inserting into the routing table.
420+
self.peers.entry(peer).or_default();
421+
415422
self.executor.read_message(peer, None, substream);
416423
}
417424

0 commit comments

Comments
 (0)