Skip to content

Commit

Permalink
potentially fix Libp2p restart test
Browse files Browse the repository at this point in the history
  • Loading branch information
rob-maron committed Dec 5, 2024
1 parent 9fac4f4 commit 48a98d6
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 26 deletions.
3 changes: 0 additions & 3 deletions crates/hotshot/src/traits/networking/combined_network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,22 +255,19 @@ pub struct UnderlyingCombinedNetworks<TYPES: NodeType>(
impl<TYPES: NodeType> TestableNetworkingImplementation<TYPES> for CombinedNetworks<TYPES> {
fn generator(
expected_node_count: usize,
test_id: usize,
da_committee_size: usize,
reliability_config: Option<Box<dyn NetworkReliability>>,
secondary_network_delay: Duration,
) -> AsyncGenerator<Arc<Self>> {
let generators = (
<PushCdnNetwork<TYPES::SignatureKey> as TestableNetworkingImplementation<TYPES>>::generator(
expected_node_count,
test_id,
da_committee_size,
None,
Duration::default(),
),
<Libp2pNetwork<TYPES> as TestableNetworkingImplementation<TYPES>>::generator(
expected_node_count,
test_id,
da_committee_size,
reliability_config,
Duration::default(),
Expand Down
38 changes: 21 additions & 17 deletions crates/hotshot/src/traits/networking/libp2p_network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ pub struct Libp2pNetwork<T: NodeType> {
}

/// Generate the expected [testing] multiaddr from a node index
fn multiaddr_from_node_index<T: NodeType>(i: usize) -> Multiaddr {
fn multiaddr_from_node_index<T: NodeType>(i: usize, bind_addresses: &[Multiaddr]) -> Multiaddr {
// Generate the node's private key from the node ID
let peers_hotshot_private_key =
T::SignatureKey::generated_from_seed_indexed([0u8; 32], i as u64).1;
Expand All @@ -182,12 +182,10 @@ fn multiaddr_from_node_index<T: NodeType>(i: usize) -> Multiaddr {
.expect("Failed to derive libp2p keypair");

// Generate the multiaddress using the peer id and port
Multiaddr::from_str(&format!(
"/ip4/127.0.0.1/udp/{}/quic-v1/p2p/{}",
48000 + i,
peers_libp2p_keypair.public().to_peer_id()
))
.expect("Failed to create multiaddr")
bind_addresses[i]
.clone()
.with_p2p(peers_libp2p_keypair.public().to_peer_id())
.expect("Failed to append libp2p peer id to multiaddr")
}

#[cfg(feature = "hotshot-testing")]
Expand All @@ -204,7 +202,6 @@ impl<T: NodeType> TestableNetworkingImplementation<T> for Libp2pNetwork<T> {
#[allow(clippy::panic, clippy::too_many_lines)]
fn generator(
expected_node_count: usize,
test_id: usize,
da_committee_size: usize,
reliability_config: Option<Box<dyn NetworkReliability>>,
_secondary_network_delay: Duration,
Expand All @@ -214,16 +211,23 @@ impl<T: NodeType> TestableNetworkingImplementation<T> for Libp2pNetwork<T> {
"DA committee size must be less than or equal to total # nodes"
);

// Generate the bind addresses each node will use
let mut bind_addresses = Vec::new();
for _ in 0..expected_node_count {
bind_addresses.push(
Multiaddr::from_str(&format!(
"/ip4/127.0.0.1/udp/{}/quic-v1",
portpicker::pick_unused_port().expect("All ports are in use")
))
.expect("Failed to create multiaddr"),
);
}

// NOTE uncomment this for easier debugging
Box::pin({
move |node_id| {
// The port is 48000 + the node id. This way it's deterministic and easy to connect nodes together
let port = 48000 + node_id;

// Create the bind address
let bind_address =
Multiaddr::from_str(&format!("/ip4/127.0.0.{test_id}/udp/{port}/quic-v1"))
.unwrap();
// Get our bind address from the list
let bind_address = bind_addresses[usize::try_from(node_id).unwrap()].clone();

// Deterministically generate the private key from the node ID
let hotshot_private_key =
Expand Down Expand Up @@ -272,7 +276,7 @@ impl<T: NodeType> TestableNetworkingImplementation<T> for Libp2pNetwork<T> {

// Create the list of known peers
let known_peers = (0..expected_node_count)
.map(|i| multiaddr_from_node_index::<T>(i))
.map(|i| multiaddr_from_node_index::<T>(i, &bind_addresses))
.collect();

// Collect the `PeerConfig`s of all nodes
Expand Down Expand Up @@ -434,7 +438,7 @@ impl<T: NodeType> Libp2pNetwork<T> {
// Spawn the network node with a copy of our config
let (mut rx, network_handle) = spawn_network_node::<T>(config.clone())
.await
.map_err(|e| NetworkError::ConfigError(format!("failed to spawn network node: {e}")))?;
.with_context(|| "failed to spawn network node")?;

// Subscribe only to the global topic (DA messages are direct-broadcasted)
let subscribed_topics = HashSet::from_iter(vec![GLOBAL_TOPIC.to_string()]);
Expand Down
1 change: 0 additions & 1 deletion crates/hotshot/src/traits/networking/memory_network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@ impl<TYPES: NodeType> TestableNetworkingImplementation<TYPES>
{
fn generator(
_expected_node_count: usize,
_test_id: usize,
da_committee_size: usize,
reliability_config: Option<Box<dyn NetworkReliability>>,
_secondary_network_delay: Duration,
Expand Down
1 change: 0 additions & 1 deletion crates/hotshot/src/traits/networking/push_cdn_network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,6 @@ impl<TYPES: NodeType> TestableNetworkingImplementation<TYPES>
#[allow(clippy::too_many_lines)]
fn generator(
_expected_node_count: usize,
_test_id: usize,
da_committee_size: usize,
_reliability_config: Option<Box<dyn NetworkReliability>>,
_secondary_network_delay: Duration,
Expand Down
1 change: 0 additions & 1 deletion crates/types/src/traits/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,6 @@ where
#[allow(clippy::type_complexity)]
fn generator(
expected_node_count: usize,
test_id: usize,
da_committee_size: usize,
reliability_config: Option<Box<dyn NetworkReliability>>,
secondary_network_delay: Duration,
Expand Down
3 changes: 0 additions & 3 deletions crates/types/src/traits/node_implementation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use std::{

use async_trait::async_trait;
use committable::Committable;
use rand::Rng;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use url::Url;
use vbs::version::StaticVersionType;
Expand Down Expand Up @@ -149,8 +148,6 @@ where
) -> AsyncGenerator<Arc<Self::Network>> {
<I::Network as TestableNetworkingImplementation<TYPES>>::generator(
expected_node_count,
// Generate a unique-ish test id between 0 and 255
rand::thread_rng().gen_range(0..255),
da_committee_size,
reliability_config.clone(),
secondary_network_delay,
Expand Down

0 comments on commit 48a98d6

Please sign in to comment.