diff --git a/protocols/gossipsub/src/behaviour.rs b/protocols/gossipsub/src/behaviour.rs index 6aa766ae791..5f9e7ccb9c0 100644 --- a/protocols/gossipsub/src/behaviour.rs +++ b/protocols/gossipsub/src/behaviour.rs @@ -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(), @@ -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(); } @@ -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" ); } @@ -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, @@ -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() } } diff --git a/protocols/gossipsub/src/behaviour/tests.rs b/protocols/gossipsub/src/behaviour/tests.rs index 476e91d829c..82de8dda73f 100644 --- a/protocols/gossipsub/src/behaviour/tests.rs +++ b/protocols/gossipsub/src/behaviour/tests.rs @@ -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 = rpc_control .ihave diff --git a/protocols/gossipsub/src/rpc_proto.rs b/protocols/gossipsub/src/rpc_proto.rs index 0020ef44e1d..c859dd50c60 100644 --- a/protocols/gossipsub/src/rpc_proto.rs +++ b/protocols/gossipsub/src/rpc_proto.rs @@ -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() { @@ -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()), @@ -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()), @@ -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()]); } }