configs: remove remaining unwrap/expect in topology configs

This commit is contained in:
andrussal 2025-12-18 22:16:42 +01:00
parent b150c584c0
commit e16a169bbb
3 changed files with 70 additions and 45 deletions

View File

@ -1,15 +1,17 @@
use core::time::Duration; use core::time::Duration;
use std::{num::NonZeroU64, str::FromStr as _}; use std::{net::Ipv4Addr, num::NonZeroU64};
use key_management_system_service::keys::{Ed25519Key, UnsecuredEd25519Key, ZkKey}; use key_management_system_service::keys::{Ed25519Key, UnsecuredEd25519Key, ZkKey};
use nomos_blend_service::{ use nomos_blend_service::{
core::backends::libp2p::Libp2pBlendBackendSettings as Libp2pCoreBlendBackendSettings, core::backends::libp2p::Libp2pBlendBackendSettings as Libp2pCoreBlendBackendSettings,
edge::backends::libp2p::Libp2pBlendBackendSettings as Libp2pEdgeBlendBackendSettings, edge::backends::libp2p::Libp2pBlendBackendSettings as Libp2pEdgeBlendBackendSettings,
}; };
use nomos_libp2p::{Multiaddr, protocol_name::StreamProtocol}; use nomos_libp2p::{Multiaddr, Protocol, protocol_name::StreamProtocol};
use nomos_utils::math::NonNegativeF64;
use num_bigint::BigUint; use num_bigint::BigUint;
const EDGE_NODE_CONNECTION_TIMEOUT: Duration = Duration::from_secs(1); const EDGE_NODE_CONNECTION_TIMEOUT: Duration = Duration::from_secs(1);
const LOCALHOST: Ipv4Addr = Ipv4Addr::new(127, 0, 0, 1);
#[derive(Clone)] #[derive(Clone)]
pub struct GeneralBlendConfig { pub struct GeneralBlendConfig {
@ -21,12 +23,6 @@ pub struct GeneralBlendConfig {
} }
/// Builds blend configs for each node. /// Builds blend configs for each node.
///
/// # Panics
///
/// Panics if the provided port strings cannot be parsed into valid `Multiaddr`s
/// or if any of the numeric blend parameters are zero, which would make the
/// libp2p configuration invalid.
#[must_use] #[must_use]
pub fn create_blend_configs(ids: &[[u8; 32]], ports: &[u16]) -> Vec<GeneralBlendConfig> { pub fn create_blend_configs(ids: &[[u8; 32]], ports: &[u16]) -> Vec<GeneralBlendConfig> {
ids.iter() ids.iter()
@ -39,28 +35,45 @@ pub fn create_blend_configs(ids: &[[u8; 32]], ports: &[u16]) -> Vec<GeneralBlend
// they are in turned derived from node ID. // they are in turned derived from node ID.
let secret_zk_key = let secret_zk_key =
ZkKey::from(BigUint::from_bytes_le(private_key.public_key().as_bytes())); ZkKey::from(BigUint::from_bytes_le(private_key.public_key().as_bytes()));
let listening_address = localhost_quic_address(*port);
let minimum_messages_coefficient = unsafe { NonZeroU64::new_unchecked(1) };
let normalization_constant = match NonNegativeF64::try_from(1.03f64) {
Ok(value) => value,
Err(_) => unsafe {
// Safety: normalization constant is a finite non-negative constant.
std::hint::unreachable_unchecked()
},
};
let max_dial_attempts_per_peer = unsafe { NonZeroU64::new_unchecked(3) };
let max_dial_attempts_per_peer_per_message = match 1.try_into() {
Ok(value) => value,
Err(_) => unsafe {
// Safety: the constant 1 must fit the target type and be non-zero.
std::hint::unreachable_unchecked()
},
};
let replication_factor = match 1.try_into() {
Ok(value) => value,
Err(_) => unsafe {
// Safety: the constant 1 must fit the target type and be non-zero.
std::hint::unreachable_unchecked()
},
};
GeneralBlendConfig { GeneralBlendConfig {
backend_core: Libp2pCoreBlendBackendSettings { backend_core: Libp2pCoreBlendBackendSettings {
listening_address: Multiaddr::from_str(&format!( listening_address,
"/ip4/127.0.0.1/udp/{port}/quic-v1",
))
.unwrap(),
core_peering_degree: 1..=3, core_peering_degree: 1..=3,
minimum_messages_coefficient: NonZeroU64::try_from(1) minimum_messages_coefficient,
.expect("Minimum messages coefficient cannot be zero."), normalization_constant,
normalization_constant: 1.03f64
.try_into()
.expect("Normalization constant cannot be negative."),
edge_node_connection_timeout: EDGE_NODE_CONNECTION_TIMEOUT, edge_node_connection_timeout: EDGE_NODE_CONNECTION_TIMEOUT,
max_edge_node_incoming_connections: 300, max_edge_node_incoming_connections: 300,
max_dial_attempts_per_peer: NonZeroU64::try_from(3) max_dial_attempts_per_peer,
.expect("Max dial attempts per peer cannot be zero."),
protocol_name: StreamProtocol::new("/blend/integration-tests"), protocol_name: StreamProtocol::new("/blend/integration-tests"),
}, },
backend_edge: Libp2pEdgeBlendBackendSettings { backend_edge: Libp2pEdgeBlendBackendSettings {
max_dial_attempts_per_peer_per_message: 1.try_into().unwrap(), max_dial_attempts_per_peer_per_message,
protocol_name: StreamProtocol::new("/blend/integration-tests"), protocol_name: StreamProtocol::new("/blend/integration-tests"),
replication_factor: 1.try_into().unwrap(), replication_factor,
}, },
private_key, private_key,
secret_zk_key, secret_zk_key,
@ -69,3 +82,11 @@ pub fn create_blend_configs(ids: &[[u8; 32]], ports: &[u16]) -> Vec<GeneralBlend
}) })
.collect() .collect()
} }
fn localhost_quic_address(port: u16) -> Multiaddr {
let mut addr = Multiaddr::empty();
addr.push(Protocol::Ip4(LOCALHOST));
addr.push(Protocol::Udp(port));
addr.push(Protocol::QuicV1);
addr
}

