penumbra_stake/validator/
state.rsuse anyhow::anyhow;
use penumbra_proto::{penumbra::core::component::stake::v1 as pb, DomainType};
use serde::{Deserialize, Serialize};
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Copy, Clone, Serialize, Deserialize)]
#[serde(try_from = "pb::ValidatorState", into = "pb::ValidatorState")]
pub enum State {
Defined,
Inactive,
Active,
Jailed,
Tombstoned,
Disabled,
}
impl std::fmt::Display for State {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
State::Defined => write!(f, "Defined"),
State::Inactive => write!(f, "Inactive"),
State::Active => write!(f, "Active"),
State::Jailed => write!(f, "Jailed"),
State::Tombstoned => write!(f, "Tombstoned"),
State::Disabled => write!(f, "Disabled"),
}
}
}
impl DomainType for State {
type Proto = pb::ValidatorState;
}
impl From<State> for pb::ValidatorState {
fn from(v: State) -> Self {
pb::ValidatorState {
state: match v {
State::Defined => pb::validator_state::ValidatorStateEnum::Defined,
State::Inactive => pb::validator_state::ValidatorStateEnum::Inactive,
State::Active => pb::validator_state::ValidatorStateEnum::Active,
State::Jailed => pb::validator_state::ValidatorStateEnum::Jailed,
State::Tombstoned => pb::validator_state::ValidatorStateEnum::Tombstoned,
State::Disabled => pb::validator_state::ValidatorStateEnum::Disabled,
} as i32,
}
}
}
impl TryFrom<pb::ValidatorState> for State {
type Error = anyhow::Error;
fn try_from(v: pb::ValidatorState) -> Result<Self, Self::Error> {
let validator_state = pb::validator_state::ValidatorStateEnum::try_from(v.state)
.map_err(|e| anyhow::anyhow!("invalid validator state, error: {e}"))?;
match validator_state {
pb::validator_state::ValidatorStateEnum::Defined => Ok(State::Defined),
pb::validator_state::ValidatorStateEnum::Inactive => Ok(State::Inactive),
pb::validator_state::ValidatorStateEnum::Active => Ok(State::Active),
pb::validator_state::ValidatorStateEnum::Jailed => Ok(State::Jailed),
pb::validator_state::ValidatorStateEnum::Tombstoned => Ok(State::Tombstoned),
pb::validator_state::ValidatorStateEnum::Disabled => Ok(State::Disabled),
pb::validator_state::ValidatorStateEnum::Unspecified => {
Err(anyhow!("unspecified validator state!"))
}
}
}
}