Skip to content

Commit

Permalink
feat: migrate to quick-protobuf
Browse files Browse the repository at this point in the history
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 libp2p#3024.

Pull-Request: libp2p#3312.
  • Loading branch information
kckeiks authored Mar 2, 2023
1 parent 27fecc0 commit bd1912b
Show file tree
Hide file tree
Showing 8 changed files with 326 additions and 101 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

- Update to `libp2p-swarm` `v0.42.0`.

- Migrate from `prost` to `quick-protobuf`. This removes `protoc` dependency. See [PR 3312].

[PR 3312]: https://github.com/libp2p/rust-libp2p/pull/3312
[PR 3153]: https://github.com/libp2p/rust-libp2p/pull/3153

# 0.9.1
Expand Down
5 changes: 1 addition & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ repository = "https://github.com/libp2p/rust-libp2p"
keywords = ["peer-to-peer", "libp2p", "networking"]
categories = ["network-programming", "asynchronous"]

[build-dependencies]
prost-build = "0.11"

[dependencies]
async-trait = "0.1"
futures = "0.3"
Expand All @@ -23,7 +20,7 @@ libp2p-swarm = { version = "0.42.0", path = "../../swarm" }
libp2p-request-response = { version = "0.24.0", path = "../request-response" }
log = "0.4"
rand = "0.8"
prost = "0.11"
quick-protobuf = "0.8"

[dev-dependencies]
async-std = { version = "1.10", features = ["attributes"] }
Expand Down
23 changes: 0 additions & 23 deletions build.rs

This file was deleted.

2 changes: 2 additions & 0 deletions src/generated/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Automatically generated mod.rs
pub mod structs;
File renamed without changes.
242 changes: 242 additions & 0 deletions src/generated/structs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
// Automatically generated rust module for 'structs.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::*;

#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Debug, Default, PartialEq, Clone)]
pub struct Message {
pub type_pb: Option<structs::mod_Message::MessageType>,
pub dial: Option<structs::mod_Message::Dial>,
pub dialResponse: Option<structs::mod_Message::DialResponse>,
}

impl<'a> MessageRead<'a> for Message {
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 = Some(r.read_enum(bytes)?),
Ok(18) => msg.dial = Some(r.read_message::<structs::mod_Message::Dial>(bytes)?),
Ok(26) => msg.dialResponse = Some(r.read_message::<structs::mod_Message::DialResponse>(bytes)?),
Ok(t) => { r.read_unknown(bytes, t)?; }
Err(e) => return Err(e),
}
}
Ok(msg)
}
}

impl MessageWrite for Message {
fn get_size(&self) -> usize {
0
+ self.type_pb.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64))
+ self.dial.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size()))
+ self.dialResponse.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size()))
}

fn write_message<W: WriterBackend>(&self, w: &mut Writer<W>) -> Result<()> {
if let Some(ref s) = self.type_pb { w.write_with_tag(8, |w| w.write_enum(*s as i32))?; }
if let Some(ref s) = self.dial { w.write_with_tag(18, |w| w.write_message(s))?; }
if let Some(ref s) = self.dialResponse { w.write_with_tag(26, |w| w.write_message(s))?; }
Ok(())
}
}

pub mod mod_Message {

use super::*;

#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Debug, Default, PartialEq, Clone)]
pub struct PeerInfo {
pub id: Option<Vec<u8>>,
pub addrs: Vec<Vec<u8>>,
}

impl<'a> MessageRead<'a> for PeerInfo {
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(10) => msg.id = Some(r.read_bytes(bytes)?.to_owned()),
Ok(18) => msg.addrs.push(r.read_bytes(bytes)?.to_owned()),
Ok(t) => { r.read_unknown(bytes, t)?; }
Err(e) => return Err(e),
}
}
Ok(msg)
}
}

impl MessageWrite for PeerInfo {
fn get_size(&self) -> usize {
0
+ self.id.as_ref().map_or(0, |m| 1 + sizeof_len((m).len()))
+ self.addrs.iter().map(|s| 1 + sizeof_len((s).len())).sum::<usize>()
}

fn write_message<W: WriterBackend>(&self, w: &mut Writer<W>) -> Result<()> {
if let Some(ref s) = self.id { w.write_with_tag(10, |w| w.write_bytes(&**s))?; }
for s in &self.addrs { w.write_with_tag(18, |w| w.write_bytes(&**s))?; }
Ok(())
}
}

#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Debug, Default, PartialEq, Clone)]
pub struct Dial {
pub peer: Option<structs::mod_Message::PeerInfo>,
}

