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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
// Copyright 2021 Axiom-Team
//
// This file is part of Duniter-v2S.
//
// Duniter-v2S is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, version 3 of the License.
//
// Duniter-v2S is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with Duniter-v2S. If not, see <https://www.gnu.org/licenses/>.

#![cfg_attr(not(feature = "std"), no_std)]
#![allow(clippy::type_complexity)]

pub mod impls;
pub mod traits;
mod types;
pub mod weights;

#[cfg(test)]
mod mock;

#[cfg(test)]
mod tests;

#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;

//pub use impls::*;
pub use pallet::*;
pub use sp_staking::SessionIndex;
pub use traits::*;
pub use types::*;
pub use weights::WeightInfo;

use frame_support::traits::Get;
use sp_runtime::traits::Convert;
use sp_std::prelude::*;

#[frame_support::pallet]
pub mod pallet {
    use super::*;
    use frame_support::pallet_prelude::*;
    use frame_support::traits::ValidatorRegistration;
    use frame_support::traits::{StorageVersion, UnfilteredDispatchable};
    use frame_system::pallet_prelude::*;
    use sp_runtime::traits::{Convert, IsMember};
    use sp_std::collections::btree_map::BTreeMap;
    use sp_std::vec;

    /// The current storage version.
    const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);

    #[pallet::pallet]
    #[pallet::storage_version(STORAGE_VERSION)]
    #[pallet::without_storage_info]
    pub struct Pallet<T>(_);

    // CONFIG //

    #[pallet::config]
    pub trait Config:
        frame_system::Config + pallet_session::Config + pallet_session::historical::Config
    {
        type IsMember: IsMember<Self::MemberId>;
        type OnNewSession: OnNewSession;
        type OnOutgoingMember: OnOutgoingMember<Self::MemberId>;
        type OnIncomingMember: OnIncomingMember<Self::MemberId>;
        /// Max number of authorities allowed
        #[pallet::constant]
        type MaxAuthorities: Get<u32>;
        type MemberId: Copy + Ord + MaybeSerializeDeserialize + Parameter;
        type MemberIdOf: Convert<Self::AccountId, Option<Self::MemberId>>;
        type RemoveMemberOrigin: EnsureOrigin<Self::RuntimeOrigin>;
        type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
        type WeightInfo: WeightInfo;
    }

    // GENESIS STUFF //

    #[pallet::genesis_config]
    pub struct GenesisConfig<T: Config> {
        pub initial_authorities: BTreeMap<T::MemberId, (T::AccountId, bool)>,
    }

    impl<T: Config> Default for GenesisConfig<T> {
        fn default() -> Self {
            Self {
                initial_authorities: BTreeMap::new(),
            }
        }
    }

    #[pallet::genesis_build]
    impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
        fn build(&self) {
            for (member_id, (account_id, _is_online)) in &self.initial_authorities {
                Members::<T>::insert(member_id, MemberData::new_genesis(account_id.to_owned()));
            }
            let mut members_ids = self
                .initial_authorities
                .iter()
                .filter_map(
                    |(member_id, (_account_id, is_online))| {
                        if *is_online {
                            Some(*member_id)
                        } else {
                            None
                        }
                    },
                )
                .collect::<Vec<T::MemberId>>();
            members_ids.sort();

            OnlineAuthorities::<T>::put(members_ids.clone());
        }
    }

    // STORAGE //

    /// list incoming authorities
    #[pallet::storage]
    #[pallet::getter(fn incoming)]
    pub type IncomingAuthorities<T: Config> = StorageValue<_, Vec<T::MemberId>, ValueQuery>;

    /// list online authorities
    #[pallet::storage]
    #[pallet::getter(fn online)]
    pub type OnlineAuthorities<T: Config> = StorageValue<_, Vec<T::MemberId>, ValueQuery>;

    /// list outgoing authorities
    #[pallet::storage]
    #[pallet::getter(fn outgoing)]
    pub type OutgoingAuthorities<T: Config> = StorageValue<_, Vec<T::MemberId>, ValueQuery>;

    /// maps member id to member data
    #[pallet::storage]
    #[pallet::getter(fn member)]
    pub type Members<T: Config> =
        StorageMap<_, Twox64Concat, T::MemberId, MemberData<T::AccountId>, OptionQuery>;

    // Blacklist.
    #[pallet::storage]
    #[pallet::getter(fn blacklist)]
    pub type Blacklist<T: Config> = StorageValue<_, Vec<T::MemberId>, ValueQuery>;

    // HOOKS //

    // EVENTS //

    #[pallet::event]
    #[pallet::generate_deposit(pub(super) fn deposit_event)]
    pub enum Event<T: Config> {
        /// List of members scheduled to join the set of authorities in the next session.
        IncomingAuthorities { members: Vec<T::MemberId> },
        /// List of members leaving the set of authorities in the next session.
        OutgoingAuthorities { members: Vec<T::MemberId> },
        /// A member will leave the set of authorities in 2 sessions.
        MemberGoOffline { member: T::MemberId },
        /// A member will join the set of authorities in 2 sessions.
        MemberGoOnline { member: T::MemberId },
        /// A member, who no longer has authority rights, will be removed from the authority set in 2 sessions.
        MemberRemoved { member: T::MemberId },
        /// A member has been removed from the blacklist.
        MemberRemovedFromBlacklist { member: T::MemberId },
        /// A member has been blacklisted.
        MemberAddedToBlacklist { member: T::MemberId },
    }

    // ERRORS //

    #[pallet::error]
    pub enum Error<T> {
        /// Member already incoming.
        AlreadyIncoming,
        /// Member already online.
        AlreadyOnline,
        /// Member already outgoing.
        AlreadyOutgoing,
        /// Owner key is invalid as a member.
        MemberIdNotFound,
        /// Member is blacklisted.
        MemberBlacklisted,
        /// Member is not blacklisted.
        MemberNotBlacklisted,
        /// Member not found.
        MemberNotFound,
        /// Neither online nor scheduled.
        NotOnlineNorIncoming,
        /// Not member.
        NotMember,
        /// Session keys not provided.
        SessionKeysNotProvided,
        /// Too many authorities.
        TooManyAuthorities,
    }

    // CALLS //

    #[pallet::call]
    impl<T: Config> Pallet<T> {
        /// ask to leave the set of validators two sessions after
        #[pallet::call_index(0)]
        #[pallet::weight(<T as pallet::Config>::WeightInfo::go_offline())]
        pub fn go_offline(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
            // Verification phase //
            let who = ensure_signed(origin)?;
            let member_id = Self::verify_ownership_and_membership(&who)?;

            if !Members::<T>::contains_key(member_id) {
                return Err(Error::<T>::MemberNotFound.into());
            }
            if Self::is_outgoing(member_id) {
                return Err(Error::<T>::AlreadyOutgoing.into());
            }
            let is_incoming = Self::is_incoming(member_id);
            if !is_incoming && !Self::is_online(member_id) {
                return Err(Error::<T>::NotOnlineNorIncoming.into());
            }

            // Apply phase //
            if is_incoming {
                Self::remove_in(member_id);
            } else {
                Self::insert_out(member_id);
            }

            Ok(().into())
        }

        /// ask to join the set of validators two sessions after
        #[pallet::call_index(1)]
        #[pallet::weight(<T as pallet::Config>::WeightInfo::go_online())]
        pub fn go_online(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
            // Verification phase //
            let who = ensure_signed(origin)?;
            let member_id = Self::verify_ownership_and_membership(&who)?;

            if Self::is_blacklisted(member_id) {
                return Err(Error::<T>::MemberBlacklisted.into());
            }
            if !Members::<T>::contains_key(member_id) {
                return Err(Error::<T>::MemberNotFound.into());
            }
            let validator_id = T::ValidatorIdOf::convert(who)
                .ok_or(pallet_session::Error::<T>::NoAssociatedValidatorId)?;
            if !pallet_session::Pallet::<T>::is_registered(&validator_id) {
                return Err(Error::<T>::SessionKeysNotProvided.into());
            }

            if Self::is_incoming(member_id) {
                return Err(Error::<T>::AlreadyIncoming.into());
            }
            let is_outgoing = Self::is_outgoing(member_id);
            if Self::is_online(member_id) && !is_outgoing {
                return Err(Error::<T>::AlreadyOnline.into());
            }
            if Self::authorities_counter() >= T::MaxAuthorities::get() {
                return Err(Error::<T>::TooManyAuthorities.into());
            }

            // Apply phase //
            if is_outgoing {
                Self::remove_out(member_id);
            } else {
                Self::insert_in(member_id);
            }

            Ok(().into())
        }

        /// declare new session keys to replace current ones
        #[pallet::call_index(2)]
        #[pallet::weight(<T as pallet::Config>::WeightInfo::set_session_keys())]
        pub fn set_session_keys(origin: OriginFor<T>, keys: T::Keys) -> DispatchResultWithPostInfo {
            let who = ensure_signed(origin.clone())?;
            let member_id = Self::verify_ownership_and_membership(&who)?;

            let _post_info = pallet_session::Call::<T>::set_keys {
                keys,
                proof: vec![],
            }
            .dispatch_bypass_filter(origin)?;

            Members::<T>::insert(member_id, MemberData { owner_key: who });

            Ok(().into())
        }

        /// remove an identity from the set of authorities
        #[pallet::call_index(3)]
        #[pallet::weight(<T as pallet::Config>::WeightInfo::remove_member())]
        pub fn remove_member(
            origin: OriginFor<T>,
            member_id: T::MemberId,
        ) -> DispatchResultWithPostInfo {
            T::RemoveMemberOrigin::ensure_origin(origin)?;

            let member_data = Members::<T>::get(member_id).ok_or(Error::<T>::MemberNotFound)?;
            Self::do_remove_member(member_id, member_data.owner_key);

            Ok(().into())
        }

        #[pallet::call_index(4)]
        #[pallet::weight(<T as pallet::Config>::WeightInfo::remove_member_from_blacklist())]
        /// remove an identity from the blacklist
        pub fn remove_member_from_blacklist(
            origin: OriginFor<T>,
            member_id: T::MemberId,
        ) -> DispatchResultWithPostInfo {
            T::RemoveMemberOrigin::ensure_origin(origin)?;
            Blacklist::<T>::mutate(|members_ids| {
                if let Ok(index) = members_ids.binary_search(&member_id) {
                    members_ids.remove(index);
                    Self::deposit_event(Event::MemberRemovedFromBlacklist { member: member_id });
                    Ok(().into())
                } else {
                    Err(Error::<T>::MemberNotBlacklisted.into())
                }
            })
        }
    }

    // PUBLIC FUNCTIONS //

    impl<T: Config> Pallet<T> {
        // Should be transactional (if an error occurs, storage should not be modified at all)
        //  Execute the annotated function in a new storage transaction.
        //  The return type of the annotated function must be `Result`. All changes to storage performed
        //  by the annotated function are discarded if it returns `Err`, or committed if `Ok`.
        #[frame_support::transactional]
        /// change owner key of an authority member
        pub fn change_owner_key(
            member_id: T::MemberId,
            new_owner_key: T::AccountId,
        ) -> DispatchResultWithPostInfo {
            let old_owner_key = Members::<T>::mutate_exists(member_id, |maybe_member_data| {
                if let Some(ref mut member_data) = maybe_member_data {
                    Ok(core::mem::replace(
                        &mut member_data.owner_key,
                        new_owner_key.clone(),
                    ))
                } else {
                    Err(Error::<T>::MemberNotFound)
                }
            })?;

            let validator_id = T::ValidatorIdOf::convert(old_owner_key.clone())
                .ok_or(pallet_session::Error::<T>::NoAssociatedValidatorId)?;
            let session_keys = pallet_session::NextKeys::<T>::get(validator_id)
                .ok_or(Error::<T>::SessionKeysNotProvided)?;

            // Purge session keys
            let _post_info = pallet_session::Call::<T>::purge_keys {}
                .dispatch_bypass_filter(frame_system::RawOrigin::Signed(old_owner_key).into())?;

            // Set session keys
            let _post_info = pallet_session::Call::<T>::set_keys {
                keys: session_keys,
                proof: vec![],
            }
            .dispatch_bypass_filter(frame_system::RawOrigin::Signed(new_owner_key).into())?;

            Ok(().into())
        }

        pub fn authorities_counter() -> u32 {
            let count = OnlineAuthorities::<T>::get().len() + IncomingAuthorities::<T>::get().len()
                - OutgoingAuthorities::<T>::get().len();
            count as u32
        }
    }

    // INTERNAL FUNCTIONS //

    impl<T: Config> Pallet<T> {
        /// perform authority member removal
        fn do_remove_member(member_id: T::MemberId, owner_key: T::AccountId) {
            if Self::is_online(member_id) {
                // Trigger the member deletion for next session
                Self::insert_out(member_id);
            }

            // remove all member data
            Self::remove_in(member_id);
            Self::remove_online(member_id);
            Members::<T>::remove(member_id);

            // Purge session keys
            if let Err(e) = pallet_session::Pallet::<T>::purge_keys(
                frame_system::Origin::<T>::Signed(owner_key).into(),
            ) {
                log::error!(
                    target: "runtime::authority_members",
                    "Logic error: fail to purge session keys in do_remove_member(): {:?}",
                    e
                );
            }

            // Emit event
            Self::deposit_event(Event::MemberRemoved { member: member_id });
        }

        /// perform incoming authorities insertion
        fn insert_in(member_id: T::MemberId) -> bool {
            let not_already_inserted = IncomingAuthorities::<T>::mutate(|members_ids| {
                if let Err(index) = members_ids.binary_search(&member_id) {
                    members_ids.insert(index, member_id);
                    true
                } else {
                    false
                }
            });
            if not_already_inserted {
                Self::deposit_event(Event::MemberGoOnline { member: member_id });
            }
            not_already_inserted
        }

        /// perform outgoing authority insertion
        pub fn insert_out(member_id: T::MemberId) -> bool {
            let not_already_inserted = OutgoingAuthorities::<T>::mutate(|members_ids| {
                if let Err(index) = members_ids.binary_search(&member_id) {
                    members_ids.insert(index, member_id);
                    true
                } else {
                    false
                }
            });
            if not_already_inserted {
                Self::deposit_event(Event::MemberGoOffline { member: member_id });
            }
            not_already_inserted
        }

        /// check if member is incoming
        fn is_incoming(member_id: T::MemberId) -> bool {
            IncomingAuthorities::<T>::get()
                .binary_search(&member_id)
                .is_ok()
        }

        /// check if member is online
        fn is_online(member_id: T::MemberId) -> bool {
            OnlineAuthorities::<T>::get()
                .binary_search(&member_id)
                .is_ok()
        }

        /// check if member is outgoing
        fn is_outgoing(member_id: T::MemberId) -> bool {
            OutgoingAuthorities::<T>::get()
                .binary_search(&member_id)
                .is_ok()
        }

        /// check if member is blacklisted
        fn is_blacklisted(member_id: T::MemberId) -> bool {
            Blacklist::<T>::get().contains(&member_id)
        }

        /// perform removal from incoming authorities
        fn remove_in(member_id: T::MemberId) {
            IncomingAuthorities::<T>::mutate(|members_ids| {
                if let Ok(index) = members_ids.binary_search(&member_id) {
                    members_ids.remove(index);
                }
            })
        }

        /// perform removal from online authorities
        fn remove_online(member_id: T::MemberId) {
            OnlineAuthorities::<T>::mutate(|members_ids| {
                if let Ok(index) = members_ids.binary_search(&member_id) {
                    members_ids.remove(index);
                }
            });
        }

        /// perform removal from outgoing authorities
        fn remove_out(member_id: T::MemberId) {
            OutgoingAuthorities::<T>::mutate(|members_ids| {
                if let Ok(index) = members_ids.binary_search(&member_id) {
                    members_ids.remove(index);
                }
            });
        }

        /// check that accountid is member
        fn verify_ownership_and_membership(
            who: &T::AccountId,
        ) -> Result<T::MemberId, DispatchError> {
            let member_id =
                T::MemberIdOf::convert(who.clone()).ok_or(Error::<T>::MemberIdNotFound)?;

            if !T::IsMember::is_member(&member_id) {
                return Err(Error::<T>::NotMember.into());
            }

            Ok(member_id)
        }
    }
}

