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
// 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/>.

//! # Duniter Web of Trust Pallet
//!
//! Duniter Web of Trust (WoT) lies at the heart of its identity system, representing a significant improvement over PGP Web of Trust. It functions as a dynamic directed graph where nodes are [identities](../identity/) and edges are [certifications](../certification/).
//!
//! ## Instances
//!
//! Duniter WoT consists of two distinct instances:
//!
//! - **Main WoT**: Designed for every human participant in the Duniter network.
//! - **Smith Sub-WoT**: Intended for authorities.
//!
//! ## Rules
//!
//! The Duniter WoT operates under a set of static and dynamic rules that govern membership conditions.
//!
//! ### Static Rules
//!
//! - **Minimum Received Certifications (Min Indegree)**: Specifies the minimum number of certifications an identity must receive to join the WoT.
//! - **Maximum Emitted Certifications (Max Outdegree)**: Limits the maximum number of certifications an identity can issue.
//! - **Distance Criterion**: Governed by the distance pallet, it defines the permissible distance between identities within the WoT graph.
//!
//! ### Dynamic Rules
//!
//! - **Time Interval Between Certifications**: Sets the minimum time interval required between two consecutive certifications issued by the same identity.
//! - **Certification Duration**: Managed by the certification pallet, it determines the validity duration of a certification.
//! - **Membership Renewal**: Regulates the frequency and conditions under which an identity must renew its membership within the WoT.
//!
//! This pallet is responsible for enforcing and validating the rules of the Duniter Web of Trust. It ensures compliance with both static prerequisites for joining and dynamic conditions for ongoing participation.

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

#[cfg(test)]
mod mock;

#[cfg(test)]
mod tests;

pub use pallet::*;

use frame_support::pallet_prelude::*;
use pallet_certification::traits::SetNextIssuableOn;
use pallet_identity::IdtyStatus;
use pallet_membership::MembershipRemovalReason;

type IdtyIndex = u32;

#[frame_support::pallet]
pub mod pallet {
    use super::*;
    use frame_support::traits::StorageVersion;

    /// 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_certification::Config<IdtyIndex = IdtyIndex>
        + pallet_identity::Config<IdtyIndex = IdtyIndex>
        + pallet_membership::Config<IdtyId = IdtyIndex>
    {
        /// The block number from which the first certification can be issued.
        #[pallet::constant]
        type FirstIssuableOn: Get<frame_system::pallet_prelude::BlockNumberFor<Self>>;

        /// The minimum number of certifications required for membership eligibility.
        #[pallet::constant]
        type MinCertForMembership: Get<u32>;

        /// The minimum number of certifications required to create an identity.
        #[pallet::constant]
        type MinCertForCreateIdtyRight: Get<u32>;
    }

    // INTERNAL FUNCTIONS //

    impl<T: Config> Pallet<T> {
        pub(super) fn do_apply_first_issuable_on(idty_index: IdtyIndex) {
            let block_number = frame_system::pallet::Pallet::<T>::block_number();
            pallet_certification::Pallet::<T>::set_next_issuable_on(
                idty_index,
                block_number + T::FirstIssuableOn::get(),
            );
        }
    }

    // ERRORS //

    #[pallet::error]
    pub enum Error<T> {
        /// Insufficient certifications received.
        NotEnoughCerts,
        /// Target status is incompatible with this operation.
        // - Membership can not be added/renewed with this status
        // - Certification can not be added to identity with this status
        TargetStatusInvalid,
        /// Identity creation period not respected.
        IdtyCreationPeriodNotRespected,
        /// Insufficient received certifications to create identity.
        NotEnoughReceivedCertsToCreateIdty,
        /// Maximum number of emitted certifications reached.
        MaxEmittedCertsReached,
        /// Issuer cannot emit a certification because it is not member.
        IssuerNotMember,
        /// Issuer or receiver not found.
        IdtyNotFound,
        /// Membership can only be renewed after an antispam delay.
        MembershipRenewalPeriodNotRespected,
    }
}

