Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modify dial_peer function to allocate new port on each dial #750

Merged
merged 2 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading