Skip to content
Snippets Groups Projects
  • Benjamin Gallois's avatar
    6cf445b5
    Harmonize and document events errors and calls (!200) · 6cf445b5
    Benjamin Gallois authored and Hugo Trentesaux's avatar Hugo Trentesaux committed
    * fix clippy errors
    
    * add Invalid case in distance status
    
    * fix events pallet-membership
    
    * fix events pallet-certification
    
    * update docs
    
    * update metadata and docs
    
    * refactor errors pallet-distance
    
    * fix distance error naming pallet duniter-wot
    
    * add pallet membership documentation
    
    * refactor acquisition and renewal pallet-membership
    
    * refactor expiration and revokation pallet-membership
    
    * refactor calls pallet-identity
    
    * add pallet standard guidelines
    
    * refactor events struct pallet-membership
    
    * add events pallet-distance
    
    * refactor documentation pallet-quota
    
    * fix pallet-quota features
    
    * refactor documentation pallet-provide-randomness
    
    * refactor documentation pallet-oneshot-account
    
    * refactor documentation pallet-offences
    
    * refactor documentation pallet-membership
    
    * refactor documentation pallet-identity
    
    * refactor documentation pallet-duniter-wot
    
    * refactor documentation pallet-duniter-account
    
    * refactor documentation pallet-distance
    
    * refactor documentation pallet-certification
    
    * refactor documentation pallet-authority-members
    
    * refactor error naming pallet-provide-randomness
    
    * remove unused errors pallet-membership
    
    * remove unused errors pallet-identity
    
    * refactor events and errors pallet-distance
    
    * refactor events and errors pallet-authority-members
    6cf445b5
    History
    Harmonize and document events errors and calls (!200)
    Benjamin Gallois authored and Hugo Trentesaux's avatar Hugo Trentesaux committed
    * fix clippy errors
    
    * add Invalid case in distance status
    
    * fix events pallet-membership
    
    * fix events pallet-certification
    
    * update docs
    
    * update metadata and docs
    
    * refactor errors pallet-distance
    
    * fix distance error naming pallet duniter-wot
    
    * add pallet membership documentation
    
    * refactor acquisition and renewal pallet-membership
    
    * refactor expiration and revokation pallet-membership
    
    * refactor calls pallet-identity
    
    * add pallet standard guidelines
    
    * refactor events struct pallet-membership
    
    * add events pallet-distance
    
    * refactor documentation pallet-quota
    
    * fix pallet-quota features
    
    * refactor documentation pallet-provide-randomness
    
    * refactor documentation pallet-oneshot-account
    
    * refactor documentation pallet-offences
    
    * refactor documentation pallet-membership
    
    * refactor documentation pallet-identity
    
    * refactor documentation pallet-duniter-wot
    
    * refactor documentation pallet-duniter-account
    
    * refactor documentation pallet-distance
    
    * refactor documentation pallet-certification
    
    * refactor documentation pallet-authority-members
    
    * refactor error naming pallet-provide-randomness
    
    * remove unused errors pallet-membership
    
    * remove unused errors pallet-identity
    
    * refactor events and errors pallet-distance
    
    * refactor events and errors pallet-authority-members
handlers.rs 8.42 KiB
// 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/>.

use super::entities::*;
use super::{AccountId, IdtyIndex};
use frame_support::dispatch::UnfilteredDispatchable;
use frame_support::instances::{Instance1, Instance2};
use frame_support::pallet_prelude::Weight;
use frame_support::Parameter;
use pallet_identity::IdtyEvent;
use sp_runtime::traits::IsMember;

// new session handler
pub struct OnNewSessionHandler<Runtime>(core::marker::PhantomData<Runtime>);
impl<Runtime> pallet_authority_members::traits::OnNewSession for OnNewSessionHandler<Runtime>
where
    Runtime: pallet_provide_randomness::Config + pallet_distance::Config,
{
    fn on_new_session(index: sp_staking::SessionIndex) -> Weight {
        pallet_provide_randomness::Pallet::<Runtime>::on_new_epoch();
        pallet_distance::Pallet::<Runtime>::on_new_session(index)
    }
}

// identity change runtime handler
pub struct OnIdtyChangeHandler<Runtime>(core::marker::PhantomData<Runtime>);
impl<T> pallet_identity::traits::OnIdtyChange<T> for OnIdtyChangeHandler<T>
where
    T: frame_system::Config<AccountId = AccountId>,
    T: pallet_authority_members::Config<MemberId = IdtyIndex>,
    T: pallet_identity::Config<IdtyIndex = IdtyIndex, IdtyData = IdtyData>,
    T: pallet_universal_dividend::Config,
{
    fn on_idty_change(idty_index: IdtyIndex, idty_event: &IdtyEvent<T>) {
        match idty_event {
            IdtyEvent::Validated => {
                // when identity is validated, it starts getting right to UD
                // but this is handeled by membership event handler (MembershipAdded)
            }
            IdtyEvent::ChangedOwnerKey { new_owner_key } => {
                if let Err(e) = pallet_authority_members::Pallet::<T>::change_owner_key(
                    idty_index,
                    new_owner_key.clone(),
                ) {
                    log::error!(
                        "on_idty_change: pallet_authority_members.change_owner_key(): {:?}",
                        e
                    );
                }
            }
            IdtyEvent::Created { .. } | IdtyEvent::Confirmed | IdtyEvent::Removed { .. } => {}
        }
    }
}

