From 4a47024a11cbece21fc9911b9eb8d46a5c7bedf6 Mon Sep 17 00:00:00 2001 From: andrussal Date: Thu, 18 Dec 2025 18:08:53 +0100 Subject: [PATCH] config: return Result from api/network config builders --- .../configs/src/topology/configs/api.rs | 26 ++++++++++++------- .../configs/src/topology/configs/mod.rs | 8 +++--- .../configs/src/topology/configs/network.rs | 21 ++++++++++----- testing-framework/core/src/topology/config.rs | 10 ++++--- .../tools/cfgsync/src/config/builder.rs | 4 ++- 5 files changed, 45 insertions(+), 24 deletions(-) diff --git a/testing-framework/configs/src/topology/configs/api.rs b/testing-framework/configs/src/topology/configs/api.rs index bd759c7..7226ca4 100644 --- a/testing-framework/configs/src/topology/configs/api.rs +++ b/testing-framework/configs/src/topology/configs/api.rs @@ -1,6 +1,7 @@ use std::net::SocketAddr; use nomos_utils::net::get_available_tcp_port; +use thiserror::Error; #[derive(Clone)] pub struct GeneralApiConfig { @@ -8,16 +9,23 @@ pub struct GeneralApiConfig { pub testing_http_address: SocketAddr, } -#[must_use] -pub fn create_api_configs(ids: &[[u8; 32]]) -> Vec { +#[derive(Debug, Error)] +pub enum ApiConfigError { + #[error("failed to allocate a free TCP port for API config")] + PortAllocationFailed, +} + +pub fn create_api_configs(ids: &[[u8; 32]]) -> Result, ApiConfigError> { ids.iter() - .map(|_| GeneralApiConfig { - address: format!("127.0.0.1:{}", get_available_tcp_port().unwrap()) - .parse() - .unwrap(), - testing_http_address: format!("127.0.0.1:{}", get_available_tcp_port().unwrap()) - .parse() - .unwrap(), + .map(|_| { + let address_port = + get_available_tcp_port().ok_or(ApiConfigError::PortAllocationFailed)?; + let testing_port = + get_available_tcp_port().ok_or(ApiConfigError::PortAllocationFailed)?; + Ok(GeneralApiConfig { + address: format!("127.0.0.1:{address_port}").parse().unwrap(), + testing_http_address: format!("127.0.0.1:{testing_port}").parse().unwrap(), + }) }) .collect() } diff --git a/testing-framework/configs/src/topology/configs/mod.rs b/testing-framework/configs/src/topology/configs/mod.rs index 2cfc058..1eb512e 100644 --- a/testing-framework/configs/src/topology/configs/mod.rs +++ b/testing-framework/configs/src/topology/configs/mod.rs @@ -96,9 +96,11 @@ pub fn create_general_configs_with_blend_core_subset( consensus::create_consensus_configs(&ids, &consensus_params, &WalletConfig::default()); let bootstrap_config = bootstrap::create_bootstrap_configs(&ids, SHORT_PROLONGED_BOOTSTRAP_PERIOD); - let network_configs = network::create_network_configs(&ids, network_params); - let da_configs = da::create_da_configs(&ids, &DaParams::default(), &da_ports); - let api_configs = api::create_api_configs(&ids); + let network_configs = + network::create_network_configs(&ids, network_params).expect("network config generation"); + let da_configs = + da::try_create_da_configs(&ids, &DaParams::default(), &da_ports).expect("DA configs"); + let api_configs = api::create_api_configs(&ids).expect("api config generation"); let blend_configs = blend::create_blend_configs(&ids, &blend_ports); let tracing_configs = tracing::create_tracing_configs(&ids); let time_config = time::default_time_config(); diff --git a/testing-framework/configs/src/topology/configs/network.rs b/testing-framework/configs/src/topology/configs/network.rs index db2b820..4a98f94 100644 --- a/testing-framework/configs/src/topology/configs/network.rs +++ b/testing-framework/configs/src/topology/configs/network.rs @@ -5,6 +5,7 @@ use nomos_libp2p::{ }; use nomos_node::config::network::serde::{BackendSettings, Config, SwarmConfig}; use nomos_utils::net::get_available_udp_port; +use thiserror::Error; use crate::node_address_from_port; @@ -25,6 +26,12 @@ pub struct NetworkParams { pub type GeneralNetworkConfig = Config; +#[derive(Debug, Error)] +pub enum NetworkConfigError { + #[error("failed to allocate a free UDP port for libp2p swarm")] + PortAllocationFailed, +} + fn default_swarm_config() -> SwarmConfig { SwarmConfig { host: std::net::Ipv4Addr::UNSPECIFIED, @@ -55,7 +62,7 @@ fn nat_settings(port: u16) -> NatSettings { pub fn create_network_configs( ids: &[[u8; 32]], network_params: &NetworkParams, -) -> Vec { +) -> Result, NetworkConfigError> { let swarm_configs: Vec = ids .iter() .map(|id| { @@ -63,8 +70,8 @@ pub fn create_network_configs( let node_key = ed25519::SecretKey::try_from_bytes(&mut node_key_bytes) .expect("Failed to generate secret key from bytes"); - let port = get_available_udp_port().unwrap(); - SwarmConfig { + let port = get_available_udp_port().ok_or(NetworkConfigError::PortAllocationFailed)?; + Ok(SwarmConfig { node_key, port, chain_sync_config: cryptarchia_sync::Config { @@ -72,13 +79,13 @@ pub fn create_network_configs( }, nat_config: nat_settings(port), ..default_swarm_config() - } + }) }) - .collect(); + .collect::>()?; let all_initial_peers = initial_peers_by_network_layout(&swarm_configs, network_params); - swarm_configs + Ok(swarm_configs .iter() .zip(all_initial_peers) .map(|(swarm_config, initial_peers)| GeneralNetworkConfig { @@ -87,7 +94,7 @@ pub fn create_network_configs( swarm: swarm_config.to_owned(), }, }) - .collect() + .collect()) } fn initial_peers_by_network_layout( diff --git a/testing-framework/core/src/topology/config.rs b/testing-framework/core/src/topology/config.rs index aeb152d..7aef6f4 100644 --- a/testing-framework/core/src/topology/config.rs +++ b/testing-framework/core/src/topology/config.rs @@ -14,7 +14,7 @@ use testing_framework_config::topology::{ ConsensusParams, ProviderInfo, create_consensus_configs, create_genesis_tx_with_declarations, }, - da::{DaParams, create_da_configs}, + da::{DaParams, try_create_da_configs}, network::{Libp2pNetworkLayout, NetworkParams, create_network_configs}, tracing::create_tracing_configs, wallet::WalletConfig, @@ -267,10 +267,12 @@ impl TopologyBuilder { let mut consensus_configs = create_consensus_configs(&ids, &config.consensus_params, &config.wallet_config); let bootstrapping_config = create_bootstrap_configs(&ids, SHORT_PROLONGED_BOOTSTRAP_PERIOD); - let da_configs = create_da_configs(&ids, &config.da_params, &da_ports); - let network_configs = create_network_configs(&ids, &config.network_params); + let da_configs = try_create_da_configs(&ids, &config.da_params, &da_ports) + .expect("failed to create DA configs"); + let network_configs = create_network_configs(&ids, &config.network_params) + .expect("failed to create network configs"); let blend_configs = create_blend_configs(&ids, &blend_ports); - let api_configs = create_api_configs(&ids); + let api_configs = create_api_configs(&ids).expect("failed to create API configs"); let tracing_configs = create_tracing_configs(&ids); let time_config = default_time_config(); diff --git a/testing-framework/tools/cfgsync/src/config/builder.rs b/testing-framework/tools/cfgsync/src/config/builder.rs index 22f779e..53f013b 100644 --- a/testing-framework/tools/cfgsync/src/config/builder.rs +++ b/testing-framework/tools/cfgsync/src/config/builder.rs @@ -66,6 +66,8 @@ pub enum NodeConfigBuildError { Providers(#[from] ProviderBuildError), #[error(transparent)] Da(#[from] da::DaConfigError), + #[error(transparent)] + Network(#[from] network::NetworkConfigError), #[error("failed to allocate an available UDP port")] PortAllocFailed, #[error("failed to parse multiaddr '{value}': {message}")] @@ -271,7 +273,7 @@ fn build_base_configs( consensus_configs: create_consensus_configs(ids, consensus_params, wallet_config), bootstrap_configs: create_bootstrap_configs(ids, SHORT_PROLONGED_BOOTSTRAP_PERIOD), da_configs: try_create_da_configs(ids, da_params, da_ports)?, - network_configs: create_network_configs(ids, &NetworkParams::default()), + network_configs: create_network_configs(ids, &NetworkParams::default())?, blend_configs: create_blend_configs(ids, blend_ports), }) }