1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use serde_json::Value;

pub type RpcValidatorsOrderedResponse =
    Vec<unc_primitives::views::validator_power_and_pledge_view::ValidatorPowerAndPledgeView>;

#[derive(thiserror::Error, Debug, serde::Serialize, serde::Deserialize)]
#[serde(tag = "name", content = "info", rename_all = "SCREAMING_SNAKE_CASE")]
pub enum RpcValidatorError {
    #[error("Epoch not found")]
    UnknownEpoch,
    #[error("Validator info unavailable")]
    ValidatorInfoUnavailable,
    #[error("The node reached its limits. Try again later. More details: {error_message}")]
    InternalError { error_message: String },
}

#[derive(serde::Serialize, serde::Deserialize, Debug, arbitrary::Arbitrary, PartialEq, Eq)]
pub struct RpcValidatorRequest {
    #[serde(flatten)]
    pub epoch_reference: unc_primitives::types::EpochReference,
}

#[derive(serde::Serialize, serde::Deserialize, Debug)]
pub struct RpcValidatorsOrderedRequest {
    pub block_id: unc_primitives::types::MaybeBlockId,
}

#[derive(serde::Serialize, serde::Deserialize, Debug)]
pub struct RpcValidatorResponse {
    #[serde(flatten)]
    pub validator_info: unc_primitives::views::EpochValidatorInfo,
}

impl From<RpcValidatorError> for crate::errors::RpcError {
    fn from(error: RpcValidatorError) -> Self {
        let error_data = match &error {
            RpcValidatorError::UnknownEpoch => Some(Value::String("Unknown Epoch".to_string())),
            RpcValidatorError::ValidatorInfoUnavailable => {
                Some(Value::String("Validator info unavailable".to_string()))
            }
            RpcValidatorError::InternalError { .. } => Some(Value::String(error.to_string())),
        };

        let error_data_value = match serde_json::to_value(error) {
            Ok(value) => value,
            Err(err) => {
                return Self::new_internal_error(
                    None,
                    format!("Failed to serialize RpcValidatorError: {:?}", err),
                )
            }
        };

        Self::new_internal_or_handler_error(error_data, error_data_value)
    }
}