diff --git a/pallets/capacity/src/lib.rs b/pallets/capacity/src/lib.rs index 5142c424f5..6ba052f4e2 100644 --- a/pallets/capacity/src/lib.rs +++ b/pallets/capacity/src/lib.rs @@ -71,8 +71,6 @@ mod benchmarking; #[cfg(test)] mod tests; -/// storage migrations -pub mod migration; pub mod weights; type BalanceOf = <::Currency as InspectFungible<::AccountId>>::Balance; diff --git a/pallets/capacity/src/migration/mod.rs b/pallets/capacity/src/migration/mod.rs deleted file mode 100644 index 5d9877e178..0000000000 --- a/pallets/capacity/src/migration/mod.rs +++ /dev/null @@ -1,4 +0,0 @@ -/// initial values for ProviderBoost storage -pub mod provider_boost_init; -/// Migration logic for 6 second block updates sideffects to capacity pallet -pub mod v4; diff --git a/pallets/capacity/src/migration/provider_boost_init.rs b/pallets/capacity/src/migration/provider_boost_init.rs deleted file mode 100644 index a1e30d2d02..0000000000 --- a/pallets/capacity/src/migration/provider_boost_init.rs +++ /dev/null @@ -1,49 +0,0 @@ -use crate::{Config, CurrentEraInfo, CurrentEraProviderBoostTotal, RewardEraInfo}; -use frame_support::{ - pallet_prelude::Weight, - traits::{Get, OnRuntimeUpgrade}, -}; -#[cfg(feature = "try-runtime")] -use sp_runtime::TryRuntimeError; - -#[cfg(feature = "try-runtime")] -use sp_std::vec::Vec; - -/// Initialization during runtime upgrade for Provider Boost storage -pub struct ProviderBoostInit(sp_std::marker::PhantomData); - -impl OnRuntimeUpgrade for ProviderBoostInit { - fn on_runtime_upgrade() -> Weight { - let current_era_info = CurrentEraInfo::::get(); // 1r - if current_era_info.eq(&RewardEraInfo::default()) { - CurrentEraInfo::::set(RewardEraInfo { - era_index: 0u32.into(), - started_at: frame_system::Pallet::::block_number(), - }); // 1w - CurrentEraProviderBoostTotal::::set(0u32.into()); // 1w - T::DbWeight::get().reads_writes(2, 1) - } else { - T::DbWeight::get().reads(1) - } - } - - #[cfg(feature = "try-runtime")] - fn pre_upgrade() -> Result, TryRuntimeError> { - if CurrentEraInfo::::exists() { - log::info!("CurrentEraInfo exists; initialization should be skipped."); - } else { - log::info!("CurrentEraInfo not found. Initialization should proceed."); - } - Ok(Vec::default()) - } - - #[cfg(feature = "try-runtime")] - fn post_upgrade(_state: Vec) -> Result<(), TryRuntimeError> { - assert!(CurrentEraInfo::::exists()); - let current_block = frame_system::Pallet::::block_number(); - let info = CurrentEraInfo::::get(); - assert_eq!(info.started_at, current_block); - log::info!("CurrentEraInfo.started_at is set to {:?}.", info.started_at); - Ok(()) - } -} diff --git a/pallets/capacity/src/migration/v4.rs b/pallets/capacity/src/migration/v4.rs deleted file mode 100644 index f1f06df928..0000000000 --- a/pallets/capacity/src/migration/v4.rs +++ /dev/null @@ -1,132 +0,0 @@ -use crate::{Config, EpochLength, Pallet}; -use frame_support::{ - pallet_prelude::{GetStorageVersion, Weight}, - traits::{Get, OnRuntimeUpgrade, StorageVersion}, -}; -use frame_system::pallet_prelude::BlockNumberFor; - -const LOG_TARGET: &str = "runtime::capacity"; - -#[cfg(feature = "try-runtime")] -use sp_std::vec::Vec; - -/// The OnRuntimeUpgrade implementation for this storage migration -pub struct MigrationToV4(sp_std::marker::PhantomData); -impl MigrationToV4 -where - T: Config, -{ - /// Update the epoch length to double the current value - pub fn update_epoch_length() -> Weight { - let current_epoch_length = EpochLength::::get(); - let new_epoch_length: BlockNumberFor = current_epoch_length * 2u32.into(); - log::info!(target: LOG_TARGET, "🔄 Capacity EpochLength update from {:?} to {:?}", current_epoch_length, new_epoch_length); - - EpochLength::::put(new_epoch_length); - - T::DbWeight::get().reads_writes(1, 1) - } -} - -impl OnRuntimeUpgrade for MigrationToV4 -where - T: Config, -{ - fn on_runtime_upgrade() -> Weight { - let on_chain_version = Pallet::::on_chain_storage_version(); // 1r - - if on_chain_version.ge(&4) { - log::info!(target: LOG_TARGET, "Old Capacity EpochLength migration attempted to run. Please remove"); - return T::DbWeight::get().reads(1); - } - - log::info!(target: LOG_TARGET, "🔄 Capacity EpochLength update migration started"); - // The migration started with 1r to get the STORAGE_VERSION - let mut total_weight = T::DbWeight::get().reads_writes(1, 0); - - total_weight += Self::update_epoch_length(); - - StorageVersion::new(4).put::>(); // 1 w - - total_weight = total_weight.saturating_add(T::DbWeight::get().reads_writes(0, 1)); - - log::info!(target: LOG_TARGET, "🔄 Capacity EpochLength second update migration finished"); - - total_weight - } - - #[cfg(feature = "try-runtime")] - fn pre_upgrade() -> Result, sp_runtime::TryRuntimeError> { - use frame_support::storage::generator::StorageValue; - use sp_std::vec; - let on_chain_version = Pallet::::on_chain_storage_version(); - if on_chain_version >= 4 { - return Ok(Vec::new()); - } - - let pallet_prefix = EpochLength::::pallet_prefix(); - let storage_prefix = EpochLength::::storage_prefix(); - assert_eq!(&b"Capacity"[..], pallet_prefix); - assert_eq!(&b"EpochLength"[..], storage_prefix); - log::info!(target: LOG_TARGET, "Running pre_upgrade..."); - - let current_epoch_length = EpochLength::::get(); - - #[cfg(feature = "frequency")] - assert_eq!(current_epoch_length, 7_200u32.into()); - #[cfg(feature = "frequency-testnet")] - assert_eq!(current_epoch_length, 100u32.into()); - - log::info!(target: LOG_TARGET, "Finish pre_upgrade for with current epoch length {:?} to ", current_epoch_length,); - Ok(vec![]) - } - - #[cfg(feature = "try-runtime")] - fn post_upgrade(_state: Vec) -> Result<(), sp_runtime::TryRuntimeError> { - let on_chain_version = Pallet::::on_chain_storage_version(); - if on_chain_version >= 4 { - return Ok(()); - } - - assert_eq!(on_chain_version, crate::pallet::STORAGE_VERSION); - - #[cfg(feature = "frequency")] - { - let post_upgrade_epoch_length = EpochLength::::get(); - assert_eq!(post_upgrade_epoch_length, 14_400u32.into()); - } - - #[cfg(feature = "frequency-testnet")] - { - let post_upgrade_epoch_length = EpochLength::::get(); - assert_eq!(post_upgrade_epoch_length, 200u32.into()); - } - - log::info!(target: LOG_TARGET, "✅ migration post_upgrade checks passed"); - Ok(()) - } -} - -#[cfg(test)] -mod test { - use super::*; - use crate::tests::mock::{Test as T, *}; - - type MigrationOf = MigrationToV4; - - #[test] - fn migration_works() { - new_test_ext().execute_with(|| { - EpochLength::::put(7_200u32); - - assert_eq!(EpochLength::::get(), 7_200u32); - - MigrationOf::::on_runtime_upgrade(); - - let on_chain_version = Pallet::::on_chain_storage_version(); - assert_eq!(on_chain_version, crate::pallet::STORAGE_VERSION); - - assert_eq!(EpochLength::::get(), 14_400u32); - }) - } -} diff --git a/pallets/messages/src/lib.rs b/pallets/messages/src/lib.rs index 135dbdec1a..d77bd7c3c0 100644 --- a/pallets/messages/src/lib.rs +++ b/pallets/messages/src/lib.rs @@ -22,8 +22,6 @@ #[cfg(feature = "runtime-benchmarks")] mod benchmarking; -/// migration module -pub mod migration; #[cfg(test)] mod tests; @@ -55,8 +53,6 @@ pub use weights::*; use cid::Cid; use frame_system::pallet_prelude::*; -const LOG_TARGET: &str = "runtime::messages"; - #[frame_support::pallet] pub mod pallet { use super::*; diff --git a/pallets/messages/src/migration/mod.rs b/pallets/messages/src/migration/mod.rs deleted file mode 100644 index c34354a101..0000000000 --- a/pallets/messages/src/migration/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -/// migrations to v2 -pub mod v2; diff --git a/pallets/messages/src/migration/v2.rs b/pallets/messages/src/migration/v2.rs deleted file mode 100644 index a681fdfc2d..0000000000 --- a/pallets/messages/src/migration/v2.rs +++ /dev/null @@ -1,149 +0,0 @@ -use crate::{BlockNumberFor, Config, Message, MessagesV2, Pallet, SchemaId, LOG_TARGET}; -use frame_support::{pallet_prelude::*, storage_alias, traits::OnRuntimeUpgrade, weights::Weight}; -use log; -use sp_runtime::Saturating; - -#[cfg(feature = "try-runtime")] -use sp_runtime::TryRuntimeError; -#[cfg(feature = "try-runtime")] -use sp_std::vec::Vec; - -/// old structures and storages -pub mod old { - use super::*; - use common_primitives::msa::MessageSourceId; - use sp_std::fmt::Debug; - - /// old message structure that was stored - #[derive(Default, Encode, Decode, PartialEq, Debug, TypeInfo, Eq, MaxEncodedLen)] - #[scale_info(skip_type_params(MaxDataSize))] - #[codec(mel_bound(MaxDataSize: MaxEncodedLen))] - pub struct OldMessage - where - MaxDataSize: Get + Debug, - { - /// Data structured by the associated schema's model. - pub payload: BoundedVec, - /// Message source account id of the Provider. This may be the same id as contained in `msa_id`, - /// indicating that the original source MSA is acting as its own provider. An id differing from that - /// of `msa_id` indicates that `provider_msa_id` was delegated by `msa_id` to send this message on - /// its behalf. - pub provider_msa_id: MessageSourceId, - /// Message source account id (the original source). - pub msa_id: Option, - /// Stores index of message in block to keep total order. - pub index: u16, - } - - /// old permanent storage for messages mapped by block number and schema id. - #[storage_alias] - pub(crate) type Messages = StorageDoubleMap< - Pallet, - Twox64Concat, - BlockNumberFor, - Twox64Concat, - SchemaId, - BoundedVec< - OldMessage<::MessagesMaxPayloadSizeBytes>, - ConstU32<200>, - >, - ValueQuery, - >; -} -/// migration to v2 implementation -pub struct MigrateToV2(PhantomData); - -impl OnRuntimeUpgrade for MigrateToV2 { - fn on_runtime_upgrade() -> Weight { - migrate_to_v2::() - } - - #[cfg(feature = "try-runtime")] - fn pre_upgrade() -> Result, TryRuntimeError> { - use frame_support::storage::generator::StorageDoubleMap; - log::info!(target: LOG_TARGET, "Running pre_upgrade..."); - let onchain_version = Pallet::::on_chain_storage_version(); - if onchain_version < 2 { - let pallet_prefix = old::Messages::::pallet_prefix(); - let storage_prefix = old::Messages::::storage_prefix(); - assert_eq!(&b"Messages"[..], pallet_prefix); - assert_eq!(&b"Messages"[..], storage_prefix); - - let mut count = 0u32; - for (_, _, messages) in old::Messages::::iter() { - count += messages.len() as u32; - } - log::info!(target: LOG_TARGET, "Finish pre_upgrade for {:?}", count); - return Ok(count.encode()) - } - Ok(Vec::new()) - } - - #[cfg(feature = "try-runtime")] - fn post_upgrade(state: Vec) -> Result<(), TryRuntimeError> { - log::info!(target: LOG_TARGET, "Running post_upgrade..."); - let onchain_version = Pallet::::on_chain_storage_version(); - if onchain_version < 2 { - let old_count: u32 = Decode::decode(&mut state.as_slice()).expect( - "the state parameter should be something that was generated by pre_upgrade", - ); - - let count = old::Messages::::iter().count(); - let moved_count = MessagesV2::::iter().count(); - - log::info!(target: LOG_TARGET, "Finish post_upgrade for {:?}", moved_count); - let onchain_version = Pallet::::on_chain_storage_version(); - - assert_eq!(count, 0usize); - assert_eq!(moved_count, old_count as usize); - assert_eq!(onchain_version, crate::pallet::STORAGE_VERSION); - } - Ok(()) - } -} -/// migrating to v2 -pub fn migrate_to_v2() -> Weight { - log::info!(target: LOG_TARGET, "Running storage migration..."); - let onchain_version = Pallet::::on_chain_storage_version(); - let current_version = Pallet::::in_code_storage_version(); - log::info!(target: LOG_TARGET, "onchain_version= {:?}, current_version={:?}", onchain_version, current_version); - - if onchain_version < 2 { - let mut reads = 1u64; - let mut writes = 0u64; - let mut bytes = 0u64; - for (block_number, schema_id, messages) in old::Messages::::drain() { - bytes = bytes.saturating_add(messages.encode().len() as u64); - - for message in &messages { - let new_msg = Message { - provider_msa_id: message.provider_msa_id, - msa_id: message.msa_id, - payload: message.payload.clone(), - }; - bytes = bytes.saturating_add(new_msg.encode().len() as u64); - MessagesV2::::insert((block_number, schema_id, message.index), new_msg); - } - - reads.saturating_inc(); - writes = writes.saturating_add(messages.len() as u64 + 1); - } - - // Set storage version to `2`. - StorageVersion::new(2).put::>(); - writes.saturating_inc(); - - log::info!(target: LOG_TARGET, "Storage migrated to version 2 read={:?}, write={:?}, bytes={:?}", reads, writes, bytes); - let weights = T::DbWeight::get().reads_writes(reads, writes).add_proof_size(bytes); - log::info!(target: LOG_TARGET, "Migration Calculated weights={:?}",weights); - weights - } else { - log::info!( - target: LOG_TARGET, - "Migration did not execute. This probably should be removed onchain:{:?}, current:{:?}", - onchain_version, - current_version - ); - T::DbWeight::get().reads(1) - } -} diff --git a/pallets/messages/src/tests/other_tests.rs b/pallets/messages/src/tests/other_tests.rs index 7cced58bfb..2494b1a55b 100644 --- a/pallets/messages/src/tests/other_tests.rs +++ b/pallets/messages/src/tests/other_tests.rs @@ -1,15 +1,6 @@ -use crate::{ - migration::{v2, v2::old::OldMessage}, - tests::mock::*, - BlockMessageIndex, Error, Event as MessageEvent, Message, MessagesV2, -}; +use crate::{tests::mock::*, BlockMessageIndex, Error, Event as MessageEvent, Message, MessagesV2}; use common_primitives::{messages::MessageResponse, schema::*}; -use frame_support::{ - assert_err, assert_noop, assert_ok, - pallet_prelude::{GetStorageVersion, StorageVersion}, - traits::OnInitialize, - BoundedVec, -}; +use frame_support::{assert_err, assert_noop, assert_ok, traits::OnInitialize, BoundedVec}; use frame_system::{EventRecord, Phase}; use multibase::Base; use parity_scale_codec::Encode; @@ -634,66 +625,3 @@ fn map_to_response_ipfs() { }; assert_eq!(msg.map_to_response(42, PayloadLocation::IPFS, 1), expected); } - -#[test] -fn migration_to_v2_should_work_as_expected() { - new_test_ext().execute_with(|| { - // Setup - let schema_id: SchemaId = IPFS_SCHEMA_ID; - let cid = &DUMMY_CID_BASE32[..]; - let message_per_block = vec![3, 4, 5, 6]; - let payload = ( - multibase::decode(sp_std::str::from_utf8(cid).unwrap()).unwrap().1, - IPFS_PAYLOAD_LENGTH, - ) - .encode(); - - let mut counter = 0; - for (idx, count) in message_per_block.iter().enumerate() { - let mut list = BoundedVec::default(); - for _ in 0..*count { - list.try_push(OldMessage { - msa_id: Some(10), - payload: payload.clone().try_into().unwrap(), - index: counter, - provider_msa_id: 1, - }) - .unwrap(); - counter += 1; - } - v2::old::Messages::::insert(idx as u32, schema_id, list); - } - - let _ = v2::migrate_to_v2::(); - - let old_count = v2::old::Messages::::iter().count(); - let new_count = MessagesV2::::iter().count(); - let current_version = MessagesPallet::in_code_storage_version(); - - assert_eq!(old_count, 0); - assert_eq!(new_count, message_per_block.iter().sum::()); - assert_eq!(current_version, StorageVersion::new(2)); - - let mut total_index = 0u16; - for (block, count) in message_per_block.iter().enumerate() { - for _ in 0..*count { - assert!(MessagesV2::::get((block as u32, schema_id, total_index)).is_some()); - total_index += 1; - } - // should not exist - assert!(MessagesV2::::get((block as u32, schema_id, total_index)).is_none()); - } - }); -} - -#[test] -fn migration_to_v2_should_have_correct_prefix() { - new_test_ext().execute_with(|| { - use frame_support::storage::generator::StorageDoubleMap; - let pallet_prefix = v2::old::Messages::::pallet_prefix(); - let storage_prefix = v2::old::Messages::::storage_prefix(); - - assert_eq!(&b"MessagesPallet"[..], pallet_prefix); - assert_eq!(&b"Messages"[..], storage_prefix); - }); -} diff --git a/pallets/schemas/src/lib.rs b/pallets/schemas/src/lib.rs index 14b44f85cd..2f70f5f9f8 100644 --- a/pallets/schemas/src/lib.rs +++ b/pallets/schemas/src/lib.rs @@ -49,8 +49,6 @@ mod benchmarking; #[cfg(feature = "runtime-benchmarks")] use common_primitives::benchmarks::SchemaBenchmarkHelper; use common_primitives::schema::{SchemaInfoResponse, SchemaVersionResponse}; -/// migration module -pub mod migration; mod types; pub use pallet::*; @@ -60,8 +58,6 @@ pub use weights::*; mod serde; -const LOG_TARGET: &str = "runtime::schemas"; - #[frame_support::pallet] pub mod pallet { use super::*; diff --git a/pallets/schemas/src/migration/mod.rs b/pallets/schemas/src/migration/mod.rs deleted file mode 100644 index 52f1e87d7d..0000000000 --- a/pallets/schemas/src/migration/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -/// migrations to v3 -pub mod v4; diff --git a/pallets/schemas/src/migration/v4.rs b/pallets/schemas/src/migration/v4.rs deleted file mode 100644 index 00a4e02d16..0000000000 --- a/pallets/schemas/src/migration/v4.rs +++ /dev/null @@ -1,171 +0,0 @@ -#[cfg(feature = "try-runtime")] -use crate::types::SCHEMA_STORAGE_VERSION; -use crate::{ - pallet::{SchemaInfos, SchemaNameToIds}, - Config, Pallet, SchemaId, SchemaName, LOG_TARGET, -}; -use common_primitives::utils::{get_chain_type_by_genesis_hash, DetectedChainType}; -use frame_support::{pallet_prelude::*, traits::OnRuntimeUpgrade, weights::Weight}; -use frame_system::pallet_prelude::BlockNumberFor; -use log; -use sp_runtime::Saturating; -#[cfg(feature = "try-runtime")] -use sp_runtime::TryRuntimeError; -use sp_std::{collections::btree_map::BTreeMap, vec::Vec}; - -/// get known schema names for mainnet -pub fn get_known_schemas() -> BTreeMap, bool)> { - let genesis_block: BlockNumberFor = 0u32.into(); - let genesis = >::block_hash(genesis_block); - - if let DetectedChainType::FrequencyMainNet = - get_chain_type_by_genesis_hash(&genesis.encode()[..]) - { - // only return what needs to change which for this case is only on mainnet - // (schema_id, name, clear prior versions) - BTreeMap::from([ - (5, (b"dsnp.update".to_vec(), true)), - (19, (b"dsnp.update".to_vec(), false)), - (6, (b"dsnp.profile".to_vec(), true)), - (7, (b"dsnp.public-key-key-agreement".to_vec(), false)), - (8, (b"dsnp.public-follows".to_vec(), false)), - (9, (b"dsnp.private-follows".to_vec(), false)), - (10, (b"dsnp.private-connections".to_vec(), false)), - ]) - } else { - BTreeMap::new() - } -} - -/// migration to v4 implementation -pub struct MigrateToV4(PhantomData); - -impl OnRuntimeUpgrade for MigrateToV4 { - fn on_runtime_upgrade() -> Weight { - migrate_to_v4::() - } - - #[cfg(feature = "try-runtime")] - fn pre_upgrade() -> Result, TryRuntimeError> { - log::info!(target: LOG_TARGET, "Running pre_upgrade..."); - let on_chain_version = Pallet::::on_chain_storage_version(); - let genesis_block: BlockNumberFor = 0u32.into(); - let genesis = >::block_hash(genesis_block); - if on_chain_version >= 4 { - return Ok(Vec::new()) - } - log::info!(target: LOG_TARGET, "Found genesis... {:?}", genesis); - let detected_chain = get_chain_type_by_genesis_hash(&genesis.encode()[..]); - log::info!(target: LOG_TARGET,"Detected Chain is {:?}", detected_chain); - Ok(Vec::new()) - } - - #[cfg(feature = "try-runtime")] - fn post_upgrade(_: Vec) -> Result<(), TryRuntimeError> { - log::info!(target: LOG_TARGET, "Running post_upgrade..."); - let on_chain_version = Pallet::::on_chain_storage_version(); - if on_chain_version > 4 { - return Ok(()) - } - let onchain_version = Pallet::::on_chain_storage_version(); - assert_eq!(onchain_version, SCHEMA_STORAGE_VERSION); - // check to ensure updates took place - let known_schemas = get_known_schemas::(); - for (schema_id, (schema_name, _should_clear)) in known_schemas.into_iter() { - // safe to use unwrap since only used in try_runtime - let bounded = BoundedVec::try_from(schema_name).unwrap(); - let parsed_name = SchemaName::try_parse::(bounded, true).unwrap(); - let current_versions = - SchemaNameToIds::::get(parsed_name.namespace, parsed_name.descriptor); - - // Just check that it contains the correct value, not position anymore - assert!( - current_versions.ids.iter().any(|&id| id == schema_id), - "Current versions does not contain schema_id {}", - schema_id - ); - } - log::info!(target: LOG_TARGET, "Finished post_upgrade"); - Ok(()) - } -} - -/// migrating to v4 -pub fn migrate_to_v4() -> Weight { - log::info!(target: LOG_TARGET, "Running storage migration..."); - let onchain_version = Pallet::::on_chain_storage_version(); - let current_version = Pallet::::in_code_storage_version(); - log::info!(target: LOG_TARGET, "onchain_version= {:?}, current_version={:?}", onchain_version, current_version); - let each_layer_access: u64 = 33 * 16; - - if onchain_version < 4 { - let known_schemas = get_known_schemas::(); - let mut reads = 1u64; - let mut writes = 0u64; - let mut bytes = 0u64; - - for (schema_id, (schema_name, should_clear)) in known_schemas.iter() { - reads.saturating_inc(); - - if let Some(mut schema) = SchemaInfos::::get(&schema_id) { - bytes = bytes.saturating_add(schema.encode().len() as u64); - bytes = bytes.saturating_add(each_layer_access * 3); // three layers in merkle tree - - match BoundedVec::try_from(schema_name.clone()) { - Ok(bounded_name) => match SchemaName::try_parse::(bounded_name, true) { - Ok(parsed_name) => { - let _ = SchemaNameToIds::::try_mutate( - parsed_name.namespace, - parsed_name.descriptor, - |schema_version_id| -> Result<(), DispatchError> { - bytes = bytes - .saturating_add(schema_version_id.encode().len() as u64); - - // clear the old/wrong one in case they exist - if *should_clear { - schema_version_id.ids.clear(); - } - let _ = schema_version_id.add::(*schema_id); - - // set schema as having a name - schema.has_name = true; - SchemaInfos::::set(&schema_id, Some(schema)); - writes.saturating_inc(); - - Ok(()) - }, - ); - reads.saturating_inc(); - writes.saturating_inc(); - }, - Err(_) => { - log::error!(target: LOG_TARGET, "Not able to parse the name {:?}", schema_name); - }, - }, - Err(_) => { - log::error!(target: LOG_TARGET, "Was not able to get bounded vec {:?}", schema_name); - }, - } - } else { - log::error!(target: LOG_TARGET, "Schema id {:?} does not exist!", schema_id); - } - } - - // Set storage version to `4`. - StorageVersion::new(4).put::>(); - writes.saturating_inc(); - - log::info!(target: LOG_TARGET, "Storage migrated to version 4 read={:?}, write={:?}, bytes={:?}", reads, writes, bytes); - let weights = T::DbWeight::get().reads_writes(reads, writes).add_proof_size(bytes); - log::info!(target: LOG_TARGET, "Migration Calculated weights={:?}",weights); - weights - } else { - log::info!( - target: LOG_TARGET, - "Migration did not execute. This probably should be removed onchain:{:?}, current:{:?}", - onchain_version, - current_version - ); - T::DbWeight::get().reads(1) - } -} diff --git a/pallets/schemas/src/tests/migrations_tests.rs b/pallets/schemas/src/tests/migrations_tests.rs deleted file mode 100644 index af2553381e..0000000000 --- a/pallets/schemas/src/tests/migrations_tests.rs +++ /dev/null @@ -1,41 +0,0 @@ -use crate::{ - migration::v4, - tests::mock::{ - create_bounded_schema_vec, new_test_ext, sudo_set_max_schema_size, test_public, - RuntimeOrigin, SchemasPallet, Test, - }, -}; -use common_primitives::{node::AccountId, schema::*}; -use frame_support::{ - assert_ok, pallet_prelude::StorageVersion, traits::GetStorageVersion, BoundedVec, -}; - -#[test] -fn schemas_migration_to_v4_should_work_as_expected() { - new_test_ext().execute_with(|| { - // Arrange - sudo_set_max_schema_size(); - let sender: AccountId = test_public(5); - let schemas = vec![r#"{"latitude": 48.858093,"longitude": 2.294694}"#; 20]; - for fields in schemas.iter() { - assert_ok!(SchemasPallet::create_schema_v3( - RuntimeOrigin::signed(sender.clone()), - create_bounded_schema_vec(fields), - ModelType::AvroBinary, - PayloadLocation::OnChain, - BoundedVec::default(), - None, - )); - } - - // Act - let _ = v4::migrate_to_v4::(); - - // Assert - let current_version = SchemasPallet::in_code_storage_version(); - assert_eq!(current_version, StorageVersion::new(4)); - - let known_schemas = v4::get_known_schemas::(); - assert_eq!(known_schemas.len(), 0); - }); -} diff --git a/pallets/schemas/src/tests/mod.rs b/pallets/schemas/src/tests/mod.rs index 55e75503fb..70188c838c 100644 --- a/pallets/schemas/src/tests/mod.rs +++ b/pallets/schemas/src/tests/mod.rs @@ -1,5 +1,4 @@ mod deprecated_tests; -mod migrations_tests; pub mod mock; mod other_tests; mod serde_tests; diff --git a/pallets/stateful-storage/src/lib.rs b/pallets/stateful-storage/src/lib.rs index c9ace189d5..81f912fe06 100644 --- a/pallets/stateful-storage/src/lib.rs +++ b/pallets/stateful-storage/src/lib.rs @@ -36,8 +36,7 @@ mod tests; #[cfg(feature = "runtime-benchmarks")] use common_primitives::benchmarks::{MsaBenchmarkHelper, SchemaBenchmarkHelper}; use sp_std::prelude::*; -/// storage migrations -pub mod migration; + mod stateful_child_tree; pub mod types; pub mod weights; @@ -61,13 +60,9 @@ use sp_core::{bounded::BoundedVec, crypto::AccountId32}; use sp_runtime::{traits::Convert, DispatchError, MultiSignature}; pub use weights::*; -const LOG_TARGET: &str = "runtime::stateful-storage"; - #[frame_support::pallet] pub mod pallet { use super::*; - use crate::migration::v1::{get_chain_type, get_testnet_msa_ids}; - use common_primitives::utils::DetectedChainType; #[pallet::config] pub trait Config: frame_system::Config { @@ -233,19 +228,6 @@ pub mod pallet { #[pallet::hooks] impl Hooks> for Pallet { fn on_initialize(_current: BlockNumberFor) -> Weight { - // this should get removed after rolling out to testnet - if DetectedChainType::FrequencyPaseoTestNet == get_chain_type::() || cfg!(test) { - let page_index = >::get(); - if get_testnet_msa_ids().len() as u32 > page_index * MIGRATION_PAGE_SIZE { - let (weight, _) = migration::v1::paginated_migration_testnet::( - MIGRATION_PAGE_SIZE, - page_index, - ); - >::set(page_index.saturating_add(1)); - return T::DbWeight::get().reads_writes(1, 1).saturating_add(weight) - } - return T::DbWeight::get().reads_writes(1, 0) - } Weight::zero() } } diff --git a/pallets/stateful-storage/src/migration/mod.rs b/pallets/stateful-storage/src/migration/mod.rs deleted file mode 100644 index 5d89e038e1..0000000000 --- a/pallets/stateful-storage/src/migration/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -/// migrations to v1 -pub mod v1; diff --git a/pallets/stateful-storage/src/migration/v1.rs b/pallets/stateful-storage/src/migration/v1.rs deleted file mode 100644 index b1dd853f76..0000000000 --- a/pallets/stateful-storage/src/migration/v1.rs +++ /dev/null @@ -1,256 +0,0 @@ -#[cfg(feature = "try-runtime")] -use crate::types::STATEFUL_STORAGE_VERSION; -use crate::{ - stateful_child_tree::StatefulChildTree, - types::{ - ItemAction, ItemizedKey, ItemizedOperations, ItemizedPage, Page, ITEMIZED_STORAGE_PREFIX, - PALLET_STORAGE_PREFIX, - }, - Config, Pallet, LOG_TARGET, -}; -use common_primitives::{ - msa::MessageSourceId, - utils::{get_chain_type_by_genesis_hash, DetectedChainType}, -}; -use frame_support::{pallet_prelude::*, traits::OnRuntimeUpgrade, weights::Weight}; -use frame_system::pallet_prelude::BlockNumberFor; -use log; -#[cfg(feature = "try-runtime")] -use sp_core::hexdisplay::HexDisplay; -use sp_runtime::Saturating; -#[cfg(feature = "try-runtime")] -use sp_runtime::TryRuntimeError; -use sp_std::{vec, vec::Vec}; - -/// testnet specific msa ids for migration -pub fn get_testnet_msa_ids() -> Vec { - vec![ - 8004, 8009, 8816, 8817, 8818, 8819, 8820, 8822, 8823, 8824, 8825, 8826, 9384, 9753, 9919, - 9992, 9994, 9996, 9997, 10009, 10010, 10012, 10013, 10014, 10015, 10019, 10020, 10021, - 10022, 10023, 10024, 10025, 10026, 10027, 10028, 10029, 10030, 10031, 10032, 10033, 10034, - 10035, 10036, 10037, 10038, 10039, 10040, 10041, 10042, 10043, 10044, 10045, 10046, 10047, - 10048, 10049, 10050, 10051, 10052, 10053, 10054, 10055, 10056, 10057, 10058, 10059, 10061, - 10062, 10064, 10067, 10068, 10069, 10070, 10071, 10072, 10075, 10076, 10077, 10078, 10079, - 10138, 10139, 10140, 10206, 10207, 10209, 10212, 10218, 10219, 10220, 10221, 10222, 10223, - 10224, 10231, 10232, 10233, 10234, 10235, 10236, 10237, 10238, 10239, 10240, 10241, 10242, - 10243, 10247, 10248, 10251, 10253, 10254, 10255, 10256, 10257, 10258, 10259, 10260, 10261, - 10262, 10263, 10264, 10265, 10266, 10267, 10268, 10269, 10270, 10271, 10272, 10273, 10274, - 10275, 10287, 10288, 10289, 10290, 10291, 10292, 10293, 10294, 10295, 10296, 10297, 10298, - 10299, 10300, 10301, 10302, 10303, 10304, 10305, 10306, 10307, 10308, 10309, 10311, 10312, - 10313, 10314, 10315, 10316, 10317, 10318, 10319, 10320, 10321, 10322, 10323, 10324, 10325, - 10326, 10327, 10328, 10329, - ] -} - -/// returns the chain type from genesis hash -pub fn get_chain_type() -> DetectedChainType { - let genesis_block: BlockNumberFor = 0u32.into(); - let genesis = >::block_hash(genesis_block); - get_chain_type_by_genesis_hash(&genesis.encode()[..]) -} - -/// get the msa ids with key migrations -pub fn get_msa_ids() -> Vec { - let chain_type = get_chain_type::(); - if let DetectedChainType::FrequencyMainNet = chain_type { - vec![ - 227, 542, 1249820, 1287729, 1288925, 1309067, 1309241, 1309258, 1309367, 1309397, - 1329112, 1329535, 1330067, - ] - } else { - if cfg!(test) { - // this allows to test the mainnet path - vec![1] - } else { - // we are going to use hooks for this multi-block migration so this is empty to only flag that - // it as done for consistency - vec![] - } - } -} - -/// migration to v1 implementation -pub struct MigrateToV1(PhantomData); - -impl OnRuntimeUpgrade for MigrateToV1 { - fn on_runtime_upgrade() -> Weight { - migrate_to_v1::() - } - - #[cfg(feature = "try-runtime")] - fn pre_upgrade() -> Result, TryRuntimeError> { - log::info!(target: LOG_TARGET, "Running pre_upgrade..."); - let on_chain_version = Pallet::::on_chain_storage_version(); - let genesis_block: BlockNumberFor = 0u32.into(); - let genesis = >::block_hash(genesis_block); - if on_chain_version >= 1 { - return Ok(Vec::new()) - } - log::info!(target: LOG_TARGET, "Found genesis... {:?}", genesis); - let detected_chain = get_chain_type_by_genesis_hash(&genesis.encode()[..]); - log::info!(target: LOG_TARGET,"Detected Chain is {:?}", detected_chain); - Ok(Vec::new()) - } - - #[cfg(feature = "try-runtime")] - fn post_upgrade(_: Vec) -> Result<(), TryRuntimeError> { - log::info!(target: LOG_TARGET, "Running post_upgrade..."); - let on_chain_version = Pallet::::on_chain_storage_version(); - if on_chain_version > 1 { - return Ok(()) - } - let onchain_version = Pallet::::on_chain_storage_version(); - assert_eq!(onchain_version, STATEFUL_STORAGE_VERSION); - // check to ensure updates took place - let schema_id = get_schema_id(); - let msa_ids = get_msa_ids::(); - for msa_id in msa_ids.into_iter() { - let itemized: ItemizedPage = Pallet::::get_itemized_page_for(msa_id, schema_id) - .map_err(|_| TryRuntimeError::Other("can not get storage"))? - .ok_or(TryRuntimeError::Other("no storage"))?; - let (_, val) = as ItemizedOperations>::try_parse( - &itemized, false, - ) - .map_err(|_| TryRuntimeError::Other("can not parse storage"))? - .items - .clone() - .into_iter() - .next() - .ok_or(TryRuntimeError::Other("no item"))?; - - assert_eq!(val.len(), 33); - log::info!(target: LOG_TARGET, "{:?}", HexDisplay::from(&val)); - } - log::info!(target: LOG_TARGET, "Finished post_upgrade"); - Ok(()) - } -} - -/// migrating to v1 -pub fn migrate_to_v1() -> Weight { - log::info!(target: LOG_TARGET, "Running storage migration..."); - let onchain_version = Pallet::::on_chain_storage_version(); - let current_version = Pallet::::in_code_storage_version(); - log::info!(target: LOG_TARGET, "onchain_version= {:?}, current_version={:?}", onchain_version, current_version); - if onchain_version < 1 { - let msa_ids = get_msa_ids::(); - let weights = migrate_msa_ids::(&msa_ids[..]); - // Set storage version to `1`. - StorageVersion::new(1).put::>(); - let total_weight = T::DbWeight::get().writes(1).saturating_add(weights); - log::info!(target: LOG_TARGET, "Migration Calculated weights={:?}",total_weight); - total_weight - } else { - log::info!( - target: LOG_TARGET, - "Migration did not execute. This probably should be removed onchain:{:?}, current:{:?}", - onchain_version, - current_version - ); - T::DbWeight::get().reads(1) - } -} - -/// migrating all msa_ids -pub fn migrate_msa_ids(msa_ids: &[MessageSourceId]) -> Weight { - let schema_id = get_schema_id(); - let key: ItemizedKey = (schema_id,); - let each_layer_access: u64 = 33 * 16; - let mut reads = 1u64; - let mut writes = 0u64; - let mut bytes = 0u64; - - for msa_id in msa_ids.iter() { - reads.saturating_inc(); - // get the itemized storages - let itemized_result: Result>, _> = - Pallet::::get_itemized_page_for(*msa_id, schema_id); - match itemized_result { - Ok(Some(existing_page)) => { - bytes = bytes.saturating_add(existing_page.encode().len() as u64); - bytes = bytes.saturating_add(each_layer_access * 3); // three layers in merkle tree - - match as ItemizedOperations>::try_parse( - &existing_page, - false, - ) { - Ok(parsed_page) => match parsed_page.items.clone().into_iter().next() { - Some((_, existing_value)) => match existing_value.len() { - 32usize => { - // 64 is decimal value for 0x40 - let mut prefixed = vec![64u8]; - prefixed.extend_from_slice(existing_value); - let bounded: BoundedVec = - prefixed.try_into().unwrap_or_default(); - - let empty_page = ItemizedPage::::default(); - match ::MaxItemizedPageSizeBytes> as ItemizedOperations>::apply_item_actions(&empty_page,&vec![ItemAction::Add { - data: bounded, - }]) { - Ok(mut updated_page) => { - updated_page.nonce = existing_page.nonce; - StatefulChildTree::::write( - msa_id, - PALLET_STORAGE_PREFIX, - ITEMIZED_STORAGE_PREFIX, - &key, - &updated_page, - ); - bytes = bytes.saturating_add(updated_page.encode().len() as u64); - writes.saturating_inc(); - }, - Err(e) => - log::error!(target: LOG_TARGET, "Error appending prefixed value {:?} and schema_id {:?} with {:?}", msa_id, schema_id, e), - } - }, - 33usize => - log::warn!(target: LOG_TARGET, "Itemized page item for msa_id {:?} and schema_id {:?} has correct size", msa_id, schema_id), - _ => - log::warn!(target: LOG_TARGET, "Itemized page item for msa_id {:?} and schema_id {:?} has invalid size {:?}", msa_id, schema_id, existing_value.len()), - }, - None => - log::warn!(target: LOG_TARGET, "Itemized page was empty for msa_id {:?} and schema_id {:?}", msa_id, schema_id), - }, - Err(e) => - log::error!(target: LOG_TARGET, "Error parsing page for msa_id {:?} and schema_id {:?} with {:?}", msa_id, schema_id, e), - } - }, - Ok(None) => - log::warn!(target: LOG_TARGET, "No page found for msa_id {:?} and schema_id {:?}", msa_id, schema_id), - Err(e) => - log::error!(target: LOG_TARGET, "Error getting the page for msa_id {:?} and schema_id {:?} with {:?}", msa_id, schema_id, e), - } - } - log::info!(target: LOG_TARGET, "Storage migrated to version 1 read={:?}, write={:?}, bytes={:?}", reads, writes, bytes); - let weights = T::DbWeight::get().reads_writes(reads, writes).add_proof_size(bytes); - log::info!(target: LOG_TARGET, "migrate_msa_ids weights={:?}",weights); - weights -} - -/// paginated migration for testnet -pub fn paginated_migration_testnet(page_size: u32, page_index: u32) -> (Weight, bool) { - let msa_ids: Vec = get_testnet_msa_ids(); - let mut chunks = msa_ids.chunks(page_size as usize); - let chunk_len = chunks.len() as u32; - let mut current = 0u32; - while current < page_index && current < chunk_len { - let _ = chunks.next(); - current += 1; - } - match chunks.next() { - Some(page) => { - let weight = migrate_msa_ids::(page); - (weight, true) - }, - None => (Weight::zero(), false), - } -} - -fn get_schema_id() -> u16 { - if cfg!(test) { - // Supported ITEMIZED_APPEND_ONLY_SCHEMA for tests - 103 - } else { - 7 - } -} diff --git a/pallets/stateful-storage/src/tests/migrations_tests.rs b/pallets/stateful-storage/src/tests/migrations_tests.rs deleted file mode 100644 index 313f0ae76f..0000000000 --- a/pallets/stateful-storage/src/tests/migrations_tests.rs +++ /dev/null @@ -1,104 +0,0 @@ -use crate::{ - migration::{v1, v1::get_testnet_msa_ids}, - pallet::MigrationPageIndex, - test_common::constants::ITEMIZED_APPEND_ONLY_SCHEMA, - tests::mock::{ - new_test_ext, run_to_block, test_public, RuntimeOrigin, StatefulStoragePallet, Test, - }, - types::{ItemAction, MIGRATION_PAGE_SIZE}, -}; -use common_primitives::stateful_storage::PageHash; -use frame_support::{ - assert_ok, pallet_prelude::StorageVersion, traits::GetStorageVersion, BoundedVec, -}; - -#[test] -fn migration_to_v1_should_work_as_expected() { - new_test_ext().execute_with(|| { - // arrange - let msa_id = 1; - let caller_1 = test_public(msa_id); - let schema_id = ITEMIZED_APPEND_ONLY_SCHEMA; - let payload = - hex::decode("0e0abc9afdfb34cf30c59d16bbbbd76cae1668fc331eb9e505cb0d6af6365f17") - .expect("should decode hex"); - let expected = - hex::decode("400e0abc9afdfb34cf30c59d16bbbbd76cae1668fc331eb9e505cb0d6af6365f17") - .expect("should decode hex"); - let prev_content_hash: PageHash = 0; - let actions = vec![ItemAction::Add { data: payload.try_into().unwrap() }]; - - // act - assert_ok!(StatefulStoragePallet::apply_item_actions( - RuntimeOrigin::signed(caller_1), - msa_id, - schema_id, - prev_content_hash, - BoundedVec::try_from(actions).unwrap(), - )); - - // Act - let _ = v1::migrate_to_v1::(); - - // Assert - let current_version = StatefulStoragePallet::on_chain_storage_version(); - assert_eq!(current_version, StorageVersion::new(1)); - - let known_msa_ids = v1::get_msa_ids::(); - assert_eq!(known_msa_ids.len(), 1); - - let after_update = - StatefulStoragePallet::get_itemized_storage(msa_id, schema_id).expect("should get"); - let item = after_update.items.into_iter().next().expect("should item exists"); - assert_eq!(item.payload, expected); - }); -} - -#[test] -fn migration_to_v1_should_work_on_test_net_and_multi_block_path() { - new_test_ext().execute_with(|| { - // arrange - let msa_ids = get_testnet_msa_ids(); - let schema_id = ITEMIZED_APPEND_ONLY_SCHEMA; - let payload = - hex::decode("0e0abc9afdfb34cf30c59d16bbbbd76cae1668fc331eb9e505cb0d6af6365f17") - .expect("should decode hex"); - let expected = - hex::decode("400e0abc9afdfb34cf30c59d16bbbbd76cae1668fc331eb9e505cb0d6af6365f17") - .expect("should decode hex"); - let prev_content_hash: PageHash = 0; - let actions = vec![ItemAction::Add { data: payload.try_into().unwrap() }]; - for msa_id in msa_ids.iter() { - let caller_1 = test_public(*msa_id); - assert_ok!(StatefulStoragePallet::apply_item_actions( - RuntimeOrigin::signed(caller_1), - *msa_id, - schema_id, - prev_content_hash, - BoundedVec::try_from(actions.clone()).unwrap(), - )); - } - let msa_id_len = msa_ids.len() as u32; - - // Act - let _ = v1::migrate_to_v1::(); - - let blocks = msa_id_len / MIGRATION_PAGE_SIZE + 5; - for b in 2..=blocks { - run_to_block(b); - } - - // Assert - let current_version = StatefulStoragePallet::on_chain_storage_version(); - assert_eq!(current_version, StorageVersion::new(1)); - - for msa_id in msa_ids { - let after_update = - StatefulStoragePallet::get_itemized_storage(msa_id, schema_id).expect("should get"); - let item = after_update.items.into_iter().next().expect("should item exists"); - assert_eq!(item.payload, expected); - } - - assert_eq!(MigrationPageIndex::::get(), msa_id_len / MIGRATION_PAGE_SIZE + 1); - }); -} diff --git a/pallets/stateful-storage/src/tests/mock.rs b/pallets/stateful-storage/src/tests/mock.rs index f7059447b4..4c97f2357a 100644 --- a/pallets/stateful-storage/src/tests/mock.rs +++ b/pallets/stateful-storage/src/tests/mock.rs @@ -20,7 +20,7 @@ use common_runtime::weights::rocksdb_weights::constants::RocksDbWeight; use frame_support::{ dispatch::DispatchResult, parameter_types, - traits::{ConstU16, ConstU32, OnFinalize, OnInitialize}, + traits::{ConstU16, ConstU32}, Twox128, }; use frame_system as system; @@ -423,15 +423,3 @@ pub fn new_test_ext_keystore() -> sp_io::TestExternalities { ext } - -/// advances the block -pub fn run_to_block(n: u32) { - while System::block_number() < n { - if System::block_number() > 1 { - System::on_finalize(System::block_number()); - } - System::set_block_number(System::block_number() + 1); - System::on_initialize(System::block_number()); - StatefulStoragePallet::on_initialize(System::block_number()); - } -} diff --git a/pallets/stateful-storage/src/tests/mod.rs b/pallets/stateful-storage/src/tests/mod.rs index ab44b035a1..333aa74e56 100644 --- a/pallets/stateful-storage/src/tests/mod.rs +++ b/pallets/stateful-storage/src/tests/mod.rs @@ -4,6 +4,5 @@ mod apply_item_actions_tests; mod child_tree_tests; mod delete_page_tests; mod itemized_operations_tests; -mod migrations_tests; mod other_tests; mod upsert_page_tests; diff --git a/pallets/time-release/src/lib.rs b/pallets/time-release/src/lib.rs index 723b9a0f4c..6d4a219956 100644 --- a/pallets/time-release/src/lib.rs +++ b/pallets/time-release/src/lib.rs @@ -42,9 +42,6 @@ mod mock; #[cfg(test)] mod tests; -/// storage migrations -pub mod migration; - mod types; pub use types::*; diff --git a/pallets/time-release/src/migration/mod.rs b/pallets/time-release/src/migration/mod.rs deleted file mode 100644 index c34354a101..0000000000 --- a/pallets/time-release/src/migration/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -/// migrations to v2 -pub mod v2; diff --git a/pallets/time-release/src/migration/v2.rs b/pallets/time-release/src/migration/v2.rs deleted file mode 100644 index b7cde231ca..0000000000 --- a/pallets/time-release/src/migration/v2.rs +++ /dev/null @@ -1,278 +0,0 @@ -use crate::{types, BalanceOf, BlockNumberFor, Config, FreezeReason, Pallet, ReleaseSchedules}; -use frame_support::{ - pallet_prelude::{GetStorageVersion, IsType, Weight}, - traits::{ - fungible::MutateFreeze, Get, LockIdentifier, LockableCurrency, OnRuntimeUpgrade, - StorageVersion, - }, -}; -use parity_scale_codec::Encode; -use sp_core::hexdisplay::HexDisplay; -use sp_runtime::{traits::Zero, Saturating}; - -const LOG_TARGET: &str = "runtime::time-release"; - -#[cfg(feature = "try-runtime")] -use sp_std::vec::Vec; - -const RELEASE_LOCK_ID: LockIdentifier = *b"timeRels"; - -/// The OnRuntimeUpgrade implementation for this storage migration -pub struct MigrationToV2(sp_std::marker::PhantomData<(T, OldCurrency)>); -impl MigrationToV2 -where - T: Config, - OldCurrency: 'static + LockableCurrency>, - OldCurrency::Balance: IsType>, -{ - /// Translate time release locked deposit to frozen deposit - pub fn translate_lock_to_freeze( - account_id: &T::AccountId, - amount: OldCurrency::Balance, - ) -> Weight { - // 1 read get Freezes: set_freeze: get locks - // 1 read get Locks: update_freezes: Locks::get().iter() - // 1 write set Account: update_freezes->mutate_account->try_mutate_account->ensure_upgraded: if account is *not* already upgraded. - // 1 write set Account: update_freezes->mutate_account->try_mutate_account: set account data - // 1 write set Freezes: update_freezes: Freezes::insert - T::Currency::set_freeze( - &FreezeReason::TimeReleaseVesting.into(), - &account_id, - amount.into(), - ) - .unwrap_or_else(|err| { - log::error!( - target: LOG_TARGET, - "Failed to freeze {:?} from account 0x{:?}, reason: {:?}", - amount, - HexDisplay::from(&account_id.encode()), - err - ); - }); - - // 1 read get Locks: remove_lock: get locks - // 1 read get Freezes - // 1 read get Account - // 1 write set Account: update_locks->try_mutate_account->ensure_upgraded: if account is *not* already upgraded. - // 1 write set Account: update_locks->try_mutate_account: set account data - // [Cached] 1 read get Locks: update_locks: set existed with `contains_key` - // 1 write set Locks: update_locks->Locks::remove: remove existed - OldCurrency::remove_lock(RELEASE_LOCK_ID, &account_id); - - T::DbWeight::get().reads_writes(5, 6) - } -} - -impl OnRuntimeUpgrade for MigrationToV2 -where - T: Config, - OldCurrency: 'static + LockableCurrency>, - OldCurrency::Balance: IsType>, -{ - fn on_runtime_upgrade() -> Weight { - log::info!(target: LOG_TARGET, "Running storage migration..."); - - let on_chain_version = Pallet::::on_chain_storage_version(); - - // storage was already migrated. - if on_chain_version.ge(&2) { - log::warn!( - target: LOG_TARGET, - "Old Time Release Locks->Freezes migration attempted to run. Please remove" - ); - return T::DbWeight::get().reads(1) - } - - log::info!(target: LOG_TARGET, "🔄 Time Release Locks->Freezes migration started"); - // The migration started with 1r to get the on_chain_storage_version - let mut total_weight = T::DbWeight::get().reads_writes(1, 0); - let mut total_accounts_migrated = 0u32; - - // Get all the keys(accounts) from the ReleaseSchedules storage - ReleaseSchedules::::iter() - .map(|(account_id, _)| account_id) - .for_each(|account_id| { - let (total_amount, cal_fn_weight) = calculate_total_scheduled_frozen_for_account::(&account_id); - - let trans_fn_weight = MigrationToV2::::translate_lock_to_freeze( - &account_id, - total_amount.into(), - ); - - total_weight = total_weight.saturating_add(cal_fn_weight).saturating_add(trans_fn_weight); - - total_accounts_migrated += 1; - - log::info!(target: LOG_TARGET, "🔄 migrated account 0x{:?}, amount:{:?}", HexDisplay::from(&account_id.encode()), total_amount); - }); - - log::info!( - target: LOG_TARGET, - "total accounts migrated from locks to frozen {:?}", - total_accounts_migrated - ); - - StorageVersion::new(2).put::>(); - total_weight.saturating_add(T::DbWeight::get().reads_writes(0, 1)); - - log::info!(target: LOG_TARGET, "🔄 Time Release migration finished"); - log::info!( - target: LOG_TARGET, - "Time Release Migration calculated weight = {:?}", - total_weight - ); - - total_weight - } - - #[cfg(feature = "try-runtime")] - fn pre_upgrade() -> Result, sp_runtime::TryRuntimeError> { - use frame_support::storage::generator::StorageMap; - - let on_chain_version = Pallet::::on_chain_storage_version(); // 1r - if on_chain_version >= 2 { - return Ok(Vec::new()) - } - - let pallet_prefix = ReleaseSchedules::::pallet_prefix(); - let storage_prefix = ReleaseSchedules::::storage_prefix(); - assert_eq!(&b"TimeRelease"[..], pallet_prefix); - assert_eq!(&b"ReleaseSchedules"[..], storage_prefix); - log::info!(target: LOG_TARGET, "Running pre_upgrade..."); - - let count = ReleaseSchedules::::iter().count() as u32; - log::info!(target: LOG_TARGET, "Finish pre_upgrade for {:?} records", count); - Ok(count.encode()) - } - - #[cfg(feature = "try-runtime")] - fn post_upgrade(state: Vec) -> Result<(), sp_runtime::TryRuntimeError> { - use parity_scale_codec::Decode; - - let on_chain_version = Pallet::::on_chain_storage_version(); // 1r - if on_chain_version >= 2 { - return Ok(()) - } - let pre_upgrade_count: u32 = Decode::decode(&mut state.as_slice()).unwrap_or_default(); - let on_chain_version = Pallet::::on_chain_storage_version(); - - assert_eq!(on_chain_version, crate::module::STORAGE_VERSION); - assert_eq!(pre_upgrade_count as usize, ReleaseSchedules::::iter().count()); - - log::info!(target: LOG_TARGET, "✅ migration post_upgrade checks passed"); - Ok(()) - } -} - -fn calculate_total_scheduled_frozen_for_account( - account_id: &T::AccountId, -) -> (BalanceOf, Weight) { - let total = ReleaseSchedules::::get(&account_id) // 1r - .iter() - .map(|schedule: &types::ReleaseSchedule, BalanceOf>| { - schedule.total_amount() - }) - .fold(Zero::zero(), |acc: BalanceOf, amount| { - acc.saturating_add(amount.unwrap_or(Zero::zero())) - }); - - (total, T::DbWeight::get().reads(1)) -} - -#[cfg(test)] -mod test { - use frame_support::traits::{tokens::fungible::InspectFreeze, WithdrawReasons}; - - use super::*; - use crate::mock::{Test as T, *}; - use pallet_balances::{BalanceLock, Reasons}; - - type MigrationOf = MigrationToV2>; - - #[test] - fn migration_works() { - ExtBuilder::build().execute_with(|| { - assert_eq!(pallet_balances::Pallet::::free_balance(&DAVE), DAVE_BALANCE); - - let schedules = vec![ - // who, start, period, period_count, per_period - (DAVE, 2, 3, 1, 5), - ]; - - create_schedules_and_set_freeze(schedules); - - assert_eq!( - pallet_balances::Pallet::::locks(&DAVE) - .iter() - .find(|l| l.id == RELEASE_LOCK_ID), - Some(&BalanceLock { - id: RELEASE_LOCK_ID, - amount: 5u64.into(), - reasons: Reasons::All - }) - ); - - // Run migration. - let pre_upgrade_count = ReleaseSchedules::::iter().count() as u32; - MigrationOf::::on_runtime_upgrade(); - let on_chain_version = Pallet::::on_chain_storage_version(); - - assert_eq!(on_chain_version, crate::module::STORAGE_VERSION); - assert_eq!(pre_upgrade_count as usize, ReleaseSchedules::::iter().count()); - - assert_eq!( - pallet_balances::Pallet::::locks(&DAVE), - vec![], - "Locks should be empty" - ); - assert_eq!( - ::Currency::balance_frozen( - &FreezeReason::TimeReleaseVesting.into(), - &DAVE - ), - 5u64, - "Frozen balance should be 5 for account {:?}", - DAVE - ); - }) - } - - fn create_schedules_and_set_freeze(schedules: Vec<(AccountId, u32, u32, u32, u64)>) { - schedules.iter().for_each(|(who, start, period, period_count, per_period)| { - let mut bounded_schedules = ReleaseSchedules::::get(who); - let new_schedule = types::ReleaseSchedule { - start: *start, - period: *period, - period_count: *period_count, - per_period: *per_period, - }; - - bounded_schedules - .try_push(new_schedule.clone()) - .expect("Max release schedules exceeded"); - - let total_amount = bounded_schedules.iter().fold( - Zero::zero(), - |acc_amount: BalanceOf, schedule| { - acc_amount.saturating_add(schedule.total_amount().unwrap_or(Zero::zero())) - }, - ); - - assert_eq!(total_amount, new_schedule.total_amount().unwrap_or(Zero::zero())); - - assert!( - pallet_balances::Pallet::::free_balance(who) >= total_amount, - "Account does not have enough balance" - ); - - pallet_balances::Pallet::::set_lock( - RELEASE_LOCK_ID, - &who, - total_amount, - WithdrawReasons::all(), - ); - - ReleaseSchedules::::insert(who, bounded_schedules); - }); - } -} diff --git a/runtime/frequency/src/lib.rs b/runtime/frequency/src/lib.rs index 9e4854b906..be470706dc 100644 --- a/runtime/frequency/src/lib.rs +++ b/runtime/frequency/src/lib.rs @@ -342,12 +342,7 @@ pub type Executive = frame_executive::Executive< frame_system::ChainContext, Runtime, AllPalletsWithSystem, - ( - MigratePalletsCurrentStorage, - pallet_capacity::migration::v4::MigrationToV4, - pallet_capacity::migration::provider_boost_init::ProviderBoostInit, - pallet_stateful_storage::migration::v1::MigrateToV1, - ), + (MigratePalletsCurrentStorage,), >; pub struct MigratePalletsCurrentStorage(sp_std::marker::PhantomData); @@ -404,7 +399,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { spec_name: create_runtime_str!("frequency"), impl_name: create_runtime_str!("frequency"), authoring_version: 1, - spec_version: 136, + spec_version: 137, impl_version: 0, apis: RUNTIME_API_VERSIONS, transaction_version: 1, @@ -418,7 +413,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { spec_name: create_runtime_str!("frequency-testnet"), impl_name: create_runtime_str!("frequency"), authoring_version: 1, - spec_version: 136, + spec_version: 137, impl_version: 0, apis: RUNTIME_API_VERSIONS, transaction_version: 1,