authority_selection_inherents/
lib.rs

1//! This crate provides inherents for authority selection.
2#![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)]
57/// Type representing committee members, either permissioned or registered
58pub enum CommitteeMember<AuthorityId, AuthorityKeys> {
59	/// A permissioned candidate
60	Permissioned {
61		/// Authority id of the candidate
62		id: AuthorityId,
63		/// Authority keys of the candidate
64		keys: AuthorityKeys,
65	},
66	/// A registered candidate
67	Registered {
68		/// Authority id of the candidate
69		id: AuthorityId,
70		/// Authority keys of the candidate
71		keys: AuthorityKeys,
72		/// Stake pool pub key of the candidate
73		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	/// Constructs new permissioned candidate
87	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
113/// Trait to try extract implementing type from [CandidateKeys].
114pub trait MaybeFromCandidateKeys: OpaqueKeys + Decode + Sized {
115	/// Depends on `Decode` that is derived by `impl_opaque_keys!`
116	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}