libp2p_gossipsub/
rpc_proto.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
21pub(crate) mod proto {
22    #![allow(unreachable_pub)]
23    include!("generated/mod.rs");
24    pub use self::gossipsub::pb::{mod_RPC::SubOpts, *};
25}
26
27#[cfg(test)]
28mod test {
29    use libp2p_identity::PeerId;
30    use quick_protobuf::{BytesReader, MessageRead, MessageWrite, Writer};
31    use rand::Rng;
32
33    use crate::{rpc_proto::proto::compat, IdentTopic as Topic};
34
35    #[test]
36    fn test_multi_topic_message_compatibility() {
37        let topic1 = Topic::new("t1").hash();
38        let topic2 = Topic::new("t2").hash();
39
40        let new_message1 = super::proto::Message {
41            from: Some(PeerId::random().to_bytes()),
42            data: Some(rand::thread_rng().gen::<[u8; 32]>().to_vec()),
43            seqno: Some(rand::thread_rng().gen::<[u8; 8]>().to_vec()),
44            topic: topic1.clone().into_string(),
45            signature: Some(rand::thread_rng().gen::<[u8; 32]>().to_vec()),
46            key: Some(rand::thread_rng().gen::<[u8; 32]>().to_vec()),
47        };
48        let old_message1 = compat::pb::Message {
49            from: Some(PeerId::random().to_bytes()),
50            data: Some(rand::thread_rng().gen::<[u8; 32]>().to_vec()),
51            seqno: Some(rand::thread_rng().gen::<[u8; 8]>().to_vec()),
52            topic_ids: vec![topic1.clone().into_string()],
53            signature: Some(rand::thread_rng().gen::<[u8; 32]>().to_vec()),
54            key: Some(rand::thread_rng().gen::<[u8; 32]>().to_vec()),
55        };
56        let old_message2 = compat::pb::Message {
57            from: Some(PeerId::random().to_bytes()),
58            data: Some(rand::thread_rng().gen::<[u8; 32]>().to_vec()),
59            seqno: Some(rand::thread_rng().gen::<[u8; 8]>().to_vec()),
60            topic_ids: vec![topic1.clone().into_string(), topic2.clone().into_string()],
61            signature: Some(rand::thread_rng().gen::<[u8; 32]>().to_vec()),
62            key: Some(rand::thread_rng().gen::<[u8; 32]>().to_vec()),
63        };
64
65        let mut new_message1b = Vec::with_capacity(new_message1.get_size());
66        let mut writer = Writer::new(&mut new_message1b);
67        new_message1.write_message(&mut writer).unwrap();
68
69        let mut old_message1b = Vec::with_capacity(old_message1.get_size());
70        let mut writer = Writer::new(&mut old_message1b);
71        old_message1.write_message(&mut writer).unwrap();
72
73        let mut old_message2b = Vec::with_capacity(old_message2.get_size());
74        let mut writer = Writer::new(&mut old_message2b);
75        old_message2.write_message(&mut writer).unwrap();
76
77        let mut reader = BytesReader::from_bytes(&old_message1b[..]);
78        let new_message =
79            super::proto::Message::from_reader(&mut reader, &old_message1b[..]).unwrap();
80        assert_eq!(new_message.topic, topic1.clone().into_string());
81
82        let mut reader = BytesReader::from_bytes(&old_message2b[..]);
83        let new_message =
84            super::proto::Message::from_reader(&mut reader, &old_message2b[..]).unwrap();
85        assert_eq!(new_message.topic, topic2.into_string());
86
87        let mut reader = BytesReader::from_bytes(&new_message1b[..]);
88        let old_message =
89            compat::pb::Message::from_reader(&mut reader, &new_message1b[..]).unwrap();
90        assert_eq!(old_message.topic_ids, vec![topic1.into_string()]);
91    }
92}