diff --git a/Cargo.lock b/Cargo.lock index dc6a9efaf..9b81cb7aa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6333,6 +6333,7 @@ dependencies = [ name = "pallet-balanced-currency-swap-bridges-initializer" version = "0.1.0" dependencies = [ + "frame-executive", "frame-support", "frame-system", "pallet-balances", @@ -6437,6 +6438,7 @@ name = "pallet-dummy-precompiles-code" version = "0.1.0" dependencies = [ "fp-evm", + "frame-executive", "frame-support", "frame-system", "hex-literal", @@ -6448,7 +6450,6 @@ dependencies = [ "parity-scale-codec", "scale-info", "sp-core", - "sp-std", ] [[package]] @@ -6684,7 +6685,6 @@ dependencies = [ "serde", "sp-core", "sp-runtime", - "sp-std", ] [[package]] diff --git a/crates/pallet-balanced-currency-swap-bridges-initializer/Cargo.toml b/crates/pallet-balanced-currency-swap-bridges-initializer/Cargo.toml index 46ae19a2a..fcb1084c9 100644 --- a/crates/pallet-balanced-currency-swap-bridges-initializer/Cargo.toml +++ b/crates/pallet-balanced-currency-swap-bridges-initializer/Cargo.toml @@ -14,6 +14,7 @@ sp-std = { workspace = true } [dev-dependencies] pallet-pot = { path = "../pallet-pot", default-features = false } +frame-executive = { workspace = true } pallet-balances = { workspace = true } sp-core = { workspace = true } @@ -21,6 +22,7 @@ sp-core = { workspace = true } default = ["std"] std = [ "codec/std", + "frame-executive/std", "frame-support/std", "frame-system/std", "pallet-balances/std", @@ -30,6 +32,7 @@ std = [ "sp-std/std", ] try-runtime = [ + "frame-executive/try-runtime", "frame-support/try-runtime", "frame-system/try-runtime", "pallet-balances/try-runtime", diff --git a/crates/pallet-balanced-currency-swap-bridges-initializer/src/lib.rs b/crates/pallet-balanced-currency-swap-bridges-initializer/src/lib.rs index 3367ff3c0..eb27a8702 100644 --- a/crates/pallet-balanced-currency-swap-bridges-initializer/src/lib.rs +++ b/crates/pallet-balanced-currency-swap-bridges-initializer/src/lib.rs @@ -14,13 +14,12 @@ use frame_support::{ }; pub use pallet::*; use sp_std::cmp::Ordering; -#[cfg(feature = "try-runtime")] -use sp_std::vec::Vec; -pub use weights::*; -pub mod weights; +mod weights; +pub use weights::*; mod upgrade_init; +pub use upgrade_init::UpgradeInit; #[cfg(test)] mod mock; @@ -147,23 +146,6 @@ pub mod pallet { NotBalanced, } - #[pallet::hooks] - impl Hooks> for Pallet { - fn on_runtime_upgrade() -> Weight { - upgrade_init::on_runtime_upgrade::() - } - - #[cfg(feature = "try-runtime")] - fn pre_upgrade() -> Result, &'static str> { - upgrade_init::pre_upgrade() - } - - #[cfg(feature = "try-runtime")] - fn post_upgrade(state: Vec) -> Result<(), &'static str> { - upgrade_init::post_upgrade::(state) - } - } - #[pallet::call(weight(T::WeightInfo))] impl Pallet { /// Verify if currencies are balanced. diff --git a/crates/pallet-balanced-currency-swap-bridges-initializer/src/mock/v1.rs b/crates/pallet-balanced-currency-swap-bridges-initializer/src/mock/v1.rs index 86dda9d63..9f1585183 100644 --- a/crates/pallet-balanced-currency-swap-bridges-initializer/src/mock/v1.rs +++ b/crates/pallet-balanced-currency-swap-bridges-initializer/src/mock/v1.rs @@ -15,11 +15,13 @@ use frame_support::{ use sp_core::{ConstU16, H256}; use super::*; +use crate::UpgradeInit; pub(crate) const FORCE_REBALANCE_ASK_COUNTER: u16 = 0; -type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; -type Block = frame_system::mocking::MockBlock; +type UncheckedExtrinsic = + frame_support::sp_runtime::testing::TestXt>; +type Block = frame_support::sp_runtime::testing::Block; frame_support::construct_runtime!( pub struct Test @@ -144,3 +146,12 @@ impl pallet_balanced_currency_swap_bridges_initializer::Config for Test { type ForceRebalanceAskCounter = ConstU16; type WeightInfo = (); } + +pub(crate) type Executive = frame_executive::Executive< + Test, + Block, + frame_system::ChainContext, + Test, + AllPalletsWithSystem, + UpgradeInit, +>; diff --git a/crates/pallet-balanced-currency-swap-bridges-initializer/src/mock/v2.rs b/crates/pallet-balanced-currency-swap-bridges-initializer/src/mock/v2.rs index 439e0c67d..e6e0c4e50 100644 --- a/crates/pallet-balanced-currency-swap-bridges-initializer/src/mock/v2.rs +++ b/crates/pallet-balanced-currency-swap-bridges-initializer/src/mock/v2.rs @@ -17,9 +17,11 @@ use sp_core::{ConstU16, H256}; pub(crate) const FORCE_REBALANCE_ASK_COUNTER: u16 = 1; use super::*; +use crate::UpgradeInit; -type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; -type Block = frame_system::mocking::MockBlock; +type UncheckedExtrinsic = + frame_support::sp_runtime::testing::TestXt>; +type Block = frame_support::sp_runtime::testing::Block; frame_support::construct_runtime!( pub struct Test @@ -144,3 +146,12 @@ impl pallet_balanced_currency_swap_bridges_initializer::Config for Test { type ForceRebalanceAskCounter = ConstU16; type WeightInfo = (); } + +pub(crate) type Executive = frame_executive::Executive< + Test, + Block, + frame_system::ChainContext, + Test, + AllPalletsWithSystem, + UpgradeInit, +>; diff --git a/crates/pallet-balanced-currency-swap-bridges-initializer/src/tests.rs b/crates/pallet-balanced-currency-swap-bridges-initializer/src/tests.rs index f5a70a3a2..d3640ffc7 100644 --- a/crates/pallet-balanced-currency-swap-bridges-initializer/src/tests.rs +++ b/crates/pallet-balanced-currency-swap-bridges-initializer/src/tests.rs @@ -1,7 +1,4 @@ -use frame_support::{ - assert_storage_noop, - traits::{Currency, OnRuntimeUpgrade}, -}; +use frame_support::{assert_storage_noop, traits::Currency}; use crate::{ mock::{new_test_ext_with, v0, v1, v2, with_runtime_lock, *}, @@ -411,7 +408,7 @@ fn runtime_upgrade() { assert_eq!(>::get(), 0); // Do runtime upgrade hook. - v1::AllPalletsWithoutSystem::on_runtime_upgrade(); + v1::Executive::execute_on_runtime_upgrade(); // Verify bridges initialization result. assert_eq!( @@ -457,7 +454,7 @@ fn runtime_upgrade() { v2::EvmBalances::total_balance(&v2::SwapBridgeEvmToNativePot::account_id()); // Do runtime upgrade hook. - v2::AllPalletsWithoutSystem::on_runtime_upgrade(); + v2::Executive::execute_on_runtime_upgrade(); // Verify result. assert_eq!( diff --git a/crates/pallet-balanced-currency-swap-bridges-initializer/src/upgrade_init.rs b/crates/pallet-balanced-currency-swap-bridges-initializer/src/upgrade_init.rs index ef9375ecd..7c8b5550d 100644 --- a/crates/pallet-balanced-currency-swap-bridges-initializer/src/upgrade_init.rs +++ b/crates/pallet-balanced-currency-swap-bridges-initializer/src/upgrade_init.rs @@ -1,6 +1,10 @@ //! Initialization of the bridge pot accounts on runtime upgrade. -use frame_support::{log, pallet_prelude::*}; +use frame_support::{ + log::{error, info}, + pallet_prelude::*, + traits::OnRuntimeUpgrade, +}; #[cfg(feature = "try-runtime")] use sp_std::vec::Vec; @@ -9,54 +13,54 @@ use crate::{ CURRENT_BRIDGES_INITIALIZER_VERSION, }; -/// Initialize the bridges pot accounts if required. -pub fn on_runtime_upgrade() -> Weight { - let last_initializer_version = >::get(); - let last_force_rebalance_ask_counter = >::get(); - let current_force_rebalance_ask_counter = T::ForceRebalanceAskCounter::get(); +/// Execute upgrade init. +pub struct UpgradeInit(sp_std::marker::PhantomData); + +impl OnRuntimeUpgrade for UpgradeInit { + fn on_runtime_upgrade() -> Weight { + let last_initializer_version = >::get(); + let last_force_rebalance_ask_counter = >::get(); + let current_force_rebalance_ask_counter = T::ForceRebalanceAskCounter::get(); + + let mut weight = T::DbWeight::get().reads(2); - let mut weight = T::DbWeight::get().reads(2); + let is_version_mismatch = last_initializer_version != CURRENT_BRIDGES_INITIALIZER_VERSION; + let is_forced = last_force_rebalance_ask_counter < current_force_rebalance_ask_counter; - let is_version_mismatch = last_initializer_version != CURRENT_BRIDGES_INITIALIZER_VERSION; - let is_forced = last_force_rebalance_ask_counter < current_force_rebalance_ask_counter; + if is_version_mismatch || is_forced { + match Pallet::::initialize() { + Ok(w) => weight.saturating_accrue(w), + Err(err) => error!("error during bridges initialization: {err:?}"), + } - if is_version_mismatch || is_forced { - match Pallet::::initialize() { - Ok(w) => weight.saturating_accrue(w), - Err(err) => log::error!("error during bridges initialization: {err:?}"), + >::put(CURRENT_BRIDGES_INITIALIZER_VERSION); + >::put(current_force_rebalance_ask_counter); + weight.saturating_accrue(T::DbWeight::get().writes(2)); + } else { + info!("Nothing to do. This runtime upgrade probably should be removed"); } - >::put(CURRENT_BRIDGES_INITIALIZER_VERSION); - >::put(current_force_rebalance_ask_counter); - weight.saturating_accrue(T::DbWeight::get().writes(2)); + weight } - weight -} - -/// Check the state before the bridges initialization. -/// -/// Panics if anything goes wrong. -#[cfg(feature = "try-runtime")] -pub fn pre_upgrade() -> Result, &'static str> { - // Do nothing. - Ok(Vec::new()) -} + #[cfg(feature = "try-runtime")] + fn pre_upgrade() -> Result, &'static str> { + // Do nothing. + Ok(Vec::new()) + } -/// Check the state after the bridges initialization. -/// -/// Panics if anything goes wrong. -#[cfg(feature = "try-runtime")] -pub fn post_upgrade(_state: Vec) -> Result<(), &'static str> { - use frame_support::{storage_root, StateVersion}; + #[cfg(feature = "try-runtime")] + fn post_upgrade(_state: Vec) -> Result<(), &'static str> { + use frame_support::{storage_root, StateVersion}; - let storage_root_before = storage_root(StateVersion::V1); + let storage_root_before = storage_root(StateVersion::V1); - if !Pallet::::is_balanced()? { - return Err("currencies are not balanced"); - } + if !Pallet::::is_balanced()? { + return Err("currencies are not balanced"); + } - assert_eq!(storage_root_before, storage_root(StateVersion::V1)); + assert_eq!(storage_root_before, storage_root(StateVersion::V1)); - Ok(()) + Ok(()) + } } diff --git a/crates/pallet-dummy-precompiles-code/Cargo.toml b/crates/pallet-dummy-precompiles-code/Cargo.toml index a5fc5ab3d..2aefec4ea 100644 --- a/crates/pallet-dummy-precompiles-code/Cargo.toml +++ b/crates/pallet-dummy-precompiles-code/Cargo.toml @@ -11,10 +11,10 @@ frame-system = { workspace = true } pallet-evm = { workspace = true } scale-info = { workspace = true, features = ["derive"] } sp-core = { workspace = true } -sp-std = { workspace = true } [dev-dependencies] fp-evm = { workspace = true } +frame-executive = { workspace = true } hex-literal = { workspace = true } pallet-balances = { workspace = true, features = ["default"] } pallet-evm-balances = { workspace = true, features = ["default"] } @@ -26,6 +26,7 @@ default = ["std"] std = [ "codec/std", "fp-evm/std", + "frame-executive/std", "frame-support/std", "frame-system/std", "pallet-balances/std", @@ -35,9 +36,9 @@ std = [ "pallet-timestamp/std", "scale-info/std", "sp-core/std", - "sp-std/std", ] try-runtime = [ + "frame-executive/try-runtime", "frame-support/try-runtime", "frame-system/try-runtime", "pallet-balances/try-runtime", diff --git a/crates/pallet-dummy-precompiles-code/src/lib.rs b/crates/pallet-dummy-precompiles-code/src/lib.rs index b43bd86eb..bf4a038b0 100644 --- a/crates/pallet-dummy-precompiles-code/src/lib.rs +++ b/crates/pallet-dummy-precompiles-code/src/lib.rs @@ -9,6 +9,9 @@ use frame_support::{ pub use pallet::*; use sp_core::H160; +mod upgrade_init; +pub use upgrade_init::UpgradeInit; + #[cfg(test)] mod mock; @@ -33,7 +36,6 @@ pub const DUMMY_CODE: &[u8] = &[0x5F, 0x5F, 0xFD]; #[frame_support::pallet] pub mod pallet { use frame_support::{pallet_prelude::*, sp_std::vec::Vec}; - use frame_system::pallet_prelude::*; use super::*; @@ -76,56 +78,6 @@ pub mod pallet { >::put(T::ForceExecuteAskCounter::get()); } } - - #[pallet::hooks] - impl Hooks> for Pallet { - fn on_runtime_upgrade() -> Weight { - let last_execution_version = Self::last_execution_version(); - let last_force_execute_ask_counter = Self::last_force_execute_ask_counter(); - - let current_force_execute_ask_counter = T::ForceExecuteAskCounter::get(); - let mut weight = T::DbWeight::get().reads(2); - - let is_version_mismatch = last_execution_version != CURRENT_EXECUTION_VERSION; - let is_forced = last_force_execute_ask_counter < current_force_execute_ask_counter; - - if is_version_mismatch || is_forced { - weight.saturating_accrue(Self::precompiles_addresses_add_dummy_code()); - - >::put(CURRENT_EXECUTION_VERSION); - >::put(current_force_execute_ask_counter); - weight.saturating_accrue(T::DbWeight::get().writes(2)); - } - - weight - } - - #[cfg(feature = "try-runtime")] - fn pre_upgrade() -> Result, &'static str> { - // Do nothing. - Ok(Vec::new()) - } - - #[cfg(feature = "try-runtime")] - fn post_upgrade(_state: Vec) -> Result<(), &'static str> { - use sp_std::vec::Vec; - - let mut not_created_precompiles = Vec::new(); - - for precompile_address in &T::PrecompilesAddresses::get() { - let code = pallet_evm::AccountCodes::::get(*precompile_address); - if code != DUMMY_CODE { - not_created_precompiles.push(*precompile_address); - } - } - - if !not_created_precompiles.is_empty() { - return Err("precompiles not created properly: {:not_created_precompiles}"); - } - - Ok(()) - } - } } impl Pallet { diff --git a/crates/pallet-dummy-precompiles-code/src/mock/v1.rs b/crates/pallet-dummy-precompiles-code/src/mock/v1.rs index 4f4e069b7..3109b5cd9 100644 --- a/crates/pallet-dummy-precompiles-code/src/mock/v1.rs +++ b/crates/pallet-dummy-precompiles-code/src/mock/v1.rs @@ -15,10 +15,11 @@ use frame_support::{ use sp_core::{ConstU16, H256, U256}; use super::*; -use crate::{self as pallet_dummy_precompiles_code}; +use crate::{self as pallet_dummy_precompiles_code, UpgradeInit}; -type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; -type Block = frame_system::mocking::MockBlock; +type UncheckedExtrinsic = + frame_support::sp_runtime::testing::TestXt>; +type Block = frame_support::sp_runtime::testing::Block; pub(crate) type AccountId = u64; pub(crate) type EvmAccountId = H160; @@ -165,3 +166,12 @@ impl pallet_dummy_precompiles_code::Config for Test { type PrecompilesAddresses = PrecompilesAddresses; type ForceExecuteAskCounter = ConstU16<0>; } + +pub(crate) type Executive = frame_executive::Executive< + Test, + Block, + frame_system::ChainContext, + Test, + AllPalletsWithSystem, + UpgradeInit, +>; diff --git a/crates/pallet-dummy-precompiles-code/src/tests.rs b/crates/pallet-dummy-precompiles-code/src/tests.rs index 64cef022d..4f565e3c8 100644 --- a/crates/pallet-dummy-precompiles-code/src/tests.rs +++ b/crates/pallet-dummy-precompiles-code/src/tests.rs @@ -1,5 +1,3 @@ -use frame_support::traits::OnRuntimeUpgrade; - use crate::{mock::*, DUMMY_CODE}; /// This test verifies that genesis initialization properly assigns the state. @@ -46,8 +44,8 @@ fn runtime_upgrade() { 0 ); - // Do runtime upgrade hook. - v1::AllPalletsWithoutSystem::on_runtime_upgrade(); + // // Do runtime upgrade hook. + v1::Executive::execute_on_runtime_upgrade(); // Verify precompiles addresses creation. for precompile_address in &v1::PrecompilesAddresses::get() { diff --git a/crates/pallet-dummy-precompiles-code/src/upgrade_init.rs b/crates/pallet-dummy-precompiles-code/src/upgrade_init.rs new file mode 100644 index 000000000..c6568375c --- /dev/null +++ b/crates/pallet-dummy-precompiles-code/src/upgrade_init.rs @@ -0,0 +1,68 @@ +//! Upgrade init implementation. + +#[cfg(feature = "try-runtime")] +use frame_support::sp_std::vec::Vec; +use frame_support::{ + log::info, + sp_std, + traits::{Get, OnRuntimeUpgrade}, + weights::Weight, +}; + +use crate::{ + Config, LastExecutionVersion, LastForceExecuteAskCounter, Pallet, CURRENT_EXECUTION_VERSION, +}; + +/// Execute upgrade init. +pub struct UpgradeInit(sp_std::marker::PhantomData); + +impl OnRuntimeUpgrade for UpgradeInit { + fn on_runtime_upgrade() -> Weight { + let last_execution_version = Pallet::::last_execution_version(); + let last_force_execute_ask_counter = Pallet::::last_force_execute_ask_counter(); + + let current_force_execute_ask_counter = T::ForceExecuteAskCounter::get(); + let mut weight = T::DbWeight::get().reads(2); + + let is_version_mismatch = last_execution_version != CURRENT_EXECUTION_VERSION; + let is_forced = last_force_execute_ask_counter < current_force_execute_ask_counter; + + if is_version_mismatch || is_forced { + weight.saturating_accrue(Pallet::::precompiles_addresses_add_dummy_code()); + + >::put(CURRENT_EXECUTION_VERSION); + >::put(current_force_execute_ask_counter); + weight.saturating_accrue(T::DbWeight::get().writes(2)); + } else { + info!("Nothing to do. This runtime upgrade probably should be removed"); + } + + weight + } + + #[cfg(feature = "try-runtime")] + fn pre_upgrade() -> Result, &'static str> { + // Do nothing. + Ok(Vec::new()) + } + + #[cfg(feature = "try-runtime")] + fn post_upgrade(_state: Vec) -> Result<(), &'static str> { + use crate::DUMMY_CODE; + + let mut not_created_precompiles = Vec::new(); + + for precompile_address in &T::PrecompilesAddresses::get() { + let code = pallet_evm::AccountCodes::::get(*precompile_address); + if code != DUMMY_CODE { + not_created_precompiles.push(*precompile_address); + } + } + + if !not_created_precompiles.is_empty() { + return Err("precompiles not created properly: {:not_created_precompiles}"); + } + + Ok(()) + } +} diff --git a/crates/pallet-erc20-support/src/lib.rs b/crates/pallet-erc20-support/src/lib.rs index c2ee7b744..2f45d9a12 100644 --- a/crates/pallet-erc20-support/src/lib.rs +++ b/crates/pallet-erc20-support/src/lib.rs @@ -11,6 +11,7 @@ use frame_support::{ pub use pallet::*; mod migrations; +pub use migrations::v1::MigrationV0ToV1; #[cfg(test)] mod mock; @@ -45,10 +46,7 @@ type BalanceOf = <>::Currency as Currency #[frame_support::pallet] pub mod pallet { - #[cfg(feature = "try-runtime")] - use frame_support::sp_std::{vec, vec::Vec}; use frame_support::{pallet_prelude::*, sp_runtime::traits::MaybeDisplay, sp_std::fmt::Debug}; - use frame_system::pallet_prelude::*; use super::*; @@ -104,35 +102,6 @@ pub mod pallet { /// Spender can't transfer tokens more than allowed. SpendMoreThanAllowed, } - - #[pallet::hooks] - impl, I: 'static> Hooks> for Pallet { - fn on_runtime_upgrade() -> Weight { - let mut weight = T::DbWeight::get().reads(1); - - if StorageVersion::get::>() == 0 { - weight.saturating_accrue(migrations::v1::migrate::()); - StorageVersion::new(1).put::>(); - weight.saturating_accrue(T::DbWeight::get().writes(1)); - } - - weight - } - - #[cfg(feature = "try-runtime")] - fn pre_upgrade() -> Result, &'static str> { - Ok(vec![]) - } - - #[cfg(feature = "try-runtime")] - fn post_upgrade(_state: Vec) -> Result<(), &'static str> { - assert_eq!( - >::on_chain_storage_version(), - >::current_storage_version() - ); - Ok(()) - } - } } impl, I: 'static> Pallet { diff --git a/crates/pallet-erc20-support/src/migrations/v1.rs b/crates/pallet-erc20-support/src/migrations/v1.rs index 6d12e1dff..2531bdf56 100644 --- a/crates/pallet-erc20-support/src/migrations/v1.rs +++ b/crates/pallet-erc20-support/src/migrations/v1.rs @@ -1,20 +1,64 @@ -//! Migration to Version 1. +//! Migration to Version 1 from Version 0. -use frame_support::{log::info, traits::Get, weights::Weight}; +#[cfg(feature = "try-runtime")] +use frame_support::sp_std::{vec, vec::Vec}; +use frame_support::{ + log::info, + pallet_prelude::*, + sp_std, + traits::{Get, OnRuntimeUpgrade}, + weights::Weight, +}; -use crate::BalanceOf; -use crate::{Approvals, Config}; +use crate::{Approvals, BalanceOf, Config, Pallet}; + +/// Execute migration to Version 1 from Version 0. +pub struct MigrationV0ToV1(sp_std::marker::PhantomData<(T, I)>); + +impl, I: 'static> OnRuntimeUpgrade for MigrationV0ToV1 { + fn on_runtime_upgrade() -> Weight { + migrate::() + } + + #[cfg(feature = "try-runtime")] + fn pre_upgrade() -> Result, &'static str> { + Ok(vec![]) + } + + #[cfg(feature = "try-runtime")] + fn post_upgrade(_state: Vec) -> Result<(), &'static str> { + assert_eq!( + >::on_chain_storage_version(), + >::current_storage_version() + ); + Ok(()) + } +} /// Migrate from version 0 to 1. -pub fn migrate, I: 'static>() -> Weight { - info!("Running migration to v1"); +fn migrate, I: 'static>() -> Weight { + let onchain_version = Pallet::::on_chain_storage_version(); - let mut weight = Weight::zero(); + // Read the onchain version. + let mut weight: Weight = T::DbWeight::get().reads(1); + + if onchain_version != 0 { + info!("Not at version 0, nothing to do. This migrarion probably should be removed"); + return weight; + } + + info!("Running migration to v1"); >::translate(|_owner, _spender, amount: BalanceOf| { weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1)); Some(amount.into()) }); + // Set storage version to `1`. + StorageVersion::new(1).put::>(); + weight.saturating_accrue(T::DbWeight::get().writes(1)); + + info!("Migrated to v1"); + weight } diff --git a/crates/pallet-humanode-session/Cargo.toml b/crates/pallet-humanode-session/Cargo.toml index cf63d823b..9910b9826 100644 --- a/crates/pallet-humanode-session/Cargo.toml +++ b/crates/pallet-humanode-session/Cargo.toml @@ -15,7 +15,6 @@ frame-system = { workspace = true } pallet-session = { workspace = true, features = ["historical"] } scale-info = { workspace = true, features = ["derive"] } sp-runtime = { workspace = true } -sp-std = { workspace = true } [dev-dependencies] mockall = { workspace = true } @@ -43,7 +42,6 @@ std = [ "serde/std", "sp-core/std", "sp-runtime/std", - "sp-std/std", ] try-runtime = [ "frame-support/try-runtime", diff --git a/crates/pallet-humanode-session/src/lib.rs b/crates/pallet-humanode-session/src/lib.rs index f9bdc7438..916d41814 100644 --- a/crates/pallet-humanode-session/src/lib.rs +++ b/crates/pallet-humanode-session/src/lib.rs @@ -2,15 +2,18 @@ #![cfg_attr(not(feature = "std"), no_std)] -use frame_support::traits::{Get, StorageVersion}; +use frame_support::{ + sp_std::prelude::*, + traits::{Get, StorageVersion}, +}; pub use pallet::*; use sp_runtime::traits::Convert; -use sp_std::prelude::*; pub use weights::*; pub mod weights; mod migrations; +pub use migrations::v1::MigrationV0ToV1; #[cfg(feature = "runtime-benchmarks")] pub mod benchmarking; @@ -160,24 +163,6 @@ pub mod pallet { Ok(()) } } - - #[pallet::hooks] - impl Hooks> for Pallet { - fn on_runtime_upgrade() -> Weight { - migrations::v1::migrate::() - } - - #[cfg(feature = "try-runtime")] - fn pre_upgrade() -> Result, &'static str> { - Ok(migrations::v1::pre_migrate::()) - } - - #[cfg(feature = "try-runtime")] - fn post_upgrade(state: Vec) -> Result<(), &'static str> { - migrations::v1::post_migrate::(state); - Ok(()) - } - } } /// The identification type, to indicate where does a particular validator comes from, in a given diff --git a/crates/pallet-humanode-session/src/migrations/v1.rs b/crates/pallet-humanode-session/src/migrations/v1.rs index 0fb340224..b2ed8dc53 100644 --- a/crates/pallet-humanode-session/src/migrations/v1.rs +++ b/crates/pallet-humanode-session/src/migrations/v1.rs @@ -1,13 +1,36 @@ -//! Migration to Version 1. +//! Migration to Version 1 from Version 0. -use frame_support::pallet_prelude::*; -use frame_support::storage_alias; -use frame_support::{dispatch::GetStorageVersion, log::info, traits::Get, weights::Weight}; #[cfg(feature = "try-runtime")] -use sp_std::{vec, vec::Vec}; +use frame_support::sp_std::{vec, vec::Vec}; +use frame_support::{ + log::info, + pallet_prelude::*, + sp_std, storage_alias, + traits::{Get, OnRuntimeUpgrade}, + weights::Weight, +}; + +use crate::{Config, CurrentSessionIndex, IdentificationFor, Pallet, SessionIdentities}; + +/// Execute migration to Version 1 from Version 0. +pub struct MigrationV0ToV1(sp_std::marker::PhantomData); + +impl OnRuntimeUpgrade for MigrationV0ToV1 { + fn on_runtime_upgrade() -> Weight { + migrate::() + } -use crate::IdentificationFor; -use crate::{Config, CurrentSessionIndex, Pallet, SessionIdentities}; + #[cfg(feature = "try-runtime")] + fn pre_upgrade() -> Result, &'static str> { + Ok(pre_migrate::()) + } + + #[cfg(feature = "try-runtime")] + fn post_upgrade(state: Vec) -> Result<(), &'static str> { + post_migrate::(state); + Ok(()) + } +} /// The Version 0 identities storage. #[storage_alias] @@ -21,19 +44,18 @@ pub type CurrentSessionIdentities = StorageMap< /// Migrate from version 0 to 1. pub fn migrate() -> Weight { - let current = >::current_storage_version(); let onchain = >::on_chain_storage_version(); // Read the onchain version. let mut weight: Weight = T::DbWeight::get().reads(1); - info!("Running migration to v1 from {onchain:?}"); - - if onchain == 1 { - info!("Already at version 1, nothing to do"); + if onchain != 0 { + info!("Not at version 0, nothing to do. This migrarion probably should be removed"); return weight; } + info!("Running migration to v1"); + // Restore session index from the session pallet. let session_index = >::current_index(); >::put(session_index); @@ -49,11 +71,11 @@ pub fn migrate() -> Weight { None }); - // Set new version. - current.put::>(); + // Set storage version to `1`. + StorageVersion::new(1).put::>(); + weight.saturating_accrue(T::DbWeight::get().writes(1)); - // Write the onchain version. - weight = weight.saturating_add(T::DbWeight::get().writes(1)); + info!("Migrated to v1"); // Done. weight