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

@ -24,8 +24,8 @@ mod handler;
pub mod rate_limiter;
use crate::behaviour::handler::Handler;
use crate::message_proto;
use crate::multiaddr_ext::MultiaddrExt;
use crate::proto;
use crate::protocol::{inbound_hop, outbound_stop};
use either::Either;
use instant::Instant;
@ -381,7 +381,7 @@ impl NetworkBehaviour for Behaviour {
peer_id: event_source,
event: Either::Left(handler::In::DenyReservationReq {
inbound_reservation_req,
status: message_proto::Status::ResourceLimitExceeded,
status: proto::Status::RESOURCE_LIMIT_EXCEEDED,
}),
}
.into()
@ -496,7 +496,7 @@ impl NetworkBehaviour for Behaviour {
event: Either::Left(handler::In::DenyCircuitReq {
circuit_id: None,
inbound_circuit_req,
status: message_proto::Status::ResourceLimitExceeded,
status: proto::Status::RESOURCE_LIMIT_EXCEEDED,
}),
}
} else if let Some(dst_conn) = self
@ -532,7 +532,7 @@ impl NetworkBehaviour for Behaviour {
event: Either::Left(handler::In::DenyCircuitReq {
circuit_id: None,
inbound_circuit_req,
status: message_proto::Status::NoReservation,
status: proto::Status::NO_RESERVATION,
}),
}
};

View File

