diff --git a/pallets/ddc-customers/src/migration.rs b/pallets/ddc-customers/src/migration.rs index 89aa46a7c..ee03c912d 100644 --- a/pallets/ddc-customers/src/migration.rs +++ b/pallets/ddc-customers/src/migration.rs @@ -67,7 +67,7 @@ pub mod v1 { } // Migrate to removable buckets -pub fn migrate_from_v0_to_v2() -> Weight { +pub fn migrate_to_v1() -> Weight { let on_chain_version = Pallet::::on_chain_storage_version(); if on_chain_version == 0 { let count = v0::BucketsCount::::get(); @@ -76,23 +76,22 @@ pub fn migrate_from_v0_to_v2() -> Weight { " >>> Updating DDC Customers storage. Migrating {} buckets...", count ); - Buckets::::translate::, _>( + v1::Buckets::::translate::, _>( |bucket_id: BucketId, bucket: v0::Bucket| { info!(target: LOG_TARGET, " Migrating bucket for bucket ID {:?}...", bucket_id); - Some(Bucket { + Some(v1::Bucket { bucket_id: bucket.bucket_id, owner_id: bucket.owner_id, cluster_id: bucket.cluster_id, is_public: bucket.is_public, is_removed: false, - total_customers_usage: None, }) }, ); // Update storage version. - StorageVersion::new(2).put::>(); + StorageVersion::new(1).put::>(); let count = v0::BucketsCount::::get(); info!( target: LOG_TARGET, @@ -106,14 +105,65 @@ pub fn migrate_from_v0_to_v2() -> Weight { } } -// Migrate to removable buckets -pub fn migrate_from_v1_to_v2() -> Weight { +pub struct MigrateToV1(sp_std::marker::PhantomData); +impl OnRuntimeUpgrade for MigrateToV1 { + fn on_runtime_upgrade() -> Weight { + migrate_to_v1::() + } + + #[cfg(feature = "try-runtime")] + fn pre_upgrade() -> Result, DispatchError> { + let prev_bucket_id = v0::BucketsCount::::get(); + let prev_count = v0::Buckets::::iter().count(); + + Ok((prev_bucket_id, prev_count as u64).encode()) + } + + #[cfg(feature = "try-runtime")] + fn post_upgrade(prev_state: Vec) -> Result<(), DispatchError> { + let (prev_bucket_id, prev_count): (u64, u64) = + Decode::decode(&mut &prev_state[..]).expect("pre_upgrade provides a valid state; qed"); + + let post_bucket_id = Pallet::::buckets_count(); + ensure!( + prev_bucket_id == post_bucket_id, + "the last bucket ID before and after the migration should be the same" + ); + + let post_count = Buckets::::iter().count() as u64; + ensure!( + prev_count == post_count, + "the bucket count before and after the migration should be the same" + ); + + let current_version = Pallet::::current_storage_version(); + let on_chain_version = Pallet::::on_chain_storage_version(); + + frame_support::ensure!(current_version == 1, "must_upgrade"); + ensure!( + current_version == on_chain_version, + "after migration, the current_version and on_chain_version should be the same" + ); + + Buckets::::iter().try_for_each(|(_id, bucket)| -> Result<(), &'static str> { + ensure!( + !bucket.is_removed, + "At this point all the bucket should have is_removed set to false" + ); + Ok(()) + })?; + Ok(()) + } +} + +// New migration to add total_customers_usage field +pub fn migrate_to_v2() -> Weight { let on_chain_version = Pallet::::on_chain_storage_version(); if on_chain_version == 1 { let count = v1::BucketsCount::::get(); info!( target: LOG_TARGET, - " >>> Updating DDC Customers storage. Migrating {} buckets...", count + " >>> Updating DDC Customers storage to v2. Migrating {} buckets...", count ); Buckets::::translate::, _>( @@ -125,7 +175,7 @@ pub fn migrate_from_v1_to_v2() -> Weight { owner_id: bucket.owner_id, cluster_id: bucket.cluster_id, is_public: bucket.is_public, - is_removed: false, + is_removed: bucket.is_removed, total_customers_usage: None, }) }, @@ -136,26 +186,26 @@ pub fn migrate_from_v1_to_v2() -> Weight { let count = v1::BucketsCount::::get(); info!( target: LOG_TARGET, - " <<< DDC Customers storage updated! Migrated {} buckets ✅", count + " <<< DDC Customers storage updated to v2! Migrated {} buckets ✅", count ); T::DbWeight::get().reads_writes(count + 2, count + 1) } else { - info!(target: LOG_TARGET, " >>> Unused migration!"); + info!(target: LOG_TARGET, " >>> Unused migration to v2!"); T::DbWeight::get().reads(1) } } -pub struct MigrateToV1(sp_std::marker::PhantomData); -impl OnRuntimeUpgrade for MigrateToV1 { +pub struct MigrateToV2(sp_std::marker::PhantomData); +impl OnRuntimeUpgrade for MigrateToV2 { fn on_runtime_upgrade() -> Weight { - migrate_from_v1_to_v2::() + migrate_to_v2::() } #[cfg(feature = "try-runtime")] fn pre_upgrade() -> Result, DispatchError> { - let prev_bucket_id = v0::BucketsCount::::get(); - let prev_count = v0::Buckets::::iter().count(); + let prev_bucket_id = v1::BucketsCount::::get(); + let prev_count = v1::Buckets::::iter().count(); Ok((prev_bucket_id, prev_count as u64).encode()) } @@ -180,7 +230,7 @@ impl OnRuntimeUpgrade for MigrateToV1 { let current_version = Pallet::::current_storage_version(); let on_chain_version = Pallet::::on_chain_storage_version(); - frame_support::ensure!(current_version == 1, "must_upgrade"); + frame_support::ensure!(current_version == 2, "must_upgrade"); ensure!( current_version == on_chain_version, "after migration, the current_version and on_chain_version should be the same" @@ -188,8 +238,8 @@ impl OnRuntimeUpgrade for MigrateToV1 { Buckets::::iter().try_for_each(|(_id, bucket)| -> Result<(), &'static str> { ensure!( - !bucket.is_removed, - "At this point all the bucket should have is_removed set to false" + bucket.total_customers_usage.is_none(), + "At this point all the bucket should have total_customers_usage set to None" ); Ok(()) })?; diff --git a/pallets/ddc-verification/src/lib.rs b/pallets/ddc-verification/src/lib.rs index dc945135f..11ac2f497 100644 --- a/pallets/ddc-verification/src/lib.rs +++ b/pallets/ddc-verification/src/lib.rs @@ -42,9 +42,9 @@ use sp_std::{collections::btree_map::BTreeMap, prelude::*}; pub mod weights; use itertools::Itertools; +use sp_staking::StakingInterface; use crate::weights::WeightInfo; -use sp_staking::StakingInterface; #[cfg(test)] pub(crate) mod mock; @@ -53,7 +53,7 @@ mod tests; #[frame_support::pallet] pub mod pallet { - use ddc_primitives::{traits::StakingVisitor, BucketId, MergeActivityHash, KEY_TYPE}; + use ddc_primitives::{BucketId, MergeActivityHash, KEY_TYPE}; use frame_support::PalletId; use sp_runtime::SaturatedConversion; @@ -2864,11 +2864,7 @@ pub mod pallet { pub fn set_current_validator(origin: OriginFor) -> DispatchResult { let caller: T::AccountId = ensure_signed(origin)?; - - ensure!( - >::get().contains(&caller), - Error::::NotValidatorStash - ); + ensure!(>::get().contains(&caller), Error::::NotValidatorStash); if Self::is_ocw_validator(caller.clone()) { log::info!("🏄‍ is_ocw_validator is a validator {:?}", caller.clone()); diff --git a/pallets/ddc-verification/src/mock.rs b/pallets/ddc-verification/src/mock.rs index d8e8794b5..efcd8c16b 100644 --- a/pallets/ddc-verification/src/mock.rs +++ b/pallets/ddc-verification/src/mock.rs @@ -1,6 +1,6 @@ use ddc_primitives::{ crypto, sr25519, - traits::{ClusterManager, ClusterQuery, StakingVisitor, StakingVisitorError}, + traits::{ClusterManager, ClusterQuery}, BucketId, ClusterNodeKind, ClusterNodeState, ClusterNodeStatus, ClusterNodesStats, ClusterStatus, PayoutError, PayoutState, StorageNodePubKey, MAX_PAYOUT_BATCH_COUNT, MAX_PAYOUT_BATCH_SIZE, @@ -229,26 +229,7 @@ impl crate::Config for Test { const MAX_PAYOUT_BATCH_SIZE: u16 = MAX_PAYOUT_BATCH_SIZE; const MAX_PAYOUT_BATCH_COUNT: u16 = MAX_PAYOUT_BATCH_COUNT; type ActivityHash = H256; - type StakingVisitor = TestStakingVisitor; -} - -pub struct TestStakingVisitor; -impl StakingVisitor for TestStakingVisitor { - fn has_activated_stake( - _node_pub_key: &NodePubKey, - _cluster_id: &ClusterId, - ) -> Result { - Ok(true) - } - fn has_stake(_node_pub_key: &NodePubKey) -> bool { - true - } - fn has_chilling_attempt(_node_pub_key: &NodePubKey) -> Result { - Ok(false) - } - fn stash_by_ctrl(controller: &T::AccountId) -> Result { - Ok(controller.clone()) - } + type StakingVisitor = Staking; } // Build genesis storage according to the mock runtime. diff --git a/runtime/cere-dev/src/lib.rs b/runtime/cere-dev/src/lib.rs index 665cd4ebc..9ccab23f8 100644 --- a/runtime/cere-dev/src/lib.rs +++ b/runtime/cere-dev/src/lib.rs @@ -521,8 +521,8 @@ fn transform_session_keys(v: AccountId, old: OldSessionKeys) -> SessionKeys { im_online: old.im_online, authority_discovery: old.authority_discovery, ddc_verification: { - - let mut id: ddc_primitives::sr25519::AuthorityId = sp_core::sr25519::Public::from_raw([0u8; 32]).into(); + let mut id: ddc_primitives::sr25519::AuthorityId = + sp_core::sr25519::Public::from_raw([0u8; 32]).into(); let id_raw: &mut [u8] = id.as_mut(); id_raw[0..32].copy_from_slice(v.as_ref()); id_raw[0..4].copy_from_slice(b"cer!"); @@ -1401,7 +1401,13 @@ pub type SignedPayload = generic::SignedPayload; pub type CheckedExtrinsic = generic::CheckedExtrinsic; /// Runtime migrations -type Migrations = (pallet_ddc_customers::migration::MigrateToV1, migrations::Unreleased); +type Migrations = ( + pallet_ddc_clusters::migrations::v2::MigrateToV2, + pallet_ddc_staking::migrations::v1::MigrateToV1, + pallet_ddc_customers::migration::MigrateToV2, + pallet_ddc_customers::migration::MigrateToV1, + migrations::Unreleased, +); /// Executive: handles dispatch to the various modules. pub type Executive = frame_executive::Executive< @@ -1413,7 +1419,6 @@ pub type Executive = frame_executive::Executive< Migrations, >; - pub mod migrations { use super::*; @@ -1427,9 +1432,7 @@ pub mod migrations { } /// Unreleased migrations. Add new ones here: - pub type Unreleased = ( - UpgradeSessionKeys, - ); + pub type Unreleased = (UpgradeSessionKeys,); } type EventRecord = frame_system::EventRecord< diff --git a/runtime/cere/src/lib.rs b/runtime/cere/src/lib.rs index ae609f21c..76933d0fe 100644 --- a/runtime/cere/src/lib.rs +++ b/runtime/cere/src/lib.rs @@ -515,8 +515,8 @@ fn transform_session_keys(v: AccountId, old: OldSessionKeys) -> SessionKeys { im_online: old.im_online, authority_discovery: old.authority_discovery, ddc_verification: { - - let mut id: ddc_primitives::sr25519::AuthorityId = sp_core::sr25519::Public::from_raw([0u8; 32]).into(); + let mut id: ddc_primitives::sr25519::AuthorityId = + sp_core::sr25519::Public::from_raw([0u8; 32]).into(); let id_raw: &mut [u8] = id.as_mut(); id_raw[0..32].copy_from_slice(v.as_ref()); id_raw[0..4].copy_from_slice(b"cer!"); @@ -1412,8 +1412,9 @@ pub type CheckedExtrinsic = generic::CheckedExtrinsic, pallet_ddc_staking::migrations::v1::MigrateToV1, + pallet_ddc_customers::migration::MigrateToV2, pallet_ddc_customers::migration::MigrateToV1, - migrations::Unreleased + migrations::Unreleased, ); pub mod migrations { @@ -1429,9 +1430,7 @@ pub mod migrations { } /// Unreleased migrations. Add new ones here: - pub type Unreleased = ( - UpgradeSessionKeys, - ); + pub type Unreleased = (UpgradeSessionKeys,); } /// Executive: handles dispatch to the various modules. pub type Executive = frame_executive::Executive<