2019-08-01 06:00:12 +08:00
|
|
|
from typing import Iterable, List, Sequence
|
2019-07-24 22:33:32 +08:00
|
|
|
|
2019-07-31 19:31:58 +08:00
|
|
|
from libp2p.peer.id import ID
|
2019-08-11 16:47:54 +08:00
|
|
|
from libp2p.typing import TProtocol
|
2019-09-03 16:49:00 +08:00
|
|
|
from libp2p.utils import encode_varint_prefixed
|
2019-07-24 14:54:30 +08:00
|
|
|
|
2019-03-30 04:23:30 +08:00
|
|
|
from .pb import rpc_pb2
|
2019-07-26 16:25:15 +08:00
|
|
|
from .pubsub import Pubsub
|
2019-04-02 04:55:44 +08:00
|
|
|
from .pubsub_router_interface import IPubsubRouter
|
2019-03-31 06:49:50 +08:00
|
|
|
|
2019-08-31 00:18:22 +08:00
|
|
|
PROTOCOL_ID = TProtocol("/floodsub/1.0.0")
|
|
|
|
|
2019-03-24 01:52:02 +08:00
|
|
|
|
|
|
|
class FloodSub(IPubsubRouter):
|
|
|
|
|
2019-08-11 16:47:54 +08:00
|
|
|
protocols: List[TProtocol]
|
2019-07-26 16:25:15 +08:00
|
|
|
|
|
|
|
pubsub: Pubsub
|
|
|
|
|
2019-08-11 16:47:54 +08:00
|
|
|
def __init__(self, protocols: Sequence[TProtocol]) -> None:
|
2019-07-27 11:27:47 +08:00
|
|
|
self.protocols = list(protocols)
|
2019-03-24 01:52:02 +08:00
|
|
|
self.pubsub = None
|
|
|
|
|
2019-08-11 16:47:54 +08:00
|
|
|
def get_protocols(self) -> List[TProtocol]:
|
2019-03-24 01:52:02 +08:00
|
|
|
"""
|
|
|
|
:return: the list of protocols supported by the router
|
|
|
|
"""
|
|
|
|
return self.protocols
|
|
|
|
|
2019-07-26 16:25:15 +08:00
|
|
|
def attach(self, pubsub: Pubsub) -> None:
|
2019-03-24 01:52:02 +08:00
|
|
|
"""
|
|
|
|
Attach is invoked by the PubSub constructor to attach the router to a
|
|
|
|
freshly initialized PubSub instance.
|
|
|
|
:param pubsub: pubsub instance to attach to
|
|
|
|
"""
|
|
|
|
self.pubsub = pubsub
|
|
|
|
|
2019-08-11 16:47:54 +08:00
|
|
|
def add_peer(self, peer_id: ID, protocol_id: TProtocol) -> None:
|
2019-03-24 01:52:02 +08:00
|
|
|
"""
|
|
|
|
Notifies the router that a new peer has been connected
|
|
|
|
:param peer_id: id of peer to add
|
|
|
|
"""
|
|
|
|
|
2019-07-27 11:27:47 +08:00
|
|
|
def remove_peer(self, peer_id: ID) -> None:
|
2019-03-24 01:52:02 +08:00
|
|
|
"""
|
|
|
|
Notifies the router that a peer has been disconnected
|
|
|
|
:param peer_id: id of peer to remove
|
|
|
|
"""
|
|
|
|
|
2019-08-11 16:47:54 +08:00
|
|
|
async def handle_rpc(self, rpc: rpc_pb2.RPC, sender_peer_id: ID) -> None:
|
2019-03-24 01:52:02 +08:00
|
|
|
"""
|
|
|
|
Invoked to process control messages in the RPC envelope.
|
|
|
|
It is invoked after subscriptions and payload messages have been processed
|
|
|
|
:param rpc: rpc message
|
|
|
|
"""
|
|
|
|
|
2019-07-29 12:09:35 +08:00
|
|
|
async def publish(self, msg_forwarder: ID, pubsub_msg: rpc_pb2.Message) -> None:
|
2019-03-24 01:52:02 +08:00
|
|
|
"""
|
|
|
|
Invoked to forward a new message that has been validated.
|
|
|
|
This is where the "flooding" part of floodsub happens
|
|
|
|
|
|
|
|
With flooding, routing is almost trivial: for each incoming message,
|
|
|
|
forward to all known peers in the topic. There is a bit of logic,
|
|
|
|
as the router maintains a timed cache of previous messages,
|
|
|
|
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.
|
2019-07-29 12:09:35 +08:00
|
|
|
:param msg_forwarder: peer ID of the peer who forwards the message to us
|
2019-07-25 14:08:16 +08:00
|
|
|
:param pubsub_msg: pubsub message in protobuf.
|
2019-03-24 01:52:02 +08:00
|
|
|
"""
|
2019-07-24 22:33:32 +08:00
|
|
|
|
|
|
|
peers_gen = self._get_peers_to_send(
|
2019-08-14 05:36:42 +08:00
|
|
|
pubsub_msg.topicIDs,
|
|
|
|
msg_forwarder=msg_forwarder,
|
|
|
|
origin=ID(pubsub_msg.from_id),
|
2019-07-24 22:33:32 +08:00
|
|
|
)
|
2019-08-01 06:00:12 +08:00
|
|
|
rpc_msg = rpc_pb2.RPC(publish=[pubsub_msg])
|
2019-07-24 22:33:32 +08:00
|
|
|
for peer_id in peers_gen:
|
2019-08-01 12:05:28 +08:00
|
|
|
stream = self.pubsub.peers[peer_id]
|
2019-07-25 23:11:27 +08:00
|
|
|
# FIXME: We should add a `WriteMsg` similar to write delimited messages.
|
|
|
|
# Ref: https://github.com/libp2p/go-libp2p-pubsub/blob/master/comm.go#L107
|
2019-09-03 16:49:00 +08:00
|
|
|
await stream.write(encode_varint_prefixed(rpc_msg.SerializeToString()))
|
2019-07-24 22:33:32 +08:00
|
|
|
|
2019-07-27 11:27:47 +08:00
|
|
|
async def join(self, topic: str) -> None:
|
2019-07-26 18:35:25 +08:00
|
|
|
"""
|
|
|
|
Join notifies the router that we want to receive and
|
|
|
|
forward messages in a topic. It is invoked after the
|
|
|
|
subscription announcement
|
|
|
|
:param topic: topic to join
|
|
|
|
"""
|
|
|
|
|
2019-07-27 11:27:47 +08:00
|
|
|
async def leave(self, topic: str) -> None:
|
2019-07-26 18:35:25 +08:00
|
|
|
"""
|
|
|
|
Leave notifies the router that we are no longer interested in a topic.
|
|
|
|
It is invoked after the unsubscription announcement.
|
|
|
|
:param topic: topic to leave
|
|
|
|
"""
|
|
|
|
|
2019-07-24 22:33:32 +08:00
|
|
|
def _get_peers_to_send(
|
2019-08-01 06:00:12 +08:00
|
|
|
self, topic_ids: Iterable[str], msg_forwarder: ID, origin: ID
|
|
|
|
) -> Iterable[ID]:
|
2019-07-25 14:08:16 +08:00
|
|
|
"""
|
2019-07-25 23:11:27 +08:00
|
|
|
Get the eligible peers to send the data to.
|
2019-07-29 12:09:35 +08:00
|
|
|
:param msg_forwarder: peer ID of the peer who forwards the message to us.
|
2019-07-28 18:06:38 +08:00
|
|
|
:param origin: peer id of the peer the message originate from.
|
2019-07-25 23:11:27 +08:00
|
|
|
:return: a generator of the peer ids who we send data to.
|
2019-07-25 14:08:16 +08:00
|
|
|
"""
|
2019-07-24 22:33:32 +08:00
|
|
|
for topic in topic_ids:
|
2019-07-25 14:08:16 +08:00
|
|
|
if topic not in self.pubsub.peer_topics:
|
2019-07-24 14:35:14 +08:00
|
|
|
continue
|
2019-08-01 12:05:28 +08:00
|
|
|
for peer_id in self.pubsub.peer_topics[topic]:
|
2019-07-29 12:09:35 +08:00
|
|
|
if peer_id in (msg_forwarder, origin):
|
2019-07-24 22:33:32 +08:00
|
|
|
continue
|
2019-08-01 12:05:28 +08:00
|
|
|
if peer_id not in self.pubsub.peers:
|
2019-07-24 22:33:32 +08:00
|
|
|
continue
|
|
|
|
yield peer_id
|