Skip to content

Commit

Permalink
refactor: Migrate to quick-protobuf
Browse files Browse the repository at this point in the history
  • Loading branch information
dariusc93 committed Jul 16, 2023
1 parent 9d18060 commit 995c2dc
Show file tree
Hide file tree
Showing 8 changed files with 338 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ license = "MIT OR Apache-2.0"
repository = "https://github.com/ipfs-rust/libp2p-bitswap"

[features]
compat = ["prost", "prost-build"]
compat = ["quick-protobuf"]

[build-dependencies]
prost-build = { version = "0.11", optional = true }
Expand All @@ -21,11 +21,12 @@ lazy_static = "1.4.0"
libipld = { version = "0.15.0", default-features = false }
libp2p = { version = "0.52", features = ["request-response"] }
prometheus = "0.13.0"
prost = { version = "0.11", optional = true }
thiserror = "1.0.30"
tracing = "0.1.29"
unsigned-varint = { version = "0.7.1", features = ["futures", "std"] }

quick-protobuf = { version = "0.8.1", optional = true }

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

This file was deleted.

56 changes: 33 additions & 23 deletions src/compat/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ use crate::compat::other;
use crate::compat::prefix::Prefix;
use crate::protocol::{BitswapRequest, BitswapResponse, RequestType};
use libipld::Cid;
use prost::Message;
use quick_protobuf::{BytesReader, MessageRead, MessageWrite, Writer};
use std::convert::TryFrom;
use std::io;

mod bitswap_pb {
include!(concat!(env!("OUT_DIR"), "/bitswap_pb.rs"));
pub use super::super::pb::bitswap_pb::Message;
pub mod message {
use super::super::super::pb::bitswap_pb::mod_Message as message;
pub use message::mod_Wantlist as wantlist;
pub use message::Wantlist;
pub use message::{Block, BlockPresence, BlockPresenceType};
}
}

#[derive(Clone, Debug, Eq, PartialEq)]
Expand All @@ -23,12 +29,12 @@ impl CompatMessage {
CompatMessage::Request(BitswapRequest { ty, cid }) => {
let mut wantlist = bitswap_pb::message::Wantlist::default();
let entry = bitswap_pb::message::wantlist::Entry {
block: cid.to_bytes(),
want_type: match ty {
block: cid.to_bytes().into(),
wantType: match ty {
RequestType::Have => bitswap_pb::message::wantlist::WantType::Have,
RequestType::Block => bitswap_pb::message::wantlist::WantType::Block,
} as _,
send_dont_have: true,
sendDontHave: true,
cancel: false,
priority: 1,
};
Expand All @@ -37,42 +43,46 @@ impl CompatMessage {
}
CompatMessage::Response(cid, BitswapResponse::Have(have)) => {
let block_presence = bitswap_pb::message::BlockPresence {
cid: cid.to_bytes(),
r#type: if *have {
cid: cid.to_bytes().into(),
type_pb: if *have {
bitswap_pb::message::BlockPresenceType::Have
} else {
bitswap_pb::message::BlockPresenceType::DontHave
} as _,
};
msg.block_presences.push(block_presence);
msg.blockPresences.push(block_presence);
}
CompatMessage::Response(cid, BitswapResponse::Block(bytes)) => {
let payload = bitswap_pb::message::Block {
prefix: Prefix::from(cid).to_bytes(),
data: bytes.to_vec(),
prefix: Prefix::from(cid).to_bytes().into(),
data: bytes.into(),
};
msg.payload.push(payload);
}
}
let mut bytes = Vec::with_capacity(msg.encoded_len());
msg.encode(&mut bytes).map_err(other)?;

let mut bytes = Vec::with_capacity(msg.get_size());
let mut writer = Writer::new(&mut bytes);
msg.write_message(&mut writer).map_err(other)?;
Ok(bytes)
}

pub fn from_bytes(bytes: &[u8]) -> io::Result<Vec<Self>> {
let msg = bitswap_pb::Message::decode(bytes)?;
let mut reader = BytesReader::from_bytes(bytes);
let msg = bitswap_pb::Message::from_reader(&mut reader, bytes).map_err(other)?;

let mut parts = vec![];
for entry in msg.wantlist.unwrap_or_default().entries {
if !entry.send_dont_have {
if !entry.sendDontHave {
tracing::error!("message hasn't set `send_dont_have`: skipping");
continue;
}
let cid = Cid::try_from(entry.block).map_err(other)?;
let ty = match entry.want_type {
ty if bitswap_pb::message::wantlist::WantType::Have as i32 == ty => {
let cid = Cid::try_from(&*entry.block).map_err(other)?;
let ty = match entry.wantType {
ty if bitswap_pb::message::wantlist::WantType::Have == ty => {
RequestType::Have
}
ty if bitswap_pb::message::wantlist::WantType::Block as i32 == ty => {
ty if bitswap_pb::message::wantlist::WantType::Block == ty => {
RequestType::Block
}
_ => {
Expand All @@ -90,11 +100,11 @@ impl CompatMessage {
BitswapResponse::Block(payload.data.to_vec()),
));
}
for presence in msg.block_presences {
let cid = Cid::try_from(presence.cid).map_err(other)?;
let have = match presence.r#type {
ty if bitswap_pb::message::BlockPresenceType::Have as i32 == ty => true,
ty if bitswap_pb::message::BlockPresenceType::DontHave as i32 == ty => false,
for presence in msg.blockPresences {
let cid = Cid::try_from(&*presence.cid).map_err(other)?;
let have = match presence.type_pb {
ty if bitswap_pb::message::BlockPresenceType::Have == ty => true,
ty if bitswap_pb::message::BlockPresenceType::DontHave == ty => false,
_ => {
tracing::error!("invalid block presence type: skipping");
continue;
Expand Down
1 change: 1 addition & 0 deletions src/compat/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod message;
mod prefix;
mod protocol;
mod pb;

pub use message::CompatMessage;
pub use protocol::{CompatProtocol, InboundMessage};
Expand Down
File renamed without changes.
Loading

0 comments on commit 995c2dc

Please sign in to comment.