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
This commit is contained in:
Jack McPherson 2023-09-11 06:14:56 +00:00
parent 1ff4033830
commit 35f47f454f

View File

@ -13,7 +13,6 @@ use tokio::runtime::Runtime;
use types::{ use types::{
ChainSpec, EnrForkId, Epoch, EthSpec, ForkContext, ForkName, Hash256, MinimalEthSpec, Slot, ChainSpec, EnrForkId, Epoch, EthSpec, ForkContext, ForkName, Hash256, MinimalEthSpec, Slot,
}; };
use unused_port::unused_tcp4_port;
type E = MinimalEthSpec; type E = MinimalEthSpec;
type ReqId = usize; type ReqId = usize;
@ -76,7 +75,7 @@ pub fn build_config(port: u16, mut boot_nodes: Vec<Enr>) -> NetworkConfig {
.unwrap(); .unwrap();
config.set_ipv4_listening_address(std::net::Ipv4Addr::UNSPECIFIED, port, port); 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.enr_address = (Some(std::net::Ipv4Addr::LOCALHOST), None);
config.boot_nodes_enr.append(&mut boot_nodes); config.boot_nodes_enr.append(&mut boot_nodes);
config.network_dir = path.into_path(); config.network_dir = path.into_path();
@ -96,7 +95,7 @@ pub async fn build_libp2p_instance(
fork_name: ForkName, fork_name: ForkName,
spec: &ChainSpec, spec: &ChainSpec,
) -> Libp2pInstance { ) -> Libp2pInstance {
let port = unused_tcp4_port().unwrap(); let port = 0;
let config = build_config(port, boot_nodes); let config = build_config(port, boot_nodes);
// launch libp2p service // launch libp2p service
@ -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 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 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 the two nodes set up listeners
let sender_fut = async { let sender_fut = async {
loop { loop {
if let NetworkEvent::NewListenAddr(_) = sender.next_event().await { if let NetworkEvent::NewListenAddr(addr) = sender.next_event().await {
return; return addr;
} }
} }
}; };
let receiver_fut = async { let receiver_fut = async {
loop { loop {
if let NetworkEvent::NewListenAddr(_) = receiver.next_event().await { if let NetworkEvent::NewListenAddr(addr) = receiver.next_event().await {
return; return addr;
} }
} }
}; };
let joined = futures::future::join(sender_fut, receiver_fut); let joined = futures::future::join(sender_fut, receiver_fut);
// wait for either both nodes to listen or a timeout let receiver_multiaddr = joined.await.1;
tokio::select! {
_ = tokio::time::sleep(Duration::from_millis(500)) => {}
_ = joined => {}
}
// sender.dial_peer(peer_id);
match sender.testing_dial(receiver_multiaddr.clone()) { match sender.testing_dial(receiver_multiaddr.clone()) {
Ok(()) => { Ok(()) => {
debug!(log, "Sender dialed receiver"; "address" => format!("{:?}", receiver_multiaddr)) debug!(log, "Sender dialed receiver"; "address" => format!("{:?}", receiver_multiaddr))