Skip to content

Commit

Permalink
Bucket params with 'is_public' flag added (#199)
Browse files Browse the repository at this point in the history
This PR introduces Bucket parameters with `is_public` flag and provides
extrinsic to update parameters for the bucket owner.
  • Loading branch information
yahortsaryk authored Dec 18, 2023
1 parent 1fbe546 commit d9312c5
Show file tree
Hide file tree
Showing 5 changed files with 216 additions and 19 deletions.
3 changes: 2 additions & 1 deletion node/service/chain-specs/example.json
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@
[
"0x0000000000000000000000000000000000000001",
"5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty",
10000000000
10000000000,
false
]
]
},
Expand Down
33 changes: 32 additions & 1 deletion pallets/ddc-customers/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,12 @@ benchmarks! {
cluster_gov_params
);

let bucket_params = BucketParams {
is_public: false
};

whitelist_account!(user);
}: _(RawOrigin::Signed(user), cluster_id)
}: _(RawOrigin::Signed(user), cluster_id, bucket_params)
verify {
assert_eq!(Pallet::<T>::buckets_count(), 1);
}
Expand Down Expand Up @@ -143,6 +147,33 @@ benchmarks! {
assert!(!Ledger::<T>::contains_key(user));
}

set_bucket_params {
let cluster_id = ClusterId::from([1; 20]);
let user = account::<T::AccountId>("user", USER_SEED, 0u32);

let bucket_id = 1;
let bucket = Bucket {
bucket_id,
owner_id: user.clone(),
cluster_id,
is_public: false,
};

<BucketsCount<T>>::set(bucket_id);
<Buckets<T>>::insert(bucket_id, bucket);

whitelist_account!(user);

let bucket_params = BucketParams {
is_public: true
};

}: _(RawOrigin::Signed(user), bucket_id, bucket_params)
verify {
let bucket = <Buckets<T>>::get(bucket_id).unwrap();
assert!(bucket.is_public);
}

