use crate::rpc::types::Bytes;
use cfx_types::{H256, U64};
use diem_crypto::ValidCryptoMaterial;
use diem_types::{
epoch_state::EpochState as PrimitiveEpochState,
validator_verifier::{
ValidatorConsensusInfo as PrimitiveValidatorConsensusInfo,
ValidatorVerifier as PrimitiveValidatorVerifier,
},
};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct EpochState {
epoch: U64,
verifier: ValidatorVerifier,
vrf_seed: Bytes,
}
impl From<&PrimitiveEpochState> for EpochState {
fn from(value: &PrimitiveEpochState) -> Self {
Self {
epoch: value.epoch.into(),
verifier: value.verifier().into(),
vrf_seed: value.vrf_seed.clone().into(),
}
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ValidatorVerifier {
address_to_validator_info: BTreeMap<H256, ValidatorConsensusInfo>,
quorum_voting_power: U64,
total_voting_power: U64,
}
impl From<&PrimitiveValidatorVerifier> for ValidatorVerifier {
fn from(value: &PrimitiveValidatorVerifier) -> Self {
Self {
address_to_validator_info: value
.address_to_validator_info()
.iter()
.map(|(k, v)| (k.to_u8().into(), v.into()))
.collect(),
quorum_voting_power: value.quorum_voting_power().into(),
total_voting_power: value.total_voting_power().into(),
}
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ValidatorConsensusInfo {
public_key: Bytes,
vrf_public_key: Option<Bytes>,
voting_power: U64,
}
impl From<&PrimitiveValidatorConsensusInfo> for ValidatorConsensusInfo {
fn from(value: &PrimitiveValidatorConsensusInfo) -> Self {
Self {
public_key: value
.public_key()
.clone()
.raw()
.as_affine()
.to_compressed()
.to_vec()
.into(),
vrf_public_key: value
.vrf_public_key()
.clone()
.map(|k| k.to_bytes().into()),
voting_power: value.voting_power().into(),
}
}
}