mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2026-02-11 23:51:07 +00:00
added WebSocket transport support
Signed-off-by: GautamBytes <manchandanigautam@gmail.com>
This commit is contained in:
0
tests/interop/__init__.py
Normal file
0
tests/interop/__init__.py
Normal file
18
tests/interop/js_libp2p/js_node/src/package.json
Normal file
18
tests/interop/js_libp2p/js_node/src/package.json
Normal file
@ -0,0 +1,18 @@
|
||||
{
|
||||
"name": "src",
|
||||
"version": "1.0.0",
|
||||
"main": "ping.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"description": "",
|
||||
"dependencies": {
|
||||
"@libp2p/ping": "^2.0.36",
|
||||
"@libp2p/websockets": "^9.2.18",
|
||||
"libp2p": "^2.9.0",
|
||||
"multiaddr": "^10.0.1"
|
||||
}
|
||||
}
|
||||
35
tests/interop/js_libp2p/js_node/src/ws_ping_node.mjs
Normal file
35
tests/interop/js_libp2p/js_node/src/ws_ping_node.mjs
Normal file
@ -0,0 +1,35 @@
|
||||
import { createLibp2p } from 'libp2p'
|
||||
import { webSockets } from '@libp2p/websockets'
|
||||
import { ping } from '@libp2p/ping'
|
||||
import { plaintext } from '@libp2p/insecure'
|
||||
import { mplex } from '@libp2p/mplex'
|
||||
|
||||
async function main() {
|
||||
const node = await createLibp2p({
|
||||
transports: [ webSockets() ],
|
||||
connectionEncryption: [ plaintext() ],
|
||||
streamMuxers: [ mplex() ],
|
||||
services: {
|
||||
// installs /ipfs/ping/1.0.0 handler
|
||||
ping: ping()
|
||||
},
|
||||
addresses: {
|
||||
listen: ['/ip4/127.0.0.1/tcp/0/ws']
|
||||
}
|
||||
})
|
||||
|
||||
await node.start()
|
||||
|
||||
console.log(node.peerId.toString())
|
||||
for (const addr of node.getMultiaddrs()) {
|
||||
console.log(addr.toString())
|
||||
}
|
||||
|
||||
// Keep the process alive
|
||||
await new Promise(() => {})
|
||||
}
|
||||
|
||||
main().catch(err => {
|
||||
console.error(err)
|
||||
process.exit(1)
|
||||
})
|
||||
85
tests/interop/test_js_ws_ping.py
Normal file
85
tests/interop/test_js_ws_ping.py
Normal file
@ -0,0 +1,85 @@
|
||||
import os
|
||||
import signal
|
||||
import subprocess
|
||||
|
||||
import pytest
|
||||
from multiaddr import Multiaddr
|
||||
import trio
|
||||
from trio.lowlevel import open_process
|
||||
|
||||
from libp2p.crypto.secp256k1 import create_new_key_pair
|
||||
from libp2p.custom_types import TProtocol
|
||||
from libp2p.host.basic_host import BasicHost
|
||||
from libp2p.network.swarm import Swarm
|
||||
from libp2p.peer.id import ID
|
||||
from libp2p.peer.peerinfo import PeerInfo
|
||||
from libp2p.peer.peerstore import PeerStore
|
||||
from libp2p.security.insecure.transport import InsecureTransport
|
||||
from libp2p.stream_muxer.mplex.mplex import MPLEX_PROTOCOL_ID, Mplex
|
||||
from libp2p.transport.upgrader import TransportUpgrader
|
||||
from libp2p.transport.websocket.transport import WebsocketTransport
|
||||
|
||||
PLAINTEXT_PROTOCOL_ID = "/plaintext/1.0.0"
|
||||
|
||||
|
||||
@pytest.mark.trio
|
||||
async def test_ping_with_js_node():
|
||||
# 1) Path to the JS node script
|
||||
js_node_dir = os.path.join(os.path.dirname(__file__), "js_libp2p", "js_node", "src")
|
||||
script_name = "ws_ping_node.mjs"
|
||||
|
||||
# 2) Launch the JS libp2p node (long-running)
|
||||
proc = await open_process(
|
||||
["node", script_name],
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.DEVNULL,
|
||||
cwd=js_node_dir,
|
||||
)
|
||||
try:
|
||||
# 3) Read first two lines (PeerID and multiaddr)
|
||||
buffer = b""
|
||||
with trio.fail_after(10):
|
||||
while buffer.count(b"\n") < 2:
|
||||
chunk = await proc.stdout.receive_some(1024) # type: ignore
|
||||
if not chunk:
|
||||
break
|
||||
buffer += chunk
|
||||
|
||||
lines = buffer.decode().strip().split("\n")
|
||||
peer_id_line, addr_line = lines[0], lines[1]
|
||||
peer_id = ID.from_base58(peer_id_line)
|
||||
maddr = Multiaddr(addr_line)
|
||||
|
||||
# 4) Set up Python host
|
||||
key_pair = create_new_key_pair()
|
||||
py_peer_id = ID.from_pubkey(key_pair.public_key)
|
||||
peer_store = PeerStore()
|
||||
peer_store.add_key_pair(py_peer_id, key_pair)
|
||||
|
||||
upgrader = TransportUpgrader(
|
||||
secure_transports_by_protocol={
|
||||
TProtocol(PLAINTEXT_PROTOCOL_ID): InsecureTransport(key_pair)
|
||||
},
|
||||
muxer_transports_by_protocol={TProtocol(MPLEX_PROTOCOL_ID): Mplex},
|
||||
)
|
||||
transport = WebsocketTransport()
|
||||
swarm = Swarm(py_peer_id, peer_store, upgrader, transport)
|
||||
host = BasicHost(swarm)
|
||||
|
||||
# 5) Connect to JS node
|
||||
peer_info = PeerInfo(peer_id, [maddr])
|
||||
await host.connect(peer_info)
|
||||
assert host.get_network().connections.get(peer_id) is not None
|
||||
await trio.sleep(0.1)
|
||||
|
||||
# 6) Ping protocol
|
||||
stream = await host.new_stream(peer_id, [TProtocol("/ipfs/ping/1.0.0")])
|
||||
await stream.write(b"ping")
|
||||
data = await stream.read(4)
|
||||
assert data == b"pong"
|
||||
|
||||
# 7) Cleanup
|
||||
await host.close()
|
||||
finally:
|
||||
proc.send_signal(signal.SIGTERM)
|
||||
await trio.sleep(0)
|
||||
0
tests/transport/__init__.py
Normal file
0
tests/transport/__init__.py
Normal file
72
tests/transport/test_websocket.py
Normal file
72
tests/transport/test_websocket.py
Normal file
@ -0,0 +1,72 @@
|
||||
from collections.abc import Sequence
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
from multiaddr import Multiaddr
|
||||
|
||||
from libp2p.crypto.secp256k1 import create_new_key_pair
|
||||
from libp2p.custom_types import TProtocol
|
||||
from libp2p.host.basic_host import BasicHost
|
||||
from libp2p.network.swarm import Swarm
|
||||
from libp2p.peer.id import ID
|
||||
from libp2p.peer.peerinfo import PeerInfo
|
||||
from libp2p.peer.peerstore import PeerStore
|
||||
from libp2p.security.insecure.transport import InsecureTransport
|
||||
from libp2p.stream_muxer.mplex.mplex import MPLEX_PROTOCOL_ID, Mplex
|
||||
from libp2p.transport.upgrader import TransportUpgrader
|
||||
from libp2p.transport.websocket.transport import WebsocketTransport
|
||||
|
||||
PLAINTEXT_PROTOCOL_ID = "/plaintext/1.0.0"
|
||||
|
||||
|
||||
async def make_host(
|
||||
listen_addrs: Sequence[Multiaddr] | None = None,
|
||||
) -> tuple[BasicHost, Any | None]:
|
||||
# 1) Identity
|
||||
key_pair = create_new_key_pair()
|
||||
peer_id = ID.from_pubkey(key_pair.public_key)
|
||||
peer_store = PeerStore()
|
||||
peer_store.add_key_pair(peer_id, key_pair)
|
||||
|
||||
# 2) Upgrader
|
||||
upgrader = TransportUpgrader(
|
||||
secure_transports_by_protocol={
|
||||
TProtocol(PLAINTEXT_PROTOCOL_ID): InsecureTransport(key_pair)
|
||||
},
|
||||
muxer_transports_by_protocol={TProtocol(MPLEX_PROTOCOL_ID): Mplex},
|
||||
)
|
||||
|
||||
# 3) Transport + Swarm + Host
|
||||
transport = WebsocketTransport()
|
||||
swarm = Swarm(peer_id, peer_store, upgrader, transport)
|
||||
host = BasicHost(swarm)
|
||||
|
||||
# 4) Optionally run/listen
|
||||
ctx = None
|
||||
if listen_addrs:
|
||||
ctx = host.run(listen_addrs)
|
||||
await ctx.__aenter__()
|
||||
|
||||
return host, ctx
|
||||
|
||||
|
||||
@pytest.mark.trio
|
||||
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")])
|
||||
# Client
|
||||
client_host, _ = await make_host(None)
|
||||
|
||||
# Dial
|
||||
peer_info = PeerInfo(server_host.get_id(), server_host.get_addrs())
|
||||
await client_host.connect(peer_info)
|
||||
|
||||
# Verify connections
|
||||
assert client_host.get_network().connections.get(server_host.get_id())
|
||||
assert server_host.get_network().connections.get(client_host.get_id())
|
||||
|
||||
# Cleanup
|
||||
await client_host.close()
|
||||
if server_ctx:
|
||||
await server_ctx.__aexit__(None, None, None)
|
||||
await server_host.close()
|
||||
Reference in New Issue
Block a user