Skip to content

Commit 6d88b70

Browse files
authored
Merge pull request #1137 from openmina/fix/initial-peers-resolve-failure
fix(p2p): Do not fail when an initial peer address cannot be resolved
2 parents 29a711c + d2229b0 commit 6d88b70

File tree

3 files changed

+35
-10
lines changed

3 files changed

+35
-10
lines changed

node/native/src/node/builder.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -392,12 +392,25 @@ fn peers_from_reader(
392392
peers: &mut Vec<P2pConnectionOutgoingInitOpts>,
393393
read: impl Read,
394394
) -> anyhow::Result<()> {
395-
let read = BufReader::new(read);
396-
for line in read.lines() {
395+
let reader = BufReader::new(read);
396+
for line in reader.lines() {
397397
let line = line.context("reading line")?;
398-
let l = line.trim();
399-
if !l.is_empty() {
400-
peers.push(l.parse().context(anyhow::anyhow!("parsing entry"))?);
398+
let trimmed = line.trim();
399+
if trimmed.is_empty() {
400+
continue;
401+
}
402+
match trimmed.parse::<P2pConnectionOutgoingInitOpts>() {
403+
Ok(opts) => {
404+
if let Some(opts) = opts.with_host_resolved() {
405+
peers.push(opts);
406+
} else {
407+
openmina_core::warn!(
408+
"Peer address name resolution failed, skipping: {:?}",
409+
trimmed
410+
);
411+
}
412+
}
413+
Err(e) => openmina_core::warn!("Peer address parse error: {:?}", e),
401414
}
402415
}
403416
Ok(())

node/testing/src/hosts.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub fn devnet() -> Vec<ListenerNode> {
2323
.split_whitespace()
2424
.map(P2pConnectionOutgoingInitOpts::from_str)
2525
.filter_map(Result::ok)
26+
.filter_map(|p| p.with_host_resolved())
2627
.map(Into::into)
2728
.collect()
2829
}

p2p/src/connection/outgoing/mod.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ pub enum P2pConnectionOutgoingInitOpts {
3939
LibP2P(P2pConnectionOutgoingInitLibp2pOpts),
4040
}
4141

42+
impl P2pConnectionOutgoingInitOpts {
43+
pub fn with_host_resolved(self) -> Option<Self> {
44+
if let Self::LibP2P(libp2p_opts) = self {
45+
Some(Self::LibP2P(libp2p_opts.with_host_resolved()?))
46+
} else {
47+
Some(self)
48+
}
49+
}
50+
}
51+
4252
#[derive(BinProtWrite, BinProtRead, Eq, PartialEq, Ord, PartialOrd, Debug, Clone)]
4353
pub struct P2pConnectionOutgoingInitLibp2pOpts {
4454
pub peer_id: PeerId,
@@ -80,6 +90,11 @@ impl P2pConnectionOutgoingInitLibp2pOpts {
8090
}
8191
}
8292
}
93+
94+
pub fn with_host_resolved(mut self) -> Option<Self> {
95+
self.host = self.host.resolve()?;
96+
Some(self)
97+
}
8398
}
8499

85100
pub(crate) mod libp2p_opts {
@@ -446,11 +461,7 @@ impl TryFrom<&multiaddr::Multiaddr> for P2pConnectionOutgoingInitLibp2pOpts {
446461
host: match iter.next() {
447462
Some(Protocol::Ip4(v)) => Host::Ipv4(v),
448463
Some(Protocol::Dns(v) | Protocol::Dns4(v) | Protocol::Dns6(v)) => {
449-
Host::Domain(v.to_string()).resolve().ok_or(
450-
P2pConnectionOutgoingInitOptsParseError::Other(format!(
451-
"cannot resolve host {v}"
452-
)),
453-
)?
464+
Host::Domain(v.to_string())
454465
}
455466
Some(_) => {
456467
return Err(P2pConnectionOutgoingInitOptsParseError::Other(

0 commit comments

Comments
 (0)