penumbra_sdk_stake/validator/
status.rs

1use penumbra_sdk_num::Amount;
2use penumbra_sdk_proto::{penumbra::core::component::stake::v1 as pb, DomainType};
3use serde::{Deserialize, Serialize};
4
5use crate::{validator::BondingState, validator::State, IdentityKey};
6
7/// The current status of a validator, including its identity, voting power, and state in the
8/// validator state machine.
9#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
10#[serde(try_from = "pb::ValidatorStatus", into = "pb::ValidatorStatus")]
11pub struct Status {
12    /// The validator's identity.
13    pub identity_key: IdentityKey,
14    /// The validator's voting power.
15    pub voting_power: Amount,
16    /// The validator's current state.
17    pub state: State,
18    /// Represents the bonding status of the validator's stake pool.
19    pub bonding_state: BondingState,
20}
21
22impl DomainType for Status {
23    type Proto = pb::ValidatorStatus;
24}
25
26impl From<Status> for pb::ValidatorStatus {
27    fn from(v: Status) -> Self {
28        pb::ValidatorStatus {
29            identity_key: Some(v.identity_key.into()),
30            voting_power: Some(v.voting_power.into()),
31            bonding_state: Some(v.bonding_state.into()),
32            state: Some(v.state.into()),
33        }
34    }
35}
36
37impl TryFrom<pb::ValidatorStatus> for Status {
38    type Error = anyhow::Error;
39    fn try_from(v: pb::ValidatorStatus) -> Result<Self, Self::Error> {
40        Ok(Status {
41            identity_key: v
42                .identity_key
43                .ok_or_else(|| anyhow::anyhow!("missing identity key field in proto"))?
44                .try_into()?,
45            voting_power: v
46                .voting_power
47                .ok_or_else(|| anyhow::anyhow!("missing voting power field in proto"))?
48                .try_into()?,
49            state: v
50                .state
51                .ok_or_else(|| anyhow::anyhow!("missing state field in proto"))?
52                .try_into()?,
53            bonding_state: v
54                .bonding_state
55                .expect("expected bonding state to be set on validator status")
56                .try_into()?,
57        })
58    }
59}