From a6b43bd41663d1cc3e6bb1d3061a76378bd2a0c1 Mon Sep 17 00:00:00 2001 From: cheme Date: Wed, 23 Nov 2022 16:21:54 +0100 Subject: [PATCH 01/10] Switch to state V1 and add state-trie-migration pallet with dummy manual account. --- Cargo.lock | 1 + runtime/westend/Cargo.toml | 7 ++++--- runtime/westend/src/lib.rs | 30 +++++++++++++++++++++++++++++- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f8c46862ef44..d1e4603d2f65 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -12313,6 +12313,7 @@ dependencies = [ "pallet-society", "pallet-staking", "pallet-staking-reward-curve", + "pallet-state-trie-migration", "pallet-sudo", "pallet-timestamp", "pallet-transaction-payment", diff --git a/runtime/westend/Cargo.toml b/runtime/westend/Cargo.toml index d5efbb28d5a4..d00ff1191ecb 100644 --- a/runtime/westend/Cargo.toml +++ b/runtime/westend/Cargo.toml @@ -65,6 +65,7 @@ pallet-session = { git = "https://github.com/paritytech/substrate", branch = "ma pallet-society = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false } pallet-staking = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false } pallet-staking-reward-curve = { package = "pallet-staking-reward-curve", git = "https://github.com/paritytech/substrate", branch = "master" } +pallet-state-trie-migration = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false } pallet-sudo = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false } pallet-timestamp = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false } pallet-transaction-payment = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false } @@ -83,7 +84,7 @@ pallet-election-provider-support-benchmarking = { git = "https://github.com/pari pallet-nomination-pools-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false, optional = true } pallet-offences-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false, optional = true } pallet-session-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false, optional = true } -hex-literal = { version = "0.3.4", optional = true } +hex-literal = { version = "0.3.4" } runtime-common = { package = "polkadot-runtime-common", path = "../common", default-features = false } primitives = { package = "polkadot-primitives", path = "../../primitives", default-features = false } @@ -95,7 +96,6 @@ xcm-executor = { package = "xcm-executor", path = "../../xcm/xcm-executor", defa xcm-builder = { package = "xcm-builder", path = "../../xcm/xcm-builder", default-features = false } [dev-dependencies] -hex-literal = "0.3.4" tiny-keccak = { version = "2.0.2", features = ["keccak"] } keyring = { package = "sp-keyring", git = "https://github.com/paritytech/substrate", branch = "master" } sp-trie = { git = "https://github.com/paritytech/substrate", branch = "master" } @@ -155,6 +155,7 @@ std = [ "pallet-session/std", "pallet-society/std", "pallet-staking/std", + "pallet-state-trie-migration/std", "pallet-sudo/std", "pallet-timestamp/std", "pallet-treasury/std", @@ -219,7 +220,6 @@ runtime-benchmarks = [ "pallet-session-benchmarking/runtime-benchmarks", "pallet-xcm/runtime-benchmarks", "frame-system-benchmarking/runtime-benchmarks", - "hex-literal", "xcm-builder/runtime-benchmarks", "pallet-xcm-benchmarks/runtime-benchmarks", "frame-election-provider-support/runtime-benchmarks", @@ -255,6 +255,7 @@ try-runtime = [ "pallet-session/try-runtime", "pallet-society/try-runtime", "pallet-staking/try-runtime", + "pallet-state-trie-migration/try-runtime", "pallet-sudo/try-runtime", "pallet-timestamp/try-runtime", "pallet-treasury/try-runtime", diff --git a/runtime/westend/src/lib.rs b/runtime/westend/src/lib.rs index a7930ff06ea6..720902ae006f 100644 --- a/runtime/westend/src/lib.rs +++ b/runtime/westend/src/lib.rs @@ -116,7 +116,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { #[cfg(feature = "disable-runtime-api")] apis: sp_version::create_apis_vec![[]], transaction_version: 14, - state_version: 0, + state_version: 1, }; /// The BABE epoch configuration at genesis. @@ -1086,6 +1086,31 @@ impl pallet_nomination_pools::Config for Runtime { type MaxPointsToBalance = MaxPointsToBalance; } +parameter_types! { + // The deposit configuration for the singed migration. Specially if you want to allow any signed account to do the migration (see `SignedFilter`, these deposits should be high) + pub const MigrationSignedDepositPerItem: Balance = 1 * CENTS; + pub const MigrationSignedDepositBase: Balance = 20 * CENTS * 100; + pub const MigrationMaxKeyLen: u32 = 512; +} + +impl pallet_state_trie_migration::Config for Runtime { + type RuntimeEvent = RuntimeEvent; + type Currency = Balances; + type SignedDepositPerItem = MigrationSignedDepositPerItem; + type SignedDepositBase = MigrationSignedDepositBase; + type ControlOrigin = EnsureRoot; + // specific account for the migration, can trigger the signed migrations. + type SignedFilter = frame_system::EnsureSignedBy; + + // Use same weights as substrate ones. + type WeightInfo = pallet_state_trie_migration::weights::SubstrateWeight; + type MaxKeyLen = MigrationMaxKeyLen; +} + +frame_support::ord_parameter_types! { + pub const MigController: AccountId = AccountId::from(hex_literal::hex!("8888888888888888888888888888888888888888888888888888888888888888")); +} + construct_runtime! { pub enum Runtime where Block = Block, @@ -1154,6 +1179,9 @@ construct_runtime! { // Fast unstake pallet: extension to staking. FastUnstake: pallet_fast_unstake = 30, + // State trie migration pallet, only temporary. + StateTrieMigration: pallet_state_trie_migration = 35, + // Parachains pallets. Start indices at 40 to leave room. ParachainsOrigin: parachains_origin::{Pallet, Origin} = 41, Configuration: parachains_configuration::{Pallet, Call, Storage, Config} = 42, From 98eb9762a9b91fbcdf7c3eb4472d21d4dd18b722 Mon Sep 17 00:00:00 2001 From: cheme Date: Fri, 25 Nov 2022 09:31:53 +0100 Subject: [PATCH 02/10] Initialize limit on runtime upgrade. --- runtime/westend/src/lib.rs | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/runtime/westend/src/lib.rs b/runtime/westend/src/lib.rs index 720902ae006f..ded8dc4d50ce 100644 --- a/runtime/westend/src/lib.rs +++ b/runtime/westend/src/lib.rs @@ -1259,6 +1259,7 @@ pub type Executive = frame_executive::Executive< parachains_configuration::migration::v3::MigrateToV3, pallet_election_provider_multi_phase::migrations::v1::MigrateToV1, pallet_fast_unstake::migrations::v1::MigrateToV1, + init_state_migration::InitMigrate, ), >; /// The payload being signed in transactions. @@ -1864,3 +1865,40 @@ mod remote_tests { ext.execute_with(|| Runtime::on_runtime_upgrade()); } } + +mod init_state_migration { + use super::Runtime; + use frame_support::traits::OnRuntimeUpgrade; + use pallet_state_trie_migration::{AutoLimits, MigrationLimits, MigrationProcess}; + + /// Initialize an automatic migration process. + pub struct InitMigrate; + impl OnRuntimeUpgrade for InitMigrate { + #[cfg(feature = "try-runtime")] + fn pre_upgrade() -> Result, &'static str> { + Ok(Default::default()) + } + + fn on_runtime_upgrade() -> frame_support::weights::Weight { + if MigrationProcess::::get() == Default::default() && + AutoLimits::::get().is_none() + { + AutoLimits::::put(Some(MigrationLimits { item: 160, size: 204800 })); + log::info!("Automatic trie migration started."); + ::DbWeight::get().reads_writes(2, 1) + } else { + log::info!("Automatic trie migration not started."); + ::DbWeight::get().reads(2) + } + } + + #[cfg(feature = "try-runtime")] + fn post_upgrade(_state: Vec) -> Result<(), &'static str> { + frame_support::ensure!( + AutoLimits::::get().is_some(), + "Automigration started." + ); + Ok(()) + } + } +} From 6dda492c5610d04a4ea49e13de030b0a263aade1 Mon Sep 17 00:00:00 2001 From: cheme Date: Fri, 25 Nov 2022 09:42:11 +0100 Subject: [PATCH 03/10] add prelude --- runtime/westend/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/runtime/westend/src/lib.rs b/runtime/westend/src/lib.rs index ded8dc4d50ce..2f3209d073b2 100644 --- a/runtime/westend/src/lib.rs +++ b/runtime/westend/src/lib.rs @@ -1870,6 +1870,7 @@ mod init_state_migration { use super::Runtime; use frame_support::traits::OnRuntimeUpgrade; use pallet_state_trie_migration::{AutoLimits, MigrationLimits, MigrationProcess}; + use sp_std::prelude::*; /// Initialize an automatic migration process. pub struct InitMigrate; From 0d01e5104eee1ebdccf2a1c96aa9c98bcccf3096 Mon Sep 17 00:00:00 2001 From: cheme Date: Fri, 25 Nov 2022 09:47:48 +0100 Subject: [PATCH 04/10] sp_std prelude only for no_std --- runtime/westend/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/runtime/westend/src/lib.rs b/runtime/westend/src/lib.rs index 2f3209d073b2..279b74107686 100644 --- a/runtime/westend/src/lib.rs +++ b/runtime/westend/src/lib.rs @@ -1870,6 +1870,7 @@ mod init_state_migration { use super::Runtime; use frame_support::traits::OnRuntimeUpgrade; use pallet_state_trie_migration::{AutoLimits, MigrationLimits, MigrationProcess}; + #[cfg(feature = "no_std")] use sp_std::prelude::*; /// Initialize an automatic migration process. From a94fc6a2194590b14a5b75a68c00953179df3711 Mon Sep 17 00:00:00 2001 From: cheme Date: Fri, 25 Nov 2022 10:16:38 +0100 Subject: [PATCH 05/10] Disable filter for signed migration --- runtime/westend/src/lib.rs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/runtime/westend/src/lib.rs b/runtime/westend/src/lib.rs index 279b74107686..049ce3ad18e0 100644 --- a/runtime/westend/src/lib.rs +++ b/runtime/westend/src/lib.rs @@ -1099,16 +1099,25 @@ impl pallet_state_trie_migration::Config for Runtime { type SignedDepositPerItem = MigrationSignedDepositPerItem; type SignedDepositBase = MigrationSignedDepositBase; type ControlOrigin = EnsureRoot; - // specific account for the migration, can trigger the signed migrations. - type SignedFilter = frame_system::EnsureSignedBy; + type SignedFilter = Disable; // Use same weights as substrate ones. type WeightInfo = pallet_state_trie_migration::weights::SubstrateWeight; type MaxKeyLen = MigrationMaxKeyLen; } -frame_support::ord_parameter_types! { - pub const MigController: AccountId = AccountId::from(hex_literal::hex!("8888888888888888888888888888888888888888888888888888888888888888")); +pub struct Disable(sp_std::marker::PhantomData); +impl frame_support::traits::EnsureOrigin for Disable { + type Success = T::AccountId; + fn try_origin(o: T::RuntimeOrigin) -> Result { + Err(o) + } + + #[cfg(feature = "runtime-benchmarks")] + fn try_successful_origin() -> Result { + // Not correct but this should only cost more. + Ok(T::RuntimeOrigin::root()) + } } construct_runtime! { From e9c4cdfa84828d443ba8f6310c56b7bea137213e Mon Sep 17 00:00:00 2001 From: cheme Date: Fri, 25 Nov 2022 10:23:47 +0100 Subject: [PATCH 06/10] revert hex dep --- runtime/westend/Cargo.toml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/runtime/westend/Cargo.toml b/runtime/westend/Cargo.toml index d00ff1191ecb..32ffd40993ee 100644 --- a/runtime/westend/Cargo.toml +++ b/runtime/westend/Cargo.toml @@ -84,7 +84,7 @@ pallet-election-provider-support-benchmarking = { git = "https://github.com/pari pallet-nomination-pools-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false, optional = true } pallet-offences-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false, optional = true } pallet-session-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false, optional = true } -hex-literal = { version = "0.3.4" } +hex-literal = { version = "0.3.4", optional = true } runtime-common = { package = "polkadot-runtime-common", path = "../common", default-features = false } primitives = { package = "polkadot-primitives", path = "../../primitives", default-features = false } @@ -96,6 +96,7 @@ xcm-executor = { package = "xcm-executor", path = "../../xcm/xcm-executor", defa xcm-builder = { package = "xcm-builder", path = "../../xcm/xcm-builder", default-features = false } [dev-dependencies] +hex-literal = "0.3.4" tiny-keccak = { version = "2.0.2", features = ["keccak"] } keyring = { package = "sp-keyring", git = "https://github.com/paritytech/substrate", branch = "master" } sp-trie = { git = "https://github.com/paritytech/substrate", branch = "master" } @@ -220,6 +221,7 @@ runtime-benchmarks = [ "pallet-session-benchmarking/runtime-benchmarks", "pallet-xcm/runtime-benchmarks", "frame-system-benchmarking/runtime-benchmarks", + "hex-literal", "xcm-builder/runtime-benchmarks", "pallet-xcm-benchmarks/runtime-benchmarks", "frame-election-provider-support/runtime-benchmarks", From 21c9093cd137be75e7fe0dca815a3533e219efb8 Mon Sep 17 00:00:00 2001 From: cheme Date: Fri, 25 Nov 2022 11:46:38 +0100 Subject: [PATCH 07/10] use NeverEnsureOrigin --- runtime/westend/src/lib.rs | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/runtime/westend/src/lib.rs b/runtime/westend/src/lib.rs index 049ce3ad18e0..143e805abfed 100644 --- a/runtime/westend/src/lib.rs +++ b/runtime/westend/src/lib.rs @@ -1099,27 +1099,13 @@ impl pallet_state_trie_migration::Config for Runtime { type SignedDepositPerItem = MigrationSignedDepositPerItem; type SignedDepositBase = MigrationSignedDepositBase; type ControlOrigin = EnsureRoot; - type SignedFilter = Disable; + type SignedFilter = frame_support::traits::NeverEnsureOrigin; // Use same weights as substrate ones. type WeightInfo = pallet_state_trie_migration::weights::SubstrateWeight; type MaxKeyLen = MigrationMaxKeyLen; } -pub struct Disable(sp_std::marker::PhantomData); -impl frame_support::traits::EnsureOrigin for Disable { - type Success = T::AccountId; - fn try_origin(o: T::RuntimeOrigin) -> Result { - Err(o) - } - - #[cfg(feature = "runtime-benchmarks")] - fn try_successful_origin() -> Result { - // Not correct but this should only cost more. - Ok(T::RuntimeOrigin::root()) - } -} - construct_runtime! { pub enum Runtime where Block = Block, From 9b75e96eca760acba88a75493eaf994ce105c5a7 Mon Sep 17 00:00:00 2001 From: cheme Date: Wed, 7 Dec 2022 14:56:05 +0100 Subject: [PATCH 08/10] fix --- runtime/westend/src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/runtime/westend/src/lib.rs b/runtime/westend/src/lib.rs index ff96e46b6a9c..c7529066aa16 100644 --- a/runtime/westend/src/lib.rs +++ b/runtime/westend/src/lib.rs @@ -1864,7 +1864,6 @@ mod init_state_migration { use super::Runtime; use frame_support::traits::OnRuntimeUpgrade; use pallet_state_trie_migration::{AutoLimits, MigrationLimits, MigrationProcess}; - #[cfg(feature = "no_std")] use sp_std::prelude::*; /// Initialize an automatic migration process. From f9c7796b68cf20f73d1d5cf9db9e8c83e549b8f0 Mon Sep 17 00:00:00 2001 From: cheme Date: Wed, 7 Dec 2022 15:07:22 +0100 Subject: [PATCH 09/10] correct fix --- runtime/westend/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/runtime/westend/src/lib.rs b/runtime/westend/src/lib.rs index c7529066aa16..ad9ae67f934e 100644 --- a/runtime/westend/src/lib.rs +++ b/runtime/westend/src/lib.rs @@ -1864,6 +1864,7 @@ mod init_state_migration { use super::Runtime; use frame_support::traits::OnRuntimeUpgrade; use pallet_state_trie_migration::{AutoLimits, MigrationLimits, MigrationProcess}; + #[cfg(not(feature = "std"))] use sp_std::prelude::*; /// Initialize an automatic migration process. From 24ede007072a7a7b50715323c0a2a94c0b147cb4 Mon Sep 17 00:00:00 2001 From: cheme Date: Fri, 9 Dec 2022 09:06:28 +0100 Subject: [PATCH 10/10] check init state in try-runtime --- runtime/westend/src/lib.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/runtime/westend/src/lib.rs b/runtime/westend/src/lib.rs index ad9ae67f934e..d38e39c12bd5 100644 --- a/runtime/westend/src/lib.rs +++ b/runtime/westend/src/lib.rs @@ -1872,6 +1872,10 @@ mod init_state_migration { impl OnRuntimeUpgrade for InitMigrate { #[cfg(feature = "try-runtime")] fn pre_upgrade() -> Result, &'static str> { + frame_support::ensure!( + AutoLimits::::get().is_none(), + "Automigration already started." + ); Ok(Default::default()) }