@ -20,7 +20,7 @@
use crate::behaviour::CircuitId;
use crate::copy_future::CopyFuture;
use crate::message_proto::Status;
use crate::proto;
use crate::protocol::{inbound_hop, outbound_stop};
use bytes::Bytes;
use either::Either;
@ -58,12 +58,12 @@ pub enum In {
},
DenyReservationReq {
inbound_reservation_req: inbound_hop::ReservationReq,
status: Status,
status: proto::Status,
},
DenyCircuitReq {
circuit_id: Option<CircuitId>,
inbound_circuit_req: inbound_hop::CircuitReq,
status: Status,
status: proto::Status,
},
NegotiateOutboundConnect {
circuit_id: CircuitId,
@ -208,7 +208,7 @@ pub enum Event {
src_peer_id: PeerId,
src_connection_id: ConnectionId,
inbound_circuit_req: inbound_hop::CircuitReq,
status: Status,
status: proto::Status,
error: ConnectionHandlerUpgrErr<outbound_stop::CircuitFailedReason>,
},
/// An inbound circuit has closed.
@ -522,12 +522,14 @@ impl Handler {
>,
) {
let (non_fatal_error, status) = match error {
ConnectionHandlerUpgrErr::Timeout => {
(ConnectionHandlerUpgrErr::Timeout, Status::ConnectionFailed)
}
ConnectionHandlerUpgrErr::Timer => {
(ConnectionHandlerUpgrErr::Timer, Status::ConnectionFailed)
}
ConnectionHandlerUpgrErr::Timeout => (
ConnectionHandlerUpgrErr::Timeout,
proto::Status::CONNECTION_FAILED,
),
ConnectionHandlerUpgrErr::Timer => (
ConnectionHandlerUpgrErr::Timer,
proto::Status::CONNECTION_FAILED,
),
ConnectionHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Select(
upgrade::NegotiationError::Failed,
)) => {
@ -556,10 +558,10 @@ impl Handler {
outbound_stop::UpgradeError::CircuitFailed(error) => {
let status = match error {
outbound_stop::CircuitFailedReason::ResourceLimitExceeded => {
Status::ResourceLimitExceeded
proto::Status::RESOURCE_LIMIT_EXCEEDED
}
outbound_stop::CircuitFailedReason::PermissionDenied => {
Status::PermissionDenied
proto::Status::PERMISSION_DENIED
}
};
(

View File

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

View File

@ -0,0 +1,346 @@
// 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::*;
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum Status {
OK = 100,
RESERVATION_REFUSED = 200,
RESOURCE_LIMIT_EXCEEDED = 201,
PERMISSION_DENIED = 202,
CONNECTION_FAILED = 203,
NO_RESERVATION = 204,
MALFORMED_MESSAGE = 400,
UNEXPECTED_MESSAGE = 401,
}
impl Default for Status {
fn default() -> Self {
Status::OK
}
}
impl From<i32> for Status {
fn from(i: i32) -> Self {
match i {
100 => Status::OK,
200 => Status::RESERVATION_REFUSED,
201 => Status::RESOURCE_LIMIT_EXCEEDED,
202 => Status::PERMISSION_DENIED,
203 => Status::CONNECTION_FAILED,
204 => Status::NO_RESERVATION,
400 => Status::MALFORMED_MESSAGE,
401 => Status::UNEXPECTED_MESSAGE,
_ => Self::default(),
}
}
}
impl<'a> From<&'a str> for Status {
fn from(s: &'a str) -> Self {
match s {
"OK" => Status::OK,
"RESERVATION_REFUSED" => Status::RESERVATION_REFUSED,
"RESOURCE_LIMIT_EXCEEDED" => Status::RESOURCE_LIMIT_EXCEEDED,
"PERMISSION_DENIED" => Status::PERMISSION_DENIED,
"CONNECTION_FAILED" => Status::CONNECTION_FAILED,
"NO_RESERVATION" => Status::NO_RESERVATION,
"MALFORMED_MESSAGE" => Status::MALFORMED_MESSAGE,
"UNEXPECTED_MESSAGE" => Status::UNEXPECTED_MESSAGE,
_ => Self::default(),
}
}
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Debug, Default, PartialEq, Clone)]
pub struct HopMessage {
pub type_pb: message_v2::pb::mod_HopMessage::Type,
pub peer: Option<message_v2::pb::Peer>,
pub reservation: Option<message_v2::pb::Reservation>,
pub limit: Option<message_v2::pb::Limit>,
pub status: Option<message_v2::pb::Status>,
}
impl<'a> MessageRead<'a> for HopMessage {
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.peer = Some(r.read_message::<message_v2::pb::Peer>(bytes)?),
Ok(26) => msg.reservation = Some(r.read_message::<message_v2::pb::Reservation>(bytes)?),
Ok(34) => msg.limit = Some(r.read_message::<message_v2::pb::Limit>(bytes)?),
Ok(40) => msg.status = Some(r.read_enum(bytes)?),
Ok(t) => { r.read_unknown(bytes, t)?; }
Err(e) => return Err(e),
}
}
Ok(msg)
}
}
impl MessageWrite for HopMessage {
fn get_size(&self) -> usize {
0
+ 1 + sizeof_varint(*(&self.type_pb) as u64)
+ self.peer.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size()))
+ self.reservation.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size()))
+ self.limit.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size()))
+ self.status.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64))
}
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))?;
if let Some(ref s) = self.peer { w.write_with_tag(18, |w| w.write_message(s))?; }
if let Some(ref s) = self.reservation { w.write_with_tag(26, |w| w.write_message(s))?; }
if let Some(ref s) = self.limit { w.write_with_tag(34, |w| w.write_message(s))?; }
if let Some(ref s) = self.status { w.write_with_tag(40, |w| w.write_enum(*s as i32))?; }
Ok(())
}
}
pub mod mod_HopMessage {
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum Type {
RESERVE = 0,
CONNECT = 1,
STATUS = 2,
}
impl Default for Type {
fn default() -> Self {
Type::RESERVE
}
}
impl From<i32> for Type {
fn from(i: i32) -> Self {
match i {
0 => Type::RESERVE,
1 => Type::CONNECT,
2 => Type::STATUS,
_ => Self::default(),
}
}
}
impl<'a> From<&'a str> for Type {
fn from(s: &'a str) -> Self {
match s {
"RESERVE" => Type::RESERVE,
"CONNECT" => Type::CONNECT,
"STATUS" => Type::STATUS,
_ => Self::default(),
}
}
}
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Debug, Default, PartialEq, Clone)]
pub struct StopMessage {
pub type_pb: message_v2::pb::mod_StopMessage::Type,
pub peer: Option<message_v2::pb::Peer>,
pub limit: Option<message_v2::pb::Limit>,
pub status: Option<message_v2::pb::Status>,
}
impl<'a> MessageRead<'a> for StopMessage {
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.peer = Some(r.read_message::<message_v2::pb::Peer>(bytes)?),
Ok(26) => msg.limit = Some(r.read_message::<message_v2::pb::Limit>(bytes)?),
Ok(32) => msg.status = Some(r.read_enum(bytes)?),
Ok(t) => { r.read_unknown(bytes, t)?; }
Err(e) => return Err(e),
}
}
Ok(msg)
}
}
impl MessageWrite for StopMessage {
fn get_size(&self) -> usize {
0
+ 1 + sizeof_varint(*(&self.type_pb) as u64)
+ self.peer.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size()))
+ self.limit.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size()))
+ self.status.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64))
}
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))?;
if let Some(ref s) = self.peer { w.write_with_tag(18, |w| w.write_message(s))?; }
if let Some(ref s) = self.limit { w.write_with_tag(26, |w| w.write_message(s))?; }
if let Some(ref s) = self.status { w.write_with_tag(32, |w| w.write_enum(*s as i32))?; }
Ok(())
}
}
pub mod mod_StopMessage {
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum Type {
CONNECT = 0,
STATUS = 1,
}
impl Default for Type {
fn default() -> Self {
Type::CONNECT
}
}
impl From<i32> for Type {
fn from(i: i32) -> Self {
match i {
0 => Type::CONNECT,
1 => Type::STATUS,
_ => Self::default(),
}
}
}
impl<'a> From<&'a str> for Type {
fn from(s: &'a str) -> Self {
match s {
"CONNECT" => Type::CONNECT,
"STATUS" => Type::STATUS,
_ => Self::default(),
}
}
}
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Debug, Default, PartialEq, Clone)]
pub struct Peer {
pub id: Vec<u8>,
pub addrs: Vec<Vec<u8>>,
}
impl<'a> MessageRead<'a> for Peer {
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 = 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 Peer {
fn get_size(&self) -> usize {
0
+ 1 + sizeof_len((&self.id).len())
+ self.addrs.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(10, |w| w.write_bytes(&**&self.id))?;
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 Reservation {
pub expire: u64,
pub addrs: Vec<Vec<u8>>,
pub voucher: Option<Vec<u8>>,
}
impl<'a> MessageRead<'a> for Reservation {
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.expire = r.read_uint64(bytes)?,
Ok(18) => msg.addrs.push(r.read_bytes(bytes)?.to_owned()),
Ok(26) => msg.voucher = Some(r.read_bytes(bytes)?.to_owned()),
Ok(t) => { r.read_unknown(bytes, t)?; }
Err(e) => return Err(e),
}
}
Ok(msg)
}
}
impl MessageWrite for Reservation {
fn get_size(&self) -> usize {
0
+ 1 + sizeof_varint(*(&self.expire) as u64)
+ self.addrs.iter().map(|s| 1 + sizeof_len((s).len())).sum::<usize>()
+ self.voucher.as_ref().map_or(0, |m| 1 + sizeof_len((m).len()))
}
fn write_message<W: WriterBackend>(&self, w: &mut Writer<W>) -> Result<()> {
w.write_with_tag(8, |w| w.write_uint64(*&self.expire))?;
for s in &self.addrs { w.write_with_tag(18, |w| w.write_bytes(&**s))?; }
if let Some(ref s) = self.voucher { w.write_with_tag(26, |w| w.write_bytes(&**s))?; }
Ok(())
}
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Debug, Default, PartialEq, Clone)]
pub struct Limit {
pub duration: Option<u32>,
pub data: Option<u64>,
}
impl<'a> MessageRead<'a> for Limit {
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.duration = Some(r.read_uint32(bytes)?),
Ok(16) => msg.data = Some(r.read_uint64(bytes)?),
Ok(t) => { r.read_unknown(bytes, t)?; }
Err(e) => return Err(e),
}
}
Ok(msg)
}
}
impl MessageWrite for Limit {
fn get_size(&self) -> usize {
0
+ self.duration.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64))
+ self.data.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64))
}
fn write_message<W: WriterBackend>(&self, w: &mut Writer<W>) -> Result<()> {
if let Some(ref s) = self.duration { w.write_with_tag(8, |w| w.write_uint32(*s))?; }
if let Some(ref s) = self.data { w.write_with_tag(16, |w| w.write_uint64(*s))?; }
Ok(())
}
}

