1#![allow(deprecated)]
2
3use alloc::{
4 collections::{btree_map::Entry, BTreeMap},
5 vec::Vec,
6};
7#[cfg(feature = "json-schema")]
8use once_cell::sync::Lazy;
9#[cfg(feature = "json-schema")]
10use schemars::JsonSchema;
11use serde::{Deserialize, Serialize};
12#[cfg(feature = "json-schema")]
13use serde_map_to_array::KeyValueJsonSchema;
14use serde_map_to_array::{BTreeMapToArray, KeyValueLabels};
15
16use crate::{
17 system::auction::{
18 Bid, BidKind, DelegatorBid, DelegatorKind, EraValidators, Staking, ValidatorBid,
19 },
20 Digest, EraId, PublicKey, U512,
21};
22
23#[cfg(feature = "json-schema")]
24static ERA_VALIDATORS: Lazy<EraValidators> = Lazy::new(|| {
25 use crate::SecretKey;
26
27 let secret_key_1 = SecretKey::ed25519_from_bytes([42; SecretKey::ED25519_LENGTH]).unwrap();
28 let public_key_1 = PublicKey::from(&secret_key_1);
29
30 let mut validator_weights = BTreeMap::new();
31 validator_weights.insert(public_key_1, U512::from(10));
32
33 let mut era_validators = BTreeMap::new();
34 era_validators.insert(EraId::from(10u64), validator_weights);
35
36 era_validators
37});
38
39#[cfg(feature = "json-schema")]
40static AUCTION_INFO: Lazy<AuctionState> = Lazy::new(|| {
41 use crate::{system::auction::DelegationRate, AccessRights, SecretKey, URef};
42 use num_traits::Zero;
43
44 let state_root_hash = Digest::from([11; Digest::LENGTH]);
45 let validator_secret_key =
46 SecretKey::ed25519_from_bytes([42; SecretKey::ED25519_LENGTH]).unwrap();
47 let validator_public_key = PublicKey::from(&validator_secret_key);
48
49 let mut bids = vec![];
50 let validator_bid = ValidatorBid::unlocked(
51 validator_public_key.clone(),
52 URef::new([250; 32], AccessRights::READ_ADD_WRITE),
53 U512::from(20),
54 DelegationRate::zero(),
55 0,
56 u64::MAX,
57 0,
58 );
59 bids.push(BidKind::Validator(Box::new(validator_bid)));
60
61 let delegator_secret_key =
62 SecretKey::ed25519_from_bytes([43; SecretKey::ED25519_LENGTH]).unwrap();
63 let delegator_public_key = PublicKey::from(&delegator_secret_key);
64 let delegator_bid = DelegatorBid::unlocked(
65 delegator_public_key.into(),
66 U512::from(10),
67 URef::new([251; 32], AccessRights::READ_ADD_WRITE),
68 validator_public_key,
69 );
70 bids.push(BidKind::Delegator(Box::new(delegator_bid)));
71
72 let height: u64 = 10;
73 let era_validators = ERA_VALIDATORS.clone();
74 AuctionState::new(state_root_hash, height, era_validators, bids)
75});
76
77#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
79#[cfg_attr(feature = "json-schema", derive(JsonSchema))]
80#[serde(deny_unknown_fields)]
81#[deprecated(since = "5.0.0")]
82pub struct JsonValidatorWeights {
83 public_key: PublicKey,
84 weight: U512,
85}
86
87#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
89#[cfg_attr(feature = "json-schema", derive(JsonSchema))]
90#[serde(deny_unknown_fields)]
91#[deprecated(since = "5.0.0")]
92pub struct JsonEraValidators {
93 era_id: EraId,
94 validator_weights: Vec<JsonValidatorWeights>,
95}
96
97#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
99#[cfg_attr(feature = "json-schema", derive(JsonSchema))]
100#[serde(deny_unknown_fields)]
101#[deprecated(since = "5.0.0")]
102pub struct AuctionState {
103 pub state_root_hash: Digest,
105 pub block_height: u64,
107 pub era_validators: Vec<JsonEraValidators>,
109 #[serde(with = "BTreeMapToArray::<PublicKey, Bid, BidLabels>")]
111 bids: BTreeMap<PublicKey, Bid>,
112}
113
114impl AuctionState {
115 pub fn new(
119 state_root_hash: Digest,
120 block_height: u64,
121 era_validators: EraValidators,
122 bids: Vec<BidKind>,
123 ) -> Self {
124 let mut json_era_validators: Vec<JsonEraValidators> = Vec::new();
125 for (era_id, validator_weights) in era_validators.iter() {
126 let mut json_validator_weights: Vec<JsonValidatorWeights> = Vec::new();
127 for (public_key, weight) in validator_weights.iter() {
128 json_validator_weights.push(JsonValidatorWeights {
129 public_key: public_key.clone(),
130 weight: *weight,
131 });
132 }
133 json_era_validators.push(JsonEraValidators {
134 era_id: *era_id,
135 validator_weights: json_validator_weights,
136 });
137 }
138
139 let staking = {
140 let mut staking: Staking = BTreeMap::new();
141 for bid_kind in bids.iter().filter(|x| x.is_unified()) {
142 if let BidKind::Unified(bid) = bid_kind {
143 let public_key = bid.validator_public_key().clone();
144 let validator_bid = ValidatorBid::unlocked(
145 bid.validator_public_key().clone(),
146 *bid.bonding_purse(),
147 *bid.staked_amount(),
148 *bid.delegation_rate(),
149 0,
150 u64::MAX,
151 0,
152 );
153 let mut delegators: BTreeMap<DelegatorKind, DelegatorBid> = BTreeMap::new();
154 for (delegator_public_key, delegator) in bid.delegators() {
155 delegators.insert(
156 DelegatorKind::PublicKey(delegator_public_key.clone()),
157 DelegatorBid::from(delegator.clone()),
158 );
159 }
160 staking.insert(public_key, (validator_bid, delegators));
161 }
162 }
163
164 for bid_kind in bids.iter().filter(|x| x.is_validator()) {
165 if let BidKind::Validator(validator_bid) = bid_kind {
166 let public_key = validator_bid.validator_public_key().clone();
167 staking.insert(public_key, (*validator_bid.clone(), BTreeMap::new()));
168 }
169 }
170
171 for bid_kind in bids.iter().filter(|x| x.is_delegator()) {
172 if let BidKind::Delegator(delegator_bid) = bid_kind {
173 let validator_public_key = delegator_bid.validator_public_key().clone();
174 if let Entry::Occupied(mut occupant) =
175 staking.entry(validator_public_key.clone())
176 {
177 let (_, delegators) = occupant.get_mut();
178 delegators.insert(
179 delegator_bid.delegator_kind().clone(),
180 *delegator_bid.clone(),
181 );
182 }
183 }
184 }
185 staking
186 };
187
188 let mut bids: BTreeMap<PublicKey, Bid> = BTreeMap::new();
189 for (public_key, (validator_bid, delegators)) in staking {
190 let bid = Bid::from_non_unified(validator_bid, delegators);
191 bids.insert(public_key, bid);
192 }
193
194 AuctionState {
195 state_root_hash,
196 block_height,
197 era_validators: json_era_validators,
198 bids,
199 }
200 }
201
202 #[doc(hidden)]
204 #[cfg(feature = "json-schema")]
205 pub fn example() -> &'static Self {
206 &AUCTION_INFO
207 }
208}
209
210struct BidLabels;
211
212impl KeyValueLabels for BidLabels {
213 const KEY: &'static str = "public_key";
214 const VALUE: &'static str = "bid";
215}
216
217#[cfg(feature = "json-schema")]
218impl KeyValueJsonSchema for BidLabels {
219 const JSON_SCHEMA_KV_NAME: Option<&'static str> = Some("PublicKeyAndBid");
220 const JSON_SCHEMA_KV_DESCRIPTION: Option<&'static str> =
221 Some("A bid associated with the given public key.");
222 const JSON_SCHEMA_KEY_DESCRIPTION: Option<&'static str> = Some("The public key of the bidder.");
223 const JSON_SCHEMA_VALUE_DESCRIPTION: Option<&'static str> = Some("The bid details.");
224}