From 34c3c06c793e21b4dcffdd771fff385ec4538012 Mon Sep 17 00:00:00 2001 From: andrussal Date: Wed, 10 Dec 2025 09:13:36 +0100 Subject: [PATCH] Split cfgsync host and network helpers into modules --- testing-framework/tools/cfgsync/src/config.rs | 122 +----------------- testing-framework/tools/cfgsync/src/host.rs | 76 +++++++++++ testing-framework/tools/cfgsync/src/lib.rs | 2 + .../tools/cfgsync/src/network/mod.rs | 49 +++++++ 4 files changed, 133 insertions(+), 116 deletions(-) create mode 100644 testing-framework/tools/cfgsync/src/host.rs create mode 100644 testing-framework/tools/cfgsync/src/network/mod.rs diff --git a/testing-framework/tools/cfgsync/src/config.rs b/testing-framework/tools/cfgsync/src/config.rs index db392f8..25dfada 100644 --- a/testing-framework/tools/cfgsync/src/config.rs +++ b/testing-framework/tools/cfgsync/src/config.rs @@ -10,7 +10,7 @@ use nomos_core::{ mantle::GenesisTx as _, sdp::{Locator, ServiceType}, }; -use nomos_libp2p::{Multiaddr, PeerId, Protocol, ed25519}; +use nomos_libp2p::{Multiaddr, PeerId, ed25519}; use nomos_tracing_service::{LoggerLayer, MetricsLayer, TracingLayer, TracingSettings}; use nomos_utils::net::get_available_udp_port; use rand::{Rng as _, thread_rng}; @@ -30,62 +30,8 @@ use testing_framework_config::topology::configs::{ wallet::WalletConfig, }; -const DEFAULT_LIBP2P_NETWORK_PORT: u16 = 3000; -const DEFAULT_DA_NETWORK_PORT: u16 = 3300; -const DEFAULT_BLEND_PORT: u16 = 3400; -const DEFAULT_API_PORT: u16 = 18080; - -#[derive(Copy, Clone, Eq, PartialEq, Hash)] -pub enum HostKind { - Validator, - Executor, -} - -#[derive(Eq, PartialEq, Hash, Clone)] -pub struct Host { - pub kind: HostKind, - pub ip: Ipv4Addr, - pub identifier: String, - pub network_port: u16, - pub da_network_port: u16, - pub blend_port: u16, - pub api_port: u16, - pub testing_http_port: u16, -} - -#[derive(Clone, Copy)] -pub struct PortOverrides { - pub network_port: Option, - pub da_network_port: Option, - pub blend_port: Option, - pub api_port: Option, - pub testing_http_port: Option, -} - -impl Host { - fn from_parts(kind: HostKind, ip: Ipv4Addr, identifier: String, ports: PortOverrides) -> Self { - Self { - kind, - ip, - identifier, - network_port: ports.network_port.unwrap_or(DEFAULT_LIBP2P_NETWORK_PORT), - da_network_port: ports.da_network_port.unwrap_or(DEFAULT_DA_NETWORK_PORT), - blend_port: ports.blend_port.unwrap_or(DEFAULT_BLEND_PORT), - api_port: ports.api_port.unwrap_or(DEFAULT_API_PORT), - testing_http_port: ports.testing_http_port.unwrap_or(DEFAULT_API_PORT + 1), - } - } - - #[must_use] - pub fn validator_from_ip(ip: Ipv4Addr, identifier: String, ports: PortOverrides) -> Self { - Self::from_parts(HostKind::Validator, ip, identifier, ports) - } - - #[must_use] - pub fn executor_from_ip(ip: Ipv4Addr, identifier: String, ports: PortOverrides) -> Self { - Self::from_parts(HostKind::Executor, ip, identifier, ports) - } -} +pub use crate::host::{Host, HostKind, PortOverrides}; +use crate::{host::sort_hosts, network::rewrite_initial_peers}; #[must_use] pub fn create_node_configs( @@ -98,20 +44,7 @@ pub fn create_node_configs( blend_ports: Option>, hosts: Vec, ) -> HashMap { - let mut hosts = hosts; - hosts.sort_by_key(|host| { - let index = host - .identifier - .rsplit('-') - .next() - .and_then(|raw| raw.parse::().ok()) - .unwrap_or(0); - let kind = match host.kind { - HostKind::Validator => 0, - HostKind::Executor => 1, - }; - (kind, index) - }); + let hosts = sort_hosts(hosts); assert_eq!( hosts.len(), @@ -308,50 +241,6 @@ fn create_providers( providers } -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() -} - -fn find_matching_host(addr: &Multiaddr, original_ports: &[u16]) -> Option { - extract_udp_port(addr).and_then(|port| { - original_ports - .iter() - .position(|candidate| *candidate == port) - }) -} - -fn extract_udp_port(addr: &Multiaddr) -> Option { - addr.iter().find_map(|protocol| { - if let Protocol::Udp(port) = protocol { - Some(port) - } else { - None - } - }) -} - fn update_tracing_identifier( settings: TracingSettings, identifier: String, @@ -437,7 +326,8 @@ mod cfgsync_tests { }; use tracing::Level; - use super::{Host, HostKind, create_node_configs}; + use super::create_node_configs; + use crate::host::{Host, HostKind}; #[test] fn basic_ip_list() { diff --git a/testing-framework/tools/cfgsync/src/host.rs b/testing-framework/tools/cfgsync/src/host.rs new file mode 100644 index 0000000..d382ba8 --- /dev/null +++ b/testing-framework/tools/cfgsync/src/host.rs @@ -0,0 +1,76 @@ +use std::net::Ipv4Addr; + +pub const DEFAULT_LIBP2P_NETWORK_PORT: u16 = 3000; +pub const DEFAULT_DA_NETWORK_PORT: u16 = 3300; +pub const DEFAULT_BLEND_PORT: u16 = 3400; +pub const DEFAULT_API_PORT: u16 = 18080; + +#[derive(Copy, Clone, Eq, PartialEq, Hash)] +pub enum HostKind { + Validator, + Executor, +} + +#[derive(Eq, PartialEq, Hash, Clone)] +pub struct Host { + pub kind: HostKind, + pub ip: Ipv4Addr, + pub identifier: String, + pub network_port: u16, + pub da_network_port: u16, + pub blend_port: u16, + pub api_port: u16, + pub testing_http_port: u16, +} + +#[derive(Clone, Copy)] +pub struct PortOverrides { + pub network_port: Option, + pub da_network_port: Option, + pub blend_port: Option, + pub api_port: Option, + pub testing_http_port: Option, +} + +impl Host { + fn from_parts(kind: HostKind, ip: Ipv4Addr, identifier: String, ports: PortOverrides) -> Self { + Self { + kind, + ip, + identifier, + network_port: ports.network_port.unwrap_or(DEFAULT_LIBP2P_NETWORK_PORT), + da_network_port: ports.da_network_port.unwrap_or(DEFAULT_DA_NETWORK_PORT), + blend_port: ports.blend_port.unwrap_or(DEFAULT_BLEND_PORT), + api_port: ports.api_port.unwrap_or(DEFAULT_API_PORT), + testing_http_port: ports.testing_http_port.unwrap_or(DEFAULT_API_PORT + 1), + } + } + + #[must_use] + pub fn validator_from_ip(ip: Ipv4Addr, identifier: String, ports: PortOverrides) -> Self { + Self::from_parts(HostKind::Validator, ip, identifier, ports) + } + + #[must_use] + pub fn executor_from_ip(ip: Ipv4Addr, identifier: String, ports: PortOverrides) -> Self { + Self::from_parts(HostKind::Executor, ip, identifier, ports) + } +} + +#[must_use] +pub fn sort_hosts(mut hosts: Vec) -> Vec { + hosts.sort_by_key(|host| { + let index = host + .identifier + .rsplit('-') + .next() + .and_then(|raw| raw.parse::().ok()) + .unwrap_or(0); + let kind = match host.kind { + HostKind::Validator => 0, + HostKind::Executor => 1, + }; + (kind, index) + }); + hosts +} diff --git a/testing-framework/tools/cfgsync/src/lib.rs b/testing-framework/tools/cfgsync/src/lib.rs index f4662d8..cd46000 100644 --- a/testing-framework/tools/cfgsync/src/lib.rs +++ b/testing-framework/tools/cfgsync/src/lib.rs @@ -1,4 +1,6 @@ pub mod client; pub mod config; +pub mod host; +pub mod network; pub mod repo; pub mod server; diff --git a/testing-framework/tools/cfgsync/src/network/mod.rs b/testing-framework/tools/cfgsync/src/network/mod.rs new file mode 100644 index 0000000..c4a3e25 --- /dev/null +++ b/testing-framework/tools/cfgsync/src/network/mod.rs @@ -0,0 +1,49 @@ +use std::str::FromStr; + +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 + } + }) +}