impl<T: Config> pallet_session::SessionManager<T::ValidatorId> for Pallet<T> {
    /// Plan a new session, and optionally provide the new validator set.
    ///
    /// Even if the validator-set is the same as before, if any underlying economic conditions have
    /// changed (i.e. stake-weights), the new validator set must be returned. This is necessary for
    /// consensus engines making use of the session pallet to issue a validator-set change so
    /// misbehavior can be provably associated with the new economic conditions as opposed to the
    /// old. The returned validator set, if any, will not be applied until `new_index`. `new_index`
    /// is strictly greater than from previous call.
    ///
    /// The first session start at index 0.
    ///
    /// `new_session(session)` is guaranteed to be called before `end_session(session-1)`. In other
    /// words, a new session must always be planned before an ongoing one can be finished.
    fn new_session(_session_index: SessionIndex) -> Option<Vec<T::ValidatorId>> {
        let members_ids_to_add = IncomingAuthorities::<T>::take();
        let members_ids_to_del = OutgoingAuthorities::<T>::take();

        if members_ids_to_add.is_empty() && members_ids_to_del.is_empty() {
            // when no change to the set of autorities, return None
            return None;
        }

        for member_id in members_ids_to_add.iter() {
            T::OnIncomingMember::on_incoming_member(*member_id);
            Self::deposit_event(Event::IncomingAuthorities {
                members: members_ids_to_add.clone(),
            });
        }
        for member_id in members_ids_to_del.iter() {
            T::OnOutgoingMember::on_outgoing_member(*member_id);
            Self::deposit_event(Event::OutgoingAuthorities {
                members: members_ids_to_del.clone(),
            });
        }

        // updates the list of OnlineAuthorities and returns the list of their key
        Some(
            OnlineAuthorities::<T>::mutate(|members_ids| {
                for member_id in members_ids_to_del {
                    if let Ok(index) = members_ids.binary_search(&member_id) {
                        members_ids.remove(index);
                    }
                }
                for member_id in members_ids_to_add {
                    if let Err(index) = members_ids.binary_search(&member_id) {
                        members_ids.insert(index, member_id);
                    }
                }
                members_ids.clone()
            })
            .into_iter()
            .filter_map(|member_id| {
                if let Some(member_data) = Members::<T>::get(member_id) {
                    T::ValidatorIdOf::convert(member_data.owner_key)
                } else {
                    None
                }
            })
            .collect(),
        )
    }