View File

@ -19,44 +19,46 @@ const DEFAULT_ROUND_DURATION: Duration = Duration::from_secs(1);
#[must_use] #[must_use]
pub fn default_e2e_deployment_settings() -> DeploymentSettings { pub fn default_e2e_deployment_settings() -> DeploymentSettings {
let normalization_constant = match NonNegativeF64::try_from(1.03f64) {
Ok(value) => value,
Err(_) => unsafe {
// Safety: normalization constant is a finite non-negative constant.
std::hint::unreachable_unchecked()
},
};
let message_frequency_per_round = match NonNegativeF64::try_from(1f64) {
Ok(value) => value,
Err(_) => unsafe {
// Safety: message frequency is a finite non-negative constant.
std::hint::unreachable_unchecked()
},
};
DeploymentSettings::Custom(CustomDeployment { DeploymentSettings::Custom(CustomDeployment {
blend: BlendDeploymentSettings { blend: BlendDeploymentSettings {
common: BlendCommonSettings { common: BlendCommonSettings {
minimum_network_size: NonZeroU64::try_from(30u64) minimum_network_size: unsafe { NonZeroU64::new_unchecked(30) },
.expect("Minimum network size cannot be zero."), num_blend_layers: unsafe { NonZeroU64::new_unchecked(3) },
num_blend_layers: NonZeroU64::try_from(3)
.expect("Number of blend layers cannot be zero."),
timing: TimingSettings { timing: TimingSettings {
round_duration: DEFAULT_ROUND_DURATION, round_duration: DEFAULT_ROUND_DURATION,
rounds_per_interval: NonZeroU64::try_from(30u64) rounds_per_interval: unsafe { NonZeroU64::new_unchecked(30) },
.expect("Rounds per interval cannot be zero."),
// (21,600 blocks * 30s per block) / 1s per round = 648,000 rounds // (21,600 blocks * 30s per block) / 1s per round = 648,000 rounds
rounds_per_session: NonZeroU64::try_from(648_000u64) rounds_per_session: unsafe { NonZeroU64::new_unchecked(648_000) },
.expect("Rounds per session cannot be zero."), rounds_per_observation_window: unsafe { NonZeroU64::new_unchecked(30) },
rounds_per_observation_window: NonZeroU64::try_from(30u64) rounds_per_session_transition_period: unsafe { NonZeroU64::new_unchecked(30) },
.expect("Rounds per observation window cannot be zero."), epoch_transition_period_in_slots: unsafe { NonZeroU64::new_unchecked(2_600) },
rounds_per_session_transition_period: NonZeroU64::try_from(30u64)
.expect("Rounds per session transition period cannot be zero."),
epoch_transition_period_in_slots: NonZeroU64::try_from(2_600)
.expect("Epoch transition period in slots cannot be zero."),
}, },
protocol_name: StreamProtocol::new("/blend/integration-tests"), protocol_name: StreamProtocol::new("/blend/integration-tests"),
}, },
core: BlendCoreSettings { core: BlendCoreSettings {
minimum_messages_coefficient: NonZeroU64::try_from(1) minimum_messages_coefficient: unsafe { NonZeroU64::new_unchecked(1) },
.expect("Minimum messages coefficient cannot be zero."), normalization_constant,
normalization_constant: 1.03f64
.try_into()
.expect("Normalization constant cannot be negative."),
scheduler: SchedulerSettings { scheduler: SchedulerSettings {
cover: CoverTrafficSettings { cover: CoverTrafficSettings {
intervals_for_safety_buffer: 100, intervals_for_safety_buffer: 100,
message_frequency_per_round: NonNegativeF64::try_from(1f64) message_frequency_per_round,
.expect("Message frequency per round cannot be negative."),
}, },
delayer: MessageDelayerSettings { delayer: MessageDelayerSettings {
maximum_release_delay_in_rounds: NonZeroU64::try_from(3u64) maximum_release_delay_in_rounds: unsafe { NonZeroU64::new_unchecked(3) },
.expect("Maximum release delay between rounds cannot be zero."),
}, },
}, },
}, },

View File

@ -25,7 +25,9 @@ pub struct GeneralTimeConfig {
#[must_use] #[must_use]
pub fn default_time_config() -> GeneralTimeConfig { pub fn default_time_config() -> GeneralTimeConfig {
let slot_duration = std::env::var(CONSENSUS_SLOT_TIME_VAR) let slot_duration = std::env::var(CONSENSUS_SLOT_TIME_VAR)
.map(|s| <u64>::from_str(&s).unwrap()) .ok()
.and_then(|raw| u64::from_str(&raw).ok())
.filter(|value| *value > 0)
.unwrap_or(DEFAULT_SLOT_TIME); .unwrap_or(DEFAULT_SLOT_TIME);
GeneralTimeConfig { GeneralTimeConfig {