Skip to content

Commit

Permalink
Merge pull request #82 from Cerebellum-Network/feature/cluster-manage…
Browse files Browse the repository at this point in the history
…rs-acl

Cluster managers ACL
  • Loading branch information
khssnv authored Sep 1, 2023
2 parents ea29a42 + a95e2f5 commit 3594d20
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- [D] New `pallet-ddc-validator` which implements DDC CDN nodes validation and rewarding. You can enable DDC validation providing `--enable-ddc-validation` argument. It will only work on the nodes with validation and offchain workers enabled as well.
- [D] Several calls for `pallet-ddc-staking` to distribute rewards.
- [D] DDC cluster managers access control list in `pallet-ddc-staking` managed by governance.

### Changed

Expand Down
18 changes: 18 additions & 0 deletions pallets/ddc-staking/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,22 @@ benchmarks! {
verify {
assert!(Ledger::<T>::contains_key(&new_controller));
}

allow_cluster_manager {
let new_cluster_manager = create_funded_user::<T>("cluster_manager", USER_SEED, 100);
let new_cluster_manager_lookup = T::Lookup::unlookup(new_cluster_manager.clone());
}: _(RawOrigin::Root, new_cluster_manager_lookup)
verify {
assert!(ClusterManagers::<T>::get().contains(&new_cluster_manager));
}

disallow_cluster_manager {
let new_cluster_manager = create_funded_user::<T>("cluster_manager", USER_SEED, 100);
let new_cluster_manager_lookup = T::Lookup::unlookup(new_cluster_manager.clone());
DdcStaking::<T>::allow_cluster_manager(RawOrigin::Root.into(), new_cluster_manager_lookup.clone())?;
assert!(ClusterManagers::<T>::get().contains(&new_cluster_manager));
}: _(RawOrigin::Root, new_cluster_manager_lookup)
verify {
assert!(!ClusterManagers::<T>::get().contains(&new_cluster_manager));
}
}
45 changes: 45 additions & 0 deletions pallets/ddc-staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,11 @@ pub mod pallet {
#[pallet::getter(fn pricing)]
pub type Pricing<T: Config> = StorageValue<_, u128>;

/// A list of accounts allowed to become cluster managers.
#[pallet::storage]
#[pallet::getter(fn cluster_managers)]
pub type ClusterManagers<T: Config> = StorageValue<_, Vec<T::AccountId>, ValueQuery>;

#[pallet::genesis_config]
pub struct GenesisConfig<T: Config> {
pub edges: Vec<(T::AccountId, T::AccountId, BalanceOf<T>, ClusterId)>,
Expand Down Expand Up @@ -861,6 +866,46 @@ pub mod pallet {
<Pricing<T>>::set(Some(price_per_byte));
Ok(())
}

/// Add a new account to the list of cluster managers.
///
/// RuntimeOrigin must be Root to call this function.
#[pallet::weight(T::WeightInfo::allow_cluster_manager())]
pub fn allow_cluster_manager(
origin: OriginFor<T>,
grantee: <T::Lookup as StaticLookup>::Source,
) -> DispatchResult {
ensure_root(origin)?;

let grantee = T::Lookup::lookup(grantee)?;
ClusterManagers::<T>::mutate(|grantees| {
if !grantees.contains(&grantee) {
grantees.push(grantee);
}
});

Ok(())
}

/// Remove an account from the list of cluster managers.
///
/// RuntimeOrigin must be Root to call this function.
#[pallet::weight(T::WeightInfo::disallow_cluster_manager())]
pub fn disallow_cluster_manager(
origin: OriginFor<T>,
revokee: <T::Lookup as StaticLookup>::Source,
) -> DispatchResult {
ensure_root(origin)?;

let revokee = T::Lookup::lookup(revokee)?;
ClusterManagers::<T>::mutate(|grantees| {
if let Some(pos) = grantees.iter().position(|g| g == &revokee) {
grantees.remove(pos);
}
});

Ok(())
}
}

impl<T: Config> Pallet<T> {
Expand Down
33 changes: 32 additions & 1 deletion pallets/ddc-staking/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//! Tests for the module.

use super::{mock::*, *};
use frame_support::{assert_noop, assert_ok, traits::ReservableCurrency};
use frame_support::{
assert_noop, assert_ok, assert_storage_noop, error::BadOrigin, traits::ReservableCurrency,
};
use pallet_balances::Error as BalancesError;

pub const BLOCK_TIME: u64 = 1000;
Expand Down Expand Up @@ -176,3 +178,32 @@ fn staking_should_work() {
assert_eq!(DdcStaking::edges(3), None);
});
}

