Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions libp2p/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
Any,
AsyncContextManager,
Optional,
Union,
)

from multiaddr import (
Expand Down Expand Up @@ -62,6 +63,9 @@
ServiceAPI,
)

# Type alias for metadata values - JSON-serializable types
MetadataValue = Union[str, int, float, bool, list, dict, None]

# -------------------------- raw_connection interface.py --------------------------


Expand Down Expand Up @@ -377,7 +381,7 @@ class IPeerMetadata(ABC):
"""

@abstractmethod
def get(self, peer_id: ID, key: str) -> Any:
def get(self, peer_id: ID, key: str) -> MetadataValue:
"""
Retrieve metadata for a specified peer.

Expand All @@ -388,7 +392,7 @@ def get(self, peer_id: ID, key: str) -> Any:
"""

@abstractmethod
def put(self, peer_id: ID, key: str, val: Any) -> None:
def put(self, peer_id: ID, key: str, val: MetadataValue) -> None:
"""
Store metadata for a specified peer.

Expand Down Expand Up @@ -843,7 +847,7 @@ class IPeerStore(

# -------METADATA---------
@abstractmethod
def get(self, peer_id: ID, key: str) -> Any:
def get(self, peer_id: ID, key: str) -> MetadataValue:
"""
Retrieve the value associated with a key for a specified peer.

Expand All @@ -856,7 +860,7 @@ def get(self, peer_id: ID, key: str) -> Any:

Returns
-------
Any
MetadataValue
The value corresponding to the specified key.

Raises
Expand All @@ -867,7 +871,7 @@ def get(self, peer_id: ID, key: str) -> Any:
"""

@abstractmethod
def put(self, peer_id: ID, key: str, val: Any) -> None:
def put(self, peer_id: ID, key: str, val: MetadataValue) -> None:
"""
Store a key-value pair for the specified peer.

Expand All @@ -877,7 +881,7 @@ def put(self, peer_id: ID, key: str, val: Any) -> None:
The identifier of the peer.
key : str
The key for the data.
val : Any
val : MetadataValue
The value to store.

"""
Expand Down Expand Up @@ -2132,21 +2136,21 @@ def clear_addrs(self) -> None:
"""

@abstractmethod
def put_metadata(self, key: str, val: Any) -> None:
def put_metadata(self, key: str, val: MetadataValue) -> None:
"""
Store a metadata key-value pair for the peer.

Parameters
----------
key : str
The metadata key.
val : Any
val : MetadataValue
The value to associate with the key.

"""

@abstractmethod
def get_metadata(self, key: str) -> IPeerMetadata:
def get_metadata(self, key: str) -> MetadataValue:
"""
Retrieve metadata for a given key.

Expand All @@ -2157,7 +2161,7 @@ def get_metadata(self, key: str) -> IPeerMetadata:

Returns
-------
IPeerMetadata
MetadataValue
The metadata value for the given key.

Raises
Expand Down
7 changes: 4 additions & 3 deletions libp2p/peer/peerdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from libp2p.abc import (
IPeerData,
MetadataValue,
)
from libp2p.crypto.keys import (
PrivateKey,
Expand All @@ -29,7 +30,7 @@
class PeerData(IPeerData):
pubkey: PublicKey | None
privkey: PrivateKey | None
metadata: dict[Any, Any]
metadata: dict[str, MetadataValue]
protocols: list[str]
addrs: list[Multiaddr]
last_identified: int
Expand Down Expand Up @@ -116,14 +117,14 @@ def clear_addrs(self) -> None:
self.addrs = []

# -------METADATA-----------
def put_metadata(self, key: str, val: Any) -> None:
def put_metadata(self, key: str, val: MetadataValue) -> None:
"""
:param key: key in KV pair
:param val: val to associate with key
"""
self.metadata[key] = val

def get_metadata(self, key: str) -> Any:
def get_metadata(self, key: str) -> MetadataValue:
"""
:param key: key in KV pair
:return: val for key
Expand Down
5 changes: 3 additions & 2 deletions libp2p/peer/peerstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from libp2p.abc import (
IHost,
IPeerStore,
MetadataValue,
)
from libp2p.crypto.keys import (
KeyPair,
Expand Down Expand Up @@ -275,7 +276,7 @@ def clear_protocol_data(self, peer_id: ID) -> None:

# ------METADATA---------

def get(self, peer_id: ID, key: str) -> Any:
def get(self, peer_id: ID, key: str) -> MetadataValue:
"""
:param peer_id: peer ID to get peer data for
:param key: the key to search value for
Expand All @@ -290,7 +291,7 @@ def get(self, peer_id: ID, key: str) -> Any:
return val
raise PeerStoreError("peer ID not found")

def put(self, peer_id: ID, key: str, val: Any) -> None:
def put(self, peer_id: ID, key: str, val: MetadataValue) -> None:
"""
:param peer_id: peer ID to put peer data for
:param key:
Expand Down
Loading