Skip to content
This repository has been archived by the owner on Oct 31, 2024. It is now read-only.

Commit

Permalink
chore: update after review
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Paitrault <[email protected]>
  • Loading branch information
Freyskeyd committed Jan 31, 2024
1 parent e5c7467 commit a917c03
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 37 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 16 additions & 16 deletions crates/topos-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,29 @@ pub trait Config: Serialize {

/// Convert the configuration to a TOML table.
fn to_toml(&self) -> Result<toml::Table, toml::ser::Error> {
toml::Table::try_from(self)
let mut config_toml = toml::Table::new();

let config = toml::Table::try_from(self)?;

// Flatten the top level
for (profile, content) in config {
config_toml.insert(profile, content);
}

Ok(config_toml)
}

/// Main function to load the configuration.
/// It will load the configuration from the file and the command line (if any)
/// It will load the configuration from the file and an optional existing struct (if any)
/// and then extract the configuration from the context in order to build the Config.
/// The Config is then returned or an error if the configuration is not valid.
fn load<S: Serialize>(home: &Path, command: Option<S>) -> Result<Self::Output, figment::Error> {
fn load<S: Serialize>(home: &Path, config: Option<S>) -> Result<Self::Output, figment::Error> {
let mut figment = Figment::new();

figment = Self::load_from_file(figment, home);

if let Some(command) = command {
figment = figment.merge(Serialized::from(command, Self::profile()))
if let Some(config) = config {
figment = figment.merge(Serialized::from(config, Self::profile()))
}

Self::load_context(figment)
Expand All @@ -52,9 +61,9 @@ pub trait Config: Serialize {

pub(crate) fn load_config<T: Config, S: Serialize>(
node_path: &Path,
command: Option<S>,
config: Option<S>,
) -> T::Output {
match T::load(node_path, command) {
match T::load(node_path, config) {
Ok(config) => config,
Err(figment::Error {
kind: Kind::MissingField(name),
Expand All @@ -69,12 +78,3 @@ pub(crate) fn load_config<T: Config, S: Serialize>(
}
}
}

pub fn insert_into_toml<T: Config>(config_toml: &mut toml::Table, config: T) {
let full = config.to_toml().expect("failed to convert config to toml");

// Flatten the top level
for (profile, content) in full {
config_toml.insert(profile, content);
}
}
13 changes: 5 additions & 8 deletions crates/topos-config/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ pub enum NodeRole {
FullNode,
}

#[derive(Serialize)]
struct Noop {}

#[derive(Serialize, Deserialize, Debug)]
pub struct NodeConfig {
pub base: BaseConfig,
Expand All @@ -32,20 +29,20 @@ pub struct NodeConfig {
}

impl NodeConfig {
pub fn new<T: Serialize>(home: &Path, cmd: Option<T>) -> Self {
let base = load_config::<BaseConfig, _>(home, cmd);
pub fn new<S: Serialize>(home: &Path, config: Option<S>) -> Self {
let base = load_config::<BaseConfig, _>(home, config);

let mut config = NodeConfig {
base: base.clone(),
sequencer: base
.need_sequencer()
.then(|| load_config::<SequencerConfig, Noop>(home, None)),
.then(|| load_config::<SequencerConfig, ()>(home, None)),
tce: base
.need_tce()
.then(|| load_config::<TceConfig, Noop>(home, None)),
.then(|| load_config::<TceConfig, ()>(home, None)),
edge: base
.need_edge()
.then(|| load_config::<EdgeConfig, Noop>(home, None)),
.then(|| load_config::<EdgeConfig, ()>(home, None)),
};

// Make the TCE DB path relative to the folder
Expand Down
3 changes: 1 addition & 2 deletions crates/topos-config/src/tce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ pub struct TceConfig {
pub validators: HashSet<ValidatorId>,
#[serde(skip)]
pub storage: StorageConfiguration,
#[serde(skip)]
pub minimum_cluster_size: usize,

#[serde(skip)]
pub version: &'static str,

Expand Down
2 changes: 1 addition & 1 deletion crates/topos-tce/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ pub async fn run(
let (network_client, event_stream, unbootstrapped_runtime) = topos_p2p::network::builder()
.peer_key(key)
.listen_addresses(config.p2p.listen_addresses.clone())
.minimum_cluster_size(config.minimum_cluster_size)
.minimum_cluster_size(config.minimum_tce_cluster_size)
.public_addresses(config.p2p.public_addresses.clone())
.known_peers(&boot_peers)
.grpc_context(grpc_context)
Expand Down
3 changes: 1 addition & 2 deletions crates/topos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,18 @@ rand.workspace = true
reqwest.workspace = true
thiserror.workspace = true
opentelemetry-otlp = { workspace = true, features = ["grpc-tonic", "metrics", "tls-roots"] }
figment = { version = "0.10", features = ["yaml", "toml", "env"] }
dirs = "5.0"
tracing-log = { version = "0.1.3", features = ["env_logger"] }
tar = "0.4.38"
flate2 ="1.0.26"
url = "2.3.1"
once_cell = "1.17.1"
toml = "0.7.4"
regex = "1"
rlp = "0.5.1"
openssl = { version = "0.10.61", features = ["vendored"] }

[dev-dependencies]
toml = "0.7.4"
topos-tce-broadcast = { path = "../topos-tce-broadcast" }
topos-tce-transport = { path = "../topos-tce-transport" }
topos-tce-synchronizer = { path = "../topos-tce-synchronizer" }
Expand Down
18 changes: 11 additions & 7 deletions crates/topos/src/components/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use tracing_opentelemetry::OpenTelemetrySpanExt;
use self::commands::{NodeCommand, NodeCommands};
use crate::edge::BINARY_NAME;
use crate::tracing::setup_tracing;
use topos_config::genesis::Genesis;
use topos_config::{insert_into_toml, node::NodeConfig, node::NodeRole};
use topos_config::{genesis::Genesis, Config};
use topos_config::{node::NodeConfig, node::NodeRole};
use topos_core::api::grpc::tce::v1::console_service_client::ConsoleServiceClient;
use topos_wallet::SecretManager;

Expand Down Expand Up @@ -70,9 +70,6 @@ pub(crate) async fn handle_command(
std::process::exit(1);
}

// Generate the configuration as per the role
let mut config_toml = toml::Table::new();

if cmd.no_edge_process {
println!("Init the node without polygon-edge process...");
} else {
Expand Down Expand Up @@ -111,7 +108,14 @@ pub(crate) async fn handle_command(
let node_config = NodeConfig::new(&node_path, Some(cmd));

// Creating the TOML output
insert_into_toml(&mut config_toml, node_config);
let config_toml = match node_config.to_toml() {
Ok(config) => config,
Err(error) => {
println!("Failed to generate TOML config: {error}");
remove_dir_all(node_path).expect("failed to remove config folder");
std::process::exit(1);
}
};

let config_path = node_path.join("config.toml");
let mut node_config_file = OpenOptions::new()
Expand All @@ -122,7 +126,7 @@ pub(crate) async fn handle_command(
.expect("failed to create default node file");

node_config_file
.write_all(toml::to_string(&config_toml).unwrap().as_bytes())
.write_all(config_toml.to_string().as_bytes())
.expect("failed to write to default node file");

println!(
Expand Down

0 comments on commit a917c03

Please sign in to comment.