Fix the error due to the breaking change

In multiaddr, `split` is moved to `Multiaddr`'s method.
This commit is contained in:
mhchia 2019-07-08 17:13:08 +08:00
parent 0fbf45d8ca
commit 73125b99b0
No known key found for this signature in database
GPG Key ID: 389EFBEA1362589A
3 changed files with 21 additions and 18 deletions

View File

@ -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]

View File

@ -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))

View File

@ -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