Skip to content
Snippets Groups Projects
Commit 2a46c45b authored by Hugo Trentesaux's avatar Hugo Trentesaux
Browse files

wip merge membership traits

parent f0dbc0b2
No related branches found
No related tags found
No related merge requests found
......@@ -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(())
}
}
......
......@@ -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>;
......
......@@ -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);
......
......@@ -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;
......
......@@ -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
}
}
pub trait IsIdtyAllowedToRenewMembership<IdtyId> {
fn is_idty_allowed_to_renew_membership(idty_id: &IdtyId) -> bool;
impl<IdtyId> CheckIdtyAllowedMembership<IdtyId> for () {
fn check_idty_allowed_to_claim_membership(_: &IdtyId) -> Result<(), DispatchError> {
Ok(())
}
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(())
}
}
......
......@@ -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>;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment