Skip to content

Commit

Permalink
Fix tests in libp2p-gossipsub
Browse files Browse the repository at this point in the history
  • Loading branch information
kckeiks committed Oct 25, 2022
1 parent e7ff68c commit fb7e6db
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 23 deletions.
18 changes: 12 additions & 6 deletions protocols/gossipsub/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3672,6 +3672,12 @@ mod local_test {
use asynchronous_codec::Encoder;
use quickcheck::*;

macro_rules! to_usize {
($object_size: expr ) => {{
usize::try_from($object_size).unwrap()
}}
}

fn empty_rpc() -> GossipsubRpc {
GossipsubRpc {
subscriptions: Vec::new(),
Expand Down Expand Up @@ -3748,7 +3754,7 @@ mod local_test {

// Messages over the limit should be split

while rpc_proto.encoded_len() < max_transmit_size {
while to_usize!(rpc_proto.compute_size()) < max_transmit_size {
rpc.messages.push(test_message());
rpc_proto = rpc.clone().into_protobuf();
}
Expand All @@ -3765,7 +3771,7 @@ mod local_test {
// all fragmented messages should be under the limit
for message in fragmented_messages {
assert!(
message.encoded_len() < max_transmit_size,
to_usize!(message.compute_size()) < max_transmit_size,
"all messages should be less than the transmission size"
);
}
Expand All @@ -3792,7 +3798,7 @@ mod local_test {
.fragment_message(rpc_proto.clone())
.expect("Messages must be valid");

if rpc_proto.encoded_len() < max_transmit_size {
if to_usize!(rpc_proto.compute_size()) < max_transmit_size {
assert_eq!(
fragmented_messages.len(),
1,
Expand All @@ -3808,12 +3814,12 @@ mod local_test {
// all fragmented messages should be under the limit
for message in fragmented_messages {
assert!(
message.encoded_len() < max_transmit_size,
"all messages should be less than the transmission size: list size {} max size{}", message.encoded_len(), max_transmit_size
to_usize!(message.compute_size()) < max_transmit_size,
"all messages should be less than the transmission size: list size {} max size{}", to_usize!(message.compute_size()), max_transmit_size
);

// ensure they can all be encoded
let mut buf = bytes::BytesMut::with_capacity(message.encoded_len());
let mut buf = bytes::BytesMut::with_capacity(to_usize!(message.compute_size()));
codec.encode(message, &mut buf).unwrap()
}
}
Expand Down
4 changes: 2 additions & 2 deletions protocols/gossipsub/src/behaviour/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,14 +256,14 @@ fn proto_to_message(rpc: &crate::rpc_proto::RPC) -> GossipsubRpc {
source: message.from.map(|x| PeerId::from_bytes(&x).unwrap()),
data: message.data.unwrap_or_default(),
sequence_number: message.seqno.map(|x| BigEndian::read_u64(&x)), // don't inform the application
topic: TopicHash::from_raw(message.topic),
topic: TopicHash::from_raw(message.topic.unwrap()),
signature: message.signature, // don't inform the application
key: None,
validated: false,
});
}
let mut control_msgs = Vec::new();
if let Some(rpc_control) = rpc.control {
if let Some(rpc_control) = rpc.control.into_option() {
// Collect the gossipsub control messages
let ihave_msgs: Vec<GossipsubControlAction> = rpc_control
.ihave
Expand Down
31 changes: 16 additions & 15 deletions protocols/gossipsub/src/rpc_proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ pub use protos::rpc::*;
mod test {
use crate::IdentTopic as Topic;
use libp2p_core::PeerId;
use prost::Message;
use protobuf::Message;
use rand::Rng;

mod compat_proto {
include!(concat!(env!("OUT_DIR"), "/compat.pb.rs"));
mod protos {
include!(concat!(env!("OUT_DIR"), "/protos/mod.rs"));
}
use protos::compat as compat_proto;

#[test]
fn test_multi_topic_message_compatibility() {
Expand All @@ -44,9 +45,10 @@ mod test {
from: Some(PeerId::random().to_bytes()),
data: Some(rand::thread_rng().gen::<[u8; 32]>().to_vec()),
seqno: Some(rand::thread_rng().gen::<[u8; 8]>().to_vec()),
topic: topic1.clone().into_string(),
topic: Some(topic1.clone().into_string()),
signature: Some(rand::thread_rng().gen::<[u8; 32]>().to_vec()),
key: Some(rand::thread_rng().gen::<[u8; 32]>().to_vec()),
..super::Message::default()
};
let old_message1 = compat_proto::Message {
from: Some(PeerId::random().to_bytes()),
Expand All @@ -55,6 +57,7 @@ mod test {
topic_ids: vec![topic1.clone().into_string()],
signature: Some(rand::thread_rng().gen::<[u8; 32]>().to_vec()),
key: Some(rand::thread_rng().gen::<[u8; 32]>().to_vec()),
..compat_proto::Message::default()
};
let old_message2 = compat_proto::Message {
from: Some(PeerId::random().to_bytes()),
Expand All @@ -63,24 +66,22 @@ mod test {
topic_ids: vec![topic1.clone().into_string(), topic2.clone().into_string()],
signature: Some(rand::thread_rng().gen::<[u8; 32]>().to_vec()),
key: Some(rand::thread_rng().gen::<[u8; 32]>().to_vec()),
..compat_proto::Message::default()
};

let mut new_message1b = Vec::with_capacity(new_message1.encoded_len());
new_message1.encode(&mut new_message1b).unwrap();
let new_message1b = new_message1.write_to_bytes().unwrap();

let mut old_message1b = Vec::with_capacity(old_message1.encoded_len());
old_message1.encode(&mut old_message1b).unwrap();
let old_message1b = old_message1.write_to_bytes().unwrap();

let mut old_message2b = Vec::with_capacity(old_message2.encoded_len());
old_message2.encode(&mut old_message2b).unwrap();
let old_message2b = old_message2.write_to_bytes().unwrap();

let new_message = super::Message::decode(&old_message1b[..]).unwrap();
assert_eq!(new_message.topic, topic1.clone().into_string());
let new_message = super::Message::parse_from_bytes(&old_message1b[..]).unwrap();
assert_eq!(new_message.topic.unwrap(), topic1.clone().into_string());

let new_message = super::Message::decode(&old_message2b[..]).unwrap();
assert_eq!(new_message.topic, topic2.into_string());
let new_message = super::Message::parse_from_bytes(&old_message2b[..]).unwrap();
assert_eq!(new_message.topic.unwrap(), topic2.into_string());

let old_message = compat_proto::Message::decode(&new_message1b[..]).unwrap();
let old_message = compat_proto::Message::parse_from_bytes(&new_message1b[..]).unwrap();
assert_eq!(old_message.topic_ids, vec![topic1.into_string()]);
}
}

0 comments on commit fb7e6db

Please sign in to comment.