e190ebb8a0
## Issue Addressed Add support for ipv6 and dual stack in lighthouse. ## Proposed Changes From an user perspective, now setting an ipv6 address, optionally configuring the ports should feel exactly the same as using an ipv4 address. If listening over both ipv4 and ipv6 then the user needs to: - use the `--listen-address` two times (ipv4 and ipv6 addresses) - `--port6` becomes then required - `--discovery-port6` can now be used to additionally configure the ipv6 udp port ### Rough list of code changes - Discovery: - Table filter and ip mode set to match the listening config. - Ipv6 address, tcp port and udp port set in the ENR builder - Reported addresses now check which tcp port to give to libp2p - LH Network Service: - Can listen over Ipv6, Ipv4, or both. This uses two sockets. Using mapped addresses is disabled from libp2p and it's the most compatible option. - NetworkGlobals: - No longer stores udp port since was not used at all. Instead, stores the Ipv4 and Ipv6 TCP ports. - NetworkConfig: - Update names to make it clear that previous udp and tcp ports in ENR were Ipv4 - Add fields to configure Ipv6 udp and tcp ports in the ENR - Include advertised enr Ipv6 address. - Add type to model Listening address that's either Ipv4, Ipv6 or both. A listening address includes the ip, udp port and tcp port. - UPnP: - Kept only for ipv4 - Cli flags: - `--listen-addresses` now can take up to two values - `--port` will apply to ipv4 or ipv6 if only one listening address is given. If two listening addresses are given it will apply only to Ipv4. - `--port6` New flag required when listening over ipv4 and ipv6 that applies exclusively to Ipv6. - `--discovery-port` will now apply to ipv4 and ipv6 if only one listening address is given. - `--discovery-port6` New flag to configure the individual udp port of ipv6 if listening over both ipv4 and ipv6. - `--enr-udp-port` Updated docs to specify that it only applies to ipv4. This is an old behaviour. - `--enr-udp6-port` Added to configure the enr udp6 field. - `--enr-tcp-port` Updated docs to specify that it only applies to ipv4. This is an old behaviour. - `--enr-tcp6-port` Added to configure the enr tcp6 field. - `--enr-addresses` now can take two values. - `--enr-match` updated behaviour. - Common: - rename `unused_port` functions to specify that they are over ipv4. - add functions to get unused ports over ipv6. - Testing binaries - Updated code to reflect network config changes and unused_port changes. ## Additional Info TODOs: - use two sockets in discovery. I'll get back to this and it's on https://github.com/sigp/discv5/pull/160 - lcli allow listening over two sockets in generate_bootnodes_enr - add at least one smoke flag for ipv6 (I have tested this and works for me) - update the book
231 lines
7.2 KiB
Rust
231 lines
7.2 KiB
Rust
#![cfg(test)]
|
|
use libp2p::gossipsub::GossipsubConfigBuilder;
|
|
use lighthouse_network::service::Network as LibP2PService;
|
|
use lighthouse_network::Enr;
|
|
use lighthouse_network::EnrExt;
|
|
use lighthouse_network::Multiaddr;
|
|
use lighthouse_network::{NetworkConfig, NetworkEvent};
|
|
use slog::{debug, error, o, Drain};
|
|
use std::sync::Arc;
|
|
use std::sync::Weak;
|
|
use std::time::Duration;
|
|
use tokio::runtime::Runtime;
|
|
use types::{
|
|
ChainSpec, EnrForkId, Epoch, EthSpec, ForkContext, ForkName, Hash256, MinimalEthSpec, Slot,
|
|
};
|
|
use unused_port::unused_tcp4_port;
|
|
|
|
type E = MinimalEthSpec;
|
|
type ReqId = usize;
|
|
|
|
use tempfile::Builder as TempBuilder;
|
|
|
|
/// Returns a dummy fork context
|
|
pub fn fork_context(fork_name: ForkName) -> ForkContext {
|
|
let mut chain_spec = E::default_spec();
|
|
let altair_fork_epoch = Epoch::new(1);
|
|
let merge_fork_epoch = Epoch::new(2);
|
|
let capella_fork_epoch = Epoch::new(3);
|
|
|
|
chain_spec.altair_fork_epoch = Some(altair_fork_epoch);
|
|
chain_spec.bellatrix_fork_epoch = Some(merge_fork_epoch);
|
|
chain_spec.capella_fork_epoch = Some(capella_fork_epoch);
|
|
|
|
let current_slot = match fork_name {
|
|
ForkName::Base => Slot::new(0),
|
|
ForkName::Altair => altair_fork_epoch.start_slot(E::slots_per_epoch()),
|
|
ForkName::Merge => merge_fork_epoch.start_slot(E::slots_per_epoch()),
|
|
ForkName::Capella => capella_fork_epoch.start_slot(E::slots_per_epoch()),
|
|
};
|
|
ForkContext::new::<E>(current_slot, Hash256::zero(), &chain_spec)
|
|
}
|
|
|
|
pub struct Libp2pInstance(LibP2PService<ReqId, E>, exit_future::Signal);
|
|
|
|
impl std::ops::Deref for Libp2pInstance {
|
|
type Target = LibP2PService<ReqId, E>;
|
|
fn deref(&self) -> &Self::Target {
|
|
&self.0
|
|
}
|
|
}
|
|
|
|
impl std::ops::DerefMut for Libp2pInstance {
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
&mut self.0
|
|
}
|
|
}
|
|
|
|
#[allow(unused)]
|
|
pub fn build_log(level: slog::Level, enabled: bool) -> slog::Logger {
|
|
let decorator = slog_term::TermDecorator::new().build();
|
|
let drain = slog_term::FullFormat::new(decorator).build().fuse();
|
|
let drain = slog_async::Async::new(drain).build().fuse();
|
|
|
|
if enabled {
|
|
slog::Logger::root(drain.filter_level(level).fuse(), o!())
|
|
} else {
|
|
slog::Logger::root(drain.filter(|_| false).fuse(), o!())
|
|
}
|
|
}
|
|
|
|
pub fn build_config(port: u16, mut boot_nodes: Vec<Enr>) -> NetworkConfig {
|
|
let mut config = NetworkConfig::default();
|
|
let path = TempBuilder::new()
|
|
.prefix(&format!("libp2p_test{}", port))
|
|
.tempdir()
|
|
.unwrap();
|
|
|
|
config.set_ipv4_listening_address(std::net::Ipv4Addr::UNSPECIFIED, port, port);
|
|
config.enr_udp4_port = Some(port);
|
|
config.enr_address = (Some(std::net::Ipv4Addr::LOCALHOST), None);
|
|
config.boot_nodes_enr.append(&mut boot_nodes);
|
|
config.network_dir = path.into_path();
|
|
// Reduce gossipsub heartbeat parameters
|
|
config.gs_config = GossipsubConfigBuilder::from(config.gs_config)
|
|
.heartbeat_initial_delay(Duration::from_millis(500))
|
|
.heartbeat_interval(Duration::from_millis(500))
|
|
.build()
|
|
.unwrap();
|
|
config
|
|
}
|
|
|
|
pub async fn build_libp2p_instance(
|
|
rt: Weak<Runtime>,
|
|
boot_nodes: Vec<Enr>,
|
|
log: slog::Logger,
|
|
fork_name: ForkName,
|
|
) -> Libp2pInstance {
|
|
let port = unused_tcp4_port().unwrap();
|
|
let config = build_config(port, boot_nodes);
|
|
// launch libp2p service
|
|
|
|
let (signal, exit) = exit_future::signal();
|
|
let (shutdown_tx, _) = futures::channel::mpsc::channel(1);
|
|
let executor = task_executor::TaskExecutor::new(rt, exit, log.clone(), shutdown_tx);
|
|
let libp2p_context = lighthouse_network::Context {
|
|
config: &config,
|
|
enr_fork_id: EnrForkId::default(),
|
|
fork_context: Arc::new(fork_context(fork_name)),
|
|
chain_spec: &ChainSpec::minimal(),
|
|
gossipsub_registry: None,
|
|
};
|
|
Libp2pInstance(
|
|
LibP2PService::new(executor, libp2p_context, &log)
|
|
.await
|
|
.expect("should build libp2p instance")
|
|
.0,
|
|
signal,
|
|
)
|
|
}
|
|
|
|
#[allow(dead_code)]
|
|
pub fn get_enr(node: &LibP2PService<ReqId, E>) -> Enr {
|
|
node.local_enr()
|
|
}
|
|
|
|
// Returns `n` libp2p peers in fully connected topology.
|
|
#[allow(dead_code)]
|
|
/*
|
|
pub async fn build_full_mesh(
|
|
rt: Weak<Runtime>,
|
|
log: slog::Logger,
|
|
n: usize,
|
|
fork_name: ForkName,
|
|
) -> Vec<Libp2pInstance> {
|
|
let mut nodes = Vec::with_capacity(n);
|
|
for _ in 0..n {
|
|
nodes.push(build_libp2p_instance(rt.clone(), vec![], log.clone(), fork_name).await);
|
|
}
|
|
let multiaddrs: Vec<Multiaddr> = nodes
|
|
.iter()
|
|
.map(|x| get_enr(x).multiaddr()[1].clone())
|
|
.collect();
|
|
|
|
for (i, node) in nodes.iter_mut().enumerate().take(n) {
|
|
for (j, multiaddr) in multiaddrs.iter().enumerate().skip(i) {
|
|
if i != j {
|
|
match libp2p::Swarm::dial(&mut node.swarm, multiaddr.clone()) {
|
|
Ok(()) => debug!(log, "Connected"),
|
|
Err(_) => error!(log, "Failed to connect"),
|
|
};
|
|
}
|
|
}
|
|
}
|
|
nodes
|
|
}*/
|
|
// Constructs a pair of nodes with separate loggers. The sender dials the receiver.
|
|
// This returns a (sender, receiver) pair.
|
|
#[allow(dead_code)]
|
|
pub async fn build_node_pair(
|
|
rt: Weak<Runtime>,
|
|
log: &slog::Logger,
|
|
fork_name: ForkName,
|
|
) -> (Libp2pInstance, Libp2pInstance) {
|
|
let sender_log = log.new(o!("who" => "sender"));
|
|
let receiver_log = log.new(o!("who" => "receiver"));
|
|
|
|
let mut sender = build_libp2p_instance(rt.clone(), vec![], sender_log, fork_name).await;
|
|
let mut receiver = build_libp2p_instance(rt, vec![], receiver_log, fork_name).await;
|
|
|
|
let receiver_multiaddr = receiver.local_enr().multiaddr()[1].clone();
|
|
|
|
// let the two nodes set up listeners
|
|
let sender_fut = async {
|
|
loop {
|
|
if let NetworkEvent::NewListenAddr(_) = sender.next_event().await {
|
|
return;
|
|
}
|
|
}
|
|
};
|
|
let receiver_fut = async {
|
|
loop {
|
|
if let NetworkEvent::NewListenAddr(_) = receiver.next_event().await {
|
|
return;
|
|
}
|
|
}
|
|
};
|
|
|
|
let joined = futures::future::join(sender_fut, receiver_fut);
|
|
|
|
// wait for either both nodes to listen or a timeout
|
|
tokio::select! {
|
|
_ = tokio::time::sleep(Duration::from_millis(500)) => {}
|
|
_ = joined => {}
|
|
}
|
|
|
|
// sender.dial_peer(peer_id);
|
|
match sender.testing_dial(receiver_multiaddr.clone()) {
|
|
Ok(()) => {
|
|
debug!(log, "Sender dialed receiver"; "address" => format!("{:?}", receiver_multiaddr))
|
|
}
|
|
Err(_) => error!(log, "Dialing failed"),
|
|
};
|
|
(sender, receiver)
|
|
}
|
|
|
|
// Returns `n` peers in a linear topology
|
|
#[allow(dead_code)]
|
|
pub async fn build_linear(
|
|
rt: Weak<Runtime>,
|
|
log: slog::Logger,
|
|
n: usize,
|
|
fork_name: ForkName,
|
|
) -> Vec<Libp2pInstance> {
|
|
let mut nodes = Vec::with_capacity(n);
|
|
for _ in 0..n {
|
|
nodes.push(build_libp2p_instance(rt.clone(), vec![], log.clone(), fork_name).await);
|
|
}
|
|
|
|
let multiaddrs: Vec<Multiaddr> = nodes
|
|
.iter()
|
|
.map(|x| get_enr(x).multiaddr()[1].clone())
|
|
.collect();
|
|
for i in 0..n - 1 {
|
|
match nodes[i].testing_dial(multiaddrs[i + 1].clone()) {
|
|
Ok(()) => debug!(log, "Connected"),
|
|
Err(_) => error!(log, "Failed to connect"),
|
|
};
|
|
}
|
|
nodes
|
|
}
|