2018-11-13 14:46:57 +01:00
|
|
|
// Copyright 2018 Parity Technologies (UK) Ltd.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the "Software"),
|
|
|
|
// to deal in the Software without restriction, including without limitation
|
|
|
|
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
// and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
// Software is furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
// DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2018-11-15 17:41:11 +01:00
|
|
|
use crate::rpc_proto;
|
2020-01-27 15:23:01 +01:00
|
|
|
use crate::topic::Topic;
|
2019-01-29 16:20:14 +01:00
|
|
|
use libp2p_core::{InboundUpgrade, OutboundUpgrade, UpgradeInfo, PeerId, upgrade};
|
2020-01-15 12:02:02 +01:00
|
|
|
use prost::Message;
|
2019-10-07 11:32:47 +02:00
|
|
|
use std::{error, fmt, io, iter, pin::Pin};
|
2020-01-27 15:23:01 +01:00
|
|
|
use futures::{Future, io::{AsyncRead, AsyncWrite}};
|
2018-11-13 14:46:57 +01:00
|
|
|
|
2018-11-14 14:07:54 +01:00
|
|
|
/// Implementation of `ConnectionUpgrade` for the floodsub protocol.
|
2019-01-22 14:45:03 +01:00
|
|
|
#[derive(Debug, Clone, Default)]
|
2020-04-09 16:15:17 +03:00
|
|
|
pub struct FloodsubProtocol {}
|
2018-11-13 14:46:57 +01:00
|
|
|
|
2020-04-09 16:15:17 +03:00
|
|
|
impl FloodsubProtocol {
|
|
|
|
/// Builds a new `FloodsubProtocol`.
|
|
|
|
pub fn new() -> FloodsubProtocol {
|
|
|
|
FloodsubProtocol {}
|
2018-11-13 14:46:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-09 16:15:17 +03:00
|
|
|
impl UpgradeInfo for FloodsubProtocol {
|
2018-12-11 15:13:10 +01:00
|
|
|
type Info = &'static [u8];
|
|
|
|
type InfoIter = iter::Once<Self::Info>;
|
2018-11-15 17:41:11 +01:00
|
|
|
|
2018-12-11 15:13:10 +01:00
|
|
|
fn protocol_info(&self) -> Self::InfoIter {
|
|
|
|
iter::once(b"/floodsub/1.0.0")
|
2018-11-15 17:41:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-09 16:15:17 +03:00
|
|
|
impl<TSocket> InboundUpgrade<TSocket> for FloodsubProtocol
|
2018-11-13 14:46:57 +01:00
|
|
|
where
|
2019-10-07 11:32:47 +02:00
|
|
|
TSocket: AsyncRead + AsyncWrite + Send + Unpin + 'static,
|
2018-11-13 14:46:57 +01:00
|
|
|
{
|
2019-01-22 14:45:03 +01:00
|
|
|
type Output = FloodsubRpc;
|
2019-01-29 16:20:14 +01:00
|
|
|
type Error = FloodsubDecodeError;
|
2019-10-07 11:32:47 +02:00
|
|
|
type Future = Pin<Box<dyn Future<Output = Result<Self::Output, Self::Error>> + Send>>;
|
2018-11-13 14:46:57 +01:00
|
|
|
|
2020-01-13 14:34:43 +01:00
|
|
|
fn upgrade_inbound(self, mut socket: TSocket, _: Self::Info) -> Self::Future {
|
2019-10-07 11:32:47 +02:00
|
|
|
Box::pin(async move {
|
|
|
|
let packet = upgrade::read_one(&mut socket, 2048).await?;
|
2020-01-15 12:02:02 +01:00
|
|
|
let rpc = rpc_proto::Rpc::decode(&packet[..])?;
|
2019-01-29 16:20:14 +01:00
|
|
|
|
2020-01-15 12:02:02 +01:00
|
|
|
let mut messages = Vec::with_capacity(rpc.publish.len());
|
|
|
|
for publish in rpc.publish.into_iter() {
|
2019-01-29 16:20:14 +01:00
|
|
|
messages.push(FloodsubMessage {
|
2020-01-15 12:02:02 +01:00
|
|
|
source: PeerId::from_bytes(publish.from.unwrap_or_default()).map_err(|_| {
|
2019-01-29 16:20:14 +01:00
|
|
|
FloodsubDecodeError::InvalidPeerId
|
|
|
|
})?,
|
2020-01-15 12:02:02 +01:00
|
|
|
data: publish.data.unwrap_or_default(),
|
|
|
|
sequence_number: publish.seqno.unwrap_or_default(),
|
|
|
|
topics: publish.topic_ids
|
2019-01-29 16:20:14 +01:00
|
|
|
.into_iter()
|
2020-01-27 15:23:01 +01:00
|
|
|
.map(Topic::new)
|
2019-01-29 16:20:14 +01:00
|
|
|
.collect(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(FloodsubRpc {
|
|
|
|
messages,
|
2020-01-15 12:02:02 +01:00
|
|
|
subscriptions: rpc.subscriptions
|
2019-01-29 16:20:14 +01:00
|
|
|
.into_iter()
|
2020-01-15 12:02:02 +01:00
|
|
|
.map(|sub| FloodsubSubscription {
|
|
|
|
action: if Some(true) == sub.subscribe {
|
2019-01-29 16:20:14 +01:00
|
|
|
FloodsubSubscriptionAction::Subscribe
|
|
|
|
} else {
|
|
|
|
FloodsubSubscriptionAction::Unsubscribe
|
|
|
|
},
|
2020-01-27 15:23:01 +01:00
|
|
|
topic: Topic::new(sub.topic_id.unwrap_or_default()),
|
2019-01-29 16:20:14 +01:00
|
|
|
})
|
|
|
|
.collect(),
|
2019-01-22 14:45:03 +01:00
|
|
|
})
|
2019-01-29 16:20:14 +01:00
|
|
|
})
|
2018-11-13 14:46:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-29 16:20:14 +01:00
|
|
|
/// Reach attempt interrupt errors.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum FloodsubDecodeError {
|
|
|
|
/// Error when reading the packet from the socket.
|
|
|
|
ReadError(upgrade::ReadOneError),
|
|
|
|
/// Error when decoding the raw buffer into a protobuf.
|
2020-01-15 12:02:02 +01:00
|
|
|
ProtobufError(prost::DecodeError),
|
2019-01-29 16:20:14 +01:00
|
|
|
/// Error when parsing the `PeerId` in the message.
|
|
|
|
InvalidPeerId,
|
2018-11-13 14:46:57 +01:00
|
|
|
}
|
|
|
|
|
2019-01-29 16:20:14 +01:00
|
|
|
impl From<upgrade::ReadOneError> for FloodsubDecodeError {
|
|
|
|
fn from(err: upgrade::ReadOneError) -> Self {
|
|
|
|
FloodsubDecodeError::ReadError(err)
|
|
|
|
}
|
|
|
|
}
|
2018-11-13 14:46:57 +01:00
|
|
|
|
2020-01-15 12:02:02 +01:00
|
|
|
impl From<prost::DecodeError> for FloodsubDecodeError {
|
|
|
|
fn from(err: prost::DecodeError) -> Self {
|
2019-01-29 16:20:14 +01:00
|
|
|
FloodsubDecodeError::ProtobufError(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for FloodsubDecodeError {
|
2019-02-11 14:58:15 +01:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2019-01-29 16:20:14 +01:00
|
|
|
match *self {
|
|
|
|
FloodsubDecodeError::ReadError(ref err) =>
|
|
|
|
write!(f, "Error while reading from socket: {}", err),
|
|
|
|
FloodsubDecodeError::ProtobufError(ref err) =>
|
|
|
|
write!(f, "Error while decoding protobuf: {}", err),
|
|
|
|
FloodsubDecodeError::InvalidPeerId =>
|
|
|
|
write!(f, "Error while decoding PeerId from message"),
|
2018-11-13 14:46:57 +01:00
|
|
|
}
|
2019-01-29 16:20:14 +01:00
|
|
|
}
|
|
|
|
}
|
2018-11-13 14:46:57 +01:00
|
|
|
|
2019-01-29 16:20:14 +01:00
|
|
|
impl error::Error for FloodsubDecodeError {
|
|
|
|
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
|
|
|
|
match *self {
|
|
|
|
FloodsubDecodeError::ReadError(ref err) => Some(err),
|
|
|
|
FloodsubDecodeError::ProtobufError(ref err) => Some(err),
|
|
|
|
FloodsubDecodeError::InvalidPeerId => None,
|
|
|
|
}
|
2018-11-13 14:46:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// An RPC received by the floodsub system.
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
|
|
|
pub struct FloodsubRpc {
|
|
|
|
/// List of messages that were part of this RPC query.
|
|
|
|
pub messages: Vec<FloodsubMessage>,
|
|
|
|
/// List of subscriptions.
|
|
|
|
pub subscriptions: Vec<FloodsubSubscription>,
|
|
|
|
}
|
|
|
|
|
2019-01-22 14:45:03 +01:00
|
|
|
impl UpgradeInfo for FloodsubRpc {
|
|
|
|
type Info = &'static [u8];
|
|
|
|
type InfoIter = iter::Once<Self::Info>;
|
|
|
|
|
|
|
|
fn protocol_info(&self) -> Self::InfoIter {
|
|
|
|
iter::once(b"/floodsub/1.0.0")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<TSocket> OutboundUpgrade<TSocket> for FloodsubRpc
|
|
|
|
where
|
2019-10-07 11:32:47 +02:00
|
|
|
TSocket: AsyncWrite + AsyncRead + Send + Unpin + 'static,
|
2019-01-22 14:45:03 +01:00
|
|
|
{
|
|
|
|
type Output = ();
|
|
|
|
type Error = io::Error;
|
2019-10-07 11:32:47 +02:00
|
|
|
type Future = Pin<Box<dyn Future<Output = Result<Self::Output, Self::Error>> + Send>>;
|
2019-01-22 14:45:03 +01:00
|
|
|
|
2020-01-13 14:34:43 +01:00
|
|
|
fn upgrade_outbound(self, mut socket: TSocket, _: Self::Info) -> Self::Future {
|
2019-10-07 11:32:47 +02:00
|
|
|
Box::pin(async move {
|
|
|
|
let bytes = self.into_bytes();
|
|
|
|
upgrade::write_one(&mut socket, bytes).await?;
|
|
|
|
Ok(())
|
|
|
|
})
|
2019-01-22 14:45:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FloodsubRpc {
|
|
|
|
/// Turns this `FloodsubRpc` into a message that can be sent to a substream.
|
2019-01-29 16:20:14 +01:00
|
|
|
fn into_bytes(self) -> Vec<u8> {
|
2020-01-15 12:02:02 +01:00
|
|
|
let rpc = rpc_proto::Rpc {
|
|
|
|
publish: self.messages.into_iter()
|
|
|
|
.map(|msg| {
|
|
|
|
rpc_proto::Message {
|
|
|
|
from: Some(msg.source.into_bytes()),
|
|
|
|
data: Some(msg.data),
|
|
|
|
seqno: Some(msg.sequence_number),
|
|
|
|
topic_ids: msg.topics
|
|
|
|
.into_iter()
|
2020-01-27 15:23:01 +01:00
|
|
|
.map(|topic| topic.into())
|
2020-01-15 12:02:02 +01:00
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
|
|
|
|
subscriptions: self.subscriptions.into_iter()
|
|
|
|
.map(|topic| {
|
|
|
|
rpc_proto::rpc::SubOpts {
|
|
|
|
subscribe: Some(topic.action == FloodsubSubscriptionAction::Subscribe),
|
2020-01-27 15:23:01 +01:00
|
|
|
topic_id: Some(topic.topic.into())
|
2020-01-15 12:02:02 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut buf = Vec::with_capacity(rpc.encoded_len());
|
|
|
|
rpc.encode(&mut buf).expect("Vec<u8> provides capacity as needed");
|
|
|
|
buf
|
2019-01-22 14:45:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-13 14:46:57 +01:00
|
|
|
/// A message received by the floodsub system.
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
|
|
|
pub struct FloodsubMessage {
|
|
|
|
/// Id of the peer that published this message.
|
|
|
|
pub source: PeerId,
|
|
|
|
|
|
|
|
/// Content of the message. Its meaning is out of scope of this library.
|
|
|
|
pub data: Vec<u8>,
|
|
|
|
|
|
|
|
/// An incrementing sequence number.
|
|
|
|
pub sequence_number: Vec<u8>,
|
|
|
|
|
|
|
|
/// List of topics this message belongs to.
|
|
|
|
///
|
|
|
|
/// Each message can belong to multiple topics at once.
|
2020-01-27 15:23:01 +01:00
|
|
|
pub topics: Vec<Topic>,
|
2018-11-13 14:46:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// A subscription received by the floodsub system.
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
|
|
|
pub struct FloodsubSubscription {
|
|
|
|
/// Action to perform.
|
|
|
|
pub action: FloodsubSubscriptionAction,
|
|
|
|
/// The topic from which to subscribe or unsubscribe.
|
2020-01-27 15:23:01 +01:00
|
|
|
pub topic: Topic,
|
2018-11-13 14:46:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Action that a subscription wants to perform.
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
|
|
|
pub enum FloodsubSubscriptionAction {
|
|
|
|
/// The remote wants to subscribe to the given topic.
|
|
|
|
Subscribe,
|
|
|
|
/// The remote wants to unsubscribe from the given topic.
|
|
|
|
Unsubscribe,
|
|
|
|
}
|