Add some loggings to swarm and cosmetic updates

This commit is contained in:
NIC619 2019-09-14 21:47:49 +08:00
parent 38f4223e62
commit 786a03544c
No known key found for this signature in database
GPG Key ID: 570C35F5C2D51B17
6 changed files with 47 additions and 48 deletions

View File

@ -21,7 +21,7 @@ from .host_interface import IHost
class BasicHost(IHost):
_network: INetwork
router: KadmeliaPeerRouter
_router: KadmeliaPeerRouter
peerstore: IPeerStore
# default options constructor
@ -56,7 +56,7 @@ class BasicHost(IHost):
def get_addrs(self) -> List[multiaddr.Multiaddr]:
"""
:return: all the multiaddr addresses this host is listening too
:return: all the multiaddr addresses this host is listening to
"""
p2p_part = multiaddr.Multiaddr("/p2p/{}".format(self.get_id().pretty()))
@ -68,23 +68,22 @@ class BasicHost(IHost):
def set_stream_handler(
self, protocol_id: TProtocol, stream_handler: StreamHandlerFn
) -> bool:
) -> None:
"""
set stream handler for host
set stream handler for given `protocol_id`
:param protocol_id: protocol id used on stream
:param stream_handler: a stream handler function
:return: true if successful
"""
return self._network.set_stream_handler(protocol_id, stream_handler)
self._network.set_stream_handler(protocol_id, stream_handler)
# protocol_id can be a list of protocol_ids
# stream will decide which protocol_id to run on
# `protocol_ids` can be a list of `protocol_id`
# stream will decide which `protocol_id` to run on
async def new_stream(
self, peer_id: ID, protocol_ids: Sequence[TProtocol]
) -> INetStream:
"""
:param peer_id: peer_id that host is connecting
:param protocol_id: protocol id that stream runs on
:param protocol_ids: available protocol ids to use for stream
:return: stream: new stream created
"""
return await self._network.new_stream(peer_id, protocol_ids)
@ -92,12 +91,11 @@ class BasicHost(IHost):
async def connect(self, peer_info: PeerInfo) -> None:
"""
connect ensures there is a connection between this host and the peer with
given peer_info.peer_id. connect will absorb the addresses in peer_info into its internal
given `peer_info.peer_id`. connect will absorb the addresses in peer_info into its internal
peerstore. If there is not an active connection, connect will issue a
dial, and block until a connection is open, or an error is
returned.
dial, and block until a connection is opened, or an error is returned.
:param peer_info: peer_info of the host we want to connect to
:param peer_info: peer_info of the peer we want to connect to
:type peer_info: peer.peerinfo.PeerInfo
"""
self.peerstore.add_addrs(peer_info.peer_id, peer_info.addrs, 10)

View File

@ -33,18 +33,17 @@ class IHost(ABC):
@abstractmethod
def get_addrs(self) -> List[multiaddr.Multiaddr]:
"""
:return: all the multiaddr addresses this host is listening too
:return: all the multiaddr addresses this host is listening to
"""
@abstractmethod
def set_stream_handler(
self, protocol_id: TProtocol, stream_handler: StreamHandlerFn
) -> bool:
) -> None:
"""
set stream handler for host
:param protocol_id: protocol id used on stream
:param stream_handler: a stream handler function
:return: true if successful
"""
# protocol_id can be a list of protocol_ids
@ -55,7 +54,7 @@ class IHost(ABC):
) -> INetStream:
"""
:param peer_id: peer_id that host is connecting
:param protocol_ids: protocol ids that stream can run on
:param protocol_ids: available protocol ids to use for stream
:return: stream: new stream created
"""
@ -65,10 +64,9 @@ class IHost(ABC):
connect ensures there is a connection between this host and the peer with
given peer_info.peer_id. connect will absorb the addresses in peer_info into its internal
peerstore. If there is not an active connection, connect will issue a
dial, and block until a connection is open, or an error is
returned.
dial, and block until a connection is opened, or an error is returned.
:param peer_info: peer_info of the host we want to connect to
:param peer_info: peer_info of the peer we want to connect to
:type peer_info: peer.peerinfo.PeerInfo
"""

View File

@ -40,11 +40,10 @@ class INetwork(ABC):
@abstractmethod
def set_stream_handler(
self, protocol_id: TProtocol, stream_handler: StreamHandlerFn
) -> bool:
) -> None:
"""
:param protocol_id: protocol id used on stream
:param stream_handler: a stream handler instance
:return: true if successful
"""
@abstractmethod

View File

@ -34,7 +34,6 @@ class NetStream(INetStream):
def set_protocol(self, protocol_id: TProtocol) -> None:
"""
:param protocol_id: protocol id that stream runs on
:return: true if successful
"""
self.protocol_id = protocol_id
@ -64,7 +63,6 @@ class NetStream(INetStream):
async def close(self) -> None:
"""
close stream
:return: true if successful
"""
await self.muxed_stream.close()

View File

@ -16,10 +16,9 @@ class INetStream(ReadWriteCloser):
"""
@abstractmethod
def set_protocol(self, protocol_id: TProtocol) -> bool:
def set_protocol(self, protocol_id: TProtocol) -> None:
"""
:param protocol_id: protocol id that stream runs on
:return: true if successful
"""
@abstractmethod

