Rename peer_map to peer_data_map

This commit is contained in:
NIC619 2019-11-24 16:52:46 +08:00
parent bcd7890124
commit 9837f30698
No known key found for this signature in database
GPG Key ID: 570C35F5C2D51B17

View File

@ -10,35 +10,35 @@ from .peerstore_interface import IPeerStore
class PeerStore(IPeerStore): class PeerStore(IPeerStore):
peer_map: Dict[ID, PeerData] peer_data_map: Dict[ID, PeerData]
def __init__(self) -> None: def __init__(self) -> None:
IPeerStore.__init__(self) IPeerStore.__init__(self)
self.peer_map = {} self.peer_data_map = {}
def __create_or_get_peer(self, peer_id: ID) -> PeerData: def __create_or_get_peer(self, peer_id: ID) -> PeerData:
""" """
Returns the peer data for peer_id or creates a new peer data (and Returns the peer data for peer_id or creates a new peer data (and
stores it in peer_map) if peer data for peer_id does not yet exist. stores it in peer_data_map) if peer data for peer_id does not yet exist.
:param peer_id: peer ID :param peer_id: peer ID
:return: peer data :return: peer data
""" """
if peer_id in self.peer_map: if peer_id in self.peer_data_map:
return self.peer_map[peer_id] return self.peer_data_map[peer_id]
data = PeerData() data = PeerData()
self.peer_map[peer_id] = data self.peer_data_map[peer_id] = data
return self.peer_map[peer_id] return self.peer_data_map[peer_id]
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_data_map:
peer_data = self.peer_map[peer_id] peer_data = self.peer_data_map[peer_id]
return PeerInfo(peer_id, peer_data.addrs) 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]:
if peer_id in self.peer_map: if peer_id in self.peer_data_map:
return self.peer_map[peer_id].get_protocols() return self.peer_data_map[peer_id].get_protocols()
raise PeerStoreError("peer ID not found") raise PeerStoreError("peer ID not found")
def add_protocols(self, peer_id: ID, protocols: Sequence[str]) -> None: def add_protocols(self, peer_id: ID, protocols: Sequence[str]) -> None:
@ -50,12 +50,12 @@ class PeerStore(IPeerStore):
peer.set_protocols(list(protocols)) peer.set_protocols(list(protocols))
def peer_ids(self) -> List[ID]: def peer_ids(self) -> List[ID]:
return list(self.peer_map.keys()) return list(self.peer_data_map.keys())
def get(self, peer_id: ID, key: str) -> Any: def get(self, peer_id: ID, key: str) -> Any:
if peer_id in self.peer_map: if peer_id in self.peer_data_map:
try: try:
val = self.peer_map[peer_id].get_metadata(key) val = self.peer_data_map[peer_id].get_metadata(key)
except PeerDataError as error: except PeerDataError as error:
raise PeerStoreError(error) raise PeerStoreError(error)
return val return val
@ -76,21 +76,21 @@ class PeerStore(IPeerStore):
peer.add_addrs(list(addrs)) peer.add_addrs(list(addrs))
def addrs(self, peer_id: ID) -> List[Multiaddr]: def addrs(self, peer_id: ID) -> List[Multiaddr]:
if peer_id in self.peer_map: if peer_id in self.peer_data_map:
return self.peer_map[peer_id].get_addrs() return self.peer_data_map[peer_id].get_addrs()
raise PeerStoreError("peer ID not found") raise PeerStoreError("peer ID not found")
def clear_addrs(self, peer_id: ID) -> None: def clear_addrs(self, peer_id: ID) -> None:
# Only clear addresses if the peer is in peer map # Only clear addresses if the peer is in peer map
if peer_id in self.peer_map: if peer_id in self.peer_data_map:
self.peer_map[peer_id].clear_addrs() self.peer_data_map[peer_id].clear_addrs()
def peers_with_addrs(self) -> List[ID]: def peers_with_addrs(self) -> List[ID]:
# Add all peers with addrs at least 1 to output # Add all peers with addrs at least 1 to output
output: List[ID] = [] output: List[ID] = []
for peer_id in self.peer_map: for peer_id in self.peer_data_map:
if len(self.peer_map[peer_id].get_addrs()) >= 1: if len(self.peer_data_map[peer_id].get_addrs()) >= 1:
output.append(peer_id) output.append(peer_id)
return output return output