Skip to content

Commit

Permalink
Await listening address from libp2p in RPC tests setup (#4705)
Browse files Browse the repository at this point in the history
## Issue Addressed

#4704 

## Proposed Changes

 - Receive multiaddr from libp2p by awaiting listener setup

## Additional Info

See also: #4675
  • Loading branch information
jmcph4 committed Sep 11, 2023
1 parent 1ff4033 commit 35f47f4
Showing 1 changed file with 7 additions and 15 deletions.
22 changes: 7 additions & 15 deletions beacon_node/lighthouse_network/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ 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;
Expand Down Expand Up @@ -76,7 +75,7 @@ pub fn build_config(port: u16, mut boot_nodes: Vec<Enr>) -> NetworkConfig {
.unwrap();

config.set_ipv4_listening_address(std::net::Ipv4Addr::UNSPECIFIED, port, port);
config.enr_udp4_port = Some(port);
config.enr_udp4_port = if port == 0 { None } else { 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();
Expand All @@ -96,7 +95,7 @@ pub async fn build_libp2p_instance(
fork_name: ForkName,
spec: &ChainSpec,
) -> Libp2pInstance {
let port = unused_tcp4_port().unwrap();
let port = 0;
let config = build_config(port, boot_nodes);
// launch libp2p service

Expand Down Expand Up @@ -139,33 +138,26 @@ pub async fn build_node_pair(
let mut sender = build_libp2p_instance(rt.clone(), vec![], sender_log, fork_name, spec).await;
let mut receiver = build_libp2p_instance(rt, vec![], receiver_log, fork_name, spec).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;
if let NetworkEvent::NewListenAddr(addr) = sender.next_event().await {
return addr;
}
}
};
let receiver_fut = async {
loop {
if let NetworkEvent::NewListenAddr(_) = receiver.next_event().await {
return;
if let NetworkEvent::NewListenAddr(addr) = receiver.next_event().await {
return addr;
}
}
};

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 => {}
}
let receiver_multiaddr = joined.await.1;

// sender.dial_peer(peer_id);
match sender.testing_dial(receiver_multiaddr.clone()) {
Ok(()) => {
debug!(log, "Sender dialed receiver"; "address" => format!("{:?}", receiver_multiaddr))
Expand Down

0 comments on commit 35f47f4

Please sign in to comment.