#[test]
fn cluster_managers_list_can_be_managed_by_governance_only() {
ExtBuilder::default().build_and_execute(|| {
// Governance can allow an account to become cluster manager.
assert_ok!(DdcStaking::allow_cluster_manager(RuntimeOrigin::root(), 1));

// Repeat call does nothing.
assert_storage_noop!(assert_ok!(DdcStaking::allow_cluster_manager(
RuntimeOrigin::root(),
1,
)));

// Non-governance can't allow an account to become a cluster manager.
assert_noop!(DdcStaking::allow_cluster_manager(RuntimeOrigin::signed(1), 2), BadOrigin);

// Non-governance can't disallow an account to become a cluster manager.
assert_noop!(DdcStaking::disallow_cluster_manager(RuntimeOrigin::signed(1), 1), BadOrigin);

// Governance can disallow an account to become a cluster manager.
assert_ok!(DdcStaking::disallow_cluster_manager(RuntimeOrigin::root(), 1));

// Repeat call does nothing.
assert_storage_noop!(assert_ok!(DdcStaking::disallow_cluster_manager(
RuntimeOrigin::root(),
1,
)));
});
}
28 changes: 27 additions & 1 deletion pallets/ddc-staking/src/weights.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Autogenerated weights for pallet_ddc_staking
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
//! DATE: 2023-07-04, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! DATE: 2023-09-01, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! HOSTNAME: `e14`, CPU: `11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz`
//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024

Expand Down Expand Up @@ -35,6 +35,8 @@ pub trait WeightInfo {
fn serve() -> Weight;
fn chill() -> Weight;
fn set_controller() -> Weight;
fn allow_cluster_manager() -> Weight;
fn disallow_cluster_manager() -> Weight;
}

/// Weights for pallet_ddc_staking using the Substrate node and recommended hardware.
Expand Down Expand Up @@ -103,6 +105,18 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
.saturating_add(T::DbWeight::get().reads(3 as u64))
.saturating_add(T::DbWeight::get().writes(3 as u64))
}
// Storage: DdcStaking ClusterManagers (r:1 w:1)
fn allow_cluster_manager() -> Weight {
Weight::from_ref_time(11_727_000 as u64)
.saturating_add(T::DbWeight::get().reads(1 as u64))
.saturating_add(T::DbWeight::get().writes(1 as u64))
}
// Storage: DdcStaking ClusterManagers (r:1 w:1)
fn disallow_cluster_manager() -> Weight {
Weight::from_ref_time(18_006_000 as u64)
.saturating_add(T::DbWeight::get().reads(1 as u64))
.saturating_add(T::DbWeight::get().writes(1 as u64))
}
}

// For backwards compatibility and tests
Expand Down Expand Up @@ -170,4 +184,16 @@ impl WeightInfo for () {
.saturating_add(RocksDbWeight::get().reads(3 as u64))
.saturating_add(RocksDbWeight::get().writes(3 as u64))
}
// Storage: DdcStaking ClusterManagers (r:1 w:1)
fn allow_cluster_manager() -> Weight {
Weight::from_ref_time(11_727_000 as u64)
.saturating_add(RocksDbWeight::get().reads(1 as u64))
.saturating_add(RocksDbWeight::get().writes(1 as u64))
}
// Storage: DdcStaking ClusterManagers (r:1 w:1)
fn disallow_cluster_manager() -> Weight {
Weight::from_ref_time(18_006_000 as u64)
.saturating_add(RocksDbWeight::get().reads(1 as u64))
.saturating_add(RocksDbWeight::get().writes(1 as u64))
}
}
2 changes: 1 addition & 1 deletion runtime/cere-dev/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// and set impl_version to 0. If only runtime
// implementation changes and behavior does not, then leave spec_version as
// is and increment impl_version.
spec_version: 48002,
spec_version: 48003,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 5,
Expand Down

0 comments on commit 3594d20

Please sign in to comment.