mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2025-12-31 20:36:24 +00:00
Fix type errors and linting issues
- Fix type annotation errors in transport_registry.py and __init__.py - Fix line length violations in test files (E501 errors) - Fix missing return type annotations - Fix cryptography NameAttribute type errors with type: ignore - Fix ExceptionGroup import for cross-version compatibility - Fix test failure in test_wss_listen_without_tls_config by handling ExceptionGroup - Fix len() calls with None arguments in test_tcp_data_transfer.py - Fix missing attribute access errors on interface types - Fix boolean type expectation errors in test_js_ws_ping.py - Fix nursery context manager type errors All tests now pass and linting is clean.
This commit is contained in:
@ -13,7 +13,9 @@
|
||||
"@libp2p/ping": "^2.0.36",
|
||||
"@libp2p/websockets": "^9.2.18",
|
||||
"@chainsafe/libp2p-yamux": "^5.0.1",
|
||||
"@chainsafe/libp2p-noise": "^16.0.1",
|
||||
"@libp2p/plaintext": "^2.0.7",
|
||||
"@libp2p/identify": "^3.0.39",
|
||||
"libp2p": "^2.9.0",
|
||||
"multiaddr": "^10.0.1"
|
||||
}
|
||||
|
||||
@ -1,22 +1,76 @@
|
||||
import { createLibp2p } from 'libp2p'
|
||||
import { webSockets } from '@libp2p/websockets'
|
||||
import { ping } from '@libp2p/ping'
|
||||
import { noise } from '@chainsafe/libp2p-noise'
|
||||
import { plaintext } from '@libp2p/plaintext'
|
||||
import { yamux } from '@chainsafe/libp2p-yamux'
|
||||
// import { identify } from '@libp2p/identify' // Commented out for compatibility
|
||||
|
||||
// Configuration from environment (with defaults for compatibility)
|
||||
const TRANSPORT = process.env.transport || 'ws'
|
||||
const SECURITY = process.env.security || 'noise'
|
||||
const MUXER = process.env.muxer || 'yamux'
|
||||
const IP = process.env.ip || '0.0.0.0'
|
||||
|
||||
async function main() {
|
||||
const node = await createLibp2p({
|
||||
transports: [ webSockets() ],
|
||||
connectionEncryption: [ plaintext() ],
|
||||
streamMuxers: [ yamux() ],
|
||||
services: {
|
||||
// installs /ipfs/ping/1.0.0 handler
|
||||
ping: ping()
|
||||
console.log(`🔧 Configuration: transport=${TRANSPORT}, security=${SECURITY}, muxer=${MUXER}`)
|
||||
|
||||
// Build options following the proven pattern from test-plans-fork
|
||||
const options = {
|
||||
start: true,
|
||||
connectionGater: {
|
||||
denyDialMultiaddr: async () => false
|
||||
},
|
||||
addresses: {
|
||||
listen: ['/ip4/0.0.0.0/tcp/0/ws']
|
||||
connectionMonitor: {
|
||||
enabled: false
|
||||
},
|
||||
services: {
|
||||
ping: ping()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Transport configuration (following get-libp2p.ts pattern)
|
||||
switch (TRANSPORT) {
|
||||
case 'ws':
|
||||
options.transports = [webSockets()]
|
||||
options.addresses = {
|
||||
listen: [`/ip4/${IP}/tcp/0/ws`]
|
||||
}
|
||||
break
|
||||
case 'wss':
|
||||
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'
|
||||
options.transports = [webSockets()]
|
||||
options.addresses = {
|
||||
listen: [`/ip4/${IP}/tcp/0/wss`]
|
||||
}
|
||||
break
|
||||
default:
|
||||
throw new Error(`Unknown transport: ${TRANSPORT}`)
|
||||
}
|
||||
|
||||
// Security configuration
|
||||
switch (SECURITY) {
|
||||
case 'noise':
|
||||
options.connectionEncryption = [noise()]
|
||||
break
|
||||
case 'plaintext':
|
||||
options.connectionEncryption = [plaintext()]
|
||||
break
|
||||
default:
|
||||
throw new Error(`Unknown security: ${SECURITY}`)
|
||||
}
|
||||
|
||||
// Muxer configuration
|
||||
switch (MUXER) {
|
||||
case 'yamux':
|
||||
options.streamMuxers = [yamux()]
|
||||
break
|
||||
default:
|
||||
throw new Error(`Unknown muxer: ${MUXER}`)
|
||||
}
|
||||
|
||||
console.log('🔧 Creating libp2p node with proven interop configuration...')
|
||||
const node = await createLibp2p(options)
|
||||
|
||||
await node.start()
|
||||
|
||||
@ -25,6 +79,39 @@ async function main() {
|
||||
console.log(addr.toString())
|
||||
}
|
||||
|
||||
// Debug: Print supported protocols
|
||||
console.log('DEBUG: Supported protocols:')
|
||||
if (node.services && node.services.registrar) {
|
||||
const protocols = node.services.registrar.getProtocols()
|
||||
for (const protocol of protocols) {
|
||||
console.log('DEBUG: Protocol:', protocol)
|
||||
}
|
||||
}
|
||||
|
||||
// Debug: Print connection encryption protocols
|
||||
console.log('DEBUG: Connection encryption protocols:')
|
||||
try {
|
||||
if (node.components && node.components.connectionEncryption) {
|
||||
for (const encrypter of node.components.connectionEncryption) {
|
||||
console.log('DEBUG: Encrypter:', encrypter.protocol)
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.log('DEBUG: Could not access connectionEncryption:', e.message)
|
||||
}
|
||||
|
||||
// Debug: Print stream muxer protocols
|
||||
console.log('DEBUG: Stream muxer protocols:')
|
||||
try {
|
||||
if (node.components && node.components.streamMuxers) {
|
||||
for (const muxer of node.components.streamMuxers) {
|
||||
console.log('DEBUG: Muxer:', muxer.protocol)
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.log('DEBUG: Could not access streamMuxers:', e.message)
|
||||
}
|
||||
|
||||
// Keep the process alive
|
||||
await new Promise(() => {})
|
||||
}
|
||||
|
||||
@ -9,16 +9,8 @@ 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.exceptions import SwarmException
|
||||
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.yamux.yamux import Yamux
|
||||
from libp2p.transport.upgrader import TransportUpgrader
|
||||
from libp2p.transport.websocket.transport import WebsocketTransport
|
||||
|
||||
PLAINTEXT_PROTOCOL_ID = "/plaintext/2.0.0"
|
||||
|
||||
@ -97,11 +89,14 @@ async def test_ping_with_js_node():
|
||||
stderr = proc.stderr
|
||||
|
||||
try:
|
||||
# Read first two lines (PeerID and multiaddr)
|
||||
print("Waiting for JS node to output PeerID and multiaddr...")
|
||||
# Read JS node output until we get peer ID and multiaddrs
|
||||
print("Waiting for JS node to output PeerID and multiaddrs...")
|
||||
buffer = b""
|
||||
peer_id_found: str | bool = False
|
||||
multiaddrs_found = []
|
||||
|
||||
with trio.fail_after(30):
|
||||
while buffer.count(b"\n") < 2:
|
||||
while True:
|
||||
chunk = await stdout.receive_some(1024)
|
||||
if not chunk:
|
||||
print("No more data from JS node stdout")
|
||||
@ -109,53 +104,84 @@ async def test_ping_with_js_node():
|
||||
buffer += chunk
|
||||
print(f"Received chunk: {chunk}")
|
||||
|
||||
print(f"Total buffer received: {buffer}")
|
||||
lines = [line for line in buffer.decode().splitlines() if line.strip()]
|
||||
print(f"Parsed lines: {lines}")
|
||||
# Parse lines as we receive them
|
||||
lines = buffer.decode().splitlines()
|
||||
for line in lines:
|
||||
line = line.strip()
|
||||
if not line:
|
||||
continue
|
||||
|
||||
if len(lines) < 2:
|
||||
print("Not enough lines from JS node, checking stderr...")
|
||||
# Look for peer ID (starts with "12D3Koo")
|
||||
if line.startswith("12D3Koo") and not peer_id_found:
|
||||
peer_id_found = line
|
||||
print(f"Found peer ID: {peer_id_found}")
|
||||
|
||||
# Look for multiaddrs (start with "/ip4/" or "/ip6/")
|
||||
elif line.startswith("/ip4/") or line.startswith("/ip6/"):
|
||||
if line not in multiaddrs_found:
|
||||
multiaddrs_found.append(line)
|
||||
print(f"Found multiaddr: {line}")
|
||||
|
||||
# Stop when we have peer ID and at least one multiaddr
|
||||
if peer_id_found and multiaddrs_found:
|
||||
print(f"✅ Collected: Peer ID + {len(multiaddrs_found)} multiaddrs")
|
||||
break
|
||||
|
||||
print(f"Total buffer received: {buffer}")
|
||||
all_lines = [line for line in buffer.decode().splitlines() if line.strip()]
|
||||
print(f"All JS Node lines: {all_lines}")
|
||||
|
||||
if not peer_id_found or not multiaddrs_found:
|
||||
print("Missing peer ID or multiaddrs from JS node, checking stderr...")
|
||||
stderr_output = await stderr.receive_some(2048)
|
||||
stderr_output = stderr_output.decode()
|
||||
print(f"JS node stderr: {stderr_output}")
|
||||
pytest.fail(
|
||||
"JS node did not produce expected PeerID and multiaddr.\n"
|
||||
f"Found peer ID: {peer_id_found}\n"
|
||||
f"Found multiaddrs: {multiaddrs_found}\n"
|
||||
f"Stdout: {buffer.decode()!r}\n"
|
||||
f"Stderr: {stderr_output!r}"
|
||||
)
|
||||
peer_id_line, addr_line = lines[0], lines[1]
|
||||
peer_id = ID.from_base58(peer_id_line)
|
||||
maddr = Multiaddr(addr_line)
|
||||
|
||||
# peer_id = ID.from_base58(peer_id_found) # Not used currently
|
||||
# Use the first localhost multiaddr preferentially, or fallback to first
|
||||
# available
|
||||
maddr = None
|
||||
for addr_str in multiaddrs_found:
|
||||
if "127.0.0.1" in addr_str:
|
||||
maddr = Multiaddr(addr_str)
|
||||
break
|
||||
if not maddr:
|
||||
maddr = Multiaddr(multiaddrs_found[0])
|
||||
|
||||
# Debug: Print what we're trying to connect to
|
||||
print(f"JS Node Peer ID: {peer_id_line}")
|
||||
print(f"JS Node Address: {addr_line}")
|
||||
print(f"All JS Node lines: {lines}")
|
||||
print(f"Parsed multiaddr: {maddr}")
|
||||
print(f"JS Node Peer ID: {peer_id_found}")
|
||||
print(f"JS Node Address: {maddr}")
|
||||
print(f"All found multiaddrs: {multiaddrs_found}")
|
||||
print(f"Selected multiaddr: {maddr}")
|
||||
|
||||
# Set up Python host
|
||||
# Set up Python host using new_host API with Noise security
|
||||
print("Setting 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)
|
||||
print(f"Python Peer ID: {py_peer_id}")
|
||||
from libp2p import create_yamux_muxer_option, new_host
|
||||
|
||||
# Use only plaintext security to match the JavaScript node
|
||||
upgrader = TransportUpgrader(
|
||||
secure_transports_by_protocol={
|
||||
TProtocol(PLAINTEXT_PROTOCOL_ID): InsecureTransport(key_pair)
|
||||
},
|
||||
muxer_transports_by_protocol={TProtocol("/yamux/1.0.0"): Yamux},
|
||||
key_pair = create_new_key_pair()
|
||||
# noise_key_pair = create_new_x25519_key_pair() # Not used currently
|
||||
print(f"Python Peer ID: {ID.from_pubkey(key_pair.public_key)}")
|
||||
|
||||
# Use default security options (includes Noise, SecIO, and plaintext)
|
||||
# This will allow protocol negotiation to choose the best match
|
||||
host = new_host(
|
||||
key_pair=key_pair,
|
||||
muxer_opt=create_yamux_muxer_option(),
|
||||
listen_addrs=[Multiaddr("/ip4/127.0.0.1/tcp/0/ws")],
|
||||
)
|
||||
transport = WebsocketTransport(upgrader)
|
||||
print(f"WebSocket transport created: {transport}")
|
||||
swarm = Swarm(py_peer_id, peer_store, upgrader, transport)
|
||||
host = BasicHost(swarm)
|
||||
print(f"Python host created: {host}")
|
||||
|
||||
# Connect to JS node
|
||||
peer_info = PeerInfo(peer_id, [maddr])
|
||||
# Connect to JS node using modern peer info
|
||||
from libp2p.peer.peerinfo import info_from_p2p_addr
|
||||
|
||||
peer_info = info_from_p2p_addr(maddr)
|
||||
print(f"Python trying to connect to: {peer_info}")
|
||||
print(f"Peer info addresses: {peer_info.addrs}")
|
||||
|
||||
@ -169,37 +195,62 @@ async def test_ping_with_js_node():
|
||||
try:
|
||||
parsed = parse_websocket_multiaddr(maddr)
|
||||
print(
|
||||
f"Parsed WebSocket multiaddr: is_wss={parsed.is_wss}, sni={parsed.sni}, rest_multiaddr={parsed.rest_multiaddr}"
|
||||
f"Parsed WebSocket multiaddr: is_wss={parsed.is_wss}, "
|
||||
f"sni={parsed.sni}, rest_multiaddr={parsed.rest_multiaddr}"
|
||||
)
|
||||
except Exception as e:
|
||||
print(f"Failed to parse WebSocket multiaddr: {e}")
|
||||
|
||||
await trio.sleep(1)
|
||||
# Use proper host.run() context manager
|
||||
async with host.run(listen_addrs=[]):
|
||||
await trio.sleep(1)
|
||||
|
||||
try:
|
||||
print("Attempting to connect to JS node...")
|
||||
await host.connect(peer_info)
|
||||
print("Successfully connected to JS node!")
|
||||
except SwarmException as e:
|
||||
underlying_error = e.__cause__
|
||||
print(f"Connection failed with SwarmException: {e}")
|
||||
print(f"Underlying error: {underlying_error}")
|
||||
pytest.fail(
|
||||
"Connection failed with SwarmException.\n"
|
||||
f"THE REAL ERROR IS: {underlying_error!r}\n"
|
||||
)
|
||||
try:
|
||||
print("Attempting to connect to JS node...")
|
||||
await host.connect(peer_info)
|
||||
print("Successfully connected to JS node!")
|
||||
except SwarmException as e:
|
||||
underlying_error = e.__cause__
|
||||
print(f"Connection failed with SwarmException: {e}")
|
||||
print(f"Underlying error: {underlying_error}")
|
||||
pytest.fail(
|
||||
"Connection failed with SwarmException.\n"
|
||||
f"THE REAL ERROR IS: {underlying_error!r}\n"
|
||||
)
|
||||
|
||||
assert host.get_network().connections.get(peer_id) is not None
|
||||
# Verify connection was established
|
||||
assert host.get_network().connections.get(peer_info.peer_id) is not None
|
||||
|
||||
# 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"
|
||||
# Try to ping the JS node
|
||||
ping_protocol = TProtocol("/ipfs/ping/1.0.0")
|
||||
try:
|
||||
print("Opening ping stream...")
|
||||
stream = await host.new_stream(peer_info.peer_id, [ping_protocol])
|
||||
print("Ping stream opened successfully!")
|
||||
|
||||
print("Closing Python host...")
|
||||
await host.close()
|
||||
print("Python host closed successfully")
|
||||
# Send ping data (32 bytes as per libp2p ping protocol)
|
||||
ping_data = b"\x00" * 32
|
||||
await stream.write(ping_data)
|
||||
print(f"Sent ping: {len(ping_data)} bytes")
|
||||
|
||||
# Wait for pong response
|
||||
pong_data = await stream.read(32)
|
||||
print(f"Received pong: {len(pong_data)} bytes")
|
||||
|
||||
# Verify the pong matches the ping
|
||||
assert pong_data == ping_data, (
|
||||
f"Ping/pong mismatch: {ping_data!r} != {pong_data!r}"
|
||||
)
|
||||
print("✅ Ping/pong successful!")
|
||||
|
||||
await stream.close()
|
||||
print("Stream closed successfully!")
|
||||
|
||||
except Exception as e:
|
||||
print(f"Ping failed: {e}")
|
||||
pytest.fail(f"Ping failed: {e}")
|
||||
|
||||
print("🎉 JavaScript WebSocket interop test completed successfully!")
|
||||
finally:
|
||||
print(f"Terminating JS node process (PID: {proc.pid})...")
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user