authority_selection_inherents/
lib.rs

1//! # Partner Chain Committee Selection
2//!
3//! Inherent data provider and selection logic for Partner Chain committee selection.
4//!
5//! ## Overview
6//!
7//! This crate provides an IDP and all types necessary for a Partner Chain to select
8//! block producer committees using data sourced from Cardano smart contracts.
9//!
10//! ## Usage
11//!
12//! ### Prerequisites
13//!
14//! This crate is intended to work with `pallet_session_validator_management`. See
15//! the pallet's documentation for instructions how to add it to you runtime.
16//!
17//! Additionally [AriadneInherentDataProvider] needs access to a data source
18//! implementing [AuthoritySelectionDataSource]. A Db-Sync-based implementation is
19//! provided by the `partner_chains_db_sync_data_sources` crate.
20//!
21//! ### Adding to the node
22//!
23//! #### Implementing runtime API
24//!
25//! Implement the [SessionValidatorManagementApi] for your runtime. Each API method has
26//! a corresponding method in the pallet that should be used for that purpose. Refer to
27//! the demo runtime for an example.
28//!
29//! #### Add the inherent data provider
30//!
31//! Wire the [AriadneInherentDataProvider] into your inherent data provider stack. The same
32//! constructor [AriadneInherentDataProvider::new] should be used for both proposing and
33//! validating blocks. Refer to the demo node implementation for an example of how to wire
34//! it correctly into a node.
35//!
36#![cfg_attr(not(feature = "std"), no_std)]
37#![deny(missing_docs)]
38
39extern crate alloc;
40
41use sidechain_domain::CandidateKeys;
42use sp_core::{ConstU32, Decode};
43use sp_runtime::traits::OpaqueKeys;
44
45mod ariadne_inherent_data_provider;
46mod authority_selection_inputs;
47mod filter_invalid_candidates;
48mod select_authorities;
49
50pub use {
51	ariadne_inherent_data_provider::AriadneInherentDataProvider,
52	authority_selection_inputs::{AriadneParameters, AuthoritySelectionInputs},
53	filter_invalid_candidates::{
54		PermissionedCandidateDataError, RegisterValidatorSignedMessage, RegistrationDataError,
55		StakeError, filter_trustless_candidates_registrations,
56		runtime_decl_for_candidate_validation_api, validate_permissioned_candidate_data,
57		validate_registration_data, validate_stake,
58	},
59	select_authorities::select_authorities,
60};
61#[cfg(feature = "std")]
62pub use {
63	authority_selection_inputs::AuthoritySelectionDataSource,
64	filter_invalid_candidates::CandidateValidationApi,
65};
66
67#[cfg(test)]
68mod runtime_api_mock;
69#[cfg(test)]
70mod tests;
71
72#[cfg(any(test, feature = "mock"))]
73pub mod mock;
74
75/// Trait to try extract implementing type from [CandidateKeys].
76pub trait MaybeFromCandidateKeys: OpaqueKeys + Decode + Sized {
77	/// Depends on `Decode` that is derived by `impl_opaque_keys!`
78	fn maybe_from(keys: &CandidateKeys) -> Option<Self> {
79		let required_keys = Self::key_ids();
80
81		let mut encoded_keys = sp_runtime::BoundedVec::<u8, ConstU32<1024>>::new();
82		for key_id in required_keys {
83			let key = keys.0.iter().find(|key| key.id == key_id.0)?;
84			encoded_keys.try_append(&mut key.bytes.clone()).ok()?;
85		}
86		Self::decode(&mut &encoded_keys[..]).ok()
87	}
88}