This commit is contained in:
NIC619 2019-11-25 17:32:53 +08:00
parent e28a974425
commit 566e4c080d
No known key found for this signature in database
GPG Key ID: 570C35F5C2D51B17
5 changed files with 17 additions and 12 deletions

View File

@ -3,7 +3,7 @@ from typing import TYPE_CHECKING, List, Sequence
import multiaddr import multiaddr
from libp2p.crypto.keys import PublicKey from libp2p.crypto.keys import PrivateKey, PublicKey
from libp2p.host.defaults import get_default_protocols from libp2p.host.defaults import get_default_protocols
from libp2p.host.exceptions import StreamFailure from libp2p.host.exceptions import StreamFailure
from libp2p.network.network_interface import INetwork from libp2p.network.network_interface import INetwork
@ -67,7 +67,7 @@ class BasicHost(IHost):
def get_public_key(self) -> PublicKey: def get_public_key(self) -> PublicKey:
return self.peerstore.pubkey(self.get_id()) return self.peerstore.pubkey(self.get_id())
def get_private_key(self) -> PublicKey: def get_private_key(self) -> PrivateKey:
return self.peerstore.privkey(self.get_id()) return self.peerstore.privkey(self.get_id())
def get_network(self) -> INetwork: def get_network(self) -> INetwork:

View File

@ -3,7 +3,7 @@ from typing import Any, List, Sequence
import multiaddr import multiaddr
from libp2p.crypto.keys import PublicKey from libp2p.crypto.keys import PrivateKey, PublicKey
from libp2p.network.network_interface import INetwork from libp2p.network.network_interface import INetwork
from libp2p.network.stream.net_stream_interface import INetStream from libp2p.network.stream.net_stream_interface import INetStream
from libp2p.peer.id import ID from libp2p.peer.id import ID
@ -25,7 +25,7 @@ class IHost(ABC):
""" """
@abstractmethod @abstractmethod
def get_private_key(self) -> PublicKey: def get_private_key(self) -> PrivateKey:
""" """
:return: the private key belonging to the peer :return: the private key belonging to the peer
""" """

View File

@ -1,4 +1,4 @@
from typing import Any, Dict, List, Optional, Sequence, Tuple from typing import Any, Dict, List, Optional, Sequence, Set
from multiaddr import Multiaddr from multiaddr import Multiaddr
@ -24,7 +24,8 @@ class PeerStore(IPeerStore):
def __create_or_get_peer(self, peer_id: ID) -> PeerData: def __create_or_get_peer(self, peer_id: ID) -> PeerData:
""" """
Returns the peer data for peer_id or creates a new peer data (and Returns the peer data for peer_id or creates a new peer data (and
stores it in peer_data_map) if peer data for peer_id does not yet exist. stores it in peer_data_map) if peer data for peer_id does not yet
exist.
:param peer_id: peer ID :param peer_id: peer ID
:return: peer data :return: peer data
@ -158,7 +159,9 @@ class PeerStore(IPeerStore):
:raise PeerStoreError: if peer ID already has pubkey set :raise PeerStoreError: if peer ID already has pubkey set
""" """
if peer_id in self.peer_pubkey_map: if peer_id in self.peer_pubkey_map:
raise PeerStoreError(f"peer ID already has pubkey: {self.peer_pubkey_map[peer_id]}") raise PeerStoreError(
f"peer ID already has pubkey: {self.peer_pubkey_map[peer_id]}"
)
self.peer_pubkey_map[peer_id] = pubkey self.peer_pubkey_map[peer_id] = pubkey
def pubkey(self, peer_id: ID) -> PublicKey: def pubkey(self, peer_id: ID) -> PublicKey:
@ -178,7 +181,9 @@ class PeerStore(IPeerStore):
:raise PeerStoreError: if peer ID already has privkey set :raise PeerStoreError: if peer ID already has privkey set
""" """
if peer_id in self.peer_privkey_map: if peer_id in self.peer_privkey_map:
raise PeerStoreError(f"peer ID already has privkey: {self.peer_privkey_map[peer_id]}") raise PeerStoreError(
f"peer ID already has privkey: {self.peer_privkey_map[peer_id]}"
)
self.peer_privkey_map[peer_id] = privkey self.peer_privkey_map[peer_id] = privkey
def privkey(self, peer_id: ID) -> PrivateKey: def privkey(self, peer_id: ID) -> PrivateKey:
@ -191,7 +196,7 @@ class PeerStore(IPeerStore):
return self.peer_privkey_map[peer_id] return self.peer_privkey_map[peer_id]
raise PeerStoreError("peer ID not found") raise PeerStoreError("peer ID not found")
def peers_with_keys(self) -> Tuple[ID]: def peers_with_keys(self) -> Set[ID]:
""" """
:return: all of the peer IDs which has pubkey/privkey stored in peer store :return: all of the peer IDs which has pubkey/privkey stored in peer store
""" """

View File

@ -1,5 +1,5 @@
from abc import abstractmethod from abc import abstractmethod
from typing import Any, List, Sequence, Tuple from typing import Any, List, Sequence, Set
from multiaddr import Multiaddr from multiaddr import Multiaddr
@ -132,7 +132,7 @@ class IPeerStore(IAddrBook, IPeerMetadata):
""" """
@abstractmethod @abstractmethod
def peers_with_keys(self) -> Tuple[ID]: def peers_with_keys(self) -> Set[ID]:
""" """
:return: all of the peer IDs which has pubkey/privkey stored in peer store :return: all of the peer IDs which has pubkey/privkey stored in peer store
""" """

View File

@ -34,7 +34,7 @@ from .constants import (
from .utils import connect, connect_swarm from .utils import connect, connect_swarm
def initialize_peerstore_with_our_keypair(self_id: ID, key_pair: KeyPair): def initialize_peerstore_with_our_keypair(self_id: ID, key_pair: KeyPair) -> PeerStore:
peer_store = PeerStore() peer_store = PeerStore()
peer_store.add_pubkey(self_id, key_pair.public_key) peer_store.add_pubkey(self_id, key_pair.public_key)
peer_store.add_privkey(self_id, key_pair.private_key) peer_store.add_privkey(self_id, key_pair.private_key)