mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2025-12-31 20:36:24 +00:00
run lint with pyupgrade at py39-plus
This commit is contained in:
@ -2,13 +2,13 @@ from abc import (
|
||||
ABC,
|
||||
abstractmethod,
|
||||
)
|
||||
from collections.abc import (
|
||||
AsyncIterable,
|
||||
KeysView,
|
||||
)
|
||||
from typing import (
|
||||
TYPE_CHECKING,
|
||||
AsyncContextManager,
|
||||
AsyncIterable,
|
||||
KeysView,
|
||||
List,
|
||||
Tuple,
|
||||
)
|
||||
|
||||
from libp2p.peer.id import (
|
||||
@ -46,7 +46,7 @@ class ISubscriptionAPI(
|
||||
|
||||
class IPubsubRouter(ABC):
|
||||
@abstractmethod
|
||||
def get_protocols(self) -> List[TProtocol]:
|
||||
def get_protocols(self) -> list[TProtocol]:
|
||||
"""
|
||||
:return: the list of protocols supported by the router
|
||||
"""
|
||||
@ -123,7 +123,7 @@ class IPubsub(ServiceAPI):
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def protocols(self) -> Tuple[TProtocol, ...]:
|
||||
def protocols(self) -> tuple[TProtocol, ...]:
|
||||
...
|
||||
|
||||
@property
|
||||
|
||||
@ -1,9 +1,8 @@
|
||||
import logging
|
||||
from typing import (
|
||||
from collections.abc import (
|
||||
Iterable,
|
||||
List,
|
||||
Sequence,
|
||||
)
|
||||
import logging
|
||||
|
||||
import trio
|
||||
|
||||
@ -36,7 +35,7 @@ logger = logging.getLogger("libp2p.pubsub.floodsub")
|
||||
|
||||
|
||||
class FloodSub(IPubsubRouter):
|
||||
protocols: List[TProtocol]
|
||||
protocols: list[TProtocol]
|
||||
|
||||
pubsub: Pubsub
|
||||
|
||||
@ -44,7 +43,7 @@ class FloodSub(IPubsubRouter):
|
||||
self.protocols = list(protocols)
|
||||
self.pubsub = None
|
||||
|
||||
def get_protocols(self) -> List[TProtocol]:
|
||||
def get_protocols(self) -> list[TProtocol]:
|
||||
"""
|
||||
:return: the list of protocols supported by the router
|
||||
"""
|
||||
|
||||
@ -4,17 +4,15 @@ from ast import (
|
||||
from collections import (
|
||||
defaultdict,
|
||||
)
|
||||
from collections.abc import (
|
||||
Iterable,
|
||||
Sequence,
|
||||
)
|
||||
import logging
|
||||
import random
|
||||
from typing import (
|
||||
Any,
|
||||
DefaultDict,
|
||||
Dict,
|
||||
Iterable,
|
||||
List,
|
||||
Sequence,
|
||||
Set,
|
||||
Tuple,
|
||||
)
|
||||
|
||||
import trio
|
||||
@ -60,7 +58,7 @@ logger = logging.getLogger("libp2p.pubsub.gossipsub")
|
||||
|
||||
|
||||
class GossipSub(IPubsubRouter, Service):
|
||||
protocols: List[TProtocol]
|
||||
protocols: list[TProtocol]
|
||||
pubsub: Pubsub
|
||||
|
||||
degree: int
|
||||
@ -69,11 +67,11 @@ class GossipSub(IPubsubRouter, Service):
|
||||
|
||||
time_to_live: int
|
||||
|
||||
mesh: Dict[str, Set[ID]]
|
||||
fanout: Dict[str, Set[ID]]
|
||||
mesh: dict[str, set[ID]]
|
||||
fanout: dict[str, set[ID]]
|
||||
|
||||
# The protocol peer supports
|
||||
peer_protocol: Dict[ID, TProtocol]
|
||||
peer_protocol: dict[ID, TProtocol]
|
||||
|
||||
# TODO: Add `time_since_last_publish`
|
||||
# Create topic --> time since last publish map.
|
||||
@ -128,7 +126,7 @@ class GossipSub(IPubsubRouter, Service):
|
||||
|
||||
# Interface functions
|
||||
|
||||
def get_protocols(self) -> List[TProtocol]:
|
||||
def get_protocols(self) -> list[TProtocol]:
|
||||
"""
|
||||
:return: the list of protocols supported by the router
|
||||
"""
|
||||
@ -237,13 +235,13 @@ class GossipSub(IPubsubRouter, Service):
|
||||
:param origin: peer id of the peer the message originate from.
|
||||
:return: a generator of the peer ids who we send data to.
|
||||
"""
|
||||
send_to: Set[ID] = set()
|
||||
send_to: set[ID] = set()
|
||||
for topic in topic_ids:
|
||||
if topic not in self.pubsub.peer_topics:
|
||||
continue
|
||||
|
||||
# floodsub peers
|
||||
floodsub_peers: Set[ID] = {
|
||||
floodsub_peers: set[ID] = {
|
||||
peer_id
|
||||
for peer_id in self.pubsub.peer_topics[topic]
|
||||
if self.peer_protocol[peer_id] == floodsub.PROTOCOL_ID
|
||||
@ -251,7 +249,7 @@ class GossipSub(IPubsubRouter, Service):
|
||||
send_to.update(floodsub_peers)
|
||||
|
||||
# gossipsub peers
|
||||
gossipsub_peers: Set[ID] = set()
|
||||
gossipsub_peers: set[ID] = set()
|
||||
if topic in self.mesh:
|
||||
gossipsub_peers = self.mesh[topic]
|
||||
else:
|
||||
@ -259,7 +257,7 @@ class GossipSub(IPubsubRouter, Service):
|
||||
# pick `self.degree` number of peers who have subscribed to the topic
|
||||
# and add them as our `fanout` peers.
|
||||
topic_in_fanout: bool = topic in self.fanout
|
||||
fanout_peers: Set[ID] = self.fanout[topic] if topic_in_fanout else set()
|
||||
fanout_peers: set[ID] = self.fanout[topic] if topic_in_fanout else set()
|
||||
fanout_size = len(fanout_peers)
|
||||
if not topic_in_fanout or (
|
||||
topic_in_fanout and fanout_size < self.degree
|
||||
@ -292,7 +290,7 @@ class GossipSub(IPubsubRouter, Service):
|
||||
self.mesh[topic] = set()
|
||||
|
||||
topic_in_fanout: bool = topic in self.fanout
|
||||
fanout_peers: Set[ID] = self.fanout[topic] if topic_in_fanout else set()
|
||||
fanout_peers: set[ID] = self.fanout[topic] if topic_in_fanout else set()
|
||||
fanout_size = len(fanout_peers)
|
||||
if not topic_in_fanout or (topic_in_fanout and fanout_size < self.degree):
|
||||
# There are less than D peers (let this number be x)
|
||||
@ -333,13 +331,13 @@ class GossipSub(IPubsubRouter, Service):
|
||||
|
||||
async def _emit_control_msgs(
|
||||
self,
|
||||
peers_to_graft: Dict[ID, List[str]],
|
||||
peers_to_prune: Dict[ID, List[str]],
|
||||
peers_to_gossip: Dict[ID, Dict[str, List[str]]],
|
||||
peers_to_graft: dict[ID, list[str]],
|
||||
peers_to_prune: dict[ID, list[str]],
|
||||
peers_to_gossip: dict[ID, dict[str, list[str]]],
|
||||
) -> None:
|
||||
graft_msgs: List[rpc_pb2.ControlGraft] = []
|
||||
prune_msgs: List[rpc_pb2.ControlPrune] = []
|
||||
ihave_msgs: List[rpc_pb2.ControlIHave] = []
|
||||
graft_msgs: list[rpc_pb2.ControlGraft] = []
|
||||
prune_msgs: list[rpc_pb2.ControlPrune] = []
|
||||
ihave_msgs: list[rpc_pb2.ControlIHave] = []
|
||||
# Starting with GRAFT messages
|
||||
for peer, topics in peers_to_graft.items():
|
||||
for topic in topics:
|
||||
@ -428,9 +426,9 @@ class GossipSub(IPubsubRouter, Service):
|
||||
|
||||
def mesh_heartbeat(
|
||||
self,
|
||||
) -> Tuple[DefaultDict[ID, List[str]], DefaultDict[ID, List[str]]]:
|
||||
peers_to_graft: DefaultDict[ID, List[str]] = defaultdict(list)
|
||||
peers_to_prune: DefaultDict[ID, List[str]] = defaultdict(list)
|
||||
) -> tuple[DefaultDict[ID, list[str]], DefaultDict[ID, list[str]]]:
|
||||
peers_to_graft: DefaultDict[ID, list[str]] = defaultdict(list)
|
||||
peers_to_prune: DefaultDict[ID, list[str]] = defaultdict(list)
|
||||
for topic in self.mesh:
|
||||
# Skip if no peers have subscribed to the topic
|
||||
if topic not in self.pubsub.peer_topics:
|
||||
@ -493,8 +491,8 @@ class GossipSub(IPubsubRouter, Service):
|
||||
# Add the peers to fanout[topic]
|
||||
self.fanout[topic].update(selected_peers)
|
||||
|
||||
def gossip_heartbeat(self) -> DefaultDict[ID, Dict[str, List[str]]]:
|
||||
peers_to_gossip: DefaultDict[ID, Dict[str, List[str]]] = defaultdict(dict)
|
||||
def gossip_heartbeat(self) -> DefaultDict[ID, dict[str, list[str]]]:
|
||||
peers_to_gossip: DefaultDict[ID, dict[str, list[str]]] = defaultdict(dict)
|
||||
for topic in self.mesh:
|
||||
msg_ids = self.mcache.window(topic)
|
||||
if msg_ids:
|
||||
@ -534,7 +532,7 @@ class GossipSub(IPubsubRouter, Service):
|
||||
@staticmethod
|
||||
def select_from_minus(
|
||||
num_to_select: int, pool: Iterable[Any], minus: Iterable[Any]
|
||||
) -> List[Any]:
|
||||
) -> list[Any]:
|
||||
"""
|
||||
Select at most num_to_select subset of elements from the set
|
||||
(pool - minus) randomly.
|
||||
@ -546,7 +544,7 @@ class GossipSub(IPubsubRouter, Service):
|
||||
# Create selection pool, which is selection_pool = pool - minus
|
||||
if minus:
|
||||
# Create a new selection pool by removing elements of minus
|
||||
selection_pool: List[Any] = [x for x in pool if x not in minus]
|
||||
selection_pool: list[Any] = [x for x in pool if x not in minus]
|
||||
else:
|
||||
# Don't create a new selection_pool if we are not subbing anything
|
||||
selection_pool = list(pool)
|
||||
@ -558,13 +556,13 @@ class GossipSub(IPubsubRouter, Service):
|
||||
return selection_pool
|
||||
|
||||
# Random selection
|
||||
selection: List[Any] = random.sample(selection_pool, num_to_select)
|
||||
selection: list[Any] = random.sample(selection_pool, num_to_select)
|
||||
|
||||
return selection
|
||||
|
||||
def _get_in_topic_gossipsub_peers_from_minus(
|
||||
self, topic: str, num_to_select: int, minus: Iterable[ID]
|
||||
) -> List[ID]:
|
||||
) -> list[ID]:
|
||||
gossipsub_peers_in_topic = {
|
||||
peer_id
|
||||
for peer_id in self.pubsub.peer_topics[topic]
|
||||
@ -587,7 +585,7 @@ class GossipSub(IPubsubRouter, Service):
|
||||
# Add all unknown message ids (ids that appear in ihave_msg but not in
|
||||
# seen_seqnos) to list of messages we want to request
|
||||
# FIXME: Update type of message ID
|
||||
msg_ids_wanted: List[Any] = [
|
||||
msg_ids_wanted: list[Any] = [
|
||||
msg_id
|
||||
for msg_id in ihave_msg.messageIDs
|
||||
if literal_eval(msg_id) not in seen_seqnos_and_peers
|
||||
@ -606,8 +604,8 @@ class GossipSub(IPubsubRouter, Service):
|
||||
"""
|
||||
# FIXME: Update type of message ID
|
||||
# FIXME: Find a better way to parse the msg ids
|
||||
msg_ids: List[Any] = [literal_eval(msg) for msg in iwant_msg.messageIDs]
|
||||
msgs_to_forward: List[rpc_pb2.Message] = []
|
||||
msg_ids: list[Any] = [literal_eval(msg) for msg in iwant_msg.messageIDs]
|
||||
msgs_to_forward: list[rpc_pb2.Message] = []
|
||||
for msg_id_iwant in msg_ids:
|
||||
# Check if the wanted message ID is present in mcache
|
||||
msg: rpc_pb2.Message = self.mcache.get(msg_id_iwant)
|
||||
@ -674,9 +672,9 @@ class GossipSub(IPubsubRouter, Service):
|
||||
|
||||
def pack_control_msgs(
|
||||
self,
|
||||
ihave_msgs: List[rpc_pb2.ControlIHave],
|
||||
graft_msgs: List[rpc_pb2.ControlGraft],
|
||||
prune_msgs: List[rpc_pb2.ControlPrune],
|
||||
ihave_msgs: list[rpc_pb2.ControlIHave],
|
||||
graft_msgs: list[rpc_pb2.ControlGraft],
|
||||
prune_msgs: list[rpc_pb2.ControlPrune],
|
||||
) -> rpc_pb2.ControlMessage:
|
||||
control_msg: rpc_pb2.ControlMessage = rpc_pb2.ControlMessage()
|
||||
if ihave_msgs:
|
||||
|
||||
@ -1,9 +1,8 @@
|
||||
from typing import (
|
||||
Dict,
|
||||
List,
|
||||
Optional,
|
||||
from collections.abc import (
|
||||
Sequence,
|
||||
Tuple,
|
||||
)
|
||||
from typing import (
|
||||
Optional,
|
||||
)
|
||||
|
||||
from .pb import (
|
||||
@ -12,14 +11,14 @@ from .pb import (
|
||||
|
||||
|
||||
class CacheEntry:
|
||||
mid: Tuple[bytes, bytes]
|
||||
topics: List[str]
|
||||
mid: tuple[bytes, bytes]
|
||||
topics: list[str]
|
||||
|
||||
"""
|
||||
A logical representation of an entry in the mcache's _history_.
|
||||
"""
|
||||
|
||||
def __init__(self, mid: Tuple[bytes, bytes], topics: Sequence[str]) -> None:
|
||||
def __init__(self, mid: tuple[bytes, bytes], topics: Sequence[str]) -> None:
|
||||
"""
|
||||
Constructor.
|
||||
|
||||
@ -34,9 +33,9 @@ class MessageCache:
|
||||
window_size: int
|
||||
history_size: int
|
||||
|
||||
msgs: Dict[Tuple[bytes, bytes], rpc_pb2.Message]
|
||||
msgs: dict[tuple[bytes, bytes], rpc_pb2.Message]
|
||||
|
||||
history: List[List[CacheEntry]]
|
||||
history: list[list[CacheEntry]]
|
||||
|
||||
def __init__(self, window_size: int, history_size: int) -> None:
|
||||
"""
|
||||
@ -62,12 +61,12 @@ class MessageCache:
|
||||
|
||||
:param msg: The rpc message to put in. Should contain seqno and from_id
|
||||
"""
|
||||
mid: Tuple[bytes, bytes] = (msg.seqno, msg.from_id)
|
||||
mid: tuple[bytes, bytes] = (msg.seqno, msg.from_id)
|
||||
self.msgs[mid] = msg
|
||||
|
||||
self.history[0].append(CacheEntry(mid, msg.topicIDs))
|
||||
|
||||
def get(self, mid: Tuple[bytes, bytes]) -> Optional[rpc_pb2.Message]:
|
||||
def get(self, mid: tuple[bytes, bytes]) -> Optional[rpc_pb2.Message]:
|
||||
"""
|
||||
Get a message from the mcache.
|
||||
|
||||
@ -79,14 +78,14 @@ class MessageCache:
|
||||
|
||||
return None
|
||||
|
||||
def window(self, topic: str) -> List[Tuple[bytes, bytes]]:
|
||||
def window(self, topic: str) -> list[tuple[bytes, bytes]]:
|
||||
"""
|
||||
Get the window for this topic.
|
||||
|
||||
:param topic: Topic whose message ids we desire.
|
||||
:return: List of mids in the current window.
|
||||
"""
|
||||
mids: List[Tuple[bytes, bytes]] = []
|
||||
mids: list[tuple[bytes, bytes]] = []
|
||||
|
||||
for entries_list in self.history[: self.window_size]:
|
||||
for entry in entries_list:
|
||||
@ -100,7 +99,7 @@ class MessageCache:
|
||||
"""
|
||||
Shift the window over by 1 position, dropping the last element of the history.
|
||||
"""
|
||||
last_entries: List[CacheEntry] = self.history[len(self.history) - 1]
|
||||
last_entries: list[CacheEntry] = self.history[len(self.history) - 1]
|
||||
|
||||
for entry in last_entries:
|
||||
self.msgs.pop(entry.mid)
|
||||
|
||||
@ -3,6 +3,9 @@ from __future__ import (
|
||||
)
|
||||
|
||||
import base64
|
||||
from collections.abc import (
|
||||
KeysView,
|
||||
)
|
||||
import functools
|
||||
import hashlib
|
||||
import logging
|
||||
@ -10,7 +13,6 @@ import time
|
||||
from typing import (
|
||||
TYPE_CHECKING,
|
||||
Callable,
|
||||
KeysView,
|
||||
NamedTuple,
|
||||
cast,
|
||||
)
|
||||
|
||||
@ -1,8 +1,10 @@
|
||||
from collections.abc import (
|
||||
AsyncIterator,
|
||||
)
|
||||
from types import (
|
||||
TracebackType,
|
||||
)
|
||||
from typing import (
|
||||
AsyncIterator,
|
||||
Optional,
|
||||
Type,
|
||||
)
|
||||
|
||||
@ -1,5 +1,7 @@
|
||||
from typing import (
|
||||
from collections.abc import (
|
||||
Awaitable,
|
||||
)
|
||||
from typing import (
|
||||
Callable,
|
||||
Union,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user