Add node config overrides (#14)

This commit is contained in:
Andrus Salumets 2026-01-27 13:12:45 +01:00 committed by GitHub
parent 374f0db6fd
commit 3977a90682
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 288 additions and 51 deletions

View File

@ -85,6 +85,7 @@ impl Workload for JoinNodeWithPeersWorkload {
let options = StartNodeOptions {
peers: PeerSelection::Named(self.peers.clone()),
config_patch: None,
};
let node = handle.start_node_with(&self.name, options).await?;
let client = node.api;

View File

@ -32,6 +32,7 @@ async fn manual_cluster_two_clusters_merge() -> Result<()> {
"a",
StartNodeOptions {
peers: PeerSelection::None,
config_patch: None,
},
)
.await?
@ -46,6 +47,7 @@ async fn manual_cluster_two_clusters_merge() -> Result<()> {
"c",
StartNodeOptions {
peers: PeerSelection::Named(vec!["node-a".to_owned()]),
config_patch: None,
},
)
.await?

View File

@ -0,0 +1,113 @@
use std::{
net::{SocketAddr, TcpListener},
time::Duration,
};
use anyhow::Result;
use testing_framework_core::{
nodes::ApiClient,
scenario::{Deployer, PeerSelection, ScenarioBuilder, StartNodeOptions},
topology::config::TopologyConfig,
};
use testing_framework_runner_local::LocalDeployer;
use tracing_subscriber::fmt::try_init;
#[tokio::test]
#[ignore = "run manually with `cargo test -p runner-examples -- --ignored manual_cluster_api_port_override`"]
async fn manual_cluster_api_port_override() -> Result<()> {
let _ = try_init();
// Required env vars (set on the command line when running this test):
// - `POL_PROOF_DEV_MODE=true`
// - `LOGOS_BLOCKCHAIN_NODE_BIN=...`
// - `LOGOS_BLOCKCHAIN_CIRCUITS=...`
// - `RUST_LOG=info` (optional)
let api_port = random_api_port();
let deployer = LocalDeployer::new();
let cluster = deployer.manual_cluster(TopologyConfig::with_node_numbers(1))?;
let node = cluster
.start_node_with(
"override-api",
StartNodeOptions {
peers: PeerSelection::None,
config_patch: None,
}
.create_patch(move |mut config| {
println!("overriding API port to {api_port}");
let current_addr = config.http.backend_settings.address;
config.http.backend_settings.address = SocketAddr::new(current_addr.ip(), api_port);
Ok(config)
}),
)
.await?
.api;
node.consensus_info()
.await
.expect("consensus_info should succeed");
assert_eq!(resolved_port(&node), api_port);
Ok(())
}
#[tokio::test]
#[ignore = "run manually with `cargo test -p runner-examples -- --ignored scenario_builder_api_port_override`"]
async fn scenario_builder_api_port_override() -> Result<()> {
let _ = try_init();
// Required env vars (set on the command line when running this test):
// - `POL_PROOF_DEV_MODE=true`
// - `LOGOS_BLOCKCHAIN_NODE_BIN=...`
// - `LOGOS_BLOCKCHAIN_CIRCUITS=...`
// - `RUST_LOG=info` (optional)
let api_port = random_api_port();
let mut scenario = ScenarioBuilder::topology_with(|t| {
t.network_star()
.nodes(1)
.node_config_patch_with(0, move |mut config| {
println!("overriding API port to {api_port}");
let current_addr = config.http.backend_settings.address;
config.http.backend_settings.address = SocketAddr::new(current_addr.ip(), api_port);
Ok(config)
})
})
.with_run_duration(Duration::from_secs(1))
.build()?;
let deployer = LocalDeployer::default();
let runner = deployer.deploy(&scenario).await?;
let handle = runner.run(&mut scenario).await?;
let client = handle
.context()
.node_clients()
.any_client()
.ok_or_else(|| anyhow::anyhow!("scenario did not expose any node clients"))?;
client
.consensus_info()
.await
.expect("consensus_info should succeed");
assert_eq!(resolved_port(&client), api_port);
Ok(())
}
fn random_api_port() -> u16 {
let listener = TcpListener::bind("127.0.0.1:0").expect("bind random API port");
listener.local_addr().expect("read API port").port()
}
fn resolved_port(client: &ApiClient) -> u16 {
client.base_url().port().unwrap_or_default()
}

View File

@ -16,6 +16,8 @@ use crate::{
node::{NodeAddresses, NodeConfigCommon, NodeHandle, SpawnNodeError, spawn_node},
},
},
scenario::DynError,
topology::config::NodeConfigPatch,
};
const BIN_PATH: &str = "target/debug/logos-blockchain-node";
@ -34,6 +36,23 @@ pub struct Node {
handle: NodeHandle<Config>,
}
pub fn apply_node_config_patches<'a>(
mut config: Config,
patches: impl IntoIterator<Item = &'a NodeConfigPatch>,
) -> Result<Config, DynError> {
for patch in patches {
config = patch(config)?;
}
Ok(config)
}
pub fn apply_node_config_patch(
config: Config,
patch: &NodeConfigPatch,
) -> Result<Config, DynError> {
apply_node_config_patches(config, [patch])
}
impl Deref for Node {
type Target = NodeHandle<Config>;

View File

@ -1,8 +1,10 @@
use std::sync::Arc;
use async_trait::async_trait;
use reqwest::Url;
use super::DynError;
use crate::nodes::ApiClient;
use crate::{nodes::ApiClient, topology::config::NodeConfigPatch};
/// Marker type used by scenario builders to request node control support.
#[derive(Clone, Copy, Debug, Default)]
@ -34,20 +36,33 @@ pub enum PeerSelection {
}
/// Options for dynamically starting a node.
#[derive(Clone, Debug)]
#[derive(Clone)]
pub struct StartNodeOptions {
/// How to select initial peers on startup.
pub peers: PeerSelection,
/// Optional node config patch applied before spawn.
pub config_patch: Option<NodeConfigPatch>,
}
impl Default for StartNodeOptions {
fn default() -> Self {
Self {
peers: PeerSelection::DefaultLayout,
config_patch: None,
}
}
}
impl StartNodeOptions {
pub fn create_patch<F>(mut self, f: F) -> Self
where
F: Fn(nomos_node::Config) -> Result<nomos_node::Config, DynError> + Send + Sync + 'static,
{
self.config_patch = Some(Arc::new(f));
self
}
}
/// Trait implemented by scenario capability markers to signal whether node
/// control is required.
pub trait RequiresNodeControl {

View File

@ -1,5 +1,6 @@
use std::{num::NonZeroUsize, sync::Arc, time::Duration};
use nomos_node::Config as NodeConfig;
use thiserror::Error;
use tracing::{debug, info};
@ -8,7 +9,7 @@ use super::{
workload::Workload,
};
use crate::topology::{
config::{TopologyBuildError, TopologyBuilder, TopologyConfig},
config::{NodeConfigPatch, TopologyBuildError, TopologyBuilder, TopologyConfig},
configs::{network::Libp2pNetworkLayout, wallet::WalletConfig},
generation::GeneratedTopology,
};
@ -302,16 +303,37 @@ impl<Caps> TopologyConfigurator<Caps> {
self
}
/// Apply a config patch for a specific node index.
#[must_use]
pub fn node_config_patch(mut self, index: usize, patch: NodeConfigPatch) -> Self {
self.builder.topology = self.builder.topology.with_node_config_patch(index, patch);
self
}
/// Apply a config patch for a specific node index.
#[must_use]
pub fn node_config_patch_with<F>(mut self, index: usize, f: F) -> Self
where
F: Fn(NodeConfig) -> Result<NodeConfig, DynError> + Send + Sync + 'static,
{
self.builder.topology = self
.builder
.topology
.with_node_config_patch(index, Arc::new(f));
self
}
/// Finalize and return the underlying scenario builder.
#[must_use]
pub fn apply(self) -> Builder<Caps> {
let mut config = TopologyConfig::with_node_numbers(self.nodes);
if self.network_star {
config.network_params.libp2p_network_layout = Libp2pNetworkLayout::Star;
}
let mut builder = self.builder;
builder.topology = TopologyBuilder::new(config);
builder.topology = builder.topology.with_node_count(self.nodes);
if self.network_star {
builder.topology = builder
.topology
.with_network_layout(Libp2pNetworkLayout::Star);
}
builder
}
}

View File

@ -1,7 +1,10 @@
use std::{collections::HashMap, sync::Arc};
use nomos_core::{
mantle::GenesisTx as _,
sdp::{Locator, ServiceType},
};
use nomos_node::Config as NodeConfig;
use testing_framework_config::topology::{
configs::{
api::{ApiConfigError, create_api_configs},
@ -18,12 +21,18 @@ use testing_framework_config::topology::{
};
use thiserror::Error;
use crate::topology::{
configs::{GeneralConfig, time::default_time_config},
generation::{GeneratedNodeConfig, GeneratedTopology},
utils::{TopologyResolveError, create_kms_configs, resolve_ids, resolve_ports},
use crate::{
scenario::DynError,
topology::{
configs::{GeneralConfig, time::default_time_config},
generation::{GeneratedNodeConfig, GeneratedTopology},
utils::{TopologyResolveError, create_kms_configs, resolve_ids, resolve_ports},
},
};
/// Per-node config patch applied after the default node config is generated.
pub type NodeConfigPatch = Arc<dyn Fn(NodeConfig) -> Result<NodeConfig, DynError> + Send + Sync>;
#[derive(Debug, Error)]
pub enum TopologyBuildError {
#[error("topology must include at least one node")]
@ -55,6 +64,7 @@ pub struct TopologyConfig {
pub consensus_params: ConsensusParams,
pub network_params: NetworkParams,
pub wallet_config: WalletConfig,
pub node_config_patches: HashMap<usize, NodeConfigPatch>,
}
impl TopologyConfig {
@ -66,6 +76,7 @@ impl TopologyConfig {
consensus_params: ConsensusParams::default_for_participants(1),
network_params: NetworkParams::default(),
wallet_config: WalletConfig::default(),
node_config_patches: HashMap::new(),
}
}
@ -77,6 +88,7 @@ impl TopologyConfig {
consensus_params: ConsensusParams::default_for_participants(2),
network_params: NetworkParams::default(),
wallet_config: WalletConfig::default(),
node_config_patches: HashMap::new(),
}
}
@ -90,6 +102,7 @@ impl TopologyConfig {
consensus_params: ConsensusParams::default_for_participants(participants),
network_params: NetworkParams::default(),
wallet_config: WalletConfig::default(),
node_config_patches: HashMap::new(),
}
}
@ -97,6 +110,17 @@ impl TopologyConfig {
pub const fn wallet(&self) -> &WalletConfig {
&self.wallet_config
}
#[must_use]
pub fn node_config_patch(&self, index: usize) -> Option<&NodeConfigPatch> {
self.node_config_patches.get(&index)
}
#[must_use]
pub fn with_node_config_patch(mut self, index: usize, patch: NodeConfigPatch) -> Self {
self.node_config_patches.insert(index, patch);
self
}
}
/// Builder that produces `GeneratedTopology` instances from a `TopologyConfig`.
@ -132,6 +156,13 @@ impl TopologyBuilder {
self
}
#[must_use]
/// Apply a config patch for a specific node index.
pub fn with_node_config_patch(mut self, index: usize, patch: NodeConfigPatch) -> Self {
self.config.node_config_patches.insert(index, patch);
self
}
#[must_use]
/// Set node counts.
pub const fn with_node_count(mut self, nodes: usize) -> Self {
@ -204,6 +235,7 @@ impl TopologyBuilder {
&tracing_configs,
&kms_configs,
&time_config,
&config.node_config_patches,
)?;
Ok(GeneratedTopology { config, nodes })
@ -291,6 +323,7 @@ fn build_node_descriptors(
tracing_configs: &[testing_framework_config::topology::configs::tracing::GeneralTracingConfig],
kms_configs: &[key_management_system_service::backend::preload::PreloadKMSBackendSettings],
time_config: &testing_framework_config::topology::configs::time::GeneralTimeConfig,
node_config_patches: &HashMap<usize, NodeConfigPatch>,
) -> Result<Vec<GeneratedNodeConfig>, TopologyBuildError> {
let mut nodes = Vec::with_capacity(config.n_nodes);
@ -324,6 +357,7 @@ fn build_node_descriptors(
id,
general,
blend_port,
config_patch: node_config_patches.get(&i).cloned(),
};
nodes.push(descriptor);

View File

@ -5,12 +5,12 @@ use thiserror::Error;
use crate::{
nodes::{
common::node::SpawnNodeError,
node::{Node, create_node_config},
node::{Node, apply_node_config_patch, create_node_config},
},
scenario,
topology::{
config::{TopologyBuildError, TopologyBuilder, TopologyConfig},
configs::GeneralConfig,
generation::find_expected_peer_counts,
generation::{GeneratedNodeConfig, find_expected_peer_counts},
readiness::{NetworkReadiness, ReadinessCheck, ReadinessError},
utils::multiaddr_port,
},
@ -29,18 +29,17 @@ pub enum SpawnTopologyError {
Build(#[from] TopologyBuildError),
#[error(transparent)]
Node(#[from] SpawnNodeError),
#[error("node config patch failed for node-{index}: {source}")]
ConfigPatch {
index: usize,
source: scenario::DynError,
},
}
impl Topology {
pub async fn spawn(config: TopologyConfig) -> Result<Self, SpawnTopologyError> {
let generated = TopologyBuilder::new(config.clone()).build()?;
let n_nodes = config.n_nodes;
let node_configs = generated
.iter()
.map(|node| node.general.clone())
.collect::<Vec<_>>();
let nodes = Self::spawn_nodes(node_configs, n_nodes).await?;
let nodes = Self::spawn_nodes(generated.nodes()).await?;
Ok(Self { nodes })
}
@ -55,28 +54,32 @@ impl Topology {
.with_blend_ports(blend_ports.to_vec())
.build()?;
let node_configs = generated
.iter()
.map(|node| node.general.clone())
.collect::<Vec<_>>();
let nodes = Self::spawn_nodes(node_configs, config.n_nodes).await?;
let nodes = Self::spawn_nodes(generated.nodes()).await?;
Ok(Self { nodes })
}
pub(crate) async fn spawn_nodes(
config: Vec<GeneralConfig>,
n_nodes: usize,
nodes: &[GeneratedNodeConfig],
) -> Result<DeployedNodes, SpawnTopologyError> {
let mut nodes = Vec::new();
for i in 0..n_nodes {
let config = create_node_config(config[i].clone());
let label = format!("node-{i}");
nodes.push(Node::spawn(config, &label).await?);
let mut spawned = Vec::new();
for node in nodes {
let mut config = create_node_config(node.general.clone());
if let Some(patch) = node.config_patch.as_ref() {
config = apply_node_config_patch(config, patch).map_err(|source| {
SpawnTopologyError::ConfigPatch {
index: node.index,
source,
}
})?;
}
let label = format!("node-{}", node.index);
spawned.push(Node::spawn(config, &label).await?);
}
Ok(nodes)
Ok(spawned)
}
#[must_use]

View File

@ -3,7 +3,7 @@ use std::{collections::HashSet, time::Duration};
use reqwest::{Client, Url};
use crate::topology::{
config::TopologyConfig,
config::{NodeConfigPatch, TopologyConfig},
configs::{GeneralConfig, wallet::WalletAccount},
deployment::{SpawnTopologyError, Topology},
readiness::{HttpNetworkReadiness, ReadinessCheck, ReadinessError},
@ -16,6 +16,7 @@ pub struct GeneratedNodeConfig {
pub id: [u8; 32],
pub general: GeneralConfig,
pub blend_port: u16,
pub config_patch: Option<NodeConfigPatch>,
}
impl GeneratedNodeConfig {
@ -82,12 +83,7 @@ impl GeneratedTopology {
}
pub async fn spawn_local(&self) -> Result<Topology, SpawnTopologyError> {
let configs = self
.iter()
.map(|node| node.general.clone())
.collect::<Vec<_>>();
let nodes = Topology::spawn_nodes(configs, self.config.n_nodes).await?;
let nodes = Topology::spawn_nodes(self.nodes()).await?;
Ok(Topology { nodes })
}

View File

@ -11,7 +11,7 @@ use testing_framework_config::topology::configs::{
use testing_framework_core::{
scenario::{PeerSelection, StartNodeOptions},
topology::{
config::TopologyConfig,
config::{NodeConfigPatch, TopologyConfig},
configs::GeneralConfig,
generation::{GeneratedNodeConfig, GeneratedTopology},
},
@ -27,7 +27,7 @@ pub(super) fn build_general_config_for(
peer_ports_by_name: &HashMap<String, u16>,
options: &StartNodeOptions,
peer_ports: &[u16],
) -> Result<(GeneralConfig, u16), LocalDynamicError> {
) -> Result<(GeneralConfig, u16, Option<NodeConfigPatch>), LocalDynamicError> {
if let Some(node) = descriptor_for(descriptors, index) {
let mut config = node.general.clone();
let initial_peers = resolve_initial_peers(
@ -40,7 +40,7 @@ pub(super) fn build_general_config_for(
config.network_config.backend.initial_peers = initial_peers;
return Ok((config, node.network_port()));
return Ok((config, node.network_port(), node.config_patch.clone()));
}
let id = random_node_id();
@ -61,7 +61,7 @@ pub(super) fn build_general_config_for(
)
.map_err(|source| LocalDynamicError::Config { source })?;
Ok((general_config, network_port))
Ok((general_config, network_port, None))
}
fn descriptor_for(descriptors: &GeneratedTopology, index: usize) -> Option<&GeneratedNodeConfig> {

View File

@ -8,7 +8,7 @@ use testing_framework_config::topology::configs::{consensus, time};
use testing_framework_core::{
nodes::{
ApiClient,
node::{Node, create_node_config},
node::{Node, apply_node_config_patch, create_node_config},
},
scenario::{DynError, NodeControlHandle, StartNodeOptions, StartedNode},
topology::{
@ -41,6 +41,8 @@ pub enum LocalDynamicError {
InvalidArgument { message: String },
#[error("{message}")]
PortAllocation { message: String },
#[error("node config patch failed: {message}")]
ConfigPatch { message: String },
}
pub struct LocalDynamicNodes {
@ -230,7 +232,7 @@ impl LocalDynamicNodes {
)
};
let (general_config, network_port) = build_general_config_for(
let (general_config, network_port, descriptor_patch) = build_general_config_for(
&self.descriptors,
&self.base_consensus,
&self.base_time,
@ -240,7 +242,12 @@ impl LocalDynamicNodes {
&peer_ports,
)?;
let config = create_node_config(general_config);
let config = build_node_config(
general_config,
descriptor_patch.as_ref(),
options.config_patch.as_ref(),
)?;
let api_client = self
.spawn_and_register_node(&node_name, network_port, config)
.await?;
@ -275,6 +282,31 @@ impl LocalDynamicNodes {
}
}
fn build_node_config(
general_config: testing_framework_config::topology::configs::GeneralConfig,
descriptor_patch: Option<&testing_framework_core::topology::config::NodeConfigPatch>,
options_patch: Option<&testing_framework_core::topology::config::NodeConfigPatch>,
) -> Result<NodeConfig, LocalDynamicError> {
let mut config = create_node_config(general_config);
config = apply_patch_if_needed(config, descriptor_patch)?;
config = apply_patch_if_needed(config, options_patch)?;
Ok(config)
}
fn apply_patch_if_needed(
config: NodeConfig,
patch: Option<&testing_framework_core::topology::config::NodeConfigPatch>,
) -> Result<NodeConfig, LocalDynamicError> {
let Some(patch) = patch else {
return Ok(config);
};
apply_node_config_patch(config, patch).map_err(|err| LocalDynamicError::ConfigPatch {
message: err.to_string(),
})
}
#[async_trait::async_trait]
impl NodeControlHandle for LocalDynamicNodes {
async fn restart_node(&self, _index: usize) -> Result<(), DynError> {