diff --git a/libp2p/peer/peerinfo.py b/libp2p/peer/peerinfo.py index 2e7edb9..e173066 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 of type `Multiaddr`") + + parts = addr.split() if not parts: - raise InvalidAddrError() + raise InvalidAddrError(f"`parts`={parts} should at least have a protocol `P_P2P`") 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/setup.py b/setup.py index 452ea3b..4807b5b 100644 --- a/setup.py +++ b/setup.py @@ -15,16 +15,16 @@ setuptools.setup( platforms=["unix", "linux", "osx"], classifiers=classifiers, install_requires=[ - "pycryptodome", - "click", - "base58", - "pymultihash", - "multiaddr==0.0.4", - "rpcudp", - "grpcio", - "grpcio-tools", + "pycryptodome>=3.8.2,<4.0.0", + "click>=7.0,<8.0", + "base58>=1.0.3,<2.0.0", + "pymultihash>=0.8.2", + "multiaddr>=0.0.8,<0.1.0", + "rpcudp>=3.0.0,<4.0.0", + "grpcio>=1.21.1,<2.0.0", + "grpcio-tools>=1.21.1,<2.0.0", "lru-dict>=1.1.6", - "aio_timers" + "aio_timers>=0.0.1,<0.1.0", ], packages=setuptools.find_packages(exclude=["tests", "tests.*"]), zip_safe=False, diff --git a/tests/peer/test_peerinfo.py b/tests/peer/test_peerinfo.py index 30f5e50..75a78ce 100644 --- a/tests/peer/test_peerinfo.py +++ b/tests/peer/test_peerinfo.py @@ -1,12 +1,17 @@ import random -import multiaddr + import pytest + +import multiaddr + from libp2p.peer.peerinfo import PeerInfo, info_from_p2p_addr, InvalidAddrError from libp2p.peer.peerdata import PeerData from libp2p.peer.id import ID + ALPHABETS = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz' + def test_init_(): peer_data = PeerData() random_addrs = [random.randint(0, 255) for r in range(4)] @@ -20,22 +25,30 @@ def test_init_(): assert peer_info.peer_id == peer_id assert peer_info.addrs == random_addrs + 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(): - with pytest.raises(InvalidAddrError): - 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) +@pytest.mark.parametrize( + 'addr', + ( + pytest.param(None), + pytest.param(random.randint(0, 255), id='random integer'), + pytest.param(multiaddr.Multiaddr('/'), id='empty multiaddr'), + pytest.param( + multiaddr.Multiaddr('/ip4/127.0.0.1'), id='multiaddr without peer_id(p2p protocol)' + ), + ) +) +def test_info_from_p2p_addr_invalid(addr): with pytest.raises(InvalidAddrError): - info_from_p2p_addr(random_addrs) + info_from_p2p_addr(addr) -def test_info_from_p2p_addr(): + +def test_info_from_p2p_addr_valid(): # pylint: disable=line-too-long m_addr = multiaddr.Multiaddr('/ip4/127.0.0.1/tcp/8000/p2p/3YgLAeMKSAPcGqZkAt8mREqhQXmJT8SN8VCMN4T6ih4GNX9wvK8mWJnWZ1qA2mLdCQ') info = info_from_p2p_addr(m_addr)