Correct discovery address CLI functionality (#818)

* Improve handling of discovery IP address CLI config

* Remove excess debug logging

* Add reviewers suggestions
This commit is contained in:
Age Manning
2020-01-23 17:31:08 +11:00
committed by GitHub
parent fdb6e28f94
commit 8c96739cab
4 changed files with 22 additions and 15 deletions

View File

@@ -20,8 +20,9 @@ pub struct Config {
/// The TCP port that libp2p listens on.
pub libp2p_port: u16,
/// The address to broadcast to peers about which address we are listening on.
pub discovery_address: std::net::IpAddr,
/// The address to broadcast to peers about which address we are listening on. None indicates
/// that no discovery address has been set in the CLI args.
pub discovery_address: Option<std::net::IpAddr>,
/// UDP port that discovery listens on.
pub discovery_port: u16,
@@ -86,7 +87,7 @@ impl Default for Config {
network_dir,
listen_address: "127.0.0.1".parse().expect("valid ip address"),
libp2p_port: 9000,
discovery_address: "127.0.0.1".parse().expect("valid ip address"),
discovery_address: None,
discovery_port: 9000,
max_peers: 10,
secret_key_hex: None,

View File

@@ -310,7 +310,9 @@ fn load_enr(
// Note: Discovery should update the ENR record's IP to the external IP as seen by the
// majority of our peers.
let mut local_enr = EnrBuilder::new("v4")
.ip(config.discovery_address)
.ip(config
.discovery_address
.unwrap_or_else(|| "127.0.0.1".parse().expect("valid ip")))
.tcp(config.libp2p_port)
.udp(config.discovery_port)
.build(&local_key)
@@ -325,7 +327,8 @@ fn load_enr(
match Enr::from_str(&enr_string) {
Ok(enr) => {
if enr.node_id() == local_enr.node_id() {
if enr.ip().map(Into::into) == Some(config.discovery_address)
if (config.discovery_address.is_none()
|| enr.ip().map(Into::into) == config.discovery_address)
&& enr.tcp() == Some(config.libp2p_port)
&& enr.udp() == Some(config.discovery_port)
{