From f5abcdb0ad830c0c454251cc150bbe61ac955045 Mon Sep 17 00:00:00 2001 From: Raid Ateir Date: Tue, 7 Nov 2023 15:30:54 +0100 Subject: [PATCH] substitute bond size check with cluster gov params --- pallets/ddc-clusters/src/lib.rs | 17 +++++++++++ pallets/ddc-staking/src/lib.rs | 50 +++++++++++++++++++++++++++------ traits/src/cluster.rs | 6 ++++ 3 files changed, 65 insertions(+), 8 deletions(-) diff --git a/pallets/ddc-clusters/src/lib.rs b/pallets/ddc-clusters/src/lib.rs index 59ce7fb52..08f246e69 100644 --- a/pallets/ddc-clusters/src/lib.rs +++ b/pallets/ddc-clusters/src/lib.rs @@ -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,22 @@ pub mod pallet { .map(|_| ()) .ok_or(ClusterVisitorError::ClusterDoesNotExist) } + + fn get_bond_size( + cluster_id: &ClusterId, + node_pub_key: &NodePubKey, + ) -> Result { + // ensure!(ClustersNodes::::contains_key(cluster_id), + // Error::::ClusterDoesNotExist); + let cluster_gov_params = ClustersGovParams::::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::()), + NodePubKey::CDNPubKey(_node_pub_key) => + 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..84c1183c0 100644 --- a/pallets/ddc-staking/src/lib.rs +++ b/pallets/ddc-staking/src/lib.rs @@ -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::*}; @@ -402,6 +402,7 @@ pub mod pallet { assert_ok!(Pallet::::serve( T::RuntimeOrigin::from(Some(controller.clone()).into()), cluster, + node.clone() )); } @@ -420,6 +421,7 @@ pub mod pallet { assert_ok!(Pallet::::store( T::RuntimeOrigin::from(Some(controller.clone()).into()), cluster, + node.clone(), )); } } @@ -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, } @@ -590,6 +594,7 @@ pub mod pallet { pub fn unbond( origin: OriginFor, #[pallet::compact] value: BalanceOf, + node_pub_key: NodePubKey, ) -> DispatchResult { let controller = ensure_signed(origin)?; let mut ledger = Self::ledger(&controller).ok_or(Error::::NotController)?; @@ -609,10 +614,15 @@ pub mod pallet { ledger.active = Zero::zero(); } + // Check if stash is mapped to the node + let node_stash = >::get(&node_pub_key).ok_or(Error::::BadState)?; + ensure!(ledger.stash.clone() == node_stash, Error::::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::>::into(ClusterVisitorError::from(e)))?; + bond_size.saturated_into::>() } else { Zero::zero() }; @@ -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, cluster_id: ClusterId) -> DispatchResult { + pub fn serve( + origin: OriginFor, + cluster_id: ClusterId, + node_pub_key: NodePubKey, + ) -> DispatchResult { let controller = ensure_signed(origin)?; T::ClusterVisitor::ensure_cluster(&cluster_id) .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, &node_pub_key) + .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; + // Check if stash is mapped to the node + let node_stash = >::get(&node_pub_key).ok_or(Error::::BadState)?; + ensure!(*stash == node_stash, Error::::NotNodeController); + // Can't participate in CDN if already participating in storage network. ensure!(!Storages::::contains_key(&stash), Error::::AlreadyInRole); @@ -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, cluster_id: ClusterId) -> DispatchResult { + pub fn store( + origin: OriginFor, + cluster_id: ClusterId, + node_pub_key: NodePubKey, + ) -> DispatchResult { let controller = ensure_signed(origin)?; T::ClusterVisitor::ensure_cluster(&cluster_id) .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, &node_pub_key) + .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; + // Check if stash is mapped to the node + let node_stash = >::get(&node_pub_key).ok_or(Error::::BadState)?; + ensure!(*stash == node_stash, Error::::NotNodeController); + // Can't participate in storage network if already participating in CDN. ensure!(!Edges::::contains_key(&stash), Error::::AlreadyInRole); @@ -1197,6 +1230,7 @@ pub mod pallet { fn from(error: ClusterVisitorError) -> Self { match error { ClusterVisitorError::ClusterDoesNotExist => Error::::NodeHasNoStake, + ClusterVisitorError::ClusterGovParamsNotSet => Error::::NoClusterGovParams, } } } diff --git a/traits/src/cluster.rs b/traits/src/cluster.rs index a1cae3515..505de5298 100644 --- a/traits/src/cluster.rs +++ b/traits/src/cluster.rs @@ -5,8 +5,14 @@ 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_pub_key: &NodePubKey, + ) -> Result; } pub enum ClusterVisitorError { ClusterDoesNotExist, + ClusterGovParamsNotSet, }