Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Node envconfig #382

Merged
merged 5 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion nodes/nomos-node/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ consensus:
fountain_settings: null
overlay_settings:
nodes: [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]]
bacv marked this conversation as resolved.
Show resolved Hide resolved
leader_super_majority_threshold: 1
bacv marked this conversation as resolved.
Show resolved Hide resolved
leader:
cur: 0
network:
backend:
host: 0.0.0.0
port: 3000
log_level: "fatal"
nodeKey: null
node_key: "0000000000000000000000000000000000000000000000000000000000000001"
discV5BootstrapNodes: []
initial_peers: []
relayTopics: []
Expand Down
300 changes: 300 additions & 0 deletions nodes/nomos-node/src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,300 @@
use std::{
net::{IpAddr, SocketAddr},
path::PathBuf,
time::Duration,
};

use crate::Carnot;
use clap::{Parser, ValueEnum};
use color_eyre::eyre::{self, eyre, Result};
use hex::FromHex;
#[cfg(feature = "metrics")]
use metrics::{backend::map::MapMetricsBackend, types::MetricsData, MetricsService};
use nomos_http::{backends::axum::AxumBackend, http::HttpService};
#[cfg(feature = "libp2p")]
use nomos_libp2p::{secp256k1::SecretKey, Multiaddr};
use nomos_log::{Logger, LoggerBackend, LoggerFormat};
#[cfg(feature = "libp2p")]
use nomos_network::backends::libp2p::Libp2p;
#[cfg(feature = "waku")]
use nomos_network::backends::waku::Waku;
use nomos_network::NetworkService;
use overwatch_rs::services::ServiceData;
use serde::{Deserialize, Serialize};
use tracing::Level;
#[cfg(feature = "waku")]
use waku_bindings::{Multiaddr, SecretKey};

#[derive(ValueEnum, Clone, Debug, Default)]

Check warning on line 28 in nodes/nomos-node/src/config.rs

View check run for this annotation

Codecov / codecov/patch

nodes/nomos-node/src/config.rs#L28

Added line #L28 was not covered by tests
pub enum LoggerBackendType {
Gelf,
File,
#[default]
Stdout,
Stderr,
}

#[derive(Parser, Debug, Clone)]

Check warning on line 37 in nodes/nomos-node/src/config.rs

View check run for this annotation

Codecov / codecov/patch

nodes/nomos-node/src/config.rs#L37

Added line #L37 was not covered by tests
pub struct LogArgs {
/// Address for the Gelf backend
#[clap(long = "log-addr", env = "LOG_ADDR", required_if_eq("backend", "Gelf"))]
log_addr: Option<SocketAddr>,

/// Directory for the File backend
#[clap(long = "log-dir", env = "LOG_DIR", required_if_eq("backend", "File"))]
directory: Option<PathBuf>,

/// Prefix for the File backend
#[clap(long = "log-path", env = "LOG_PATH", required_if_eq("backend", "File"))]
prefix: Option<PathBuf>,

/// Backend type
#[clap(long = "log-backend", env = "LOG_BACKEND", value_enum)]
backend: Option<LoggerBackendType>,

#[clap(long = "log-format", env = "LOG_FORMAT")]
format: Option<String>,

#[clap(long = "log-level", env = "LOG_LEVEL")]
level: Option<String>,
}

#[derive(Parser, Debug, Clone)]

Check warning on line 62 in nodes/nomos-node/src/config.rs

View check run for this annotation

Codecov / codecov/patch

nodes/nomos-node/src/config.rs#L62

Added line #L62 was not covered by tests
pub struct NetworkArgs {
#[clap(long = "net-host", env = "NET_HOST")]
host: Option<IpAddr>,

#[clap(long = "net-port", env = "NET_PORT")]
port: Option<usize>,

#[clap(long = "net-node-key", env = "NET_NODE_KEY")]
node_key: Option<String>,

#[clap(long = "net-initial-peers", env = "NET_INITIAL_PEERS")]
pub initial_peers: Option<Vec<Multiaddr>>,

Check warning on line 74 in nodes/nomos-node/src/config.rs

View check run for this annotation

Codecov / codecov/patch

nodes/nomos-node/src/config.rs#L74

Added line #L74 was not covered by tests
}

#[derive(Parser, Debug, Clone)]

Check warning on line 77 in nodes/nomos-node/src/config.rs

View check run for this annotation

Codecov / codecov/patch

nodes/nomos-node/src/config.rs#L77