/// Implementing identity call allowance check for the pallet.
impl<AccountId, T: Config> pallet_identity::traits::CheckIdtyCallAllowed<T> for Pallet<T>
where
    T: frame_system::Config<AccountId = AccountId> + pallet_membership::Config,
{
    /// Checks if identity creation is allowed.
    /// This implementation checks the following:
    ///
    /// - Whether the identity has the right to create an identity.
    /// - Whether the issuer can emit a certification.
    /// - Whether the issuer respect creation period.
    fn check_create_identity(creator: IdtyIndex) -> Result<(), DispatchError> {
        let cert_meta = pallet_certification::Pallet::<T>::idty_cert_meta(creator);

        // 1. Check that the identity has the right to create an identity
        // Identity can be a member with 5 certifications and still not reach the identity creation threshold, which could be higher (6, 7...)
        ensure!(
            cert_meta.received_count >= T::MinCertForCreateIdtyRight::get(),
            Error::<T>::NotEnoughReceivedCertsToCreateIdty
        );

        // 2. Check that the issuer can emit one more certification (partial check)
        ensure!(
            cert_meta.issued_count < T::MaxByIssuer::get(),
            Error::<T>::MaxEmittedCertsReached
        );

        // 3. Check that the issuer respects certification creation period
        ensure!(
            cert_meta.next_issuable_on <= frame_system::pallet::Pallet::<T>::block_number(),
            Error::<T>::IdtyCreationPeriodNotRespected
        );
        Ok(())
    }
}

/// Implementing certification allowance check for the pallet.
impl<T: Config> pallet_certification::traits::CheckCertAllowed<IdtyIndex> for Pallet<T> {
    /// Checks if certification is allowed.
    /// This implementation checks the following:
    ///
    /// - Whether the issuer has an identity.
    /// - Whether the issuer's identity is a member.
    /// - Whether the receiver has an identity.
    /// - Whether the receiver's identity is confirmed and not revoked.
    fn check_cert_allowed(issuer: IdtyIndex, receiver: IdtyIndex) -> Result<(), DispatchError> {
        // Issuer checks
        // Ensure issuer is a member
        let issuer_data =
            pallet_identity::Pallet::<T>::identity(issuer).ok_or(Error::<T>::IdtyNotFound)?;
        ensure!(
            issuer_data.status == IdtyStatus::Member,
            Error::<T>::IssuerNotMember
        );

        // Receiver checks
        // Ensure receiver identity is confirmed and not revoked
        let receiver_data =
            pallet_identity::Pallet::<T>::identity(receiver).ok_or(Error::<T>::IdtyNotFound)?;
        ensure!(
            receiver_data.status == IdtyStatus::Unvalidated
                || receiver_data.status == IdtyStatus::Member
                || receiver_data.status == IdtyStatus::NotMember,
            Error::<T>::TargetStatusInvalid
        );
        Ok(())
    }
}

/// Implementing membership operation checks for the pallet.
impl<T: Config> sp_membership::traits::CheckMembershipOpAllowed<IdtyIndex> for Pallet<T> {
    /// This implementation checks the following:
    ///
    /// - Whether the identity's status is unvalidated or not a member.
    /// - The count of certifications associated with the identity.
    fn check_add_membership(idty_index: IdtyIndex) -> Result<(), DispatchError> {
        // Check identity status
        let idty_value =
            pallet_identity::Pallet::<T>::identity(idty_index).ok_or(Error::<T>::IdtyNotFound)?;
        ensure!(
            idty_value.status == IdtyStatus::Unvalidated
                || idty_value.status == IdtyStatus::NotMember,
            Error::<T>::TargetStatusInvalid
        );

        // Check certificate count
        check_cert_count::<T>(idty_index)?;
        Ok(())
    }

    /// This implementation checks the following:
    ///
    /// - Whether the identity's status is member.
    ///
    /// Note: There is no need to check certification count since losing certifications makes membership expire.
    /// Membership renewal is only possible when identity is member.
    fn check_renew_membership(idty_index: IdtyIndex) -> Result<(), DispatchError> {
        let idty_value =
            pallet_identity::Pallet::<T>::identity(idty_index).ok_or(Error::<T>::IdtyNotFound)?;
        ensure!(
            idty_value.status == IdtyStatus::Member,
            Error::<T>::TargetStatusInvalid
        );
        Ok(())
    }
}

