Skip to content

Commit

Permalink
feat: root curator (#56)
Browse files Browse the repository at this point in the history
Closes CHAIN-41
  • Loading branch information
devwckd authored Jan 7, 2025
1 parent c6325ef commit fb6a0c7
Show file tree
Hide file tree
Showing 7 changed files with 233 additions and 117 deletions.
28 changes: 14 additions & 14 deletions pallets/emission0/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
//! Autogenerated weights for `pallet_emission0`
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 43.0.0
//! DATE: 2025-01-03, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! DATE: 2025-01-06, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! WORST CASE MAP SIZE: `1000000`
//! HOSTNAME: `Luizs-MacBook-Pro.local`, CPU: `<UNKNOWN>`
//! HOSTNAME: `MacBook-Pro-de-Joao.local`, CPU: `<UNKNOWN>`
//! WASM-EXECUTION: `Compiled`, CHAIN: `Some("dev")`, DB CACHE: `1024`
// Executed Command:
Expand Down Expand Up @@ -64,8 +64,8 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
// Proof Size summary in bytes:
// Measured: `339`
// Estimated: `7638`
// Minimum execution time: 24_000_000 picoseconds.
Weight::from_parts(26_000_000, 7638)
// Minimum execution time: 33_000_000 picoseconds.
Weight::from_parts(34_000_000, 7638)
.saturating_add(T::DbWeight::get().reads(8_u64))
.saturating_add(T::DbWeight::get().writes(1_u64))
}
Expand All @@ -77,8 +77,8 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
// Proof Size summary in bytes:
// Measured: `168`
// Estimated: `7638`
// Minimum execution time: 12_000_000 picoseconds.
Weight::from_parts(12_000_000, 7638)
// Minimum execution time: 17_000_000 picoseconds.
Weight::from_parts(18_000_000, 7638)
.saturating_add(T::DbWeight::get().reads(2_u64))
.saturating_add(T::DbWeight::get().writes(1_u64))
}
Expand All @@ -88,8 +88,8 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
// Proof Size summary in bytes:
// Measured: `199`
// Estimated: `3529`
// Minimum execution time: 7_000_000 picoseconds.
Weight::from_parts(8_000_000, 3529)
// Minimum execution time: 11_000_000 picoseconds.
Weight::from_parts(11_000_000, 3529)
.saturating_add(T::DbWeight::get().reads(1_u64))
.saturating_add(T::DbWeight::get().writes(1_u64))
}
Expand All @@ -115,8 +115,8 @@ impl WeightInfo for () {
// Proof Size summary in bytes:
// Measured: `339`
// Estimated: `7638`
// Minimum execution time: 24_000_000 picoseconds.
Weight::from_parts(26_000_000, 7638)
// Minimum execution time: 33_000_000 picoseconds.
Weight::from_parts(34_000_000, 7638)
.saturating_add(RocksDbWeight::get().reads(8_u64))
.saturating_add(RocksDbWeight::get().writes(1_u64))
}
Expand All @@ -128,8 +128,8 @@ impl WeightInfo for () {
// Proof Size summary in bytes:
// Measured: `168`
// Estimated: `7638`
// Minimum execution time: 12_000_000 picoseconds.
Weight::from_parts(12_000_000, 7638)
// Minimum execution time: 17_000_000 picoseconds.
Weight::from_parts(18_000_000, 7638)
.saturating_add(RocksDbWeight::get().reads(2_u64))
.saturating_add(RocksDbWeight::get().writes(1_u64))
}
Expand All @@ -139,8 +139,8 @@ impl WeightInfo for () {
// Proof Size summary in bytes:
// Measured: `199`
// Estimated: `3529`
// Minimum execution time: 7_000_000 picoseconds.
Weight::from_parts(8_000_000, 3529)
// Minimum execution time: 11_000_000 picoseconds.
Weight::from_parts(11_000_000, 3529)
.saturating_add(RocksDbWeight::get().reads(1_u64))
.saturating_add(RocksDbWeight::get().writes(1_u64))
}
Expand Down
9 changes: 9 additions & 0 deletions pallets/governance/src/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,13 @@ benchmarks! {
let _ = <T as crate::Config>::Currency::deposit_creating(&module_key, cost);

}: _(RawOrigin::Signed(module_key.clone()), Percent::from_parts(40), Percent::from_parts(40), data)

set_root_curator {
let module_key: T::AccountId = account("ModuleKey", 0, 2);
}: _(RawOrigin::Root, module_key)

