Skip to content

Commit

Permalink
feat: setting cluster_id for node
Browse files Browse the repository at this point in the history
  • Loading branch information
yahortsaryk committed Oct 2, 2023
1 parent e5c6077 commit 42e4362
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
3 changes: 2 additions & 1 deletion pallets/ddc-clusters/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ pub mod pallet {
node_pub_key: NodePubKey,
) -> DispatchResult {
ensure_signed(origin)?;
let node = T::NodeRepository::get(node_pub_key)?;

T::NodeRepository::add_to_cluster(node_pub_key, cluster_id)?;

Ok(())
}
Expand Down
1 change: 1 addition & 0 deletions pallets/ddc-nodes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ sp-io = { version = "6.0.0", default-features = false, git = "https://github.com
sp-runtime = { version = "6.0.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.30" }
sp-staking = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.30" }
sp-std = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.30" }
sp-core = { version = "6.0.0", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.30", default-features = false }

[dev-dependencies]
sp-core = { version = "6.0.0", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.30" }
Expand Down
36 changes: 28 additions & 8 deletions pallets/ddc-nodes/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ 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::*;

Expand Down Expand Up @@ -59,11 +60,14 @@ pub mod pallet {
#[pallet::getter(fn cdn_nodes)]
pub type CDNNodes<T: Config> = StorageMap<_, Blake2_128Concat, CDNNodePubKey, CDNNode>;

// todo: add the type to the Config
type ClusterId = H160;

type StorageNodePubKey = sp_runtime::AccountId32;
#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, PartialEq)]
pub struct StorageNode {
pub_key: StorageNodePubKey,
status: u8,
cluster_id: Option<ClusterId>,
props: StorageNodeProps,
}

Expand All @@ -82,7 +86,7 @@ pub mod pallet {
#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, PartialEq)]
pub struct CDNNode {
pub_key: CDNNodePubKey,
status: u8,
cluster_id: Option<ClusterId>,
props: CDNNodeProps,
}

Expand Down Expand Up @@ -167,7 +171,7 @@ pub mod pallet {
match params {
NodeParams::StorageParams(params) => Ok(Node::Storage(StorageNode {
pub_key: params.pub_key,
status: 1,
cluster_id: None,
props: StorageNodeProps { capacity: params.capacity },
})),
_ => Err(Error::<T>::NodeAlreadyExists),
Expand All @@ -189,7 +193,7 @@ pub mod pallet {
match params {
NodeParams::CDNParams(params) => Ok(Node::CDN(CDNNode {
pub_key: params.pub_key,
status: 1,
cluster_id: None,
props: CDNNodeProps { url: params.url, location: params.location },
})),
_ => Err(Error::<T>::NodeAlreadyExists),
Expand Down Expand Up @@ -245,7 +249,7 @@ pub mod pallet {
}

pub trait NodeRepositoryTrait {
fn save<T: Config>(node: Node) -> Result<(), pallet::Error<T>>;
fn create<T: Config>(node: Node) -> Result<(), pallet::Error<T>>;
fn get<T: Config>(pub_key: NodePubKey) -> Result<Node, pallet::Error<T>>;
}

Expand All @@ -256,19 +260,20 @@ pub mod pallet {
ensure_signed(origin)?;
let node: Node = Node::from_params::<T>(node_params)?;
let node_pub_key = node.get_pub_key().to_owned();
Self::save(node)?;
Self::create(node)?;
Self::deposit_event(Event::<T>::NodeCreated(node_pub_key));
Ok(())
}
}

pub trait NodeRepository {
fn save(node: Node) -> Result<(), &'static str>;
fn create(node: Node) -> Result<(), &'static str>;
fn get(pub_key: NodePubKey) -> Result<Node, &'static str>;
fn add_to_cluster(pub_key: NodePubKey, cluster_id: ClusterId) -> Result<(), &'static str>;
}

impl<T: Config> NodeRepository for Pallet<T> {
fn save(node: Node) -> Result<(), &'static str> {
fn create(node: Node) -> Result<(), &'static str> {
match node {
Node::Storage(storage_node) => {
if StorageNodes::<T>::contains_key(&storage_node.pub_key) {
Expand Down Expand Up @@ -299,5 +304,20 @@ pub mod pallet {
},
}
}

fn add_to_cluster(pub_key: NodePubKey, cluster_id: ClusterId) -> Result<(), &'static str> {
let mut node = Self::get(pub_key)?;
match node {
Node::Storage(mut storage_node) => {
storage_node.cluster_id = Some(cluster_id);
StorageNodes::<T>::insert(storage_node.pub_key.clone(), storage_node);
},
Node::CDN(mut cdn_node) => {
cdn_node.cluster_id = Some(cluster_id);
CDNNodes::<T>::insert(cdn_node.pub_key.clone(), cdn_node);
},
}
Ok(())
}
}
}

0 comments on commit 42e4362

Please sign in to comment.