Added line #L77 was not covered by tests
pub struct HttpArgs {
#[clap(long = "http-host", env = "HTTP_HOST")]
http_addr: Option<SocketAddr>,

#[clap(long = "http-cors-origin", env = "HTTP_CORS_ORIGIN")]
pub cors_origins: Option<Vec<String>>,

Check warning on line 83 in nodes/nomos-node/src/config.rs

View check run for this annotation

Codecov / codecov/patch

nodes/nomos-node/src/config.rs#L83

Added line #L83 was not covered by tests
}

#[derive(Parser, Debug, Clone)]

Check warning on line 86 in nodes/nomos-node/src/config.rs

View check run for this annotation

Codecov / codecov/patch

nodes/nomos-node/src/config.rs#L86

Added line #L86 was not covered by tests
pub struct ConsensusArgs {
#[clap(long = "consensus-priv-key", env = "CONSENSUS_PRIV_KEY")]
consensus_priv_key: Option<String>,

#[clap(long = "consensus-timeout-secs", env = "CONSENSUS_TIMEOUT_SECS")]
consensus_timeout_secs: Option<String>,
}

#[derive(ValueEnum, Clone, Debug, Default)]

Check warning on line 95 in nodes/nomos-node/src/config.rs

View check run for this annotation

Codecov / codecov/patch

nodes/nomos-node/src/config.rs#L95

Added line #L95 was not covered by tests
pub enum OverlayType {
#[default]
Flat,
Tree,
}

#[derive(Parser, Debug, Clone)]

Check warning on line 102 in nodes/nomos-node/src/config.rs

View check run for this annotation

Codecov / codecov/patch

nodes/nomos-node/src/config.rs#L102

Added line #L102 was not covered by tests
pub struct OverlayArgs {
// TODO: Act on type and support other overlays.
#[clap(long = "overlay-type", env = "OVERLAY_TYPE")]
pub overlay_type: Option<OverlayType>,

#[clap(long = "overlay-nodes", env = "OVERLAY_NODES")]
pub overlay_nodes: Option<Vec<String>>,

Check warning on line 109 in nodes/nomos-node/src/config.rs

View check run for this annotation

Codecov / codecov/patch

nodes/nomos-node/src/config.rs#L109

Added line #L109 was not covered by tests

#[clap(long = "overlay-leader", env = "OVERLAY_LEADER")]
pub overlay_leader: Option<usize>,

#[clap(
long = "overlay-leader-super-majority-threshold",
env = "OVERLAY_LEADER_SUPER_MAJORITY_THRESHOLD"
)]
pub overlay_leader_super_majority_threshold: Option<usize>,
}

#[derive(Deserialize, Debug, Clone, Serialize)]

Check warning on line 121 in nodes/nomos-node/src/config.rs

View check run for this annotation

Codecov / codecov/patch

nodes/nomos-node/src/config.rs#L121

Added line #L121 was not covered by tests
pub struct Config {
pub log: <Logger as ServiceData>::Settings,
#[cfg(feature = "waku")]
pub network: <NetworkService<Waku> as ServiceData>::Settings,
#[cfg(feature = "libp2p")]
pub network: <NetworkService<Libp2p> as ServiceData>::Settings,
pub http: <HttpService<AxumBackend> as ServiceData>::Settings,
pub consensus: <Carnot as ServiceData>::Settings,
#[cfg(feature = "metrics")]
pub metrics: <MetricsService<MapMetricsBackend<MetricsData>> as ServiceData>::Settings,
}

