Skip to content

Commit d5b7ed4

Browse files
author
Andres Correa Casablanca
committed
Extract code to graphs module
Signed-off-by: Andres Correa Casablanca <[email protected]>
1 parent bcfed76 commit d5b7ed4

File tree

3 files changed

+67
-20
lines changed

3 files changed

+67
-20
lines changed

experiments/forking_simulation.py

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,7 @@
6565
import test_framework.util as tf_util
6666

6767
from experiments.graph import (
68-
enforce_nodes_reconnections,
69-
ensure_one_inbound_connection_per_node,
70-
create_directed_graph,
68+
create_network_graph,
7169
create_simple_dense_graph
7270
)
7371
from network.latencies import StaticLatencyPolicy
@@ -505,26 +503,11 @@ def define_network_topology(self, graph_model: str):
505503
"""This function defines the network's topology"""
506504

507505
self.logger.info('Defining network graph')
508-
509-
graph_edges, inbound_degrees = create_directed_graph(
506+
self.graph_edges = create_network_graph(
510507
num_nodes=self.num_nodes,
511508
num_outbound_connections=NUM_OUTBOUND_CONNECTIONS,
512509
max_inbound_connections=NUM_INBOUND_CONNECTIONS,
513-
model=graph_model
514-
)
515-
516-
# We try to avoid having sink sub-graphs
517-
graph_edges, inbound_degrees = enforce_nodes_reconnections(
518-
graph_edges=graph_edges,
519-
inbound_degrees=inbound_degrees,
520-
num_reconnection_rounds=1,
521-
)
522-
523-
# This fix the rare case where some nodes don't have inbound connections
524-
self.graph_edges, _ = ensure_one_inbound_connection_per_node(
525-
num_nodes=self.num_nodes,
526-
graph_edges=graph_edges,
527-
inbound_degrees=inbound_degrees,
510+
graph_model=graph_model
528511
)
529512

530513

experiments/graph.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,41 @@ def create_simple_dense_graph(
213213
return directed_edges
214214

215215

216+
def create_network_graph(
217+
num_nodes: int,
218+
num_outbound_connections: int,
219+
max_inbound_connections: int,
220+
graph_model: str = 'preferential_attachment',
221+
) -> Set[Tuple[int, int]]:
222+
"""
223+
This function creates a graph ensuring that it holds some properties that
224+
makes it suitable to represent a Unit-e network's topology without isolated
225+
nor sink nodes.
226+
"""
227+
graph_edges, inbound_degrees = create_directed_graph(
228+
num_nodes=num_nodes,
229+
num_outbound_connections=num_outbound_connections,
230+
max_inbound_connections=max_inbound_connections,
231+
model=graph_model
232+
)
233+
234+
# We try to avoid having sink sub-graphs
235+
graph_edges, inbound_degrees = enforce_nodes_reconnections(
236+
graph_edges=graph_edges,
237+
inbound_degrees=inbound_degrees,
238+
num_reconnection_rounds=1,
239+
)
240+
241+
# This fix the rare case where some nodes don't have inbound connections
242+
graph_edges, _ = ensure_one_inbound_connection_per_node(
243+
num_nodes=num_nodes,
244+
graph_edges=graph_edges,
245+
inbound_degrees=inbound_degrees,
246+
)
247+
248+
return graph_edges
249+
250+
216251
def enforce_nodes_reconnections(
217252
graph_edges: Set[Tuple[int, int]],
218253
inbound_degrees: Dict[int, int],

test/experiments/test_graph.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
create_simple_dense_graph,
1414
create_static_graph,
1515
create_growing_graph,
16+
create_network_graph,
1617
create_preferential_attachment_graph,
1718
degrees_distribution,
1819
enforce_nodes_reconnections,
@@ -206,3 +207,31 @@ def test_create_simple_dense_graph():
206207
assert len({
207208
(i, j) for i, j in graph if i == node_id
208209
}) == min(len(node_ids) - 1, outbound_degree)
210+
211+
212+
def test_create_network_graph():
213+
for _ in range(10):
214+
# Execute multiple times to take into account the randomness effects.
215+
graph_edges = create_network_graph(
216+
num_nodes=100,
217+
num_outbound_connections=8,
218+
max_inbound_connections=125,
219+
graph_model='preferential_attachment'
220+
)
221+
222+
# First, we'll check that every node has 8 outbound connections
223+
outbound_peers_by_node = {
224+
src: {dst for _src, dst in graph_edges if _src == src}
225+
for src in {_src for _src, _dst in graph_edges}
226+
}
227+
for outbound_peers in outbound_peers_by_node.values():
228+
assert 8 == len(outbound_peers)
229+
230+
# Second, we'll check that every node hast at least 1 inbound
231+
# connection, and at most 125 inbound connections
232+
inbound_peers_by_node = {
233+
dst: {src for src, _dst in graph_edges if _dst == dst}
234+
for dst in {_dst for _src, _dst in graph_edges}
235+
}
236+
for inbound_peers in inbound_peers_by_node.values():
237+
assert 1 <= len(inbound_peers) <= 125

0 commit comments

Comments
 (0)