From 8596f7390f1fdd51498f7777cc094b4ac86acbaf Mon Sep 17 00:00:00 2001 From: mhchia Date: Tue, 20 Aug 2019 11:02:21 +0800 Subject: [PATCH] PR feedback: set protocol_id to constants --- libp2p/__init__.py | 9 ++++----- libp2p/security/insecure/transport.py | 3 +++ libp2p/stream_muxer/mplex/mplex.py | 3 +++ 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/libp2p/__init__.py b/libp2p/__init__.py index 5490499..21f1d9c 100644 --- a/libp2p/__init__.py +++ b/libp2p/__init__.py @@ -13,9 +13,9 @@ from libp2p.peer.peerstore import PeerStore from libp2p.peer.peerstore_interface import IPeerStore from libp2p.routing.interfaces import IPeerRouting from libp2p.routing.kademlia.kademlia_peer_router import KadmeliaPeerRouter -from libp2p.security.insecure.transport import InsecureTransport +from libp2p.security.insecure.transport import PLAINTEXT_PROTOCOL_ID, InsecureTransport from libp2p.security.secure_transport_interface import ISecureTransport -from libp2p.stream_muxer.mplex.mplex import Mplex +from libp2p.stream_muxer.mplex.mplex import MPLEX_PROTOCOL_ID, Mplex from libp2p.stream_muxer.muxer_multistream import MuxerClassType from libp2p.transport.tcp.tcp import TCP from libp2p.transport.upgrader import TransportUpgrader @@ -96,10 +96,9 @@ def initialize_default_swarm( # TODO: Parse `transport_opt` to determine transport transport = TCP() - # TODO TransportUpgrader is not doing anything really - muxer_transports_by_protocol = muxer_opt or {TProtocol("/mplex/6.7.0"): Mplex} + muxer_transports_by_protocol = muxer_opt or {MPLEX_PROTOCOL_ID: Mplex} security_transports_by_protocol = sec_opt or { - TProtocol("/plaintext/1.0.0"): InsecureTransport(key_pair) + PLAINTEXT_PROTOCOL_ID: InsecureTransport(key_pair) } upgrader = TransportUpgrader( security_transports_by_protocol, muxer_transports_by_protocol diff --git a/libp2p/security/insecure/transport.py b/libp2p/security/insecure/transport.py index 2d8a5a8..97c9676 100644 --- a/libp2p/security/insecure/transport.py +++ b/libp2p/security/insecure/transport.py @@ -3,6 +3,9 @@ from libp2p.peer.id import ID from libp2p.security.base_session import BaseSession from libp2p.security.base_transport import BaseSecureTransport from libp2p.security.secure_conn_interface import ISecureConn +from libp2p.typing import TProtocol + +PLAINTEXT_PROTOCOL_ID = TProtocol("/plaintext/1.0.0") class InsecureSession(BaseSession): diff --git a/libp2p/stream_muxer/mplex/mplex.py b/libp2p/stream_muxer/mplex/mplex.py index f154a16..aa64b69 100644 --- a/libp2p/stream_muxer/mplex/mplex.py +++ b/libp2p/stream_muxer/mplex/mplex.py @@ -8,12 +8,15 @@ from libp2p.network.typing import GenericProtocolHandlerFn from libp2p.peer.id import ID from libp2p.security.secure_conn_interface import ISecureConn from libp2p.stream_muxer.abc import IMuxedConn, IMuxedStream +from libp2p.typing import TProtocol from .constants import HeaderTags from .exceptions import StreamNotFound from .mplex_stream import MplexStream from .utils import decode_uvarint_from_stream, encode_uvarint +MPLEX_PROTOCOL_ID = TProtocol("/mplex/6.7.0") + class Mplex(IMuxedConn): """