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::{Message, RawMessage, TopicHash};
29
30/// A general trait of transforming a [`RawMessage`] into a [`Message`]. The
31///
32/// [`RawMessage`] is obtained from the wire and the [`Message`] is used to
33/// calculate the [`crate::MessageId`] of the message and is what is sent to the application.
34///
35/// The inbound/outbound transforms must be inverses. Applying the inbound transform and then the
36/// outbound transform MUST leave the underlying data un-modified.
37///
38/// By default, this is the identity transform for all fields in [`Message`].
39pub trait DataTransform {
40 /// Takes a [`RawMessage`] received and converts it to a [`Message`].
41 fn inbound_transform(&self, raw_message: RawMessage) -> Result<Message, std::io::Error>;
42
43 /// Takes the data to be published (a topic and associated data) transforms the data. The
44 /// transformed data will then be used to create a [`crate::RawMessage`] to be sent to peers.
45 fn outbound_transform(
46 &self,
47 topic: &TopicHash,
48 data: Vec<u8>,
49 ) -> Result<Vec<u8>, std::io::Error>;
50}
51
52/// The default transform, the raw data is propagated as is to the application layer gossipsub.
53#[derive(Default, Clone)]
54pub struct IdentityTransform;
55
56impl DataTransform for IdentityTransform {
57 fn inbound_transform(&self, raw_message: RawMessage) -> Result<Message, std::io::Error> {
58 Ok(Message {
59 source: raw_message.source,
60 data: raw_message.data,
61 sequence_number: raw_message.sequence_number,
62 topic: raw_message.topic,
63 })
64 }
65
66 fn outbound_transform(
67 &self,
68 _topic: &TopicHash,
69 data: Vec<u8>,
70 ) -> Result<Vec<u8>, std::io::Error> {
71 Ok(data)
72 }
73}