Skip to content

Commit

Permalink
chore: cluster and nodes split to modules
Browse files Browse the repository at this point in the history
  • Loading branch information
yahortsaryk committed Oct 12, 2023
1 parent 87da868 commit fb158d4
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 50 deletions.
61 changes: 61 additions & 0 deletions pallets/ddc-clusters/src/cluster.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use crate::pallet::Error;
use codec::{Decode, Encode};
use frame_support::{pallet_prelude::*, parameter_types, BoundedVec};
use scale_info::TypeInfo;
use sp_core::hash::H160;
use sp_std::vec::Vec;

pub type ClusterId = H160;
parameter_types! {
pub MaxClusterParamsLen: u16 = 2048;
}

#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, PartialEq)]
pub struct Cluster<ManagerId> {
pub cluster_id: ClusterId,
pub manager_id: ManagerId,
pub props: ClusterProps,
}

#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, PartialEq)]
pub struct ClusterProps {
// this is a temporal way of storing cluster parameters as a stringified json,
// should be replaced with specific properties for cluster
pub params: BoundedVec<u8, MaxClusterParamsLen>,
}

#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, PartialEq)]
pub struct ClusterParams {
pub params: Vec<u8>,
}

impl<ManagerId> Cluster<ManagerId> {
pub fn from_params(
cluster_id: ClusterId,
manager_id: ManagerId,
cluster_params: ClusterParams,
) -> Result<Cluster<ManagerId>, ClusterError> {
Ok(Cluster {
cluster_id,
manager_id,
props: ClusterProps {
params: match cluster_params.params.try_into() {
Ok(vec) => vec,
Err(_) => return Err(ClusterError::ClusterParamsExceedsLimit),
},
},
})
}
}

pub enum ClusterError {
ClusterParamsExceedsLimit,
}

impl<T> From<ClusterError> for Error<T> {
fn from(error: ClusterError) -> Self {
match error {
ClusterError::ClusterParamsExceedsLimit => Error::<T>::ClusterParamsExceedsLimit,
}
}
}
40 changes: 12 additions & 28 deletions pallets/ddc-clusters/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,15 @@
#![cfg_attr(not(feature = "std"), no_std)]
#![recursion_limit = "256"]

use codec::{Decode, Encode};
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
pub use pallet::*;
use scale_info::TypeInfo;
use sp_core::hash::H160;
use sp_runtime::RuntimeDebug;
use pallet_ddc_nodes::{NodePubKey, NodeRepository, NodeTrait};
use sp_std::prelude::*;

use pallet_ddc_nodes::{NodePubKey, NodeRepository, NodeTrait};
pub use pallet::*;
mod cluster;

pub use crate::cluster::{Cluster, ClusterError, ClusterId, ClusterParams};