remove_root_curator {
}: _(RawOrigin::Root)


}
30 changes: 28 additions & 2 deletions pallets/governance/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use polkadot_sdk::sp_std::vec::Vec;
#[frame::pallet]
pub mod pallet {
#![allow(clippy::too_many_arguments)]
use frame::prelude::ensure_signed_or_root;
use proposal::GlobalParamsData;
use weights::WeightInfo;

Expand Down Expand Up @@ -71,6 +72,9 @@ pub mod pallet {
#[pallet::storage]
pub type Whitelist<T: Config> = StorageMap<_, Identity, AccountIdOf<T>, ()>;

#[pallet::storage]
pub type RootCurator<T: Config> = StorageValue<_, AccountIdOf<T>, OptionQuery>;

#[pallet::storage]
pub type Curators<T: Config> = StorageMap<_, Identity, AccountIdOf<T>, ()>;

Expand Down Expand Up @@ -160,14 +164,20 @@ pub mod pallet {
#[pallet::call_index(0)]
#[pallet::weight((<T as Config>::WeightInfo::add_curator(), DispatchClass::Normal, Pays::Yes))]
pub fn add_curator(origin: OriginFor<T>, key: AccountIdOf<T>) -> DispatchResult {
ensure_root(origin)?;
if ensure_signed_or_root(origin.clone())?.is_some() {
roles::ensure_root_curator::<T>(origin)?;
}

roles::manage_role::<T, Curators<T>>(key, true, Error::<T>::AlreadyCurator)
}

#[pallet::call_index(1)]
#[pallet::weight((<T as Config>::WeightInfo::remove_curator(), DispatchClass::Normal, Pays::Yes))]
pub fn remove_curator(origin: OriginFor<T>, key: AccountIdOf<T>) -> DispatchResult {
ensure_root(origin)?;
if ensure_signed_or_root(origin.clone())?.is_some() {
roles::ensure_root_curator::<T>(origin)?;
}

roles::manage_role::<T, Curators<T>>(key, false, Error::<T>::NotAllocator)
}

Expand Down Expand Up @@ -322,6 +332,20 @@ pub mod pallet {
data,
)
}

#[pallet::call_index(18)]
#[pallet::weight((<T as Config>::WeightInfo::set_root_curator(), DispatchClass::Normal, Pays::No))]
pub fn set_root_curator(origin: OriginFor<T>, key: AccountIdOf<T>) -> DispatchResult {
ensure_root(origin)?;
roles::set_root_curator::<T>(key)
}

#[pallet::call_index(19)]
#[pallet::weight((<T as Config>::WeightInfo::remove_root_curator(), DispatchClass::Normal, Pays::No))]
pub fn remove_root_curator(origin: OriginFor<T>) -> DispatchResult {
ensure_root(origin)?;
roles::remove_root_curator::<T>()
}
}

#[pallet::event]
Expand Down Expand Up @@ -408,6 +432,8 @@ pub mod pallet {
NotEnoughBalanceToApply,
/// The operation can only be performed by the curator.
NotCurator,
/// The operation can only be performed by the root curator.
NotRootCurator,
/// The application with the given ID was not found.
ApplicationNotFound,
/// The account is already whitelisted and cannot be added again.
Expand Down
21 changes: 20 additions & 1 deletion pallets/governance/src/roles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ pub(super) fn manage_role<T: Config, M: StorageMap<AccountIdOf<T>, ()>>(
Ok(())
}

pub fn set_root_curator<T: Config>(key: AccountIdOf<T>) -> DispatchResult {
crate::RootCurator::<T>::set(Some(key));
Ok(())
}

pub fn remove_root_curator<T: Config>() -> DispatchResult {
crate::RootCurator::<T>::set(None);
Ok(())
}

pub fn penalize_agent<T: Config>(agent_key: AccountIdOf<T>, percentage: u8) -> DispatchResult {
let percentage = Percent::from_parts(percentage);
if percentage > T::MaxPenaltyPercentage::get() {
Expand All @@ -40,9 +50,18 @@ pub fn penalize_agent<T: Config>(agent_key: AccountIdOf<T>, percentage: u8) -> D
Ok(())
}

pub fn ensure_root_curator<T: Config>(origin: OriginFor<T>) -> DispatchResult {
let key: AccountIdOf<T> = ensure_signed(origin)?;
if crate::RootCurator::<T>::get() != Some(key) {
return Err(Error::<T>::NotRootCurator.into());
}

Ok(())
}

pub fn ensure_curator<T: Config>(origin: OriginFor<T>) -> DispatchResult {
let key: AccountIdOf<T> = ensure_signed(origin)?;
if !crate::Curators::<T>::contains_key(key) {
if !crate::Curators::<T>::contains_key(&key) && crate::RootCurator::<T>::get() != Some(key) {
return Err(Error::<T>::NotCurator.into());
}

Expand Down
Loading

0 comments on commit fb6a0c7

Please sign in to comment.