From cef114c9e93685e76b813fafb8be61bcf4be8839 Mon Sep 17 00:00:00 2001 From: Raid Ateir Date: Mon, 6 Nov 2023 18:45:49 +0100 Subject: [PATCH 1/3] extend clusters with cluster gov params --- pallets/ddc-clusters/src/cluster.rs | 39 ++++++++++++++++++++++++--- pallets/ddc-clusters/src/lib.rs | 41 +++++++++++++++++++++++++---- runtime/cere-dev/src/lib.rs | 1 + 3 files changed, 73 insertions(+), 8 deletions(-) diff --git a/pallets/ddc-clusters/src/cluster.rs b/pallets/ddc-clusters/src/cluster.rs index 862c42ddf..862124bb4 100644 --- a/pallets/ddc-clusters/src/cluster.rs +++ b/pallets/ddc-clusters/src/cluster.rs @@ -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! { @@ -10,10 +12,11 @@ parameter_types! { } #[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, PartialEq)] -pub struct Cluster { +pub struct Cluster { pub cluster_id: ClusterId, pub manager_id: AccountId, pub props: ClusterProps, + pub gov_params: ClusterGovParams, } #[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, PartialEq)] @@ -31,12 +34,33 @@ pub struct ClusterParams { pub node_provider_auth_contract: AccountId, } -impl Cluster { +// ClusterGovParams includes Governance sensetive parameters +#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, PartialEq)] +pub struct ClusterGovParams { + pub treasury_share: Perbill, + pub validators_share: Perbill, + pub cluster_reserve_share: Perbill, + #[codec(compact)] + pub edge_bond_size: Balance, + pub edge_chill_delay: EraIndex, + pub edge_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 Cluster { pub fn new( cluster_id: ClusterId, manager_id: AccountId, cluster_params: ClusterParams, - ) -> Result, ClusterError> { + gov_params: ClusterGovParams, + ) -> Result, ClusterError> { Ok(Cluster { cluster_id, manager_id, @@ -47,6 +71,7 @@ impl Cluster { }, node_provider_auth_contract: cluster_params.node_provider_auth_contract, }, + gov_params, }) } @@ -63,6 +88,14 @@ impl Cluster { }; Ok(()) } + + pub fn set_gov_params( + &mut self, + cluster_gov_params: ClusterGovParams, + ) -> Result<(), ClusterError> { + self.gov_params = cluster_gov_params; + Ok(()) + } } pub enum ClusterError { diff --git a/pallets/ddc-clusters/src/lib.rs b/pallets/ddc-clusters/src/lib.rs index 2d389b431..b2aed6706 100644 --- a/pallets/ddc-clusters/src/lib.rs +++ b/pallets/ddc-clusters/src/lib.rs @@ -16,7 +16,7 @@ #![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}; @@ -24,7 +24,10 @@ 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}; @@ -33,6 +36,10 @@ use sp_std::prelude::*; mod cluster; mod node_provider_auth; +/// The balance type of this pallet. +pub type BalanceOf = + <::Currency as Currency<::AccountId>>::Balance; + #[frame_support::pallet] pub mod pallet { use super::*; @@ -48,6 +55,7 @@ pub mod pallet { type RuntimeEvent: From> + IsType<::RuntimeEvent>; type NodeRepository: NodeRepository; // todo: get rid of tight coupling with nodes-pallet type StakingVisitor: StakingVisitor; + type Currency: LockableCurrency; } #[pallet::event] @@ -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] @@ -80,7 +89,7 @@ pub mod pallet { #[pallet::storage] #[pallet::getter(fn clusters)] pub type Clusters = - StorageMap<_, Blake2_128Concat, ClusterId, Cluster>; + StorageMap<_, Blake2_128Concat, ClusterId, Cluster>>; #[pallet::storage] #[pallet::getter(fn clusters_nodes)] @@ -104,10 +113,12 @@ pub mod pallet { origin: OriginFor, cluster_id: ClusterId, 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)))?; + let cluster = + Cluster::new(cluster_id.clone(), caller_id, cluster_params, cluster_gov_params) + .map_err(|e: ClusterError| Into::>::into(ClusterError::from(e)))?; ensure!(!Clusters::::contains_key(&cluster_id), Error::::ClusterAlreadyExists); Clusters::::insert(cluster_id.clone(), cluster); Self::deposit_event(Event::::ClusterCreated { cluster_id }); @@ -204,6 +215,26 @@ pub mod pallet { Ok(()) } + + // Sets Governance sensetive parameters + #[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 mut cluster = + Clusters::::try_get(&cluster_id).map_err(|_| Error::::ClusterDoesNotExist)?; + ensure!(cluster.manager_id == caller_id, Error::::OnlyClusterManager); + cluster + .set_gov_params(cluster_gov_params) + .map_err(|e: ClusterError| Into::>::into(ClusterError::from(e)))?; + Clusters::::insert(cluster_id.clone(), cluster); + Self::deposit_event(Event::::ClusterGovParamsSet { cluster_id }); + + Ok(()) + } } impl ClusterVisitor for Pallet { diff --git a/runtime/cere-dev/src/lib.rs b/runtime/cere-dev/src/lib.rs index 0637375e6..7d678d1b6 100644 --- a/runtime/cere-dev/src/lib.rs +++ b/runtime/cere-dev/src/lib.rs @@ -1373,6 +1373,7 @@ impl pallet_ddc_clusters::Config for Runtime { type RuntimeEvent = RuntimeEvent; type NodeRepository = pallet_ddc_nodes::Pallet; type StakingVisitor = pallet_ddc_staking::Pallet; + type Currency = Balances; } construct_runtime!( From 491216649e7c34d1db04d6c1b5d80b3438faf3d4 Mon Sep 17 00:00:00 2001 From: Raid Ateir Date: Mon, 6 Nov 2023 19:01:38 +0100 Subject: [PATCH 2/3] add separate mapping for cluster gov params --- pallets/ddc-clusters/src/cluster.rs | 17 +++-------------- pallets/ddc-clusters/src/lib.rs | 20 ++++++++++---------- 2 files changed, 13 insertions(+), 24 deletions(-) diff --git a/pallets/ddc-clusters/src/cluster.rs b/pallets/ddc-clusters/src/cluster.rs index 862124bb4..12aaeda0d 100644 --- a/pallets/ddc-clusters/src/cluster.rs +++ b/pallets/ddc-clusters/src/cluster.rs @@ -12,11 +12,10 @@ parameter_types! { } #[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, PartialEq)] -pub struct Cluster { +pub struct Cluster { pub cluster_id: ClusterId, pub manager_id: AccountId, pub props: ClusterProps, - pub gov_params: ClusterGovParams, } #[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, PartialEq)] @@ -54,13 +53,12 @@ pub struct ClusterGovParams { pub unit_per_get_request: u128, } -impl Cluster { +impl Cluster { pub fn new( cluster_id: ClusterId, manager_id: AccountId, cluster_params: ClusterParams, - gov_params: ClusterGovParams, - ) -> Result, ClusterError> { + ) -> Result, ClusterError> { Ok(Cluster { cluster_id, manager_id, @@ -71,7 +69,6 @@ impl Cluster { }, node_provider_auth_contract: cluster_params.node_provider_auth_contract, }, - gov_params, }) } @@ -88,14 +85,6 @@ impl Cluster { }; Ok(()) } - - pub fn set_gov_params( - &mut self, - cluster_gov_params: ClusterGovParams, - ) -> Result<(), ClusterError> { - self.gov_params = cluster_gov_params; - Ok(()) - } } pub enum ClusterError { diff --git a/pallets/ddc-clusters/src/lib.rs b/pallets/ddc-clusters/src/lib.rs index b2aed6706..59ce7fb52 100644 --- a/pallets/ddc-clusters/src/lib.rs +++ b/pallets/ddc-clusters/src/lib.rs @@ -89,7 +89,12 @@ pub mod pallet { #[pallet::storage] #[pallet::getter(fn clusters)] pub type Clusters = - StorageMap<_, Blake2_128Concat, ClusterId, Cluster>>; + StorageMap<_, Blake2_128Concat, ClusterId, Cluster>; + + #[pallet::storage] + #[pallet::getter(fn clusters_gov_params)] + pub type ClustersGovParams = + StorageMap<_, Twox64Concat, ClusterId, ClusterGovParams>>; #[pallet::storage] #[pallet::getter(fn clusters_nodes)] @@ -113,12 +118,10 @@ pub mod pallet { origin: OriginFor, cluster_id: ClusterId, 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, cluster_gov_params) - .map_err(|e: ClusterError| Into::>::into(ClusterError::from(e)))?; + let cluster = Cluster::new(cluster_id.clone(), caller_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); Self::deposit_event(Event::::ClusterCreated { cluster_id }); @@ -224,13 +227,10 @@ pub mod pallet { cluster_gov_params: ClusterGovParams>, ) -> DispatchResult { let caller_id = ensure_signed(origin)?; - let mut cluster = + let cluster = Clusters::::try_get(&cluster_id).map_err(|_| Error::::ClusterDoesNotExist)?; ensure!(cluster.manager_id == caller_id, Error::::OnlyClusterManager); - cluster - .set_gov_params(cluster_gov_params) - .map_err(|e: ClusterError| Into::>::into(ClusterError::from(e)))?; - Clusters::::insert(cluster_id.clone(), cluster); + ClustersGovParams::::insert(cluster_id.clone(), cluster_gov_params); Self::deposit_event(Event::::ClusterGovParamsSet { cluster_id }); Ok(()) From 7b3c6659959ac9a4828cad8c65b56952659978e2 Mon Sep 17 00:00:00 2001 From: Raid Ateir Date: Mon, 6 Nov 2023 21:14:39 +0100 Subject: [PATCH 3/3] change edge to cdn prefix in naming --- pallets/ddc-clusters/src/cluster.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pallets/ddc-clusters/src/cluster.rs b/pallets/ddc-clusters/src/cluster.rs index 12aaeda0d..7274fbf16 100644 --- a/pallets/ddc-clusters/src/cluster.rs +++ b/pallets/ddc-clusters/src/cluster.rs @@ -40,9 +40,9 @@ pub struct ClusterGovParams { pub validators_share: Perbill, pub cluster_reserve_share: Perbill, #[codec(compact)] - pub edge_bond_size: Balance, - pub edge_chill_delay: EraIndex, - pub edge_unbonding_delay: EraIndex, + 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,