Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

substitute bond size check with cluster gov params #120

Merged
merged 5 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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_pub_key {
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,
}
Loading