feat: migrate to quick-protobuf

Instead of relying on `protoc` and buildscripts, we generate the bindings using `pb-rs` and version them within our codebase. This makes for a better IDE integration, a faster build and an easier use of `rust-libp2p` because we don't force the `protoc` dependency onto them.

Resolves #3024.

Pull-Request: #3312.
This commit is contained in:
Miguel Guarniz
2023-03-02 05:45:07 -05:00
committed by GitHub
parent 4910160bea
commit db82e0210e
141 changed files with 3662 additions and 1276 deletions

View File

@ -0,0 +1,2 @@
// Automatically generated mod.rs
pub mod pb;

View File

@ -0,0 +1,88 @@
// Automatically generated rust module for 'message.proto' file
#![allow(non_snake_case)]
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(unused_imports)]
#![allow(unknown_lints)]
#![allow(clippy::all)]
#![cfg_attr(rustfmt, rustfmt_skip)]
use quick_protobuf::{MessageInfo, MessageRead, MessageWrite, BytesReader, Writer, WriterBackend, Result};
use quick_protobuf::sizeofs::*;
use super::super::*;
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Debug, Default, PartialEq, Clone)]
pub struct HolePunch {
pub type_pb: holepunch::pb::mod_HolePunch::Type,
pub ObsAddrs: Vec<Vec<u8>>,
}
impl<'a> MessageRead<'a> for HolePunch {
fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result<Self> {
let mut msg = Self::default();
while !r.is_eof() {
match r.next_tag(bytes) {
Ok(8) => msg.type_pb = r.read_enum(bytes)?,
Ok(18) => msg.ObsAddrs.push(r.read_bytes(bytes)?.to_owned()),
Ok(t) => { r.read_unknown(bytes, t)?; }
Err(e) => return Err(e),
}
}
Ok(msg)
}
}
impl MessageWrite for HolePunch {
fn get_size(&self) -> usize {
0
+ 1 + sizeof_varint(*(&self.type_pb) as u64)
+ self.ObsAddrs.iter().map(|s| 1 + sizeof_len((s).len())).sum::<usize>()
}
fn write_message<W: WriterBackend>(&self, w: &mut Writer<W>) -> Result<()> {
w.write_with_tag(8, |w| w.write_enum(*&self.type_pb as i32))?;
for s in &self.ObsAddrs { w.write_with_tag(18, |w| w.write_bytes(&**s))?; }
Ok(())
}
}
pub mod mod_HolePunch {
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum Type {
CONNECT = 100,
SYNC = 300,
}
impl Default for Type {
fn default() -> Self {
Type::CONNECT
}
}
impl From<i32> for Type {
fn from(i: i32) -> Self {
match i {
100 => Type::CONNECT,
300 => Type::SYNC,
_ => Self::default(),
}
}
}
impl<'a> From<&'a str> for Type {
fn from(s: &'a str) -> Self {
match s {
"CONNECT" => Type::CONNECT,
"SYNC" => Type::SYNC,
_ => Self::default(),
}
}
}
}

View File

@ -0,0 +1,2 @@
// Automatically generated mod.rs
pub mod holepunch;

View File

