From 55dcbd991574ff30b8aeb4c0b2ddfd72e1c08d27 Mon Sep 17 00:00:00 2001 From: Liam Date: Thu, 23 May 2024 12:13:53 +0400 Subject: [PATCH] remove applied migrations enable ci for devnet and testnet remove dead code --- .github/workflows/check-rust.yml | 68 +++--- runtime/src/lib.rs | 10 +- .../src/migrations/account_data_migration.rs | 204 ------------------ .../src/migrations/init_storage_versions.rs | 26 --- runtime/src/migrations/mod.rs | 3 +- 5 files changed, 34 insertions(+), 277 deletions(-) delete mode 100644 runtime/src/migrations/account_data_migration.rs delete mode 100644 runtime/src/migrations/init_storage_versions.rs diff --git a/.github/workflows/check-rust.yml b/.github/workflows/check-rust.yml index f7478028a..61f7fb920 100644 --- a/.github/workflows/check-rust.yml +++ b/.github/workflows/check-rust.yml @@ -359,40 +359,34 @@ jobs: runtime-package: "node-subtensor-runtime" node-uri: "wss://entrypoint-finney.opentensor.ai:443" checks: "pre-and-post" - extra-args: "--disable-spec-version-check --no-weight-warnings --disable-idempotency-checks" - - # ---- - # We can enable devnet and finney migrations once Polkadot v1.0 is deployed to finney, after - # which time all future migrations should be idempotent and won't start failing after the - # upgrade is deployed. - # ---- - # check-devnet-migrations: - # name: check devnet migrations - # runs-on: ubuntu-22.04 - # steps: - # - name: Checkout sources - # uses: actions/checkout@v3 - # - # - name: Run Try Runtime Checks - # uses: "paritytech/try-runtime-gha@v0.1.0" - # with: - # runtime-package: "node-subtensor-runtime" - # node-uri: "wss://dev.chain.opentensor.ai:443" - # checks: "pre-and-post" - # extra-args: "--disable-spec-version-check --no-weight-warnings --disable-idempotency-checks" - # - # check-testnet-migrations: - # name: check testnet migrations - # runs-on: ubuntu-22.04 - # steps: - # - name: Checkout sources - # uses: actions/checkout@v3 - # - # - name: Run Try Runtime Checks - # uses: "paritytech/try-runtime-gha@v0.1.0" - # with: - # runtime-package: "node-subtensor-runtime" - # node-uri: "wss://test.chain.opentensor.ai:443" - # checks: "pre-and-post" - # extra-args: "--disable-spec-version-check --no-weight-warnings --disable-idempotency-checks" - # + extra-args: "--disable-spec-version-check --no-weight-warnings" + + check-devnet-migrations: + name: check devnet migrations + runs-on: ubuntu-22.04 + steps: + - name: Checkout sources + uses: actions/checkout@v3 + + - name: Run Try Runtime Checks + uses: "paritytech/try-runtime-gha@v0.1.0" + with: + runtime-package: "node-subtensor-runtime" + node-uri: "wss://dev.chain.opentensor.ai:443" + checks: "pre-and-post" + extra-args: "--disable-spec-version-check --no-weight-warnings" + + check-testnet-migrations: + name: check testnet migrations + runs-on: ubuntu-22.04 + steps: + - name: Checkout sources + uses: actions/checkout@v3 + + - name: Run Try Runtime Checks + uses: "paritytech/try-runtime-gha@v0.1.0" + with: + runtime-package: "node-subtensor-runtime" + node-uri: "wss://test.chain.opentensor.ai:443" + checks: "pre-and-post" + extra-args: "--disable-spec-version-check --no-weight-warnings" diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 46d16a010..2ccf5e02d 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -14,10 +14,9 @@ use frame_support::{ dispatch::DispatchResultWithPostInfo, genesis_builder_helper::{build_config, create_default_config}, pallet_prelude::{DispatchError, Get}, - traits::{fungible::HoldConsideration, LinearStoragePrice, OnRuntimeUpgrade}, + traits::{fungible::HoldConsideration, LinearStoragePrice}, }; use frame_system::{EnsureNever, EnsureRoot, RawOrigin}; -use migrations::{account_data_migration, init_storage_versions}; use pallet_commitments::CanCommit; use pallet_grandpa::{ fg_primitives, AuthorityId as GrandpaId, AuthorityList as GrandpaAuthorityList, @@ -1185,12 +1184,7 @@ pub type SignedExtra = ( pallet_commitments::CommitmentsSignedExtension, ); -type Migrations = ( - init_storage_versions::Migration, - account_data_migration::Migration, - pallet_multisig::migrations::v1::MigrateToV1, - pallet_grandpa::migrations::MigrateV4ToV5, -); +type Migrations = pallet_grandpa::migrations::MigrateV4ToV5; // Unchecked extrinsic type as expected by this runtime. pub type UncheckedExtrinsic = diff --git a/runtime/src/migrations/account_data_migration.rs b/runtime/src/migrations/account_data_migration.rs deleted file mode 100644 index 610d496ab..000000000 --- a/runtime/src/migrations/account_data_migration.rs +++ /dev/null @@ -1,204 +0,0 @@ -use crate::*; -use pallet_balances::ExtraFlags; - -#[cfg(feature = "try-runtime")] -use sp_std::collections::btree_map::BTreeMap; - -mod prev { - use super::*; - use frame_support::{pallet_prelude::ValueQuery, storage_alias, Blake2_128Concat}; - - #[derive( - Clone, Eq, PartialEq, Default, RuntimeDebug, Encode, Decode, TypeInfo, MaxEncodedLen, - )] - pub struct AccountDataStruct { - pub free: Balance, - pub reserved: Balance, - pub misc_frozen: Balance, - pub fee_frozen: Balance, - } - - #[derive( - Clone, Eq, PartialEq, Default, RuntimeDebug, Encode, Decode, TypeInfo, MaxEncodedLen, - )] - pub struct AccountStruct { - pub nonce: u32, - pub consumers: u32, - pub providers: u32, - pub sufficients: u32, - pub data: AccountDataStruct, - } - - #[storage_alias] - pub type Account = StorageMap< - frame_system::pallet::Pallet, - Blake2_128Concat, - AccountId, - AccountStruct, - ValueQuery, - >; -} - -#[cfg(feature = "try-runtime")] -#[derive(Encode, Decode)] -enum PreUpgradeInfo { - MigrationAlreadyOccured, - MigrationShouldRun( - BTreeMap>>, - ), -} - -const TARGET: &str = "runtime::account_data_migration"; -pub struct Migration; -impl OnRuntimeUpgrade for Migration { - /// Save pre-upgrade account ids to check are decodable post-upgrade. - #[cfg(feature = "try-runtime")] - fn pre_upgrade() -> Result, sp_runtime::TryRuntimeError> { - // Skip migration if it already took place. - if migration_already_occured() { - return Ok(PreUpgradeInfo::MigrationAlreadyOccured.encode()); - } - - log::info!(target: TARGET, "pre-upgrade"); - // Save the expected post-migration account state. - let mut expected_account: BTreeMap< - AccountId, - frame_system::AccountInfo>, - > = BTreeMap::new(); - - for (acc_id, acc) in prev::Account::::iter() { - let expected_data = pallet_balances::AccountData { - free: acc.data.free, - reserved: acc.data.reserved, - frozen: acc.data.misc_frozen.saturating_add(acc.data.fee_frozen), - flags: ExtraFlags::default(), - }; - - // `ensure_upgraded` bumps the consumers if there is a non zero reserved balance and no frozen balance. - // https://github.com/paritytech/polkadot-sdk/blob/305d311d5c732fcc4629f3295768f1ed44ef434c/substrate/frame/balances/src/lib.rs#L785 - let expected_consumers = if acc.data.reserved > 0 && expected_data.frozen == 0 { - acc.consumers + 1 - } else { - acc.consumers - }; - let expected_acc = frame_system::AccountInfo { - nonce: acc.nonce, - consumers: expected_consumers, - providers: acc.providers, - sufficients: acc.sufficients, - data: expected_data, - }; - expected_account.insert(acc_id, expected_acc); - } - - Ok(PreUpgradeInfo::MigrationShouldRun(expected_account).encode()) - } - - /// Migrates Account storage to the new format, and calls `ensure_upgraded` for them. - fn on_runtime_upgrade() -> Weight { - // Skip migration if it already took place. - if migration_already_occured() { - log::warn!( - target: TARGET, - "Migration already completed and can be removed.", - ); - return ::DbWeight::get().reads_writes(0u64, 0u64); - } - - // Pull the storage in the previous format into memory - let accounts = prev::Account::::iter().collect::>(); - log::info!(target: TARGET, "Migrating {} accounts...", accounts.len()); - - for (acc_id, acc_info) in accounts.clone().into_iter() { - let prev_data = acc_info.clone().data; - - // Move account to new data format - let new_data = pallet_balances::AccountData { - free: prev_data.free, - reserved: prev_data.reserved, - frozen: prev_data.misc_frozen.saturating_add(prev_data.fee_frozen), - flags: ExtraFlags::old_logic(), - }; - let new_account = frame_system::AccountInfo { - nonce: acc_info.nonce, - consumers: acc_info.consumers, - providers: acc_info.providers, - sufficients: acc_info.sufficients, - data: new_data, - }; - frame_system::pallet::Account::::insert(acc_id.clone(), new_account); - - // Ensure upgraded - pallet_balances::Pallet::::ensure_upgraded(&acc_id); - } - - log::info!(target: TARGET, "Migrated {} accounts ✅", accounts.len()); - - // R/W not important for solo chain. - ::DbWeight::get().reads_writes(0u64, 0u64) - } - - /// Ensures post-upgrade that every Account entry matches what is expected. - #[cfg(feature = "try-runtime")] - fn post_upgrade(state: Vec) -> Result<(), sp_runtime::TryRuntimeError> { - use frame_support::ensure; - - log::info!(target: TARGET, "Running post-upgrade..."); - - let pre_upgrade_data: PreUpgradeInfo = - Decode::decode(&mut &state[..]).expect("decoding state failed"); - - match pre_upgrade_data { - PreUpgradeInfo::MigrationAlreadyOccured => Ok(()), - PreUpgradeInfo::MigrationShouldRun(expected_accounts) => { - // Ensure the actual post-migration state matches the expected - for (acc_id, acc) in frame_system::pallet::Account::::iter() { - let expected = expected_accounts.get(&acc_id).expect("account not found"); - - // New system logic nukes the account if no providers or sufficients. - if acc.providers > 0 || acc.sufficients > 0 { - ensure!(acc.nonce == expected.nonce, "nonce mismatch"); - ensure!(acc.consumers == expected.consumers, "consumers mismatch"); - ensure!(acc.providers == expected.providers, "providers mismatch"); - ensure!( - acc.sufficients == expected.sufficients, - "sufficients mismatch" - ); - ensure!(acc.data.free == expected.data.free, "data.free mismatch"); - ensure!( - acc.data.reserved == expected.data.reserved, - "data.reserved mismatch" - ); - ensure!( - acc.data.frozen == expected.data.frozen, - "data.frozen mismatch" - ); - ensure!(acc.data.flags == expected.data.flags, "data.flags mismatch"); - } - } - - log::info!(target: TARGET, "post-upgrade success ✅"); - Ok(()) - } - } - } -} - -/// Check if the migration already took place. The migration is all-or-nothing, so if one -/// account is migrated we know that the rest also must be migrated. -/// -/// We can use the nonce to check if it's been migrated, because it being zero meant that -/// the decode succeeded and this account has been migrated already. -/// -/// Performance note: While this may appear poor for performance due to touching all keys, -/// these keys will need to be touched anyway, so it's fine to just touch them loading them into -/// the storage overlay here. -fn migration_already_occured() -> bool { - for (_, acc) in frame_system::pallet::Account::::iter() { - if acc.nonce > 0 { - return true; - }; - } - - false -} diff --git a/runtime/src/migrations/init_storage_versions.rs b/runtime/src/migrations/init_storage_versions.rs deleted file mode 100644 index d5cb4bff6..000000000 --- a/runtime/src/migrations/init_storage_versions.rs +++ /dev/null @@ -1,26 +0,0 @@ -use crate::*; - -/// Init the on-chain storage versions of pallets added to the runtime prior to this being an automatic process. -pub struct Migration; - -impl OnRuntimeUpgrade for Migration { - fn on_runtime_upgrade() -> Weight { - use frame_support::traits::GetStorageVersion; - use frame_support::traits::StorageVersion; - - if Triumvirate::on_chain_storage_version() == StorageVersion::new(0) { - Triumvirate::in_code_storage_version().put::(); - } - if TriumvirateMembers::on_chain_storage_version() == StorageVersion::new(0) { - TriumvirateMembers::in_code_storage_version().put::(); - } - if SenateMembers::on_chain_storage_version() == StorageVersion::new(0) { - SenateMembers::in_code_storage_version().put::(); - } - if Scheduler::on_chain_storage_version() == StorageVersion::new(0) { - Scheduler::in_code_storage_version().put::(); - } - - ::DbWeight::get().reads_writes(4, 4) - } -} diff --git a/runtime/src/migrations/mod.rs b/runtime/src/migrations/mod.rs index 494f81fa9..ecc48efcd 100644 --- a/runtime/src/migrations/mod.rs +++ b/runtime/src/migrations/mod.rs @@ -1,2 +1 @@ -pub mod account_data_migration; -pub mod init_storage_versions; +//! Export migrations from here.