Lint peer classes
This commit is contained in:
parent
88b7eda256
commit
7d124bb047
|
@ -2,50 +2,51 @@ from abc import ABC, abstractmethod
|
|||
|
||||
class IAddrBook(ABC):
|
||||
|
||||
def __init__(self, context):
|
||||
def __init__(self, context):
|
||||
self.context = context
|
||||
|
||||
@abstractmethod
|
||||
def add_addr(self, peerID, addr, ttl):
|
||||
"""
|
||||
Calls add_addrs(peerID, [addr], ttl)
|
||||
:param peerID: the peer to add address for
|
||||
:param addr: multiaddress of the peer
|
||||
:param ttl: time-to-live for the address (after this time, address is no longer valid)
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def add_addrs(self, peerID, addrs []ma.Multiaddr, ttl time.Duration):
|
||||
"""
|
||||
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.
|
||||
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 addr: multiaddresses of the peer
|
||||
:param ttl: time-to-live for the address (after this time, address is no longer valid
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def addrs(self, peerID):
|
||||
"""
|
||||
:param peerID: peer to get addresses of
|
||||
:return: all known (and valid) addresses for the given peer
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def clear_addrs(self, peerID):
|
||||
"""
|
||||
Removes all previously stored addresses
|
||||
:param peerID: peer to remove addresses of
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def peers_with_addrs(self):
|
||||
"""
|
||||
:return: all of the peer IDs stored with addresses
|
||||
@abstractmethod
|
||||
def add_addr(self, peer_id, addr, ttl):
|
||||
"""
|
||||
pass
|
||||
Calls add_addrs(peer_id, [addr], ttl)
|
||||
:param peer_id: the peer to add address for
|
||||
:param addr: multiaddress of the peer
|
||||
:param ttl: time-to-live for the address (after this time, address is no longer valid)
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
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
|
||||
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.
|
||||
:param peer_id: the peer to add address for
|
||||
:param addr: multiaddresses of the peer
|
||||
:param ttl: time-to-live for the address (after this time, address is no longer valid
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def addrs(self, peer_id):
|
||||
"""
|
||||
:param peer_id: peer to get addresses of
|
||||
:return: all known (and valid) addresses for the given peer
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def clear_addrs(self, peer_id):
|
||||
"""
|
||||
Removes all previously stored addresses
|
||||
:param peer_id: peer to remove addresses of
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def peers_with_addrs(self):
|
||||
"""
|
||||
:return: all of the peer IDs stored with addresses
|
||||
"""
|
||||
pass
|
||||
|
|
@ -2,31 +2,30 @@ from .peerdata_interface import IPeerData
|
|||
|
||||
class PeerData(IPeerData):
|
||||
|
||||
def __init__(self, context):
|
||||
self.metadata = {}
|
||||
def __init__(self):
|
||||
self.metadata = {}
|
||||
self.protocols = []
|
||||
self.addrs = []
|
||||
|
||||
def get_protocols(self):
|
||||
return self.protocols
|
||||
return self.protocols
|
||||
|
||||
def add_protocols(self, protocols):
|
||||
self.protocols.extend(protocols)
|
||||
|
||||
def add_addrs(self, addrs):
|
||||
self.addrs.extend(addrs)
|
||||
self.addrs.extend(addrs)
|
||||
|
||||
def get_addrs(self):
|
||||
return self.addrs
|
||||
return self.addrs
|
||||
|
||||
def clear_addrs(self):
|
||||
self.addrs = []
|
||||
self.addrs = []
|
||||
|
||||
def put_metadata(self, key, val):
|
||||
self.metadata[key] = val
|
||||
self.metadata[key] = val
|
||||
|
||||
def get_metadata(self, key):
|
||||
if key in self.metadata:
|
||||
if key in self.metadata:
|
||||
return self.metadata[key], None
|
||||
else:
|
||||
return None, "key not found"
|
||||
return None, "key not found"
|
||||
|
|
|
@ -2,50 +2,46 @@ from abc import ABC, abstractmethod
|
|||
|
||||
class IPeerData(ABC):
|
||||
|
||||
def __init__(self, context):
|
||||
self.context = context
|
||||
|
||||
"""
|
||||
:return: all protocols associated with given peer
|
||||
"""
|
||||
:return: all protocols associated with given peer
|
||||
"""
|
||||
def get_protocols(self):
|
||||
pass
|
||||
pass
|
||||
|
||||
"""
|
||||
:param protocols: protocols to add
|
||||
"""
|
||||
"""
|
||||
def add_protocols(self, protocols):
|
||||
pass
|
||||
pass
|
||||
|
||||
"""
|
||||
:param addrs: multiaddresses to add
|
||||
"""
|
||||
"""
|
||||
def add_addrs(self, addrs):
|
||||
pass
|
||||
pass
|
||||
|
||||
"""
|
||||
:return: all multiaddresses
|
||||
"""
|
||||
"""
|
||||
def get_addrs(self):
|
||||
pass
|
||||
pass
|
||||
|
||||
"""
|
||||
Clear all addresses
|
||||
"""
|
||||
def clear_addrs(self):
|
||||
pass
|
||||
pass
|
||||
|
||||
"""
|
||||
:param key: key in KV pair
|
||||
:param val: val to associate with key
|
||||
"""
|
||||
def put_metadata(self, key, val):
|
||||
pass
|
||||
pass
|
||||
|
||||
"""
|
||||
:param key: key in KV pair
|
||||
:return: val for key, error (only defined if key not found)
|
||||
"""
|
||||
def get_metadata(self, key):
|
||||
pass
|
||||
|
||||
pass
|
||||
|
|
|
@ -2,24 +2,25 @@ from abc import ABC, abstractmethod
|
|||
|
||||
class IPeerMetadata(ABC):
|
||||
|
||||
def __init__(self, context):
|
||||
def __init__(self, context):
|
||||
self.context = context
|
||||
|
||||
@abstractmethod
|
||||
def get(self, peerID, key):
|
||||
"""
|
||||
:param peerID: peer ID to lookup key for
|
||||
:param key: key to look up
|
||||
@abstractmethod
|
||||
def get(self, peer_id, key):
|
||||
"""
|
||||
:param peer_id: peer ID to lookup key for
|
||||
:param key: key to look up
|
||||
:return: value at key for given peer, error
|
||||
"""
|
||||
pass
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def put(self, peerID, key, val):
|
||||
"""
|
||||
:param peerID: peer ID to lookup key for
|
||||
:param key: key to associate with peer
|
||||
:param val: value to associated with key
|
||||
@abstractmethod
|
||||
def put(self, peer_id, key, val):
|
||||
"""
|
||||
: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
|
||||
"""
|
||||
pass
|
||||
pass
|
||||
|
|
@ -1,88 +1,82 @@
|
|||
from .peerstore_interface import IPeerStore
|
||||
import asyncio
|
||||
from .peerdata import PeerData
|
||||
|
||||
class PeerStore(IPeerStore):
|
||||
|
||||
def __init__(self, context):
|
||||
self.context = context
|
||||
self.peerMap = {}
|
||||
IPeerStore.__init__(self, context)
|
||||
self.peer_map = {}
|
||||
|
||||
def peer_info(self, peerID):
|
||||
if peerID in self.peerMap:
|
||||
peer = self.peerMap[peerID]
|
||||
return {
|
||||
"peerID": peerID,
|
||||
"addrs": peer.get_addrs()
|
||||
}
|
||||
else:
|
||||
return None
|
||||
def __create_or_get_peer(self, peer_id):
|
||||
"""
|
||||
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 get_protocols(self, peerID):
|
||||
if peerID in self.peerMap:
|
||||
return self.peerMap[peerId].get_protocols(), None
|
||||
else:
|
||||
return None, peerID + " not found"
|
||||
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 None
|
||||
|
||||
def add_protocols(self, peerID, protocols):
|
||||
peer = __create_or_get_peer(peerID)
|
||||
peer.add_protocols(protocols)
|
||||
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"
|
||||
|
||||
def peers(self):
|
||||
return self.peerMap.keys()
|
||||
def add_protocols(self, peer_id, protocols):
|
||||
peer = self.__create_or_get_peer(peer_id)
|
||||
peer.add_protocols(protocols)
|
||||
|
||||
def get(self, peerID, key):
|
||||
if peerID in self.peerMap:
|
||||
val, error = self.peerMap[peerID].get_metadata(key)
|
||||
return val, error
|
||||
else:
|
||||
return None, peerID + " not found"
|
||||
def peers(self):
|
||||
return self.peer_map.keys()
|
||||
|
||||
def put(self, peerID, key, val):
|
||||
# <<?>>
|
||||
# This can output an error, not sure what the possible errors are
|
||||
peer = __create_or_get_peer(peerID)
|
||||
self.peerMap[peerID].put_metadata(key, val)
|
||||
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"
|
||||
|
||||
def add_addr(self, peerID, addr, ttl):
|
||||
self.add_addrs(self, peerID, [addr])
|
||||
def put(self, peer_id, key, val):
|
||||
# <<?>>
|
||||
# This can output an error, not sure what the possible errors are
|
||||
peer = self.__create_or_get_peer(peer_id)
|
||||
peer.put_metadata(key, val)
|
||||
|
||||
def add_addrs(self, peerID, addrs, ttl):
|
||||
# Ignore ttl for now
|
||||
peer = __create_or_get_peer(peerID)
|
||||
peer.add_addrs(addrs)
|
||||
def add_addr(self, peer_id, addr, ttl):
|
||||
self.add_addrs(self, peer_id, [addr])
|
||||
|
||||
def addrs(self, peerID):
|
||||
if peerID in self.peerMap:
|
||||
return self.peerMap[peerId].get_addrs(), None
|
||||
else:
|
||||
return None, peerID + " not found"
|
||||
pass
|
||||
def add_addrs(self, peer_id, addrs, ttl):
|
||||
# Ignore ttl for now
|
||||
peer = self.__create_or_get_peer(peer_id)
|
||||
peer.add_addrs(addrs)
|
||||
|
||||
def clear_addrs(self, peerID):
|
||||
# Only clear addresses if the peer is in peer map
|
||||
if peerID in self.peerMap:
|
||||
self.peerMap[peerID].clear_addrs()
|
||||
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"
|
||||
|
||||
def peers_with_addrs(self):
|
||||
# Add all peers with addrs at least 1 to output
|
||||
output = []
|
||||
def clear_addrs(self, peer_id):
|
||||
# Only clear addresses if the peer is in peer map
|
||||
if peer_id in self.peer_map:
|
||||
self.peer_map[peer_id].clear_addrs()
|
||||
|
||||
for key in self.peerMap:
|
||||
if len(self.peerMap[key].get_addrs()) >= 1:
|
||||
output.append(key)
|
||||
return output
|
||||
def peers_with_addrs(self):
|
||||
# Add all peers with addrs at least 1 to output
|
||||
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]
|
||||
for key in self.peer_map:
|
||||
if len(self.peer_map[key].get_addrs()) >= 1:
|
||||
output.append(key)
|
||||
return output
|
||||
|
|
|
@ -1,49 +1,50 @@
|
|||
from abc import ABC, abstractmethod
|
||||
from .addrbook_interace import IAddrBook
|
||||
from .addrbook_interface import IAddrBook
|
||||
from .peermetadata_interface import IPeerMetadata
|
||||
|
||||
class IPeerStore(ABC, IAddrBook, IPeerMetadata):
|
||||
|
||||
def __init__(self, context):
|
||||
self.context = context
|
||||
def __init__(self, context):
|
||||
IPeerMetadata.__init__(self, context)
|
||||
IAddrBook.__init__(self, context)
|
||||
|
||||
@abstractmethod
|
||||
def peer_info(self, peerID):
|
||||
"""
|
||||
:param peerID: peer ID to get info for
|
||||
@abstractmethod
|
||||
def peer_info(self, peer_id):
|
||||
"""
|
||||
:param peer_id: peer ID to get info for
|
||||
:return: peer info object
|
||||
"""
|
||||
pass
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def get_protocols(self, peerID):
|
||||
"""
|
||||
:param peerID: peer ID to get protocols for
|
||||
@abstractmethod
|
||||
def get_protocols(self, peer_id):
|
||||
"""
|
||||
:param peer_id: peer ID to get protocols for
|
||||
:return: protocols (as strings), error
|
||||
"""
|
||||
pass
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def add_protocols(self, peerID, protocols):
|
||||
"""
|
||||
:param peerID: peer ID to add protocols for
|
||||
@abstractmethod
|
||||
def add_protocols(self, peer_id, protocols):
|
||||
"""
|
||||
:param peer_id: peer ID to add protocols for
|
||||
:param protocols: protocols to add
|
||||
:return: error
|
||||
"""
|
||||
pass
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def set_protocols(self, peerID, protocols):
|
||||
"""
|
||||
:param peerID: peer ID to set protocols for
|
||||
@abstractmethod
|
||||
def set_protocols(self, peer_id, protocols):
|
||||
"""
|
||||
:param peer_id: peer ID to set protocols for
|
||||
:param protocols: protocols to set
|
||||
:return: error
|
||||
"""
|
||||
pass
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def peers(self):
|
||||
"""
|
||||
@abstractmethod
|
||||
def peers(self):
|
||||
"""
|
||||
:return: all of the peer IDs stored in peer store
|
||||
"""
|
||||
pass
|
||||
pass
|
||||
|
|
Loading…
Reference in New Issue
Block a user