casper_storage/data_access_layer/
era_validators.rs

1//! Support for querying era validators.
2
3use crate::tracking_copy::TrackingCopyError;
4use casper_types::{system::auction::EraValidators, Digest};
5use std::fmt::{Display, Formatter};
6
7/// Request for era validators.
8#[derive(Debug, Clone, PartialEq, Eq)]
9pub struct EraValidatorsRequest {
10    state_hash: Digest,
11}
12
13impl EraValidatorsRequest {
14    /// Constructs a new EraValidatorsRequest.
15    pub fn new(state_hash: Digest) -> Self {
16        EraValidatorsRequest { state_hash }
17    }
18
19    /// Get the state hash.
20    pub fn state_hash(&self) -> Digest {
21        self.state_hash
22    }
23}
24
25/// Result enum that represents all possible outcomes of a era validators request.
26#[derive(Debug)]
27pub enum EraValidatorsResult {
28    /// Returned if auction is not found. This is a catastrophic outcome.
29    AuctionNotFound,
30    /// Returned if a passed state root hash is not found. This is recoverable.
31    RootNotFound,
32    /// Value not found. This is not erroneous if the record does not exist.
33    ValueNotFound(String),
34    /// There is no systemic issue, but the query itself errored.
35    Failure(TrackingCopyError),
36    /// The query succeeded.
37    Success {
38        /// Era Validators.
39        era_validators: EraValidators,
40    },
41}
42
43impl EraValidatorsResult {
44    /// Returns true if success.
45    pub fn is_success(&self) -> bool {
46        matches!(self, EraValidatorsResult::Success { .. })
47    }
48
49    /// Takes era validators.
50    pub fn take_era_validators(self) -> Option<EraValidators> {
51        match self {
52            EraValidatorsResult::AuctionNotFound
53            | EraValidatorsResult::RootNotFound
54            | EraValidatorsResult::ValueNotFound(_)
55            | EraValidatorsResult::Failure(_) => None,
56            EraValidatorsResult::Success { era_validators } => Some(era_validators),
57        }
58    }
59}
60
61impl Display for EraValidatorsResult {
62    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
63        match self {
64            EraValidatorsResult::AuctionNotFound => write!(f, "system auction not found"),
65            EraValidatorsResult::RootNotFound => write!(f, "state root not found"),
66            EraValidatorsResult::ValueNotFound(msg) => write!(f, "value not found: {}", msg),
67            EraValidatorsResult::Failure(tce) => write!(f, "{}", tce),
68            EraValidatorsResult::Success { .. } => {
69                write!(f, "success")
70            }
71        }
72    }
73}