1
// Copyright (C) Moondance Labs Ltd.
2
// This file is part of Tanssi.
3

            
4
// Tanssi is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8

            
9
// Tanssi is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13

            
14
// You should have received a copy of the GNU General Public License
15
// along with Tanssi.  If not, see <http://www.gnu.org/licenses/>
16

            
17
//! # Authority Mapping Pallet
18
//!
19
//! This pallet stores the AuthorityId -> AccountID mapping for two sessions
20
//! In particular it holds the mapping for the current and the past session
21
//! Both are necessary to verify block-authorship with respect to current
22
//! block proposals or blocks that have been proposed in the past-session
23

            
24
#![cfg_attr(not(feature = "std"), no_std)]
25

            
26
pub use pallet::*;
27
use {
28
    frame_support::pallet_prelude::*,
29
    sp_runtime::{
30
        traits::{AtLeast32BitUnsigned, CheckedSub},
31
        RuntimeAppPublic,
32
    },
33
    sp_std::{collections::btree_map::BTreeMap, vec},
34
};
35

            
36
#[cfg(test)]
37
mod mock;
38

            
39
#[cfg(test)]
40
mod tests;
41

            
42
528
#[frame_support::pallet]
43
pub mod pallet {
44
    use super::*;
45

            
46
13620
    #[pallet::pallet]
47
    #[pallet::without_storage_info]
48
    pub struct Pallet<T>(_);
49

            
50
    /// Configure the pallet by specifying the parameters and types on which it depends.
51
    #[pallet::config]
52
    pub trait Config: frame_system::Config {
53
        type SessionIndex: parity_scale_codec::FullCodec + TypeInfo + Copy + AtLeast32BitUnsigned;
54

            
55
        // Sessions after which keys should be removed
56
        #[pallet::constant]
57
        type SessionRemovalBoundary: Get<Self::SessionIndex>;
58

            
59
        type AuthorityId: Member
60
            + Parameter
61
            + Ord
62
            + RuntimeAppPublic
63
            + MaybeSerializeDeserialize
64
            + MaxEncodedLen;
65
    }
66

            
67
9515
    #[pallet::storage]
68
    #[pallet::getter(fn authority_id_mapping)]
69
    pub type AuthorityIdMapping<T: Config> = StorageMap<
70
        _,
71
        Twox64Concat,
72
        T::SessionIndex,
73
        BTreeMap<T::AuthorityId, T::AccountId>,
74
        OptionQuery,
75
    >;
76

            
77
    impl<T: Config> Pallet<T> {
78
1774
        pub fn initializer_on_new_session(
79
1774
            session_index: &T::SessionIndex,
80
1774
            authorities: &[(T::AccountId, T::AuthorityId)],
81
1774
        ) {
82
            // Remove only if the checked sub does not saturate
83
965
            if let Some(session_index_to_remove) =
84
1774
                session_index.checked_sub(&T::SessionRemovalBoundary::get())
85
            {
86
965
                AuthorityIdMapping::<T>::remove(session_index_to_remove)
87
809
            }
88

            
89
1774
            let assignation: BTreeMap<T::AuthorityId, T::AccountId> =
90
6066
                authorities.iter().cloned().map(|(a, b)| (b, a)).collect();
91
1774
            AuthorityIdMapping::<T>::insert(session_index, assignation);
92
1774
        }
93
    }
94
}