1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
use frame_support::{
    sp_runtime::{traits::OpaqueKeys, RuntimeAppPublic},
};
use core_primitives::AuthorityId;
use sp_std::prelude::*;
use sp_std::vec;

use crate::Config;

/// Authorities provider, used only as default value in case of missing this information in our pallet. This can
/// happen for the session after runtime upgraded.
pub trait NextSessionAuthorityProvider<T: Config> {
    fn next_authorities() -> Vec<T::AuthorityId>;
}

impl<T> NextSessionAuthorityProvider<T> for pallet_session::Pallet<T>
    where
        T: Config + pallet_session::Config,
{
    fn next_authorities() -> Vec<T::AuthorityId> {
        let next: Option<Vec<_>> = pallet_session::Pallet::<T>::queued_keys()
            .iter()
            .map(|(_, key)| key.get(AuthorityId::ID))
            .collect();

        next.unwrap_or_else(|| {
            log::error!(target: "pallet_phron", "Missing next session keys");
            vec![]
        })
    }
}