Skip to content

Commit

Permalink
Merge pull request #118 from Cerebellum-Network/feature/cluster-with-…
Browse files Browse the repository at this point in the history
…gov-params

Feature/cluster with gov params
  • Loading branch information
Raid5594 authored Nov 6, 2023
2 parents 75dee3f + 7b3c665 commit d6db558
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
22 changes: 22 additions & 0 deletions pallets/ddc-clusters/src/cluster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use codec::{Decode, Encode};
use ddc_primitives::ClusterId;
use frame_support::{pallet_prelude::*, parameter_types, BoundedVec};
use scale_info::TypeInfo;
use sp_runtime::Perbill;
use sp_staking::EraIndex;
use sp_std::vec::Vec;

parameter_types! {
Expand Down Expand Up @@ -31,6 +33,26 @@ pub struct ClusterParams<AccountId> {
pub node_provider_auth_contract: AccountId,
}

// ClusterGovParams includes Governance sensetive parameters
#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, PartialEq)]
pub struct ClusterGovParams<Balance> {
pub treasury_share: Perbill,
pub validators_share: Perbill,
pub cluster_reserve_share: Perbill,
#[codec(compact)]
pub cdn_bond_size: Balance,
pub cdn_chill_delay: EraIndex,
pub cdn_unbonding_delay: EraIndex,
#[codec(compact)]
pub storage_bond_size: Balance,
pub storage_chill_delay: EraIndex,
pub storage_unbonding_delay: EraIndex,
pub unit_per_mb_stored: u128,
pub unit_per_mb_streamed: u128,
pub unit_per_put_request: u128,
pub unit_per_get_request: u128,
}

impl<AccountId> Cluster<AccountId> {
pub fn new(
cluster_id: ClusterId,
Expand Down
35 changes: 33 additions & 2 deletions pallets/ddc-clusters/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@
#![feature(is_some_and)] // ToDo: delete at rustc > 1.70

use crate::{
cluster::{Cluster, ClusterError, ClusterParams},
cluster::{Cluster, ClusterError, ClusterGovParams, ClusterParams},
node_provider_auth::{NodeProviderAuthContract, NodeProviderAuthContractError},
};
use ddc_primitives::{ClusterId, NodePubKey};
use ddc_traits::{
cluster::{ClusterVisitor, ClusterVisitorError},
staking::{StakingVisitor, StakingVisitorError},
};
use frame_support::pallet_prelude::*;
use frame_support::{
pallet_prelude::*,
traits::{Currency, LockableCurrency},
};
use frame_system::pallet_prelude::*;
pub use pallet::*;
use pallet_ddc_nodes::{NodeRepository, NodeTrait};
Expand All @@ -33,6 +36,10 @@ use sp_std::prelude::*;
mod cluster;
mod node_provider_auth;

/// The balance type of this pallet.
pub type BalanceOf<T> =
<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;

#[frame_support::pallet]
pub mod pallet {
use super::*;
Expand All @@ -48,6 +55,7 @@ pub mod pallet {
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
type NodeRepository: NodeRepository<Self>; // todo: get rid of tight coupling with nodes-pallet
type StakingVisitor: StakingVisitor<Self>;
type Currency: LockableCurrency<Self::AccountId, Moment = Self::BlockNumber>;
}

#[pallet::event]
Expand All @@ -57,6 +65,7 @@ pub mod pallet {
ClusterNodeAdded { cluster_id: ClusterId, node_pub_key: NodePubKey },
ClusterNodeRemoved { cluster_id: ClusterId, node_pub_key: NodePubKey },
ClusterParamsSet { cluster_id: ClusterId },
ClusterGovParamsSet { cluster_id: ClusterId },
}

#[pallet::error]
Expand All @@ -82,6 +91,11 @@ pub mod pallet {
pub type Clusters<T: Config> =
StorageMap<_, Blake2_128Concat, ClusterId, Cluster<T::AccountId>>;

#[pallet::storage]
#[pallet::getter(fn clusters_gov_params)]
pub type ClustersGovParams<T: Config> =
StorageMap<_, Twox64Concat, ClusterId, ClusterGovParams<BalanceOf<T>>>;

#[pallet::storage]
#[pallet::getter(fn clusters_nodes)]
pub type ClustersNodes<T: Config> = StorageDoubleMap<
Expand Down Expand Up @@ -204,6 +218,23 @@ pub mod pallet {

Ok(())
}

// Sets Governance sensetive parameters
#[pallet::weight(10_000)]
pub fn set_cluster_gov_params(
origin: OriginFor<T>,
cluster_id: ClusterId,
cluster_gov_params: ClusterGovParams<BalanceOf<T>>,
) -> DispatchResult {
let caller_id = ensure_signed(origin)?;
let cluster =
Clusters::<T>::try_get(&cluster_id).map_err(|_| Error::<T>::ClusterDoesNotExist)?;
ensure!(cluster.manager_id == caller_id, Error::<T>::OnlyClusterManager);
ClustersGovParams::<T>::insert(cluster_id.clone(), cluster_gov_params);
Self::deposit_event(Event::<T>::ClusterGovParamsSet { cluster_id });

Ok(())
}
}

impl<T: Config> ClusterVisitor<T> for Pallet<T> {
Expand Down
1 change: 1 addition & 0 deletions runtime/cere-dev/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1357,6 +1357,7 @@ impl pallet_ddc_clusters::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type NodeRepository = pallet_ddc_nodes::Pallet<Runtime>;
type StakingVisitor = pallet_ddc_staking::Pallet<Runtime>;
type Currency = Balances;
}

construct_runtime!(
Expand Down

0 comments on commit d6db558

Please sign in to comment.