Skip to content

Commit 35f47f4

Browse files
committed
Await listening address from libp2p in RPC tests setup (#4705)
## Issue Addressed #4704 ## Proposed Changes - Receive multiaddr from libp2p by awaiting listener setup ## Additional Info See also: #4675
1 parent 1ff4033 commit 35f47f4

File tree

1 file changed

+7
-15
lines changed
  • beacon_node/lighthouse_network/tests

1 file changed

+7
-15
lines changed

beacon_node/lighthouse_network/tests/common.rs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use tokio::runtime::Runtime;
1313
use types::{
1414
ChainSpec, EnrForkId, Epoch, EthSpec, ForkContext, ForkName, Hash256, MinimalEthSpec, Slot,
1515
};
16-
use unused_port::unused_tcp4_port;
1716

1817
type E = MinimalEthSpec;
1918
type ReqId = usize;
@@ -76,7 +75,7 @@ pub fn build_config(port: u16, mut boot_nodes: Vec<Enr>) -> NetworkConfig {
7675
.unwrap();
7776

7877
config.set_ipv4_listening_address(std::net::Ipv4Addr::UNSPECIFIED, port, port);
79-
config.enr_udp4_port = Some(port);
78+
config.enr_udp4_port = if port == 0 { None } else { Some(port) };
8079
config.enr_address = (Some(std::net::Ipv4Addr::LOCALHOST), None);
8180
config.boot_nodes_enr.append(&mut boot_nodes);
8281
config.network_dir = path.into_path();
@@ -96,7 +95,7 @@ pub async fn build_libp2p_instance(
9695
fork_name: ForkName,
9796
spec: &ChainSpec,
9897
) -> Libp2pInstance {
99-
let port = unused_tcp4_port().unwrap();
98+
let port = 0;
10099
let config = build_config(port, boot_nodes);
101100
// launch libp2p service
102101

@@ -139,33 +138,26 @@ pub async fn build_node_pair(
139138
let mut sender = build_libp2p_instance(rt.clone(), vec![], sender_log, fork_name, spec).await;
140139
let mut receiver = build_libp2p_instance(rt, vec![], receiver_log, fork_name, spec).await;
141140

142-
let receiver_multiaddr = receiver.local_enr().multiaddr()[1].clone();
143-
144141
// let the two nodes set up listeners
145142
let sender_fut = async {
146143
loop {
147-
if let NetworkEvent::NewListenAddr(_) = sender.next_event().await {
148-
return;
144+
if let NetworkEvent::NewListenAddr(addr) = sender.next_event().await {
145+
return addr;
149146
}
150147
}
151148
};
152149
let receiver_fut = async {
153150
loop {
154-
if let NetworkEvent::NewListenAddr(_) = receiver.next_event().await {
155-
return;
151+
if let NetworkEvent::NewListenAddr(addr) = receiver.next_event().await {
152+
return addr;
156153
}
157154
}
158155
};
159156

160157
let joined = futures::future::join(sender_fut, receiver_fut);
161158

162-
// wait for either both nodes to listen or a timeout
163-
tokio::select! {
164-
_ = tokio::time::sleep(Duration::from_millis(500)) => {}
165-
_ = joined => {}
166-
}
159+
let receiver_multiaddr = joined.await.1;
167160

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

0 commit comments

Comments
 (0)