diff --git a/testing-framework/tools/cfgsync/src/network/address.rs b/testing-framework/tools/cfgsync/src/network/address.rs new file mode 100644 index 0000000..175a004 --- /dev/null +++ b/testing-framework/tools/cfgsync/src/network/address.rs @@ -0,0 +1,19 @@ +use nomos_libp2p::{Multiaddr, Protocol}; + +pub fn extract_udp_port(addr: &Multiaddr) -> Option { + addr.iter().find_map(|protocol| { + if let Protocol::Udp(port) = protocol { + Some(port) + } else { + None + } + }) +} + +pub fn find_matching_host(addr: &Multiaddr, original_ports: &[u16]) -> Option { + extract_udp_port(addr).and_then(|port| { + original_ports + .iter() + .position(|candidate| *candidate == port) + }) +} diff --git a/testing-framework/tools/cfgsync/src/network/mod.rs b/testing-framework/tools/cfgsync/src/network/mod.rs index c4a3e25..ec42960 100644 --- a/testing-framework/tools/cfgsync/src/network/mod.rs +++ b/testing-framework/tools/cfgsync/src/network/mod.rs @@ -1,49 +1,4 @@ -use std::str::FromStr; +pub mod address; +pub mod peers; -use nomos_libp2p::{Multiaddr, PeerId, Protocol}; - -use crate::host::Host; - -pub fn rewrite_initial_peers( - templates: &[Vec], - original_ports: &[u16], - hosts: &[Host], - peer_ids: &[PeerId], -) -> Vec> { - templates - .iter() - .enumerate() - .map(|(node_idx, peers)| { - peers - .iter() - .filter_map(|addr| find_matching_host(addr, original_ports)) - .filter(|&peer_idx| peer_idx != node_idx) - .map(|peer_idx| { - ::from_str(&format!( - "/ip4/{}/udp/{}/quic-v1/p2p/{}", - hosts[peer_idx].ip, hosts[peer_idx].network_port, peer_ids[peer_idx] - )) - .expect("valid peer multiaddr") - }) - .collect() - }) - .collect() -} - -pub fn find_matching_host(addr: &Multiaddr, original_ports: &[u16]) -> Option { - extract_udp_port(addr).and_then(|port| { - original_ports - .iter() - .position(|candidate| *candidate == port) - }) -} - -pub fn extract_udp_port(addr: &Multiaddr) -> Option { - addr.iter().find_map(|protocol| { - if let Protocol::Udp(port) = protocol { - Some(port) - } else { - None - } - }) -} +pub use peers::rewrite_initial_peers; diff --git a/testing-framework/tools/cfgsync/src/network/peers.rs b/testing-framework/tools/cfgsync/src/network/peers.rs new file mode 100644 index 0000000..2cbbfa2 --- /dev/null +++ b/testing-framework/tools/cfgsync/src/network/peers.rs @@ -0,0 +1,32 @@ +use std::str::FromStr; + +use nomos_libp2p::{Multiaddr, PeerId}; + +use super::address::find_matching_host; +use crate::host::Host; + +pub fn rewrite_initial_peers( + templates: &[Vec], + original_ports: &[u16], + hosts: &[Host], + peer_ids: &[PeerId], +) -> Vec> { + templates + .iter() + .enumerate() + .map(|(node_idx, peers)| { + peers + .iter() + .filter_map(|addr| find_matching_host(addr, original_ports)) + .filter(|&peer_idx| peer_idx != node_idx) + .map(|peer_idx| { + Multiaddr::from_str(&format!( + "/ip4/{}/udp/{}/quic-v1/p2p/{}", + hosts[peer_idx].ip, hosts[peer_idx].network_port, peer_ids[peer_idx] + )) + .expect("valid peer multiaddr") + }) + .collect() + }) + .collect() +}