casper_client/types/
auction_state.rs

1//! Types associated with reporting auction state.
2
3mod era_validators;
4mod validator_weight;
5
6use std::{
7    collections::BTreeMap,
8    fmt::{self, Display, Formatter},
9};
10
11use itertools::Itertools;
12use serde::{Deserialize, Serialize};
13use serde_map_to_array::{BTreeMapToArray, KeyValueLabels};
14
15use casper_types::{system::auction::Bid, Digest, PublicKey};
16
17pub use era_validators::EraValidators;
18pub use validator_weight::ValidatorWeight;
19
20/// The state associated with the auction system contract as at the given block height and
21/// corresponding state root hash.
22#[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Debug)]
23#[serde(deny_unknown_fields)]
24pub struct AuctionState {
25    state_root_hash: Digest,
26    block_height: u64,
27    era_validators: Vec<EraValidators>,
28    #[serde(with = "BTreeMapToArray::<PublicKey, Bid, BidLabels>")]
29    bids: BTreeMap<PublicKey, Bid>,
30}
31
32impl AuctionState {
33    /// Returns the state root hash applicable to this auction state.
34    pub fn state_root_hash(&self) -> &Digest {
35        &self.state_root_hash
36    }
37
38    /// Returns the block height applicable to this auction state.
39    pub fn block_height(&self) -> u64 {
40        self.block_height
41    }
42
43    /// Returns the validators for the applicable era.
44    pub fn era_validators(&self) -> impl Iterator<Item = &EraValidators> {
45        self.era_validators.iter()
46    }
47
48    /// Returns the bids for the applicable era.
49    pub fn bids(&self) -> impl Iterator<Item = (&PublicKey, &Bid)> {
50        self.bids.iter()
51    }
52}
53
54impl Display for AuctionState {
55    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
56        write!(
57            formatter,
58            "auction state {{ state root hash {}, block height {}, validators {{{}}}, bids {{{}}} \
59            }}",
60            self.state_root_hash,
61            self.block_height,
62            self.era_validators().format(", "),
63            self.bids.values().format(", "),
64        )
65    }
66}
67
68struct BidLabels;
69
70impl KeyValueLabels for BidLabels {
71    const KEY: &'static str = "public_key";
72    const VALUE: &'static str = "bid";
73}