/// Implementing membership event handling for the pallet.
impl<T: Config> sp_membership::traits::OnNewMembership<IdtyIndex> for Pallet<T>
where
    T: pallet_membership::Config,
{
    /// This implementation notifies the identity pallet when a main membership is acquired.
    /// It is only used on the first membership acquisition.
    fn on_created(idty_index: &IdtyIndex) {
        pallet_identity::Pallet::<T>::membership_added(*idty_index);
    }

    fn on_renewed(_idty_index: &IdtyIndex) {}
}

/// Implementing membership removal event handling for the pallet.
impl<T: Config> sp_membership::traits::OnRemoveMembership<IdtyIndex> for Pallet<T>
where
    T: pallet_membership::Config,
{
    /// This implementation notifies the identity pallet when a main membership is lost.
    fn on_removed(idty_index: &IdtyIndex) -> Weight {
        pallet_identity::Pallet::<T>::membership_removed(*idty_index)
    }
}

/// Implementing the identity event handler for the pallet.
impl<T: Config> pallet_identity::traits::OnNewIdty<T> for Pallet<T> {
    /// This implementation adds a certificate when a new identity is created.
    fn on_created(idty_index: &IdtyIndex, creator: &IdtyIndex) {
        if let Err(e) =
            <pallet_certification::Pallet<T>>::do_add_cert_checked(*creator, *idty_index, true)
        {
            sp_std::if_std! {
                println!("fail to force add cert: {:?}", e)
            }
        }
    }
}

/// Implementing identity removal event handling for the pallet.
impl<T: Config> pallet_identity::traits::OnRemoveIdty<T> for Pallet<T> {
    /// This implementation removes both membership and certificates associated with the identity.
    fn on_removed(idty_index: &IdtyIndex) -> Weight {
        let mut weight = Self::on_revoked(idty_index);
        weight = weight.saturating_add(
            <pallet_certification::Pallet<T>>::do_remove_all_certs_received_by(*idty_index),
        );
        weight
    }

    /// This implementation removes membership only.
    fn on_revoked(idty_index: &IdtyIndex) -> Weight {
        let mut weight = Weight::zero();
        weight = weight.saturating_add(<pallet_membership::Pallet<T>>::do_remove_membership(
            *idty_index,
            MembershipRemovalReason::Revoked,
        ));
        weight
    }
}

/// Implementing the certification event handler for the pallet.
impl<T: Config> pallet_certification::traits::OnNewcert<IdtyIndex> for Pallet<T> {
    /// This implementation checks if the receiver has received enough certificates to be able to issue certificates,
    /// and applies the first issuable if the condition is met.
    fn on_new_cert(
        _issuer: IdtyIndex,
        _issuer_issued_count: u32,
        receiver: IdtyIndex,
        receiver_received_count: u32,
    ) {
        if receiver_received_count == T::MinReceivedCertToBeAbleToIssueCert::get() {
            Self::do_apply_first_issuable_on(receiver);
        }
    }
}

/// Implementing the certification removal event handler for the pallet.
impl<T: Config> pallet_certification::traits::OnRemovedCert<IdtyIndex> for Pallet<T> {
    /// This implementation checks if the receiver has received fewer certificates than required for membership,
    /// and if so, and the receiver is a member, it expires the receiver's membership.
    fn on_removed_cert(
        _issuer: IdtyIndex,
        _issuer_issued_count: u32,
        receiver: IdtyIndex,
        receiver_received_count: u32,
        _expiration: bool,
    ) {
        if receiver_received_count < T::MinCertForMembership::get()
            && pallet_membership::Pallet::<T>::is_member(&receiver)
        {
            // Expire receiver membership
            <pallet_membership::Pallet<T>>::do_remove_membership(
                receiver,
                MembershipRemovalReason::NotEnoughCerts,
            );
        }
    }
}

