mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2026-02-12 16:10:57 +00:00
@ -24,11 +24,11 @@ PLAINTEXT_PROTOCOL_ID = "/plaintext/1.0.0"
|
|||||||
|
|
||||||
@pytest.mark.trio
|
@pytest.mark.trio
|
||||||
async def test_ping_with_js_node():
|
async def test_ping_with_js_node():
|
||||||
# 1) Path to the JS node script
|
# Path to the JS node script
|
||||||
js_node_dir = os.path.join(os.path.dirname(__file__), "js_libp2p", "js_node", "src")
|
js_node_dir = os.path.join(os.path.dirname(__file__), "js_libp2p", "js_node", "src")
|
||||||
script_name = "ws_ping_node.mjs"
|
script_name = "ws_ping_node.mjs"
|
||||||
|
|
||||||
# 2) Launch the JS libp2p node (long-running)
|
# Launch the JS libp2p node (long-running)
|
||||||
proc = await open_process(
|
proc = await open_process(
|
||||||
["node", script_name],
|
["node", script_name],
|
||||||
stdout=subprocess.PIPE,
|
stdout=subprocess.PIPE,
|
||||||
@ -36,7 +36,7 @@ async def test_ping_with_js_node():
|
|||||||
cwd=js_node_dir,
|
cwd=js_node_dir,
|
||||||
)
|
)
|
||||||
try:
|
try:
|
||||||
# 3) Read first two lines (PeerID and multiaddr)
|
# Read first two lines (PeerID and multiaddr)
|
||||||
buffer = b""
|
buffer = b""
|
||||||
with trio.fail_after(10):
|
with trio.fail_after(10):
|
||||||
while buffer.count(b"\n") < 2:
|
while buffer.count(b"\n") < 2:
|
||||||
@ -50,7 +50,7 @@ async def test_ping_with_js_node():
|
|||||||
peer_id = ID.from_base58(peer_id_line)
|
peer_id = ID.from_base58(peer_id_line)
|
||||||
maddr = Multiaddr(addr_line)
|
maddr = Multiaddr(addr_line)
|
||||||
|
|
||||||
# 4) Set up Python host
|
# Set up Python host
|
||||||
key_pair = create_new_key_pair()
|
key_pair = create_new_key_pair()
|
||||||
py_peer_id = ID.from_pubkey(key_pair.public_key)
|
py_peer_id = ID.from_pubkey(key_pair.public_key)
|
||||||
peer_store = PeerStore()
|
peer_store = PeerStore()
|
||||||
@ -66,19 +66,19 @@ async def test_ping_with_js_node():
|
|||||||
swarm = Swarm(py_peer_id, peer_store, upgrader, transport)
|
swarm = Swarm(py_peer_id, peer_store, upgrader, transport)
|
||||||
host = BasicHost(swarm)
|
host = BasicHost(swarm)
|
||||||
|
|
||||||
# 5) Connect to JS node
|
# Connect to JS node
|
||||||
peer_info = PeerInfo(peer_id, [maddr])
|
peer_info = PeerInfo(peer_id, [maddr])
|
||||||
await host.connect(peer_info)
|
await host.connect(peer_info)
|
||||||
assert host.get_network().connections.get(peer_id) is not None
|
assert host.get_network().connections.get(peer_id) is not None
|
||||||
await trio.sleep(0.1)
|
await trio.sleep(0.1)
|
||||||
|
|
||||||
# 6) Ping protocol
|
# Ping protocol
|
||||||
stream = await host.new_stream(peer_id, [TProtocol("/ipfs/ping/1.0.0")])
|
stream = await host.new_stream(peer_id, [TProtocol("/ipfs/ping/1.0.0")])
|
||||||
await stream.write(b"ping")
|
await stream.write(b"ping")
|
||||||
data = await stream.read(4)
|
data = await stream.read(4)
|
||||||
assert data == b"pong"
|
assert data == b"pong"
|
||||||
|
|
||||||
# 7) Cleanup
|
# Cleanup
|
||||||
await host.close()
|
await host.close()
|
||||||
finally:
|
finally:
|
||||||
proc.send_signal(signal.SIGTERM)
|
proc.send_signal(signal.SIGTERM)
|
||||||
|
|||||||
@ -22,13 +22,13 @@ PLAINTEXT_PROTOCOL_ID = "/plaintext/1.0.0"
|
|||||||
async def make_host(
|
async def make_host(
|
||||||
listen_addrs: Sequence[Multiaddr] | None = None,
|
listen_addrs: Sequence[Multiaddr] | None = None,
|
||||||
) -> tuple[BasicHost, Any | None]:
|
) -> tuple[BasicHost, Any | None]:
|
||||||
# 1) Identity
|
# Identity
|
||||||
key_pair = create_new_key_pair()
|
key_pair = create_new_key_pair()
|
||||||
peer_id = ID.from_pubkey(key_pair.public_key)
|
peer_id = ID.from_pubkey(key_pair.public_key)
|
||||||
peer_store = PeerStore()
|
peer_store = PeerStore()
|
||||||
peer_store.add_key_pair(peer_id, key_pair)
|
peer_store.add_key_pair(peer_id, key_pair)
|
||||||
|
|
||||||
# 2) Upgrader
|
# Upgrader
|
||||||
upgrader = TransportUpgrader(
|
upgrader = TransportUpgrader(
|
||||||
secure_transports_by_protocol={
|
secure_transports_by_protocol={
|
||||||
TProtocol(PLAINTEXT_PROTOCOL_ID): InsecureTransport(key_pair)
|
TProtocol(PLAINTEXT_PROTOCOL_ID): InsecureTransport(key_pair)
|
||||||
@ -36,12 +36,12 @@ async def make_host(
|
|||||||
muxer_transports_by_protocol={TProtocol(MPLEX_PROTOCOL_ID): Mplex},
|
muxer_transports_by_protocol={TProtocol(MPLEX_PROTOCOL_ID): Mplex},
|
||||||
)
|
)
|
||||||
|
|
||||||
# 3) Transport + Swarm + Host
|
# Transport + Swarm + Host
|
||||||
transport = WebsocketTransport()
|
transport = WebsocketTransport()
|
||||||
swarm = Swarm(peer_id, peer_store, upgrader, transport)
|
swarm = Swarm(peer_id, peer_store, upgrader, transport)
|
||||||
host = BasicHost(swarm)
|
host = BasicHost(swarm)
|
||||||
|
|
||||||
# 4) Optionally run/listen
|
# Optionally run/listen
|
||||||
ctx = None
|
ctx = None
|
||||||
if listen_addrs:
|
if listen_addrs:
|
||||||
ctx = host.run(listen_addrs)
|
ctx = host.run(listen_addrs)
|
||||||
@ -52,20 +52,15 @@ async def make_host(
|
|||||||
|
|
||||||
@pytest.mark.trio
|
@pytest.mark.trio
|
||||||
async def test_websocket_dial_and_listen():
|
async def test_websocket_dial_and_listen():
|
||||||
# Start server
|
|
||||||
server_host, server_ctx = await make_host([Multiaddr("/ip4/127.0.0.1/tcp/0/ws")])
|
server_host, server_ctx = await make_host([Multiaddr("/ip4/127.0.0.1/tcp/0/ws")])
|
||||||
# Client
|
|
||||||
client_host, _ = await make_host(None)
|
client_host, _ = await make_host(None)
|
||||||
|
|
||||||
# Dial
|
|
||||||
peer_info = PeerInfo(server_host.get_id(), server_host.get_addrs())
|
peer_info = PeerInfo(server_host.get_id(), server_host.get_addrs())
|
||||||
await client_host.connect(peer_info)
|
await client_host.connect(peer_info)
|
||||||
|
|
||||||
# Verify connections
|
|
||||||
assert client_host.get_network().connections.get(server_host.get_id())
|
assert client_host.get_network().connections.get(server_host.get_id())
|
||||||
assert server_host.get_network().connections.get(client_host.get_id())
|
assert server_host.get_network().connections.get(client_host.get_id())
|
||||||
|
|
||||||
# Cleanup
|
|
||||||
await client_host.close()
|
await client_host.close()
|
||||||
if server_ctx:
|
if server_ctx:
|
||||||
await server_ctx.__aexit__(None, None, None)
|
await server_ctx.__aexit__(None, None, None)
|
||||||
|
|||||||
Reference in New Issue
Block a user