Skip to content

Commit

Permalink
sim-rs: fix embarrassing bug where nodes peered to themselves
Browse files Browse the repository at this point in the history
  • Loading branch information
SupernaviX committed Dec 19, 2024
1 parent b73d6d6 commit afc4b17
Show file tree
Hide file tree
Showing 6 changed files with 13,685 additions and 12,829 deletions.
5 changes: 4 additions & 1 deletion sim-rs/sim-cli/src/bin/gen-test-data/strategy/globe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ pub fn globe(args: &GlobeArgs) -> Result<(Vec<RawNodeConfig>, Vec<RawLinkConfig>
};

for to in first_candidate_connection..args.node_count {
if from == to {
continue;
}
// nodes are connected probabilistically, based on how far apart they are
let dist = distance(nodes[from].location, nodes[to].location);
let probability = alpha * (-dist / (beta * max_distance)).exp();
Expand All @@ -135,7 +138,7 @@ pub fn globe(args: &GlobeArgs) -> Result<(Vec<RawNodeConfig>, Vec<RawLinkConfig>
let candidate_targets: Vec<usize> = if from < args.stake_pool_count {
(args.stake_pool_count..args.node_count).collect()
} else {
(0..args.node_count).filter(|&to| to == from).collect()
(0..args.node_count).filter(|&to| to != from).collect()
};
let to = candidate_targets.choose(&mut rng).cloned().unwrap();
links.add(from, to, None);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ pub fn random_graph(args: &RandomGraphArgs) -> Result<(Vec<RawNodeConfig>, Vec<R
};

for to in first_candidate_connection..args.node_count {
if from == to {
continue;
}
// nodes are connected probabilistically, based on how far apart they are
let dist = distance(nodes[from].location, nodes[to].location);
let probability = alpha * (-dist / (beta * max_distance)).exp();
Expand All @@ -69,7 +72,7 @@ pub fn random_graph(args: &RandomGraphArgs) -> Result<(Vec<RawNodeConfig>, Vec<R
let candidate_targets: Vec<usize> = if from < args.stake_pool_count {
(args.stake_pool_count..args.node_count).collect()
} else {
(0..args.node_count).filter(|&to| to == from).collect()
(0..args.node_count).filter(|&to| to != from).collect()
};
let to = candidate_targets.choose(&mut rng).cloned().unwrap();
links.add(from, to, None);
Expand Down
10 changes: 10 additions & 0 deletions sim-rs/sim-core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ impl SimConfiguration {
// The graph must be nonempty and fully connected,
// and every link must be between two nodes which exist
let mut connected_nodes = HashSet::new();
let mut self_connected_nodes = vec![];
let mut frontier = VecDeque::new();
let first_node = self
.nodes
Expand All @@ -178,6 +179,9 @@ impl SimConfiguration {
while let Some(node) = frontier.pop_front() {
if connected_nodes.insert(node.id) {
for peer_id in &node.peers {
if node.id == *peer_id {
self_connected_nodes.push(node.id);
}
let peer = self
.nodes
.get(peer_id.0)
Expand All @@ -186,6 +190,12 @@ impl SimConfiguration {
}
}
}
if !self_connected_nodes.is_empty() {
bail!(
"{} node(s) are connected to themselves!",
self_connected_nodes.len()
);
}
if connected_nodes.len() < self.nodes.len() {
bail!("Graph must be fully connected!");
}
Expand Down
Loading

0 comments on commit afc4b17

Please sign in to comment.