cs_mwc_libp2p_gossipsub/
transform.rs

1// Copyright 2020 Sigma Prime Pty Ltd.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the "Software"),
5// to deal in the Software without restriction, including without limitation
6// the rights to use, copy, modify, merge, publish, distribute, sublicense,
7// and/or sell copies of the Software, and to permit persons to whom the
8// Software is furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
14// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19// DEALINGS IN THE SOFTWARE.
20
21//! This trait allows of extended user-level decoding that can apply to message-data before a
22//! message-id is calculated.
23//!
24//! This is primarily designed to allow applications to implement their own custom compression
25//! algorithms that can be topic-specific. Once the raw data is transformed the message-id is then
26//! calculated, allowing for applications to employ message-id functions post compression.
27
28use crate::{GossipsubMessage, RawGossipsubMessage, TopicHash};
29
30/// A general trait of transforming a [`RawGossipsubMessage`] into a [`GossipsubMessage`]. The
31/// [`RawGossipsubMessage`] is obtained from the wire and the [`GossipsubMessage`] is used to
32/// calculate the [`crate::MessageId`] of the message and is what is sent to the application.
33///
34/// The inbound/outbound transforms must be inverses. Applying the inbound transform and then the
35/// outbound transform MUST leave the underlying data un-modified.
36///
37/// By default, this is the identity transform for all fields in [`GossipsubMessage`].
38pub trait DataTransform {
39    /// Takes a [`RawGossipsubMessage`] received and converts it to a [`GossipsubMessage`].
40    fn inbound_transform(
41        &self,
42        raw_message: RawGossipsubMessage,
43    ) -> Result<GossipsubMessage, std::io::Error>;
44
45    /// Takes the data to be published (a topic and associated data) transforms the data. The
46    /// transformed data will then be used to create a [`crate::RawGossipsubMessage`] to be sent to peers.
47    fn outbound_transform(
48        &self,
49        topic: &TopicHash,
50        data: Vec<u8>,
51    ) -> Result<Vec<u8>, std::io::Error>;
52}
53
54/// The default transform, the raw data is propagated as is to the application layer gossipsub.
55#[derive(Default, Clone)]
56pub struct IdentityTransform;
57
58impl DataTransform for IdentityTransform {
59    fn inbound_transform(
60        &self,
61        raw_message: RawGossipsubMessage,
62    ) -> Result<GossipsubMessage, std::io::Error> {
63        Ok(GossipsubMessage {
64            source: raw_message.source,
65            data: raw_message.data,
66            sequence_number: raw_message.sequence_number,
67            topic: raw_message.topic,
68        })
69    }
70
71    fn outbound_transform(
72        &self,
73        _topic: &TopicHash,
74        data: Vec<u8>,
75    ) -> Result<Vec<u8>, std::io::Error> {
76        Ok(data)
77    }
78}