Change PeerInfo to remove dep on PeerData

This commit is contained in:
mhchia 2019-09-02 13:52:33 +08:00
parent b77834d129
commit dfd9ebdc5e
No known key found for this signature in database
GPG Key ID: 389EFBEA1362589A
3 changed files with 5 additions and 18 deletions

View File

@ -3,7 +3,6 @@ from typing import List
import multiaddr
from .id import ID
from .peerdata import PeerData
class PeerInfo:
@ -11,9 +10,9 @@ class PeerInfo:
peer_id: ID
addrs: List[multiaddr.Multiaddr]
def __init__(self, peer_id: ID, peer_data: PeerData = None) -> None:
def __init__(self, peer_id: ID, addrs: List[multiaddr.Multiaddr]) -> None:
self.peer_id = peer_id
self.addrs = peer_data.get_addrs() if peer_data else None
self.addrs = addrs
def info_from_p2p_addr(addr: multiaddr.Multiaddr) -> PeerInfo:
@ -44,11 +43,7 @@ def info_from_p2p_addr(addr: multiaddr.Multiaddr) -> PeerInfo:
if len(parts) > 1:
addr = multiaddr.Multiaddr.join(*parts[:-1])
peer_data = PeerData()
peer_data.add_addrs([addr])
peer_data.set_protocols([p.code for p in addr.protocols()])
return PeerInfo(peer_id, peer_data)
return PeerInfo(peer_id, [addr])
class InvalidAddrError(ValueError):

View File

@ -33,7 +33,7 @@ class PeerStore(IPeerStore):
def peer_info(self, peer_id: ID) -> Optional[PeerInfo]:
if peer_id in self.peer_map:
peer_data = self.peer_map[peer_id]
return PeerInfo(peer_id, peer_data)
return PeerInfo(peer_id, peer_data.addrs)
return None
def get_protocols(self, peer_id: ID) -> List[str]:

View File

@ -4,7 +4,6 @@ import multiaddr
import pytest
from libp2p.peer.id import ID
from libp2p.peer.peerdata import PeerData
from libp2p.peer.peerinfo import InvalidAddrError, PeerInfo, info_from_p2p_addr
ALPHABETS = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
@ -12,24 +11,17 @@ VALID_MULTI_ADDR_STR = "/ip4/127.0.0.1/tcp/8000/p2p/3YgLAeMKSAPcGqZkAt8mREqhQXmJ
def test_init_():
peer_data = PeerData()
random_addrs = [random.randint(0, 255) for r in range(4)]
peer_data.add_addrs(random_addrs)
random_id_string = ""
for _ in range(10):
random_id_string += random.SystemRandom().choice(ALPHABETS)
peer_id = ID(random_id_string.encode())
peer_info = PeerInfo(peer_id, peer_data)
peer_info = PeerInfo(peer_id, random_addrs)
assert peer_info.peer_id == peer_id
assert peer_info.addrs == random_addrs
def test_init_no_value():
with pytest.raises(Exception):
PeerInfo()
@pytest.mark.parametrize(
"addr",
(