Skip to content

Commit

Permalink
chore: PR #163 is backported
Browse files Browse the repository at this point in the history
  • Loading branch information
Raid5594 authored and yahortsaryk committed Dec 8, 2023
1 parent 1a6d621 commit 59fba68
Show file tree
Hide file tree
Showing 15 changed files with 292 additions and 108 deletions.
3 changes: 1 addition & 2 deletions pallets/ddc-clusters/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
//! Tests for the module.
use super::{mock::*, *};
use ddc_primitives::{ClusterId, ClusterParams, NodePubKey};
use ddc_primitives::{CDNNodeParams, ClusterId, ClusterParams, NodeParams, NodePubKey};
use frame_support::{assert_noop, assert_ok, error::BadOrigin};
use frame_system::Config;
use hex_literal::hex;
use pallet_ddc_nodes::{CDNNodeParams, NodeParams};
use sp_runtime::{traits::Hash, AccountId32, Perquintill};

#[test]
Expand Down
12 changes: 2 additions & 10 deletions pallets/ddc-nodes/src/cdn_node.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::node::{NodeError, NodeParams, NodeProps, NodeTrait};
use crate::node::{NodeError, NodeProps, NodeTrait};
use codec::{Decode, Encode};
use ddc_primitives::{CDNNodePubKey, ClusterId, NodePubKey, NodeType};
use ddc_primitives::{CDNNodePubKey, ClusterId, NodeParams, NodePubKey, NodeType};
use frame_support::{parameter_types, BoundedVec};
use scale_info::TypeInfo;
use sp_runtime::RuntimeDebug;
Expand Down Expand Up @@ -28,14 +28,6 @@ pub struct CDNNodeProps {
pub p2p_port: u16,
}

#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, PartialEq)]
pub struct CDNNodeParams {
pub host: Vec<u8>,
pub http_port: u16,
pub grpc_port: u16,
pub p2p_port: u16,
}

impl<T: frame_system::Config> CDNNode<T> {
pub fn new(
node_pub_key: NodePubKey,
Expand Down
21 changes: 17 additions & 4 deletions pallets/ddc-nodes/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ pub mod benchmarking;
#[cfg(any(feature = "runtime-benchmarks", test))]
pub mod testing_utils;

use ddc_primitives::{CDNNodePubKey, ClusterId, NodePubKey, StorageNodePubKey};
use ddc_primitives::{CDNNodePubKey, ClusterId, NodeParams, NodePubKey, StorageNodePubKey};
use ddc_traits::{
node::{NodeVisitor, NodeVisitorError},
node::{NodeCreator, NodeVisitor, NodeVisitorError},
staking::StakingVisitor,
};

Expand All @@ -43,8 +43,8 @@ mod node;
mod storage_node;

pub use crate::{
cdn_node::{CDNNode, CDNNodeParams},
node::{Node, NodeError, NodeParams, NodeTrait},
cdn_node::CDNNode,
node::{Node, NodeError, NodeTrait},
storage_node::StorageNode,
};

Expand Down Expand Up @@ -242,4 +242,17 @@ pub mod pallet {
Self::get(node_pub_key.clone()).is_ok()
}
}

impl<T: Config> NodeCreator<T> for Pallet<T> {
fn create_node(
node_pub_key: NodePubKey,
provider_id: T::AccountId,
node_params: NodeParams,
) -> DispatchResult {
let node = Node::<T>::new(node_pub_key, provider_id, node_params)
.map_err(Into::<Error<T>>::into)?;
Self::create(node).map_err(Into::<Error<T>>::into)?;
Ok(())
}
}
}
13 changes: 3 additions & 10 deletions pallets/ddc-nodes/src/node.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#![allow(clippy::needless_lifetimes)] // ToDo

use crate::{
cdn_node::{CDNNode, CDNNodeParams, CDNNodeProps},
cdn_node::{CDNNode, CDNNodeProps},
pallet::Error,
storage_node::{StorageNode, StorageNodeParams, StorageNodeProps},
storage_node::{StorageNode, StorageNodeProps},
ClusterId,
};
use codec::{Decode, Encode};
use ddc_primitives::{NodePubKey, NodeType};
use ddc_primitives::{NodeParams, NodePubKey, NodeType};
use scale_info::TypeInfo;
use sp_runtime::RuntimeDebug;

Expand All @@ -17,13 +17,6 @@ pub enum Node<T: frame_system::Config> {
CDN(CDNNode<T>),
}

// Params fields are always coming from extrinsic input
#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, PartialEq)]
pub enum NodeParams {
StorageParams(StorageNodeParams),
CDNParams(CDNNodeParams),
}

