From fbee0ba2ab3c0aa8ee3c18ceed64fba24cc3144f Mon Sep 17 00:00:00 2001 From: Mystical <125946525+mystical-prog@users.noreply.github.com> Date: Mon, 23 Jun 2025 01:00:46 +0530 Subject: [PATCH] added newsfragment --- newsfragments/690.feature.rst | 1 + .../pubsub/test_gossipsub_px_and_backoff.py | 50 +++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 newsfragments/690.feature.rst diff --git a/newsfragments/690.feature.rst b/newsfragments/690.feature.rst new file mode 100644 index 00000000..450ffdfd --- /dev/null +++ b/newsfragments/690.feature.rst @@ -0,0 +1 @@ +added peer exchange and backoff logic as part of Gossipsub v1.1 upgrade diff --git a/tests/core/pubsub/test_gossipsub_px_and_backoff.py b/tests/core/pubsub/test_gossipsub_px_and_backoff.py index a47aa3ce..8c623ebd 100644 --- a/tests/core/pubsub/test_gossipsub_px_and_backoff.py +++ b/tests/core/pubsub/test_gossipsub_px_and_backoff.py @@ -113,3 +113,53 @@ async def test_unsubscribe_backoff(): assert host_0.get_id() in gsub1.mesh[topic], ( "peer should be able to rejoin after backoff" ) + + +@pytest.mark.trio +async def test_peer_exchange(): + async with PubsubFactory.create_batch_with_gossipsub( + 3, + heartbeat_interval=0.5, + do_px=True, + px_peers_count=1, + ) as pubsubs: + gsub0 = pubsubs[0].router + gsub1 = pubsubs[1].router + gsub2 = pubsubs[2].router + assert isinstance(gsub0, GossipSub) + assert isinstance(gsub1, GossipSub) + assert isinstance(gsub2, GossipSub) + host_0 = pubsubs[0].host + host_1 = pubsubs[1].host + host_2 = pubsubs[2].host + + topic = "test_peer_exchange" + + # connect hosts + await connect(host_1, host_0) + await connect(host_1, host_2) + await trio.sleep(0.5) + + # all join the topic and 0 <-> 1 and 1 <-> 2 graft + await pubsubs[1].subscribe(topic) + await pubsubs[0].subscribe(topic) + await pubsubs[2].subscribe(topic) + await gsub1.emit_graft(topic, host_0.get_id()) + await gsub1.emit_graft(topic, host_2.get_id()) + await gsub0.emit_graft(topic, host_1.get_id()) + await gsub2.emit_graft(topic, host_1.get_id()) + await trio.sleep(1) + + # ensure peer is registered in mesh + assert host_0.get_id() in gsub1.mesh[topic] + assert host_2.get_id() in gsub1.mesh[topic] + assert host_2.get_id() not in gsub0.mesh[topic] + + # host_1 unsubscribes from the topic + await gsub1.leave(topic) + await trio.sleep(1) # Wait for heartbeat to update mesh + assert topic not in gsub1.mesh + + # Wait for gsub0 to graft host_2 into its mesh via PX + await trio.sleep(1) + assert host_2.get_id() in gsub0.mesh[topic]