Skip to content

Commit eb49a9d

Browse files
committed
p2p: Introduce MdnsConfig
1 parent 2b5da02 commit eb49a9d

File tree

9 files changed

+126
-66
lines changed

9 files changed

+126
-66
lines changed

node/src/config.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use anyhow::{Context, Result};
2424
use serde::{Deserialize, Serialize};
2525

2626
use chainstate::ChainstateConfig;
27-
use p2p::config::P2pConfig;
27+
use p2p::config::{MdnsConfig, P2pConfig};
2828
use rpc::RpcConfig;
2929

3030
use crate::RunOptions;
@@ -101,26 +101,23 @@ fn p2p_config(config: P2pConfig, options: &RunOptions) -> P2pConfig {
101101
bind_address,
102102
ban_threshold,
103103
outbound_connection_timeout,
104-
enable_mdns,
105-
mdns_query_interval,
106-
mdns_enable_ipv6,
104+
mdns_config: _,
107105
} = config;
108106

109107
let bind_address = options.p2p_addr.clone().unwrap_or(bind_address);
110108
let ban_threshold = options.p2p_ban_threshold.unwrap_or(ban_threshold);
111109
let outbound_connection_timeout =
112110
options.p2p_outbound_connection_timeout.unwrap_or(outbound_connection_timeout);
113-
let enable_mdns = options.enable_mdns.unwrap_or(enable_mdns);
114-
let mdns_query_interval = options.mdns_query_interval.unwrap_or(mdns_query_interval);
115-
let mdns_enable_ipv6 = options.mdns_enable_ipv6.unwrap_or(mdns_enable_ipv6);
116111

117112
P2pConfig {
118113
bind_address,
119114
ban_threshold,
120115
outbound_connection_timeout,
121-
enable_mdns,
122-
mdns_query_interval,
123-
mdns_enable_ipv6,
116+
mdns_config: MdnsConfig::from_options(
117+
options.p2p_enable_mdns.unwrap_or(false),
118+
options.p2p_mdns_query_interval,
119+
options.p2p_enable_ipv6_mdns_discovery,
120+
),
124121
}
125122
}
126123

node/src/options.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,21 +77,21 @@ pub struct RunOptions {
7777
#[clap(long)]
7878
pub p2p_ban_threshold: Option<u32>,
7979

80-
/// The p2p timeout value in seconds.
80+
/// Use IPv6 instead of IPv4 for mDNS.
8181
#[clap(long)]
82-
pub p2p_outbound_connection_timeout: Option<u64>,
82+
pub p2p_enable_ipv6_mdns_discovery: Option<bool>,
8383

8484
/// Enable multicast DNS peer discovery
8585
#[clap(long)]
86-
pub enable_mdns: Option<bool>,
86+
pub p2p_enable_mdns: Option<bool>,
8787

8888
/// Interval (in milliseconds) at which to poll the network for new peers.
8989
#[clap(long)]
90-
pub mdns_query_interval: Option<u64>,
90+
pub p2p_mdns_query_interval: Option<u64>,
9191

92-
/// Use IPv6 instead of IPv4 for mDNS.
92+
/// The p2p timeout value in seconds.
9393
#[clap(long)]
94-
pub mdns_enable_ipv6: Option<bool>,
94+
pub p2p_outbound_connection_timeout: Option<u64>,
9595

9696
/// Address to bind RPC to.
9797
#[clap(long, value_name = "ADDR")]

node/tests/cli.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ fn read_config_override_values() {
9797
p2p_addr: Some(p2p_addr.into()),
9898
p2p_ban_threshold: Some(p2p_ban_threshold),
9999
p2p_outbound_connection_timeout: Some(p2p_timeout),
100-
enable_mdns: Some(enable_mdns),
101-
mdns_query_interval: None,
102-
mdns_enable_ipv6: None,
100+
p2p_enable_mdns: Some(enable_mdns),
101+
p2p_mdns_query_interval: None,
102+
p2p_enable_ipv6_mdns_discovery: None,
103103
rpc_addr: Some(rpc_addr),
104104
};
105105
let config = NodeConfig::read(&config_path, &options).unwrap();
@@ -172,9 +172,9 @@ fn default_run_options() -> RunOptions {
172172
p2p_addr: None,
173173
p2p_ban_threshold: None,
174174
p2p_outbound_connection_timeout: None,
175-
enable_mdns: None,
176-
mdns_query_interval: None,
177-
mdns_enable_ipv6: None,
175+
p2p_enable_mdns: None,
176+
p2p_mdns_query_interval: None,
177+
p2p_enable_ipv6_mdns_discovery: None,
178178
rpc_addr: None,
179179
}
180180
}

p2p/src/config.rs

Lines changed: 68 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,54 @@
1515

1616
use serde::{Deserialize, Serialize};
1717

