diff --git a/pallets/ddc-clusters/src/lib.rs b/pallets/ddc-clusters/src/lib.rs index 59ce7fb52..6a7c0549b 100644 --- a/pallets/ddc-clusters/src/lib.rs +++ b/pallets/ddc-clusters/src/lib.rs @@ -19,7 +19,7 @@ use crate::{ cluster::{Cluster, ClusterError, ClusterGovParams, ClusterParams}, node_provider_auth::{NodeProviderAuthContract, NodeProviderAuthContractError}, }; -use ddc_primitives::{ClusterId, NodePubKey}; +use ddc_primitives::{ClusterId, NodePubKey, NodeType}; use ddc_traits::{ cluster::{ClusterVisitor, ClusterVisitorError}, staking::{StakingVisitor, StakingVisitorError}, @@ -31,6 +31,7 @@ use frame_support::{ use frame_system::pallet_prelude::*; pub use pallet::*; use pallet_ddc_nodes::{NodeRepository, NodeTrait}; +use sp_runtime::SaturatedConversion; use sp_std::prelude::*; mod cluster; @@ -247,6 +248,21 @@ pub mod pallet { .map(|_| ()) .ok_or(ClusterVisitorError::ClusterDoesNotExist) } + + fn get_bond_size( + 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 { + NodeType::Storage => + Ok(cluster_gov_params.storage_bond_size.saturated_into::()), + NodeType::CDN => Ok(cluster_gov_params.cdn_bond_size.saturated_into::()), + } + } } impl From for Error { diff --git a/pallets/ddc-staking/src/lib.rs b/pallets/ddc-staking/src/lib.rs index 55614769b..6ef7d34af 100644 --- a/pallets/ddc-staking/src/lib.rs +++ b/pallets/ddc-staking/src/lib.rs @@ -29,7 +29,7 @@ pub mod weights; use crate::weights::WeightInfo; use codec::{Decode, Encode, HasCompact}; -pub use ddc_primitives::{ClusterId, NodePubKey}; +pub use ddc_primitives::{ClusterId, NodePubKey, NodeType}; use ddc_traits::{ cluster::{ClusterVisitor, ClusterVisitorError}, staking::{StakingVisitor, StakingVisitorError}, @@ -49,7 +49,7 @@ use frame_system::pallet_prelude::*; use scale_info::TypeInfo; use sp_runtime::{ traits::{AccountIdConversion, AtLeast32BitUnsigned, Saturating, StaticLookup, Zero}, - RuntimeDebug, + RuntimeDebug, SaturatedConversion, }; use sp_staking::EraIndex; use sp_std::{collections::btree_map::BTreeMap, prelude::*}; @@ -487,6 +487,8 @@ pub mod pallet { NotNodeController, /// No stake found associated with the provided node. NodeHasNoStake, + /// No cluster governance params found for cluster + NoClusterGovParams, /// Conditions for fast chill are not met, try the regular `chill` from FastChillProhibited, } @@ -610,9 +612,14 @@ pub mod pallet { } let min_active_bond = if let Some(cluster_id) = Self::edges(&ledger.stash) { - Self::settings(cluster_id).edge_bond_size + let bond_size = T::ClusterVisitor::get_bond_size(&cluster_id, NodeType::CDN) + .map_err(|e| Into::>::into(ClusterVisitorError::from(e)))?; + bond_size.saturated_into::>() } else if let Some(cluster_id) = Self::storages(&ledger.stash) { - Self::settings(cluster_id).storage_bond_size + let bond_size = + T::ClusterVisitor::get_bond_size(&cluster_id, NodeType::Storage) + .map_err(|e| Into::>::into(ClusterVisitorError::from(e)))?; + bond_size.saturated_into::>() } else { Zero::zero() }; @@ -699,8 +706,12 @@ pub mod pallet { .map_err(|e| Into::>::into(ClusterVisitorError::from(e)))?; let ledger = Self::ledger(&controller).ok_or(Error::::NotController)?; + // Retrieve the respective bond size from Cluster Visitor + let bond_size = T::ClusterVisitor::get_bond_size(&cluster_id, NodeType::CDN) + .map_err(|e| Into::>::into(ClusterVisitorError::from(e)))?; + ensure!( - ledger.active >= Self::settings(cluster_id).edge_bond_size, + ledger.active >= bond_size.saturated_into::>(), Error::::InsufficientBond ); let stash = &ledger.stash; @@ -736,8 +747,11 @@ pub mod pallet { .map_err(|e| Into::>::into(ClusterVisitorError::from(e)))?; let ledger = Self::ledger(&controller).ok_or(Error::::NotController)?; + // Retrieve the respective bond size from Cluster Visitor + let bond_size = T::ClusterVisitor::get_bond_size(&cluster_id, NodeType::Storage) + .map_err(|e| Into::>::into(ClusterVisitorError::from(e)))?; ensure!( - ledger.active >= Self::settings(cluster_id).storage_bond_size, + ledger.active >= bond_size.saturated_into::>(), Error::::InsufficientBond ); let stash = &ledger.stash; @@ -1197,6 +1211,7 @@ pub mod pallet { fn from(error: ClusterVisitorError) -> Self { match error { ClusterVisitorError::ClusterDoesNotExist => Error::::NodeHasNoStake, + ClusterVisitorError::ClusterGovParamsNotSet => Error::::NoClusterGovParams, } } } diff --git a/pallets/ddc-staking/src/mock.rs b/pallets/ddc-staking/src/mock.rs index cf55112b7..f009fec41 100644 --- a/pallets/ddc-staking/src/mock.rs +++ b/pallets/ddc-staking/src/mock.rs @@ -124,6 +124,12 @@ impl ClusterVisitor for TestClusterVisitor { fn ensure_cluster(_cluster_id: &ClusterId) -> Result<(), ClusterVisitorError> { Ok(()) } + fn get_bond_size( + _cluster_id: &ClusterId, + _node_type: NodeType, + ) -> Result { + Ok(10) + } } pub struct ExtBuilder { has_edges: bool, diff --git a/traits/src/cluster.rs b/traits/src/cluster.rs index a1cae3515..99cd8be6b 100644 --- a/traits/src/cluster.rs +++ b/traits/src/cluster.rs @@ -1,12 +1,18 @@ -use ddc_primitives::{ClusterId, NodePubKey}; +use ddc_primitives::{ClusterId, NodePubKey, NodeType}; use frame_system::Config; pub trait ClusterVisitor { fn cluster_has_node(cluster_id: &ClusterId, node_pub_key: &NodePubKey) -> bool; fn ensure_cluster(cluster_id: &ClusterId) -> Result<(), ClusterVisitorError>; + + fn get_bond_size( + cluster_id: &ClusterId, + node_type: NodeType, + ) -> Result; } pub enum ClusterVisitorError { ClusterDoesNotExist, + ClusterGovParamsNotSet, }