Merge pull request #27 from zixuanzh/peer-info

Peer info
This commit is contained in:
ZX 2018-10-30 00:07:08 +01:00 committed by GitHub
commit 73dc67050b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 39 additions and 19 deletions

View File

@ -1,3 +1,5 @@
# PeerStore
The PeerStore contains a mapping of peer IDs to PeerData objects. Each PeerData object represents a peer, and each PeerData contains a collection of protocols, addresses, and a mapping of metadata. PeerStore implements the IPeerStore (peer protocols), IAddrBook (address book), and IPeerMetadata (peer metadata) interfaces, which allows the peer store to effectively function as a dictionary for peer ID to protocol, address, and metadata.
Note: PeerInfo represents a read-only summary of a PeerData object. Only the attributes assigned in PeerInfo are readable by references to PeerInfo objects.

View File

@ -27,5 +27,10 @@ class PeerData(IPeerData):
def get_metadata(self, key):
if key in self.metadata:
return self.metadata[key], None
return None, "key not found"
return self.metadata[key]
else:
raise PeerDataError("key not found")
class PeerDataError(KeyError):
"""Raised when a key is not found in peer metadata"""
pass

View File

@ -35,13 +35,15 @@ class IPeerData(ABC):
"""
:param key: key in KV pair
:param val: val to associate with key
:raise Exception: unsuccesful put
"""
def put_metadata(self, key, val):
pass
"""
:param key: key in KV pair
:return: val for key, error (only defined if key not found)
:return: val for key
:raise Exception: key not found
"""
def get_metadata(self, key):
pass

4
peer/peerinfo.py Normal file
View File

@ -0,0 +1,4 @@
class PeerInfo:
def __init__(self, peer_id, peer_data):
self.peer_id = peer_id
self.addrs = peer_data.get_addrs()

View File

@ -10,7 +10,8 @@ class IPeerMetadata(ABC):
"""
:param peer_id: peer ID to lookup key for
:param key: key to look up
:return: value at key for given peer, error
:return: value at key for given peer
:raise Exception: peer ID not found
"""
pass
@ -20,7 +21,7 @@ class IPeerMetadata(ABC):
:param peer_id: peer ID to lookup key for
:param key: key to associate with peer
:param val: value to associated with key
:return: error
:raise Exception: unsuccessful put
"""
pass

View File

@ -1,5 +1,6 @@
from .peerstore_interface import IPeerStore
from .peerdata import PeerData
from .peerinfo import PeerInfo
class PeerStore(IPeerStore):
@ -24,16 +25,14 @@ class PeerStore(IPeerStore):
def peer_info(self, peer_id):
if peer_id in self.peer_map:
peer = self.peer_map[peer_id]
return {
"peer_id": peer_id,
"addrs": peer.get_addrs()
}
return PeerInfo(peer_id, peer)
return None
def get_protocols(self, peer_id):
if peer_id in self.peer_map:
return self.peer_map[peer_id].get_protocols(), None
return None, peer_id + " not found"
return self.peer_map[peer_id].get_protocols()
else:
raise PeerStoreError("peer ID not found")
def add_protocols(self, peer_id, protocols):
peer = self.__create_or_get_peer(peer_id)
@ -44,9 +43,10 @@ class PeerStore(IPeerStore):
def get(self, peer_id, key):
if peer_id in self.peer_map:
val, error = self.peer_map[peer_id].get_metadata(key)
return val, error
return None, peer_id + " not found"
val = self.peer_map[peer_id].get_metadata(key)
return val
else:
raise PeerStoreError("peer ID not found")
def put(self, peer_id, key, val):
# <<?>>
@ -64,8 +64,9 @@ class PeerStore(IPeerStore):
def addrs(self, peer_id):
if peer_id in self.peer_map:
return self.peer_map[peer_id].get_addrs(), None
return None, peer_id + " not found"
return self.peer_map[peer_id].get_addrs()
else:
raise PeerStoreError("peer ID not found")
def clear_addrs(self, peer_id):
# Only clear addresses if the peer is in peer map
@ -80,3 +81,7 @@ class PeerStore(IPeerStore):
if len(self.peer_map[key].get_addrs()) >= 1:
output.append(key)
return output
class PeerStoreError(KeyError):
"""Raised when peer ID is not found in peer store"""
pass

View File

@ -20,7 +20,8 @@ class IPeerStore(ABC, IAddrBook, IPeerMetadata):
def get_protocols(self, peer_id):
"""
:param peer_id: peer ID to get protocols for
:return: protocols (as strings), error
:return: protocols (as strings)
:raise Exception: peer ID not found exception
"""
pass
@ -29,7 +30,7 @@ class IPeerStore(ABC, IAddrBook, IPeerMetadata):
"""
:param peer_id: peer ID to add protocols for
:param protocols: protocols to add
:return: error
:raise Exception: peer ID not found
"""
pass
@ -38,7 +39,7 @@ class IPeerStore(ABC, IAddrBook, IPeerMetadata):
"""
:param peer_id: peer ID to set protocols for
:param protocols: protocols to set
:return: error
:raise Exception: peer ID not found
"""
pass