feat: properly encapsulate quick_protobuf::Error

I noticed that we also still leak that dependency in several crates by providing a `From` impl so I've removed that one as well.

Resolves #3534.

Pull-Request: #3894.
This commit is contained in:
Thomas Eizinger 2023-05-12 04:26:01 +02:00 committed by GitHub
parent 6985d72462
commit 1bf6264cbe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 17 additions and 50 deletions

View File

@ -47,8 +47,6 @@ mod proto {
/// Multi-address re-export. /// Multi-address re-export.
pub use multiaddr; pub use multiaddr;
use std::fmt;
use std::fmt::Formatter;
pub type Negotiated<T> = multistream_select::Negotiated<T>; pub type Negotiated<T> = multistream_select::Negotiated<T>;
#[deprecated(since = "0.39.0", note = "Depend on `libp2p-identity` instead.")] #[deprecated(since = "0.39.0", note = "Depend on `libp2p-identity` instead.")]
@ -130,16 +128,5 @@ pub use transport::Transport;
pub use upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeError, UpgradeInfo}; pub use upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeError, UpgradeInfo};
#[derive(Debug, thiserror::Error)] #[derive(Debug, thiserror::Error)]
pub struct DecodeError(String); #[error(transparent)]
pub struct DecodeError(quick_protobuf::Error);
impl From<quick_protobuf::Error> for DecodeError {
fn from(e: quick_protobuf::Error) -> Self {
Self(e.to_string())
}
}
impl fmt::Display for DecodeError {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}

View File

