Skip to content

Commit

Permalink
add: check for duplicate node ids
Browse files Browse the repository at this point in the history
fixes #32
  • Loading branch information
0xB10C committed Feb 19, 2024
1 parent 6ae5ffa commit edeefca
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,22 @@ pub fn load_config() -> Result<Config, ConfigError> {
let mut networks: Vec<Network> = vec![];
for toml_network in toml_config.networks.iter() {
let mut nodes: Vec<BoxedSyncSendNode> = vec![];
let mut node_ids: Vec<u32> = vec![];
for toml_node in toml_network.nodes.iter() {
match parse_toml_node(toml_node) {
Ok(node) => nodes.push(node),
Ok(node) => {
if !node_ids.contains(&node.info().id) {
node_ids.push(node.info().id);
nodes.push(node);
} else {
error!(
"Duplicate node id {}: The node {} could not be loaded.",
node.info().id,
node.info()
);
return Err(ConfigError::DuplicateNodeId);
}
}
Err(e) => {
error!("Error while parsing a node configuration: {}", toml_node);
return Err(e);
Expand Down
3 changes: 3 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ pub enum ConfigError {
NoBtcdRpcAuth,
NoNetworks,
UnknownImplementation,
DuplicateNodeId,
TomlError(toml::de::Error),
ReadError(io::Error),
AddrError(AddrParseError),
Expand All @@ -124,6 +125,7 @@ impl fmt::Display for ConfigError {
ConfigError::NoBtcdRpcAuth => write!(f, "no values for rpc_user and rpc_password"),
ConfigError::NoNetworks => write!(f, "no networks defined in the configuration"),
ConfigError::UnknownImplementation => write!(f, "the node implementation defined in the config is not supported"),
ConfigError::DuplicateNodeId => write!(f, "a node id has been used multiple times in the same network"),
ConfigError::TomlError(e) => write!(f, "the TOML in the configuration file could not be parsed: {}", e),
ConfigError::ReadError(e) => write!(f, "the configuration file could not be read: {}", e),
ConfigError::AddrError(e) => write!(f, "the address could not be parsed: {}", e),
Expand All @@ -142,6 +144,7 @@ impl error::Error for ConfigError {
ConfigError::TomlError(ref e) => Some(e),
ConfigError::ReadError(ref e) => Some(e),
ConfigError::AddrError(ref e) => Some(e),
ConfigError::DuplicateNodeId => None,
}
}
}
Expand Down

0 comments on commit edeefca

Please sign in to comment.