impl Config {
pub fn update_log(mut self, log_args: LogArgs) -> Result<Self> {
let LogArgs {
backend,
log_addr: addr,
directory,
prefix,
format,
level,
} = log_args;

Check warning on line 143 in nodes/nomos-node/src/config.rs

View check run for this annotation

Codecov / codecov/patch

nodes/nomos-node/src/config.rs#L135-L143

Added lines #L135 - L143 were not covered by tests

// Override the file config with the one from env variables.
if let Some(backend) = backend {
self.log.backend = match backend {

Check warning on line 147 in nodes/nomos-node/src/config.rs

View check run for this annotation

Codecov / codecov/patch

nodes/nomos-node/src/config.rs#L146-L147

Added lines #L146 - L147 were not covered by tests
LoggerBackendType::Gelf => LoggerBackend::Gelf {
addr: addr.ok_or_else(|| eyre!("Gelf backend requires an address."))?,

Check warning on line 149 in nodes/nomos-node/src/config.rs

View check run for this annotation

Codecov / codecov/patch

nodes/nomos-node/src/config.rs#L149

Added line #L149 was not covered by tests
},
LoggerBackendType::File => LoggerBackend::File {
directory: directory
.ok_or_else(|| eyre!("File backend requires a directory."))?,
prefix,

Check warning on line 154 in nodes/nomos-node/src/config.rs

View check run for this annotation

Codecov / codecov/patch

nodes/nomos-node/src/config.rs#L152-L154

Added lines #L152 - L154 were not covered by tests
},
LoggerBackendType::Stdout => LoggerBackend::Stdout,
LoggerBackendType::Stderr => LoggerBackend::Stderr,

Check warning on line 157 in nodes/nomos-node/src/config.rs

View check run for this annotation

Codecov / codecov/patch

nodes/nomos-node/src/config.rs#L156-L157

Added lines #L156 - L157 were not covered by tests
}
};

Check warning on line 159 in nodes/nomos-node/src/config.rs

View check run for this annotation

Codecov / codecov/patch

nodes/nomos-node/src/config.rs#L159

Added line #L159 was not covered by tests

// Update parts of the config.
if let Some(format_str) = format {
self.log.format = match format_str.as_str() {
"Json" => LoggerFormat::Json,
"Plain" => LoggerFormat::Plain,
_ => return Err(eyre!("Invalid log format provided.")),

Check warning on line 166 in nodes/nomos-node/src/config.rs

View check run for this annotation

Codecov / codecov/patch

nodes/nomos-node/src/config.rs#L162-L166

Added lines #L162 - L166 were not covered by tests
};
}
if let Some(level_str) = level {
self.log.level = match level_str.as_str() {
"DEBUG" => Level::DEBUG,
_ => return Err(eyre!("Invalid log level provided.")),

Check warning on line 172 in nodes/nomos-node/src/config.rs

View check run for this annotation

Codecov / codecov/patch

nodes/nomos-node/src/config.rs#L168-L172

Added lines #L168 - L172 were not covered by tests
};
}
Ok(self)
}

Check warning on line 176 in nodes/nomos-node/src/config.rs

View check run for this annotation

Codecov / codecov/patch

nodes/nomos-node/src/config.rs#L174-L176

Added lines #L174 - L176 were not covered by tests

#[cfg(feature = "waku")]
pub fn update_network(mut self, network_args: NetworkArgs) -> Result<Self> {
let NetworkArgs {
host,
port,
node_key,
initial_peers,
} = network_args;

Check warning on line 185 in nodes/nomos-node/src/config.rs

View check run for this annotation

Codecov / codecov/patch

nodes/nomos-node/src/config.rs#L179-L185

Added lines #L179 - L185 were not covered by tests

if let Some(host) = host {
self.network.backend.inner.host = Some(host);
}

Check warning on line 189 in nodes/nomos-node/src/config.rs

View check run for this annotation

Codecov / codecov/patch

nodes/nomos-node/src/config.rs#L187-L189

Added lines #L187 - L189 were not covered by tests

if let Some(port) = port {
self.network.backend.inner.port = Some(port);
}

Check warning on line 193 in nodes/nomos-node/src/config.rs

View check run for this annotation

Codecov / codecov/patch

nodes/nomos-node/src/config.rs#L191-L193

Added lines #L191 - L193 were not covered by tests

if let Some(node_key) = node_key {

Check warning on line 195 in nodes/nomos-node/src/config.rs

View check run for this annotation

Codecov / codecov/patch

nodes/nomos-node/src/config.rs#L195

Added line #L195 was not covered by tests
use std::str::FromStr;
self.network.backend.inner.node_key = Some(SecretKey::from_str(&node_key)?);
}

Check warning on line 198 in nodes/nomos-node/src/config.rs

View check run for this annotation

Codecov / codecov/patch

nodes/nomos-node/src/config.rs#L197-L198

Added lines #L197 - L198 were not covered by tests

if let Some(peers) = initial_peers {
self.network.backend.initial_peers = peers;
}

Check warning on line 202 in nodes/nomos-node/src/config.rs

View check run for this annotation

Codecov / codecov/patch

nodes/nomos-node/src/config.rs#L200-L202

Added lines #L200 - L202 were not covered by tests

Ok(self)
}

Check warning on line 205 in nodes/nomos-node/src/config.rs

View check run for this annotation

Codecov / codecov/patch

nodes/nomos-node/src/config.rs#L204-L205

Added lines #L204 - L205 were not covered by tests

#[cfg(feature = "libp2p")]
pub fn update_network(mut self, network_args: NetworkArgs) -> Result<Self> {
let NetworkArgs {
host,
port,
node_key,
initial_peers,
} = network_args;

Check warning on line 214 in nodes/nomos-node/src/config.rs

View check run for this annotation

Codecov / codecov/patch

nodes/nomos-node/src/config.rs#L208-L214

Added lines #L208 - L214 were not covered by tests

if let Some(IpAddr::V4(h)) = host {
self.network.backend.host = h;
} else if host.is_some() {
return Err(eyre!("Unsupported ip version"));
}

Check warning on line 220 in nodes/nomos-node/src/config.rs

View check run for this annotation

Codecov / codecov/patch

nodes/nomos-node/src/config.rs#L216-L220

Added lines #L216 - L220 were not covered by tests

if let Some(port) = port {
self.network.backend.port = port as u16;
}

Check warning on line 224 in nodes/nomos-node/src/config.rs

View check run for this annotation

Codecov / codecov/patch

nodes/nomos-node/src/config.rs#L222-L224

Added lines #L222 - L224 were not covered by tests

if let Some(node_key) = node_key {
let mut key_bytes = hex::decode(node_key)?;
self.network.backend.node_key = SecretKey::try_from_bytes(key_bytes.as_mut_slice())?;
}

Check warning on line 229 in nodes/nomos-node/src/config.rs

View check run for this annotation

Codecov / codecov/patch

nodes/nomos-node/src/config.rs#L226-L229

Added lines #L226 - L229 were not covered by tests

if let Some(peers) = initial_peers {
self.network.backend.initial_peers = peers;
}

Check warning on line 233 in nodes/nomos-node/src/config.rs

View check run for this annotation

Codecov / codecov/patch

nodes/nomos-node/src/config.rs#L231-L233

Added lines #L231 - L233 were not covered by tests

Ok(self)
}

Check warning on line 236 in nodes/nomos-node/src/config.rs

View check run for this annotation

Codecov / codecov/patch

nodes/nomos-node/src/config.rs#L235-L236

Added lines #L235 - L236 were not covered by tests

pub fn update_http(mut self, http_args: HttpArgs) -> Result<Self> {
let HttpArgs {
http_addr,
cors_origins,
} = http_args;

Check warning on line 242 in nodes/nomos-node/src/config.rs

View check run for this annotation

Codecov / codecov/patch

nodes/nomos-node/src/config.rs#L238-L242

Added lines #L238 - L242 were not covered by tests

if let Some(addr) = http_addr {
self.http.backend.address = addr;
}

Check warning on line 246 in nodes/nomos-node/src/config.rs

View check run for this annotation

Codecov / codecov/patch

nodes/nomos-node/src/config.rs#L244-L246

Added lines #L244 - L246 were not covered by tests

if let Some(cors) = cors_origins {
self.http.backend.cors_origins = cors;
}

Check warning on line 250 in nodes/nomos-node/src/config.rs

View check run for this annotation

Codecov / codecov/patch

nodes/nomos-node/src/config.rs#L248-L250

Added lines #L248 - L250 were not covered by tests

Ok(self)
}

Check warning on line 253 in nodes/nomos-node/src/config.rs

View check run for this annotation

Codecov / codecov/patch

nodes/nomos-node/src/config.rs#L252-L253

Added lines #L252 - L253 were not covered by tests

pub fn update_consensus(mut self, consensus_args: ConsensusArgs) -> Result<Self> {
let ConsensusArgs {
consensus_priv_key,
consensus_timeout_secs,
} = consensus_args;

Check warning on line 259 in nodes/nomos-node/src/config.rs

View check run for this annotation

Codecov / codecov/patch

nodes/nomos-node/src/config.rs#L255-L259

Added lines #L255 - L259 were not covered by tests

if let Some(private_key) = consensus_priv_key {
let bytes = <[u8; 32]>::from_hex(private_key)?;
self.consensus.private_key = bytes;
}

Check warning on line 264 in nodes/nomos-node/src/config.rs

View check run for this annotation

Codecov / codecov/patch

nodes/nomos-node/src/config.rs#L261-L264

Added lines #L261 - L264 were not covered by tests

if let Some(timeout) = consensus_timeout_secs {
let secs = timeout.parse::<u64>()?;
self.consensus.timeout = Duration::from_secs(secs);
}

Check warning on line 269 in nodes/nomos-node/src/config.rs

View check run for this annotation

Codecov / codecov/patch

nodes/nomos-node/src/config.rs#L266-L269

Added lines #L266 - L269 were not covered by tests

Ok(self)
}

Check warning on line 272 in nodes/nomos-node/src/config.rs

View check run for this annotation

Codecov / codecov/patch

nodes/nomos-node/src/config.rs#L271-L272

Added lines #L271 - L272 were not covered by tests

pub fn update_overlay(mut self, overlay_args: OverlayArgs) -> Result<Self> {
let OverlayArgs {
overlay_nodes,
overlay_leader_super_majority_threshold,
..
} = overlay_args;

Check warning on line 279 in nodes/nomos-node/src/config.rs

View check run for this annotation

Codecov / codecov/patch

nodes/nomos-node/src/config.rs#L274-L279

Added lines #L274 - L279 were not covered by tests

if let Some(nodes) = overlay_nodes {
self.consensus.overlay_settings.nodes = nodes
.iter()
.map(|n| {
<[u8; 32]>::from_hex(n)
.map_err(|e| eyre::eyre!("Failed to decode hex: {}", e))
.map(|b| b.into())
})
.collect::<Result<Vec<_>, eyre::Report>>()?;
}

Check warning on line 290 in nodes/nomos-node/src/config.rs

View check run for this annotation

Codecov / codecov/patch

nodes/nomos-node/src/config.rs#L281-L290

Added lines #L281 - L290 were not covered by tests

if let Some(threshold) = overlay_leader_super_majority_threshold {
self.consensus
.overlay_settings
.leader_super_majority_threshold = Some(threshold.into());
}

Check warning on line 296 in nodes/nomos-node/src/config.rs

View check run for this annotation

Codecov / codecov/patch

nodes/nomos-node/src/config.rs#L292-L296

Added lines #L292 - L296 were not covered by tests

Ok(self)
}

Check warning on line 299 in nodes/nomos-node/src/config.rs

View check run for this annotation

Codecov / codecov/patch

nodes/nomos-node/src/config.rs#L298-L299

Added lines #L298 - L299 were not covered by tests
}
41 changes: 3 additions & 38 deletions nodes/nomos-node/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod blob;
mod config;
mod tx;