    /// Same as `new_session`, but it this should only be called at genesis.
    fn new_session_genesis(_new_index: SessionIndex) -> Option<Vec<T::ValidatorId>> {
        Some(
            OnlineAuthorities::<T>::get()
                .into_iter()
                .filter_map(|member_id| {
                    if let Some(member_data) = Members::<T>::get(member_id) {
                        T::ValidatorIdOf::convert(member_data.owner_key)
                    } else {
                        None
                    }
                })
                .collect(),
        )
    }

    /// End the session.
    ///
    /// Because the session pallet can queue validator set the ending session can be lower than the
    /// last new session index.
    fn end_session(_end_index: SessionIndex) {}

    /// Start an already planned session.
    ///
    /// The session start to be used for validation.
    fn start_session(start_index: SessionIndex) {
        T::OnNewSession::on_new_session(start_index);
    }
}

// see substrate FullIdentification
fn add_full_identification<T: Config>(
    validator_id: T::ValidatorId,
) -> Option<(T::ValidatorId, T::FullIdentification)> {
    use sp_runtime::traits::Convert as _;
    T::FullIdentificationOf::convert(validator_id.clone())
        .map(|full_ident| (validator_id, full_ident))
}

// implement SessionManager with FullIdentification
impl<T: Config> pallet_session::historical::SessionManager<T::ValidatorId, T::FullIdentification>
    for Pallet<T>
{
    fn new_session(
        new_index: SessionIndex,
    ) -> Option<sp_std::vec::Vec<(T::ValidatorId, T::FullIdentification)>> {
        <Self as pallet_session::SessionManager<_>>::new_session(new_index).map(|validators_ids| {
            validators_ids
                .into_iter()
                .filter_map(add_full_identification::<T>)
                .collect()
        })
    }

    fn new_session_genesis(
        new_index: SessionIndex,
    ) -> Option<sp_std::vec::Vec<(T::ValidatorId, T::FullIdentification)>> {
        <Self as pallet_session::SessionManager<_>>::new_session_genesis(new_index).map(
            |validators_ids| {
                validators_ids
                    .into_iter()
                    .filter_map(add_full_identification::<T>)
                    .collect()
            },
        )
    }

    fn start_session(start_index: SessionIndex) {
        <Self as pallet_session::SessionManager<_>>::start_session(start_index)
    }

    fn end_session(end_index: SessionIndex) {
        <Self as pallet_session::SessionManager<_>>::end_session(end_index)
    }
}