Skip to content

Commit

Permalink
merge create_bucket and allocate_bucket_to_cluster methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Raid5594 committed Nov 7, 2023
1 parent 12a09cf commit fb15886
Showing 1 changed file with 8 additions and 30 deletions.
38 changes: 8 additions & 30 deletions pallets/ddc-customers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ pub struct Bucket<AccountId> {
bucket_id: BucketId,
owner_id: AccountId,
cluster_id: Option<ClusterId>,
public_availability: bool,
}

#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug, TypeInfo)]
Expand Down Expand Up @@ -245,51 +244,30 @@ pub mod pallet {

#[pallet::call]
impl<T: Config> Pallet<T> {
/// Create new bucket with provided public availability & reserved resources
/// Create new bucket with specified cluster id
///
/// Anyone can create a bucket
#[pallet::weight(10_000)]
pub fn create_bucket(origin: OriginFor<T>, public_availability: bool) -> DispatchResult {
pub fn create_bucket(origin: OriginFor<T>, cluster_id: ClusterId) -> DispatchResult {
let bucket_owner = ensure_signed(origin)?;
let cur_bucket_id = Self::buckets_count();

ensure!(
ddc_clusters::pallet::Clusters::<T>::contains_key(&cluster_id),
Error::<T>::ClusterDoesNotExist
);

let bucket = Bucket {
bucket_id: cur_bucket_id + 1,
owner_id: bucket_owner,
cluster_id: None,
public_availability,
cluster_id: Some(cluster_id),
};

<BucketsCount<T>>::set(cur_bucket_id + 1);
<Buckets<T>>::insert(cur_bucket_id + 1, bucket);
Ok(())
}

/// Allocates specified bucket into specified cluster
///
/// Only bucket owner can call this method
#[pallet::weight(10_000)]
pub fn allocate_bucket_to_cluster(
origin: OriginFor<T>,
bucket_id: BucketId,
cluster_id: ClusterId,
) -> DispatchResult {
let bucket_owner = ensure_signed(origin)?;

let mut bucket = Self::buckets(bucket_id).ok_or(Error::<T>::NoBucketWithId)?;

ensure!(bucket.cluster_id == None, Error::<T>::BucketAlreadyInCluster);
ensure!(bucket.owner_id == bucket_owner, Error::<T>::NotBucketOwner);

ensure!(
ddc_clusters::pallet::Clusters::<T>::contains_key(&cluster_id),
Error::<T>::ClusterDoesNotExist
);
bucket.cluster_id = Some(cluster_id);

Ok(())
}

/// Take the origin account as a owner and lock up `value` of its balance. `Owner` will
/// be the account that controls it.
///
Expand Down

0 comments on commit fb15886

Please sign in to comment.