Skip to content
Snippets Groups Projects
Commit 8f0e16b6 authored by Cédric Moreau's avatar Cédric Moreau
Browse files

feat(smith-members): refact: check_* and do_* functions

parent a16039d3
No related branches found
No related tags found
No related merge requests found
......@@ -27,10 +27,12 @@ mod traits;
mod types;
use codec::{Codec, Decode, Encode};
use frame_support::dispatch::TypeInfo;
use frame_support::dispatch::{DispatchResultWithPostInfo, TypeInfo};
use frame_support::pallet_prelude::Get;
use frame_support::RuntimeDebug;
use sp_runtime::traits::AtLeast32BitUnsigned;
use frame_support::{ensure, RuntimeDebug};
use frame_system::ensure_signed;
use frame_system::pallet_prelude::OriginFor;
use sp_runtime::traits::{AtLeast32BitUnsigned, Convert};
use sp_std::fmt::Debug;
use sp_std::prelude::*;
......@@ -238,8 +240,6 @@ pub mod pallet {
NotAMember,
}
// TODO: refactor with check_* and do_* functions
#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
......@@ -250,6 +250,41 @@ pub mod pallet {
) -> DispatchResultWithPostInfo {
let who = ensure_signed(origin.clone())?;
let issuer = T::IdtyIdOf::convert(who.clone()).ok_or(Error::<T>::UnknownIssuer)?;
Self::check_invite_smith(issuer, receiver)?;
Self::do_invite_smith(receiver);
Ok(().into())
}
#[pallet::call_index(1)]
#[pallet::weight(1_000_000_000)]
pub fn accept_invitation(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
let who = ensure_signed(origin.clone())?;
let receiver = T::IdtyIdOf::convert(who.clone()).ok_or(Error::<T>::UnknownIssuer)?;
Self::check_accept_invitation(receiver)?;
Self::do_accept_invitation(receiver)?;
Ok(().into())
}
#[pallet::call_index(2)]
#[pallet::weight(1_000_000_000)]
pub fn certify_smith(
origin: OriginFor<T>,
receiver: T::IdtyIndex,
) -> DispatchResultWithPostInfo {
let who = ensure_signed(origin)?;
let issuer = T::IdtyIdOf::convert(who.clone()).ok_or(Error::<T>::UnknownIssuer)?;
Self::check_certify_smith(issuer, receiver)?;
Self::do_certify_smith(receiver, issuer);
Ok(().into())
}
}
}
impl<T: Config> Pallet<T> {
fn check_invite_smith(
issuer: T::IdtyIndex,
receiver: T::IdtyIndex,
) -> DispatchResultWithPostInfo {
let issuer_status = Smiths::<T>::get(issuer)
.ok_or(Error::<T>::IssuerHasNoSmithStatus)?
.status;
......@@ -262,6 +297,10 @@ pub mod pallet {
Error::<T>::ReceveirAlreadyHasSmithStatus
);
Ok(().into())
}
fn do_invite_smith(receiver: T::IdtyIndex) {
let new_expires_on = CurrentSession::<T>::get() + T::InactivityMaxDuration::get();
Smiths::<T>::insert(
receiver,
......@@ -272,39 +311,31 @@ pub mod pallet {
},
);
ExpiresOn::<T>::append(new_expires_on, receiver);
Ok(().into())
}
#[pallet::call_index(1)]
#[pallet::weight(1_000_000_000)]
pub fn accept_invitation(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
let who = ensure_signed(origin.clone())?;
let pretender = T::IdtyIdOf::convert(who.clone()).ok_or(Error::<T>::UnknownIssuer)?;
let pretender_status = Smiths::<T>::get(pretender)
fn check_accept_invitation(receiver: T::IdtyIndex) -> DispatchResultWithPostInfo {
let pretender_status = Smiths::<T>::get(receiver)
.ok_or(Error::<T>::NotInvited)?
.status;
ensure!(
pretender_status == SmithStatus::Invited,
Error::<T>::AlreadyAcceptedInvitation
);
Ok(().into())
}
Smiths::<T>::mutate(pretender, |maybe_smith_meta| {
fn do_accept_invitation(receiver: T::IdtyIndex) -> DispatchResultWithPostInfo {
Smiths::<T>::mutate(receiver, |maybe_smith_meta| {
let maybe_smith_meta = maybe_smith_meta.as_mut().expect("status checked earlier");
maybe_smith_meta.status = SmithStatus::Pending;
});
Ok(().into())
}
#[pallet::call_index(2)]
#[pallet::weight(1_000_000_000)]
pub fn certify_smith(
origin: OriginFor<T>,
fn check_certify_smith(
issuer: T::IdtyIndex,
receiver: T::IdtyIndex,
) -> DispatchResultWithPostInfo {
let who = ensure_signed(origin)?;
let issuer = T::IdtyIdOf::convert(who.clone()).ok_or(Error::<T>::UnknownIssuer)?;
ensure!(issuer != receiver, Error::<T>::CannotCertifySelf);
let issuer_status = Smiths::<T>::get(issuer)
.ok_or(Error::<T>::IssuerHasNoSmithStatus)?
......@@ -321,14 +352,17 @@ pub mod pallet {
Error::<T>::ReceveirMustAcceptInvitation
);
Ok(().into())
}
fn do_certify_smith(receiver: T::IdtyIndex, issuer: T::IdtyIndex) {
Smiths::<T>::mutate(receiver, |maybe_smith_meta| {
let maybe_smith_meta = maybe_smith_meta.as_mut().expect("status checked earlier");
maybe_smith_meta.received_certs.push(issuer);
maybe_smith_meta.received_certs.sort();
// TODO: "as u32" allowed?
maybe_smith_meta.status = if maybe_smith_meta.received_certs.len() as u32
>= T::MinCertForMembership::get()
{
maybe_smith_meta.status =
if maybe_smith_meta.received_certs.len() as u32 >= T::MinCertForMembership::get() {
SmithStatus::Smith
} else {
SmithStatus::Pending
......@@ -338,13 +372,8 @@ pub mod pallet {
maybe_smith_meta.expires_on = Some(new_expires_on);
// TODO: unschedule old expiry
});
Ok(().into())
}
}
}
impl<T: Config> Pallet<T> {
// TODO: return what?
fn remove_expired_smiths(at: SessionIndex) {
if let Some(smiths_to_remove) = ExpiresOn::<T>::get(at) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment