mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2025-12-31 20:36:24 +00:00
added: logs
This commit is contained in:
@ -15,6 +15,7 @@ from libp2p.crypto.keys import (
|
||||
from libp2p.crypto.rsa import (
|
||||
create_new_key_pair,
|
||||
)
|
||||
from libp2p.crypto.x25519 import create_new_key_pair as create_new_x25519_key_pair
|
||||
from libp2p.custom_types import (
|
||||
TProtocol,
|
||||
)
|
||||
@ -22,11 +23,17 @@ from libp2p.security.insecure.transport import (
|
||||
PLAINTEXT_PROTOCOL_ID,
|
||||
InsecureTransport,
|
||||
)
|
||||
from libp2p.security.noise.transport import PROTOCOL_ID as NOISE_PROTOCOL_ID
|
||||
from libp2p.security.noise.transport import Transport as NoiseTransport
|
||||
import libp2p.security.secio.transport as secio
|
||||
from libp2p.stream_muxer.mplex.mplex import (
|
||||
MPLEX_PROTOCOL_ID,
|
||||
Mplex,
|
||||
)
|
||||
from libp2p.stream_muxer.yamux.yamux import (
|
||||
Yamux,
|
||||
)
|
||||
from libp2p.stream_muxer.yamux.yamux import PROTOCOL_ID as YAMUX_PROTOCOL_ID
|
||||
|
||||
|
||||
def generate_new_rsa_identity() -> KeyPair:
|
||||
@ -39,7 +46,7 @@ async def build_host(transport: str, ip: str, port: str, sec_protocol: str, muxe
|
||||
key_pair = create_new_key_pair()
|
||||
host = new_host(
|
||||
key_pair,
|
||||
{MPLEX_PROTOCOL_ID: Mplex},
|
||||
{TProtocol(MPLEX_PROTOCOL_ID): Mplex},
|
||||
{
|
||||
TProtocol(PLAINTEXT_PROTOCOL_ID): InsecureTransport(key_pair),
|
||||
TProtocol(secio.ID): secio.Transport(key_pair),
|
||||
@ -47,6 +54,33 @@ async def build_host(transport: str, ip: str, port: str, sec_protocol: str, muxe
|
||||
)
|
||||
muladdr = multiaddr.Multiaddr(f"/ip4/{ip}/tcp/{port}")
|
||||
return (host, muladdr)
|
||||
case ("insecure", "yamux"):
|
||||
key_pair = create_new_key_pair()
|
||||
host = new_host(
|
||||
key_pair,
|
||||
{TProtocol(YAMUX_PROTOCOL_ID): Yamux},
|
||||
{
|
||||
TProtocol(PLAINTEXT_PROTOCOL_ID): InsecureTransport(key_pair),
|
||||
TProtocol(secio.ID): secio.Transport(key_pair),
|
||||
},
|
||||
)
|
||||
muladdr = multiaddr.Multiaddr(f"/ip4/{ip}/tcp/{port}")
|
||||
return (host, muladdr)
|
||||
case ("noise", "yamux"):
|
||||
key_pair = create_new_key_pair()
|
||||
noise_key_pair = create_new_x25519_key_pair()
|
||||
|
||||
host = new_host(
|
||||
key_pair,
|
||||
{TProtocol(YAMUX_PROTOCOL_ID): Yamux},
|
||||
{
|
||||
NOISE_PROTOCOL_ID: NoiseTransport(
|
||||
key_pair, noise_privkey=noise_key_pair.private_key
|
||||
)
|
||||
},
|
||||
)
|
||||
muladdr = multiaddr.Multiaddr(f"/ip4/{ip}/tcp/{port}")
|
||||
return (host, muladdr)
|
||||
case _:
|
||||
raise ValueError("Protocols not supported")
|
||||
|
||||
|
||||
@ -89,10 +89,18 @@ async def run_test(
|
||||
|
||||
handshake_start = time.perf_counter()
|
||||
|
||||
logger.info("GETTING READY FOR CONNECTION")
|
||||
await host.connect(info)
|
||||
stream = await host.new_stream(info.peer_id, [PING_PROTOCOL_ID])
|
||||
logger.info("HOST CONNECTED")
|
||||
|
||||
# TILL HERE EVERYTHING IS FINE
|
||||
|
||||
stream = await host.new_stream(info.peer_id, [PING_PROTOCOL_ID])
|
||||
logger.info("CREATED NEW STREAM")
|
||||
|
||||
# DOES NOT MORE FORWARD FROM THIS
|
||||
logger.info("Remote conection established")
|
||||
|
||||
nursery.start_soon(send_ping, stream)
|
||||
|
||||
handshake_plus_ping = (time.perf_counter() - handshake_start) * 1000.0
|
||||
|
||||
@ -5,7 +5,6 @@ from collections.abc import (
|
||||
from contextlib import (
|
||||
asynccontextmanager,
|
||||
)
|
||||
import logging
|
||||
from typing import (
|
||||
TYPE_CHECKING,
|
||||
Optional,
|
||||
@ -68,7 +67,10 @@ if TYPE_CHECKING:
|
||||
# telling it to listen on the given listen addresses.
|
||||
|
||||
|
||||
logger = logging.getLogger("libp2p.network.basic_host")
|
||||
# logger = logging.getLogger("libp2p.network.basic_host")
|
||||
from loguru import (
|
||||
logger,
|
||||
)
|
||||
|
||||
|
||||
class BasicHost(IHost):
|
||||
@ -181,12 +183,15 @@ class BasicHost(IHost):
|
||||
:return: stream: new stream created
|
||||
"""
|
||||
net_stream = await self._network.new_stream(peer_id)
|
||||
|
||||
logger.info("INETSTREAM CHECKING IN")
|
||||
logger.info(protocol_ids)
|
||||
# Perform protocol muxing to determine protocol to use
|
||||
try:
|
||||
logger.debug("PROTOCOLS TRYING TO GET SENT")
|
||||
selected_protocol = await self.multiselect_client.select_one_of(
|
||||
list(protocol_ids), MultiselectCommunicator(net_stream)
|
||||
)
|
||||
logger.info("PROTOCOLS GOT SENT")
|
||||
except MultiselectClientError as error:
|
||||
logger.debug("fail to open a stream to peer %s, error=%s", peer_id, error)
|
||||
await net_stream.reset()
|
||||
|
||||
@ -1,8 +1,11 @@
|
||||
import logging
|
||||
from typing import (
|
||||
Optional,
|
||||
)
|
||||
|
||||
# logger = logging.getLogger("libp2p.network.swarm")
|
||||
from loguru import (
|
||||
logger,
|
||||
)
|
||||
from multiaddr import (
|
||||
Multiaddr,
|
||||
)
|
||||
@ -55,8 +58,6 @@ from .exceptions import (
|
||||
SwarmException,
|
||||
)
|
||||
|
||||
logger = logging.getLogger("libp2p.network.swarm")
|
||||
|
||||
|
||||
def create_default_stream_handler(network: INetworkService) -> StreamHandlerFn:
|
||||
async def stream_handler(stream: INetStream) -> None:
|
||||
@ -130,6 +131,7 @@ class Swarm(Service, INetworkService):
|
||||
:return: muxed connection
|
||||
"""
|
||||
if peer_id in self.connections:
|
||||
logger.info("WE ARE RETURNING, PEER ALREADAY EXISTS")
|
||||
# If muxed connection already exists for peer_id,
|
||||
# set muxed connection equal to existing muxed connection
|
||||
return self.connections[peer_id]
|
||||
@ -150,6 +152,7 @@ class Swarm(Service, INetworkService):
|
||||
# Try all known addresses
|
||||
for multiaddr in addrs:
|
||||
try:
|
||||
logger.info("HANDSHAKE GOING TO HAPPEN")
|
||||
return await self.dial_addr(multiaddr, peer_id)
|
||||
except SwarmException as e:
|
||||
exceptions.append(e)
|
||||
@ -224,8 +227,11 @@ class Swarm(Service, INetworkService):
|
||||
logger.debug("attempting to open a stream to peer %s", peer_id)
|
||||
|
||||
swarm_conn = await self.dial_peer(peer_id)
|
||||
logger.info("INETCONN CREATED")
|
||||
|
||||
net_stream = await swarm_conn.new_stream()
|
||||
logger.info("INETSTREAM CREATED")
|
||||
|
||||
logger.debug("successfully opened a stream to peer %s", peer_id)
|
||||
return net_stream
|
||||
|
||||
|
||||
@ -2,6 +2,10 @@ from collections.abc import (
|
||||
Sequence,
|
||||
)
|
||||
|
||||
from loguru import (
|
||||
logger,
|
||||
)
|
||||
|
||||
from libp2p.abc import (
|
||||
IMultiselectClient,
|
||||
IMultiselectCommunicator,
|
||||
@ -36,11 +40,15 @@ class MultiselectClient(IMultiselectClient):
|
||||
try:
|
||||
await communicator.write(MULTISELECT_PROTOCOL_ID)
|
||||
except MultiselectCommunicatorError as error:
|
||||
logger.error("WROTE FAIL")
|
||||
raise MultiselectClientError() from error
|
||||
|
||||
logger.info(f"WROTE SUC, {MULTISELECT_PROTOCOL_ID}")
|
||||
try:
|
||||
handshake_contents = await communicator.read()
|
||||
logger.info(f"READ SUC, {handshake_contents}")
|
||||
except MultiselectCommunicatorError as error:
|
||||
logger.error(f"READ FAIL, {error}")
|
||||
raise MultiselectClientError() from error
|
||||
|
||||
if not is_valid_handshake(handshake_contents):
|
||||
@ -59,9 +67,12 @@ class MultiselectClient(IMultiselectClient):
|
||||
:return: selected protocol
|
||||
:raise MultiselectClientError: raised when protocol negotiation failed
|
||||
"""
|
||||
logger.info("TRYING TO GET THE HANDSHAKE HAPPENED")
|
||||
await self.handshake(communicator)
|
||||
logger.info("HANDSHAKE HAPPENED")
|
||||
|
||||
for protocol in protocols:
|
||||
logger.info(protocol)
|
||||
try:
|
||||
selected_protocol = await self.try_select(communicator, protocol)
|
||||
return selected_protocol
|
||||
@ -113,11 +124,17 @@ class MultiselectClient(IMultiselectClient):
|
||||
"""
|
||||
try:
|
||||
await communicator.write(protocol)
|
||||
from loguru import (
|
||||
logger,
|
||||
)
|
||||
|
||||
logger.info(protocol)
|
||||
except MultiselectCommunicatorError as error:
|
||||
raise MultiselectClientError() from error
|
||||
|
||||
try:
|
||||
response = await communicator.read()
|
||||
logger.info("Response: ", response)
|
||||
except MultiselectCommunicatorError as error:
|
||||
raise MultiselectClientError() from error
|
||||
|
||||
|
||||
Reference in New Issue
Block a user