// Props fields may include internal protocol properties
#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, PartialEq)]
pub enum NodeProps {
Expand Down
13 changes: 2 additions & 11 deletions pallets/ddc-nodes/src/storage_node.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use crate::node::{NodeError, NodeParams, NodeProps, NodeTrait};
use crate::node::{NodeError, NodeProps, NodeTrait};
use codec::{Decode, Encode};
use ddc_primitives::{ClusterId, NodePubKey, NodeType, StorageNodePubKey};
use ddc_primitives::{ClusterId, NodeParams, NodePubKey, NodeType, StorageNodePubKey};
use frame_support::{parameter_types, BoundedVec};
use scale_info::TypeInfo;
use sp_runtime::RuntimeDebug;
use sp_std::prelude::Vec;

parameter_types! {
pub MaxStorageNodeParamsLen: u16 = 2048;
Expand All @@ -28,14 +27,6 @@ pub struct StorageNodeProps {
pub p2p_port: u16,
}

#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, PartialEq)]
pub struct StorageNodeParams {
pub host: Vec<u8>,
pub http_port: u16,
pub grpc_port: u16,
pub p2p_port: u16,
}

impl<T: frame_system::Config> StorageNode<T> {
pub fn new(
node_pub_key: NodePubKey,
Expand Down
4 changes: 2 additions & 2 deletions pallets/ddc-nodes/src/testing_utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Testing utils for ddc-staking.
use crate::{cdn_node::CDNNodeParams, node::NodeParams, Config, NodePubKey};
use ddc_primitives::CDNNodePubKey;
use crate::{Config, NodePubKey};
use ddc_primitives::{CDNNodeParams, CDNNodePubKey, NodeParams};
use frame_benchmarking::account;
use sp_std::vec;

Expand Down
3 changes: 1 addition & 2 deletions pallets/ddc-nodes/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
//! Tests for the module.
use super::{mock::*, *};
use crate::{cdn_node::CDNNodeParams, storage_node::StorageNodeParams};
use ddc_primitives::NodePubKey;
use ddc_primitives::{CDNNodeParams, NodePubKey, StorageNodeParams};
use frame_support::{assert_noop, assert_ok};
use sp_runtime::AccountId32;

Expand Down
21 changes: 17 additions & 4 deletions pallets/ddc-staking/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use super::*;
use crate::Pallet as DdcStaking;
use ddc_primitives::{CDNNodePubKey, NodeType};
use ddc_primitives::{CDNNodeParams, CDNNodePubKey, NodeParams, NodeType, StorageNodePubKey};
use testing_utils::*;

use frame_support::traits::Currency;
Expand All @@ -23,6 +23,16 @@ benchmarks! {
let controller_lookup: <T::Lookup as StaticLookup>::Source
= T::Lookup::unlookup(controller.clone());
let node = NodePubKey::CDNPubKey(CDNNodePubKey::new([0; 32]));
let _ = T::NodeCreator::create_node(
node.clone(),
stash.clone(),
NodeParams::CDNParams(CDNNodeParams {
host: vec![1u8, 255],
http_port: 35000u16,
grpc_port: 25000u16,
p2p_port: 15000u16,
})
)?;
let amount = T::Currency::minimum_balance() * 10u32.into();
whitelist_account!(stash);
}: _(RawOrigin::Signed(stash.clone()), controller_lookup, node.clone(), amount)
Expand Down Expand Up @@ -65,7 +75,8 @@ benchmarks! {
}

store {
let (stash, controller, _) = create_stash_controller_node_with_balance::<T>(0, T::ClusterVisitor::get_bond_size(&ClusterId::from([1; 20]), NodeType::CDN).unwrap_or(10u128).saturated_into::<BalanceOf<T>>())?;
let node_pub_key = NodePubKey::StoragePubKey(StorageNodePubKey::new([0; 32]));
let (stash, controller, _) = create_stash_controller_node_with_balance::<T>(0, T::ClusterVisitor::get_bond_size(&ClusterId::from([1; 20]), NodeType::CDN).unwrap_or(100u128), node_pub_key)?;

whitelist_account!(controller);
}: _(RawOrigin::Signed(controller), ClusterId::from([1; 20]))
Expand All @@ -74,7 +85,8 @@ benchmarks! {
}

serve {
let (stash, controller, _) = create_stash_controller_node_with_balance::<T>(0, T::ClusterVisitor::get_bond_size(&ClusterId::from([1; 20]), NodeType::CDN).unwrap_or(10u128).saturated_into::<BalanceOf<T>>())?;
let node_pub_key = NodePubKey::CDNPubKey(CDNNodePubKey::new([0; 32]));
let (stash, controller, _) = create_stash_controller_node_with_balance::<T>(0, T::ClusterVisitor::get_bond_size(&ClusterId::from([1; 20]), NodeType::CDN).unwrap_or(10u128), node_pub_key)?;

whitelist_account!(controller);
}: _(RawOrigin::Signed(controller), ClusterId::from([1; 20]))
Expand All @@ -86,7 +98,8 @@ benchmarks! {
// clean up any existing state.
clear_storages_and_cdns::<T>();

let (cdn_stash, cdn_controller, _) = create_stash_controller_node_with_balance::<T>(0, T::ClusterVisitor::get_bond_size(&ClusterId::from([1; 20]), NodeType::CDN).unwrap_or(10u128).saturated_into::<BalanceOf<T>>())?;
let node_pub_key = NodePubKey::CDNPubKey(CDNNodePubKey::new([0; 32]));
let (cdn_stash, cdn_controller, _) = create_stash_controller_node_with_balance::<T>(0, T::ClusterVisitor::get_bond_size(&ClusterId::from([1; 20]), NodeType::CDN).unwrap_or(10u128), node_pub_key)?;
DdcStaking::<T>::serve(RawOrigin::Signed(cdn_controller.clone()).into(), ClusterId::from([1; 20]))?;
assert!(CDNs::<T>::contains_key(&cdn_stash));
frame_system::Pallet::<T>::set_block_number(T::BlockNumber::from(1u32));
Expand Down
8 changes: 6 additions & 2 deletions pallets/ddc-staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ use crate::weights::WeightInfo;
use codec::{Decode, Encode, HasCompact};
pub use ddc_primitives::{ClusterId, NodePubKey, NodeType};
use ddc_traits::{
cluster::{ClusterVisitor, ClusterVisitorError},
node::NodeVisitor,
cluster::{ClusterCreator, ClusterVisitor, ClusterVisitorError},
node::{NodeCreator, NodeVisitor},
staking::{StakingVisitor, StakingVisitorError},
};

Expand Down Expand Up @@ -161,9 +161,13 @@ pub mod pallet {

type ClusterVisitor: ClusterVisitor<Self>;

type ClusterCreator: ClusterCreator<Self, BalanceOf<Self>>;

type ClusterManager: ClusterManager<Self>;

type NodeVisitor: NodeVisitor<Self>;

type NodeCreator: NodeCreator<Self>;
}

/// Map from all locked "stash" accounts to the controller account.
Expand Down
32 changes: 31 additions & 1 deletion pallets/ddc-staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

use crate::{self as pallet_ddc_staking, *};
use ddc_primitives::{
CDNNodePubKey, ClusterBondingParams, ClusterFeesParams, ClusterPricingParams, StorageNodePubKey,
CDNNodePubKey, ClusterBondingParams, ClusterFeesParams, ClusterGovParams, ClusterParams,
ClusterPricingParams, NodeParams, NodePubKey, StorageNodePubKey,
};
use ddc_traits::{
cluster::{ClusterManager, ClusterManagerError, ClusterVisitor, ClusterVisitorError},
Expand All @@ -13,9 +14,11 @@ use ddc_traits::{

use frame_support::{
construct_runtime,
dispatch::DispatchResult,
traits::{ConstU32, ConstU64, Everything, GenesisBuild},
weights::constants::RocksDbWeight,
};

use frame_system::mocking::{MockBlock, MockUncheckedExtrinsic};
use lazy_static::lazy_static;
use parking_lot::{ReentrantMutex, ReentrantMutexGuard};
Expand Down Expand Up @@ -108,11 +111,38 @@ impl crate::pallet::Config for Test {
type ClusterVisitor = TestClusterVisitor;
type ClusterManager = TestClusterManager;
type NodeVisitor = MockNodeVisitor;
type NodeCreator = TestNodeCreator;
type ClusterCreator = TestClusterCreator;
}

pub(crate) type DdcStakingCall = crate::Call<Test>;
pub(crate) type TestRuntimeCall = <Test as frame_system::Config>::RuntimeCall;
pub struct TestNodeCreator;
pub struct TestClusterCreator;
pub struct TestClusterVisitor;

impl<T: Config> NodeCreator<T> for TestNodeCreator {
fn create_node(
_node_pub_key: NodePubKey,
_provider_id: T::AccountId,
_node_params: NodeParams,
) -> DispatchResult {
Ok(())
}
}

impl<T: Config> ClusterCreator<T, u128> for TestClusterCreator {
fn create_new_cluster(
_cluster_id: ClusterId,
_cluster_manager_id: T::AccountId,
_cluster_reserve_id: T::AccountId,
_cluster_params: ClusterParams<T::AccountId>,
_cluster_gov_params: ClusterGovParams<Balance, T::BlockNumber>,
) -> DispatchResult {
Ok(())
}
}

impl<T: Config> ClusterVisitor<T> for TestClusterVisitor {
fn ensure_cluster(_cluster_id: &ClusterId) -> Result<(), ClusterVisitorError> {
Ok(())
Expand Down
Loading

0 comments on commit 59fba68

Please sign in to comment.