diff --git a/modules/graph_trace_helper.py b/modules/graph_trace_helper.py index 8c04ed6..3d4f73a 100644 --- a/modules/graph_trace_helper.py +++ b/modules/graph_trace_helper.py @@ -100,6 +100,10 @@ def update_mesh_graph_from_trace_data( if is_immediate_neighbor: neighbor_prefix = path_hashes[0].lower() + if neighbor_prefix == bot_prefix: + # The graph, keyed by prefix, can't tell this neighbor from the bot + bot.logger.debug(f"Mesh graph: Neighbor {neighbor_prefix} shares the bot's prefix, skipping trace update") + return neighbor_key = None try: count_query = f""" @@ -208,9 +212,12 @@ def update_mesh_graph_from_trace_data( geographic_distance = None from_location, to_location = locations[i], locations[i + 1] if from_location and to_location: - geographic_distance = calculate_distance( - from_location[0], from_location[1], to_location[0], to_location[1], - ) + try: + geographic_distance = calculate_distance( + from_location[0], from_location[1], to_location[0], to_location[1], + ) + except Exception as e: + bot.logger.debug(f"Could not calculate distance for trace edge {from_node}->{to_node}: {e}") mesh_graph.add_edge( from_prefix=from_node, to_prefix=to_node, diff --git a/tests/test_graph_trace_helper.py b/tests/test_graph_trace_helper.py index 615ed45..f1669c8 100644 --- a/tests/test_graph_trace_helper.py +++ b/tests/test_graph_trace_helper.py @@ -377,3 +377,19 @@ class TestOwnTraceChain: assert [(c["from_prefix"], c["to_prefix"]) for c in calls] == [("dd", "aa"), ("aa", "dd")] assert calls[0]["from_public_key"] == "dd" * 32 assert calls[1]["to_public_key"] is None # the hop "dd", not the bot + + + def test_a_neighbor_sharing_the_bots_prefix_adds_no_self_loop(self): + bot = _make_bot(bot_prefix="dd") + update_mesh_graph_from_trace_data(bot, ["dd"], {}, is_our_trace=True) + bot.mesh_graph.add_edge.assert_not_called() + + def test_a_bad_stored_location_still_records_the_edges(self, monkeypatch): + import modules.utils as utils + + bot = _make_bot(bot_prefix="dd") + monkeypatch.setattr(utils, "_get_node_location_from_db", lambda *a, **k: ((float("inf"), 0.0), None)) + monkeypatch.setattr(utils, "calculate_distance", Mock(side_effect=ValueError("math domain error"))) + update_mesh_graph_from_trace_data(bot, ["aa", "bb"], {}) + assert bot.mesh_graph.add_edge.call_count == 2 + assert all(c.kwargs["geographic_distance"] is None for c in bot.mesh_graph.add_edge.call_args_list)