mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2025-12-31 20:36:24 +00:00
Pubsub example for py-libp2p (#515)
* Initial setup for pubsup * Created node and trying to setup gossipsub * Fix: Use pubsub object for publishing messages instead of gossipsub * Correct help message for port argument. * Fix: Used pubsub object instead of gossipsub object on Client side * Fix: handle_new_peer method of pubsub is used to connect to new peers. * used for host.connect to connect to peers * Corrected script for connecting to other peers. * message receiving function created * message publishing function created * Refactored the code for improved clarity and maintainability. * fix: make publish loop input non-blocking to prevent event loop blocking * refactored the code for better user experience while publishing message * corrected the name of protocol * Fix: Correct the implementation of the port argument * Added pubsub initialization * added logging * pubsub instance is running * Enhance publish loop with user prompts and error handling * Connection monitoring added * Add key pair generation and security options to pubsub host initialization * Refactor pubsub logging and corrected gossipsub protocol id * Started gossipsub service * Add dynamic port assignment * Refactor pubsub example for CI * feat: monitor_peer_topics function added * Noise protocol added * refactor: default port set to none and some logging changes. * refactor: Add graceful shutdown with termination events - Replace infinite loops with termination events - Add proper shutdown handling for all loops - Implement clean resource cleanup on exit - Add shutdown message for better user feedback - Update signal handling for graceful termination * Changed import path for factories file. - to align import statement with changes from PR 543 * Added News Fragment * Added pub-sub demo to the console_scripts section in setup.py * Added pubsub example to Documentation * Fix formatting and path in PubSub documentation example * Added pubsub example in toctree * Added tests for pubsub example * updated the description of pubsub example * corrected the name of pubsub docs file * Remove unused imports and security options from pubsub example * Update script usage instructions in pubsub example * Enhanced compatibility for python 3.9 * Corrected console output
This commit is contained in:
@ -1,12 +1,24 @@
|
||||
import pytest
|
||||
import trio
|
||||
|
||||
from libp2p.custom_types import (
|
||||
TProtocol,
|
||||
)
|
||||
from libp2p.host.exceptions import (
|
||||
StreamFailure,
|
||||
)
|
||||
from libp2p.peer.peerinfo import (
|
||||
info_from_p2p_addr,
|
||||
)
|
||||
from libp2p.pubsub.gossipsub import (
|
||||
GossipSub,
|
||||
)
|
||||
from libp2p.pubsub.pubsub import (
|
||||
Pubsub,
|
||||
)
|
||||
from libp2p.tools.async_service.trio_service import (
|
||||
background_trio_service,
|
||||
)
|
||||
from libp2p.tools.utils import (
|
||||
MAX_READ_LEN,
|
||||
)
|
||||
@ -17,6 +29,8 @@ from tests.utils.factories import (
|
||||
CHAT_PROTOCOL_ID = "/chat/1.0.0"
|
||||
ECHO_PROTOCOL_ID = "/echo/1.0.0"
|
||||
PING_PROTOCOL_ID = "/ipfs/ping/1.0.0"
|
||||
GOSSIPSUB_PROTOCOL_ID = TProtocol("/meshsub/1.0.0")
|
||||
PUBSUB_TEST_TOPIC = "test-pubsub-topic"
|
||||
|
||||
|
||||
async def hello_world(host_a, host_b):
|
||||
@ -185,6 +199,53 @@ async def ping_demo(host_a, host_b):
|
||||
assert response == ping_data
|
||||
|
||||
|
||||
async def pubsub_demo(host_a, host_b):
|
||||
gossipsub_a = GossipSub([GOSSIPSUB_PROTOCOL_ID], 3, 2, 4, 0.1, 1)
|
||||
gossipsub_b = GossipSub([GOSSIPSUB_PROTOCOL_ID], 3, 2, 4, 0.1, 1)
|
||||
pubsub_a = Pubsub(host_a, gossipsub_a)
|
||||
pubsub_b = Pubsub(host_b, gossipsub_b)
|
||||
message_a_to_b = "Hello from A to B"
|
||||
b_received = trio.Event()
|
||||
received_by_b = None
|
||||
|
||||
async def handle_subscription_b(subscription):
|
||||
nonlocal received_by_b
|
||||
message = await subscription.get()
|
||||
received_by_b = message.data.decode("utf-8")
|
||||
print(f"Host B received: {received_by_b}")
|
||||
b_received.set()
|
||||
|
||||
async with background_trio_service(pubsub_a):
|
||||
async with background_trio_service(pubsub_b):
|
||||
async with background_trio_service(gossipsub_a):
|
||||
async with background_trio_service(gossipsub_b):
|
||||
await pubsub_a.wait_until_ready()
|
||||
await pubsub_b.wait_until_ready()
|
||||
|
||||
listen_addrs_b = host_b.get_addrs()
|
||||
peer_info_b = info_from_p2p_addr(listen_addrs_b[0])
|
||||
try:
|
||||
await pubsub_a.host.connect(peer_info_b)
|
||||
print("Connection attempt completed")
|
||||
except Exception as e:
|
||||
print(f"Connection error: {e}")
|
||||
raise
|
||||
|
||||
subscription_b = await pubsub_b.subscribe(PUBSUB_TEST_TOPIC)
|
||||
async with trio.open_nursery() as nursery:
|
||||
nursery.start_soon(handle_subscription_b, subscription_b)
|
||||
await trio.sleep(0.1)
|
||||
await pubsub_a.publish(
|
||||
PUBSUB_TEST_TOPIC, message_a_to_b.encode()
|
||||
)
|
||||
with trio.move_on_after(3):
|
||||
await b_received.wait()
|
||||
nursery.cancel_scope.cancel()
|
||||
|
||||
assert received_by_b == message_a_to_b
|
||||
assert b_received.is_set()
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"test",
|
||||
[
|
||||
@ -195,6 +256,7 @@ async def ping_demo(host_a, host_b):
|
||||
chat_demo,
|
||||
echo_demo,
|
||||
ping_demo,
|
||||
pubsub_demo,
|
||||
],
|
||||
)
|
||||
@pytest.mark.trio
|
||||
@ -203,8 +265,9 @@ async def test_protocols(test, security_protocol):
|
||||
async with HostFactory.create_batch_and_listen(
|
||||
2, security_protocol=security_protocol
|
||||
) as hosts:
|
||||
addr = hosts[0].get_addrs()[0]
|
||||
info = info_from_p2p_addr(addr)
|
||||
await hosts[1].connect(info)
|
||||
if test != pubsub_demo:
|
||||
addr = hosts[0].get_addrs()[0]
|
||||
info = info_from_p2p_addr(addr)
|
||||
await hosts[1].connect(info)
|
||||
|
||||
await test(hosts[0], hosts[1])
|
||||
|
||||
Reference in New Issue
Block a user