Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Await listening address from libp2p in RPC tests setup #4705

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Comment on lines +98 to 99
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
let port = 0;
let config = build_config(port, boot_nodes);
let config = build_config(0, boot_nodes);

Copy link
Collaborator

@divagant-martian divagant-martian Sep 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually think it's more readable as he has it right now since it gives the extra context that 0 is a port, not a size, or a retry count etc

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this is the only reason I didn't roll it into the build_config call directly.

// 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
Loading