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
// Copyright 2021-2023 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 Smith Pallet
//!
//! The Smith pallet in Duniter serves as a bridge between the `identity` and `authority-members` pallets.
//!
//! ## Overview
//!
//! The Smith pallet manages the certification and membership status of Smiths. Smiths are identities that have met certain requirements and play a critical role in the network's operations (block authoring, distance evaluation).
//!
//! ## Key Concepts
//!
//! ### Smith Status
//!
//! The status of an identity within the Smith pallet can be one of the following:
//! - **Invited**: The identity has been invited by a Smith but has not yet accepted the invitation.
//! - **Pending**: The identity has accepted the invitation and is pending to become a full Smith.
//! - **Smith**: The identity has fulfilled the requirements and is a full-fledged Smith, eligible to perform critical network functions.
//! - **Excluded**: The identity has been removed from the Smiths set but its certifications are retained for tracking purposes.
//!
//! ### Certifications
//!
//! Certifications are crucial in determining Smith status:
//! - An identity needs a minimum number of certifications to become a Smith (`MinCertForMembership`).
#![cfg_attr(not(feature = "std"), no_std)]
#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;
mod impls;
pub mod traits;
mod types;
pub mod weights;
#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
use codec::{Codec, Decode, Encode};
use duniter_primitives::Idty;
use frame_support::{
dispatch::DispatchResultWithPostInfo,
ensure,
pallet_prelude::{Get, RuntimeDebug, Weight},
};
use frame_system::{ensure_signed, pallet_prelude::OriginFor};
use scale_info::TypeInfo;
use sp_runtime::traits::{AtLeast32BitUnsigned, IsMember};
use sp_std::{fmt::Debug, prelude::*};
use crate::traits::OnSmithDelete;
pub use crate::weights::WeightInfo;
pub use pallet::*;
use pallet_authority_members::SessionIndex;
pub use types::*;
/// Reasons for the removal of a Smith identity.
#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo)]
pub enum SmithRemovalReason {
/// Membership was lost due to expiration or other reasons.
LostMembership,
/// Smith was offline for too long.
OfflineTooLong,
/// Smith was blacklisted.
Blacklisted,
}
/// Possible statuses of a Smith identity.
#[derive(Encode, Decode, Copy, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo)]
pub enum SmithStatus {
/// The identity has been invited by a Smith but has not accepted yet.
Invited,
/// The identity has accepted to eventually become a Smith.
Pending,
/// The identity has reached the requirements to become a Smith and can now perform Smith operations.
Smith,
/// The identity has been removed from the Smiths set but is kept to track its certifications.
Excluded,
}
#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::{pallet_prelude::*, traits::StorageVersion};
use pallet_authority_members::SessionIndex;
use sp_runtime::traits::{Convert, IsMember};
use sp_std::{collections::btree_map::BTreeMap, vec, vec::Vec};
const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);
#[pallet::pallet]
#[pallet::storage_version(STORAGE_VERSION)]
#[pallet::without_storage_info]
pub struct Pallet<T>(_);
/// The pallet's config trait.
#[pallet::config]
pub trait Config: frame_system::Config {
/// Trait to check if identity is a WoT members.
type IsWoTMember: IsMember<Self::IdtyIndex>;
type OnSmithDelete: traits::OnSmithDelete<Self::IdtyIndex>;
/// The overarching event type for this pallet.
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
/// A short identity index type.
type IdtyIndex: Parameter
+ Member
+ AtLeast32BitUnsigned
+ Codec
+ Default
+ Copy
+ MaybeSerializeDeserialize
+ Debug
+ MaxEncodedLen;
/// Identifier type for an authority-member.
type MemberId: Copy + Ord + MaybeSerializeDeserialize + Parameter;
/// Something that gives the IdtyIndex of an AccountId and reverse.
type IdtyAttr: duniter_primitives::Idty<Self::IdtyIndex, Self::AccountId>;
/// Something that gives the AccountId of an identity.
type IdtyIdOfAuthorityId: Convert<Self::MemberId, Option<Self::IdtyIndex>>;
/// Maximum number of active certifications per issuer.
#[pallet::constant]
type MaxByIssuer: Get<u32>;
/// Minimum number of certifications required to become a Smith.
#[pallet::constant]
type MinCertForMembership: Get<u32>;
/// Maximum duration of inactivity allowed before a Smith is removed.
#[pallet::constant]
type SmithInactivityMaxDuration: Get<u32>;
/// Type representing the weight of this pallet.
type WeightInfo: WeightInfo;
}
/// Events type.
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
/// An identity is being inivited to become a smith.
InvitationSent {
issuer: T::IdtyIndex,
receiver: T::IdtyIndex,
},
/// The invitation has been accepted.
InvitationAccepted { idty_index: T::IdtyIndex },
/// Certification received
SmithCertAdded {
issuer: T::IdtyIndex,
receiver: T::IdtyIndex,
},
/// Certification lost
SmithCertRemoved {
issuer: T::IdtyIndex,
receiver: T::IdtyIndex,
},
/// A smith gathered enough certifications to become an authority (can call `go_online()`).
SmithMembershipAdded { idty_index: T::IdtyIndex },
/// A smith has been removed from the smiths set.
SmithMembershipRemoved { idty_index: T::IdtyIndex },
}
#[pallet::genesis_config]
pub struct GenesisConfig<T: Config> {
pub initial_smiths: BTreeMap<T::IdtyIndex, (bool, Vec<T::IdtyIndex>)>,
}
impl<T: Config> Default for GenesisConfig<T> {
fn default() -> Self {
Self {
initial_smiths: Default::default(),
}
}
}
#[pallet::genesis_build]
impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
fn build(&self) {
CurrentSession::<T>::put(0);
let mut cert_meta_by_issuer = BTreeMap::<T::IdtyIndex, Vec<T::IdtyIndex>>::new();
for (receiver, (is_online, issuers)) in &self.initial_smiths {
// Forbid self-cert
assert!(
!issuers.contains(receiver),
"Identity cannot certify it-self."
);
let mut issuers_: Vec<_> = Vec::with_capacity(issuers.len());
for issuer in issuers {
// Count issued certs
cert_meta_by_issuer
.entry(*issuer)
.or_insert(vec![])
.push(*receiver);
issuers_.push(*issuer);
}
// Write CertsByReceiver
issuers_.sort();
let issuers_count = issuers_.len();
let smith_status = if issuers_count >= T::MinCertForMembership::get() as usize {
SmithStatus::Smith
} else {
SmithStatus::Pending
};
Smiths::<T>::insert(
receiver,
SmithMeta {
status: smith_status,
expires_on: if *is_online {
None
} else {
Some(CurrentSession::<T>::get() + T::SmithInactivityMaxDuration::get())
},
issued_certs: vec![],
received_certs: issuers_,
},
);
ExpiresOn::<T>::append(
CurrentSession::<T>::get() + T::SmithInactivityMaxDuration::get(),
receiver,
);
}
for (issuer, issued_certs) in cert_meta_by_issuer {
// Write CertsByIssuer
Smiths::<T>::mutate(issuer, |maybe_smith_meta| {
if let Some(smith_meta) = maybe_smith_meta {
smith_meta.issued_certs = issued_certs;
}
});
}
}
}
/// The Smith metadata for each identity.
#[pallet::storage]
#[pallet::getter(fn smiths)]
pub type Smiths<T: Config> =
StorageMap<_, Twox64Concat, T::IdtyIndex, SmithMeta<T::IdtyIndex>, OptionQuery>;
/// The indexes of Smith to remove at a given session.
#[pallet::storage]
#[pallet::getter(fn expires_on)]
pub type ExpiresOn<T: Config> =
StorageMap<_, Twox64Concat, SessionIndex, Vec<T::IdtyIndex>, OptionQuery>;
/// The current session index.
#[pallet::storage]
#[pallet::getter(fn current_session)]
pub type CurrentSession<T: Config> = StorageValue<_, SessionIndex, ValueQuery>;
// ERRORS //
#[pallet::error]
pub enum Error<T> {
/// Issuer of anything (invitation, acceptance, certification) must have an identity ID
OriginMustHaveAnIdentity,
/// Issuer must be known as a potential smith
OriginHasNeverBeenInvited,
/// Invitation is reseverd to smiths
InvitationIsASmithPrivilege,
/// Invitation is reseverd to online smiths
InvitationIsAOnlineSmithPrivilege,
/// Invitation must not have been accepted yet
InvitationAlreadyAccepted,
/// Invitation of an already known smith is forbidden except if it has been excluded
InvitationOfExistingNonExcluded,
/// Invitation of a non-member (of the WoT) is forbidden
InvitationOfNonMember,
/// Certification cannot be made on someone who has not accepted an invitation
CertificationMustBeAgreed,
/// Certification cannot be made on excluded
CertificationOnExcludedIsForbidden,
/// Issuer must be a smith
CertificationIsASmithPrivilege,
/// Only online smiths can certify
CertificationIsAOnlineSmithPrivilege,
/// Smith cannot certify itself
CertificationOfSelfIsForbidden,
/// Receiver must be invited by another smith
CertificationReceiverMustHaveBeenInvited,
/// Receiver must not already have this certification
CertificationAlreadyExists,
/// A smith has a limited stock of certifications
CertificationStockFullyConsumed,
}
#[pallet::call]
impl<T: Config> Pallet<T> {
/// Invite a member of the Web of Trust to attempt becoming a Smith.
#[pallet::call_index(0)]
#[pallet::weight(T::WeightInfo::invite_smith())]
pub fn invite_smith(
origin: OriginFor<T>,
receiver: T::IdtyIndex,
) -> DispatchResultWithPostInfo {
let who = ensure_signed(origin.clone())?;
let issuer =
T::IdtyAttr::idty_index(who.clone()).ok_or(Error::<T>::OriginMustHaveAnIdentity)?;
Self::check_invite_smith(issuer, receiver)?;
Self::do_invite_smith(issuer, receiver);
Ok(().into())
}
/// Accept an invitation to become a Smith (must have been invited first).
#[pallet::call_index(1)]
#[pallet::weight(T::WeightInfo::accept_invitation())]
pub fn accept_invitation(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
let who = ensure_signed(origin.clone())?;
let receiver =
T::IdtyAttr::idty_index(who.clone()).ok_or(Error::<T>::OriginMustHaveAnIdentity)?;
Self::check_accept_invitation(receiver)?;
Self::do_accept_invitation(receiver)?;
Ok(().into())
}
/// Certify an invited Smith, which can lead the certified to become a Smith.
#[pallet::call_index(2)]
#[pallet::weight(T::WeightInfo::certify_smith())]
pub fn certify_smith(
origin: OriginFor<T>,
receiver: T::IdtyIndex,
) -> DispatchResultWithPostInfo {
let who = ensure_signed(origin)?;
let issuer =
T::IdtyAttr::idty_index(who.clone()).ok_or(Error::<T>::OriginMustHaveAnIdentity)?;
Self::check_certify_smith(issuer, receiver)?;
Self::do_certify_smith(receiver, issuer);
Ok(().into())
}
}
}
impl<T: Config> Pallet<T> {
/// Check conditions before inviting a potential Smith.
fn check_invite_smith(
issuer: T::IdtyIndex,
receiver: T::IdtyIndex,
) -> DispatchResultWithPostInfo {
let issuer = Smiths::<T>::get(issuer).ok_or(Error::<T>::OriginHasNeverBeenInvited)?;
ensure!(
issuer.status == SmithStatus::Smith,
Error::<T>::InvitationIsASmithPrivilege
);
ensure!(
issuer.expires_on.is_none(),
Error::<T>::InvitationIsAOnlineSmithPrivilege
);
if let Some(receiver_meta) = Smiths::<T>::get(receiver) {
ensure!(
receiver_meta.status == SmithStatus::Excluded,
Error::<T>::InvitationOfExistingNonExcluded
);
}
ensure!(
T::IsWoTMember::is_member(&receiver),
Error::<T>::InvitationOfNonMember
);
Ok(().into())
}
/// Perform the invitation of a potential Smith.
fn do_invite_smith(issuer: T::IdtyIndex, receiver: T::IdtyIndex) {
let new_expires_on = CurrentSession::<T>::get() + T::SmithInactivityMaxDuration::get();
let mut existing = Smiths::<T>::get(receiver).unwrap_or_default();
existing.status = SmithStatus::Invited;
existing.expires_on = Some(new_expires_on);
existing.received_certs = vec![];
Smiths::<T>::insert(receiver, existing);
ExpiresOn::<T>::append(new_expires_on, receiver);
Self::deposit_event(Event::<T>::InvitationSent { issuer, receiver });
}
/// Check conditions before accepting an invitation to become a Smith.
fn check_accept_invitation(receiver: T::IdtyIndex) -> DispatchResultWithPostInfo {
let pretender_status = Smiths::<T>::get(receiver)
.ok_or(Error::<T>::OriginHasNeverBeenInvited)?
.status;
ensure!(
pretender_status == SmithStatus::Invited,
Error::<T>::InvitationAlreadyAccepted
);
Ok(().into())
}
/// Accept the invitation to become a Smith.
fn do_accept_invitation(receiver: T::IdtyIndex) -> DispatchResultWithPostInfo {
Smiths::<T>::mutate(receiver, |maybe_smith_meta| {
if let Some(smith_meta) = maybe_smith_meta {
smith_meta.status = SmithStatus::Pending;
}
});
Self::deposit_event(Event::<T>::InvitationAccepted {
idty_index: receiver,
});
Ok(().into())
}
/// Check conditions before certifying a potential Smith.
fn check_certify_smith(
issuer_index: T::IdtyIndex,
receiver_index: T::IdtyIndex,
) -> DispatchResultWithPostInfo {
ensure!(
issuer_index != receiver_index,
Error::<T>::CertificationOfSelfIsForbidden
);
let issuer = Smiths::<T>::get(issuer_index).ok_or(Error::<T>::OriginHasNeverBeenInvited)?;
ensure!(
issuer.status == SmithStatus::Smith,
Error::<T>::CertificationIsASmithPrivilege
);
ensure!(
issuer.expires_on.is_none(),
Error::<T>::CertificationIsAOnlineSmithPrivilege
);
let issued_certs = issuer.issued_certs.len();
ensure!(
issued_certs < T::MaxByIssuer::get() as usize,
Error::<T>::CertificationStockFullyConsumed
);
let receiver = Smiths::<T>::get(receiver_index)
.ok_or(Error::<T>::CertificationReceiverMustHaveBeenInvited)?;
ensure!(
receiver.status != SmithStatus::Invited,
Error::<T>::CertificationMustBeAgreed
);
ensure!(
receiver.status != SmithStatus::Excluded,
Error::<T>::CertificationOnExcludedIsForbidden
);
ensure!(
receiver
.received_certs
.binary_search(&issuer_index)
.is_err(),
Error::<T>::CertificationAlreadyExists
);
Ok(().into())
}
/// Perform certification of a potential Smith by another Smith.
fn do_certify_smith(receiver: T::IdtyIndex, issuer: T::IdtyIndex) {
// - adds a certification in issuer issued list
Smiths::<T>::mutate(issuer, |maybe_smith_meta| {
if let Some(smith_meta) = maybe_smith_meta {
smith_meta.issued_certs.push(receiver);
smith_meta.issued_certs.sort();
}
});
Smiths::<T>::mutate(receiver, |maybe_smith_meta| {
if let Some(smith_meta) = maybe_smith_meta {
// - adds a certification in receiver received list
smith_meta.received_certs.push(issuer);
smith_meta.received_certs.sort();
Self::deposit_event(Event::<T>::SmithCertAdded { issuer, receiver });
// - receiving a certification either lead us to Pending or Smith status
let previous_status = smith_meta.status;
smith_meta.status =
if smith_meta.received_certs.len() >= T::MinCertForMembership::get() as usize {
// - if the number of certification received by the receiver is enough, win the Smith status (or keep it)
SmithStatus::Smith
} else {
// - otherwise we are (still) a pending smith
SmithStatus::Pending
};
if previous_status != SmithStatus::Smith {
// - postpone the expiration: a Pending smith cannot do anything but wait
// this postponement is here to ease the process of becoming a smith
let new_expires_on =
CurrentSession::<T>::get() + T::SmithInactivityMaxDuration::get();
smith_meta.expires_on = Some(new_expires_on);
ExpiresOn::<T>::append(new_expires_on, receiver);
}
// - if the status is smith but wasn't, notify that smith gained membership
if smith_meta.status == SmithStatus::Smith && previous_status != SmithStatus::Smith
{
Self::deposit_event(Event::<T>::SmithMembershipAdded {
idty_index: receiver,
});
}
// TODO: (optimization) unschedule old expiry
}
});
}
/// Handle the removal of Smiths whose expiration time has been reached at a given session index.
fn on_exclude_expired_smiths(at: SessionIndex) {
if let Some(smiths_to_remove) = ExpiresOn::<T>::get(at) {
for smith in smiths_to_remove {
if let Some(smith_meta) = Smiths::<T>::get(smith) {
if let Some(expires_on) = smith_meta.expires_on {
if expires_on == at {
Self::_do_exclude_smith(smith, SmithRemovalReason::OfflineTooLong);
}
}
}
}
}
}
/// Handle actions upon the removal of a Web of Trust member.
pub fn on_removed_wot_member(idty_index: T::IdtyIndex) -> Weight {
let mut weight = T::WeightInfo::on_removed_wot_member_empty();
if Smiths::<T>::get(idty_index).is_some() {
Self::_do_exclude_smith(idty_index, SmithRemovalReason::LostMembership);
weight = weight.saturating_add(T::WeightInfo::on_removed_wot_member());
}
weight
}
/// Perform the exclusion of a Smith.
fn _do_exclude_smith(receiver: T::IdtyIndex, reason: SmithRemovalReason) {
let mut lost_certs = vec![];
Smiths::<T>::mutate(receiver, |maybe_smith_meta| {
if let Some(smith_meta) = maybe_smith_meta {
smith_meta.expires_on = None;
smith_meta.status = SmithStatus::Excluded;
for cert in &smith_meta.received_certs {
lost_certs.push(*cert);
}
smith_meta.received_certs = vec![];
// N.B.: the issued certs are kept in case the smith joins back
}
});
// We remove the lost certs from their issuer's stock
for lost_cert_issuer in lost_certs {
Smiths::<T>::mutate(lost_cert_issuer, |maybe_smith_meta| {
if let Some(smith_meta) = maybe_smith_meta {
if let Ok(index) = smith_meta.issued_certs.binary_search(&receiver) {
smith_meta.issued_certs.remove(index);
Self::deposit_event(Event::<T>::SmithCertRemoved {
issuer: lost_cert_issuer,
receiver,
});
}
}
});
}
// Deletion done: notify (authority-members) for cascading
T::OnSmithDelete::on_smith_delete(receiver, reason);
Self::deposit_event(Event::<T>::SmithMembershipRemoved {
idty_index: receiver,
});
}
/// Handle the event when a Smith goes online.
pub fn on_smith_goes_online(idty_index: T::IdtyIndex) {
if let Some(smith_meta) = Smiths::<T>::get(idty_index) {
if smith_meta.expires_on.is_some() {
Smiths::<T>::mutate(idty_index, |maybe_smith_meta| {
if let Some(smith_meta) = maybe_smith_meta {
// As long as the smith is online, it cannot expire
smith_meta.expires_on = None;
// FIXME: unschedule old expiry
}
});
}
}
}
/// Handle the event when a Smith goes offline.
pub fn on_smith_goes_offline(idty_index: T::IdtyIndex) {
if let Some(smith_meta) = Smiths::<T>::get(idty_index) {
if smith_meta.expires_on.is_none() {
Smiths::<T>::mutate(idty_index, |maybe_smith_meta| {
if let Some(smith_meta) = maybe_smith_meta {
// As long as the smith is online, it cannot expire
let new_expires_on =
CurrentSession::<T>::get() + T::SmithInactivityMaxDuration::get();
smith_meta.expires_on = Some(new_expires_on);
ExpiresOn::<T>::append(new_expires_on, idty_index);
}
});
}
}
}
/// Provide whether the given identity index is a Smith.
fn provide_is_member(idty_id: &T::IdtyIndex) -> bool {
let Some(smith) = Smiths::<T>::get(idty_id) else {
return false;
};
smith.status == SmithStatus::Smith
}
}
impl<T: Config> sp_runtime::traits::IsMember<T::IdtyIndex> for Pallet<T> {
fn is_member(idty_id: &T::IdtyIndex) -> bool {
Self::provide_is_member(idty_id)
}
}