Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
wirednkod committed Sep 5, 2023
1 parent 678fae7 commit de20044
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 26 deletions.
2 changes: 1 addition & 1 deletion crates/configuration/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"
regex = { workspace = true }
lazy_static = { workspace = true }
multiaddr = { workspace = true }
url = { workspace = true }
url = { workspace = true, features = ["serde"] }
thiserror = { workspace = true }
anyhow = { workspace = true }
serde = { workspace = true, features = ["derive"] }
Expand Down
5 changes: 3 additions & 2 deletions crates/configuration/src/global_settings.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{error::Error, fmt::Display, net::IpAddr, str::FromStr};

use multiaddr::Multiaddr;
use serde::Serialize;
use serde::{Deserialize, Serialize};

use crate::shared::{
errors::{ConfigError, FieldError},
Expand All @@ -10,9 +10,10 @@ use crate::shared::{
};

/// Global settings applied to an entire network.
#[derive(Debug, Clone, PartialEq, Serialize)]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct GlobalSettings {
#[serde(skip_serializing_if = "std::vec::Vec::is_empty")]
#[serde(default)]
bootnodes_addresses: Vec<Multiaddr>,
// TODO: parse both case in zombienet node version to avoid renamed ?
#[serde(rename = "timeout")]
Expand Down
4 changes: 2 additions & 2 deletions crates/configuration/src/hrmp_channel.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::marker::PhantomData;

use serde::Serialize;
use serde::{Deserialize, Serialize};

use crate::shared::{macros::states, types::ParaId};

/// HRMP channel configuration, with fine-grained configuration options.
#[derive(Debug, Clone, PartialEq, Serialize)]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct HrmpChannelConfig {
sender: ParaId,
recipient: ParaId,
Expand Down
74 changes: 71 additions & 3 deletions crates/configuration/src/network.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{cell::RefCell, marker::PhantomData, rc::Rc};
use std::{cell::RefCell, fs, marker::PhantomData, rc::Rc};

use regex::Regex;
use serde::Serialize;
use serde::{Deserialize, Serialize};

use crate::{
global_settings::{GlobalSettings, GlobalSettingsBuilder},
Expand All @@ -12,14 +12,16 @@ use crate::{
};

/// A network configuration, composed of a relaychain, parachains and HRMP channels.
#[derive(Debug, Clone, PartialEq, Serialize)]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct NetworkConfig {
#[serde(rename = "settings")]
global_settings: GlobalSettings,
relaychain: Option<RelaychainConfig>,
#[serde(skip_serializing_if = "std::vec::Vec::is_empty")]
#[serde(default)]
parachains: Vec<ParachainConfig>,
#[serde(skip_serializing_if = "std::vec::Vec::is_empty")]
#[serde(default)]
hrmp_channels: Vec<HrmpChannelConfig>,
}

Expand Down Expand Up @@ -178,6 +180,61 @@ impl NetworkConfigBuilder<Initial> {
Self::default()
}

pub fn toml_to_network(self, path: &str) -> Result<String, toml::ser::Error> {
let file_str = fs::read_to_string(path).unwrap();
let toml: NetworkConfig = toml::from_str(&file_str).unwrap();
if toml.relaychain.is_some() {
NetworkConfigBuilder::new().with_relaychain(|relaychain| {
relaychain
.with_chain(toml.relaychain.unwrap().chain().as_str())
.with_default_command("polkadot")
});

// .with_relaychain(|relaychain| {
// relaychain
// .with_chain("polkadot")
// .with_random_nominators_count(10)
// .with_node(|node| {
// node.with_name("node")
// .with_command("command")
// .validator(true)
// })
// });

// |relaychain| {
// relaychain
// .with_chain(toml.relaychain.unwrap().chain().as_str())
// .with_default_command("polkadot")
// .with_default_image("docker.io/parity/polkadot:latest")
// .with_default_args(vec![("-name", "value").into(), "--flag".into()])
// .with_default_db_snapshot("https://storage.com/path/to/db_snapshot.tgz")
// .with_default_resources(|resources| {
// resources
// .with_request_cpu(100000)
// .with_request_memory("500M")
// .with_limit_cpu("10Gi")
// .with_limit_memory("4000M")
// })
// .with_node(|node| {
// node.with_name("alice")
// .with_initial_balance(1_000_000_000)
// .validator(true)
// .bootnode(true)
// .invulnerable(true)
// })
// });
println!("- relaychain ------- : {:?}", toml.relaychain);
}
if !toml.parachains.is_empty() {
println!("- parachains ------- : {:?}", toml.parachains);
}

if !toml.hrmp_channels.is_empty() {
println!("- hrmp_channels ------- : {:?}", toml.hrmp_channels);
}
Ok("done".to_string())
}

/// Set the relay chain using a nested [`RelaychainConfigBuilder`].
pub fn with_relaychain(
self,
Expand Down Expand Up @@ -852,4 +909,15 @@ mod tests {
fs::read_to_string("./testing/snapshots/0002-overridden-defaults.toml").unwrap();
assert_eq!(got, expected);
}

#[test]
fn dumb_test() {
// let file_str = fs::read_to_string("./testing/snapshots/0002-overridden-defaults.toml").unwrap();
// let converted_toml = toml::from_str::<toml::Value>(file_str.as_str()).unwrap();

let config = NetworkConfigBuilder::new()
.toml_to_network("./testing/snapshots/0000-small-network.toml")
.unwrap();
println!("{:?}", config);
}
}
6 changes: 3 additions & 3 deletions crates/configuration/src/parachain.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{cell::RefCell, error::Error, fmt::Display, marker::PhantomData, rc::Rc};

use multiaddr::Multiaddr;
use serde::{ser::SerializeStruct, Serialize};
use serde::{ser::SerializeStruct, Deserialize, Serialize};

use crate::shared::{
errors::{ConfigError, FieldError},
Expand All @@ -14,7 +14,7 @@ use crate::shared::{
},
};

#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Deserialize)]
pub enum RegistrationStrategy {
InGenesis,
UsingExtrinsic,
Expand All @@ -37,7 +37,7 @@ impl Serialize for RegistrationStrategy {
}

/// A parachain configuration, composed of collators and fine-grained configuration options.
#[derive(Debug, Clone, PartialEq, Serialize)]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ParachainConfig {
id: u32,
chain: Option<Chain>,
Expand Down
4 changes: 2 additions & 2 deletions crates/configuration/src/relaychain.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{cell::RefCell, error::Error, fmt::Debug, marker::PhantomData, rc::Rc};

use serde::Serialize;
use serde::{Deserialize, Serialize};

use crate::shared::{
errors::{ConfigError, FieldError},
Expand All @@ -12,7 +12,7 @@ use crate::shared::{
};

/// A relay chain configuration, composed of nodes and fine-grained configuration options.
#[derive(Debug, Clone, PartialEq, Serialize)]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct RelaychainConfig {
chain: Chain,
default_command: Option<Command>,
Expand Down
14 changes: 11 additions & 3 deletions crates/configuration/src/shared/node.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{cell::RefCell, error::Error, fmt::Display, marker::PhantomData, rc::Rc};

use multiaddr::Multiaddr;
use serde::{ser::SerializeStruct, Serialize};
use serde::{ser::SerializeStruct, Deserialize, Serialize};

use super::{
errors::FieldError,
Expand Down Expand Up @@ -33,7 +33,7 @@ use crate::shared::{
/// }
/// )
/// ```
#[derive(Debug, Clone, PartialEq, Serialize)]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct EnvVar {
/// The name of the environment variable.
pub name: String,
Expand All @@ -52,17 +52,24 @@ impl From<(&str, &str)> for EnvVar {
}

/// A node configuration, with fine-grained configuration options.
#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Deserialize)]
pub struct NodeConfig {
name: String,
image: Option<Image>,
command: Option<Command>,
#[serde(default)]
args: Vec<Arg>,
#[serde(default)]
is_validator: bool,
#[serde(default)]
is_invulnerable: bool,
#[serde(default)]
is_bootnode: bool,
#[serde(default)]
initial_balance: U128,
#[serde(default)]
env: Vec<EnvVar>,
#[serde(default)]
bootnodes_addresses: Vec<Multiaddr>,
resources: Option<Resources>,
ws_port: Option<Port>,
Expand All @@ -71,6 +78,7 @@ pub struct NodeConfig {
p2p_port: Option<Port>,
p2p_cert_hash: Option<String>,
db_snapshot: Option<AssetLocation>,
#[serde(default)]
// used to skip serialization of fields with defaults to avoid duplication
chain_context: ChainDefaultContext,
}
Expand Down
6 changes: 3 additions & 3 deletions crates/configuration/src/shared/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::error::Error;

use lazy_static::lazy_static;
use regex::Regex;
use serde::{ser::SerializeStruct, Serialize};
use serde::{ser::SerializeStruct, Deserialize, Serialize};

use super::{
errors::{ConversionError, FieldError},
Expand All @@ -28,7 +28,7 @@ use super::{
/// assert_eq!(quantity3.as_str(), "1Gi");
/// assert_eq!(quantity4.as_str(), "10000");
/// ```
#[derive(Debug, Clone, PartialEq, Serialize)]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ResourceQuantity(String);

impl ResourceQuantity {
Expand Down Expand Up @@ -64,7 +64,7 @@ impl From<u64> for ResourceQuantity {
}

/// Resources limits used in the context of podman/k8s.
#[derive(Debug, Default, Clone, PartialEq)]
#[derive(Debug, Default, Clone, PartialEq, Deserialize)]
pub struct Resources {
request_memory: Option<ResourceQuantity>,
request_cpu: Option<ResourceQuantity>,
Expand Down
Loading

0 comments on commit de20044

Please sign in to comment.