/// Implementing the valid distance status event handler for the pallet.
impl<T: Config + pallet_distance::Config> pallet_distance::traits::OnValidDistanceStatus<T>
    for Pallet<T>
{
    /// This implementation handles different scenarios based on the identity's status:
    ///
    /// - For `Unconfirmed` or `Revoked` identities, no action is taken.
    /// - For `Unvalidated` or `NotMember` identities, an attempt is made to add membership.
    /// - For `Member` identities, an attempt is made to renew membership.
    fn on_valid_distance_status(idty_index: IdtyIndex) {
        if let Some(identity) = pallet_identity::Identities::<T>::get(idty_index) {
            match identity.status {
                IdtyStatus::Unconfirmed | IdtyStatus::Revoked => {
                    // IdtyStatus::Unconfirmed
                    // distance evaluation request should never happen for unconfirmed identity
                    // IdtyStatus::Revoked
                    // the identity can have been revoked during distance evaluation by the oracle
                }

                IdtyStatus::Unvalidated | IdtyStatus::NotMember => {
                    // IdtyStatus::Unvalidated
                    // normal scenario for first entry
                    // IdtyStatus::NotMember
                    // normal scenario for re-entry
                    // the following can fail if a certification expired during distance evaluation
                    // otherwise it should succeed
                    let _ = pallet_membership::Pallet::<T>::try_add_membership(idty_index);
                    // sp_std::if_std! {
                    //     if let Err(e) = r {
                    //         print!("failed to claim identity when distance status was found ok: ");
                    //         println!("{:?}", idty_index);
                    //         println!("reason: {:?}", e);
                    //     }
                    // }
                }
                IdtyStatus::Member => {
                    // IdtyStatus::Member
                    // normal scenario for renewal
                    // should succeed
                    let _ = pallet_membership::Pallet::<T>::try_renew_membership(idty_index);
                    // sp_std::if_std! {
                    //     if let Err(e) = r {
                    //         print!("failed to renew identity when distance status was found ok: ");
                    //         println!("{:?}", idty_index);
                    //         println!("reason: {:?}", e);
                    //     }
                    // }
                }
            }
        } else {
            // identity was removed before distance status was found
            // so it's ok to do nothing
            sp_std::if_std! {
                println!("identity was removed before distance status was found: {:?}", idty_index);
            }
        }
    }
}

/// Implementing the request distance evaluation check for the pallet.
impl<T: Config + pallet_distance::Config> pallet_distance::traits::CheckRequestDistanceEvaluation<T>
    for Pallet<T>
{
    /// This implementation performs the following checks:
    ///
    /// - Membership renewal anti-spam check: Ensures that membership renewal requests respect the anti-spam period.
    /// - Certificate count check: Ensures that the identity has a sufficient number of certificates.
    fn check_request_distance_evaluation(idty_index: IdtyIndex) -> Result<(), DispatchError> {
        // Check membership renewal anti-spam
        let maybe_membership_data = pallet_membership::Pallet::<T>::membership(idty_index);
        if let Some(membership_data) = maybe_membership_data {
            // If membership data exists, this is for a renewal, apply anti-spam
            ensure!(
                // current_block > expiration block - membership period + renewal period
                membership_data.expire_on
                    + <T as pallet_membership::Config>::MembershipRenewalPeriod::get()
                    < frame_system::Pallet::<T>::block_number()
                        + <T as pallet_membership::Config>::MembershipPeriod::get(),
                Error::<T>::MembershipRenewalPeriodNotRespected
            );
        };
        // Check certificate count
        check_cert_count::<T>(idty_index)?;
        Ok(())
    }
}

/// Checks the certificate count for an identity.
fn check_cert_count<T: Config>(idty_index: IdtyIndex) -> Result<(), DispatchError> {
    let idty_cert_meta = pallet_certification::Pallet::<T>::idty_cert_meta(idty_index);
    ensure!(
        idty_cert_meta.received_count >= T::MinCertForMembership::get(),
        Error::<T>::NotEnoughCerts
    );
    Ok(())
}