hosts track their public key

This commit is contained in:
Alex Stokes
2019-10-28 18:44:55 +09:00
parent 11db313b17
commit 32c55bcaf2
4 changed files with 16 additions and 4 deletions

View File

@ -154,9 +154,9 @@ async def new_node(
# TODO routing unimplemented
host: IHost # If not explicitly typed, MyPy raises error
if disc_opt:
host = RoutedHost(swarm_opt, disc_opt)
host = RoutedHost(key_pair.public_key, swarm_opt, disc_opt)
else:
host = BasicHost(swarm_opt)
host = BasicHost(key_pair.public_key, swarm_opt)
# Kick off cleanup job
asyncio.ensure_future(cleanup_done_tasks())

View File

@ -46,9 +46,11 @@ class BasicHost(IHost):
def __init__(
self,
public_key: PublicKey,
network: INetwork,
default_protocols: "OrderedDict[TProtocol, StreamHandlerFn]" = None,
) -> None:
self._public_key = public_key
self._network = network
self._network.set_stream_handler(self._swarm_stream_handler)
self.peerstore = self._network.peerstore
@ -63,6 +65,9 @@ class BasicHost(IHost):
"""
return self._network.get_peer_id()
def get_public_key(self) -> PublicKey:
return self._public_key
def get_network(self) -> INetwork:
"""
:return: network instance of host

View File

@ -17,6 +17,12 @@ class IHost(ABC):
:return: peer_id of host
"""
@abstractmethod
def get_public_key(self) -> PublicKey:
"""
:return: the public key belonging to the peer
"""
@abstractmethod
def get_network(self) -> INetwork:
"""

View File

@ -1,3 +1,4 @@
from libp2p.crypto.keys import PublicKey
from libp2p.host.basic_host import BasicHost
from libp2p.host.exceptions import ConnectionFailure
from libp2p.network.network_interface import INetwork
@ -10,8 +11,8 @@ from libp2p.routing.interfaces import IPeerRouting
class RoutedHost(BasicHost):
_router: IPeerRouting
def __init__(self, network: INetwork, router: IPeerRouting):
super().__init__(network)
def __init__(self, public_key: PublicKey, network: INetwork, router: IPeerRouting):
super().__init__(public_key, network)
self._router = router
async def connect(self, peer_info: PeerInfo) -> None: