Skip to content

Commit

Permalink
feat: trait bounds are added for the node provider type
Browse files Browse the repository at this point in the history
  • Loading branch information
yahortsaryk committed Nov 3, 2023
1 parent 46cbf99 commit b490d95
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 61 deletions.
13 changes: 7 additions & 6 deletions pallets/ddc-clusters/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub mod pallet {
Blake2_128Concat,
NodePubKey,
bool,
ValueQuery,
OptionQuery,
>;

#[pallet::call]
Expand Down Expand Up @@ -147,11 +147,12 @@ pub mod pallet {
// Cluster extension smart contract allows joining.
let call_data = {
// is_authorized(node_provider: AccountId, node: Vec<u8>, node_variant: u8) -> bool
let args: ([u8; 4], /* T::AccountId, */ Vec<u8>, u8) = (
let args: ([u8; 4], T::AccountId, Vec<u8>, u8) = (
INK_SELECTOR_IS_AUTHORIZED,
// *node.get_provider_id(),
node_pub_key.encode()[1..].to_vec(), // remove the first byte added by SCALE
node_pub_key.variant_as_number(),
node.get_provider_id().to_owned(),
/* remove the first byte* added by SCALE */
node.get_pub_key().to_owned().encode()[1..].to_vec(),
node.get_type().into(),
);
args.encode()
};
Expand Down Expand Up @@ -223,7 +224,7 @@ pub mod pallet {

impl<T: Config> ClusterVisitor<T> for Pallet<T> {
fn cluster_has_node(cluster_id: &ClusterId, node_pub_key: &NodePubKey) -> bool {
ClustersNodes::<T>::get(cluster_id, node_pub_key)
ClustersNodes::<T>::get(cluster_id, node_pub_key).is_some()
}

fn ensure_cluster(cluster_id: &ClusterId) -> Result<(), ClusterVisitorError> {
Expand Down
15 changes: 8 additions & 7 deletions pallets/ddc-nodes/src/cdn_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ parameter_types! {
}

#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, PartialEq)]
pub struct CDNNode<AccountId> {
#[scale_info(skip_type_params(T))]
pub struct CDNNode<T: frame_system::Config> {
pub pub_key: CDNNodePubKey,
pub provider_id: AccountId,
pub provider_id: T::AccountId,
pub cluster_id: Option<ClusterId>,
pub props: CDNNodeProps,
}
Expand All @@ -32,11 +33,11 @@ pub struct CDNNodeParams {
pub params: Vec<u8>, // should be replaced with specific parameters for this type of node
}

impl<AccountId> NodeTrait<AccountId> for CDNNode<AccountId> {
impl<T: frame_system::Config> NodeTrait<T> for CDNNode<T> {
fn get_pub_key<'a>(&'a self) -> NodePubKeyRef<'a> {
NodePubKeyRef::CDNPubKeyRef(&self.pub_key)
}
fn get_provider_id(&self) -> &AccountId {
fn get_provider_id(&self) -> &T::AccountId {
&self.provider_id
}
fn get_props<'a>(&'a self) -> NodePropsRef<'a> {
Expand Down Expand Up @@ -70,12 +71,12 @@ impl<AccountId> NodeTrait<AccountId> for CDNNode<AccountId> {
}
fn new(
node_pub_key: NodePubKey,
provider_id: AccountId,
provider_id: T::AccountId,
node_params: NodeParams,
) -> Result<Node<AccountId>, NodeError> {
) -> Result<Node<T>, NodeError> {
match node_pub_key {
NodePubKey::CDNPubKey(pub_key) => match node_params {
NodeParams::CDNParams(node_params) => Ok(Node::CDN(CDNNode::<AccountId> {
NodeParams::CDNParams(node_params) => Ok(Node::CDN(CDNNode::<T> {
provider_id,
pub_key,
cluster_id: None,
Expand Down
19 changes: 9 additions & 10 deletions pallets/ddc-nodes/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,11 @@ pub mod pallet {
#[pallet::storage]
#[pallet::getter(fn storage_nodes)]
pub type StorageNodes<T: Config> =
StorageMap<_, Blake2_128Concat, StorageNodePubKey, StorageNode<T::AccountId>>;
StorageMap<_, Blake2_128Concat, StorageNodePubKey, StorageNode<T>>;

#[pallet::storage]
#[pallet::getter(fn cdn_nodes)]
pub type CDNNodes<T: Config> =
StorageMap<_, Blake2_128Concat, CDNNodePubKey, CDNNode<T::AccountId>>;
pub type CDNNodes<T: Config> = StorageMap<_, Blake2_128Concat, CDNNodePubKey, CDNNode<T>>;

#[pallet::call]
impl<T: Config> Pallet<T> {
Expand All @@ -82,7 +81,7 @@ pub mod pallet {
node_params: NodeParams,
) -> DispatchResult {
let caller_id = ensure_signed(origin)?;
let node = Node::<T::AccountId>::new(node_pub_key.clone(), caller_id, node_params)
let node = Node::<T>::new(node_pub_key.clone(), caller_id, node_params)
.map_err(|e| Into::<Error<T>>::into(NodeError::from(e)))?;
Self::create(node).map_err(|e| Into::<Error<T>>::into(NodeRepositoryError::from(e)))?;
Self::deposit_event(Event::<T>::NodeCreated { node_pub_key });
Expand Down Expand Up @@ -121,9 +120,9 @@ pub mod pallet {
}

pub trait NodeRepository<T: frame_system::Config> {
fn create(node: Node<T::AccountId>) -> Result<(), NodeRepositoryError>;
fn get(node_pub_key: NodePubKey) -> Result<Node<T::AccountId>, NodeRepositoryError>;
fn update(node: Node<T::AccountId>) -> Result<(), NodeRepositoryError>;
fn create(node: Node<T>) -> Result<(), NodeRepositoryError>;
fn get(node_pub_key: NodePubKey) -> Result<Node<T>, NodeRepositoryError>;
fn update(node: Node<T>) -> Result<(), NodeRepositoryError>;
fn delete(node_pub_key: NodePubKey) -> Result<(), NodeRepositoryError>;
}

Expand All @@ -146,7 +145,7 @@ pub mod pallet {
}

impl<T: Config> NodeRepository<T> for Pallet<T> {
fn create(node: Node<T::AccountId>) -> Result<(), NodeRepositoryError> {
fn create(node: Node<T>) -> Result<(), NodeRepositoryError> {
match node {
Node::Storage(storage_node) => {
if StorageNodes::<T>::contains_key(&storage_node.pub_key) {
Expand All @@ -165,7 +164,7 @@ pub mod pallet {
}
}

fn get(node_pub_key: NodePubKey) -> Result<Node<T::AccountId>, NodeRepositoryError> {
fn get(node_pub_key: NodePubKey) -> Result<Node<T>, NodeRepositoryError> {
match node_pub_key {
NodePubKey::StoragePubKey(pub_key) => match StorageNodes::<T>::try_get(pub_key) {
Ok(storage_node) => Ok(Node::Storage(storage_node)),
Expand All @@ -178,7 +177,7 @@ pub mod pallet {
}
}

fn update(node: Node<T::AccountId>) -> Result<(), NodeRepositoryError> {
fn update(node: Node<T>) -> Result<(), NodeRepositoryError> {
match node {
Node::Storage(storage_node) => {
if !StorageNodes::<T>::contains_key(&storage_node.pub_key) {
Expand Down
24 changes: 12 additions & 12 deletions pallets/ddc-nodes/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ use scale_info::TypeInfo;
use sp_runtime::RuntimeDebug;

#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, PartialEq)]
pub enum Node<AccountId> {
Storage(StorageNode<AccountId>),
CDN(CDNNode<AccountId>),
pub enum Node<T: frame_system::Config> {
Storage(StorageNode<T>),
CDN(CDNNode<T>),
}

// Params fields are always coming from extrinsic input
Expand Down Expand Up @@ -52,9 +52,9 @@ pub enum NodePropsRef<'a> {
CDNPropsRef(&'a CDNNodeProps),
}

pub trait NodeTrait<AccountId> {
pub trait NodeTrait<T: frame_system::Config> {
fn get_pub_key<'a>(&'a self) -> NodePubKeyRef<'a>;
fn get_provider_id(&self) -> &AccountId;
fn get_provider_id(&self) -> &T::AccountId;
fn get_props<'a>(&'a self) -> NodePropsRef<'a>;
fn set_props(&mut self, props: NodeProps) -> Result<(), NodeError>;
fn set_params(&mut self, props: NodeParams) -> Result<(), NodeError>;
Expand All @@ -63,19 +63,19 @@ pub trait NodeTrait<AccountId> {
fn get_type(&self) -> NodeType;
fn new(
node_pub_key: NodePubKey,
provider_id: AccountId,
provider_id: T::AccountId,
params: NodeParams,
) -> Result<Node<AccountId>, NodeError>;
) -> Result<Node<T>, NodeError>;
}

impl<AccountId> NodeTrait<AccountId> for Node<AccountId> {
impl<T: frame_system::Config> NodeTrait<T> for Node<T> {
fn get_pub_key<'a>(&'a self) -> NodePubKeyRef<'a> {
match &self {
Node::Storage(node) => node.get_pub_key(),
Node::CDN(node) => node.get_pub_key(),
}
}
fn get_provider_id(&self) -> &AccountId {
fn get_provider_id(&self) -> &T::AccountId {
match &self {
Node::Storage(node) => node.get_provider_id(),
Node::CDN(node) => node.get_provider_id(),
Expand Down Expand Up @@ -119,9 +119,9 @@ impl<AccountId> NodeTrait<AccountId> for Node<AccountId> {
}
fn new(
node_pub_key: NodePubKey,
provider_id: AccountId,
provider_id: T::AccountId,
node_params: NodeParams,
) -> Result<Node<AccountId>, NodeError> {
) -> Result<Node<T>, NodeError> {
match node_pub_key {
NodePubKey::StoragePubKey(_) =>
StorageNode::new(node_pub_key, provider_id, node_params),
Expand Down Expand Up @@ -175,7 +175,7 @@ impl<T> From<NodeError> for Error<T> {
NodeError::InvalidStorageNodeParams => Error::<T>::InvalidNodeParams,
NodeError::InvalidCDNNodeParams => Error::<T>::InvalidNodeParams,
NodeError::StorageNodeParamsExceedsLimit => Error::<T>::NodeParamsExceedsLimit,
NodeError::CDNNodeParamsExceedsLimit => Error::<T>::InvalidNodeParams,
NodeError::CDNNodeParamsExceedsLimit => Error::<T>::NodeParamsExceedsLimit,
NodeError::InvalidStorageNodeProps => Error::<T>::InvalidNodeParams,
NodeError::InvalidCDNNodeProps => Error::<T>::InvalidNodeParams,
}
Expand Down
34 changes: 17 additions & 17 deletions pallets/ddc-nodes/src/storage_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ parameter_types! {
}

#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, PartialEq)]
pub struct StorageNode<AccountId> {
#[scale_info(skip_type_params(T))]
pub struct StorageNode<T: frame_system::Config> {
pub pub_key: StorageNodePubKey,
pub provider_id: AccountId,
pub provider_id: T::AccountId,
pub cluster_id: Option<ClusterId>,
pub props: StorageNodeProps,
}
Expand All @@ -32,11 +33,11 @@ pub struct StorageNodeParams {
pub params: Vec<u8>, // should be replaced with specific parameters for this type of node
}

impl<AccountId> NodeTrait<AccountId> for StorageNode<AccountId> {
impl<T: frame_system::Config> NodeTrait<T> for StorageNode<T> {
fn get_pub_key<'a>(&'a self) -> NodePubKeyRef<'a> {
NodePubKeyRef::StoragePubKeyRef(&self.pub_key)
}
fn get_provider_id(&self) -> &AccountId {
fn get_provider_id(&self) -> &T::AccountId {
&self.provider_id
}
fn get_props<'a>(&'a self) -> NodePropsRef<'a> {
Expand Down Expand Up @@ -70,23 +71,22 @@ impl<AccountId> NodeTrait<AccountId> for StorageNode<AccountId> {
}
fn new(
node_pub_key: NodePubKey,
provider_id: AccountId,
provider_id: T::AccountId,
node_params: NodeParams,
) -> Result<Node<AccountId>, NodeError> {
) -> Result<Node<T>, NodeError> {
match node_pub_key {
NodePubKey::StoragePubKey(pub_key) => match node_params {
NodeParams::StorageParams(node_params) =>
Ok(Node::Storage(StorageNode::<AccountId> {
provider_id,
pub_key,
cluster_id: None,
props: StorageNodeProps {
params: match node_params.params.try_into() {
Ok(vec) => vec,
Err(_) => return Err(NodeError::StorageNodeParamsExceedsLimit),
},
NodeParams::StorageParams(node_params) => Ok(Node::Storage(StorageNode::<T> {
provider_id,
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),
},
_ => Err(NodeError::InvalidStorageNodePubKey),
Expand Down
9 changes: 0 additions & 9 deletions primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,5 @@ pub enum NodePubKey {
CDNPubKey(CDNNodePubKey),
}

impl NodePubKey {
pub fn variant_as_number(&self) -> u8 {
match self {
NodePubKey::CDNPubKey(_) => 0,
NodePubKey::StoragePubKey(_) => 1,
}
}
}

pub type StorageNodePubKey = AccountId32;
pub type CDNNodePubKey = AccountId32;

0 comments on commit b490d95

Please sign in to comment.