Skip to content

Commit

Permalink
Revert changes on ChainDefaultContext
Browse files Browse the repository at this point in the history
  • Loading branch information
wirednkod committed Sep 12, 2023
1 parent 712a314 commit 5e5c902
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 127 deletions.
4 changes: 2 additions & 2 deletions crates/configuration/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl NetworkConfig {
let file_str = fs::read_to_string(path).expect(&format!("{} {}", RW_FAILED, THIS_IS_A_BUG));
let re: Regex = Regex::new(r"(?<field_name>(initial_)?balance)\s+=\s+(?<u128_value>\d+)")
.expect(&format!("{} {}", VALID_REGEX, THIS_IS_A_BUG));
let mut network_config: NetworkConfig = toml::from_str(
let network_config: NetworkConfig = toml::from_str(
re.replace_all(&file_str, "$field_name = \"$u128_value\"")
.as_ref(),
)?;
Expand Down Expand Up @@ -142,7 +142,7 @@ impl NetworkConfig {

if node.args().is_empty() {
node.set_args(default_args.clone());
node.chain_context.set_default_args(default_args.clone())
node.chain_context.default_args = default_args.clone()
}
}

Expand Down
34 changes: 7 additions & 27 deletions crates/configuration/src/parachain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,33 +218,13 @@ impl<A> ParachainConfigBuilder<A> {
}

fn default_chain_context(&self) -> ChainDefaultContext {
let mut chain_default = ChainDefaultContext::default();
chain_default.set_default_command(
self.config
.default_command()
.cloned()
.expect("default command should be set"),
);
chain_default.set_default_image(
self.config
.default_image()
.cloned()
.expect("default image should be set"),
);
chain_default.set_default_resouces(
self.config
.default_resources
.clone()
.expect("default resources should be set"),
);
chain_default.set_default_db_snapshot(
self.config
.default_db_snapshot
.clone()
.expect("default db snapshot should be set"),
);
chain_default.set_default_args(self.config.default_args().into_iter().cloned().collect());
chain_default
ChainDefaultContext {
default_command: self.config.default_command.clone(),
default_image: self.config.default_image.clone(),
default_resources: self.config.default_resources.clone(),
default_db_snapshot: self.config.default_db_snapshot.clone(),
default_args: self.config.default_args().into_iter().cloned().collect(),
}
}
}

Expand Down
46 changes: 13 additions & 33 deletions crates/configuration/src/relaychain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ impl RelaychainConfig {
self.default_args.iter().collect::<Vec<&Arg>>()
}

// /// Set the default arguments that will be used to launch the node command.
// pub(crate) fn set_default_args(&mut self, args: Vec<Arg>) {
// self.default_args = args;
// }
/// Set the default arguments that will be used to launch the node command.
pub(crate) fn set_default_args(&mut self, args: Vec<Arg>) {
self.default_args = args;
}

/// The location of an pre-existing chain specification for the relay chain.
pub fn chain_spec_path(&self) -> Option<&AssetLocation> {
Expand All @@ -84,10 +84,10 @@ impl RelaychainConfig {
self.nodes.iter().collect::<Vec<&NodeConfig>>()
}

// /// The nodes of the relay chain.
// pub(crate) fn set_nodes(&mut self, nodes: Vec<NodeConfig>) {
// self.nodes = nodes;
// }
/// The nodes of the relay chain.
pub(crate) fn set_nodes(&mut self, nodes: Vec<NodeConfig>) {
self.nodes = nodes;
}
}

states! {
Expand Down Expand Up @@ -145,31 +145,11 @@ impl<A> RelaychainConfigBuilder<A> {

fn default_chain_context(&self) -> ChainDefaultContext {
let mut chain_default = ChainDefaultContext::default();
chain_default.set_default_command(
self.config
.default_command()
.cloned()
.expect("default command should be set"),
);
chain_default.set_default_image(
self.config
.default_image()
.cloned()
.expect("default image should be set"),
);
chain_default.set_default_resouces(
self.config
.default_resources
.clone()
.expect("default resources should be set"),
);
chain_default.set_default_db_snapshot(
self.config
.default_db_snapshot
.clone()
.expect("default db snapshot should be set"),
);
chain_default.set_default_args(self.config.default_args().into_iter().cloned().collect());
chain_default.default_command = self.config.default_command().cloned();
chain_default.default_image = self.config.default_image().cloned();
chain_default.default_resources = self.config.default_resources.clone();
chain_default.default_db_snapshot = self.config.default_db_snapshot.clone();
chain_default.default_args = self.config.default_args().into_iter().cloned().collect();
chain_default
}
}
Expand Down
20 changes: 10 additions & 10 deletions crates/configuration/src/shared/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,19 @@ impl Serialize for NodeConfig {
let mut state = serializer.serialize_struct("NodeConfig", 18)?;
state.serialize_field("name", &self.name)?;

if self.image == self.chain_context.default_image().cloned() {
if self.image == self.chain_context.default_image {
state.skip_field("image")?;
} else {
state.serialize_field("image", &self.image)?;
}

if self.command == self.chain_context.default_command().cloned() {
if self.command == self.chain_context.default_command {
state.skip_field("command")?;
} else {
state.serialize_field("command", &self.command)?;
}

if self.args.is_empty() || self.args() == self.chain_context.default_args() {
if self.args.is_empty() || self.args() == self.chain_context.default_args.iter().collect::<Vec<&Arg>>() {
state.skip_field("args")?;
} else {
state.serialize_field("args", &self.args)?;
Expand All @@ -127,7 +127,7 @@ impl Serialize for NodeConfig {
state.serialize_field("bootnodes_addresses", &self.bootnodes_addresses)?;
}

if self.resources == self.chain_context.default_resources().cloned() {
if self.resources == self.chain_context.default_resources {
state.skip_field("resources")?;
} else {
state.serialize_field("resources", &self.resources)?;
Expand All @@ -139,7 +139,7 @@ impl Serialize for NodeConfig {
state.serialize_field("p2p_port", &self.p2p_port)?;
state.serialize_field("p2p_cert_hash", &self.p2p_cert_hash)?;

if self.db_snapshot == self.chain_context.default_db_snapshot().cloned() {
if self.db_snapshot == self.chain_context.default_db_snapshot {
state.skip_field("db_snapshot")?;
} else {
state.serialize_field("db_snapshot", &self.db_snapshot)?;
Expand Down Expand Up @@ -342,11 +342,11 @@ impl NodeConfigBuilder<Initial> {
) -> Self {
Self::transition(
NodeConfig {
command: chain_context.default_command().cloned(),
image: chain_context.default_image().cloned(),
resources: chain_context.default_resources().cloned(),
db_snapshot: chain_context.default_db_snapshot().cloned(),
args: chain_context.default_args().into_iter().cloned().collect(),
command: chain_context.default_command.clone(),
image: chain_context.default_image.clone(),
resources: chain_context.default_resources.clone(),
db_snapshot: chain_context.default_db_snapshot.clone(),
args: chain_context.default_args.clone(),
chain_context,
..Self::default().config
},
Expand Down
58 changes: 5 additions & 53 deletions crates/configuration/src/shared/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,60 +364,12 @@ pub struct ValidationContext {

#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
pub struct ChainDefaultContext {
default_command: Option<Command>,
default_image: Option<Image>,
default_resources: Option<Resources>,
default_db_snapshot: Option<AssetLocation>,
pub(crate) default_command: Option<Command>,
pub(crate) default_image: Option<Image>,
pub(crate) default_resources: Option<Resources>,
pub(crate) default_db_snapshot: Option<AssetLocation>,
#[serde(default)]
default_args: Vec<Arg>,
}

impl ChainDefaultContext {
/// The default command used for nodes.
pub(crate) fn default_command(&self) -> Option<&Command> {
self.default_command.as_ref()
}

pub(crate) fn set_default_command(&mut self, command: Command) {
self.default_command = Some(command);
}

/// The default container image used for nodes.
pub(crate) fn default_image(&self) -> Option<&Image> {
self.default_image.as_ref()
}

pub(crate) fn set_default_image(&mut self, image: Image) {
self.default_image = Some(image);
}

/// The default resources limits used for nodes.
pub(crate) fn default_resources(&self) -> Option<&Resources> {
self.default_resources.as_ref()
}

pub(crate) fn set_default_resouces(&mut self, resources: Resources) {
self.default_resources = Some(resources);
}

/// The default database snapshot location that will be used for state.
pub(crate) fn default_db_snapshot(&self) -> Option<&AssetLocation> {
self.default_db_snapshot.as_ref()
}

pub(crate) fn set_default_db_snapshot(&mut self, db_snapshot: AssetLocation) {
self.default_db_snapshot = Some(db_snapshot);
}

/// The default arguments that will be used from the ChainDefaultContext
pub(crate) fn default_args(&self) -> Vec<&Arg> {
self.default_args.iter().collect::<Vec<&Arg>>()
}

/// Set the default arguments
pub(crate) fn set_default_args(&mut self, args: Vec<Arg>) {
self.default_args = args;
}
pub(crate) default_args: Vec<Arg>,
}

#[cfg(test)]
Expand Down
9 changes: 7 additions & 2 deletions crates/examples/examples/small_network.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use configuration::NetworkConfigBuilder;
use configuration::{NetworkConfigBuilder, ParachainConfigBuilder};

fn main() {
let config = NetworkConfigBuilder::new()
Expand All @@ -8,5 +8,10 @@ fn main() {
})
.build();

println!("{:?}", config.unwrap());
println!("{:?}", config.unwrap().parachains() );

for parachain in config.unwrap().parachains() {
let builder = ParachainConfigBuilder::new(Default::default());
println!("{:?}", parachain.chain_context().name() );
}
}

0 comments on commit 5e5c902

Please sign in to comment.