commit
73dc67050b
@ -1,3 +1,5 @@
|
|||||||
# PeerStore
|
# 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.
|
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.
|
@ -27,5 +27,10 @@ class PeerData(IPeerData):
|
|||||||
|
|
||||||
def get_metadata(self, key):
|
def get_metadata(self, key):
|
||||||
if key in self.metadata:
|
if key in self.metadata:
|
||||||
return self.metadata[key], None
|
return self.metadata[key]
|
||||||
return None, "key not found"
|
else:
|
||||||
|
raise PeerDataError("key not found")
|
||||||
|
|
||||||
|
class PeerDataError(KeyError):
|
||||||
|
"""Raised when a key is not found in peer metadata"""
|
||||||
|
pass
|
||||||
|
@ -35,13 +35,15 @@ class IPeerData(ABC):
|
|||||||
"""
|
"""
|
||||||
:param key: key in KV pair
|
:param key: key in KV pair
|
||||||
:param val: val to associate with key
|
:param val: val to associate with key
|
||||||
|
:raise Exception: unsuccesful put
|
||||||
"""
|
"""
|
||||||
def put_metadata(self, key, val):
|
def put_metadata(self, key, val):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
"""
|
"""
|
||||||
:param key: key in KV pair
|
: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):
|
def get_metadata(self, key):
|
||||||
pass
|
pass
|
||||||
|
4
peer/peerinfo.py
Normal file
4
peer/peerinfo.py
Normal 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()
|
@ -10,7 +10,8 @@ class IPeerMetadata(ABC):
|
|||||||
"""
|
"""
|
||||||
:param peer_id: peer ID to lookup key for
|
:param peer_id: peer ID to lookup key for
|
||||||
:param key: key to look up
|
: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
|
pass
|
||||||
|
|
||||||
@ -20,7 +21,7 @@ class IPeerMetadata(ABC):
|
|||||||
:param peer_id: peer ID to lookup key for
|
:param peer_id: peer ID to lookup key for
|
||||||
:param key: key to associate with peer
|
:param key: key to associate with peer
|
||||||
:param val: value to associated with key
|
:param val: value to associated with key
|
||||||
:return: error
|
:raise Exception: unsuccessful put
|
||||||
"""
|
"""
|
||||||
pass
|
pass
|
||||||
|
|
@ -1,5 +1,6 @@
|
|||||||
from .peerstore_interface import IPeerStore
|
from .peerstore_interface import IPeerStore
|
||||||
from .peerdata import PeerData
|
from .peerdata import PeerData
|
||||||
|
from .peerinfo import PeerInfo
|
||||||
|
|
||||||
class PeerStore(IPeerStore):
|
class PeerStore(IPeerStore):
|
||||||
|
|
||||||
@ -24,16 +25,14 @@ class PeerStore(IPeerStore):
|
|||||||
def peer_info(self, peer_id):
|
def peer_info(self, peer_id):
|
||||||
if peer_id in self.peer_map:
|
if peer_id in self.peer_map:
|
||||||
peer = self.peer_map[peer_id]
|
peer = self.peer_map[peer_id]
|
||||||
return {
|
return PeerInfo(peer_id, peer)
|
||||||
"peer_id": peer_id,
|
|
||||||
"addrs": peer.get_addrs()
|
|
||||||
}
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def get_protocols(self, peer_id):
|
def get_protocols(self, peer_id):
|
||||||
if peer_id in self.peer_map:
|
if peer_id in self.peer_map:
|
||||||
return self.peer_map[peer_id].get_protocols(), None
|
return self.peer_map[peer_id].get_protocols()
|
||||||
return None, peer_id + " not found"
|
else:
|
||||||
|
raise PeerStoreError("peer ID not found")
|
||||||
|
|
||||||
def add_protocols(self, peer_id, protocols):
|
def add_protocols(self, peer_id, protocols):
|
||||||
peer = self.__create_or_get_peer(peer_id)
|
peer = self.__create_or_get_peer(peer_id)
|
||||||
@ -44,9 +43,10 @@ class PeerStore(IPeerStore):
|
|||||||
|
|
||||||
def get(self, peer_id, key):
|
def get(self, peer_id, key):
|
||||||
if peer_id in self.peer_map:
|
if peer_id in self.peer_map:
|
||||||
val, error = self.peer_map[peer_id].get_metadata(key)
|
val = self.peer_map[peer_id].get_metadata(key)
|
||||||
return val, error
|
return val
|
||||||
return None, peer_id + " not found"
|
else:
|
||||||
|
raise PeerStoreError("peer ID not found")
|
||||||
|
|
||||||
def put(self, peer_id, key, val):
|
def put(self, peer_id, key, val):
|
||||||
# <<?>>
|
# <<?>>
|
||||||
@ -64,8 +64,9 @@ class PeerStore(IPeerStore):
|
|||||||
|
|
||||||
def addrs(self, peer_id):
|
def addrs(self, peer_id):
|
||||||
if peer_id in self.peer_map:
|
if peer_id in self.peer_map:
|
||||||
return self.peer_map[peer_id].get_addrs(), None
|
return self.peer_map[peer_id].get_addrs()
|
||||||
return None, peer_id + " not found"
|
else:
|
||||||
|
raise PeerStoreError("peer ID not found")
|
||||||
|
|
||||||
def clear_addrs(self, peer_id):
|
def clear_addrs(self, peer_id):
|
||||||
# Only clear addresses if the peer is in peer map
|
# 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:
|
if len(self.peer_map[key].get_addrs()) >= 1:
|
||||||
output.append(key)
|
output.append(key)
|
||||||
return output
|
return output
|
||||||
|
|
||||||
|
class PeerStoreError(KeyError):
|
||||||
|
"""Raised when peer ID is not found in peer store"""
|
||||||
|
pass
|
||||||
|
@ -20,7 +20,8 @@ class IPeerStore(ABC, IAddrBook, IPeerMetadata):
|
|||||||
def get_protocols(self, peer_id):
|
def get_protocols(self, peer_id):
|
||||||
"""
|
"""
|
||||||
:param peer_id: peer ID to get protocols for
|
: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
|
pass
|
||||||
|
|
||||||
@ -29,7 +30,7 @@ class IPeerStore(ABC, IAddrBook, IPeerMetadata):
|
|||||||
"""
|
"""
|
||||||
:param peer_id: peer ID to add protocols for
|
:param peer_id: peer ID to add protocols for
|
||||||
:param protocols: protocols to add
|
:param protocols: protocols to add
|
||||||
:return: error
|
:raise Exception: peer ID not found
|
||||||
"""
|
"""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@ -38,7 +39,7 @@ class IPeerStore(ABC, IAddrBook, IPeerMetadata):
|
|||||||
"""
|
"""
|
||||||
:param peer_id: peer ID to set protocols for
|
:param peer_id: peer ID to set protocols for
|
||||||
:param protocols: protocols to set
|
:param protocols: protocols to set
|
||||||
:return: error
|
:raise Exception: peer ID not found
|
||||||
"""
|
"""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user