View File

@ -80,14 +80,12 @@ class Swarm(INetwork):
def set_stream_handler(
self, protocol_id: TProtocol, stream_handler: StreamHandlerFn
) -> bool:
) -> None:
"""
:param protocol_id: protocol id used on stream
:param stream_handler: a stream handler instance
:return: true if successful
"""
self.multiselect.add_handler(protocol_id, stream_handler)
return True
async def dial_peer(self, peer_id: ID) -> IMuxedConn:
"""
@ -128,11 +126,10 @@ class Swarm(INetwork):
try:
secured_conn = await self.upgrader.upgrade_security(raw_conn, peer_id, True)
except SecurityUpgradeFailure as error:
# TODO: Add logging to indicate the failure
error_msg = "fail to upgrade security for peer %s" % peer_id
logger.debug(error_msg)
await raw_conn.close()
raise SwarmException(
f"fail to upgrade the connection to a secured connection from {peer_id}"
) from error
raise SwarmException(error_msg) from error
logger.debug("upgraded security for peer %s", peer_id)
@ -141,11 +138,10 @@ class Swarm(INetwork):
secured_conn, self.generic_protocol_handler, peer_id
)
except MuxerUpgradeFailure as error:
# TODO: Add logging to indicate the failure
error_msg = "fail to upgrade mux for peer %s" % peer_id
logger.debug(error_msg)
await secured_conn.close()
raise SwarmException(
f"fail to upgrade the connection to a muxed connection from {peer_id}"
) from error
raise SwarmException(error_msg) from error
logger.debug("upgraded mux for peer %s", peer_id)
@ -168,6 +164,11 @@ class Swarm(INetwork):
:param protocol_id: protocol id
:return: net stream instance
"""
logger.debug(
"attempting to open a stream to peer %s, over one of the protocols %s",
peer_id,
protocol_ids,
)
muxed_conn = await self.dial_peer(peer_id)
@ -183,6 +184,12 @@ class Swarm(INetwork):
net_stream = NetStream(muxed_stream)
net_stream.set_protocol(selected_protocol)
logger.debug(
"successfully opened a stream to peer %s, over protocol %s",
peer_id,
selected_protocol,
)
# Call notifiers since event occurred
for notifee in self.notifees:
await notifee.opened_stream(self, net_stream)
@ -215,8 +222,6 @@ class Swarm(INetwork):
peer_addr = f"/ip4/{connection_info[0]}/tcp/{connection_info[1]}"
logger.debug("inbound connection at %s", peer_addr)
# logger.debug("inbound connection request", peer_id)
# Upgrade reader/write to a net_stream and pass \
# to appropriate stream handler (using multiaddr)
raw_conn = RawConnection(reader, writer, False)
# Per, https://discuss.libp2p.io/t/multistream-security/130, we first secure
@ -227,11 +232,10 @@ class Swarm(INetwork):
raw_conn, ID(b""), False
)
except SecurityUpgradeFailure as error:
# TODO: Add logging to indicate the failure
error_msg = "fail to upgrade security for peer at %s" % peer_addr
logger.debug(error_msg)
await raw_conn.close()
raise SwarmException(
"fail to upgrade the connection to a secured connection"
) from error
raise SwarmException(error_msg) from error
peer_id = secured_conn.get_remote_peer()
logger.debug("upgraded security for peer at %s", peer_addr)
@ -242,11 +246,10 @@ class Swarm(INetwork):
secured_conn, self.generic_protocol_handler, peer_id
)
except MuxerUpgradeFailure as error:
# TODO: Add logging to indicate the failure
error_msg = "fail to upgrade mux for peer %s" % peer_id
logger.debug(error_msg)
await secured_conn.close()
raise SwarmException(
f"fail to upgrade the connection to a muxed connection from {peer_id}"
) from error
raise SwarmException(error_msg) from error
logger.debug("upgraded mux for peer %s", peer_id)
# Store muxed_conn with peer id
self.connections[peer_id] = muxed_conn
@ -269,7 +272,7 @@ class Swarm(INetwork):
return True
except IOError:
# Failed. Continue looping.
print("Failed to connect to: " + str(maddr))
logger.debug("fail to listen on: " + str(maddr))
# No maddr succeeded
return False
@ -301,6 +304,8 @@ class Swarm(INetwork):
*[connection.close() for connection in self.connections.values()]
)
logger.debug("swarm successfully closed")
async def close_peer(self, peer_id: ID) -> None:
if peer_id not in self.connections:
return
@ -308,6 +313,8 @@ class Swarm(INetwork):
del self.connections[peer_id]
await connection.close()
logger.debug("successfully close the connection to peer %s", peer_id)
def create_generic_protocol_handler(swarm: Swarm) -> GenericProtocolHandlerFn:
"""