Skip to content

Commit

Permalink
feat: cluster missing parameters added
Browse files Browse the repository at this point in the history
  • Loading branch information
yahortsaryk committed Nov 9, 2023
1 parent 21f5e5f commit 4a939e1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 23 deletions.
18 changes: 4 additions & 14 deletions pallets/ddc-clusters/src/cluster.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use crate::pallet::Error;
use codec::{Decode, Encode};
use ddc_primitives::ClusterId;
use frame_support::{pallet_prelude::*, parameter_types, BoundedVec};
use frame_support::{pallet_prelude::*, parameter_types};
use scale_info::TypeInfo;
use sp_runtime::Perbill;
use sp_staking::EraIndex;
use sp_std::vec::Vec;

parameter_types! {
pub MaxClusterParamsLen: u16 = 2048;
Expand All @@ -15,21 +14,18 @@ parameter_types! {
pub struct Cluster<AccountId> {
pub cluster_id: ClusterId,
pub manager_id: AccountId,
pub reserve_id: AccountId,
pub props: ClusterProps<AccountId>,
}

#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, PartialEq)]
pub struct ClusterProps<AccountId> {
// this is a temporal way of storing cluster parameters as a stringified json,
// should be replaced with specific properties for cluster
pub params: BoundedVec<u8, MaxClusterParamsLen>,
pub node_provider_auth_contract: AccountId,
}

// ClusterParams includes Governance non-sensetive parameters only
#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, PartialEq)]
pub struct ClusterParams<AccountId> {
pub params: Vec<u8>,
pub node_provider_auth_contract: AccountId,
}

Expand Down Expand Up @@ -57,16 +53,14 @@ impl<AccountId> Cluster<AccountId> {
pub fn new(
cluster_id: ClusterId,
manager_id: AccountId,
reserve_id: AccountId,
cluster_params: ClusterParams<AccountId>,
) -> Result<Cluster<AccountId>, ClusterError> {
Ok(Cluster {
cluster_id,
manager_id,
reserve_id,
props: ClusterProps {
params: match cluster_params.params.try_into() {
Ok(vec) => vec,
Err(_) => return Err(ClusterError::ClusterParamsExceedsLimit),
},
node_provider_auth_contract: cluster_params.node_provider_auth_contract,
},
})
Expand All @@ -77,10 +71,6 @@ impl<AccountId> Cluster<AccountId> {
cluster_params: ClusterParams<AccountId>,
) -> Result<(), ClusterError> {
self.props = ClusterProps {
params: match cluster_params.params.try_into() {
Ok(vec) => vec,
Err(_) => return Err(ClusterError::ClusterParamsExceedsLimit),
},
node_provider_auth_contract: cluster_params.node_provider_auth_contract,
};
Ok(())
Expand Down
26 changes: 17 additions & 9 deletions pallets/ddc-clusters/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,25 @@ pub mod pallet {
pub fn create_cluster(
origin: OriginFor<T>,
cluster_id: ClusterId,
cluster_manager_id: T::AccountId,
cluster_reserve_id: T::AccountId,
cluster_params: ClusterParams<T::AccountId>,
cluster_gov_params: ClusterGovParams<BalanceOf<T>>,
) -> DispatchResult {
let caller_id = ensure_signed(origin)?;
let cluster = Cluster::new(cluster_id.clone(), caller_id, cluster_params)
.map_err(|e: ClusterError| Into::<Error<T>>::into(ClusterError::from(e)))?;
ensure_root(origin)?; // requires Governance approval
let cluster = Cluster::new(
cluster_id.clone(),
cluster_manager_id,
cluster_reserve_id,
cluster_params,
)
.map_err(|e: ClusterError| Into::<Error<T>>::into(ClusterError::from(e)))?;
ensure!(!Clusters::<T>::contains_key(&cluster_id), Error::<T>::ClusterAlreadyExists);

Clusters::<T>::insert(cluster_id.clone(), cluster);
ClustersGovParams::<T>::insert(cluster_id.clone(), cluster_gov_params);
Self::deposit_event(Event::<T>::ClusterCreated { cluster_id });

Ok(())
}

Expand Down Expand Up @@ -220,17 +231,16 @@ pub mod pallet {
Ok(())
}

// Sets Governance sensetive parameters
// Requires Governance approval
#[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 =
ensure_root(origin)?; // requires Governance approval
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 });

Expand All @@ -253,8 +263,6 @@ pub mod pallet {
cluster_id: &ClusterId,
node_type: NodeType,
) -> Result<u128, ClusterVisitorError> {
// ensure!(ClustersNodes::<T>::contains_key(cluster_id),
// Error::<T>::ClusterDoesNotExist);
let cluster_gov_params = ClustersGovParams::<T>::try_get(cluster_id)
.map_err(|_| ClusterVisitorError::ClusterGovParamsNotSet)?;
match node_type {
Expand Down

0 comments on commit 4a939e1

Please sign in to comment.