diff --git a/libp2p/peer/peerinfo.py b/libp2p/peer/peerinfo.py index 2e7edb9..bbc3eb1 100644 --- a/libp2p/peer/peerinfo.py +++ b/libp2p/peer/peerinfo.py @@ -1,5 +1,4 @@ import multiaddr -import multiaddr.util from .id import id_b58_decode from .peerdata import PeerData @@ -12,17 +11,23 @@ class PeerInfo: self.addrs = peer_data.get_addrs() if peer_data else None -def info_from_p2p_addr(addr): +def info_from_p2p_addr(addr: multiaddr.Multiaddr) -> PeerInfo: if not addr: - raise InvalidAddrError() + raise InvalidAddrError("addr should not be None") - parts = multiaddr.util.split(addr) + if not isinstance(addr, multiaddr.Multiaddr): + raise InvalidAddrError(f"addr={addr} should be in type Multiaddr") + + parts = addr.split() if not parts: raise InvalidAddrError() p2p_part = parts[-1] - if p2p_part.protocols()[0].code != multiaddr.protocols.P_P2P: - raise InvalidAddrError() + last_protocol_code = p2p_part.protocols()[0].code + if last_protocol_code != multiaddr.protocols.P_P2P: + raise InvalidAddrError( + f"the last protocol should be P_P2P instead of {last_protocol_code}" + ) # make sure the /p2p value parses as a peer.ID peer_id_str = p2p_part.value_for_protocol(multiaddr.protocols.P_P2P) @@ -30,7 +35,7 @@ def info_from_p2p_addr(addr): # we might have received just an / p2p part, which means there's no addr. if len(parts) > 1: - addr = multiaddr.util.join(parts[:-1]) + addr = multiaddr.Multiaddr.join(*parts[:-1]) peer_data = PeerData() peer_data.addrs = [addr] diff --git a/libp2p/transport/tcp/tcp.py b/libp2p/transport/tcp/tcp.py index f99b78c..bcbb533 100644 --- a/libp2p/transport/tcp/tcp.py +++ b/libp2p/transport/tcp/tcp.py @@ -27,13 +27,11 @@ class TCP(ITransport): :param maddr: maddr of peer :return: return True if successful """ - _multiaddr = maddr - _multiaddr = _multiaddr.decapsulate('/p2p') - - coroutine = asyncio.start_server(self.handler, - _multiaddr.value_for_protocol('ip4'), - _multiaddr.value_for_protocol('tcp')) - self.server = await coroutine + self.server = await asyncio.start_server( + self.handler, + maddr.value_for_protocol('ip4'), + maddr.value_for_protocol('tcp'), + ) socket = self.server.sockets[0] self.multiaddrs.append(_multiaddr_from_socket(socket)) diff --git a/tests/peer/test_peerinfo.py b/tests/peer/test_peerinfo.py index 30f5e50..ea86158 100644 --- a/tests/peer/test_peerinfo.py +++ b/tests/peer/test_peerinfo.py @@ -22,7 +22,7 @@ def test_init_(): def test_init_no_value(): with pytest.raises(Exception) as _: - #pylint: disable=no-value-for-parameter + # pylint: disable=no-value-for-parameter PeerInfo() def test_invalid_addr_1(): @@ -30,10 +30,10 @@ def test_invalid_addr_1(): info_from_p2p_addr(None) def test_invalid_addr_2(monkeypatch): - random_addrs = [random.randint(0, 255) for r in range(4)] - monkeypatch.setattr("multiaddr.util.split", lambda x: None) + random_addr = random.randint(0, 255) + monkeypatch.setattr("multiaddr.Multiaddr.split", lambda x: None) with pytest.raises(InvalidAddrError): - info_from_p2p_addr(random_addrs) + info_from_p2p_addr(random_addr) def test_info_from_p2p_addr(): # pylint: disable=line-too-long