View File

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

View File

@ -30,9 +30,11 @@ mod priv_client;
mod protocol;
pub mod v2;
#[allow(clippy::derive_partial_eq_without_eq)]
mod message_proto {
include!(concat!(env!("OUT_DIR"), "/message_v2.pb.rs"));
mod proto {
include!("generated/mod.rs");
pub use self::message_v2::pb::mod_HopMessage::Type as HopMessageType;
pub use self::message_v2::pb::mod_StopMessage::Type as StopMessageType;
pub use self::message_v2::pb::{HopMessage, Limit, Peer, Reservation, Status, StopMessage};
}
pub use behaviour::{Behaviour, CircuitId, Config, Event};

View File

@ -18,8 +18,8 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
use crate::message_proto::Status;
use crate::priv_client::transport;
use crate::proto;
use crate::protocol::{self, inbound_stop, outbound_hop};
use either::Either;
use futures::channel::{mpsc, oneshot};
@ -218,7 +218,7 @@ impl Handler {
.circuit_deny_futs
.insert(
src_peer_id,
inbound_circuit.deny(Status::NoReservation).boxed(),
inbound_circuit.deny(proto::Status::NO_RESERVATION).boxed(),
)
.is_some()
{

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;
use crate::proto;
use std::time::Duration;
pub mod inbound_hop;
@ -37,8 +37,8 @@ pub struct Limit {
data_in_bytes: Option<u64>,
}
impl From<message_proto::Limit> for Limit {
fn from(limit: message_proto::Limit) -> Self {
impl From<proto::Limit> for Limit {
fn from(limit: proto::Limit) -> Self {
Limit {
duration: limit.duration.map(|d| Duration::from_secs(d.into())),
data_in_bytes: limit.data,

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::{hop_message, HopMessage, Limit, Reservation, Status};
use crate::proto;
use crate::protocol::{HOP_PROTOCOL_NAME, MAX_MESSAGE_SIZE};
use asynchronous_codec::{Framed, FramedParts};
use bytes::Bytes;
@ -51,11 +51,14 @@ impl upgrade::InboundUpgrade<NegotiatedSubstream> for Upgrade {
type Future = BoxFuture<'static, Result<Self::Output, Self::Error>>;
fn upgrade_inbound(self, substream: NegotiatedSubstream, _: Self::Info) -> Self::Future {
let mut substream = Framed::new(substream, prost_codec::Codec::new(MAX_MESSAGE_SIZE));
let mut substream = Framed::new(
substream,
quick_protobuf_codec::Codec::new(MAX_MESSAGE_SIZE),
);
async move {
let HopMessage {
r#type,
let proto::HopMessage {
type_pb,
peer,
reservation: _,
limit: _,
@ -65,21 +68,19 @@ impl upgrade::InboundUpgrade<NegotiatedSubstream> for Upgrade {
.await
.ok_or(FatalUpgradeError::StreamClosed)??;
let r#type =
hop_message::Type::from_i32(r#type).ok_or(FatalUpgradeError::ParseTypeField)?;
let req = match r#type {
hop_message::Type::Reserve => Req::Reserve(ReservationReq {
let req = match type_pb {
proto::HopMessageType::RESERVE => Req::Reserve(ReservationReq {
substream,
reservation_duration: self.reservation_duration,
max_circuit_duration: self.max_circuit_duration,
max_circuit_bytes: self.max_circuit_bytes,
}),
hop_message::Type::Connect => {
proto::HopMessageType::CONNECT => {
let dst = PeerId::from_bytes(&peer.ok_or(FatalUpgradeError::MissingPeer)?.id)
.map_err(|_| FatalUpgradeError::ParsePeerId)?;
Req::Connect(CircuitReq { dst, substream })
}
hop_message::Type::Status => {
proto::HopMessageType::STATUS => {
return Err(FatalUpgradeError::UnexpectedTypeStatus.into())
}
};
@ -96,8 +97,8 @@ pub enum UpgradeError {
Fatal(#[from] FatalUpgradeError),
}
impl From<prost_codec::Error> for UpgradeError {
fn from(error: prost_codec::Error) -> Self {
impl From<quick_protobuf_codec::Error> for UpgradeError {
fn from(error: quick_protobuf_codec::Error) -> Self {
Self::Fatal(error.into())
}
}
@ -105,7 +106,7 @@ impl From<prost_codec::Error> for UpgradeError {
#[derive(Debug, Error)]
pub enum FatalUpgradeError {
#[error(transparent)]
Codec(#[from] prost_codec::Error),
Codec(#[from] quick_protobuf_codec::Error),
#[error("Stream closed")]
StreamClosed,
#[error("Failed to parse response type field.")]
@ -124,7 +125,7 @@ pub enum Req {
}
pub struct ReservationReq {
substream: Framed<NegotiatedSubstream, prost_codec::Codec<HopMessage>>,
substream: Framed<NegotiatedSubstream, quick_protobuf_codec::Codec<proto::HopMessage>>,
reservation_duration: Duration,
max_circuit_duration: Duration,
max_circuit_bytes: u64,
@ -132,10 +133,10 @@ pub struct ReservationReq {
impl ReservationReq {
pub async fn accept(self, addrs: Vec<Multiaddr>) -> Result<(), UpgradeError> {
let msg = HopMessage {
r#type: hop_message::Type::Status.into(),
let msg = proto::HopMessage {
type_pb: proto::HopMessageType::STATUS,
peer: None,
reservation: Some(Reservation {
reservation: Some(proto::Reservation {
addrs: addrs.into_iter().map(|a| a.to_vec()).collect(),
expire: (SystemTime::now() + self.reservation_duration)
.duration_since(SystemTime::UNIX_EPOCH)
@ -143,7 +144,7 @@ impl ReservationReq {
.as_secs(),
voucher: None,
}),
limit: Some(Limit {
limit: Some(proto::Limit {
duration: Some(
self.max_circuit_duration
.as_secs()
@ -152,25 +153,25 @@ impl ReservationReq {
),
data: Some(self.max_circuit_bytes),
}),
status: Some(Status::Ok.into()),
status: Some(proto::Status::OK),
};
self.send(msg).await
}
pub async fn deny(self, status: Status) -> Result<(), UpgradeError> {
let msg = HopMessage {
r#type: hop_message::Type::Status.into(),
pub async fn deny(self, status: proto::Status) -> Result<(), UpgradeError> {
let msg = proto::HopMessage {
type_pb: proto::HopMessageType::STATUS,
peer: None,
reservation: None,
limit: None,
status: Some(status.into()),
status: Some(status),
};
self.send(msg).await
}
async fn send(mut self, msg: HopMessage) -> Result<(), UpgradeError> {
async fn send(mut self, msg: proto::HopMessage) -> Result<(), UpgradeError> {
self.substream.send(msg).await?;
self.substream.flush().await?;
self.substream.close().await?;
@ -181,7 +182,7 @@ impl ReservationReq {
pub struct CircuitReq {
dst: PeerId,
substream: Framed<NegotiatedSubstream, prost_codec::Codec<HopMessage>>,
substream: Framed<NegotiatedSubstream, quick_protobuf_codec::Codec<proto::HopMessage>>,
}
impl CircuitReq {
@ -190,12 +191,12 @@ impl CircuitReq {
}
pub async fn accept(mut self) -> Result<(NegotiatedSubstream, Bytes), UpgradeError> {
let msg = HopMessage {
r#type: hop_message::Type::Status.into(),
let msg = proto::HopMessage {
type_pb: proto::HopMessageType::STATUS,
peer: None,
reservation: None,
limit: None,
status: Some(Status::Ok.into()),
status: Some(proto::Status::OK),
};
self.send(msg).await?;
@ -214,19 +215,19 @@ impl CircuitReq {
Ok((io, read_buffer.freeze()))
}
pub async fn deny(mut self, status: Status) -> Result<(), UpgradeError> {
let msg = HopMessage {
r#type: hop_message::Type::Status.into(),
pub async fn deny(mut self, status: proto::Status) -> Result<(), UpgradeError> {
let msg = proto::HopMessage {
type_pb: proto::HopMessageType::STATUS,
peer: None,
reservation: None,
limit: None,
status: Some(status.into()),
status: Some(status),
};
self.send(msg).await?;
self.substream.close().await.map_err(Into::into)
}
async fn send(&mut self, msg: HopMessage) -> Result<(), prost_codec::Error> {
async fn send(&mut self, msg: proto::HopMessage) -> Result<(), quick_protobuf_codec::Error> {
self.substream.send(msg).await?;
self.substream.flush().await?;

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::{stop_message, Status, StopMessage};
use crate::proto;
use crate::protocol::{self, MAX_MESSAGE_SIZE, STOP_PROTOCOL_NAME};
use asynchronous_codec::{Framed, FramedParts};
use bytes::Bytes;
@ -45,11 +45,14 @@ impl upgrade::InboundUpgrade<NegotiatedSubstream> for Upgrade {
type Future = BoxFuture<'static, Result<Self::Output, Self::Error>>;
fn upgrade_inbound(self, substream: NegotiatedSubstream, _: Self::Info) -> Self::Future {
let mut substream = Framed::new(substream, prost_codec::Codec::new(MAX_MESSAGE_SIZE));
let mut substream = Framed::new(
substream,
quick_protobuf_codec::Codec::new(MAX_MESSAGE_SIZE),
);
async move {
let StopMessage {
r#type,
let proto::StopMessage {
type_pb,
peer,
limit,
status: _,
@ -58,10 +61,8 @@ impl upgrade::InboundUpgrade<NegotiatedSubstream> for Upgrade {
.await
.ok_or(FatalUpgradeError::StreamClosed)??;
let r#type =
stop_message::Type::from_i32(r#type).ok_or(FatalUpgradeError::ParseTypeField)?;
match r#type {
stop_message::Type::Connect => {
match type_pb {
proto::StopMessageType::CONNECT => {
let src_peer_id =
PeerId::from_bytes(&peer.ok_or(FatalUpgradeError::MissingPeer)?.id)
.map_err(|_| FatalUpgradeError::ParsePeerId)?;
@ -71,7 +72,9 @@ impl upgrade::InboundUpgrade<NegotiatedSubstream> for Upgrade {
limit: limit.map(Into::into),
})
}
stop_message::Type::Status => Err(FatalUpgradeError::UnexpectedTypeStatus.into()),
proto::StopMessageType::STATUS => {
Err(FatalUpgradeError::UnexpectedTypeStatus.into())
}
}
}
.boxed()
@ -84,8 +87,8 @@ pub enum UpgradeError {
Fatal(#[from] FatalUpgradeError),
}
impl From<prost_codec::Error> for UpgradeError {
fn from(error: prost_codec::Error) -> Self {
impl From<quick_protobuf_codec::Error> for UpgradeError {
fn from(error: quick_protobuf_codec::Error) -> Self {
Self::Fatal(error.into())
}
}
@ -93,7 +96,7 @@ impl From<prost_codec::Error> for UpgradeError {
#[derive(Debug, Error)]
pub enum FatalUpgradeError {
#[error(transparent)]
Codec(#[from] prost_codec::Error),
Codec(#[from] quick_protobuf_codec::Error),
#[error("Stream closed")]
StreamClosed,
#[error("Failed to parse response type field.")]
@ -107,7 +110,7 @@ pub enum FatalUpgradeError {
}
pub struct Circuit {
substream: Framed<NegotiatedSubstream, prost_codec::Codec<StopMessage>>,
substream: Framed<NegotiatedSubstream, quick_protobuf_codec::Codec<proto::StopMessage>>,
src_peer_id: PeerId,
limit: Option<protocol::Limit>,
}
@ -122,11 +125,11 @@ impl Circuit {
}
pub async fn accept(mut self) -> Result<(NegotiatedSubstream, Bytes), UpgradeError> {
let msg = StopMessage {
r#type: stop_message::Type::Status.into(),
let msg = proto::StopMessage {
type_pb: proto::StopMessageType::STATUS,
peer: None,
limit: None,
status: Some(Status::Ok.into()),
status: Some(proto::Status::OK),
};
self.send(msg).await?;
@ -145,18 +148,18 @@ impl Circuit {
Ok((io, read_buffer.freeze()))
}
pub async fn deny(mut self, status: Status) -> Result<(), UpgradeError> {
let msg = StopMessage {
r#type: stop_message::Type::Status.into(),
pub async fn deny(mut self, status: proto::Status) -> Result<(), UpgradeError> {
let msg = proto::StopMessage {
type_pb: proto::StopMessageType::STATUS,
peer: None,
limit: None,
status: Some(status.into()),
status: Some(status),
};
self.send(msg).await.map_err(Into::into)
}
async fn send(&mut self, msg: StopMessage) -> Result<(), prost_codec::Error> {
async fn send(&mut self, msg: proto::StopMessage) -> Result<(), quick_protobuf_codec::Error> {
self.substream.send(msg).await?;
self.substream.flush().await?;

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::{hop_message, HopMessage, Peer, Status};
use crate::proto;
use crate::protocol::{Limit, HOP_PROTOCOL_NAME, MAX_MESSAGE_SIZE};
use asynchronous_codec::{Framed, FramedParts};
use bytes::Bytes;
@ -52,16 +52,16 @@ impl upgrade::OutboundUpgrade<NegotiatedSubstream> for Upgrade {
fn upgrade_outbound(self, substream: NegotiatedSubstream, _: Self::Info) -> Self::Future {
let msg = match self {
Upgrade::Reserve => HopMessage {
r#type: hop_message::Type::Reserve.into(),
Upgrade::Reserve => proto::HopMessage {
type_pb: proto::HopMessageType::RESERVE,
peer: None,
reservation: None,
limit: None,
status: None,
},
Upgrade::Connect { dst_peer_id } => HopMessage {
r#type: hop_message::Type::Connect.into(),
peer: Some(Peer {
Upgrade::Connect { dst_peer_id } => proto::HopMessage {
type_pb: proto::HopMessageType::CONNECT,
peer: Some(proto::Peer {
id: dst_peer_id.to_bytes(),
addrs: vec![],
}),
@ -71,12 +71,15 @@ impl upgrade::OutboundUpgrade<NegotiatedSubstream> for Upgrade {
},
};
let mut substream = Framed::new(substream, prost_codec::Codec::new(MAX_MESSAGE_SIZE));
let mut substream = Framed::new(
substream,
quick_protobuf_codec::Codec::new(MAX_MESSAGE_SIZE),
);
async move {
substream.send(msg).await?;
let HopMessage {
r#type,
let proto::HopMessage {
type_pb,
peer: _,
reservation,
limit,
@ -86,31 +89,28 @@ impl upgrade::OutboundUpgrade<NegotiatedSubstream> for Upgrade {
.await
.ok_or(FatalUpgradeError::StreamClosed)??;
let r#type =
hop_message::Type::from_i32(r#type).ok_or(FatalUpgradeError::ParseTypeField)?;
match r#type {
hop_message::Type::Connect => {
match type_pb {
proto::HopMessageType::CONNECT => {
return Err(FatalUpgradeError::UnexpectedTypeConnect.into())
}
hop_message::Type::Reserve => {
proto::HopMessageType::RESERVE => {
return Err(FatalUpgradeError::UnexpectedTypeReserve.into())
}
hop_message::Type::Status => {}
proto::HopMessageType::STATUS => {}
}
let status = Status::from_i32(status.ok_or(FatalUpgradeError::MissingStatusField)?)
.ok_or(FatalUpgradeError::ParseStatusField)?;
let limit = limit.map(Into::into);
let output = match self {
Upgrade::Reserve => {
match status {
Status::Ok => {}
Status::ReservationRefused => {
match status
.ok_or(UpgradeError::Fatal(FatalUpgradeError::MissingStatusField))?
{
proto::Status::OK => {}
proto::Status::RESERVATION_REFUSED => {
return Err(ReservationFailedReason::Refused.into())
}
Status::ResourceLimitExceeded => {
proto::Status::RESOURCE_LIMIT_EXCEEDED => {
return Err(ReservationFailedReason::ResourceLimitExceeded.into())
}
s => return Err(FatalUpgradeError::UnexpectedStatus(s).into()),
@ -126,7 +126,7 @@ impl upgrade::OutboundUpgrade<NegotiatedSubstream> for Upgrade {
let addrs = reservation
.addrs
.into_iter()
.map(TryFrom::try_from)
.map(|b| Multiaddr::try_from(b.to_vec()))
.collect::<Result<Vec<Multiaddr>, _>>()
.map_err(|_| FatalUpgradeError::InvalidReservationAddrs)?;
@ -153,18 +153,20 @@ impl upgrade::OutboundUpgrade<NegotiatedSubstream> for Upgrade {
}
}
Upgrade::Connect { .. } => {
match status {
Status::Ok => {}
Status::ResourceLimitExceeded => {
match status
.ok_or(UpgradeError::Fatal(FatalUpgradeError::MissingStatusField))?
{
proto::Status::OK => {}
proto::Status::RESOURCE_LIMIT_EXCEEDED => {
return Err(CircuitFailedReason::ResourceLimitExceeded.into())
}
Status::ConnectionFailed => {
proto::Status::CONNECTION_FAILED => {
return Err(CircuitFailedReason::ConnectionFailed.into())
}
Status::NoReservation => {
proto::Status::NO_RESERVATION => {
return Err(CircuitFailedReason::NoReservation.into())
}
Status::PermissionDenied => {
proto::Status::PERMISSION_DENIED => {
return Err(CircuitFailedReason::PermissionDenied.into())
}
s => return Err(FatalUpgradeError::UnexpectedStatus(s).into()),
@ -205,8 +207,8 @@ pub enum UpgradeError {
Fatal(#[from] FatalUpgradeError),
}
impl From<prost_codec::Error> for UpgradeError {
fn from(error: prost_codec::Error) -> Self {
impl From<quick_protobuf_codec::Error> for UpgradeError {
fn from(error: quick_protobuf_codec::Error) -> Self {
Self::Fatal(error.into())
}
}
@ -234,7 +236,7 @@ pub enum ReservationFailedReason {
#[derive(Debug, Error)]
pub enum FatalUpgradeError {
#[error(transparent)]
Codec(#[from] prost_codec::Error),
Codec(#[from] quick_protobuf_codec::Error),
#[error("Stream closed")]
StreamClosed,
#[error("Expected 'status' field to be set.")]
@ -256,7 +258,7 @@ pub enum FatalUpgradeError {
#[error("Failed to parse response type field.")]
ParseStatusField,
#[error("Unexpected message status '{0:?}'")]
UnexpectedStatus(Status),
UnexpectedStatus(proto::Status),
}
pub enum Output {

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::{stop_message, Limit, Peer, Status, StopMessage};
use crate::proto;
use crate::protocol::{MAX_MESSAGE_SIZE, STOP_PROTOCOL_NAME};
use asynchronous_codec::{Framed, FramedParts};
use bytes::Bytes;
@ -51,13 +51,13 @@ impl upgrade::OutboundUpgrade<NegotiatedSubstream> for Upgrade {
type Future = BoxFuture<'static, Result<Self::Output, Self::Error>>;
fn upgrade_outbound(self, substream: NegotiatedSubstream, _: Self::Info) -> Self::Future {
let msg = StopMessage {
r#type: stop_message::Type::Connect.into(),
peer: Some(Peer {
let msg = proto::StopMessage {
type_pb: proto::StopMessageType::CONNECT,
peer: Some(proto::Peer {
id: self.relay_peer_id.to_bytes(),
addrs: vec![],
}),
limit: Some(Limit {
limit: Some(proto::Limit {
duration: Some(
self.max_circuit_duration
.as_secs()
@ -69,12 +69,15 @@ impl upgrade::OutboundUpgrade<NegotiatedSubstream> for Upgrade {
status: None,
};
let mut substream = Framed::new(substream, prost_codec::Codec::new(MAX_MESSAGE_SIZE));
let mut substream = Framed::new(
substream,
quick_protobuf_codec::Codec::new(MAX_MESSAGE_SIZE),
);
async move {
substream.send(msg).await?;
let StopMessage {
r#type,
let proto::StopMessage {
type_pb,
peer: _,
limit: _,
status,
@ -83,23 +86,19 @@ impl upgrade::OutboundUpgrade<NegotiatedSubstream> for Upgrade {
.await
.ok_or(FatalUpgradeError::StreamClosed)??;
let r#type =
stop_message::Type::from_i32(r#type).ok_or(FatalUpgradeError::ParseTypeField)?;
match r#type {
stop_message::Type::Connect => {
match type_pb {
proto::StopMessageType::CONNECT => {
return Err(FatalUpgradeError::UnexpectedTypeConnect.into())
}
stop_message::Type::Status => {}
proto::StopMessageType::STATUS => {}
}
let status = Status::from_i32(status.ok_or(FatalUpgradeError::MissingStatusField)?)
.ok_or(FatalUpgradeError::ParseStatusField)?;
match status {
Status::Ok => {}
Status::ResourceLimitExceeded => {
match status.ok_or(UpgradeError::Fatal(FatalUpgradeError::MissingStatusField))? {
proto::Status::OK => {}
proto::Status::RESOURCE_LIMIT_EXCEEDED => {
return Err(CircuitFailedReason::ResourceLimitExceeded.into())
}
Status::PermissionDenied => {
proto::Status::PERMISSION_DENIED => {
return Err(CircuitFailedReason::PermissionDenied.into())
}
s => return Err(FatalUpgradeError::UnexpectedStatus(s).into()),
@ -130,8 +129,8 @@ pub enum UpgradeError {
Fatal(#[from] FatalUpgradeError),
}
impl From<prost_codec::Error> for UpgradeError {
fn from(error: prost_codec::Error) -> Self {
impl From<quick_protobuf_codec::Error> for UpgradeError {
fn from(error: quick_protobuf_codec::Error) -> Self {
Self::Fatal(error.into())
}
}
@ -147,7 +146,7 @@ pub enum CircuitFailedReason {
#[derive(Debug, Error)]
pub enum FatalUpgradeError {
#[error(transparent)]
Codec(#[from] prost_codec::Error),
Codec(#[from] quick_protobuf_codec::Error),
#[error("Stream closed")]
StreamClosed,
#[error("Expected 'status' field to be set.")]
@ -159,5 +158,5 @@ pub enum FatalUpgradeError {
#[error("Failed to parse response type field.")]
ParseStatusField,
#[error("Unexpected message status '{0:?}'")]
UnexpectedStatus(Status),
UnexpectedStatus(proto::Status),
}