// membership event runtime handler
pub struct OnMembershipEventHandler<Inner, Runtime>(core::marker::PhantomData<(Inner, Runtime)>);
impl<
        Inner: sp_membership::traits::OnEvent<IdtyIndex>,
        Runtime: frame_system::Config<AccountId = AccountId>
            + pallet_identity::Config<IdtyData = IdtyData, IdtyIndex = IdtyIndex>
            + pallet_membership::Config<Instance1>
            + pallet_universal_dividend::Config,
    > sp_membership::traits::OnEvent<IdtyIndex> for OnMembershipEventHandler<Inner, Runtime>
{
    fn on_event(membership_event: &sp_membership::Event<IdtyIndex>) {
        (match membership_event {
            // when membership is removed, call on_removed_member handler which auto claims UD
            sp_membership::Event::MembershipRemoved(idty_index) => {
                if let Some(idty_value) = pallet_identity::Identities::<Runtime>::get(idty_index) {
                    if let Some(first_ud_index) = idty_value.data.first_eligible_ud.into() {
                        pallet_universal_dividend::Pallet::<Runtime>::on_removed_member(
                            first_ud_index,
                            &idty_value.owner_key,
                        );
                    }
                }
            }
            // when main membership is acquired, it starts getting right to UD
            sp_membership::Event::MembershipAdded(idty_index) => {
                pallet_identity::Identities::<Runtime>::mutate_exists(idty_index, |idty_val_opt| {
                    if let Some(ref mut idty_val) = idty_val_opt {
                        idty_val.data = IdtyData {
                            first_eligible_ud:
                                pallet_universal_dividend::Pallet::<Runtime>::init_first_eligible_ud(
                                ),
                        };
                    }
                });
            }
            // in other case, ther is nothing to do
            sp_membership::Event::MembershipRenewed(_)
            | sp_membership::Event::PendingMembershipAdded(_)
            | sp_membership::Event::PendingMembershipExpired(_) => (),
        });
        Inner::on_event(membership_event)
    }
}

// smith membership event handler
pub struct OnSmithMembershipEventHandler<Inner, Runtime>(
    core::marker::PhantomData<(Inner, Runtime)>,
);
impl<
        IdtyIndex: Copy + Parameter,
        Inner: sp_membership::traits::OnEvent<IdtyIndex>,
        Runtime: frame_system::Config<AccountId = AccountId>
            + pallet_identity::Config<IdtyIndex = IdtyIndex>
            + pallet_authority_members::Config<MemberId = IdtyIndex>
            + pallet_membership::Config<Instance2>,
    > sp_membership::traits::OnEvent<IdtyIndex> for OnSmithMembershipEventHandler<Inner, Runtime>
{
    fn on_event(membership_event: &sp_membership::Event<IdtyIndex>) {
        (match membership_event {
            sp_membership::Event::MembershipAdded(_idty_index) => {
                // nothing when smith membership acquired
                // user will have to claim authority membership
            }
            sp_membership::Event::MembershipRemoved(idty_index) => {
                let call = pallet_authority_members::Call::<Runtime>::remove_member {
                    member_id: *idty_index,
                };
                if let Err(e) =
                    call.dispatch_bypass_filter(frame_system::Origin::<Runtime>::Root.into())
                {
                    sp_std::if_std! {
                        println!("faid to remove member: {:?}", e)
                    }
                }
            }
            _ => (),
        });
        Inner::on_event(membership_event)
    }
}

// authority member removal handler
pub struct OnRemovedAuthorityMemberHandler<Runtime>(core::marker::PhantomData<Runtime>);
impl<Runtime> pallet_authority_members::traits::OnRemovedMember<IdtyIndex>
    for OnRemovedAuthorityMemberHandler<Runtime>
where
    Runtime: frame_system::Config + pallet_membership::Config<Instance2, IdtyId = IdtyIndex>,
{
    fn on_removed_member(idty_index: IdtyIndex) -> Weight {
        // TODO investigate why we should remove smith membership when removing authority member
        pallet_membership::Pallet::<Runtime, Instance2>::force_revoke_membership(idty_index);
        // TODO investigate why weight zero
        Weight::zero()
    }
}

// identity removal handler
pub struct RemoveIdentityConsumersImpl<Runtime>(core::marker::PhantomData<Runtime>);
impl<Runtime> pallet_identity::traits::RemoveIdentityConsumers<IdtyIndex>
    for RemoveIdentityConsumersImpl<Runtime>
where
    Runtime: pallet_identity::Config<IdtyIndex = IdtyIndex>
        + pallet_authority_members::Config<MemberId = IdtyIndex>
        + pallet_membership::Config<Instance1, IdtyId = IdtyIndex>
        + pallet_membership::Config<Instance2, IdtyId = IdtyIndex>,
{
    fn remove_idty_consumers(idty_index: IdtyIndex) -> Weight {
        // Remove smith member
        if pallet_membership::Pallet::<Runtime, Instance2>::is_member(&idty_index) {
            pallet_membership::Pallet::<Runtime, Instance2>::force_revoke_membership(idty_index);
        }
        // Remove "classic" member
        pallet_membership::Pallet::<Runtime, Instance1>::force_revoke_membership(idty_index);

        Weight::zero()
    }
}

// spend treasury handler
pub struct TreasurySpendFunds<Runtime>(core::marker::PhantomData<Runtime>);
impl<Runtime> pallet_treasury::SpendFunds<Runtime> for TreasurySpendFunds<Runtime>
where
    Runtime: pallet_treasury::Config,
{
    fn spend_funds(
        _budget_remaining: &mut pallet_treasury::BalanceOf<Runtime>,
        _imbalance: &mut pallet_treasury::PositiveImbalanceOf<Runtime>,
        _total_weight: &mut Weight,
        missed_any: &mut bool,
    ) {
        *missed_any = true;
    }
}