#[frame_support::pallet]
pub mod pallet {
Expand All @@ -49,40 +48,25 @@ pub mod pallet {
#[pallet::error]
pub enum Error<T> {
ClusterAlreadyExists,
ClusterParamsExceedsLimit,
}

#[pallet::storage]
#[pallet::getter(fn storage_nodes)]
pub type Clusters<T: Config> = StorageMap<_, Blake2_128Concat, ClusterId, Cluster>;

type ClusterId = H160;

#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, PartialEq)]
pub struct Cluster {
cluster_id: ClusterId,
}

#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, PartialEq)]
pub struct ClusterParams {
cluster_id: ClusterId,
}

impl Cluster {
fn from_params(params: ClusterParams) -> Cluster {
Cluster { cluster_id: params.cluster_id }
}
}
pub type Clusters<T: Config> =
StorageMap<_, Blake2_128Concat, ClusterId, Cluster<T::AccountId>>;

#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::weight(10_000)]
pub fn create_cluster(
origin: OriginFor<T>,
cluster_id: ClusterId,
cluster_params: ClusterParams,
) -> DispatchResult {
ensure_signed(origin)?;

let cluster = Cluster::from_params(cluster_params);
let manager_id = ensure_signed(origin)?;
let cluster = Cluster::from_params(cluster_id, manager_id, cluster_params)
.map_err(|e| Into::<Error<T>>::into(ClusterError::from(e)))?;
let cluster_id = cluster.cluster_id.clone();

ensure!(!Clusters::<T>::contains_key(&cluster_id), Error::<T>::ClusterAlreadyExists);
Expand Down
27 changes: 18 additions & 9 deletions pallets/ddc-nodes/src/cdn_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ use crate::{
ClusterId,
};
use codec::{Decode, Encode};
use frame_support::{parameter_types, BoundedVec};
use scale_info::TypeInfo;
use sp_runtime::{AccountId32, RuntimeDebug};
use sp_std::prelude::*;

pub type CDNNodePubKey = AccountId32;
parameter_types! {
pub MaxCDNNodeParamsLen: u16 = 2048;
}

#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, PartialEq)]
pub struct CDNNode<ProviderId> {
Expand All @@ -19,15 +23,15 @@ pub struct CDNNode<ProviderId> {

#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, PartialEq)]
pub struct CDNNodeProps {
pub url: Vec<u8>,
pub location: Vec<u8>,
// this is a temporal way of storing node parameters as a stringified json,
// should be replaced with specific properties for this type of node once they are defined
pub params: BoundedVec<u8, MaxCDNNodeParamsLen>,
}

#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, PartialEq)]
pub struct CDNNodeParams {
pub pub_key: CDNNodePubKey,
pub url: Vec<u8>,
pub location: Vec<u8>,
pub params: Vec<u8>, // should be replaced with specific parameters for this type of node
}

impl<ProviderId> NodeTrait<ProviderId> for CDNNode<ProviderId> {
Expand All @@ -51,14 +55,19 @@ impl<ProviderId> NodeTrait<ProviderId> for CDNNode<ProviderId> {
}
fn from_params(
provider_id: ProviderId,
params: NodeParams,
node_params: NodeParams,
) -> Result<Node<ProviderId>, NodeError> {
match params {
NodeParams::CDNParams(params) => Ok(Node::CDN(CDNNode::<ProviderId> {
match node_params {
NodeParams::CDNParams(node_params) => Ok(Node::CDN(CDNNode::<ProviderId> {
provider_id,
pub_key: params.pub_key,
pub_key: node_params.pub_key,
cluster_id: None,
props: CDNNodeProps { url: params.url, location: params.location },
props: CDNNodeProps {
params: match node_params.params.try_into() {
Ok(vec) => vec,
Err(_) => return Err(NodeError::CDNNodeParamsExceedsLimit),
},
},
})),
_ => Err(NodeError::InvalidCDNNodeParams),
}
Expand Down
4 changes: 1 addition & 3 deletions pallets/ddc-nodes/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,9 @@
#![cfg_attr(not(feature = "std"), no_std)]
#![recursion_limit = "256"]

use codec::{Decode, Encode};
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
use scale_info::TypeInfo;
use sp_core::hash::H160;
use sp_runtime::RuntimeDebug;
use sp_std::prelude::*;

pub use pallet::*;
Expand Down Expand Up @@ -58,6 +55,7 @@ pub mod pallet {
NodeAlreadyExists,
NodeDoesNotExist,
InvalidNodeParams,
NodeParamsExceedsLimit,
}

#[pallet::storage]
Expand Down
4 changes: 4 additions & 0 deletions pallets/ddc-nodes/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,17 @@ impl TryFrom<u8> for NodeType {
pub enum NodeError {
InvalidStorageNodeParams,
InvalidCDNNodeParams,
StorageNodeParamsExceedsLimit,
CDNNodeParamsExceedsLimit,
}

impl<T> From<NodeError> for Error<T> {
fn from(error: NodeError) -> Self {
match error {
NodeError::InvalidStorageNodeParams => Error::<T>::InvalidNodeParams,
NodeError::InvalidCDNNodeParams => Error::<T>::InvalidNodeParams,
NodeError::StorageNodeParamsExceedsLimit => Error::<T>::NodeParamsExceedsLimit,
NodeError::CDNNodeParamsExceedsLimit => Error::<T>::NodeParamsExceedsLimit,
}
}
}
33 changes: 23 additions & 10 deletions pallets/ddc-nodes/src/storage_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@ use crate::{
ClusterId,
};
use codec::{Decode, Encode};
use frame_support::{parameter_types, BoundedVec};
use scale_info::TypeInfo;
use sp_runtime::{AccountId32, RuntimeDebug};
use sp_std::prelude::Vec;

pub type StorageNodePubKey = AccountId32;
parameter_types! {
pub MaxStorageNodeParamsLen: u16 = 2048;
}

#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, PartialEq)]
pub struct StorageNode<ProviderId> {
Expand All @@ -18,13 +23,15 @@ pub struct StorageNode<ProviderId> {

#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, PartialEq)]
pub struct StorageNodeProps {
pub capacity: u32,
// this is a temporal way of storing node parameters as a stringified json,
// should be replaced with specific properties for this type of node once they are defined
pub params: BoundedVec<u8, MaxStorageNodeParamsLen>,
}

#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, PartialEq)]
pub struct StorageNodeParams {
pub pub_key: StorageNodePubKey,
pub capacity: u32,
pub params: Vec<u8>, // should be replaced with specific parameters for this type of node
}

impl<ProviderId> NodeTrait<ProviderId> for StorageNode<ProviderId> {
Expand All @@ -48,15 +55,21 @@ impl<ProviderId> NodeTrait<ProviderId> for StorageNode<ProviderId> {
}
fn from_params(
provider_id: ProviderId,
params: NodeParams,
node_params: NodeParams,
) -> Result<Node<ProviderId>, NodeError> {
match params {
NodeParams::StorageParams(params) => Ok(Node::Storage(StorageNode::<ProviderId> {
provider_id,
pub_key: params.pub_key,
cluster_id: None,
props: StorageNodeProps { capacity: params.capacity },
})),
match node_params {
NodeParams::StorageParams(node_params) =>
Ok(Node::Storage(StorageNode::<ProviderId> {
provider_id,
pub_key: node_params.pub_key,
cluster_id: None,
props: StorageNodeProps {
params: match node_params.params.try_into() {
Ok(vec) => vec,
Err(_) => return Err(NodeError::StorageNodeParamsExceedsLimit),
},
},
})),
_ => Err(NodeError::InvalidStorageNodeParams),
}
}
Expand Down

0 comments on commit fb158d4

Please sign in to comment.