diff --git a/pallets/duniter-wot/src/lib.rs b/pallets/duniter-wot/src/lib.rs index e84484e7b49ef5b180e31eacba33fb5317d2344e..5afc0ce52f6fe78b4c9b60a8849557067ef9a7de 100644 --- a/pallets/duniter-wot/src/lib.rs +++ b/pallets/duniter-wot/src/lib.rs @@ -97,6 +97,12 @@ pub mod pallet { #[pallet::error] pub enum Error<T, I = ()> { + /// Identity not allowed to claim membership + IdtyNotAllowedToClaimMembership, + /// Identity not allowed to request membership + IdtyNotAllowedToRequestMembership, + /// Identity not allowed to renew membership + IdtyNotAllowedToRenewMembership, /// Identity creation period not respected IdtyCreationPeriodNotRespected, /// Not enough received certifications to create identity @@ -215,36 +221,40 @@ impl<T: Config<I>, I: 'static> pallet_certification::traits::CheckCertAllowed<Id } } -impl<T: Config<I>, I: 'static> sp_membership::traits::IsIdtyAllowedToClaimMembership<IdtyIndex> +impl<T: Config<I>, I: 'static> sp_membership::traits::CheckIdtyAllowedMembership<IdtyIndex> for Pallet<T, I> { - fn is_idty_allowed_to_claim_membership(idty_index: &IdtyIndex) -> bool { + fn check_idty_allowed_to_claim_membership(idty_index: &IdtyIndex) -> Result<(), DispatchError> { let idty_cert_meta = pallet_certification::Pallet::<T, I>::idty_cert_meta(idty_index); - idty_cert_meta.received_count >= T::MinCertForMembership::get() as u32 + ensure!( + idty_cert_meta.received_count >= T::MinCertForMembership::get() as u32, + Error::<T, I>::IdtyNotAllowedToClaimMembership + ); + Ok(()) } -} -impl<T: Config<I>, I: 'static> sp_membership::traits::IsIdtyAllowedToRenewMembership<IdtyIndex> - for Pallet<T, I> -{ - fn is_idty_allowed_to_renew_membership(idty_index: &IdtyIndex) -> bool { + fn check_idty_allowed_to_renew_membership(idty_index: &IdtyIndex) -> Result<(), DispatchError> { if let Some(idty_value) = pallet_identity::Pallet::<T>::identity(idty_index) { - idty_value.status == IdtyStatus::Validated - } else { - false + ensure!( + idty_value.status == IdtyStatus::Validated, + Error::<T, I>::IdtyNotAllowedToRenewMembership + ); } + // FIXME what if identity not found? + Ok(()) } -} -impl<T: Config<I>, I: 'static> sp_membership::traits::IsIdtyAllowedToRequestMembership<IdtyIndex> - for Pallet<T, I> -{ - fn is_idty_allowed_to_request_membership(idty_index: &IdtyIndex) -> bool { + fn check_idty_allowed_to_request_membership( + idty_index: &IdtyIndex, + ) -> Result<(), DispatchError> { if let Some(idty_value) = pallet_identity::Pallet::<T>::identity(idty_index) { - T::IsSubWot::get() && idty_value.status == IdtyStatus::Validated - } else { - false + ensure!( + T::IsSubWot::get() && idty_value.status == IdtyStatus::Validated, + Error::<T, I>::IdtyNotAllowedToRequestMembership + ); } + // FIXME what if identity not found? + Ok(()) } } diff --git a/pallets/duniter-wot/src/mock.rs b/pallets/duniter-wot/src/mock.rs index d26889790d297bd33e05c2165a24f2598d65f540..5552ca3bc7a103018e92e06fb53be846cbee59b4 100644 --- a/pallets/duniter-wot/src/mock.rs +++ b/pallets/duniter-wot/src/mock.rs @@ -142,9 +142,7 @@ parameter_types! { } impl pallet_membership::Config<Instance1> for Test { - type IsIdtyAllowedToClaimMembership = DuniterWot; - type IsIdtyAllowedToRenewMembership = DuniterWot; - type IsIdtyAllowedToRequestMembership = DuniterWot; + type CheckIdtyAllowedMembership = DuniterWot; type Event = Event; type IdtyId = IdtyIndex; type IdtyIdOf = IdentityIndexOf<Self>; @@ -196,9 +194,7 @@ parameter_types! { } impl pallet_membership::Config<Instance2> for Test { - type IsIdtyAllowedToClaimMembership = SmithsSubWot; - type IsIdtyAllowedToRenewMembership = SmithsSubWot; - type IsIdtyAllowedToRequestMembership = SmithsSubWot; + type CheckIdtyAllowedMembership = SmithsSubWot; type Event = Event; type IdtyId = IdtyIndex; type IdtyIdOf = IdentityIndexOf<Self>; diff --git a/pallets/membership/src/lib.rs b/pallets/membership/src/lib.rs index 4c9d84b2457caec93266f13647b04f197b634f4f..e3634f5d443aa6e0f6e1b0045cf37f9b409bcf23 100644 --- a/pallets/membership/src/lib.rs +++ b/pallets/membership/src/lib.rs @@ -59,12 +59,8 @@ pub mod pallet { #[pallet::config] pub trait Config<I: 'static = ()>: frame_system::Config { - /// Ask the runtime if the identity can claim the membership - type IsIdtyAllowedToClaimMembership: IsIdtyAllowedToClaimMembership<Self::IdtyId>; - /// Ask the runtime if the identity can renew his membership - type IsIdtyAllowedToRenewMembership: IsIdtyAllowedToRenewMembership<Self::IdtyId>; - /// Ask the runtime if the identity can request the membership - type IsIdtyAllowedToRequestMembership: IsIdtyAllowedToRequestMembership<Self::IdtyId>; + /// Ask the runtime if the identity can perform membership operations + type CheckIdtyAllowedMembership: CheckIdtyAllowedMembership<Self::IdtyId>; /// Because this pallet emits events, it depends on the runtime's definition of an event. type Event: From<Event<Self, I>> + IsType<<Self as frame_system::Config>::Event>; /// Something that identifies an identity @@ -160,12 +156,6 @@ pub mod pallet { #[pallet::error] pub enum Error<T, I = ()> { - /// Identity not allowed to claim membership - IdtyNotAllowedToClaimMembership, - /// Identity not allowed to request membership - IdtyNotAllowedToRequestMembership, - /// Identity not allowed to renew membership - IdtyNotAllowedToRenewMembership, /// Invalid meta data InvalidMetaData, /// Identity id not found @@ -221,10 +211,7 @@ pub mod pallet { if !metadata.validate(&who) { return Err(Error::<T, I>::InvalidMetaData.into()); } - if !T::IsIdtyAllowedToRequestMembership::is_idty_allowed_to_request_membership(&idty_id) - { - return Err(Error::<T, I>::IdtyNotAllowedToRequestMembership.into()); - } + T::CheckIdtyAllowedMembership::check_idty_allowed_to_request_membership(&idty_id)?; Self::do_request_membership(idty_id, metadata) } @@ -242,10 +229,7 @@ pub mod pallet { Error::<T, I>::MembershipAlreadyAcquired ); - ensure!( - T::IsIdtyAllowedToClaimMembership::is_idty_allowed_to_claim_membership(&idty_id), - Error::<T, I>::IdtyNotAllowedToClaimMembership - ); + T::CheckIdtyAllowedMembership::check_idty_allowed_to_claim_membership(&idty_id)?; let metadata = PendingMembership::<T, I>::take(&idty_id) .ok_or(Error::<T, I>::MembershipRequestNotFound)?; @@ -271,10 +255,7 @@ pub mod pallet { Error::<T, I>::MembershipNotFound ); - ensure!( - T::IsIdtyAllowedToRenewMembership::is_idty_allowed_to_renew_membership(&idty_id), - Error::<T, I>::IdtyNotAllowedToRenewMembership, - ); + T::CheckIdtyAllowedMembership::check_idty_allowed_to_renew_membership(&idty_id)?; let _ = Self::do_renew_membership(idty_id); diff --git a/pallets/membership/src/mock.rs b/pallets/membership/src/mock.rs index 7002e13db1def145f37e768253a61737339eb6d4..5710aa3413a4e4aa7c7fa5dd798817176767c53d 100644 --- a/pallets/membership/src/mock.rs +++ b/pallets/membership/src/mock.rs @@ -83,9 +83,7 @@ parameter_types! { } impl pallet_membership::Config for Test { - type IsIdtyAllowedToClaimMembership = (); - type IsIdtyAllowedToRenewMembership = (); - type IsIdtyAllowedToRequestMembership = (); + type CheckIdtyAllowedMembership = (); type Event = Event; type IdtyId = IdtyId; type IdtyIdOf = ConvertInto; diff --git a/primitives/membership/src/traits.rs b/primitives/membership/src/traits.rs index eaaeb98fb3077763e2d0eb30189d7b15d234b525..5a6e7df1d964131a12545b1ba68024554639cba6 100644 --- a/primitives/membership/src/traits.rs +++ b/primitives/membership/src/traits.rs @@ -14,35 +14,23 @@ // 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 frame_support::pallet_prelude::Weight; +use frame_support::pallet_prelude::*; -pub trait IsIdtyAllowedToClaimMembership<IdtyId> { - fn is_idty_allowed_to_claim_membership(idty_id: &IdtyId) -> bool; +pub trait CheckIdtyAllowedMembership<IdtyId> { + fn check_idty_allowed_to_claim_membership(idty_id: &IdtyId) -> Result<(), DispatchError>; + fn check_idty_allowed_to_renew_membership(idty_id: &IdtyId) -> Result<(), DispatchError>; + fn check_idty_allowed_to_request_membership(idty_id: &IdtyId) -> Result<(), DispatchError>; } -impl<IdtyId> IsIdtyAllowedToClaimMembership<IdtyId> for () { - fn is_idty_allowed_to_claim_membership(_: &IdtyId) -> bool { - true +impl<IdtyId> CheckIdtyAllowedMembership<IdtyId> for () { + fn check_idty_allowed_to_claim_membership(_: &IdtyId) -> Result<(), DispatchError> { + Ok(()) } -} - -pub trait IsIdtyAllowedToRenewMembership<IdtyId> { - fn is_idty_allowed_to_renew_membership(idty_id: &IdtyId) -> bool; -} - -impl<IdtyId> IsIdtyAllowedToRenewMembership<IdtyId> for () { - fn is_idty_allowed_to_renew_membership(_: &IdtyId) -> bool { - true + fn check_idty_allowed_to_renew_membership(_: &IdtyId) -> Result<(), DispatchError> { + Ok(()) } -} - -pub trait IsIdtyAllowedToRequestMembership<IdtyId> { - fn is_idty_allowed_to_request_membership(idty_id: &IdtyId) -> bool; -} - -impl<IdtyId> IsIdtyAllowedToRequestMembership<IdtyId> for () { - fn is_idty_allowed_to_request_membership(_: &IdtyId) -> bool { - true + fn check_idty_allowed_to_request_membership(_: &IdtyId) -> Result<(), DispatchError> { + Ok(()) } } diff --git a/runtime/common/src/pallets_config.rs b/runtime/common/src/pallets_config.rs index c5c6418fe87a56b67388e3a754e65c69c8a799ff..895dfdc8a6b01cd82d7dae5256d16709e30aa023 100644 --- a/runtime/common/src/pallets_config.rs +++ b/runtime/common/src/pallets_config.rs @@ -444,9 +444,7 @@ macro_rules! pallets_config { } impl pallet_membership::Config<frame_support::instances::Instance1> for Runtime { - type IsIdtyAllowedToClaimMembership = Wot; - type IsIdtyAllowedToRenewMembership = Wot; - type IsIdtyAllowedToRequestMembership = Wot; + type CheckIdtyAllowedMembership = Wot; type Event = Event; type IdtyId = IdtyIndex; type IdtyIdOf = common_runtime::providers::IdentityIndexOf<Self>; @@ -480,9 +478,7 @@ macro_rules! pallets_config { } impl pallet_membership::Config<Instance2> for Runtime { - type IsIdtyAllowedToClaimMembership = SmithsSubWot; - type IsIdtyAllowedToRenewMembership = SmithsSubWot; - type IsIdtyAllowedToRequestMembership = SmithsSubWot; + type CheckIdtyAllowedMembership = SmithsSubWot; type Event = Event; type IdtyId = IdtyIndex; type IdtyIdOf = common_runtime::providers::IdentityIndexOf<Self>;