Skip to content

Commit

Permalink
Consensus env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
bacv committed Sep 11, 2023
1 parent d3ce77d commit c987d42
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 5 deletions.
89 changes: 86 additions & 3 deletions nodes/nomos-node/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use std::{
net::{IpAddr, SocketAddr},
path::PathBuf,
time::Duration,
};

use crate::Carnot;
use clap::{Parser, ValueEnum};
use color_eyre::eyre::{eyre, Result};
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};
Expand Down Expand Up @@ -81,6 +83,41 @@ pub struct HttpArgs {
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,
Expand Down Expand Up @@ -200,11 +237,11 @@ impl Config {

pub fn update_http(mut self, http_args: HttpArgs) -> Result<Self> {
let HttpArgs {
http_addr: addr,
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) = addr {
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

Expand All @@ -214,4 +251,50 @@ impl Config {

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
}
2 changes: 1 addition & 1 deletion nodes/nomos-node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use overwatch_derive::*;
use overwatch_rs::services::handle::ServiceHandle;

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

#[cfg(all(feature = "waku", feature = "libp2p"))]
Expand Down
15 changes: 14 additions & 1 deletion nodes/nomos-node/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use nomos_node::{Config, HttpArgs, LogArgs, NetworkArgs, Nomos, NomosServiceSettings, Tx};
use nomos_node::{
Config, ConsensusArgs, HttpArgs, LogArgs, NetworkArgs, Nomos, NomosServiceSettings,
OverlayArgs, Tx,
};

mod bridges;

Expand Down Expand Up @@ -31,6 +34,12 @@ struct Args {
/// Overrides http config.
#[clap(flatten)]
http_args: HttpArgs,
/// Overrides consensus config.
#[clap(flatten)]
consensus_args: ConsensusArgs,
/// Overrides overlay config.
#[clap(flatten)]
overlay_args: OverlayArgs,
}

fn main() -> Result<()> {
Expand All @@ -39,10 +48,14 @@ fn main() -> Result<()> {
log_args,
http_args,
network_args,
consensus_args,
overlay_args,
} = Args::parse();
let config = serde_yaml::from_reader::<_, Config>(std::fs::File::open(config)?)?
.update_log(log_args)?
.update_http(http_args)?
.update_consensus(consensus_args)?
.update_overlay(overlay_args)?
.update_network(network_args)?;

Check warning on line 59 in nodes/nomos-node/src/main.rs

View check run for this annotation

Codecov / codecov/patch

nodes/nomos-node/src/main.rs#L46-L59

Added lines #L46 - L59 were not covered by tests

let bridges: Vec<HttpBridge> = vec![
Expand Down

0 comments on commit c987d42

Please sign in to comment.