impl_benchmark_test_suite!(
DdcCustomers,
crate::mock::ExtBuilder.build(),
Expand Down
48 changes: 41 additions & 7 deletions pallets/ddc-customers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ pub struct Bucket<AccountId> {
bucket_id: BucketId,
owner_id: AccountId,
cluster_id: ClusterId,
is_public: bool,
}

#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug, TypeInfo)]
pub struct BucketsDetails<Balance: HasCompact> {
pub bucket_id: BucketId,
pub amount: Balance,
pub struct BucketParams {
is_public: bool,
}

#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug, TypeInfo)]
Expand Down Expand Up @@ -230,6 +230,8 @@ pub mod pallet {
Charged(T::AccountId, BalanceOf<T>),
/// Bucket with specific id created
BucketCreated(BucketId),
/// Bucket with specific id updated
BucketUpdated(BucketId),
}

#[pallet::error]
Expand Down Expand Up @@ -264,7 +266,7 @@ pub mod pallet {

#[pallet::genesis_config]
pub struct GenesisConfig<T: Config> {
pub buckets: Vec<(ClusterId, T::AccountId, BalanceOf<T>)>,
pub buckets: Vec<(ClusterId, T::AccountId, BalanceOf<T>, bool)>,
}

#[cfg(feature = "std")]
Expand All @@ -283,7 +285,7 @@ pub mod pallet {
let _ = <T as pallet::Config>::Currency::make_free_balance_be(&account_id, min);
}

for &(ref cluster_id, ref owner_id, ref deposit) in &self.buckets {
for &(ref cluster_id, ref owner_id, ref deposit, ref is_public) in &self.buckets {
let cur_bucket_id = <BucketsCount<T>>::get()
.checked_add(1)
.ok_or(Error::<T>::ArithmeticOverflow)
Expand All @@ -294,6 +296,7 @@ pub mod pallet {
bucket_id: cur_bucket_id,
owner_id: owner_id.clone(),
cluster_id: *cluster_id,
is_public: *is_public,
};
<Buckets<T>>::insert(cur_bucket_id, bucket);

Expand All @@ -317,15 +320,24 @@ pub mod pallet {
///
/// Anyone can create a bucket
#[pallet::weight(T::WeightInfo::create_bucket())]
pub fn create_bucket(origin: OriginFor<T>, cluster_id: ClusterId) -> DispatchResult {
pub fn create_bucket(
origin: OriginFor<T>,
cluster_id: ClusterId,
bucket_params: BucketParams,
) -> DispatchResult {
let bucket_owner = ensure_signed(origin)?;
let cur_bucket_id =
Self::buckets_count().checked_add(1).ok_or(Error::<T>::ArithmeticOverflow)?;

<T as pallet::Config>::ClusterVisitor::ensure_cluster(&cluster_id)
.map_err(|_| Error::<T>::ClusterDoesNotExist)?;

let bucket = Bucket { bucket_id: cur_bucket_id, owner_id: bucket_owner, cluster_id };
let bucket = Bucket {
bucket_id: cur_bucket_id,
owner_id: bucket_owner,
cluster_id,
is_public: bucket_params.is_public,
};

<BucketsCount<T>>::set(cur_bucket_id);
<Buckets<T>>::insert(cur_bucket_id, bucket);
Expand Down Expand Up @@ -498,6 +510,28 @@ pub mod pallet {

Ok(post_info_weight.into())
}

/// Sets bucket parameters.
///
/// The dispatch origin for this call must be _Signed_ by the bucket owner.
///
/// Emits `BucketUpdated`.
#[pallet::weight(T::WeightInfo::set_bucket_params())]
pub fn set_bucket_params(
origin: OriginFor<T>,
bucket_id: BucketId,
bucket_params: BucketParams,
) -> DispatchResult {
let owner = ensure_signed(origin)?;
let mut bucket = Self::buckets(bucket_id).ok_or(Error::<T>::NoBucketWithId)?;
ensure!(bucket.owner_id == owner, Error::<T>::NotBucketOwner);

bucket.is_public = bucket_params.is_public;
<Buckets<T>>::insert(bucket_id, bucket);
Self::deposit_event(Event::<T>::BucketUpdated(bucket_id));

Ok(())
}
}

impl<T: Config> Pallet<T> {
Expand Down
130 changes: 124 additions & 6 deletions pallets/ddc-customers/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,25 @@ fn create_bucket_works() {

let cluster_id = ClusterId::from([1; 20]);
let account_1 = 1;
let bucket_params = BucketParams { is_public: false };

// Bucket created
assert_ok!(DdcCustomers::create_bucket(RuntimeOrigin::signed(account_1), cluster_id));
assert_ok!(DdcCustomers::create_bucket(
RuntimeOrigin::signed(account_1),
cluster_id,
bucket_params.clone()
));

// Check storage
assert_eq!(DdcCustomers::buckets_count(), 1);
assert_eq!(
DdcCustomers::buckets(1),
Some(Bucket { bucket_id: 1, owner_id: account_1, cluster_id })
Some(Bucket {
bucket_id: 1,
owner_id: account_1,
cluster_id,
is_public: bucket_params.is_public
})
);

// Checking that event was emitted
Expand All @@ -37,24 +47,44 @@ fn create_two_buckets_works() {

let cluster_id = ClusterId::from([1; 20]);
let account_1 = 1;
let bucket_1_params = BucketParams { is_public: false };
let bucket_2_params = BucketParams { is_public: true };

// Buckets created
assert_ok!(DdcCustomers::create_bucket(RuntimeOrigin::signed(account_1), cluster_id));
assert_ok!(DdcCustomers::create_bucket(
RuntimeOrigin::signed(account_1),
cluster_id,
bucket_1_params.clone()
));
assert_eq!(System::events().len(), 1);
System::assert_last_event(Event::BucketCreated(1u64).into());
assert_ok!(DdcCustomers::create_bucket(RuntimeOrigin::signed(account_1), cluster_id));
assert_ok!(DdcCustomers::create_bucket(
RuntimeOrigin::signed(account_1),
cluster_id,
bucket_2_params.clone()
));
assert_eq!(System::events().len(), 2);
System::assert_last_event(Event::BucketCreated(2u64).into());

// Check storage
assert_eq!(DdcCustomers::buckets_count(), 2);
assert_eq!(
DdcCustomers::buckets(1),
Some(Bucket { bucket_id: 1, owner_id: account_1, cluster_id })
Some(Bucket {
bucket_id: 1,
owner_id: account_1,
cluster_id,
is_public: bucket_1_params.is_public
})
);
assert_eq!(
DdcCustomers::buckets(2),
Some(Bucket { bucket_id: 2, owner_id: account_1, cluster_id })
Some(Bucket {
bucket_id: 2,
owner_id: account_1,
cluster_id,
is_public: bucket_2_params.is_public
})
);
})
}
Expand Down Expand Up @@ -289,3 +319,91 @@ fn unlock_and_withdraw_deposit_works() {
assert_eq!(DdcCustomers::ledger(&account_1), None);
})
}

#[test]
fn set_bucket_params_works() {
ExtBuilder.build_and_execute(|| {
System::set_block_number(1);

let cluster_id = ClusterId::from([1; 20]);
let bucket_owner = 1;
let bucket_params = BucketParams { is_public: false };

// Bucket created
assert_ok!(DdcCustomers::create_bucket(
RuntimeOrigin::signed(bucket_owner),
cluster_id,
bucket_params
));

// Checking that event was emitted
assert_eq!(System::events().len(), 1);
System::assert_last_event(Event::BucketCreated(1u64).into());

let bucket_id = 1;
let update_bucket_params = BucketParams { is_public: true };
assert_ok!(DdcCustomers::set_bucket_params(
RuntimeOrigin::signed(bucket_owner),
bucket_id,
update_bucket_params.clone()
));

assert_eq!(DdcCustomers::buckets_count(), 1);
assert_eq!(
DdcCustomers::buckets(1),
Some(Bucket {
bucket_id,
owner_id: bucket_owner,
cluster_id,
is_public: update_bucket_params.is_public
})
);

// Checking that event was emitted
assert_eq!(System::events().len(), 2);
System::assert_last_event(Event::BucketUpdated(bucket_id).into());
})
}

#[test]
fn set_bucket_params_checks_work() {
ExtBuilder.build_and_execute(|| {
System::set_block_number(1);

let cluster_id = ClusterId::from([1; 20]);
let bucket_owner = 1;
let bucket_params = BucketParams { is_public: false };

// Bucket created
assert_ok!(DdcCustomers::create_bucket(
RuntimeOrigin::signed(bucket_owner),
cluster_id,
bucket_params
));

// Checking that event was emitted
assert_eq!(System::events().len(), 1);
System::assert_last_event(Event::BucketCreated(1u64).into());
let bucket_id = 1;

let non_existent_bucket_id = 2;
assert_noop!(
DdcCustomers::set_bucket_params(
RuntimeOrigin::signed(bucket_owner),
non_existent_bucket_id,
BucketParams { is_public: true }
),
Error::<Test>::NoBucketWithId
);

let not_bucket_owner_id = 2;
assert_noop!(
DdcCustomers::set_bucket_params(
RuntimeOrigin::signed(not_bucket_owner_id),
bucket_id,
BucketParams { is_public: true }
),
Error::<Test>::NotBucketOwner
);
})
}
21 changes: 17 additions & 4 deletions pallets/ddc-customers/src/weights.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Autogenerated weights for pallet_ddc_customers
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
//! DATE: 2023-12-14, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! DATE: 2023-12-15, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! HOSTNAME: `Yahors-MacBook-Pro.local`, CPU: `<UNKNOWN>`
//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024
Expand Down Expand Up @@ -33,6 +33,7 @@ pub trait WeightInfo {
fn unlock_deposit() -> Weight;
fn withdraw_unlocked_deposit_update() -> Weight;
fn withdraw_unlocked_deposit_kill() -> Weight;
fn set_bucket_params() -> Weight;
}

/// Weights for pallet_ddc_customers using the Substrate node and recommended hardware.
Expand Down Expand Up @@ -76,10 +77,16 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
// Storage: DdcCustomers Ledger (r:1 w:1)
// Storage: System Account (r:1 w:1)
fn withdraw_unlocked_deposit_kill() -> Weight {
Weight::from_ref_time(36_000_000_u64)
Weight::from_ref_time(32_000_000_u64)
.saturating_add(T::DbWeight::get().reads(2_u64))
.saturating_add(T::DbWeight::get().writes(2_u64))
}
// Storage: DdcCustomers Buckets (r:1 w:1)
fn set_bucket_params() -> Weight {
Weight::from_ref_time(14_000_000_u64)
.saturating_add(T::DbWeight::get().reads(1_u64))
.saturating_add(T::DbWeight::get().writes(1_u64))
}
}

// For backwards compatibility and tests
Expand Down Expand Up @@ -122,8 +129,14 @@ impl WeightInfo for () {
// Storage: DdcCustomers Ledger (r:1 w:1)
// Storage: System Account (r:1 w:1)
fn withdraw_unlocked_deposit_kill() -> Weight {
Weight::from_ref_time(36_000_000_u64)
Weight::from_ref_time(32_000_000_u64)
.saturating_add(RocksDbWeight::get().reads(2_u64))
.saturating_add(RocksDbWeight::get().writes(2_u64))
}
}
// Storage: DdcCustomers Buckets (r:1 w:1)
fn set_bucket_params() -> Weight {
Weight::from_ref_time(14_000_000_u64)
.saturating_add(RocksDbWeight::get().reads(1_u64))
.saturating_add(RocksDbWeight::get().writes(1_u64))
}
}

0 comments on commit d9312c5

Please sign in to comment.