mod era_validators;
mod validator_weight;
use std::{
collections::BTreeMap,
fmt::{self, Display, Formatter},
};
use itertools::Itertools;
use serde::{Deserialize, Serialize};
use serde_map_to_array::{BTreeMapToArray, KeyValueLabels};
use casper_types::{system::auction::Bid, Digest, PublicKey};
pub use era_validators::EraValidators;
pub use validator_weight::ValidatorWeight;
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct AuctionState {
state_root_hash: Digest,
block_height: u64,
era_validators: Vec<EraValidators>,
#[serde(with = "BTreeMapToArray::<PublicKey, Bid, BidLabels>")]
bids: BTreeMap<PublicKey, Bid>,
}
impl AuctionState {
pub fn state_root_hash(&self) -> &Digest {
&self.state_root_hash
}
pub fn block_height(&self) -> u64 {
self.block_height
}
pub fn era_validators(&self) -> impl Iterator<Item = &EraValidators> {
self.era_validators.iter()
}
pub fn bids(&self) -> impl Iterator<Item = (&PublicKey, &Bid)> {
self.bids.iter()
}
}
impl Display for AuctionState {
fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
write!(
formatter,
"auction state {{ state root hash {}, block height {}, validators {{{}}}, bids {{{}}} \
}}",
self.state_root_hash,
self.block_height,
self.era_validators().format(", "),
self.bids.values().format(", "),
)
}
}
struct BidLabels;
impl KeyValueLabels for BidLabels {
const KEY: &'static str = "public_key";
const VALUE: &'static str = "bid";
}