Skip to content

Commit

Permalink
Merge pull request #120 from Cerebellum-Network/feature/ddc-staking-r…
Browse files Browse the repository at this point in the history
…eads-gov-params-from-cluster

substitute bond size check with cluster gov params
  • Loading branch information
yahortsaryk authored Nov 7, 2023
2 parents d6db558 + 24bb2ea commit fd38969
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 8 deletions.
18 changes: 17 additions & 1 deletion pallets/ddc-clusters/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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;
Expand Down Expand Up @@ -247,6 +248,21 @@ pub mod pallet {
.map(|_| ())
.ok_or(ClusterVisitorError::ClusterDoesNotExist)
}

fn get_bond_size(
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 {
NodeType::Storage =>
Ok(cluster_gov_params.storage_bond_size.saturated_into::<u128>()),
NodeType::CDN => Ok(cluster_gov_params.cdn_bond_size.saturated_into::<u128>()),
}
}
}

impl<T> From<StakingVisitorError> for Error<T> {
Expand Down
27 changes: 21 additions & 6 deletions pallets/ddc-staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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::*};
Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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::<Error<T>>::into(ClusterVisitorError::from(e)))?;
bond_size.saturated_into::<BalanceOf<T>>()
} 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::<Error<T>>::into(ClusterVisitorError::from(e)))?;
bond_size.saturated_into::<BalanceOf<T>>()
} else {
Zero::zero()
};
Expand Down Expand Up @@ -699,8 +706,12 @@ pub mod pallet {
.map_err(|e| Into::<Error<T>>::into(ClusterVisitorError::from(e)))?;

let ledger = Self::ledger(&controller).ok_or(Error::<T>::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::<Error<T>>::into(ClusterVisitorError::from(e)))?;

ensure!(
ledger.active >= Self::settings(cluster_id).edge_bond_size,
ledger.active >= bond_size.saturated_into::<BalanceOf<T>>(),
Error::<T>::InsufficientBond
);
let stash = &ledger.stash;
Expand Down Expand Up @@ -736,8 +747,11 @@ pub mod pallet {
.map_err(|e| Into::<Error<T>>::into(ClusterVisitorError::from(e)))?;

let ledger = Self::ledger(&controller).ok_or(Error::<T>::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::<Error<T>>::into(ClusterVisitorError::from(e)))?;
ensure!(
ledger.active >= Self::settings(cluster_id).storage_bond_size,
ledger.active >= bond_size.saturated_into::<BalanceOf<T>>(),
Error::<T>::InsufficientBond
);
let stash = &ledger.stash;
Expand Down Expand Up @@ -1197,6 +1211,7 @@ pub mod pallet {
fn from(error: ClusterVisitorError) -> Self {
match error {
ClusterVisitorError::ClusterDoesNotExist => Error::<T>::NodeHasNoStake,
ClusterVisitorError::ClusterGovParamsNotSet => Error::<T>::NoClusterGovParams,
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions pallets/ddc-staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ impl<T: Config> ClusterVisitor<T> for TestClusterVisitor {
fn ensure_cluster(_cluster_id: &ClusterId) -> Result<(), ClusterVisitorError> {
Ok(())
}
fn get_bond_size(
_cluster_id: &ClusterId,
_node_type: NodeType,
) -> Result<u128, ClusterVisitorError> {
Ok(10)
}
}
pub struct ExtBuilder {
has_edges: bool,
Expand Down
8 changes: 7 additions & 1 deletion traits/src/cluster.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
use ddc_primitives::{ClusterId, NodePubKey};
use ddc_primitives::{ClusterId, NodePubKey, NodeType};
use frame_system::Config;

pub trait ClusterVisitor<T: Config> {
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<u128, ClusterVisitorError>;
}

pub enum ClusterVisitorError {
ClusterDoesNotExist,
ClusterGovParamsNotSet,
}

0 comments on commit fd38969

Please sign in to comment.