Skip to content

Commit ab0c027

Browse files
authored
Merge pull request #834 from openmina/feat/p2p/configure-external-ip
Configure external ip addresses
2 parents 99f3d72 + fa60fba commit ab0c027

File tree

7 files changed

+36
-2
lines changed

7 files changed

+36
-2
lines changed

cli/src/commands/node/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ pub struct Node {
4444
#[arg(env = "MINA_LIBP2P_PASS")]
4545
pub libp2p_password: Option<String>,
4646

47+
/// List of external addresses at which this node is accessible
48+
#[arg(long)]
49+
pub libp2p_external_ip: Vec<String>,
50+
4751
/// Http port to listen on
4852
#[arg(long, short, env, default_value = "3000")]
4953
pub port: u16,
@@ -194,6 +198,12 @@ impl Node {
194198

195199
node_builder.p2p_libp2p_port(self.libp2p_port);
196200

201+
node_builder.external_addrs(
202+
self.libp2p_external_ip
203+
.into_iter()
204+
.filter_map(|s| s.parse().ok()),
205+
);
206+
197207
self.seed.then(|| node_builder.p2p_seed_node());
198208
self.no_peers_discovery
199209
.then(|| node_builder.p2p_no_discovery());

node/native/src/node/builder.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::{
22
fs::File,
33
io::{BufRead, BufReader, Read},
4+
net::IpAddr,
45
path::Path,
56
sync::Arc,
67
time::Duration,
@@ -40,6 +41,7 @@ pub struct NodeBuilder {
4041
p2p_no_discovery: bool,
4142
p2p_is_started: bool,
4243
initial_peers: Vec<P2pConnectionOutgoingInitOpts>,
44+
external_addrs: Vec<IpAddr>,
4345
block_producer: Option<BlockProducerConfig>,
4446
snarker: Option<SnarkerConfig>,
4547
service: NodeServiceBuilder,
@@ -73,6 +75,7 @@ impl NodeBuilder {
7375
p2p_no_discovery: false,
7476
p2p_is_started: false,
7577
initial_peers: Vec::new(),
78+
external_addrs: Vec::new(),
7679
block_producer: None,
7780
snarker: None,
7881
service: NodeServiceBuilder::new(rng_seed),
@@ -121,6 +124,11 @@ impl NodeBuilder {
121124
self
122125
}
123126

127+
pub fn external_addrs(&mut self, v: impl Iterator<Item = IpAddr>) -> &mut Self {
128+
self.external_addrs.extend(v);
129+
self
130+
}
131+
124132
/// Extend p2p initial peers from file.
125133
pub fn initial_peers_from_file(&mut self, path: impl AsRef<Path>) -> anyhow::Result<&mut Self> {
126134
peers_from_reader(
@@ -282,6 +290,8 @@ impl NodeBuilder {
282290
})
283291
.collect();
284292

293+
let external_addrs = self.external_addrs;
294+
285295
let srs = self.verifier_srs.unwrap_or_else(get_srs);
286296
let block_verifier_index = self
287297
.block_verifier_index
@@ -311,6 +321,7 @@ impl NodeBuilder {
311321
listen_port: self.http_port,
312322
identity_pub_key: p2p_sec_key.public_key(),
313323
initial_peers,
324+
external_addrs,
314325
ask_initial_peers_interval: Duration::from_secs(3600),
315326
enabled_channels: ChannelId::iter_all().collect(),
316327
peer_discovery: !self.p2p_no_discovery,

node/testing/src/cluster/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ impl Cluster {
281281
listen_port: Some(http_port),
282282
identity_pub_key: p2p_sec_key.public_key(),
283283
initial_peers,
284+
external_addrs: vec![],
284285
ask_initial_peers_interval: testing_config.ask_initial_peers_interval,
285286
enabled_channels: ChannelId::iter_all().collect(),
286287
peer_discovery: true,

node/web/src/node/builder.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ impl NodeBuilder {
230230
listen_port: None,
231231
identity_pub_key: p2p_sec_key.public_key(),
232232
initial_peers,
233+
external_addrs: vec![],
233234
ask_initial_peers_interval: Duration::from_secs(3600),
234235
enabled_channels: ChannelId::iter_all().collect(),
235236
peer_discovery: !self.p2p_no_discovery,

p2p/src/network/identify/stream_effectful/p2p_network_identify_stream_effectful_effects.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,15 @@ impl P2pNetworkIdentifyStreamEffectfulAction {
4949
peer_id,
5050
stream_id,
5151
} => {
52-
let mut listen_addrs = Vec::new();
52+
let config = &store.state().config;
53+
let ips = &config.external_addrs;
54+
let port = config.libp2p_port.unwrap_or(8302);
55+
56+
let mut listen_addrs = ips
57+
.iter()
58+
.map(|ip| Multiaddr::from(*ip).with(multiaddr::Protocol::Tcp(port)))
59+
.collect::<Vec<_>>();
60+
5361
for addr in store
5462
.state()
5563
.network

p2p/src/p2p_config.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{collections::BTreeSet, time::Duration};
1+
use std::{collections::BTreeSet, net::IpAddr, time::Duration};
22

33
use serde::{Deserialize, Serialize};
44

@@ -22,6 +22,8 @@ pub struct P2pConfig {
2222
pub identity_pub_key: PublicKey,
2323
/// A list addresses of seed nodes.
2424
pub initial_peers: Vec<P2pConnectionOutgoingInitOpts>,
25+
/// External addresses
26+
pub external_addrs: Vec<IpAddr>,
2527

2628
/// The time interval that must elapse before the next peer discovery request.
2729
/// The node periodically polls peers for their connections to keep our list up to date.

p2p/testing/src/cluster.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ impl Cluster {
354354
listen_port: Some(listen_port),
355355
identity_pub_key: secret_key.public_key(),
356356
initial_peers,
357+
external_addrs: vec![],
357358
ask_initial_peers_interval: Duration::from_secs(5),
358359
enabled_channels: p2p::channels::ChannelId::for_libp2p().collect(),
359360
peer_discovery: config.discovery,

0 commit comments

Comments
 (0)