use std::{
borrow::Cow,
collections::{BTreeMap, HashSet},
fmt::{self, Display, Formatter},
};
use casper_types::{EraId, PublicKey, Timestamp, U512};
use serde::Serialize;
use crate::components::consensus::{highway_core::State, ClContext, HighwayProtocol};
use super::Era;
#[derive(Debug, Serialize)]
pub(crate) struct EraDump<'a> {
pub(crate) id: EraId,
pub(crate) start_time: Timestamp,
pub(crate) start_height: u64,
pub(crate) faulty: &'a HashSet<PublicKey>,
pub(crate) cannot_propose: &'a HashSet<PublicKey>,
pub(crate) accusations: &'a HashSet<PublicKey>,
pub(crate) validators: &'a BTreeMap<PublicKey, U512>,
pub(crate) highway_state: &'a State<ClContext>,
}
impl Display for EraDump<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "era {}: TBD", self.id)
}
}
impl<'a> EraDump<'a> {
pub(crate) fn dump_era(era: &'a Era, era_id: EraId) -> Result<Self, Cow<'static, str>> {
let highway = era
.consensus
.as_any()
.downcast_ref::<HighwayProtocol<ClContext>>()
.ok_or(Cow::Borrowed(
"could not downcast `ConsensusProtocol` into `HighwayProtocol<ClContext>`",
))?;
Ok(EraDump {
id: era_id,
start_time: era.start_time,
start_height: era.start_height,
faulty: &era.faulty,
cannot_propose: &era.cannot_propose,
accusations: &era.accusations,
validators: &era.validators,
highway_state: highway.highway().state(),
})
}
}