From 4a939e1dce242037bda7b5c3b087db20a1ecae4b Mon Sep 17 00:00:00 2001 From: yahortsaryk Date: Thu, 9 Nov 2023 01:40:19 +0100 Subject: [PATCH] feat: cluster missing parameters added --- pallets/ddc-clusters/src/cluster.rs | 18 ++++-------------- pallets/ddc-clusters/src/lib.rs | 26 +++++++++++++++++--------- 2 files changed, 21 insertions(+), 23 deletions(-) diff --git a/pallets/ddc-clusters/src/cluster.rs b/pallets/ddc-clusters/src/cluster.rs index 7274fbf16..fa50f2411 100644 --- a/pallets/ddc-clusters/src/cluster.rs +++ b/pallets/ddc-clusters/src/cluster.rs @@ -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; @@ -15,21 +14,18 @@ parameter_types! { pub struct Cluster { pub cluster_id: ClusterId, pub manager_id: AccountId, + pub reserve_id: AccountId, pub props: ClusterProps, } #[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, PartialEq)] pub struct ClusterProps { - // this is a temporal way of storing cluster parameters as a stringified json, - // should be replaced with specific properties for cluster - pub params: BoundedVec, pub node_provider_auth_contract: AccountId, } // ClusterParams includes Governance non-sensetive parameters only #[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, PartialEq)] pub struct ClusterParams { - pub params: Vec, pub node_provider_auth_contract: AccountId, } @@ -57,16 +53,14 @@ impl Cluster { pub fn new( cluster_id: ClusterId, manager_id: AccountId, + reserve_id: AccountId, cluster_params: ClusterParams, ) -> Result, 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, }, }) @@ -77,10 +71,6 @@ impl Cluster { cluster_params: ClusterParams, ) -> 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(()) diff --git a/pallets/ddc-clusters/src/lib.rs b/pallets/ddc-clusters/src/lib.rs index 6a7c0549b..b927958e1 100644 --- a/pallets/ddc-clusters/src/lib.rs +++ b/pallets/ddc-clusters/src/lib.rs @@ -118,14 +118,25 @@ pub mod pallet { pub fn create_cluster( origin: OriginFor, cluster_id: ClusterId, + cluster_manager_id: T::AccountId, + cluster_reserve_id: T::AccountId, cluster_params: ClusterParams, + cluster_gov_params: ClusterGovParams>, ) -> DispatchResult { - let caller_id = ensure_signed(origin)?; - let cluster = Cluster::new(cluster_id.clone(), caller_id, cluster_params) - .map_err(|e: ClusterError| Into::>::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::>::into(ClusterError::from(e)))?; ensure!(!Clusters::::contains_key(&cluster_id), Error::::ClusterAlreadyExists); + Clusters::::insert(cluster_id.clone(), cluster); + ClustersGovParams::::insert(cluster_id.clone(), cluster_gov_params); Self::deposit_event(Event::::ClusterCreated { cluster_id }); + Ok(()) } @@ -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, cluster_id: ClusterId, cluster_gov_params: ClusterGovParams>, ) -> DispatchResult { - let caller_id = ensure_signed(origin)?; - let cluster = + ensure_root(origin)?; // requires Governance approval + let _cluster = Clusters::::try_get(&cluster_id).map_err(|_| Error::::ClusterDoesNotExist)?; - ensure!(cluster.manager_id == caller_id, Error::::OnlyClusterManager); ClustersGovParams::::insert(cluster_id.clone(), cluster_gov_params); Self::deposit_event(Event::::ClusterGovParamsSet { cluster_id }); @@ -253,8 +263,6 @@ pub mod pallet { cluster_id: &ClusterId, node_type: NodeType, ) -> Result { - // ensure!(ClustersNodes::::contains_key(cluster_id), - // Error::::ClusterDoesNotExist); let cluster_gov_params = ClustersGovParams::::try_get(cluster_id) .map_err(|_| ClusterVisitorError::ClusterGovParamsNotSet)?; match node_type {