use color_eyre::eyre::Result;
Expand All @@ -14,8 +15,6 @@ use nomos_core::fountain::mock::MockFountain;
use nomos_http::backends::axum::AxumBackend;
use nomos_http::bridge::HttpBridgeService;
use nomos_http::http::HttpService;
#[cfg(feature = "libp2p")]
use nomos_libp2p::secp256k1::SecretKey;
use nomos_log::Logger;
#[cfg(feature = "libp2p")]
use nomos_mempool::network::adapters::libp2p::Libp2pAdapter as MempoolLibp2pAdapter;
Expand All @@ -28,49 +27,15 @@ use nomos_network::backends::libp2p::Libp2p;
use nomos_network::backends::waku::Waku;
use nomos_network::NetworkService;
use overwatch_derive::*;
use overwatch_rs::services::{handle::ServiceHandle, ServiceData};
use serde::{Deserialize, Serialize};
#[cfg(feature = "waku")]
use waku_bindings::SecretKey;
use overwatch_rs::services::handle::ServiceHandle;

use crate::blob::Blob;
pub use config::{Config, ConsensusArgs, HttpArgs, LogArgs, NetworkArgs, OverlayArgs};
pub use tx::Tx;

#[cfg(all(feature = "waku", feature = "libp2p"))]
compile_error!("feature \"waku\" and feature \"libp2p\" cannot be enabled at the same time");

