Skip to content

Commit

Permalink
modify dial_peer function to allocate new port on each dial (#750)
Browse files Browse the repository at this point in the history
  • Loading branch information
sh3ll3x3c authored Nov 22, 2024
1 parent f16de98 commit df7d0c4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
2 changes: 2 additions & 0 deletions core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## 1.0.5

- Allocate new port on each new dial attempt
- Set different dial conditions for bootstrap process and diagnostics API
- Move p2p diagnostics APIs to its own module
- Decrease connection idle timeout to 10s
- Enforce project name as its own distinct type
Expand Down
11 changes: 9 additions & 2 deletions core/src/api/diagnostics/p2p.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ use crate::{
api::types::Error,
network::p2p::{self, MultiAddressInfo},
};
use libp2p::{swarm::DialError, Multiaddr, PeerId};
use libp2p::{
swarm::{dial_opts::PeerCondition, DialError},
Multiaddr, PeerId,
};
use serde::{Deserialize, Serialize};
use warp::reply::Reply;

Expand Down Expand Up @@ -115,7 +118,11 @@ pub async fn dial_external_peer(
peer_address: ExternalPeerMultiaddress,
) -> Result<ExternalPeerDialResponse, Error> {
p2p_client
.dial_peer(peer_address.peer_id, vec![peer_address.multiaddress])
.dial_peer(
peer_address.peer_id,
vec![peer_address.multiaddress],
PeerCondition::NotDialing,
)
.await
.map(|connection_info| ExternalPeerDialResponse {
dial_success: Some(ExternalPeerDialSuccess {
Expand Down
13 changes: 9 additions & 4 deletions core/src/network/p2p/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use futures::future::join_all;
use libp2p::{
core::transport::ListenerId,
kad::{store::RecordStore, Mode, PeerRecord, Quorum, Record, RecordKey},
swarm::dial_opts::DialOpts,
swarm::dial_opts::{DialOpts, PeerCondition},
Multiaddr, PeerId,
};
use std::time::{Duration, Instant};
Expand Down Expand Up @@ -131,10 +131,15 @@ impl Client {
&self,
peer_id: PeerId,
peer_address: Vec<Multiaddr>,
dial_condition: PeerCondition,
) -> Result<ConnectionEstablishedInfo> {
self.execute_sync(|response_sender| {
Box::new(move |context: &mut EventLoop| {
let opts = DialOpts::peer_id(peer_id).addresses(peer_address).build();
let opts = DialOpts::peer_id(peer_id)
.addresses(peer_address)
.allocate_new_port()
.condition(dial_condition)
.build();
context.swarm.dial(opts)?;

context
Expand Down Expand Up @@ -163,9 +168,9 @@ impl Client {
// Bootstrap nodes are also used as autonat servers
pub async fn bootstrap_on_startup(&self, bootstraps: &[MultiaddrConfig]) -> Result<()> {
for (peer, addr) in bootstraps.iter().map(Into::into) {
self.dial_peer(peer, vec![addr.clone()])
self.dial_peer(peer, vec![addr.clone()], PeerCondition::Always)
.await
.wrap_err("Dialing Bootstrap peer failed.")?;
.map_err(|e| eyre!("Failed to dial bootstrap peer: {e}"))?;
self.add_address(peer, addr.clone()).await?;

self.add_autonat_server(peer, addr).await?;
Expand Down

0 comments on commit df7d0c4

Please sign in to comment.