Skip to content

Commit

Permalink
remove unnecessary params & update mock
Browse files Browse the repository at this point in the history
  • Loading branch information
Raid5594 committed Nov 7, 2023
1 parent f5abcdb commit 1b067e3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 25 deletions.
36 changes: 11 additions & 25 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, SaturatedConversion,
AccountId32, RuntimeDebug, SaturatedConversion,
};
use sp_staking::EraIndex;
use sp_std::{collections::btree_map::BTreeMap, prelude::*};
Expand Down Expand Up @@ -402,7 +402,6 @@ pub mod pallet {
assert_ok!(Pallet::<T>::serve(
T::RuntimeOrigin::from(Some(controller.clone()).into()),
cluster,
node.clone()
));
}

Expand All @@ -421,7 +420,6 @@ pub mod pallet {
assert_ok!(Pallet::<T>::store(
T::RuntimeOrigin::from(Some(controller.clone()).into()),
cluster,
node.clone(),
));
}
}
Expand Down Expand Up @@ -702,31 +700,25 @@ 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,
node_pub_key: NodePubKey,
) -> DispatchResult {
pub fn serve(origin: OriginFor<T>, cluster_id: ClusterId) -> DispatchResult {
let controller = ensure_signed(origin)?;
let account = AccountId32::new([0; 32]);

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)))?;
let bond_size =
T::ClusterVisitor::get_bond_size(&cluster_id, &NodePubKey::CDNPubKey(account))
.map_err(|e| Into::<Error<T>>::into(ClusterVisitorError::from(e)))?;

ensure!(
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 @@ -751,30 +743,24 @@ 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,
node_pub_key: NodePubKey,
) -> DispatchResult {
pub fn store(origin: OriginFor<T>, cluster_id: ClusterId) -> DispatchResult {
let controller = ensure_signed(origin)?;
let account = AccountId32::new([0; 32]);

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)))?;
let bond_size =
T::ClusterVisitor::get_bond_size(&cluster_id, &NodePubKey::StoragePubKey(account))
.map_err(|e| Into::<Error<T>>::into(ClusterVisitorError::from(e)))?;
ensure!(
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
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_pub_key: &NodePubKey,
) -> Result<u128, ClusterVisitorError> {
Ok(10)
}
}
pub struct ExtBuilder {
has_edges: bool,
Expand Down

0 comments on commit 1b067e3

Please sign in to comment.