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
// 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)]

#[cfg(test)]
mod mock;

#[cfg(test)]
mod tests;

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

pub mod weights;

pub use pallet::*;
pub use weights::WeightInfo;

use frame_support::pallet_prelude::Weight;
use frame_support::pallet_prelude::*;
use sp_membership::traits::*;
use sp_membership::MembershipData;
use sp_runtime::traits::Zero;
use sp_std::collections::btree_map::BTreeMap;
use sp_std::prelude::*;

#[cfg(feature = "runtime-benchmarks")]
pub trait SetupBenchmark<IdtyId, AccountId> {
    fn force_valid_distance_status(idty_index: &IdtyId);
    fn add_cert(_issuer: &IdtyId, _receiver: &IdtyId);
}

#[cfg(feature = "runtime-benchmarks")]
impl<IdtyId, AccountId> SetupBenchmark<IdtyId, AccountId> for () {
    fn force_valid_distance_status(_idty_id: &IdtyId) {}

    fn add_cert(_issuer: &IdtyId, _receiver: &IdtyId) {}
}

#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo)]
pub enum MembershipRemovalReason {
    // reach end of life
    Expired,
    // was explicitly revoked
    Revoked,
    // received certs count passed below threshold
    NotEnoughCerts,
    // system reasons (consumers, authority members, or root)
    System,
}

#[frame_support::pallet]
pub mod pallet {
    use super::*;
    use frame_support::traits::StorageVersion;
    use frame_system::pallet_prelude::*;
    use sp_runtime::traits::Convert;