18+
pub const MDNS_DEFAULT_QUERY_INTERVAL: u64 = 0;
19+
pub const MDNS_DEFAULT_IPV6_STATE: bool = false;
20+
21+
/// Multicast DNS configuration.
22+
#[derive(Serialize, Deserialize, Debug)]
23+
#[serde(tag = "state")]
24+
pub enum MdnsConfig {
25+
Enabled {
26+
/// Interval (in milliseconds) at which to poll the network for new peers.
27+
query_interval: u64,
28+
/// Use IPv6 for multicast DNS
29+
enable_ipv6_mdns_discovery: bool,
30+
},
31+
Disabled,
32+
}
33+
34+
impl MdnsConfig {
35+
pub fn new() -> Self {
36+
MdnsConfig::Disabled
37+
}
38+
39+
pub fn from_options(
40+
enable_mdns: bool,
41+
query_interval: Option<u64>,
42+
enable_ipv6_mdns_discovery: Option<bool>,
43+
) -> Self {
44+
if enable_mdns {
45+
MdnsConfig::Enabled {
46+
query_interval: query_interval.unwrap_or(MDNS_DEFAULT_QUERY_INTERVAL),
47+
enable_ipv6_mdns_discovery: enable_ipv6_mdns_discovery
48+
.unwrap_or(MDNS_DEFAULT_IPV6_STATE),
49+
}
50+
} else {
51+
// TODO: make the check for these automatic
52+
assert!(
53+
query_interval.is_none(),
54+
"mDNS is disabled but query interval is specified"
55+
);
56+
assert!(
57+
enable_ipv6_mdns_discovery.is_none(),
58+
"mDNS is disabled but transport over IPv6 is enabled"
59+
);
60+
61+
MdnsConfig::Disabled
62+
}
63+
}
64+
}
65+
1866
/// The p2p subsystem configuration.
1967
#[derive(Serialize, Deserialize, Debug)]
2068
pub struct P2pConfig {
@@ -24,12 +72,8 @@ pub struct P2pConfig {
2472
pub ban_threshold: u32,
2573
/// The outbound connection timeout value in seconds.
2674
pub outbound_connection_timeout: u64,
27-
/// Enable multicast DNS peer discovery.
28-
pub enable_mdns: bool,
29-
/// Interval (in milliseconds) at which to poll the network for new peers.
30-
pub mdns_query_interval: u64,
31-
/// Use IPv6 for multicast DNS
32-
pub mdns_enable_ipv6: bool,
75+
/// Multicast DNS configuration.
76+
pub mdns_config: MdnsConfig,
3377
}
3478

3579
impl P2pConfig {
@@ -45,9 +89,24 @@ impl Default for P2pConfig {
4589
bind_address: "/ip6/::1/tcp/3031".into(),
4690
ban_threshold: 100,
4791
outbound_connection_timeout: 10,
48-
enable_mdns: false,
49-
mdns_query_interval: 0,
50-
mdns_enable_ipv6: false,
92+
mdns_config: MdnsConfig::Disabled,
5193
}
5294
}
5395
}
96+
97+
#[cfg(test)]
98+
mod tests {
99+
use super::*;
100+
101+
#[test]
102+
#[should_panic]
103+
fn mdsn_disabled_but_query_interval_specified() {
104+
MdnsConfig::from_options(false, Some(200), None);
105+
}
106+
107+
#[test]
108+
#[should_panic]
109+
fn mdsn_disabled_but_ipv6_enabled() {
110+
MdnsConfig::from_options(false, None, Some(true));
111+
}
112+
}

