From 45a167495249a4f6b48c29d6c756ab371a8a78ba Mon Sep 17 00:00:00 2001 From: zcalz Date: Thu, 6 Jun 2024 23:31:58 -0400 Subject: [PATCH] feat: unregister worker cluster --- pallets/worker-registration/src/lib.rs | 28 ++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/pallets/worker-registration/src/lib.rs b/pallets/worker-registration/src/lib.rs index 175d378..d852876 100644 --- a/pallets/worker-registration/src/lib.rs +++ b/pallets/worker-registration/src/lib.rs @@ -203,6 +203,7 @@ pub mod pallet { #[pallet::generate_deposit(pub(super) fn deposit_event)] pub enum Event { WorkerRegistered{ creator: T::AccountId }, + WorkerUnRegistered{ creator: T::AccountId, cluster_id: ClusterId }, TaskScheduled { worker: T::AccountId, owner: T::AccountId, @@ -221,7 +222,9 @@ pub mod pallet { WorkerRegisterMissingIp, WorkerRegisterMissingPort, ClusterExists, + ClusterDoesNotExists, NoWorkersAvailable, + InvalidOwnerOfCluster, WorkerClusterNotRegistered, UnassignedTaskId, InvalidTaskOwner, @@ -363,6 +366,7 @@ pub mod pallet { //check cluster ensure!(WorkerAccounts::::contains_key(creator.clone()) == false, + //One work per account for now Error::::ClusterExists); let cid = NextClusterId::::get(); @@ -486,6 +490,30 @@ pub mod pallet { // Return a successful DispatchResult Ok(()) } + + #[pallet::call_index(3)] + #[pallet::weight(10_000 + T::DbWeight::get().writes(1).ref_time())] + pub fn unregister_worker( + origin: OriginFor, + cluster_id: ClusterId, + ) -> DispatchResult { + let creator = ensure_signed(origin)?; + + //check cluster + ensure!(WorkerAccounts::::get(creator.clone()) == Some(cluster_id), + Error::::InvalidOwnerOfCluster); + ensure!(WorkerClusters::::get(cluster_id) != None, + Error::::ClusterDoesNotExists); + + // update storage + WorkerClusters::::remove(cluster_id); + WorkerAccounts::::remove(creator.clone()); + + // Emit an event. + Self::deposit_event(Event::WorkerUnRegistered { creator, cluster_id }); + + Ok(()) + } // //TODO: update implementation // #[pallet::call_index(3)] // #[pallet::weight({0})]