@ -36,8 +36,7 @@ impl PeerRecord {
let (payload, signing_key) = let (payload, signing_key) =
envelope.payload_and_signing_key(String::from(DOMAIN_SEP), PAYLOAD_TYPE.as_bytes())?; envelope.payload_and_signing_key(String::from(DOMAIN_SEP), PAYLOAD_TYPE.as_bytes())?;
let mut reader = BytesReader::from_bytes(payload); let mut reader = BytesReader::from_bytes(payload);
let record = let record = proto::PeerRecord::from_reader(&mut reader, payload).map_err(DecodeError)?;
proto::PeerRecord::from_reader(&mut reader, payload).map_err(DecodeError::from)?;
let peer_id = PeerId::from_bytes(&record.peer_id)?; let peer_id = PeerId::from_bytes(&record.peer_id)?;

View File

@ -97,8 +97,7 @@ impl SignedEnvelope {
use quick_protobuf::MessageRead; use quick_protobuf::MessageRead;
let mut reader = BytesReader::from_bytes(bytes); let mut reader = BytesReader::from_bytes(bytes);
let envelope = let envelope = proto::Envelope::from_reader(&mut reader, bytes).map_err(DecodeError)?;
proto::Envelope::from_reader(&mut reader, bytes).map_err(DecodeError::from)?;
Ok(Self { Ok(Self {
key: PublicKey::try_decode_protobuf(&envelope.public_key)?, key: PublicKey::try_decode_protobuf(&envelope.public_key)?,

View File

@ -28,7 +28,7 @@ mod proto {
use crate::io::{framed::NoiseFramed, Output}; use crate::io::{framed::NoiseFramed, Output};
use crate::protocol::{KeypairIdentity, STATIC_KEY_DOMAIN}; use crate::protocol::{KeypairIdentity, STATIC_KEY_DOMAIN};
use crate::Error; use crate::{DecodeError, Error};
use bytes::Bytes; use bytes::Bytes;
use futures::prelude::*; use futures::prelude::*;
use libp2p_identity as identity; use libp2p_identity as identity;
@ -140,7 +140,8 @@ where
{ {
let msg = recv(state).await?; let msg = recv(state).await?;
let mut reader = BytesReader::from_bytes(&msg[..]); let mut reader = BytesReader::from_bytes(&msg[..]);
let pb = proto::NoiseHandshakePayload::from_reader(&mut reader, &msg[..])?; let pb =
proto::NoiseHandshakePayload::from_reader(&mut reader, &msg[..]).map_err(DecodeError)?;
state.id_remote_pubkey = Some(identity::PublicKey::try_decode_protobuf(&pb.identity_key)?); state.id_remote_pubkey = Some(identity::PublicKey::try_decode_protobuf(&pb.identity_key)?);

View File

@ -69,8 +69,6 @@ use libp2p_core::{InboundUpgrade, OutboundUpgrade, UpgradeInfo};
use libp2p_identity as identity; use libp2p_identity as identity;
use libp2p_identity::PeerId; use libp2p_identity::PeerId;
use snow::params::NoiseParams; use snow::params::NoiseParams;
use std::fmt;
use std::fmt::Formatter;
use std::pin::Pin; use std::pin::Pin;
/// The configuration for the noise handshake. /// The configuration for the noise handshake.
@ -211,29 +209,12 @@ pub enum Error {
BadSignature, BadSignature,
#[error("Authentication failed")] #[error("Authentication failed")]
AuthenticationFailed, AuthenticationFailed,
#[error(transparent)] #[error("failed to decode protobuf ")]
InvalidPayload(DecodeError), InvalidPayload(#[from] DecodeError),
#[error(transparent)] #[error(transparent)]
SigningError(#[from] libp2p_identity::SigningError), SigningError(#[from] libp2p_identity::SigningError),
} }
#[derive(Debug, thiserror::Error)] #[derive(Debug, thiserror::Error)]
pub struct DecodeError(String); #[error(transparent)]
pub struct DecodeError(quick_protobuf::Error);
impl fmt::Display for DecodeError {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl From<quick_protobuf::Error> for DecodeError {
fn from(e: quick_protobuf::Error) -> Self {
Self(e.to_string())
}
}
impl From<quick_protobuf::Error> for Error {
fn from(e: quick_protobuf::Error) -> Self {
Error::InvalidPayload(e.into())
}
}

View File

@ -41,7 +41,7 @@ pub enum PlainTextError {
} }
#[derive(Debug)] #[derive(Debug)]
pub struct DecodeError(quick_protobuf::Error); pub struct DecodeError(pub(crate) quick_protobuf::Error);
impl fmt::Display for DecodeError { impl fmt::Display for DecodeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@ -87,9 +87,9 @@ impl From<IoError> for PlainTextError {
} }
} }
impl From<quick_protobuf::Error> for PlainTextError { impl From<DecodeError> for PlainTextError {
fn from(err: quick_protobuf::Error) -> PlainTextError { fn from(err: DecodeError) -> PlainTextError {
PlainTextError::InvalidPayload(DecodeError(err)) PlainTextError::InvalidPayload(err)
} }
} }

View File

@ -18,7 +18,7 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE. // DEALINGS IN THE SOFTWARE.
use crate::error::PlainTextError; use crate::error::{DecodeError, PlainTextError};
use crate::proto::Exchange; use crate::proto::Exchange;
use crate::PlainText2Config; use crate::PlainText2Config;
@ -74,7 +74,7 @@ impl HandshakeContext<Local> {
exchange_bytes: BytesMut, exchange_bytes: BytesMut,
) -> Result<HandshakeContext<Remote>, PlainTextError> { ) -> Result<HandshakeContext<Remote>, PlainTextError> {
let mut reader = BytesReader::from_bytes(&exchange_bytes); let mut reader = BytesReader::from_bytes(&exchange_bytes);
let prop = Exchange::from_reader(&mut reader, &exchange_bytes)?; let prop = Exchange::from_reader(&mut reader, &exchange_bytes).map_err(DecodeError)?;
let public_key = PublicKey::try_decode_protobuf(&prop.pubkey.unwrap_or_default())?; let public_key = PublicKey::try_decode_protobuf(&prop.pubkey.unwrap_or_default())?;
let peer_id = PeerId::from_bytes(&prop.id.unwrap_or_default())?; let peer_id = PeerId::from_bytes(&prop.id.unwrap_or_default())?;