Skip to content

Commit

Permalink
Use IPv4 fallback in reserve_listener()
Browse files Browse the repository at this point in the history
  • Loading branch information
slowli committed Nov 3, 2023
1 parent 9e4b05b commit 45dc3fe
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 28 deletions.
4 changes: 2 additions & 2 deletions node/actors/network/src/consensus/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ async fn test_one_connection_per_validator() {
let mut nodes = testonly::Instance::new(rng, 3, 1);

scope::run!(ctx, |ctx,s| async {
for (i,n) in nodes.iter().enumerate() {
for (i, node) in nodes.iter().enumerate() {
let (network_pipe, _) = pipe::new();

s.spawn_bg(run_network(
ctx,
n.state.clone(),
node.state.clone(),
network_pipe
).instrument(tracing::info_span!("node", i)));
}
Expand Down
4 changes: 2 additions & 2 deletions node/actors/network/src/gossip/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ async fn test_one_connection_per_node() {
let mut nodes: Vec<_> = testonly::Instance::new(rng, 5, 2);

scope::run!(ctx, |ctx,s| async {
for n in &nodes {
for node in &nodes {
let (network_pipe, _) = pipe::new();

s.spawn_bg(run_network(
ctx,
n.state.clone(),
node.state.clone(),
network_pipe
));
}
Expand Down
40 changes: 20 additions & 20 deletions node/actors/network/src/testonly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,26 +53,26 @@ impl Instance {
pub fn new_configs<R: Rng>(rng: &mut R, n: usize, gossip_peers: usize) -> Vec<Config> {
let keys: Vec<validator::SecretKey> = (0..n).map(|_| rng.gen()).collect();
let validators = validator::ValidatorSet::new(keys.iter().map(|k| k.public())).unwrap();
let mut cfgs: Vec<_> = (0..n)
.map(|i| {
let addr = net::tcp::testonly::reserve_listener();
Config {
server_addr: addr,
validators: validators.clone(),
consensus: Some(consensus::Config {
key: keys[i].clone(),
public_addr: *addr,
}),
gossip: gossip::Config {
key: rng.gen(),
dynamic_inbound_limit: n as u64,
static_inbound: HashSet::default(),
static_outbound: HashMap::default(),
enable_pings: true,
},
}
})
.collect();
let configs = keys.iter().map(|key| {
let addr = net::tcp::testonly::reserve_listener();
Config {
server_addr: addr,
validators: validators.clone(),
consensus: Some(consensus::Config {
key: key.clone(),
public_addr: *addr,
}),
gossip: gossip::Config {
key: rng.gen(),
dynamic_inbound_limit: n as u64,
static_inbound: HashSet::default(),
static_outbound: HashMap::default(),
enable_pings: true,
},
}
});
let mut cfgs: Vec<_> = configs.collect();

for i in 0..cfgs.len() {
for j in 0..gossip_peers {
let j = (i + j + 1) % n;
Expand Down
4 changes: 2 additions & 2 deletions node/actors/network/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ async fn test_metrics() {
let rng = &mut ctx.rng();
let nodes = testonly::Instance::new(rng, 3, 1);
scope::run!(ctx, |ctx, s| async {
for (i, n) in nodes.iter().enumerate() {
for (i, node) in nodes.iter().enumerate() {
let (network_pipe, _) = pipe::new();
s.spawn_bg(
run_network(ctx, n.state.clone(), network_pipe)
run_network(ctx, node.state.clone(), network_pipe)
.instrument(tracing::info_span!("node", i)),
);
}
Expand Down
14 changes: 12 additions & 2 deletions node/libs/concurrency/src/net/tcp/testonly.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
//! Test-only TCP utilities.
use super::{accept, connect, ListenerAddr, Stream, RESERVED_LISTENER_ADDRS};
use crate::{ctx, scope};
use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr};

/// Reserves a random port on localhost for a TCP listener.
pub fn reserve_listener() -> ListenerAddr {
let guard = tokio::net::TcpSocket::new_v6().unwrap();
// Try to bind to an Ipv6 address, then fall back to Ipv4 if Ipv6 is not available.
let localhost_addr = SocketAddr::from((Ipv6Addr::LOCALHOST, 0));
let mut guard = tokio::net::TcpSocket::new_v6().unwrap();
guard.set_reuseaddr(true).unwrap();
guard.set_reuseport(true).unwrap();
guard.bind("[::1]:0".parse().unwrap()).unwrap();

if guard.bind(localhost_addr).is_err() {
let localhost_addr = SocketAddr::from((Ipv4Addr::LOCALHOST, 0));
guard = tokio::net::TcpSocket::new_v4().unwrap();
guard.set_reuseaddr(true).unwrap();
guard.set_reuseport(true).unwrap();
guard.bind(localhost_addr).unwrap();
}
let addr = guard.local_addr().unwrap();
RESERVED_LISTENER_ADDRS.lock().unwrap().insert(addr, guard);
ListenerAddr(addr)
Expand Down

0 comments on commit 45dc3fe

Please sign in to comment.