penumbra_sdk_stake/
genesis.rs

1use anyhow::Context;
2use penumbra_sdk_proto::{penumbra::core::component::stake::v1 as pb, DomainType};
3use serde::{Deserialize, Serialize};
4
5use crate::params::StakeParameters;
6
7#[derive(Deserialize, Serialize, Debug, Clone, Default)]
8#[serde(try_from = "pb::GenesisContent", into = "pb::GenesisContent")]
9pub struct Content {
10    /// The initial configuration parameters for the staking component.
11    pub stake_params: StakeParameters,
12    /// The initial validator set.
13    pub validators: Vec<pb::Validator>,
14}
15
16impl DomainType for Content {
17    type Proto = pb::GenesisContent;
18}
19
20impl From<Content> for pb::GenesisContent {
21    fn from(value: Content) -> Self {
22        pb::GenesisContent {
23            stake_params: Some(value.stake_params.into()),
24            validators: value.validators.into_iter().map(Into::into).collect(),
25        }
26    }
27}
28
29impl TryFrom<pb::GenesisContent> for Content {
30    type Error = anyhow::Error;
31
32    fn try_from(msg: pb::GenesisContent) -> Result<Self, Self::Error> {
33        Ok(Content {
34            stake_params: msg
35                .stake_params
36                .context("stake params not present in protobuf message")?
37                .try_into()?,
38            validators: msg
39                .validators
40                .into_iter()
41                .map(TryInto::try_into)
42                .collect::<Result<_, _>>()?,
43        })
44    }
45}