| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | use crate::proto; |
| | use crate::topic::Topic; |
| | use asynchronous_codec::Framed; |
| | use bytes::Bytes; |
| | use futures::{ |
| | io::{AsyncRead, AsyncWrite}, |
| | Future, |
| | }; |
| | use futures::{SinkExt, StreamExt}; |
| | use libp2p_core::{InboundUpgrade, OutboundUpgrade, UpgradeInfo}; |
| | use libp2p_identity::PeerId; |
| | use libp2p_swarm::StreamProtocol; |
| | use std::{io, iter, pin::Pin}; |
| |
|
| | const MAX_MESSAGE_LEN_BYTES: usize = 2048; |
| |
|
| | const PROTOCOL_NAME: StreamProtocol = StreamProtocol::new("/floodsub/1.0.0"); |
| |
|
| | |
| | #[derive(Debug, Clone, Default)] |
| | pub struct FloodsubProtocol {} |
| |
|
| | impl FloodsubProtocol { |
| | |
| | pub fn new() -> FloodsubProtocol { |
| | FloodsubProtocol {} |
| | } |
| | } |
| |
|
| | impl UpgradeInfo for FloodsubProtocol { |
| | type Info = StreamProtocol; |
| | type InfoIter = iter::Once<Self::Info>; |
| |
|
| | fn protocol_info(&self) -> Self::InfoIter { |
| | iter::once(PROTOCOL_NAME) |
| | } |
| | } |
| |
|
| | impl<TSocket> InboundUpgrade<TSocket> for FloodsubProtocol |
| | where |
| | TSocket: AsyncRead + AsyncWrite + Send + Unpin + 'static, |
| | { |
| | type Output = FloodsubRpc; |
| | type Error = FloodsubError; |
| | type Future = Pin<Box<dyn Future<Output = Result<Self::Output, Self::Error>> + Send>>; |
| |
|
| | fn upgrade_inbound(self, socket: TSocket, _: Self::Info) -> Self::Future { |
| | Box::pin(async move { |
| | let mut framed = Framed::new( |
| | socket, |
| | quick_protobuf_codec::Codec::<proto::RPC>::new(MAX_MESSAGE_LEN_BYTES), |
| | ); |
| |
|
| | let rpc = framed |
| | .next() |
| | .await |
| | .ok_or_else(|| FloodsubError::ReadError(io::ErrorKind::UnexpectedEof.into()))? |
| | .map_err(CodecError)?; |
| |
|
| | let mut messages = Vec::with_capacity(rpc.publish.len()); |
| | for publish in rpc.publish.into_iter() { |
| | messages.push(FloodsubMessage { |
| | source: PeerId::from_bytes(&publish.from.unwrap_or_default()) |
| | .map_err(|_| FloodsubError::InvalidPeerId)?, |
| | data: publish.data.unwrap_or_default().into(), |
| | sequence_number: publish.seqno.unwrap_or_default(), |
| | topics: publish.topic_ids.into_iter().map(Topic::new).collect(), |
| | }); |
| | } |
| |
|
| | Ok(FloodsubRpc { |
| | messages, |
| | subscriptions: rpc |
| | .subscriptions |
| | .into_iter() |
| | .map(|sub| FloodsubSubscription { |
| | action: if Some(true) == sub.subscribe { |
| | FloodsubSubscriptionAction::Subscribe |
| | } else { |
| | FloodsubSubscriptionAction::Unsubscribe |
| | }, |
| | topic: Topic::new(sub.topic_id.unwrap_or_default()), |
| | }) |
| | .collect(), |
| | }) |
| | }) |
| | } |
| | } |
| |
|
| | |
| | #[derive(thiserror::Error, Debug)] |
| | pub enum FloodsubError { |
| | |
| | #[error("Failed to decode PeerId from message")] |
| | InvalidPeerId, |
| | |
| | #[error("Failed to decode protobuf")] |
| | ProtobufError(#[from] CodecError), |
| | |
| | #[error("Failed to read from socket")] |
| | ReadError(#[from] io::Error), |
| | } |
| |
|
| | #[derive(thiserror::Error, Debug)] |
| | #[error(transparent)] |
| | pub struct CodecError(#[from] quick_protobuf_codec::Error); |
| |
|
| | |
| | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
| | pub struct FloodsubRpc { |
| | |
| | pub messages: Vec<FloodsubMessage>, |
| | |
| | pub subscriptions: Vec<FloodsubSubscription>, |
| | } |
| |
|
| | impl UpgradeInfo for FloodsubRpc { |
| | type Info = StreamProtocol; |
| | type InfoIter = iter::Once<Self::Info>; |
| |
|
| | fn protocol_info(&self) -> Self::InfoIter { |
| | iter::once(PROTOCOL_NAME) |
| | } |
| | } |
| |
|
| | impl<TSocket> OutboundUpgrade<TSocket> for FloodsubRpc |
| | where |
| | TSocket: AsyncWrite + AsyncRead + Send + Unpin + 'static, |
| | { |
| | type Output = (); |
| | type Error = CodecError; |
| | type Future = Pin<Box<dyn Future<Output = Result<Self::Output, Self::Error>> + Send>>; |
| |
|
| | fn upgrade_outbound(self, socket: TSocket, _: Self::Info) -> Self::Future { |
| | Box::pin(async move { |
| | let mut framed = Framed::new( |
| | socket, |
| | quick_protobuf_codec::Codec::<proto::RPC>::new(MAX_MESSAGE_LEN_BYTES), |
| | ); |
| | framed.send(self.into_rpc()).await?; |
| | framed.close().await?; |
| | Ok(()) |
| | }) |
| | } |
| | } |
| |
|
| | impl FloodsubRpc { |
| | |
| | fn into_rpc(self) -> proto::RPC { |
| | proto::RPC { |
| | publish: self |
| | .messages |
| | .into_iter() |
| | .map(|msg| proto::Message { |
| | from: Some(msg.source.to_bytes()), |
| | data: Some(msg.data.to_vec()), |
| | seqno: Some(msg.sequence_number), |
| | topic_ids: msg.topics.into_iter().map(|topic| topic.into()).collect(), |
| | }) |
| | .collect(), |
| |
|
| | subscriptions: self |
| | .subscriptions |
| | .into_iter() |
| | .map(|topic| proto::SubOpts { |
| | subscribe: Some(topic.action == FloodsubSubscriptionAction::Subscribe), |
| | topic_id: Some(topic.topic.into()), |
| | }) |
| | .collect(), |
| | } |
| | } |
| | } |
| |
|
| | |
| | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
| | pub struct FloodsubMessage { |
| | |
| | pub source: PeerId, |
| |
|
| | |
| | pub data: Bytes, |
| |
|
| | |
| | pub sequence_number: Vec<u8>, |
| |
|
| | |
| | |
| | |
| | pub topics: Vec<Topic>, |
| | } |
| |
|
| | |
| | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
| | pub struct FloodsubSubscription { |
| | |
| | pub action: FloodsubSubscriptionAction, |
| | |
| | pub topic: Topic, |
| | } |
| |
|
| | |
| | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
| | pub enum FloodsubSubscriptionAction { |
| | |
| | Subscribe, |
| | |
| | Unsubscribe, |
| | } |
| |
|