From 2f5ab9c5ebf5485c15022ee71d4909d159959353 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Thu, 18 Aug 2022 01:18:40 +0000 Subject: [PATCH] derives Error trait for ClusterInfoError and core::result::Error (backport #27208) (#27220) derives Error trait for ClusterInfoError and core::result::Error (#27208) (cherry picked from commit fea66c8b6365c924b71daa264d0919127b330008) Co-authored-by: behzad nouri --- core/src/result.rs | 80 +++++++++++--------------------------- gossip/src/cluster_info.rs | 8 +++- 2 files changed, 29 insertions(+), 59 deletions(-) diff --git a/core/src/result.rs b/core/src/result.rs index 6c9b66b6d459c0..2aa8f8718f5141 100644 --- a/core/src/result.rs +++ b/core/src/result.rs @@ -3,53 +3,42 @@ use { solana_gossip::{cluster_info, gossip_error::GossipError}, solana_ledger::blockstore, + thiserror::Error, }; -#[derive(Debug)] +#[derive(Debug, Error)] pub enum Error { - Io(std::io::Error), - Recv(crossbeam_channel::RecvError), + #[error(transparent)] + Blockstore(#[from] blockstore::BlockstoreError), + #[error(transparent)] + ClusterInfo(#[from] cluster_info::ClusterInfoError), + #[error(transparent)] + Gossip(#[from] GossipError), + #[error(transparent)] + Io(#[from] std::io::Error), + #[error("ReadyTimeout")] ReadyTimeout, - RecvTimeout(crossbeam_channel::RecvTimeoutError), - TrySend, - Serialize(std::boxed::Box), - ClusterInfo(cluster_info::ClusterInfoError), + #[error(transparent)] + Recv(#[from] crossbeam_channel::RecvError), + #[error(transparent)] + RecvTimeout(#[from] crossbeam_channel::RecvTimeoutError), + #[error("Send")] Send, - Blockstore(blockstore::BlockstoreError), - WeightedIndex(rand::distributions::weighted::WeightedError), - Gossip(GossipError), + #[error("TrySend")] + TrySend, + #[error(transparent)] + Serialize(#[from] std::boxed::Box), + #[error(transparent)] + WeightedIndex(#[from] rand::distributions::weighted::WeightedError), } pub type Result = std::result::Result; -impl std::fmt::Display for Error { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "solana error") - } -} - -impl std::error::Error for Error {} - -impl std::convert::From for Error { - fn from(e: crossbeam_channel::RecvError) -> Error { - Error::Recv(e) - } -} impl std::convert::From for Error { fn from(_e: crossbeam_channel::ReadyTimeoutError) -> Error { Error::ReadyTimeout } } -impl std::convert::From for Error { - fn from(e: crossbeam_channel::RecvTimeoutError) -> Error { - Error::RecvTimeout(e) - } -} -impl std::convert::From for Error { - fn from(e: cluster_info::ClusterInfoError) -> Error { - Error::ClusterInfo(e) - } -} impl std::convert::From> for Error { fn from(_e: crossbeam_channel::TrySendError) -> Error { Error::TrySend @@ -60,31 +49,6 @@ impl std::convert::From> for Error { Error::Send } } -impl std::convert::From for Error { - fn from(e: std::io::Error) -> Error { - Error::Io(e) - } -} -impl std::convert::From> for Error { - fn from(e: std::boxed::Box) -> Error { - Error::Serialize(e) - } -} -impl std::convert::From for Error { - fn from(e: blockstore::BlockstoreError) -> Error { - Error::Blockstore(e) - } -} -impl std::convert::From for Error { - fn from(e: rand::distributions::weighted::WeightedError) -> Error { - Error::WeightedIndex(e) - } -} -impl std::convert::From for Error { - fn from(e: GossipError) -> Error { - Error::Gossip(e) - } -} #[cfg(test)] mod tests { diff --git a/gossip/src/cluster_info.rs b/gossip/src/cluster_info.rs index c0a8712f783ba8..92b14bf9fba125 100644 --- a/gossip/src/cluster_info.rs +++ b/gossip/src/cluster_info.rs @@ -92,6 +92,7 @@ use { thread::{sleep, Builder, JoinHandle}, time::{Duration, Instant}, }, + thiserror::Error, }; /// The Data plane fanout size, also used as the neighborhood size @@ -138,12 +139,17 @@ const MIN_STAKE_FOR_GOSSIP: u64 = solana_sdk::native_token::LAMPORTS_PER_SOL; /// Minimum number of staked nodes for enforcing stakes in gossip. const MIN_NUM_STAKED_NODES: usize = 500; -#[derive(Debug, PartialEq, Eq)] +#[derive(Debug, PartialEq, Eq, Error)] pub enum ClusterInfoError { + #[error("NoPeers")] NoPeers, + #[error("NoLeader")] NoLeader, + #[error("BadContactInfo")] BadContactInfo, + #[error("BadGossipAddress")] BadGossipAddress, + #[error("TooManyIncrementalSnapshotHashes")] TooManyIncrementalSnapshotHashes, }