Skip to content

Commit

Permalink
substitute bond size check with cluster gov params
Browse files Browse the repository at this point in the history
  • Loading branch information
Raid5594 committed Nov 7, 2023
1 parent d6db558 commit f5abcdb
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 8 deletions.
17 changes: 17 additions & 0 deletions pallets/ddc-clusters/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,22 @@ pub mod pallet {
.map(|_| ())
.ok_or(ClusterVisitorError::ClusterDoesNotExist)
}

fn get_bond_size(
cluster_id: &ClusterId,
node_pub_key: &NodePubKey,
) -> 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 {
NodePubKey::StoragePubKey(_node_pub_key) =>
Ok(cluster_gov_params.storage_bond_size.saturated_into::<u128>()),
NodePubKey::CDNPubKey(_node_pub_key) =>
Ok(cluster_gov_params.cdn_bond_size.saturated_into::<u128>()),
}
}
}

impl<T> From<StakingVisitorError> for Error<T> {
Expand Down
50 changes: 42 additions & 8 deletions pallets/ddc-staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 @@ -402,6 +402,7 @@ pub mod pallet {
assert_ok!(Pallet::<T>::serve(
T::RuntimeOrigin::from(Some(controller.clone()).into()),
cluster,
node.clone()
));
}

Expand All @@ -420,6 +421,7 @@ pub mod pallet {
assert_ok!(Pallet::<T>::store(
T::RuntimeOrigin::from(Some(controller.clone()).into()),
cluster,
node.clone(),
));
}
}
Expand Down Expand Up @@ -487,6 +489,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 @@ -590,6 +594,7 @@ pub mod pallet {
pub fn unbond(
origin: OriginFor<T>,
#[pallet::compact] value: BalanceOf<T>,
node_pub_key: NodePubKey,
) -> DispatchResult {
let controller = ensure_signed(origin)?;
let mut ledger = Self::ledger(&controller).ok_or(Error::<T>::NotController)?;
Expand All @@ -609,10 +614,15 @@ pub mod pallet {
ledger.active = Zero::zero();
}

// Check if stash is mapped to the node
let node_stash = <Nodes<T>>::get(&node_pub_key).ok_or(Error::<T>::BadState)?;
ensure!(ledger.stash.clone() == node_stash, Error::<T>::NotNodeController);

let min_active_bond = if let Some(cluster_id) = Self::edges(&ledger.stash) {
Self::settings(cluster_id).edge_bond_size
} else if let Some(cluster_id) = Self::storages(&ledger.stash) {
Self::settings(cluster_id).storage_bond_size
// Retrieve the respective bond size from Cluster Visitor
let bond_size = T::ClusterVisitor::get_bond_size(&cluster_id, &node_pub_key)
.map_err(|e| Into::<Error<T>>::into(ClusterVisitorError::from(e)))?;
bond_size.saturated_into::<BalanceOf<T>>()
} else {
Zero::zero()
};
Expand Down Expand Up @@ -692,19 +702,31 @@ pub mod pallet {
/// The dispatch origin for this call must be _Signed_ by the controller, not the stash. The
/// bond size must be greater than or equal to the `EdgeBondSize`.
#[pallet::weight(T::WeightInfo::serve())]
pub fn serve(origin: OriginFor<T>, cluster_id: ClusterId) -> DispatchResult {
pub fn serve(
origin: OriginFor<T>,
cluster_id: ClusterId,
node_pub_key: NodePubKey,
) -> DispatchResult {
let controller = ensure_signed(origin)?;

T::ClusterVisitor::ensure_cluster(&cluster_id)
.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, &node_pub_key)
.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;

// Check if stash is mapped to the node
let node_stash = <Nodes<T>>::get(&node_pub_key).ok_or(Error::<T>::BadState)?;
ensure!(*stash == node_stash, Error::<T>::NotNodeController);

// Can't participate in CDN if already participating in storage network.
ensure!(!Storages::<T>::contains_key(&stash), Error::<T>::AlreadyInRole);

Expand All @@ -729,19 +751,30 @@ pub mod pallet {
/// The dispatch origin for this call must be _Signed_ by the controller, not the stash. The
/// bond size must be greater than or equal to the `StorageBondSize`.
#[pallet::weight(T::WeightInfo::store())]
pub fn store(origin: OriginFor<T>, cluster_id: ClusterId) -> DispatchResult {
pub fn store(
origin: OriginFor<T>,
cluster_id: ClusterId,
node_pub_key: NodePubKey,
) -> DispatchResult {
let controller = ensure_signed(origin)?;

T::ClusterVisitor::ensure_cluster(&cluster_id)
.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, &node_pub_key)
.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;

// Check if stash is mapped to the node
let node_stash = <Nodes<T>>::get(&node_pub_key).ok_or(Error::<T>::BadState)?;
ensure!(*stash == node_stash, Error::<T>::NotNodeController);

// Can't participate in storage network if already participating in CDN.
ensure!(!Edges::<T>::contains_key(&stash), Error::<T>::AlreadyInRole);

Expand Down Expand Up @@ -1197,6 +1230,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 traits/src/cluster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@ 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_pub_key: &NodePubKey,
) -> Result<u128, ClusterVisitorError>;
}

pub enum ClusterVisitorError {
ClusterDoesNotExist,
ClusterGovParamsNotSet,
}

0 comments on commit f5abcdb

Please sign in to comment.