Skip to content

Commit

Permalink
derives Error trait for ClusterInfoError and core::result::Error (bac…
Browse files Browse the repository at this point in the history
…kport #27208) (#27220)

derives Error trait for ClusterInfoError and core::result::Error (#27208)

(cherry picked from commit fea66c8)

Co-authored-by: behzad nouri <behzadnouri@gmail.com>
  • Loading branch information
mergify[bot] and behzadnouri authored Aug 18, 2022
1 parent e48d8a9 commit 2f5ab9c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 59 deletions.
80 changes: 22 additions & 58 deletions core/src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bincode::ErrorKind>),
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<bincode::ErrorKind>),
#[error(transparent)]
WeightedIndex(#[from] rand::distributions::weighted::WeightedError),
}

pub type Result<T> = std::result::Result<T, Error>;

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<crossbeam_channel::RecvError> for Error {
fn from(e: crossbeam_channel::RecvError) -> Error {
Error::Recv(e)
}
}
impl std::convert::From<crossbeam_channel::ReadyTimeoutError> for Error {
fn from(_e: crossbeam_channel::ReadyTimeoutError) -> Error {
Error::ReadyTimeout
}
}
impl std::convert::From<crossbeam_channel::RecvTimeoutError> for Error {
fn from(e: crossbeam_channel::RecvTimeoutError) -> Error {
Error::RecvTimeout(e)
}
}
impl std::convert::From<cluster_info::ClusterInfoError> for Error {
fn from(e: cluster_info::ClusterInfoError) -> Error {
Error::ClusterInfo(e)
}
}
impl<T> std::convert::From<crossbeam_channel::TrySendError<T>> for Error {
fn from(_e: crossbeam_channel::TrySendError<T>) -> Error {
Error::TrySend
Expand All @@ -60,31 +49,6 @@ impl<T> std::convert::From<crossbeam_channel::SendError<T>> for Error {
Error::Send
}
}
impl std::convert::From<std::io::Error> for Error {
fn from(e: std::io::Error) -> Error {
Error::Io(e)
}
}
impl std::convert::From<std::boxed::Box<bincode::ErrorKind>> for Error {
fn from(e: std::boxed::Box<bincode::ErrorKind>) -> Error {
Error::Serialize(e)
}
}
impl std::convert::From<blockstore::BlockstoreError> for Error {
fn from(e: blockstore::BlockstoreError) -> Error {
Error::Blockstore(e)
}
}
impl std::convert::From<rand::distributions::weighted::WeightedError> for Error {
fn from(e: rand::distributions::weighted::WeightedError) -> Error {
Error::WeightedIndex(e)
}
}
impl std::convert::From<GossipError> for Error {
fn from(e: GossipError) -> Error {
Error::Gossip(e)
}
}

#[cfg(test)]
mod tests {
Expand Down
8 changes: 7 additions & 1 deletion gossip/src/cluster_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
}

Expand Down

0 comments on commit 2f5ab9c

Please sign in to comment.