impl<'a> MessageRead<'a> for Dial {
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(10) => msg.peer = Some(r.read_message::<structs::mod_Message::PeerInfo>(bytes)?),
Ok(t) => { r.read_unknown(bytes, t)?; }
Err(e) => return Err(e),
}
}
Ok(msg)
}
}

impl MessageWrite for Dial {
fn get_size(&self) -> usize {
0
+ self.peer.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size()))
}

fn write_message<W: WriterBackend>(&self, w: &mut Writer<W>) -> Result<()> {
if let Some(ref s) = self.peer { w.write_with_tag(10, |w| w.write_message(s))?; }
Ok(())
}
}

#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Debug, Default, PartialEq, Clone)]
pub struct DialResponse {
pub status: Option<structs::mod_Message::ResponseStatus>,
pub statusText: Option<String>,
pub addr: Option<Vec<u8>>,
}

impl<'a> MessageRead<'a> for DialResponse {
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.status = Some(r.read_enum(bytes)?),
Ok(18) => msg.statusText = Some(r.read_string(bytes)?.to_owned()),
Ok(26) => msg.addr = Some(r.read_bytes(bytes)?.to_owned()),
Ok(t) => { r.read_unknown(bytes, t)?; }
Err(e) => return Err(e),
}
}
Ok(msg)
}
}

impl MessageWrite for DialResponse {
fn get_size(&self) -> usize {
0
+ self.status.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64))
+ self.statusText.as_ref().map_or(0, |m| 1 + sizeof_len((m).len()))
+ self.addr.as_ref().map_or(0, |m| 1 + sizeof_len((m).len()))
}

fn write_message<W: WriterBackend>(&self, w: &mut Writer<W>) -> Result<()> {
if let Some(ref s) = self.status { w.write_with_tag(8, |w| w.write_enum(*s as i32))?; }
if let Some(ref s) = self.statusText { w.write_with_tag(18, |w| w.write_string(&**s))?; }
if let Some(ref s) = self.addr { w.write_with_tag(26, |w| w.write_bytes(&**s))?; }
Ok(())
}
}

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum MessageType {
DIAL = 0,
DIAL_RESPONSE = 1,
}

impl Default for MessageType {
fn default() -> Self {
MessageType::DIAL
}
}

impl From<i32> for MessageType {
fn from(i: i32) -> Self {
match i {
0 => MessageType::DIAL,
1 => MessageType::DIAL_RESPONSE,
_ => Self::default(),
}
}
}

impl<'a> From<&'a str> for MessageType {
fn from(s: &'a str) -> Self {
match s {
"DIAL" => MessageType::DIAL,
"DIAL_RESPONSE" => MessageType::DIAL_RESPONSE,
_ => Self::default(),
}
}
}

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum ResponseStatus {
OK = 0,
E_DIAL_ERROR = 100,
E_DIAL_REFUSED = 101,
E_BAD_REQUEST = 200,
E_INTERNAL_ERROR = 300,
}

impl Default for ResponseStatus {
fn default() -> Self {
ResponseStatus::OK
}
}

impl From<i32> for ResponseStatus {
fn from(i: i32) -> Self {
match i {
0 => ResponseStatus::OK,
100 => ResponseStatus::E_DIAL_ERROR,
101 => ResponseStatus::E_DIAL_REFUSED,
200 => ResponseStatus::E_BAD_REQUEST,
300 => ResponseStatus::E_INTERNAL_ERROR,
_ => Self::default(),
}
}
}

impl<'a> From<&'a str> for ResponseStatus {
fn from(s: &'a str) -> Self {
match s {
"OK" => ResponseStatus::OK,
"E_DIAL_ERROR" => ResponseStatus::E_DIAL_ERROR,
"E_DIAL_REFUSED" => ResponseStatus::E_DIAL_REFUSED,
"E_BAD_REQUEST" => ResponseStatus::E_BAD_REQUEST,
"E_INTERNAL_ERROR" => ResponseStatus::E_INTERNAL_ERROR,
_ => Self::default(),
}
}
}

}

6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub use self::{
};
pub use libp2p_request_response::{InboundFailure, OutboundFailure};

#[allow(clippy::derive_partial_eq_without_eq)]
mod structs_proto {
include!(concat!(env!("OUT_DIR"), "/structs.rs"));
mod proto {
include!("generated/mod.rs");
pub use self::structs::{mod_Message::*, Message};
}
Loading

0 comments on commit bd1912b

Please sign in to comment.