Skip to content

Commit

Permalink
upgrade pallet-privilege to 0.9.16
Browse files Browse the repository at this point in the history
  • Loading branch information
hussein-aitlahcen committed Feb 4, 2022
1 parent 7ad33f5 commit c223774
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
7 changes: 1 addition & 6 deletions frame/composable-traits/src/privilege.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,7 @@ pub trait MutatePrivilege: InspectPrivilege {
fn revoke(account_id: &Self::AccountId, privilege: Privilege) -> DispatchResult;
}

#[repr(transparent)]
#[derive(Default, Encode, Decode, MaxEncodedLen, TypeInfo)]
/// A privileged group.
pub struct PrivilegedGroupSet<T>(pub T);

pub type PrivilegedGroupOf<T> = PrivilegedGroupSet<<T as InspectPrivilegeGroup>::Group>;
pub type PrivilegedGroupOf<T> = <T as InspectPrivilegeGroup>::Group;

/// An privilege group, a set of privileged accounts.
pub trait InspectPrivilegeGroup {
Expand Down
33 changes: 19 additions & 14 deletions frame/privilege/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub mod pallet {
math::WrappingNext,
privilege::{
InspectPrivilege, InspectPrivilegeGroup, MutatePrivilege, MutatePrivilegeGroup,
Privilege, PrivilegedGroupOf, PrivilegedGroupSet,
Privilege, PrivilegedGroupOf,
},
};
use frame_support::{pallet_prelude::*, PalletId};
Expand Down Expand Up @@ -132,8 +132,13 @@ pub mod pallet {
// FIXME: Temporary fix to get CI to pass, separate PRs will be made per pallet to refactor to
// use OptionQuery instead
#[allow(clippy::disallowed_type)]
pub type GroupMembers<T: Config> =
StorageMap<_, Blake2_128Concat, T::GroupId, PrivilegedGroupOf<Pallet<T>>, ValueQuery>;
pub type GroupMembers<T: Config> = StorageMap<
_,
Blake2_128Concat,
T::GroupId,
BoundedVec<<T as Config>::AccountId, T::MaxMember>,
ValueQuery,
>;

#[pallet::storage]
#[pallet::getter(fn group_id_last)]
Expand Down Expand Up @@ -195,7 +200,7 @@ pub mod pallet {
impl<T: Config> InspectPrivilegeGroup for Pallet<T> {
type AccountId = AccountIdOf<T>;
type GroupId = T::GroupId;
type Group = PrivilegedGroupSet<BoundedVec<Self::AccountId, T::MaxMember>>;
type Group = BoundedVec<Self::AccountId, T::MaxMember>;

fn privilege(group_id: Self::GroupId) -> Result<Privilege, DispatchError> {
GroupPrivileges::<T>::try_get(group_id).map_err(|_| Error::<T>::GroupNotFound.into())
Expand All @@ -215,7 +220,7 @@ pub mod pallet {

impl<T: Config> MutatePrivilegeGroup for Pallet<T> {
fn create(
PrivilegedGroupSet(group): PrivilegedGroupOf<Self>,
group: PrivilegedGroupOf<Self>,
privilege: Privilege,
) -> Result<Self::GroupId, DispatchError> {
ensure!(GroupCount::<T>::get() < T::MaxGroup::get(), Error::<T>::TooManyGroup);
Expand All @@ -231,9 +236,14 @@ pub mod pallet {

GroupCount::<T>::mutate(|x| *x += 1);
GroupPrivileges::<T>::insert(group_id, privilege);
GroupMembers::<T>::insert(group_id, PrivilegedGroupSet(group));
// NOTE(hussein-aitlahcen): we don't know if it's correctly sorted at creation, hence we promote member per member.
GroupMembers::<T>::insert(group_id, BoundedVec::with_bounded_capacity(group.len()));
Self::deposit_event(Event::GroupCreated { group_id, privilege });

for member in group {
<Self as MutatePrivilegeGroup>::promote(group_id, &member)?;
}

Ok(group_id)
})
}
Expand All @@ -255,17 +265,12 @@ pub mod pallet {
fn promote(group_id: Self::GroupId, account_id: &Self::AccountId) -> DispatchResult {
let privilege = Self::privilege(group_id)?;
ensure!(Self::has_privilege(account_id, privilege), Error::<T>::GroupPrivilegeNotHeld);
GroupMembers::<T>::try_mutate(group_id, |PrivilegedGroupSet(group)| {
ensure!(group.len() < T::MaxMember::get() as usize, Error::<T>::TooManyMember);
/* NOTE(hussein-aitlahcen):
No hashset on-chain, is there a better way?
This shouldn't happen so much, probably only done by governance.
*/
GroupMembers::<T>::try_mutate(group_id, |group| {
// Match to make it clear that in case of Ok => already present
match group.binary_search(account_id) {
Ok(_) => Err(Error::<T>::AlreadyGroupMember.into()),
Err(i) => {
group.insert(i, *account_id);
group.try_insert(i, *account_id).map_err(|_| Error::<T>::TooManyMember)?;
Self::deposit_event(Event::GroupMemberAdded {
group_id,
account_id: *account_id,
Expand All @@ -282,7 +287,7 @@ pub mod pallet {
Currently it's not the case.
*/
fn revoke(group_id: Self::GroupId, account_id: &Self::AccountId) -> DispatchResult {
GroupMembers::<T>::try_mutate(group_id, |PrivilegedGroupSet(group)| {
GroupMembers::<T>::try_mutate(group_id, |group| {
/* NOTE(hussein-aitlahcen):
No hashset on-chain, is there a better way?
This shouldn't happen so much, probably only done by governance.
Expand Down

0 comments on commit c223774

Please sign in to comment.