Skip to content

Commit 4f67c94

Browse files
committed
Refactor network address parsing in LibP2pNetworkService for improved clarity. Simplify error handling in the bootstrap peers loop and streamline event handling for Kademlia bootstrap initiation.
1 parent 021e904 commit 4f67c94

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

src/infrastructure/network.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -147,18 +147,16 @@ impl LibP2pNetworkService {
147147
];
148148

149149
for addr_str in &bootstrap_peers {
150-
if let Ok(addr) = addr_str.parse::<Multiaddr>() {
151-
if let Some(peer_id) = addr.iter().find_map(|p| {
152-
if let libp2p::multiaddr::Protocol::P2p(peer_id) = p {
153-
Some(peer_id)
154-
} else {
155-
None
156-
}
157-
}) {
158-
kademlia.add_address(&peer_id, addr);
159-
info!("Added Kademlia bootstrap peer: {}", peer_id);
150+
let Ok(addr) = addr_str.parse::<Multiaddr>() else { continue };
151+
let Some(peer_id) = addr.iter().find_map(|p| {
152+
if let libp2p::multiaddr::Protocol::P2p(peer_id) = p {
153+
Some(peer_id)
154+
} else {
155+
None
160156
}
161-
}
157+
}) else { continue };
158+
kademlia.add_address(&peer_id, addr);
159+
info!("Added Kademlia bootstrap peer: {}", peer_id);
162160
}
163161

164162
// Create behaviour
@@ -232,15 +230,13 @@ impl LibP2pNetworkService {
232230
// Handle swarm events
233231
event = swarm.select_next_some() => {
234232
// Trigger Kademlia bootstrap once we start listening
235-
if !bootstrap_attempted {
236-
if let SwarmEvent::NewListenAddr { .. } = event {
233+
if !bootstrap_attempted && matches!(event, SwarmEvent::NewListenAddr { .. }) {
237234
if let Err(e) = swarm.behaviour_mut().kademlia.bootstrap() {
238235
warn!("Kademlia bootstrap failed: {:?}", e);
239236
} else {
240237
info!("Kademlia bootstrap initiated successfully");
241238
bootstrap_attempted = true;
242239
}
243-
}
244240
}
245241

246242
if let Err(e) = Self::handle_swarm_event(

0 commit comments

Comments
 (0)