Change PeerInfo
to remove dep on PeerData
This commit is contained in:
parent
b77834d129
commit
dfd9ebdc5e
|
@ -3,7 +3,6 @@ from typing import List
|
||||||
import multiaddr
|
import multiaddr
|
||||||
|
|
||||||
from .id import ID
|
from .id import ID
|
||||||
from .peerdata import PeerData
|
|
||||||
|
|
||||||
|
|
||||||
class PeerInfo:
|
class PeerInfo:
|
||||||
|
@ -11,9 +10,9 @@ class PeerInfo:
|
||||||
peer_id: ID
|
peer_id: ID
|
||||||
addrs: List[multiaddr.Multiaddr]
|
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.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:
|
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:
|
if len(parts) > 1:
|
||||||
addr = multiaddr.Multiaddr.join(*parts[:-1])
|
addr = multiaddr.Multiaddr.join(*parts[:-1])
|
||||||
|
|
||||||
peer_data = PeerData()
|
return PeerInfo(peer_id, [addr])
|
||||||
peer_data.add_addrs([addr])
|
|
||||||
peer_data.set_protocols([p.code for p in addr.protocols()])
|
|
||||||
|
|
||||||
return PeerInfo(peer_id, peer_data)
|
|
||||||
|
|
||||||
|
|
||||||
class InvalidAddrError(ValueError):
|
class InvalidAddrError(ValueError):
|
||||||
|
|
|
@ -33,7 +33,7 @@ class PeerStore(IPeerStore):
|
||||||
def peer_info(self, peer_id: ID) -> Optional[PeerInfo]:
|
def peer_info(self, peer_id: ID) -> Optional[PeerInfo]:
|
||||||
if peer_id in self.peer_map:
|
if peer_id in self.peer_map:
|
||||||
peer_data = self.peer_map[peer_id]
|
peer_data = self.peer_map[peer_id]
|
||||||
return PeerInfo(peer_id, peer_data)
|
return PeerInfo(peer_id, peer_data.addrs)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def get_protocols(self, peer_id: ID) -> List[str]:
|
def get_protocols(self, peer_id: ID) -> List[str]:
|
||||||
|
|
|
@ -4,7 +4,6 @@ import multiaddr
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from libp2p.peer.id import ID
|
from libp2p.peer.id import ID
|
||||||
from libp2p.peer.peerdata import PeerData
|
|
||||||
from libp2p.peer.peerinfo import InvalidAddrError, PeerInfo, info_from_p2p_addr
|
from libp2p.peer.peerinfo import InvalidAddrError, PeerInfo, info_from_p2p_addr
|
||||||
|
|
||||||
ALPHABETS = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
|
ALPHABETS = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
|
||||||
|
@ -12,24 +11,17 @@ VALID_MULTI_ADDR_STR = "/ip4/127.0.0.1/tcp/8000/p2p/3YgLAeMKSAPcGqZkAt8mREqhQXmJ
|
||||||
|
|
||||||
|
|
||||||
def test_init_():
|
def test_init_():
|
||||||
peer_data = PeerData()
|
|
||||||
random_addrs = [random.randint(0, 255) for r in range(4)]
|
random_addrs = [random.randint(0, 255) for r in range(4)]
|
||||||
peer_data.add_addrs(random_addrs)
|
|
||||||
random_id_string = ""
|
random_id_string = ""
|
||||||
for _ in range(10):
|
for _ in range(10):
|
||||||
random_id_string += random.SystemRandom().choice(ALPHABETS)
|
random_id_string += random.SystemRandom().choice(ALPHABETS)
|
||||||
peer_id = ID(random_id_string.encode())
|
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.peer_id == peer_id
|
||||||
assert peer_info.addrs == random_addrs
|
assert peer_info.addrs == random_addrs
|
||||||
|
|
||||||
|
|
||||||
def test_init_no_value():
|
|
||||||
with pytest.raises(Exception):
|
|
||||||
PeerInfo()
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"addr",
|
"addr",
|
||||||
(
|
(
|
||||||
|
|
Loading…
Reference in New Issue
Block a user