mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2026-02-10 07:00:54 +00:00
Migrate to new project structure.
This commit is contained in:
5
libp2p/peer/README.md
Normal file
5
libp2p/peer/README.md
Normal file
@ -0,0 +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.
|
||||
0
libp2p/peer/__init__.py
Normal file
0
libp2p/peer/__init__.py
Normal file
48
libp2p/peer/addrbook_interface.py
Normal file
48
libp2p/peer/addrbook_interface.py
Normal file
@ -0,0 +1,48 @@
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
|
||||
class IAddrBook(ABC):
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def add_addr(self, peer_id, addr, ttl):
|
||||
"""
|
||||
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)
|
||||
"""
|
||||
|
||||
@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
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def addrs(self, peer_id):
|
||||
"""
|
||||
:param peer_id: peer to get addresses of
|
||||
:return: all known (and valid) addresses for the given peer
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def clear_addrs(self, peer_id):
|
||||
"""
|
||||
Removes all previously stored addresses
|
||||
:param peer_id: peer to remove addresses of
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def peers_with_addrs(self):
|
||||
"""
|
||||
:return: all of the peer IDs stored with addresses
|
||||
"""
|
||||
|
||||
66
libp2p/peer/id.py
Normal file
66
libp2p/peer/id.py
Normal file
@ -0,0 +1,66 @@
|
||||
import base58
|
||||
import multihash
|
||||
|
||||
# MaxInlineKeyLength is the maximum length a key can be for it to be inlined in
|
||||
# the peer ID.
|
||||
# * When `len(pubKey.Bytes()) <= MaxInlineKeyLength`, the peer ID is the
|
||||
# identity multihash hash of the public key.
|
||||
# * When `len(pubKey.Bytes()) > MaxInlineKeyLength`, the peer ID is the
|
||||
# sha2-256 multihash of the public key.
|
||||
MAX_INLINE_KEY_LENGTH = 42
|
||||
|
||||
|
||||
class ID:
|
||||
|
||||
def __init__(self, id_str):
|
||||
self._id_str = id_str
|
||||
|
||||
def pretty(self):
|
||||
return base58.b58encode(self._id_str).decode()
|
||||
|
||||
def __str__(self):
|
||||
pid = self.pretty()
|
||||
if len(pid) <= 10:
|
||||
return "<peer.ID %s>" % pid
|
||||
return "<peer.ID %s*%s>" % (pid[:2], pid[len(pid)-6:])
|
||||
|
||||
__repr__ = __str__
|
||||
|
||||
def __eq__(self, other):
|
||||
#pylint: disable=protected-access
|
||||
return self._id_str == other._id_str
|
||||
|
||||
def __hash__(self):
|
||||
return hash(self._id_str)
|
||||
|
||||
|
||||
def id_b58_encode(peer_id):
|
||||
"""
|
||||
return a b58-encoded string
|
||||
"""
|
||||
#pylint: disable=protected-access
|
||||
return base58.b58encode(peer_id._id_str).decode()
|
||||
|
||||
|
||||
def id_b58_decode(peer_id_str):
|
||||
"""
|
||||
return a base58-decoded peer ID
|
||||
"""
|
||||
return ID(base58.b58decode(peer_id_str))
|
||||
|
||||
|
||||
def id_from_public_key(key):
|
||||
# export into binary format
|
||||
key_bin = key.exportKey("DER")
|
||||
|
||||
algo = multihash.Func.sha2_256
|
||||
# TODO: seems identity is not yet supported in pymultihash
|
||||
# if len(b) <= MAX_INLINE_KEY_LENGTH:
|
||||
# algo multihash.func.identity
|
||||
|
||||
mh_digest = multihash.digest(key_bin, algo)
|
||||
return ID(mh_digest.encode())
|
||||
|
||||
|
||||
def id_from_private_key(key):
|
||||
return id_from_public_key(key.publickey())
|
||||
39
libp2p/peer/peerdata.py
Normal file
39
libp2p/peer/peerdata.py
Normal file
@ -0,0 +1,39 @@
|
||||
from .peerdata_interface import IPeerData
|
||||
|
||||
|
||||
class PeerData(IPeerData):
|
||||
|
||||
def __init__(self):
|
||||
self.metadata = {}
|
||||
self.protocols = []
|
||||
self.addrs = []
|
||||
|
||||
def get_protocols(self):
|
||||
return self.protocols
|
||||
|
||||
def add_protocols(self, protocols):
|
||||
self.protocols.extend(protocols)
|
||||
|
||||
def set_protocols(self, protocols):
|
||||
self.protocols = protocols
|
||||
|
||||
def add_addrs(self, addrs):
|
||||
self.addrs.extend(addrs)
|
||||
|
||||
def get_addrs(self):
|
||||
return self.addrs
|
||||
|
||||
def clear_addrs(self):
|
||||
self.addrs = []
|
||||
|
||||
def put_metadata(self, key, val):
|
||||
self.metadata[key] = val
|
||||
|
||||
def get_metadata(self, key):
|
||||
if key in self.metadata:
|
||||
return self.metadata[key]
|
||||
raise PeerDataError("key not found")
|
||||
|
||||
|
||||
class PeerDataError(KeyError):
|
||||
"""Raised when a key is not found in peer metadata"""
|
||||
56
libp2p/peer/peerdata_interface.py
Normal file
56
libp2p/peer/peerdata_interface.py
Normal file
@ -0,0 +1,56 @@
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
|
||||
class IPeerData(ABC):
|
||||
|
||||
@abstractmethod
|
||||
def get_protocols(self):
|
||||
"""
|
||||
:return: all protocols associated with given peer
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def add_protocols(self, protocols):
|
||||
"""
|
||||
:param protocols: protocols to add
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def set_protocols(self, protocols):
|
||||
"""
|
||||
:param protocols: protocols to add
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def add_addrs(self, addrs):
|
||||
"""
|
||||
:param addrs: multiaddresses to add
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def get_addrs(self):
|
||||
"""
|
||||
:return: all multiaddresses
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def clear_addrs(self):
|
||||
"""
|
||||
Clear all addresses
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def put_metadata(self, key, val):
|
||||
"""
|
||||
:param key: key in KV pair
|
||||
:param val: val to associate with key
|
||||
:raise Exception: unsuccesful put
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def get_metadata(self, key):
|
||||
"""
|
||||
:param key: key in KV pair
|
||||
:return: val for key
|
||||
:raise Exception: key not found
|
||||
"""
|
||||
43
libp2p/peer/peerinfo.py
Normal file
43
libp2p/peer/peerinfo.py
Normal file
@ -0,0 +1,43 @@
|
||||
import multiaddr
|
||||
import multiaddr.util
|
||||
|
||||
from .id import id_b58_decode
|
||||
from .peerdata import PeerData
|
||||
|
||||
|
||||
class PeerInfo:
|
||||
# pylint: disable=too-few-public-methods
|
||||
def __init__(self, peer_id, peer_data):
|
||||
self.peer_id = peer_id
|
||||
self.addrs = peer_data.get_addrs()
|
||||
|
||||
|
||||
def info_from_p2p_addr(addr):
|
||||
if not addr:
|
||||
raise InvalidAddrError()
|
||||
|
||||
parts = multiaddr.util.split(addr)
|
||||
if not parts:
|
||||
raise InvalidAddrError()
|
||||
|
||||
p2p_part = parts[-1]
|
||||
if p2p_part.protocols()[0].code != multiaddr.protocols.P_P2P:
|
||||
raise InvalidAddrError()
|
||||
|
||||
# make sure the /p2p value parses as a peer.ID
|
||||
peer_id_str = p2p_part.value_for_protocol(multiaddr.protocols.P_P2P)
|
||||
peer_id = id_b58_decode(peer_id_str)
|
||||
|
||||
# we might have received just an / p2p part, which means there's no addr.
|
||||
if len(parts) > 1:
|
||||
addr = multiaddr.util.join(parts[:-1])
|
||||
|
||||
peer_data = PeerData()
|
||||
peer_data.addrs = [addr]
|
||||
peer_data.protocols = [p.code for p in addr.protocols()]
|
||||
|
||||
return PeerInfo(peer_id, peer_data)
|
||||
|
||||
|
||||
class InvalidAddrError(ValueError):
|
||||
pass
|
||||
25
libp2p/peer/peermetadata_interface.py
Normal file
25
libp2p/peer/peermetadata_interface.py
Normal file
@ -0,0 +1,25 @@
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
|
||||
class IPeerMetadata(ABC):
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
@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
|
||||
:raise Exception: peer ID not found
|
||||
"""
|
||||
|
||||
@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
|
||||
:raise Exception: unsuccessful put
|
||||
"""
|
||||
89
libp2p/peer/peerstore.py
Normal file
89
libp2p/peer/peerstore.py
Normal file
@ -0,0 +1,89 @@
|
||||
from .peerstore_interface import IPeerStore
|
||||
from .peerdata import PeerData
|
||||
from .peerinfo import PeerInfo
|
||||
|
||||
|
||||
class PeerStore(IPeerStore):
|
||||
|
||||
def __init__(self):
|
||||
IPeerStore.__init__(self)
|
||||
self.peer_map = {}
|
||||
|
||||
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 peer_info(self, peer_id):
|
||||
if peer_id in self.peer_map:
|
||||
peer = self.peer_map[peer_id]
|
||||
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()
|
||||
raise PeerStoreError("peer ID not found")
|
||||
|
||||
def add_protocols(self, peer_id, protocols):
|
||||
peer = self.__create_or_get_peer(peer_id)
|
||||
peer.add_protocols(protocols)
|
||||
|
||||
def set_protocols(self, peer_id, protocols):
|
||||
peer = self.__create_or_get_peer(peer_id)
|
||||
peer.set_protocols(protocols)
|
||||
|
||||
def peers(self):
|
||||
return list(self.peer_map.keys())
|
||||
|
||||
def get(self, peer_id, key):
|
||||
if peer_id in self.peer_map:
|
||||
val = self.peer_map[peer_id].get_metadata(key)
|
||||
return val
|
||||
raise PeerStoreError("peer ID not found")
|
||||
|
||||
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_addr(self, peer_id, addr, ttl):
|
||||
self.add_addrs(peer_id, [addr], ttl)
|
||||
|
||||
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 addrs(self, peer_id):
|
||||
if peer_id in self.peer_map:
|
||||
return self.peer_map[peer_id].get_addrs()
|
||||
raise PeerStoreError("peer ID not found")
|
||||
|
||||
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()
|
||||
|
||||
def peers_with_addrs(self):
|
||||
# Add all peers with addrs at least 1 to output
|
||||
output = []
|
||||
|
||||
for key in self.peer_map:
|
||||
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"""
|
||||
48
libp2p/peer/peerstore_interface.py
Normal file
48
libp2p/peer/peerstore_interface.py
Normal file
@ -0,0 +1,48 @@
|
||||
from abc import abstractmethod
|
||||
|
||||
from .addrbook_interface import IAddrBook
|
||||
from .peermetadata_interface import IPeerMetadata
|
||||
|
||||
|
||||
class IPeerStore(IAddrBook, IPeerMetadata):
|
||||
|
||||
def __init__(self):
|
||||
IPeerMetadata.__init__(self)
|
||||
IAddrBook.__init__(self)
|
||||
|
||||
@abstractmethod
|
||||
def peer_info(self, peer_id):
|
||||
"""
|
||||
:param peer_id: peer ID to get info for
|
||||
:return: peer info object
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def get_protocols(self, peer_id):
|
||||
"""
|
||||
:param peer_id: peer ID to get protocols for
|
||||
:return: protocols (as strings)
|
||||
:raise Exception: peer ID not found exception
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def add_protocols(self, peer_id, protocols):
|
||||
"""
|
||||
:param peer_id: peer ID to add protocols for
|
||||
:param protocols: protocols to add
|
||||
:raise Exception: peer ID not found
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def set_protocols(self, peer_id, protocols):
|
||||
"""
|
||||
:param peer_id: peer ID to set protocols for
|
||||
:param protocols: protocols to set
|
||||
:raise Exception: peer ID not found
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def peers(self):
|
||||
"""
|
||||
:return: all of the peer IDs stored in peer store
|
||||
"""
|
||||
Reference in New Issue
Block a user