mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2026-02-10 15:10:54 +00:00
Reflect PR feedback
* Rename `src` to `msg_forwarder` in pubsub/floodsub/gossipsub * Rename Variables * Sort imports * Clean up
This commit is contained in:
@ -51,7 +51,7 @@ class FloodSub(IPubsubRouter):
|
||||
:param rpc: rpc message
|
||||
"""
|
||||
|
||||
async def publish(self, src: ID, pubsub_msg: rpc_pb2.Message) -> None:
|
||||
async def publish(self, msg_forwarder: ID, pubsub_msg: rpc_pb2.Message) -> None:
|
||||
"""
|
||||
Invoked to forward a new message that has been validated.
|
||||
This is where the "flooding" part of floodsub happens
|
||||
@ -62,13 +62,13 @@ class FloodSub(IPubsubRouter):
|
||||
so that seen messages are not further forwarded.
|
||||
It also never forwards a message back to the source
|
||||
or the peer that forwarded the message.
|
||||
:param src: peer ID of the peer who forwards the message to us
|
||||
:param msg_forwarder: peer ID of the peer who forwards the message to us
|
||||
:param pubsub_msg: pubsub message in protobuf.
|
||||
"""
|
||||
|
||||
peers_gen = self._get_peers_to_send(
|
||||
pubsub_msg.topicIDs,
|
||||
src=src,
|
||||
msg_forwarder=msg_forwarder,
|
||||
origin=ID(pubsub_msg.from_id),
|
||||
)
|
||||
rpc_msg = rpc_pb2.RPC(
|
||||
@ -98,11 +98,11 @@ class FloodSub(IPubsubRouter):
|
||||
def _get_peers_to_send(
|
||||
self,
|
||||
topic_ids: Iterable[str],
|
||||
src: ID,
|
||||
msg_forwarder: ID,
|
||||
origin: ID) -> Iterable[ID]:
|
||||
"""
|
||||
Get the eligible peers to send the data to.
|
||||
:param src: peer ID of the peer who forwards the message to us.
|
||||
:param msg_forwarder: peer ID of the peer who forwards the message to us.
|
||||
:param origin: peer id of the peer the message originate from.
|
||||
:return: a generator of the peer ids who we send data to.
|
||||
"""
|
||||
@ -111,7 +111,7 @@ class FloodSub(IPubsubRouter):
|
||||
continue
|
||||
for peer_id_str in self.pubsub.peer_topics[topic]:
|
||||
peer_id = id_b58_decode(peer_id_str)
|
||||
if peer_id in (src, origin):
|
||||
if peer_id in (msg_forwarder, origin):
|
||||
continue
|
||||
# FIXME: Should change `self.pubsub.peers` to Dict[PeerID, ...]
|
||||
if str(peer_id) not in self.pubsub.peers:
|
||||
|
||||
@ -119,7 +119,7 @@ class GossipSub(IPubsubRouter):
|
||||
for prune in control_message.prune:
|
||||
await self.handle_prune(prune, sender_peer_id)
|
||||
|
||||
async def publish(self, src: ID, pubsub_msg: rpc_pb2.Message) -> None:
|
||||
async def publish(self, msg_forwarder: ID, pubsub_msg: rpc_pb2.Message) -> None:
|
||||
# pylint: disable=too-many-locals
|
||||
"""
|
||||
Invoked to forward a new message that has been validated.
|
||||
@ -128,7 +128,7 @@ class GossipSub(IPubsubRouter):
|
||||
|
||||
peers_gen = self._get_peers_to_send(
|
||||
pubsub_msg.topicIDs,
|
||||
src=src,
|
||||
msg_forwarder=msg_forwarder,
|
||||
origin=ID(pubsub_msg.from_id),
|
||||
)
|
||||
rpc_msg = rpc_pb2.RPC(
|
||||
@ -144,11 +144,11 @@ class GossipSub(IPubsubRouter):
|
||||
def _get_peers_to_send(
|
||||
self,
|
||||
topic_ids: Iterable[str],
|
||||
src: ID,
|
||||
msg_forwarder: ID,
|
||||
origin: ID) -> Iterable[ID]:
|
||||
"""
|
||||
Get the eligible peers to send the data to.
|
||||
:param src: the peer id of the peer who forwards the message to me.
|
||||
:param msg_forwarder: the peer id of the peer who forwards the message to me.
|
||||
:param origin: peer id of the peer the message originate from.
|
||||
:return: a generator of the peer ids who we send data to.
|
||||
"""
|
||||
@ -167,10 +167,10 @@ class GossipSub(IPubsubRouter):
|
||||
|
||||
# gossipsub peers
|
||||
# FIXME: Change `str` to `ID`
|
||||
gossipsub_peers: List[str] = None
|
||||
in_topic_gossipsub_peers: List[str] = None
|
||||
# TODO: Do we need to check `topic in self.pubsub.my_topics`?
|
||||
if topic in self.mesh:
|
||||
gossipsub_peers = self.mesh[topic]
|
||||
in_topic_gossipsub_peers = self.mesh[topic]
|
||||
else:
|
||||
# TODO(robzajac): Is topic DEFINITELY supposed to be in fanout if we are not
|
||||
# subscribed?
|
||||
@ -185,11 +185,11 @@ class GossipSub(IPubsubRouter):
|
||||
self.degree,
|
||||
[],
|
||||
)
|
||||
gossipsub_peers = self.fanout[topic]
|
||||
for peer_id_str in gossipsub_peers:
|
||||
in_topic_gossipsub_peers = self.fanout[topic]
|
||||
for peer_id_str in in_topic_gossipsub_peers:
|
||||
send_to.add(id_b58_decode(peer_id_str))
|
||||
# Excludes `src` and `origin`
|
||||
yield from send_to.difference([src, origin])
|
||||
# Excludes `msg_forwarder` and `origin`
|
||||
yield from send_to.difference([msg_forwarder, origin])
|
||||
|
||||
async def join(self, topic):
|
||||
# Note: the comments here are the near-exact algorithm description from the spec
|
||||
|
||||
@ -142,7 +142,7 @@ class Pubsub:
|
||||
continue
|
||||
# TODO(mhchia): This will block this read_stream loop until all data are pushed.
|
||||
# Should investigate further if this is an issue.
|
||||
await self.push_msg(src=peer_id, msg=msg)
|
||||
await self.push_msg(msg_forwarder=peer_id, msg=msg)
|
||||
|
||||
if rpc_incoming.subscriptions:
|
||||
# deal with RPC.subscriptions
|
||||
@ -331,10 +331,10 @@ class Pubsub:
|
||||
|
||||
await self.push_msg(self.host.get_id(), msg)
|
||||
|
||||
async def push_msg(self, src: ID, msg: rpc_pb2.Message) -> None:
|
||||
async def push_msg(self, msg_forwarder: ID, msg: rpc_pb2.Message) -> None:
|
||||
"""
|
||||
Push a pubsub message to others.
|
||||
:param src: the peer who forward us the message.
|
||||
:param msg_forwarder: the peer who forward us the message.
|
||||
:param msg: the message we are going to push out.
|
||||
"""
|
||||
# TODO: - Check if the `source` is in the blacklist. If yes, reject.
|
||||
@ -350,7 +350,7 @@ class Pubsub:
|
||||
|
||||
self._mark_msg_seen(msg)
|
||||
await self.handle_talk(msg)
|
||||
await self.router.publish(src, msg)
|
||||
await self.router.publish(msg_forwarder, msg)
|
||||
|
||||
def _next_seqno(self) -> bytes:
|
||||
"""
|
||||
|
||||
@ -42,10 +42,10 @@ class IPubsubRouter(ABC):
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
async def publish(self, src, pubsub_msg) -> None:
|
||||
async def publish(self, msg_forwarder, pubsub_msg):
|
||||
"""
|
||||
Invoked to forward a new message that has been validated
|
||||
:param src: peer_id of message sender
|
||||
:param msg_forwarder: peer_id of message sender
|
||||
:param pubsub_msg: pubsub message to forward
|
||||
"""
|
||||
|
||||
|
||||
Reference in New Issue
Block a user