authority_selection_inherents/
lib.rs1#![cfg_attr(not(feature = "std"), no_std)]
3#![deny(missing_docs)]
4
5extern crate alloc;
6
7use scale_info::TypeInfo;
8use serde::{Deserialize, Serialize};
9use sidechain_domain::{CandidateKeys, StakePoolPublicKey};
10use sp_core::{ConstU32, Decode, DecodeWithMemTracking, Encode, MaxEncodedLen};
11use sp_runtime::traits::OpaqueKeys;
12use sp_session_validator_management::CommitteeMember as CommitteeMemberT;
13
14mod ariadne_inherent_data_provider;
15mod authority_selection_inputs;
16mod filter_invalid_candidates;
17mod select_authorities;
18
19pub use {
20 ariadne_inherent_data_provider::AriadneInherentDataProvider,
21 authority_selection_inputs::{AriadneParameters, AuthoritySelectionInputs},
22 filter_invalid_candidates::{
23 PermissionedCandidateDataError, RegisterValidatorSignedMessage, RegistrationDataError,
24 StakeError, filter_trustless_candidates_registrations,
25 runtime_decl_for_candidate_validation_api, validate_permissioned_candidate_data,
26 validate_registration_data, validate_stake,
27 },
28 select_authorities::select_authorities,
29};
30#[cfg(feature = "std")]
31pub use {
32 authority_selection_inputs::AuthoritySelectionDataSource,
33 filter_invalid_candidates::CandidateValidationApi,
34};
35
36#[cfg(test)]
37mod runtime_api_mock;
38#[cfg(test)]
39mod tests;
40
41#[cfg(any(test, feature = "mock"))]
42pub mod mock;
43
44#[derive(
45 Serialize,
46 Deserialize,
47 Clone,
48 Encode,
49 Decode,
50 DecodeWithMemTracking,
51 TypeInfo,
52 MaxEncodedLen,
53 Debug,
54 PartialEq,
55 Eq,
56)]
57pub enum CommitteeMember<AuthorityId, AuthorityKeys> {
59 Permissioned {
61 id: AuthorityId,
63 keys: AuthorityKeys,
65 },
66 Registered {
68 id: AuthorityId,
70 keys: AuthorityKeys,
72 stake_pool_pub_key: StakePoolPublicKey,
74 },
75}
76
77impl<AuthorityId, AuthorityKeys> From<(AuthorityId, AuthorityKeys)>
78 for CommitteeMember<AuthorityId, AuthorityKeys>
79{
80 fn from((id, keys): (AuthorityId, AuthorityKeys)) -> Self {
81 Self::Permissioned { id, keys }
82 }
83}
84
85impl<AuthorityId, AuthorityKeys> CommitteeMember<AuthorityId, AuthorityKeys> {
86 pub fn permissioned(id: AuthorityId, keys: AuthorityKeys) -> Self {
88 Self::Permissioned { id, keys }
89 }
90}
91
92impl<AuthorityId: Clone, AuthorityKeys: Clone> CommitteeMemberT
93 for CommitteeMember<AuthorityId, AuthorityKeys>
94{
95 type AuthorityId = AuthorityId;
96 type AuthorityKeys = AuthorityKeys;
97
98 fn authority_id(&self) -> AuthorityId {
99 match self {
100 Self::Permissioned { id, .. } => id.clone(),
101 Self::Registered { id, .. } => id.clone(),
102 }
103 }
104
105 fn authority_keys(&self) -> AuthorityKeys {
106 match self {
107 Self::Permissioned { keys, .. } => keys.clone(),
108 Self::Registered { keys, .. } => keys.clone(),
109 }
110 }
111}
112
113pub trait MaybeFromCandidateKeys: OpaqueKeys + Decode + Sized {
115 fn maybe_from(keys: &CandidateKeys) -> Option<Self> {
117 let required_keys = Self::key_ids();
118
119 let mut encoded_keys = sp_runtime::BoundedVec::<u8, ConstU32<1024>>::new();
120 for key_id in required_keys {
121 let key = keys.0.iter().find(|key| key.id == key_id.0)?;
122 encoded_keys.try_append(&mut key.bytes.clone()).ok()?;
123 }
124 Self::decode(&mut &encoded_keys[..]).ok()
125 }
126}