Lint peer classes

This commit is contained in:
Stuckinaboot 2018-10-21 15:15:53 -04:00
parent 88b7eda256
commit 7d124bb047
7 changed files with 173 additions and 181 deletions

View File

@ -6,40 +6,40 @@ class IAddrBook(ABC):
self.context = context self.context = context
@abstractmethod @abstractmethod
def add_addr(self, peerID, addr, ttl): def add_addr(self, peer_id, addr, ttl):
""" """
Calls add_addrs(peerID, [addr], ttl) Calls add_addrs(peer_id, [addr], ttl)
:param peerID: the peer to add address for :param peer_id: the peer to add address for
:param addr: multiaddress of the peer :param addr: multiaddress of the peer
:param ttl: time-to-live for the address (after this time, address is no longer valid) :param ttl: time-to-live for the address (after this time, address is no longer valid)
""" """
pass pass
@abstractmethod @abstractmethod
def add_addrs(self, peerID, addrs []ma.Multiaddr, ttl time.Duration): def add_addrs(self, peer_id, addrs, ttl):
""" """
Adds addresses for a given peer all with the same time-to-live. If one of the Adds addresses for a given peer all with the same time-to-live. If one of the
addresses already exists for the peer and has a longer TTL, no operation should take place. addresses already exists for the peer and has a longer TTL, no operation should take place.
If one of the addresses exists with a shorter TTL, extend the TTL to equal param ttl. If one of the addresses exists with a shorter TTL, extend the TTL to equal param ttl.
:param peerID: the peer to add address for :param peer_id: the peer to add address for
:param addr: multiaddresses of the peer :param addr: multiaddresses of the peer
:param ttl: time-to-live for the address (after this time, address is no longer valid :param ttl: time-to-live for the address (after this time, address is no longer valid
""" """
pass pass
@abstractmethod @abstractmethod
def addrs(self, peerID): def addrs(self, peer_id):
""" """
:param peerID: peer to get addresses of :param peer_id: peer to get addresses of
:return: all known (and valid) addresses for the given peer :return: all known (and valid) addresses for the given peer
""" """
pass pass
@abstractmethod @abstractmethod
def clear_addrs(self, peerID): def clear_addrs(self, peer_id):
""" """
Removes all previously stored addresses Removes all previously stored addresses
:param peerID: peer to remove addresses of :param peer_id: peer to remove addresses of
""" """
pass pass
@ -49,3 +49,4 @@ class IAddrBook(ABC):
:return: all of the peer IDs stored with addresses :return: all of the peer IDs stored with addresses
""" """
pass pass

View File

View File

@ -2,7 +2,7 @@ from .peerdata_interface import IPeerData
class PeerData(IPeerData): class PeerData(IPeerData):
def __init__(self, context): def __init__(self):
self.metadata = {} self.metadata = {}
self.protocols = [] self.protocols = []
self.addrs = [] self.addrs = []
@ -28,5 +28,4 @@ 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], None
else:
return None, "key not found" return None, "key not found"

View File

@ -2,9 +2,6 @@ from abc import ABC, abstractmethod
class IPeerData(ABC): class IPeerData(ABC):
def __init__(self, context):
self.context = context
""" """
:return: all protocols associated with given peer :return: all protocols associated with given peer
""" """
@ -48,4 +45,3 @@ class IPeerData(ABC):
""" """
def get_metadata(self, key): def get_metadata(self, key):
pass pass

View File

@ -6,20 +6,21 @@ class IPeerMetadata(ABC):
self.context = context self.context = context
@abstractmethod @abstractmethod
def get(self, peerID, key): def get(self, peer_id, key):
""" """
:param peerID: 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, error
""" """
pass pass
@abstractmethod @abstractmethod
def put(self, peerID, key, val): def put(self, peer_id, key, val):
""" """
:param peerID: 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 :return: error
""" """
pass pass

View File

@ -1,88 +1,82 @@
from .peerstore_interface import IPeerStore from .peerstore_interface import IPeerStore
import asyncio from .peerdata import PeerData
class PeerStore(IPeerStore): class PeerStore(IPeerStore):
def __init__(self, context): def __init__(self, context):
self.context = context IPeerStore.__init__(self, context)
self.peerMap = {} self.peer_map = {}
def peer_info(self, peerID): def __create_or_get_peer(self, peer_id):
if peerID in self.peerMap: """
peer = self.peerMap[peerID] 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
:param peer_id: peer ID
:return: peer data
"""
if peer_id in self.peer_map:
return self.peer_map[peer_id]
data = PeerData()
self.peer_map[peer_id] = data
return self.peer_map[peer_id]
def peer_info(self, peer_id):
if peer_id in self.peer_map:
peer = self.peer_map[peer_id]
return { return {
"peerID": peerID, "peer_id": peer_id,
"addrs": peer.get_addrs() "addrs": peer.get_addrs()
} }
else:
return None return None
def get_protocols(self, peerID): def get_protocols(self, peer_id):
if peerID in self.peerMap: if peer_id in self.peer_map:
return self.peerMap[peerId].get_protocols(), None return self.peer_map[peer_id].get_protocols(), None
else: return None, peer_id + " not found"
return None, peerID + " not found"
def add_protocols(self, peerID, protocols): def add_protocols(self, peer_id, protocols):
peer = __create_or_get_peer(peerID) peer = self.__create_or_get_peer(peer_id)
peer.add_protocols(protocols) peer.add_protocols(protocols)
def peers(self): def peers(self):
return self.peerMap.keys() return self.peer_map.keys()
def get(self, peerID, key): def get(self, peer_id, key):
if peerID in self.peerMap: if peer_id in self.peer_map:
val, error = self.peerMap[peerID].get_metadata(key) val, error = self.peer_map[peer_id].get_metadata(key)
return val, error return val, error
else: return None, peer_id + " not found"
return None, peerID + " not found"
def put(self, peerID, key, val): def put(self, peer_id, key, val):
# <<?>> # <<?>>
# This can output an error, not sure what the possible errors are # This can output an error, not sure what the possible errors are
peer = __create_or_get_peer(peerID) peer = self.__create_or_get_peer(peer_id)
self.peerMap[peerID].put_metadata(key, val) peer.put_metadata(key, val)
def add_addr(self, peerID, addr, ttl): def add_addr(self, peer_id, addr, ttl):
self.add_addrs(self, peerID, [addr]) self.add_addrs(self, peer_id, [addr])
def add_addrs(self, peerID, addrs, ttl): def add_addrs(self, peer_id, addrs, ttl):
# Ignore ttl for now # Ignore ttl for now
peer = __create_or_get_peer(peerID) peer = self.__create_or_get_peer(peer_id)
peer.add_addrs(addrs) peer.add_addrs(addrs)
def addrs(self, peerID): def addrs(self, peer_id):
if peerID in self.peerMap: if peer_id in self.peer_map:
return self.peerMap[peerId].get_addrs(), None return self.peer_map[peer_id].get_addrs(), None
else: return None, peer_id + " not found"
return None, peerID + " not found"
pass
def clear_addrs(self, peerID): 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
if peerID in self.peerMap: if peer_id in self.peer_map:
self.peerMap[peerID].clear_addrs() self.peer_map[peer_id].clear_addrs()
def peers_with_addrs(self): def peers_with_addrs(self):
# Add all peers with addrs at least 1 to output # Add all peers with addrs at least 1 to output
output = [] output = []
for key in self.peerMap: for key in self.peer_map:
if len(self.peerMap[key].get_addrs()) >= 1: if len(self.peer_map[key].get_addrs()) >= 1:
output.append(key) output.append(key)
return output return output
def __create_or_get_peer(self, peerID):
"""
Returns the peer data for peerID or creates a new
peer data (and stores it in peerMap) if peer
data for peerID does not yet exist
:param peerID: peer ID
:return: peer data
"""
if peerId in self.peerMap:
return self.peerMap[peerId]
else:
data = PeerData(self.context)
self.peerMap[peerId] = data
return self.peerMap[peerId]

View File

@ -1,41 +1,42 @@
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from .addrbook_interace import IAddrBook from .addrbook_interface import IAddrBook
from .peermetadata_interface import IPeerMetadata from .peermetadata_interface import IPeerMetadata
class IPeerStore(ABC, IAddrBook, IPeerMetadata): class IPeerStore(ABC, IAddrBook, IPeerMetadata):
def __init__(self, context): def __init__(self, context):
self.context = context IPeerMetadata.__init__(self, context)
IAddrBook.__init__(self, context)
@abstractmethod @abstractmethod
def peer_info(self, peerID): def peer_info(self, peer_id):
""" """
:param peerID: peer ID to get info for :param peer_id: peer ID to get info for
:return: peer info object :return: peer info object
""" """
pass pass
@abstractmethod @abstractmethod
def get_protocols(self, peerID): def get_protocols(self, peer_id):
""" """
:param peerID: peer ID to get protocols for :param peer_id: peer ID to get protocols for
:return: protocols (as strings), error :return: protocols (as strings), error
""" """
pass pass
@abstractmethod @abstractmethod
def add_protocols(self, peerID, protocols): def add_protocols(self, peer_id, protocols):
""" """
:param peerID: 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 :return: error
""" """
pass pass
@abstractmethod @abstractmethod
def set_protocols(self, peerID, protocols): def set_protocols(self, peer_id, protocols):
""" """
:param peerID: 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 :return: error
""" """