#[derive(Deserialize, Debug, Clone, Serialize)]
pub struct Config {
pub log: <Logger as ServiceData>::Settings,
#[cfg(feature = "waku")]
pub network: <NetworkService<Waku> as ServiceData>::Settings,
#[cfg(feature = "libp2p")]
pub network: <NetworkService<Libp2p> as ServiceData>::Settings,
pub http: <HttpService<AxumBackend> as ServiceData>::Settings,
pub consensus: <Carnot as ServiceData>::Settings,
#[cfg(feature = "metrics")]
pub metrics: <MetricsService<MapMetricsBackend<MetricsData>> as ServiceData>::Settings,
}

impl Config {
pub fn set_node_key(&mut self, node_key: Option<String>) -> Result<()> {
if let Some(node_key) = node_key {
#[cfg(feature = "waku")]
{
use std::str::FromStr;
self.network.backend.inner.node_key = Some(SecretKey::from_str(&node_key)?)
}
#[cfg(feature = "libp2p")]
{
let mut key_bytes = hex::decode(node_key)?;
self.network.backend.node_key =
SecretKey::try_from_bytes(key_bytes.as_mut_slice())?;
}
}
Ok(())
}
}

#[cfg(feature = "waku")]
pub type Carnot = CarnotConsensus<
ConsensusWakuAdapter,
Expand Down
Loading
Loading