Skip to content

Commit

Permalink
update bucket_id type from u128 to u64 and add storage migration
Browse files Browse the repository at this point in the history
  • Loading branch information
Raid5594 committed Nov 1, 2023
1 parent 1a1eaaa commit 62f3a5e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 7 deletions.
23 changes: 16 additions & 7 deletions pallets/ddc-customers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ use sp_runtime::{
use sp_staking::EraIndex;
use sp_std::prelude::*;

mod migration;

pub use pallet::*;

/// The balance type of this pallet.
Expand All @@ -43,7 +45,7 @@ pub struct UnlockChunk<Balance: HasCompact> {

#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug, TypeInfo)]
pub struct Bucket<AccountId> {
bucket_id: u128,
bucket_id: u64,
owner_id: AccountId,
cluster_id: Option<ClusterId>,
public_availability: bool,
Expand All @@ -52,7 +54,7 @@ pub struct Bucket<AccountId> {

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

Expand Down Expand Up @@ -147,6 +149,13 @@ pub mod pallet {
#[pallet::without_storage_info]
pub struct Pallet<T>(_);

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_runtime_upgrade() -> frame_support::weights::Weight {
migration::migrate_to_v2::<T>()
}
}

#[pallet::config]
pub trait Config: frame_system::Config + ddc_staking::Config + ddc_clusters::Config {
/// The accounts's pallet id, used for deriving its sovereign account ID.
Expand All @@ -166,19 +175,19 @@ pub mod pallet {
StorageMap<_, Identity, T::AccountId, AccountsLedger<T::AccountId, BalanceOf<T>>>;

#[pallet::type_value]
pub fn DefaultBucketCount<T: Config>() -> u128 {
0_u128
pub fn DefaultBucketCount<T: Config>() -> u64 {
0_u64
}
#[pallet::storage]
#[pallet::getter(fn buckets_count)]
pub type BucketsCount<T: Config> =
StorageValue<Value = u128, QueryKind = ValueQuery, OnEmpty = DefaultBucketCount<T>>;
StorageValue<Value = u64, QueryKind = ValueQuery, OnEmpty = DefaultBucketCount<T>>;

/// Map from bucket ID to to the bucket structure
#[pallet::storage]
#[pallet::getter(fn buckets)]
pub type Buckets<T: Config> =
StorageMap<_, Twox64Concat, u128, Bucket<T::AccountId>, OptionQuery>;
StorageMap<_, Twox64Concat, u64, Bucket<T::AccountId>, OptionQuery>;

#[pallet::event]
#[pallet::generate_deposit(pub(crate) fn deposit_event)]
Expand Down Expand Up @@ -275,7 +284,7 @@ pub mod pallet {
#[pallet::weight(10_000)]
pub fn allocate_bucket_to_cluster(
origin: OriginFor<T>,
bucket_id: u128,
bucket_id: u64,
cluster_id: ClusterId,
) -> DispatchResult {
let bucket_owner = ensure_signed(origin)?;
Expand Down
36 changes: 36 additions & 0 deletions pallets/ddc-customers/src/migration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use super::*;

use frame_support::{
storage_alias,
traits::{Get, GetStorageVersion, StorageVersion},
weights::Weight,
BoundedVec, Twox64Concat,
};

// only contains V1 storage format
pub mod v1 {
use super::*;

#[storage_alias]
pub(super) type Buckets<T: Config> =
StorageMap<Pallet<T>, Twox64Concat, u64, Bucket<<T as frame_system::Config>::AccountId>>;
}

// contains checks and transforms storage to V2 format
pub fn migrate_to_v2<T: Config>() -> Weight {
// We transform the storage values from the old into the new format.
Buckets::<T>::translate::<Bucket<T::AccountId>, _>(|_k: u64, bucket: Bucket<T::AccountId>| {
Some(Bucket {
bucket_id: bucket.bucket_id as u64,
owner_id: bucket.owner_id,
cluster_id: bucket.cluster_id,
public_availability: bucket.public_availability,
resources_reserved: bucket.resources_reserved,
})
});

// Very inefficient, mostly here for illustration purposes.
let count = Buckets::<T>::iter().count();
// Return the weight consumed by the migration.
T::DbWeight::get().reads_writes(count as u64 + 1, count as u64 + 1)
}

0 comments on commit 62f3a5e

Please sign in to comment.