penumbra_stake/validator/
info.rs

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
57
58
59
60
61
62
63
64
65
66
67
68
69
use penumbra_proto::{
    core::component::stake::v1::ValidatorInfoResponse, penumbra::core::component::stake::v1 as pb,
    DomainType,
};
use serde::{Deserialize, Serialize};

use super::{Status, Validator};
use crate::rate::RateData;

#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
#[serde(try_from = "pb::ValidatorInfo", into = "pb::ValidatorInfo")]
pub struct Info {
    pub validator: Validator,
    pub status: Status,
    pub rate_data: RateData,
}

impl DomainType for Info {
    type Proto = pb::ValidatorInfo;
}

impl From<Info> for pb::ValidatorInfo {
    fn from(v: Info) -> Self {
        pb::ValidatorInfo {
            validator: Some(v.validator.into()),
            status: Some(v.status.into()),
            rate_data: Some(v.rate_data.into()),
        }
    }
}

impl From<Info> for ValidatorInfoResponse {
    fn from(v: Info) -> Self {
        ValidatorInfoResponse {
            validator_info: Some(v.into()),
        }
    }
}

impl TryFrom<pb::ValidatorInfo> for Info {
    type Error = anyhow::Error;
    fn try_from(v: pb::ValidatorInfo) -> Result<Self, Self::Error> {
        Ok(Info {
            validator: v
                .validator
                .ok_or_else(|| anyhow::anyhow!("missing validator field in proto"))?
                .try_into()?,
            status: v
                .status
                .ok_or_else(|| anyhow::anyhow!("missing status field in proto"))?
                .try_into()?,
            rate_data: v
                .rate_data
                .ok_or_else(|| anyhow::anyhow!("missing rate_data field in proto"))?
                .try_into()?,
        })
    }
}

impl TryFrom<ValidatorInfoResponse> for Info {
    type Error = anyhow::Error;

    fn try_from(info_resp: ValidatorInfoResponse) -> Result<Self, Self::Error> {
        info_resp
            .validator_info
            .ok_or_else(|| anyhow::anyhow!("empty ValidatorInfoResponse message"))?
            .try_into()
    }
}