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

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

pub mod traits;
mod types;
pub mod weights;

#[cfg(test)]
mod mock;

#[cfg(test)]
mod tests;

use frame_system::pallet_prelude::BlockNumberFor;
pub use pallet::*;
pub use types::*;
pub use weights::WeightInfo;

use crate::traits::*;
use codec::Codec;
use duniter_primitives::Idty;
use frame_support::pallet_prelude::*;
use frame_support::traits::StorageVersion;
use sp_runtime::traits::AtLeast32BitUnsigned;
use sp_std::{fmt::Debug, vec::Vec};

#[frame_support::pallet]
pub mod pallet {
    use super::*;
    use frame_system::pallet_prelude::*;
    use sp_runtime::traits::Saturating;
    use sp_std::collections::btree_map::BTreeMap;

    /// 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>(PhantomData<T>);

    #[pallet::config]
    pub trait Config: frame_system::Config {
        #[pallet::constant]
        /// Minimum duration between two certifications issued by the same issuer.
        type CertPeriod: Get<BlockNumberFor<Self>>;
        /// A short identity index.
        type IdtyIndex: Parameter
            + Member
            + AtLeast32BitUnsigned
            + Codec
            + Default
            + Copy
            + MaybeSerializeDeserialize
            + Debug
            + MaxEncodedLen;
        /// Something that gives the IdtyId of an AccountId and reverse
        type IdtyAttr: duniter_primitives::Idty<Self::IdtyIndex, Self::AccountId>;
        /// Provide method to check that cert is allowed.
        type CheckCertAllowed: CheckCertAllowed<Self::IdtyIndex>;
        #[pallet::constant]
        /// Maximum number of active certifications by issuer.
        type MaxByIssuer: Get<u32>;
        /// Minimum number of certifications received to be allowed to issue a certification.
        #[pallet::constant]
        type MinReceivedCertToBeAbleToIssueCert: Get<u32>;
        /// Handler for NewCert event.
        type OnNewcert: OnNewcert<Self::IdtyIndex>;
        /// Handler for Removed event.
        type OnRemovedCert: OnRemovedCert<Self::IdtyIndex>;
        /// 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 representing the weight of this pallet.
        type WeightInfo: WeightInfo;
        #[pallet::constant]
        /// Duration of validity of a certification.
        type ValidityPeriod: Get<BlockNumberFor<Self>>;
    }

    // GENESIS STUFF //

    #[pallet::genesis_config]
    #[allow(clippy::type_complexity)]
    pub struct GenesisConfig<T: Config> {
        pub apply_cert_period_at_genesis: bool,
        pub certs_by_receiver:
            BTreeMap<T::IdtyIndex, BTreeMap<T::IdtyIndex, Option<BlockNumberFor<T>>>>,
    }

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

