Enables ENR auto-update based on new listen address (#610)

This commit is contained in:
Age Manning
2019-11-20 15:03:50 +11:00
committed by Paul Hauner
parent e1de30bd64
commit 62d66f1c10
4 changed files with 64 additions and 3 deletions

View File

@@ -30,6 +30,8 @@ pub struct Service {
pub swarm: Swarm<Libp2pStream, Libp2pBehaviour>,
/// This node's PeerId.
pub local_peer_id: PeerId,
/// Indicates if the listening address have been verified and compared to the expected ENR.
pub verified_listen_address: bool,
/// The libp2p logger handle.
pub log: slog::Logger,
}
@@ -151,6 +153,7 @@ impl Service {
Ok(Service {
local_peer_id,
swarm,
verified_listen_address: false,
log,
})
}
@@ -189,7 +192,18 @@ impl Stream for Service {
}
},
Ok(Async::Ready(None)) => unreachable!("Swarm stream shouldn't end"),
Ok(Async::NotReady) => break,
Ok(Async::NotReady) => {
// check to see if the address is different to the config. If so, update our ENR
if !self.verified_listen_address {
let multiaddr = Swarm::listeners(&self.swarm).next();
if let Some(multiaddr) = multiaddr {
if let Some(socket_addr) = multiaddr_to_socket_addr(multiaddr) {
self.swarm.update_local_enr_socket(socket_addr, true);
}
}
}
break;
}
_ => break,
}
}
@@ -197,6 +211,30 @@ impl Stream for Service {
}
}
/// Converts a multiaddr to a `SocketAddr` if the multiaddr has the TCP/IP form. Libp2p currently
/// only supports TCP, so the UDP case is currently ignored.
fn multiaddr_to_socket_addr(multiaddr: &Multiaddr) -> Option<std::net::SocketAddr> {
let protocols = multiaddr.iter().collect::<Vec<_>>();
// assume the IP protocol
match protocols[0] {
Protocol::Ip4(address) => {
if let Protocol::Tcp(port) = protocols[1] {
Some(std::net::SocketAddr::new(address.into(), port))
} else {
None
}
}
Protocol::Ip6(address) => {
if let Protocol::Tcp(port) = protocols[1] {
Some(std::net::SocketAddr::new(address.into(), port))
} else {
None
}
}
_ => None,
}
}
/// The implementation supports TCP/IP, WebSockets over TCP/IP, secio as the encryption layer, and
/// mplex or yamux as the multiplexing layer.
fn build_transport(local_private_key: Keypair) -> Boxed<(PeerId, StreamMuxerBox), Error> {