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

Handle unique validation when parachain id is same #102

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
51 changes: 43 additions & 8 deletions crates/configuration/src/parachain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use serde::{
use crate::{
shared::{
errors::{ConfigError, FieldError},
helpers::{merge_errors, merge_errors_vecs},
helpers::{ensure_parachain_id_unique, merge_errors, merge_errors_vecs},
macros::states,
node::{self, NodeConfig, NodeConfigBuilder},
resources::{Resources, ResourcesBuilder},
Expand Down Expand Up @@ -298,13 +298,19 @@ impl ParachainConfigBuilder<Initial> {
}

/// Set the parachain ID (should be unique).
// TODO: handle unique validation
pub fn with_id(self, id: u32) -> ParachainConfigBuilder<WithId> {
Self::transition(
ParachainConfig { id, ..self.config },
self.validation_context,
self.errors,
)
match ensure_parachain_id_unique(id, self.validation_context.clone()) {
Ok(_) => Self::transition(
ParachainConfig { id, ..self.config },
self.validation_context,
self.errors,
),
Err(error) => Self::transition(
self.config,
self.validation_context,
merge_errors(self.errors, error),
),
}
}
}

Expand Down Expand Up @@ -677,7 +683,7 @@ impl ParachainConfigBuilder<WithAtLeastOneCollator> {
#[cfg(test)]
mod tests {
use super::*;
use crate::NetworkConfig;
use crate::{NetworkConfig, NetworkConfigBuilder};

#[test]
fn parachain_config_builder_should_succeeds_and_returns_a_new_parachain_config() {
Expand Down Expand Up @@ -1062,6 +1068,35 @@ mod tests {
);
}

#[test]
#[should_panic(expected = "'1000' is already used across config")]
fn parachains_with_same_id_should_fail() {
let network = NetworkConfigBuilder::new()
.with_relaychain(|relaychain| {
relaychain
.with_chain("polkadot")
.with_node(|node| node.with_name("alice"))
})
.with_parachain(|parachain| {
parachain
.with_id(1000)
.with_collator(|collator| collator.with_name("charles"))
})
.with_parachain(|parachain| {
parachain
.with_id(1000)
.with_collator(|collator| collator.with_name("jim"))
})
.build()
.unwrap();

network.parachains().iter().for_each(|parachain| {
if parachain.id() != 1000 {
assert_eq!(parachain.id(), 1001);
}
});
}

#[test]
fn import_toml_registration_strategy_should_deserialize() {
let load_from_toml =
Expand Down
3 changes: 3 additions & 0 deletions crates/configuration/src/shared/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,7 @@ pub enum ValidationError {

#[error("'{0}' is already used across config")]
NodeNameAlreadyUsed(String),

#[error("'{0}' is already used across config")]
ParachainIdAlreadyUsed(ParaId),
}
18 changes: 17 additions & 1 deletion crates/configuration/src/shared/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{cell::RefCell, rc::Rc};
use super::{
constants::{BORROWABLE, THIS_IS_A_BUG},
errors::ValidationError,
types::{Port, ValidationContext},
types::{ParaId, Port, ValidationContext},
};

pub fn merge_errors(errors: Vec<anyhow::Error>, new_error: anyhow::Error) -> Vec<anyhow::Error> {
Expand Down Expand Up @@ -57,3 +57,19 @@ pub fn ensure_port_unique(

Err(ValidationError::PortAlreadyUsed(port).into())
}

pub fn ensure_parachain_id_unique(
id: ParaId,
validation_context: Rc<RefCell<ValidationContext>>,
) -> Result<(), anyhow::Error> {
let mut context = validation_context
.try_borrow_mut()
.expect(&format!("{}, {}", BORROWABLE, THIS_IS_A_BUG));

if !context.used_parachain_ids.contains(&id) {
context.used_parachain_ids.push(id);
return Ok(());
}

Err(ValidationError::ParachainIdAlreadyUsed(id).into())
}
1 change: 1 addition & 0 deletions crates/configuration/src/shared/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ impl<'de> Deserialize<'de> for Arg {
pub struct ValidationContext {
pub used_ports: Vec<Port>,
pub used_nodes_names: Vec<String>,
pub used_parachain_ids: Vec<ParaId>,
}

#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
Expand Down
Loading