    #[pallet::genesis_build]
    impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
        fn build(&self) {
            let mut cert_meta_by_issuer =
                BTreeMap::<T::IdtyIndex, IdtyCertMeta<BlockNumberFor<T>>>::new();
            let mut certs_removable_on =
                BTreeMap::<BlockNumberFor<T>, Vec<(T::IdtyIndex, T::IdtyIndex)>>::new();
            for (receiver, issuers) in &self.certs_by_receiver {
                // Forbid self-cert
                assert!(
                    !issuers.contains_key(receiver),
                    "Identity cannot certify it-self."
                );

                // We should insert cert_meta for receivers that have not issued any cert.
                cert_meta_by_issuer
                    .entry(*receiver)
                    .or_insert(IdtyCertMeta {
                        issued_count: 0,
                        next_issuable_on: sp_runtime::traits::Zero::zero(),
                        received_count: 0,
                    })
                    .received_count = issuers.len() as u32;

                let mut issuers_: Vec<_> = Vec::with_capacity(issuers.len());
                for (issuer, maybe_removable_on) in issuers {
                    // Count issued certs
                    cert_meta_by_issuer
                        .entry(*issuer)
                        .or_insert(IdtyCertMeta {
                            issued_count: 0,
                            next_issuable_on: sp_runtime::traits::Zero::zero(),
                            received_count: 0,
                        })
                        .issued_count += 1;

                    // Compute and store removable_on
                    let removable_on = maybe_removable_on.unwrap_or_else(T::ValidityPeriod::get);
                    issuers_.push((*issuer, removable_on));

                    // Prepare CertsRemovableOn
                    certs_removable_on
                        .entry(removable_on)
                        .or_default()
                        .push((*issuer, *receiver));

                    if self.apply_cert_period_at_genesis {
                        let issuer_next_issuable_on = removable_on
                            .saturating_add(T::CertPeriod::get())
                            .saturating_sub(T::ValidityPeriod::get());
                        if let Some(cert_meta) = cert_meta_by_issuer.get_mut(issuer) {
                            if cert_meta.next_issuable_on < issuer_next_issuable_on {
                                cert_meta.next_issuable_on = issuer_next_issuable_on;
                            }
                        }
                    }
                }

                // Write CertsByReceiver
                issuers_.sort();
                CertsByReceiver::<T>::insert(receiver, issuers_);
            }

            // Write StorageIdtyCertMeta
            for (issuer, cert_meta) in cert_meta_by_issuer {
                assert!(
                    !cert_meta.issued_count >= T::MaxByIssuer::get(),
                    "Identity n°{:?} exceed MaxByIssuer.",
                    issuer
                );
                assert!(
                    !cert_meta.received_count >= T::MinReceivedCertToBeAbleToIssueCert::get(),
                    "Identity n°{:?} not respect MinReceivedCertToBeAbleToIssueCert.",
                    issuer
                );
                StorageIdtyCertMeta::<T>::insert(issuer, cert_meta);
            }
            // Write storage CertsRemovableOn
            for (removable_on, certs) in certs_removable_on {
                CertsRemovableOn::<T>::insert(removable_on, certs);
            }
        }
    }

    // STORAGE //

    /// Certifications metada by issuer.
    #[pallet::storage]
    #[pallet::getter(fn idty_cert_meta)]
    pub type StorageIdtyCertMeta<T: Config> =
        StorageMap<_, Twox64Concat, T::IdtyIndex, IdtyCertMeta<BlockNumberFor<T>>, ValueQuery>;

    /// Certifications by receiver.
    #[pallet::storage]
    #[pallet::getter(fn certs_by_receiver)]
    pub type CertsByReceiver<T: Config> = StorageMap<
        _,
        Twox64Concat,
        T::IdtyIndex,
        Vec<(T::IdtyIndex, BlockNumberFor<T>)>,
        ValueQuery,
    >;

    /// Certifications removable on.
    #[pallet::storage]
    #[pallet::getter(fn certs_removable_on)]
    pub type CertsRemovableOn<T: Config> = StorageMap<
        _,
        Twox64Concat,
        BlockNumberFor<T>,
        Vec<(T::IdtyIndex, T::IdtyIndex)>,
        OptionQuery,
    >;

    // EVENTS //

    #[pallet::event]
    #[pallet::generate_deposit(pub(super) fn deposit_event)]
    pub enum Event<T: Config> {
        /// A new certification was added.
        CertAdded {
            issuer: T::IdtyIndex,
            receiver: T::IdtyIndex,
        },
        /// A certification was removed.
        CertRemoved {
            issuer: T::IdtyIndex,
            receiver: T::IdtyIndex,
            expiration: bool,
        },
        /// A certification was renewed.
        CertRenewed {
            issuer: T::IdtyIndex,
            receiver: T::IdtyIndex,
        },
    }

    // ERRORS //

    #[pallet::error]
    pub enum Error<T> {
        /// Issuer of a certification must have an identity
        OriginMustHaveAnIdentity,
        /// Identity cannot certify itself.
        CannotCertifySelf,
        /// Identity has already issued the maximum number of certifications.
        IssuedTooManyCert,
        /// Insufficient certifications received.
        NotEnoughCertReceived,
        /// Identity has issued a certification too recently.
        NotRespectCertPeriod,
        /// Can not add an already-existing cert
        CertAlreadyExists,
        /// Can not renew a non-existing cert
        CertDoesNotExist,
    }

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

    // CALLS //

    #[pallet::call]
    impl<T: Config> Pallet<T> {
        /// Add a new certification.
        #[pallet::call_index(0)]
        #[pallet::weight(T::WeightInfo::add_cert())]
        pub fn add_cert(
            origin: OriginFor<T>,
            receiver: T::IdtyIndex,
        ) -> DispatchResultWithPostInfo {
            let issuer = Self::origin_to_index(origin)?;
            let block_number = frame_system::pallet::Pallet::<T>::block_number();
            Self::check_add_cert(issuer, receiver, block_number)?;
            Self::try_add_cert(block_number, issuer, receiver)?;
            Ok(().into())
        }

        /// Renew an existing certification.
        #[pallet::call_index(3)]
        #[pallet::weight(T::WeightInfo::renew_cert())]
        pub fn renew_cert(
            origin: OriginFor<T>,
            receiver: T::IdtyIndex,
        ) -> DispatchResultWithPostInfo {
            let issuer = Self::origin_to_index(origin)?;
            let block_number = frame_system::pallet::Pallet::<T>::block_number();
            Self::check_renew_cert(issuer, receiver, block_number)?;
            Self::try_renew_cert(block_number, issuer, receiver)?;
            Ok(().into())
        }

        /// remove a certification (only root)
        #[pallet::call_index(1)]
        #[pallet::weight(T::WeightInfo::del_cert())]
        pub fn del_cert(
            origin: OriginFor<T>,
            issuer: T::IdtyIndex,
            receiver: T::IdtyIndex,
        ) -> DispatchResultWithPostInfo {
            ensure_root(origin)?;
            Self::do_remove_cert(issuer, receiver, None);
            Ok(().into())
        }

        /// remove all certifications received by an identity (only root)
        #[pallet::call_index(2)]
        #[pallet::weight(T::WeightInfo::remove_all_certs_received_by(CertsByReceiver::<T>::get(idty_index).len() as u32))]
        pub fn remove_all_certs_received_by(
            origin: OriginFor<T>,
            idty_index: T::IdtyIndex,
        ) -> DispatchResultWithPostInfo {
            ensure_root(origin)?;
            let _ = Self::do_remove_all_certs_received_by(idty_index);
            Ok(().into())
        }
    }

    // INTERNAL FUNCTIONS //

    impl<T: Config> Pallet<T> {
        pub fn do_remove_all_certs_received_by(idty_index: T::IdtyIndex) -> Weight {
            let mut weight = T::DbWeight::get().reads_writes(1, 0);
            for (issuer, _) in CertsByReceiver::<T>::get(idty_index) {
                weight = weight.saturating_add(Self::do_remove_cert(issuer, idty_index, None));
            }
            weight
        }

        /// get issuer index from origin
        pub fn origin_to_index(origin: OriginFor<T>) -> Result<T::IdtyIndex, DispatchError> {
            let who = ensure_signed(origin)?;
            T::IdtyAttr::idty_index(who).ok_or(Error::<T>::OriginMustHaveAnIdentity.into())
        }

        /// add a certification without checks
        // this is used on identity creation to add the first certification
        // The weight is approximated on the worst path.
        pub fn do_add_cert_checked(
            issuer: T::IdtyIndex,
            receiver: T::IdtyIndex,
            verify_rules: bool,
        ) -> DispatchResultWithPostInfo {
            let block_number = frame_system::pallet::Pallet::<T>::block_number();

            if verify_rules {
                // only verify internal rules if asked
                Self::check_add_cert_internal(issuer, receiver, block_number)?;
            };

            Self::try_add_cert(block_number, issuer, receiver)?;
            Ok(().into())
        }

        /// perform cert addition if not existing, else CertAlreadyExists
        // must be transactional
        fn try_add_cert(
            block_number: BlockNumberFor<T>,
            issuer: T::IdtyIndex,
            receiver: T::IdtyIndex,
        ) -> DispatchResultWithPostInfo {
            // Write CertsRemovableOn
            let removable_on = block_number + T::ValidityPeriod::get();
            <CertsRemovableOn<T>>::append(removable_on, (issuer, receiver));

            // Write CertsByReceiver
            CertsByReceiver::<T>::mutate_exists(receiver, |maybe_issuers| {
                let issuers = maybe_issuers.get_or_insert(Vec::with_capacity(0));
                // cert does not exist, must be created
                if let Err(index) = issuers.binary_search_by(|(issuer_, _)| issuer_.cmp(&issuer)) {
                    issuers.insert(index, (issuer, removable_on));
                    Ok(())
                } else {
                    // cert exists, must be renewed instead
                    Err(Error::<T>::CertAlreadyExists)
                }
            })?;

            // Write StorageIdtyCertMeta for issuer
            let issuer_issued_count =
                StorageIdtyCertMeta::<T>::mutate(issuer, |issuer_idty_cert_meta| {
                    issuer_idty_cert_meta.issued_count =
                        issuer_idty_cert_meta.issued_count.saturating_add(1);
                    issuer_idty_cert_meta.next_issuable_on = block_number + T::CertPeriod::get();
                    issuer_idty_cert_meta.issued_count
                });

            // Write StorageIdtyCertMeta for receiver
            let receiver_received_count =
                <StorageIdtyCertMeta<T>>::mutate_exists(receiver, |cert_meta_opt| {
                    let cert_meta = cert_meta_opt.get_or_insert(IdtyCertMeta::default());
                    cert_meta.received_count = cert_meta.received_count.saturating_add(1);
                    cert_meta.received_count
                });

            // emit CertAdded event
            Self::deposit_event(Event::CertAdded { issuer, receiver });
            T::OnNewcert::on_new_cert(
                issuer,
                issuer_issued_count,
                receiver,
                receiver_received_count,
            );
            Ok(().into())
        }

        /// perform cert renewal if exisiting, else error with CertDoesNotExist
        // must be used in transactional context
        // (it can fail if certification does not exist after having modified state)
        fn try_renew_cert(
            block_number: BlockNumberFor<T>,
            issuer: T::IdtyIndex,
            receiver: T::IdtyIndex,
        ) -> DispatchResultWithPostInfo {
            // Write CertsRemovableOn
            let removable_on = block_number + T::ValidityPeriod::get();
            <CertsRemovableOn<T>>::append(removable_on, (issuer, receiver));
            // Write CertsByReceiver
            CertsByReceiver::<T>::mutate_exists(receiver, |maybe_issuers| {
                let issuers = maybe_issuers.get_or_insert(Vec::with_capacity(0));
                // cert exists, can be renewed
                if let Ok(index) = issuers.binary_search_by(|(issuer_, _)| issuer_.cmp(&issuer)) {
                    issuers[index] = (issuer, removable_on);
                    Ok(())
                } else {
                    // cert does not exist, must be created
                    Err(Error::<T>::CertDoesNotExist)
                }
            })?;
            // Update next_issuable_on in StorageIdtyCertMeta for issuer
            StorageIdtyCertMeta::<T>::mutate(issuer, |issuer_idty_cert_meta| {
                issuer_idty_cert_meta.next_issuable_on = block_number + T::CertPeriod::get();
            });
            // emit CertRenewed event
            Self::deposit_event(Event::CertRenewed { issuer, receiver });
            Ok(().into())
        }

        /// remove the certifications due to expire on the given block
        // (run at on_initialize step)
        fn prune_certifications(block_number: BlockNumberFor<T>) -> Weight {
            // See on initialize for the overhead weight accounting
            let mut weight = Weight::zero();

            if let Some(certs) = CertsRemovableOn::<T>::take(block_number) {
                for (issuer, receiver) in certs {
                    weight = weight.saturating_add(Self::do_remove_cert(
                        issuer,
                        receiver,
                        Some(block_number),
                    ));
                }
            }
            weight
        }

        /// perform the certification removal
        /// if block number is given only remove cert if still set to expire at this block number
        // this is used because cert expiry unscheduling is not done (#110)
        pub fn do_remove_cert(
            issuer: T::IdtyIndex,
            receiver: T::IdtyIndex,
            block_number_opt: Option<BlockNumberFor<T>>,
        ) -> Weight {
            let mut total_weight = Weight::zero();
            let mut removed = false;
            CertsByReceiver::<T>::mutate_exists(receiver, |issuers_opt| {
                let issuers = issuers_opt.get_or_insert(Vec::with_capacity(0));
                if let Ok(index) = issuers.binary_search_by(|(issuer_, _)| issuer_.cmp(&issuer)) {
                    if let Some(block_number) = block_number_opt {
                        if let Some((_, removable_on)) = issuers.get(index) {
                            // only remove cert if block number is matching
                            if *removable_on == block_number {
                                issuers.remove(index);
                                removed = true;
                            }
                        }
                    } else {
                        issuers.remove(index);
                        removed = true;
                    }
                } else {
                    total_weight += T::WeightInfo::do_remove_cert_noop();
                }
            });
            if removed {
                let issuer_issued_count =
                    <StorageIdtyCertMeta<T>>::mutate_exists(issuer, |cert_meta_opt| {
                        let cert_meta = cert_meta_opt.get_or_insert(IdtyCertMeta::default());
                        cert_meta.issued_count = cert_meta.issued_count.saturating_sub(1);
                        cert_meta.issued_count
                    });
                let receiver_received_count =
                    <StorageIdtyCertMeta<T>>::mutate_exists(receiver, |cert_meta_opt| {
                        let cert_meta = cert_meta_opt.get_or_insert(IdtyCertMeta::default());
                        cert_meta.received_count = cert_meta.received_count.saturating_sub(1);
                        cert_meta.received_count
                    });
                Self::deposit_event(Event::CertRemoved {
                    issuer,
                    receiver,
                    expiration: block_number_opt.is_some(),
                });
                T::OnRemovedCert::on_removed_cert(
                    issuer,
                    issuer_issued_count,
                    receiver,
                    receiver_received_count,
                    block_number_opt.is_some(),
                );
                // Pessimistic overhead estimation based on the worst path of a successfull
                // certificate removal to avoid multiplying benchmarks for every branching,
                // include the OnRemovedCert weight.
                total_weight.saturating_add(T::WeightInfo::do_remove_cert());
            }
            total_weight
        }

        /// check cert allowed
        // 1. no self cert
        // 2. issuer received cert count
        // 3. issuer max emitted cert
        // 4. issuer cert period
        fn check_add_cert_internal(
            issuer: T::IdtyIndex,
            receiver: T::IdtyIndex,
            block_number: BlockNumberFor<T>,
        ) -> DispatchResult {
            // 1. Forbid self cert
            ensure!(issuer != receiver, Error::<T>::CannotCertifySelf);

            // 2. Verify rule MinReceivedCertToBeAbleToIssueCert
            // (this number can differ from the one necessary to be member)
            let issuer_idty_cert_meta = <StorageIdtyCertMeta<T>>::get(issuer);
            ensure!(
                issuer_idty_cert_meta.received_count
                    >= T::MinReceivedCertToBeAbleToIssueCert::get(),
                Error::<T>::NotEnoughCertReceived
            );

            // 3. Verify rule MaxByIssuer
            ensure!(
                issuer_idty_cert_meta.issued_count < T::MaxByIssuer::get(),
                Error::<T>::IssuedTooManyCert
            );

            // 4. Verify rule CertPeriod
            ensure!(
                block_number >= issuer_idty_cert_meta.next_issuable_on,
                Error::<T>::NotRespectCertPeriod
            );

            Ok(())
        }

        /// check cert allowed
        // first internal checks
        // then external checks
        fn check_add_cert(
            issuer: T::IdtyIndex,
            receiver: T::IdtyIndex,
            block_number: BlockNumberFor<T>,
        ) -> DispatchResult {
            // internal checks
            Self::check_add_cert_internal(issuer, receiver, block_number)?;

            // --- then external checks
            // - issuer is member
            // - receiver is confirmed
            // - receiver is not revoked
            T::CheckCertAllowed::check_cert_allowed(issuer, receiver)?;

            Ok(())
        }

        /// check renew cert allowed
        fn check_renew_cert(
            issuer: T::IdtyIndex,
            receiver: T::IdtyIndex,
            block_number: BlockNumberFor<T>,
        ) -> DispatchResult {
            Self::check_add_cert_internal(issuer, receiver, block_number)?;
            T::CheckCertAllowed::check_cert_allowed(issuer, receiver)?;
            Ok(())
        }
    }
}

// implement setting next_issuable_on for certification period
impl<T: Config> SetNextIssuableOn<BlockNumberFor<T>, T::IdtyIndex> for Pallet<T> {
    fn set_next_issuable_on(idty_index: T::IdtyIndex, next_issuable_on: BlockNumberFor<T>) {
        <StorageIdtyCertMeta<T>>::mutate_exists(idty_index, |cert_meta_opt| {
            let cert_meta = cert_meta_opt.get_or_insert(IdtyCertMeta::default());
            cert_meta.next_issuable_on = next_issuable_on;
        });
    }
}