    /// 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 {
        /// Ask the runtime whether the identity can perform membership operations
        type CheckMembershipOpAllowed: CheckMembershipOpAllowed<Self::IdtyId>;
        /// Something that identifies an identity
        type IdtyId: Copy + MaybeSerializeDeserialize + Parameter + Ord;
        /// Something that gives the IdtyId of an AccountId
        type IdtyIdOf: Convert<Self::AccountId, Option<Self::IdtyId>>;
        /// Something that gives the AccountId of an IdtyId
        type AccountIdOf: Convert<Self::IdtyId, Option<Self::AccountId>>;
        /// Maximum life span of a single membership (in number of blocks)
        // (this could be renamed "validity" or "duration")
        #[pallet::constant]
        type MembershipPeriod: Get<BlockNumberFor<Self>>;
        /// Minimum delay to wait before renewing membership
        // i.e. asking for distance evaluation
        #[pallet::constant]
        type MembershipRenewalPeriod: Get<BlockNumberFor<Self>>;
        /// On new and renew membership handler.
        type OnNewMembership: OnNewMembership<Self::IdtyId>;
        /// On revoked and removed membership handler.
        type OnRemoveMembership: OnRemoveMembership<Self::IdtyId>;
        /// Because this pallet emits events, it depends on the runtime's definition of an event.
        type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
        type WeightInfo: WeightInfo;
        #[cfg(feature = "runtime-benchmarks")]
        type BenchmarkSetupHandler: SetupBenchmark<Self::IdtyId, Self::AccountId>;
    }

    // GENESIS STUFF //

    #[pallet::genesis_config]
    pub struct GenesisConfig<T: Config> {
        pub memberships: BTreeMap<T::IdtyId, MembershipData<BlockNumberFor<T>>>,
    }

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

    #[pallet::genesis_build]
    impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
        fn build(&self) {
            for (idty_id, membership_data) in &self.memberships {
                MembershipsExpireOn::<T>::append(membership_data.expire_on, idty_id);
                Membership::<T>::insert(idty_id, membership_data);
            }
        }
    }

    // STORAGE //

    /// maps identity id to membership data
    // (expiration block for instance)
    #[pallet::storage]
    #[pallet::getter(fn membership)]
    pub type Membership<T: Config> = CountedStorageMap<
        _,
        Twox64Concat,
        T::IdtyId,
        MembershipData<BlockNumberFor<T>>,
        OptionQuery,
    >;

    /// maps block number to the list of identity id set to expire at this block
    #[pallet::storage]
    #[pallet::getter(fn memberships_expire_on)]
    pub type MembershipsExpireOn<T: Config> =
        StorageMap<_, Twox64Concat, BlockNumberFor<T>, Vec<T::IdtyId>, ValueQuery>;

    // EVENTS //

    #[pallet::event]
    #[pallet::generate_deposit(pub(super) fn deposit_event)]
    pub enum Event<T: Config> {
        /// A membership was added.
        MembershipAdded {
            member: T::IdtyId,
            expire_on: BlockNumberFor<T>,
        },
        /// A membership was renewed.
        MembershipRenewed {
            member: T::IdtyId,
            expire_on: BlockNumberFor<T>,
        },
        /// A membership was removed.
        MembershipRemoved {
            member: T::IdtyId,
            reason: MembershipRemovalReason,
        },
    }

    // ERRORS//

    #[pallet::error]
    pub enum Error<T> {
        /// Membership not found, can not renew.
        MembershipNotFound,
        /// Already member, can not add membership.
        AlreadyMember,
    }

    // HOOKS //

    #[pallet::hooks]
    impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
        fn on_initialize(n: BlockNumberFor<T>) -> Weight {
            if n > BlockNumberFor::<T>::zero() {
                T::WeightInfo::on_initialize().saturating_add(Self::expire_memberships(n))
            } else {
                T::WeightInfo::on_initialize()
            }
        }
    }

    // // CALLS //
    // #[pallet::call]
    // impl<T: Config> Pallet<T> {
    //     // no calls for membership pallet
    // }

    // INTERNAL FUNCTIONS //
    impl<T: Config> Pallet<T> {
        /// unschedule membership expiry
        fn unschedule_membership_expiry(idty_id: T::IdtyId, block_number: BlockNumberFor<T>) {
            let mut scheduled = MembershipsExpireOn::<T>::get(block_number);

            if let Some(pos) = scheduled.iter().position(|x| *x == idty_id) {
                scheduled.swap_remove(pos);
                MembershipsExpireOn::<T>::set(block_number, scheduled);
            }
        }

        /// schedule membership expiry
        fn insert_membership_and_schedule_expiry(idty_id: T::IdtyId) -> BlockNumberFor<T> {
            let block_number = frame_system::pallet::Pallet::<T>::block_number();
            let expire_on = block_number + T::MembershipPeriod::get();

            Membership::<T>::insert(idty_id, MembershipData { expire_on });
            MembershipsExpireOn::<T>::append(expire_on, idty_id);
            expire_on
        }

        /// check that membership can be claimed
        pub fn check_add_membership(idty_id: T::IdtyId) -> Result<(), DispatchError> {
            // no-op is error
            ensure!(
                Membership::<T>::get(idty_id).is_none(),
                Error::<T>::AlreadyMember
            );

            // enough certifications and distance rule for example
            T::CheckMembershipOpAllowed::check_add_membership(idty_id)?;
            Ok(())
        }

        /// check that membership can be renewed
        pub fn check_renew_membership(
            idty_id: T::IdtyId,
        ) -> Result<MembershipData<BlockNumberFor<T>>, DispatchError> {
            let membership_data =
                Membership::<T>::get(idty_id).ok_or(Error::<T>::MembershipNotFound)?;

            // enough certifications
            T::CheckMembershipOpAllowed::check_renew_membership(idty_id)?;
            Ok(membership_data)
        }

        /// try claim membership
        pub fn try_add_membership(idty_id: T::IdtyId) -> Result<(), DispatchError> {
            Self::check_add_membership(idty_id)?;
            Self::do_add_membership(idty_id);
            Ok(())
        }

        /// try renew membership
        pub fn try_renew_membership(idty_id: T::IdtyId) -> Result<(), DispatchError> {
            let membership_data = Self::check_renew_membership(idty_id)?;
            Self::do_renew_membership(idty_id, membership_data);
            Ok(())
        }

        /// perform membership addition
        fn do_add_membership(idty_id: T::IdtyId) {
            let expire_on = Self::insert_membership_and_schedule_expiry(idty_id);
            Self::deposit_event(Event::MembershipAdded {
                member: idty_id,
                expire_on,
            });
            T::OnNewMembership::on_created(&idty_id);
        }

        /// perform membership renewal
        fn do_renew_membership(
            idty_id: T::IdtyId,
            membership_data: MembershipData<BlockNumberFor<T>>,
        ) {
            Self::unschedule_membership_expiry(idty_id, membership_data.expire_on);
            let expire_on = Self::insert_membership_and_schedule_expiry(idty_id);
            Self::deposit_event(Event::MembershipRenewed {
                member: idty_id,
                expire_on,
            });
            T::OnNewMembership::on_renewed(&idty_id);
        }

        /// perform membership removal
        pub fn do_remove_membership(idty_id: T::IdtyId, reason: MembershipRemovalReason) -> Weight {
            let mut weight = T::DbWeight::get().reads_writes(2, 3);
            if let Some(membership_data) = Membership::<T>::take(idty_id) {
                Self::unschedule_membership_expiry(idty_id, membership_data.expire_on);
                Self::deposit_event(Event::MembershipRemoved {
                    member: idty_id,
                    reason,
                });
                weight += T::OnRemoveMembership::on_removed(&idty_id);
            }
            weight
        }

        /// perform the membership expiry scheduled at given block
        pub fn expire_memberships(block_number: BlockNumberFor<T>) -> Weight {
            let mut expired_idty_count = 0u32;

            for idty_id in MembershipsExpireOn::<T>::take(block_number) {
                // remove membership (take)
                Self::do_remove_membership(idty_id, MembershipRemovalReason::Expired);
                expired_idty_count += 1;
            }
            T::WeightInfo::expire_memberships(expired_idty_count)
        }

        /// check if identity is member
        pub fn is_member(idty_id: &T::IdtyId) -> bool {
            Membership::<T>::contains_key(idty_id)
        }
    }
}

// implement traits

impl<T: Config> sp_runtime::traits::IsMember<T::IdtyId> for Pallet<T> {
    fn is_member(idty_id: &T::IdtyId) -> bool {
        Self::is_member(idty_id)
    }
}

impl<T: Config> MembersCount for Pallet<T> {
    fn members_count() -> u32 {
        Membership::<T>::count()
    }
}