p2p/src/net/libp2p/discovery/mdns.rs

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
//! Multicast DNS (mDNS) discovery behaviour for the libp2p backend
1919
20+
use crate::config;
2021
use libp2p::{
2122
core::connection::{ConnectedPoint, ConnectionId},
2223
mdns,
@@ -38,23 +39,27 @@ pub enum Mdns {
3839
}
3940

4041
impl Mdns {
41-
pub async fn new(enabled: bool, enable_ipv6: bool, query_interval: u64) -> Self {
42-
if enabled {
43-
match mdns::Mdns::new(mdns::MdnsConfig {
44-
ttl: Default::default(),
45-
query_interval: std::time::Duration::from_millis(query_interval),
46-
enable_ipv6,
47-
})
48-
.await
49-
{
50-
Ok(mdns) => Mdns::Enabled(Box::new(mdns)),
51-
Err(err) => {
52-
log::error!("Failed to initialize mDNS: {:?}", err);
53-
Mdns::Disabled
42+
pub async fn new(config: &config::MdnsConfig) -> Self {
43+
match config {
44+
config::MdnsConfig::Enabled {
45+
query_interval,
46+
enable_ipv6_mdns_discovery,
47+
} => {
48+
match mdns::Mdns::new(mdns::MdnsConfig {
49+
ttl: Default::default(),
50+
query_interval: std::time::Duration::from_millis(*query_interval),
51+
enable_ipv6: *enable_ipv6_mdns_discovery,
52+
})
53+
.await
54+
{
55+
Ok(mdns) => Mdns::Enabled(Box::new(mdns)),
56+
Err(err) => {
57+
log::error!("Failed to initialize mDNS: {:?}", err);
58+
Mdns::Disabled
59+
}
5460
}
5561
}
56-
} else {
57-
Mdns::Disabled
62+
config::MdnsConfig::Disabled => Mdns::Disabled,
5863
}
5964
}
6065

@@ -102,7 +107,7 @@ mod tests {
102107

103108
#[tokio::test]
104109
async fn mdns_disabled() {
105-
let mdns = Mdns::new(false, false, 0).await;
110+
let mdns = Mdns::new(&config::MdnsConfig::new()).await;
106111
assert!(std::matches!(mdns, Mdns::Disabled));
107112
}
108113

@@ -154,14 +159,22 @@ mod tests {
154159
}
155160

156161
let tester1 = MdnsTester {
157-
mdns: Mdns::new(true, false, 200).await,
162+
mdns: Mdns::new(&config::MdnsConfig::Enabled {
163+
query_interval: 200,
164+
enable_ipv6_mdns_discovery: false,
165+
})
166+
.await,
158167
poll_params: TestParams {
159168
peer_id: PeerId::random(),
160169
addr: "/ip6/::1/tcp/9999".parse().unwrap(),
161170
},
162171
};
163172
let tester2 = MdnsTester {
164-
mdns: Mdns::new(true, false, 200).await,
173+
mdns: Mdns::new(&config::MdnsConfig::Enabled {
174+
query_interval: 200,
175+
enable_ipv6_mdns_discovery: false,
176+
})
177+
.await,
165178
poll_params: TestParams {
166179
peer_id: PeerId::random(),
167180
addr: "/ip6/::1/tcp/8888".parse().unwrap(),

p2p/src/net/libp2p/discovery/mod.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,7 @@ pub struct DiscoveryManager {
5353
impl DiscoveryManager {
5454
pub async fn new(p2p_config: Arc<config::P2pConfig>) -> Self {
5555
Self {
56-
mdns: mdns::Mdns::new(
57-
p2p_config.enable_mdns,
58-
p2p_config.mdns_enable_ipv6,
59-
p2p_config.mdns_query_interval,
60-
)
61-
.await,
56+
mdns: mdns::Mdns::new(&p2p_config.mdns_config).await,
6257
}
6358
}
6459
}

p2p/src/net/libp2p/tests/mdns.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ async fn test_discovered_and_expired() {
3131
let (mut backend1, _, _conn_rx, _, _) = make_libp2p(
3232
common::chain::config::create_mainnet(),
3333
Arc::new(config::P2pConfig {
34-
enable_mdns: true,
35-
mdns_query_interval: 200,
34+
mdns_config: config::MdnsConfig::from_options(true, Some(200), None),
3635
..Default::default()
3736
}),
3837
make_libp2p_addr(),
@@ -43,8 +42,7 @@ async fn test_discovered_and_expired() {
4342
let (mut backend2, _, _, _, _) = make_libp2p(
4443
common::chain::config::create_mainnet(),
4544
Arc::new(config::P2pConfig {
46-
enable_mdns: true,
47-
mdns_query_interval: 200,
45+
mdns_config: config::MdnsConfig::from_options(true, Some(200), None),
4846
..Default::default()
4947
}),
5048
make_libp2p_addr(),

p2p/tests/libp2p-mdns.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ async fn test_libp2p_peer_discovery() {
3333
make_libp2p_addr(),
3434
Arc::clone(&config),
3535
Arc::new(config::P2pConfig {
36-
enable_mdns: true,
37-
mdns_query_interval: 200,
36+
mdns_config: config::MdnsConfig::from_options(true, Some(200), None),
3837
..Default::default()
3938
}),
4039
)
@@ -45,8 +44,7 @@ async fn test_libp2p_peer_discovery() {
4544
make_libp2p_addr(),
4645
Arc::clone(&config),
4746
Arc::new(config::P2pConfig {
48-
enable_mdns: true,
49-
mdns_query_interval: 200,
47+
mdns_config: config::MdnsConfig::from_options(true, Some(200), None),
5048
..Default::default()
5149
}),
5250
)

test/functional/test_framework/.mintlayer/config.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ max_orphan_blocks = 512
88
bind_address = "/ip6/::1/tcp/3031"
99
ban_threshold = 100
1010
outbound_connection_timeout = 10
11-
enable_mdns = false
12-
mdns_query_interval = 0
13-
mdns_enable_ipv6 = false
11+
12+
[p2p.mdns_config]
13+
state = "Disabled"
1414

1515
[rpc]
1616
bind_address = "127.0.0.1:3030"

0 commit comments

Comments
 (0)