@ -26,9 +26,10 @@
mod behaviour_impl; // TODO: Rename back `behaviour` once deprecation symbols are removed.
mod handler;
mod protocol;
#[allow(clippy::derive_partial_eq_without_eq)]
mod message_proto {
include!(concat!(env!("OUT_DIR"), "/holepunch.pb.rs"));
mod proto {
include!("generated/mod.rs");
pub use self::holepunch::pb::{mod_HolePunch::*, HolePunch};
}
pub use behaviour_impl::Behaviour;

View File

@ -18,7 +18,7 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
use crate::message_proto::{hole_punch, HolePunch};
use crate::proto;
use asynchronous_codec::Framed;
use futures::{future::BoxFuture, prelude::*};
use libp2p_core::{multiaddr::Protocol, upgrade, Multiaddr};
@ -46,19 +46,19 @@ impl upgrade::InboundUpgrade<NegotiatedSubstream> for Upgrade {
fn upgrade_inbound(self, substream: NegotiatedSubstream, _: Self::Info) -> Self::Future {
let mut substream = Framed::new(
substream,
prost_codec::Codec::new(super::MAX_MESSAGE_SIZE_BYTES),
quick_protobuf_codec::Codec::new(super::MAX_MESSAGE_SIZE_BYTES),
);
async move {
let HolePunch { r#type, obs_addrs } =
let proto::HolePunch { type_pb, ObsAddrs } =
substream.next().await.ok_or(UpgradeError::StreamClosed)??;
let obs_addrs = if obs_addrs.is_empty() {
let obs_addrs = if ObsAddrs.is_empty() {
return Err(UpgradeError::NoAddresses);
} else {
obs_addrs
ObsAddrs
.into_iter()
.filter_map(|a| match Multiaddr::try_from(a) {
.filter_map(|a| match Multiaddr::try_from(a.to_vec()) {
Ok(a) => Some(a),
Err(e) => {
log::debug!("Unable to parse multiaddr: {e}");
@ -77,11 +77,9 @@ impl upgrade::InboundUpgrade<NegotiatedSubstream> for Upgrade {
.collect::<Vec<Multiaddr>>()
};
let r#type = hole_punch::Type::from_i32(r#type).ok_or(UpgradeError::ParseTypeField)?;
match r#type {
hole_punch::Type::Connect => {}
hole_punch::Type::Sync => return Err(UpgradeError::UnexpectedTypeSync),
match type_pb {
proto::Type::CONNECT => {}
proto::Type::SYNC => return Err(UpgradeError::UnexpectedTypeSync),
}
Ok(PendingConnect {
@ -94,7 +92,7 @@ impl upgrade::InboundUpgrade<NegotiatedSubstream> for Upgrade {
}
pub struct PendingConnect {
substream: Framed<NegotiatedSubstream, prost_codec::Codec<HolePunch>>,
substream: Framed<NegotiatedSubstream, quick_protobuf_codec::Codec<proto::HolePunch>>,
remote_obs_addrs: Vec<Multiaddr>,
}
@ -103,22 +101,21 @@ impl PendingConnect {
mut self,
local_obs_addrs: Vec<Multiaddr>,
) -> Result<Vec<Multiaddr>, UpgradeError> {
let msg = HolePunch {
r#type: hole_punch::Type::Connect.into(),
obs_addrs: local_obs_addrs.into_iter().map(|a| a.to_vec()).collect(),
let msg = proto::HolePunch {
type_pb: proto::Type::CONNECT,
ObsAddrs: local_obs_addrs.into_iter().map(|a| a.to_vec()).collect(),
};
self.substream.send(msg).await?;
let HolePunch { r#type, .. } = self
let proto::HolePunch { type_pb, .. } = self
.substream
.next()
.await
.ok_or(UpgradeError::StreamClosed)??;
let r#type = hole_punch::Type::from_i32(r#type).ok_or(UpgradeError::ParseTypeField)?;
match r#type {
hole_punch::Type::Connect => return Err(UpgradeError::UnexpectedTypeConnect),
hole_punch::Type::Sync => {}
match type_pb {
proto::Type::CONNECT => return Err(UpgradeError::UnexpectedTypeConnect),
proto::Type::SYNC => {}
}
Ok(self.remote_obs_addrs)
@ -128,7 +125,7 @@ impl PendingConnect {
#[derive(Debug, Error)]
pub enum UpgradeError {
#[error(transparent)]
Codec(#[from] prost_codec::Error),
Codec(#[from] quick_protobuf_codec::Error),
#[error("Stream closed")]
StreamClosed,
#[error("Expected at least one address in reservation.")]

View File

@ -18,7 +18,7 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
use crate::message_proto::{hole_punch, HolePunch};
use crate::proto;
use asynchronous_codec::Framed;
use futures::{future::BoxFuture, prelude::*};
use futures_timer::Delay;
@ -56,12 +56,12 @@ impl upgrade::OutboundUpgrade<NegotiatedSubstream> for Upgrade {
fn upgrade_outbound(self, substream: NegotiatedSubstream, _: Self::Info) -> Self::Future {
let mut substream = Framed::new(
substream,
prost_codec::Codec::new(super::MAX_MESSAGE_SIZE_BYTES),
quick_protobuf_codec::Codec::new(super::MAX_MESSAGE_SIZE_BYTES),
);
let msg = HolePunch {
r#type: hole_punch::Type::Connect.into(),
obs_addrs: self.obs_addrs.into_iter().map(|a| a.to_vec()).collect(),
let msg = proto::HolePunch {
type_pb: proto::Type::CONNECT,
ObsAddrs: self.obs_addrs.into_iter().map(|a| a.to_vec()).collect(),
};
async move {
@ -69,23 +69,22 @@ impl upgrade::OutboundUpgrade<NegotiatedSubstream> for Upgrade {
let sent_time = Instant::now();
let HolePunch { r#type, obs_addrs } =
let proto::HolePunch { type_pb, ObsAddrs } =
substream.next().await.ok_or(UpgradeError::StreamClosed)??;
let rtt = sent_time.elapsed();
let r#type = hole_punch::Type::from_i32(r#type).ok_or(UpgradeError::ParseTypeField)?;
match r#type {
hole_punch::Type::Connect => {}
hole_punch::Type::Sync => return Err(UpgradeError::UnexpectedTypeSync),
match type_pb {
proto::Type::CONNECT => {}
proto::Type::SYNC => return Err(UpgradeError::UnexpectedTypeSync),
}
let obs_addrs = if obs_addrs.is_empty() {
let obs_addrs = if ObsAddrs.is_empty() {
return Err(UpgradeError::NoAddresses);
} else {
obs_addrs
ObsAddrs
.into_iter()
.filter_map(|a| match Multiaddr::try_from(a) {
.filter_map(|a| match Multiaddr::try_from(a.to_vec()) {
Ok(a) => Some(a),
Err(e) => {
log::debug!("Unable to parse multiaddr: {e}");
@ -104,9 +103,9 @@ impl upgrade::OutboundUpgrade<NegotiatedSubstream> for Upgrade {
.collect::<Vec<Multiaddr>>()
};
let msg = HolePunch {
r#type: hole_punch::Type::Sync.into(),
obs_addrs: vec![],
let msg = proto::HolePunch {
type_pb: proto::Type::SYNC,
ObsAddrs: vec![],
};
substream.send(msg).await?;
@ -126,7 +125,7 @@ pub struct Connect {
#[derive(Debug, Error)]
pub enum UpgradeError {
#[error(transparent)]
Codec(#[from] prost_codec::Error),
Codec(#[from] quick_protobuf_codec::Error),
#[error("Stream closed")]
StreamClosed,
#[error("Expected 'status' field to be set.")]