2019-07-24 22:33:32 +08:00
|
|
|
from typing import (
|
2019-07-25 14:08:16 +08:00
|
|
|
Iterable,
|
2019-07-26 16:25:15 +08:00
|
|
|
List,
|
|
|
|
Sequence,
|
2019-07-24 22:33:32 +08:00
|
|
|
)
|
|
|
|
|
2019-07-24 14:54:30 +08:00
|
|
|
from libp2p.peer.id import (
|
|
|
|
ID,
|
2019-07-25 14:08:16 +08:00
|
|
|
id_b58_decode,
|
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-07-26 16:25:15 +08:00
|
|
|
from libp2p.network.stream.net_stream_interface import (
|
|
|
|
INetStream,
|
|
|
|
)
|
2019-03-31 06:49:50 +08:00
|
|
|
|
2019-03-24 01:52:02 +08:00
|
|
|
|
|
|
|
class FloodSub(IPubsubRouter):
|
2019-04-02 04:55:44 +08:00
|
|
|
# pylint: disable=no-member
|
2019-03-24 01:52:02 +08:00
|
|
|
|
2019-07-26 16:25:15 +08:00
|
|
|
protocols: List[str]
|
|
|
|
|
|
|
|
pubsub: Pubsub
|
|
|
|
|
|
|
|
def __init__(self, protocols: Sequence[str]) -> 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-07-26 16:25:15 +08:00
|
|
|
def get_protocols(self) -> List[str]:
|
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-07-27 11:27:47 +08:00
|
|
|
# FIXME: Should be changed to type 'peer.ID'
|
|
|
|
def add_peer(self, peer_id: str, protocol_id: str) -> 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-07-27 11:27:47 +08:00
|
|
|
# FIXME: Should be changed to type 'peer.ID'
|
|
|
|
async def handle_rpc(self, rpc: rpc_pb2.ControlMessage, sender_peer_id: str) -> 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-07-25 14:08:16 +08:00
|
|
|
pubsub_msg.topicIDs,
|
2019-07-29 12:09:35 +08:00
|
|
|
msg_forwarder=msg_forwarder,
|
2019-07-25 14:08:16 +08:00
|
|
|
origin=ID(pubsub_msg.from_id),
|
2019-07-24 22:33:32 +08:00
|
|
|
)
|
|
|
|
rpc_msg = rpc_pb2.RPC(
|
2019-07-25 14:08:16 +08:00
|
|
|
publish=[pubsub_msg],
|
2019-07-24 22:33:32 +08:00
|
|
|
)
|
|
|
|
for peer_id in peers_gen:
|
|
|
|
stream = self.pubsub.peers[str(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-07-24 22:33:32 +08:00
|
|
|
await stream.write(rpc_msg.SerializeToString())
|
|
|
|
|
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(
|
|
|
|
self,
|
2019-07-25 14:08:16 +08:00
|
|
|
topic_ids: Iterable[str],
|
2019-07-29 12:09:35 +08:00
|
|
|
msg_forwarder: ID,
|
2019-07-25 14:08:16 +08:00
|
|
|
origin: ID) -> Iterable[ID]:
|
|
|
|
"""
|
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-07-25 14:08:16 +08:00
|
|
|
for peer_id_str in self.pubsub.peer_topics[topic]:
|
|
|
|
peer_id = id_b58_decode(peer_id_str)
|
2019-07-29 12:09:35 +08:00
|
|
|
if peer_id in (msg_forwarder, origin):
|
2019-07-24 22:33:32 +08:00
|
|
|
continue
|
|
|
|
# FIXME: Should change `self.pubsub.peers` to Dict[PeerID, ...]
|
|
|
|
if str(peer_id) not in self.pubsub.peers:
|
|
|
|
continue
|
|
|
|
yield peer_id
|