From af1b50171425e533743a61a811d49774b8dd06f9 Mon Sep 17 00:00:00 2001 From: Dmitry Lavrenov Date: Fri, 1 Nov 2024 13:59:58 +0300 Subject: [PATCH 01/15] Rework migration for pallet-erc20-support --- crates/pallet-erc20-support/src/lib.rs | 33 +---------- .../pallet-erc20-support/src/migrations/v1.rs | 59 +++++++++++++++++-- 2 files changed, 54 insertions(+), 38 deletions(-) diff --git a/crates/pallet-erc20-support/src/lib.rs b/crates/pallet-erc20-support/src/lib.rs index c2ee7b744..5f5feb354 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::MigrationToV1; #[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..81997049f 100644 --- a/crates/pallet-erc20-support/src/migrations/v1.rs +++ b/crates/pallet-erc20-support/src/migrations/v1.rs @@ -1,20 +1,67 @@ //! Migration to Version 1. -use frame_support::{log::info, traits::Get, weights::Weight}; +use frame_support::{ + log::info, + pallet_prelude::*, + sp_std, + traits::{Get, OnRuntimeUpgrade}, + weights::Weight, +}; +#[cfg(feature = "try-runtime")] +use frame_support::{ + sp_runtime::TryRuntimeError, + sp_std::{vec, vec::Vec}, +}; -use crate::BalanceOf; -use crate::{Approvals, Config}; +use crate::{Approvals, BalanceOf, Config, Pallet}; + +/// Execute migration to Version 1. +pub struct MigrationToV1(sp_std::marker::PhantomData<(T, I)>); + +impl, I: 'static> OnRuntimeUpgrade for MigrationToV1 { + fn on_runtime_upgrade() -> Weight { + migrate::() + } + + #[cfg(feature = "try-runtime")] + fn pre_upgrade() -> Result, TryRuntimeError> { + Ok(vec![]) + } + + #[cfg(feature = "try-runtime")] + fn post_upgrade(_state: Vec) -> Result<(), TryRuntimeError> { + ensure!( + >::on_chain_storage_version() == >::current_storage_version(), + "the current storage version and onchain storage version should be the same" + ); + 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!("Already 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 } From 556f2ef955a70d9105ae2941e951ed905f13acfe Mon Sep 17 00:00:00 2001 From: Dmitry Lavrenov Date: Fri, 1 Nov 2024 14:22:04 +0300 Subject: [PATCH 02/15] Rework migration for pallet-humanode-session --- Cargo.lock | 1 - crates/pallet-humanode-session/Cargo.toml | 2 - crates/pallet-humanode-session/src/lib.rs | 25 ++------- .../src/migrations/v1.rs | 55 +++++++++++++------ 4 files changed, 44 insertions(+), 39 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index dc6a9efaf..3e8197536 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6684,7 +6684,6 @@ dependencies = [ "serde", "sp-core", "sp-runtime", - "sp-std", ] [[package]] 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..959e8dfb6 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::MigrationToV1; #[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..4804c77d9 100644 --- a/crates/pallet-humanode-session/src/migrations/v1.rs +++ b/crates/pallet-humanode-session/src/migrations/v1.rs @@ -1,13 +1,39 @@ //! Migration to Version 1. -use frame_support::pallet_prelude::*; -use frame_support::storage_alias; -use frame_support::{dispatch::GetStorageVersion, log::info, traits::Get, weights::Weight}; +use frame_support::{ + log::info, + pallet_prelude::*, + sp_std, storage_alias, + traits::{Get, OnRuntimeUpgrade}, + weights::Weight, +}; #[cfg(feature = "try-runtime")] -use sp_std::{vec, vec::Vec}; +use frame_support::{ + sp_runtime::TryRuntimeError, + sp_std::{vec, vec::Vec}, +}; -use crate::IdentificationFor; -use crate::{Config, CurrentSessionIndex, Pallet, SessionIdentities}; +use crate::{Config, CurrentSessionIndex, IdentificationFor, Pallet, SessionIdentities}; + +/// Execute migration to Version 1. +pub struct MigrationToV1(sp_std::marker::PhantomData); + +impl OnRuntimeUpgrade for MigrationToV1 { + fn on_runtime_upgrade() -> Weight { + migrate::() + } + + #[cfg(feature = "try-runtime")] + fn pre_upgrade() -> Result, TryRuntimeError> { + Ok(pre_migrate::()) + } + + #[cfg(feature = "try-runtime")] + fn post_upgrade(state: Vec) -> Result<(), TryRuntimeError> { + post_migrate::(state); + Ok(()) + } +} /// The Version 0 identities storage. #[storage_alias] @@ -21,19 +47,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!("Already 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 +74,9 @@ pub fn migrate() -> Weight { None }); - // Set new version. - current.put::>(); - - // Write the onchain version. - weight = weight.saturating_add(T::DbWeight::get().writes(1)); + // Set storage version to `1`. + StorageVersion::new(1).put::>(); + weight.saturating_accrue(T::DbWeight::get().writes(1)); // Done. weight From 39214bca1c5bd50fa68b3f078069f7d3d3226848 Mon Sep 17 00:00:00 2001 From: Dmitry Lavrenov Date: Fri, 1 Nov 2024 15:09:07 +0300 Subject: [PATCH 03/15] Rework on runtime upgrade logic for pallet-dummy-precompiles-code --- Cargo.lock | 1 - .../pallet-dummy-precompiles-code/Cargo.toml | 2 - .../pallet-dummy-precompiles-code/src/lib.rs | 54 +------------ .../src/upgrade_init.rs | 75 +++++++++++++++++++ 4 files changed, 78 insertions(+), 54 deletions(-) create mode 100644 crates/pallet-dummy-precompiles-code/src/upgrade_init.rs diff --git a/Cargo.lock b/Cargo.lock index 3e8197536..695c24731 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6448,7 +6448,6 @@ dependencies = [ "parity-scale-codec", "scale-info", "sp-core", - "sp-std", ] [[package]] diff --git a/crates/pallet-dummy-precompiles-code/Cargo.toml b/crates/pallet-dummy-precompiles-code/Cargo.toml index a5fc5ab3d..dcc0163c5 100644 --- a/crates/pallet-dummy-precompiles-code/Cargo.toml +++ b/crates/pallet-dummy-precompiles-code/Cargo.toml @@ -11,7 +11,6 @@ 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 } @@ -35,7 +34,6 @@ std = [ "pallet-timestamp/std", "scale-info/std", "sp-core/std", - "sp-std/std", ] try-runtime = [ "frame-support/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/upgrade_init.rs b/crates/pallet-dummy-precompiles-code/src/upgrade_init.rs new file mode 100644 index 000000000..1a51de26c --- /dev/null +++ b/crates/pallet-dummy-precompiles-code/src/upgrade_init.rs @@ -0,0 +1,75 @@ +//! Upgrade init implementation. + +#[cfg(feature = "try-runtime")] +use frame_support::{sp_runtime::TryRuntimeError, sp_std::vec::Vec, traits::GetStorageVersion}; +use frame_support::{ + sp_std, + sp_tracing::info, + 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!(message = "Nothing to do. This runtime upgrade probably should be removed"); + } + + weight + } + + #[cfg(feature = "try-runtime")] + fn pre_upgrade() -> Result, TryRuntimeError> { + // Do nothing. + Ok(Vec::new()) + } + + #[cfg(feature = "try-runtime")] + fn post_upgrade(_state: Vec) -> Result<(), TryRuntimeError> { + 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(TryRuntimeError::Other( + "precompiles not created properly: {:not_created_precompiles}", + )); + } + + assert_eq!( + >::on_chain_storage_version(), + >::current_storage_version() + ); + + Ok(()) + } +} From 08c62fb64dcf785efa8768a3bd82bc7dcd7bc5e4 Mon Sep 17 00:00:00 2001 From: Dmitry Lavrenov Date: Fri, 1 Nov 2024 15:42:28 +0300 Subject: [PATCH 04/15] Fix error type in try-runtime code --- .../src/upgrade_init.rs | 12 +++++------- crates/pallet-erc20-support/src/migrations/v1.rs | 11 ++++------- crates/pallet-humanode-session/src/migrations/v1.rs | 11 ++++------- 3 files changed, 13 insertions(+), 21 deletions(-) diff --git a/crates/pallet-dummy-precompiles-code/src/upgrade_init.rs b/crates/pallet-dummy-precompiles-code/src/upgrade_init.rs index 1a51de26c..82ff66662 100644 --- a/crates/pallet-dummy-precompiles-code/src/upgrade_init.rs +++ b/crates/pallet-dummy-precompiles-code/src/upgrade_init.rs @@ -1,13 +1,13 @@ //! Upgrade init implementation. -#[cfg(feature = "try-runtime")] -use frame_support::{sp_runtime::TryRuntimeError, sp_std::vec::Vec, traits::GetStorageVersion}; use frame_support::{ sp_std, sp_tracing::info, traits::{Get, OnRuntimeUpgrade}, weights::Weight, }; +#[cfg(feature = "try-runtime")] +use frame_support::{sp_std::vec::Vec, traits::GetStorageVersion}; use crate::{ Config, LastExecutionVersion, LastForceExecuteAskCounter, Pallet, CURRENT_EXECUTION_VERSION, @@ -41,13 +41,13 @@ impl OnRuntimeUpgrade for UpgradeInit { } #[cfg(feature = "try-runtime")] - fn pre_upgrade() -> Result, TryRuntimeError> { + fn pre_upgrade() -> Result, &'static str> { // Do nothing. Ok(Vec::new()) } #[cfg(feature = "try-runtime")] - fn post_upgrade(_state: Vec) -> Result<(), TryRuntimeError> { + fn post_upgrade(_state: Vec) -> Result<(), &'static str> { use crate::DUMMY_CODE; let mut not_created_precompiles = Vec::new(); @@ -60,9 +60,7 @@ impl OnRuntimeUpgrade for UpgradeInit { } if !not_created_precompiles.is_empty() { - return Err(TryRuntimeError::Other( - "precompiles not created properly: {:not_created_precompiles}", - )); + return Err("precompiles not created properly: {:not_created_precompiles}"); } assert_eq!( diff --git a/crates/pallet-erc20-support/src/migrations/v1.rs b/crates/pallet-erc20-support/src/migrations/v1.rs index 81997049f..5595975e6 100644 --- a/crates/pallet-erc20-support/src/migrations/v1.rs +++ b/crates/pallet-erc20-support/src/migrations/v1.rs @@ -1,5 +1,7 @@ //! Migration to Version 1. +#[cfg(feature = "try-runtime")] +use frame_support::sp_std::{vec, vec::Vec}; use frame_support::{ log::info, pallet_prelude::*, @@ -7,11 +9,6 @@ use frame_support::{ traits::{Get, OnRuntimeUpgrade}, weights::Weight, }; -#[cfg(feature = "try-runtime")] -use frame_support::{ - sp_runtime::TryRuntimeError, - sp_std::{vec, vec::Vec}, -}; use crate::{Approvals, BalanceOf, Config, Pallet}; @@ -24,12 +21,12 @@ impl, I: 'static> OnRuntimeUpgrade for MigrationToV1 { } #[cfg(feature = "try-runtime")] - fn pre_upgrade() -> Result, TryRuntimeError> { + fn pre_upgrade() -> Result, &'static str> { Ok(vec![]) } #[cfg(feature = "try-runtime")] - fn post_upgrade(_state: Vec) -> Result<(), TryRuntimeError> { + fn post_upgrade(_state: Vec) -> Result<(), &'static str> { ensure!( >::on_chain_storage_version() == >::current_storage_version(), "the current storage version and onchain storage version should be the same" diff --git a/crates/pallet-humanode-session/src/migrations/v1.rs b/crates/pallet-humanode-session/src/migrations/v1.rs index 4804c77d9..bfd517722 100644 --- a/crates/pallet-humanode-session/src/migrations/v1.rs +++ b/crates/pallet-humanode-session/src/migrations/v1.rs @@ -1,5 +1,7 @@ //! Migration to Version 1. +#[cfg(feature = "try-runtime")] +use frame_support::sp_std::{vec, vec::Vec}; use frame_support::{ log::info, pallet_prelude::*, @@ -7,11 +9,6 @@ use frame_support::{ traits::{Get, OnRuntimeUpgrade}, weights::Weight, }; -#[cfg(feature = "try-runtime")] -use frame_support::{ - sp_runtime::TryRuntimeError, - sp_std::{vec, vec::Vec}, -}; use crate::{Config, CurrentSessionIndex, IdentificationFor, Pallet, SessionIdentities}; @@ -24,12 +21,12 @@ impl OnRuntimeUpgrade for MigrationToV1 { } #[cfg(feature = "try-runtime")] - fn pre_upgrade() -> Result, TryRuntimeError> { + fn pre_upgrade() -> Result, &'static str> { Ok(pre_migrate::()) } #[cfg(feature = "try-runtime")] - fn post_upgrade(state: Vec) -> Result<(), TryRuntimeError> { + fn post_upgrade(state: Vec) -> Result<(), &'static str> { post_migrate::(state); Ok(()) } From 4b263f1c1fb8e4584b7b55b4fab33d15bd172500 Mon Sep 17 00:00:00 2001 From: Dmitry Lavrenov Date: Mon, 4 Nov 2024 15:35:42 +0300 Subject: [PATCH 05/15] Fix test for pallet-dummpy-precompiles-code --- crates/pallet-dummy-precompiles-code/src/tests.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/pallet-dummy-precompiles-code/src/tests.rs b/crates/pallet-dummy-precompiles-code/src/tests.rs index 64cef022d..678a22ba7 100644 --- a/crates/pallet-dummy-precompiles-code/src/tests.rs +++ b/crates/pallet-dummy-precompiles-code/src/tests.rs @@ -1,6 +1,6 @@ use frame_support::traits::OnRuntimeUpgrade; -use crate::{mock::*, DUMMY_CODE}; +use crate::{mock::*, UpgradeInit, DUMMY_CODE}; /// This test verifies that genesis initialization properly assigns the state. #[test] @@ -47,7 +47,7 @@ fn runtime_upgrade() { ); // Do runtime upgrade hook. - v1::AllPalletsWithoutSystem::on_runtime_upgrade(); + UpgradeInit::::on_runtime_upgrade(); // Verify precompiles addresses creation. for precompile_address in &v1::PrecompilesAddresses::get() { From 9f8ccc977795140b164f47291718a40ecb1d4ef2 Mon Sep 17 00:00:00 2001 From: Dmitry Lavrenov Date: Mon, 4 Nov 2024 15:36:13 +0300 Subject: [PATCH 06/15] Add migrations to humanode-runtime --- crates/humanode-runtime/src/lib.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/crates/humanode-runtime/src/lib.rs b/crates/humanode-runtime/src/lib.rs index 4b5d68aef..f53c5d789 100644 --- a/crates/humanode-runtime/src/lib.rs +++ b/crates/humanode-runtime/src/lib.rs @@ -887,6 +887,9 @@ pub type Executive = frame_executive::Executive< BalancedCurrencySwapBridgesInitializer, Runtime, >, + pallet_dummy_precompiles_code::UpgradeInit, + pallet_erc20_support::MigrationToV1, + pallet_humanode_session::MigrationToV1, ), >; From 3e71309f2449c0a6882784880bbca21b40ae55d1 Mon Sep 17 00:00:00 2001 From: Dmitry Lavrenov Date: Mon, 4 Nov 2024 15:39:21 +0300 Subject: [PATCH 07/15] Remove not expected check --- crates/pallet-dummy-precompiles-code/src/upgrade_init.rs | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/crates/pallet-dummy-precompiles-code/src/upgrade_init.rs b/crates/pallet-dummy-precompiles-code/src/upgrade_init.rs index 82ff66662..929b6e73a 100644 --- a/crates/pallet-dummy-precompiles-code/src/upgrade_init.rs +++ b/crates/pallet-dummy-precompiles-code/src/upgrade_init.rs @@ -1,13 +1,13 @@ //! Upgrade init implementation. +#[cfg(feature = "try-runtime")] +use frame_support::sp_std::vec::Vec; use frame_support::{ sp_std, sp_tracing::info, traits::{Get, OnRuntimeUpgrade}, weights::Weight, }; -#[cfg(feature = "try-runtime")] -use frame_support::{sp_std::vec::Vec, traits::GetStorageVersion}; use crate::{ Config, LastExecutionVersion, LastForceExecuteAskCounter, Pallet, CURRENT_EXECUTION_VERSION, @@ -63,11 +63,6 @@ impl OnRuntimeUpgrade for UpgradeInit { return Err("precompiles not created properly: {:not_created_precompiles}"); } - assert_eq!( - >::on_chain_storage_version(), - >::current_storage_version() - ); - Ok(()) } } From 4e8aa38fc66eab3ef57540093e305b2373ec37da Mon Sep 17 00:00:00 2001 From: Dmitry Lavrenov Date: Mon, 4 Nov 2024 15:51:09 +0300 Subject: [PATCH 08/15] Rework on runtime upgrade logic for pallet-balanced-currency-swap-bridges-initializer --- .../src/lib.rs | 24 +----- .../src/tests.rs | 6 +- .../src/upgrade_init.rs | 82 ++++++++++--------- 3 files changed, 49 insertions(+), 63 deletions(-) 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/tests.rs b/crates/pallet-balanced-currency-swap-bridges-initializer/src/tests.rs index f5a70a3a2..ce6be6787 100644 --- a/crates/pallet-balanced-currency-swap-bridges-initializer/src/tests.rs +++ b/crates/pallet-balanced-currency-swap-bridges-initializer/src/tests.rs @@ -5,7 +5,7 @@ use frame_support::{ use crate::{ mock::{new_test_ext_with, v0, v1, v2, with_runtime_lock, *}, - swappable_balance, LastForceRebalanceAskCounter, LastInitializerVersion, + swappable_balance, LastForceRebalanceAskCounter, LastInitializerVersion, UpgradeInit, }; /// This test verifies that balanced bridges initialization works in case bridge pot accounts @@ -411,7 +411,7 @@ fn runtime_upgrade() { assert_eq!(>::get(), 0); // Do runtime upgrade hook. - v1::AllPalletsWithoutSystem::on_runtime_upgrade(); + UpgradeInit::::on_runtime_upgrade(); // Verify bridges initialization result. assert_eq!( @@ -457,7 +457,7 @@ fn runtime_upgrade() { v2::EvmBalances::total_balance(&v2::SwapBridgeEvmToNativePot::account_id()); // Do runtime upgrade hook. - v2::AllPalletsWithoutSystem::on_runtime_upgrade(); + UpgradeInit::::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(()) + } } From 4a49be2f7dcfae6567dcec8b68747a4c5658207d Mon Sep 17 00:00:00 2001 From: Dmitry Lavrenov Date: Fri, 8 Nov 2024 10:28:11 +0300 Subject: [PATCH 09/15] Rename migration name --- crates/humanode-runtime/src/lib.rs | 5 +++-- crates/pallet-erc20-support/src/lib.rs | 2 +- crates/pallet-erc20-support/src/migrations/v1.rs | 8 ++++---- crates/pallet-humanode-session/src/lib.rs | 2 +- crates/pallet-humanode-session/src/migrations/v1.rs | 10 +++++----- 5 files changed, 14 insertions(+), 13 deletions(-) diff --git a/crates/humanode-runtime/src/lib.rs b/crates/humanode-runtime/src/lib.rs index f53c5d789..a9913be5d 100644 --- a/crates/humanode-runtime/src/lib.rs +++ b/crates/humanode-runtime/src/lib.rs @@ -888,8 +888,9 @@ pub type Executive = frame_executive::Executive< Runtime, >, pallet_dummy_precompiles_code::UpgradeInit, - pallet_erc20_support::MigrationToV1, - pallet_humanode_session::MigrationToV1, + pallet_balanced_currency_swap_bridges_initializer::UpgradeInit, + pallet_erc20_support::MigrationV0ToV1, + pallet_humanode_session::MigrationV0ToV1, ), >; diff --git a/crates/pallet-erc20-support/src/lib.rs b/crates/pallet-erc20-support/src/lib.rs index 5f5feb354..2f45d9a12 100644 --- a/crates/pallet-erc20-support/src/lib.rs +++ b/crates/pallet-erc20-support/src/lib.rs @@ -11,7 +11,7 @@ use frame_support::{ pub use pallet::*; mod migrations; -pub use migrations::v1::MigrationToV1; +pub use migrations::v1::MigrationV0ToV1; #[cfg(test)] mod mock; diff --git a/crates/pallet-erc20-support/src/migrations/v1.rs b/crates/pallet-erc20-support/src/migrations/v1.rs index 5595975e6..1bcfa5656 100644 --- a/crates/pallet-erc20-support/src/migrations/v1.rs +++ b/crates/pallet-erc20-support/src/migrations/v1.rs @@ -1,4 +1,4 @@ -//! Migration to Version 1. +//! Migration to Version 1 from Version 0. #[cfg(feature = "try-runtime")] use frame_support::sp_std::{vec, vec::Vec}; @@ -12,10 +12,10 @@ use frame_support::{ use crate::{Approvals, BalanceOf, Config, Pallet}; -/// Execute migration to Version 1. -pub struct MigrationToV1(sp_std::marker::PhantomData<(T, I)>); +/// Execute migration to Version 1 from Version 0. +pub struct MigrationV0ToV1(sp_std::marker::PhantomData<(T, I)>); -impl, I: 'static> OnRuntimeUpgrade for MigrationToV1 { +impl, I: 'static> OnRuntimeUpgrade for MigrationV0ToV1 { fn on_runtime_upgrade() -> Weight { migrate::() } diff --git a/crates/pallet-humanode-session/src/lib.rs b/crates/pallet-humanode-session/src/lib.rs index 959e8dfb6..916d41814 100644 --- a/crates/pallet-humanode-session/src/lib.rs +++ b/crates/pallet-humanode-session/src/lib.rs @@ -13,7 +13,7 @@ pub use weights::*; pub mod weights; mod migrations; -pub use migrations::v1::MigrationToV1; +pub use migrations::v1::MigrationV0ToV1; #[cfg(feature = "runtime-benchmarks")] pub mod benchmarking; diff --git a/crates/pallet-humanode-session/src/migrations/v1.rs b/crates/pallet-humanode-session/src/migrations/v1.rs index bfd517722..beacbe074 100644 --- a/crates/pallet-humanode-session/src/migrations/v1.rs +++ b/crates/pallet-humanode-session/src/migrations/v1.rs @@ -1,4 +1,4 @@ -//! Migration to Version 1. +//! Migration to Version 1 from Version 0. #[cfg(feature = "try-runtime")] use frame_support::sp_std::{vec, vec::Vec}; @@ -12,10 +12,10 @@ use frame_support::{ use crate::{Config, CurrentSessionIndex, IdentificationFor, Pallet, SessionIdentities}; -/// Execute migration to Version 1. -pub struct MigrationToV1(sp_std::marker::PhantomData); +/// Execute migration to Version 1 from Version 0. +pub struct MigrationV0ToV1(sp_std::marker::PhantomData); -impl OnRuntimeUpgrade for MigrationToV1 { +impl OnRuntimeUpgrade for MigrationV0ToV1 { fn on_runtime_upgrade() -> Weight { migrate::() } @@ -50,7 +50,7 @@ pub fn migrate() -> Weight { let mut weight: Weight = T::DbWeight::get().reads(1); if onchain != 0 { - info!("Already not at version 0, nothing to do. This migrarion probably should be removed"); + info!("Not at version 0, nothing to do. This migrarion probably should be removed"); return weight; } From fadf85009904c366e851d1ec9f586bfd511b55d9 Mon Sep 17 00:00:00 2001 From: Dmitry Lavrenov Date: Fri, 8 Nov 2024 10:32:59 +0300 Subject: [PATCH 10/15] Use assert_eq instead of ensure --- crates/pallet-erc20-support/src/migrations/v1.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/pallet-erc20-support/src/migrations/v1.rs b/crates/pallet-erc20-support/src/migrations/v1.rs index 1bcfa5656..b1a699a39 100644 --- a/crates/pallet-erc20-support/src/migrations/v1.rs +++ b/crates/pallet-erc20-support/src/migrations/v1.rs @@ -27,9 +27,9 @@ impl, I: 'static> OnRuntimeUpgrade for MigrationV0ToV1 { #[cfg(feature = "try-runtime")] fn post_upgrade(_state: Vec) -> Result<(), &'static str> { - ensure!( - >::on_chain_storage_version() == >::current_storage_version(), - "the current storage version and onchain storage version should be the same" + assert_eq!( + >::on_chain_storage_version(), + >::current_storage_version() ); Ok(()) } From 9ad5aba692a9aa900884b3fc1440389cca938441 Mon Sep 17 00:00:00 2001 From: Dmitry Lavrenov Date: Fri, 8 Nov 2024 10:34:20 +0300 Subject: [PATCH 11/15] Improve info message --- crates/pallet-erc20-support/src/migrations/v1.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/pallet-erc20-support/src/migrations/v1.rs b/crates/pallet-erc20-support/src/migrations/v1.rs index b1a699a39..2531bdf56 100644 --- a/crates/pallet-erc20-support/src/migrations/v1.rs +++ b/crates/pallet-erc20-support/src/migrations/v1.rs @@ -43,7 +43,7 @@ fn migrate, I: 'static>() -> Weight { let mut weight: Weight = T::DbWeight::get().reads(1); if onchain_version != 0 { - info!("Already not at version 0, nothing to do. This migrarion probably should be removed"); + info!("Not at version 0, nothing to do. This migrarion probably should be removed"); return weight; } From dba2f4c0d243c1abadbdb91b5e59e18ab81f3290 Mon Sep 17 00:00:00 2001 From: Dmitry Lavrenov Date: Fri, 8 Nov 2024 10:46:48 +0300 Subject: [PATCH 12/15] Use log instead of sp_tracing --- crates/pallet-dummy-precompiles-code/src/upgrade_init.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/pallet-dummy-precompiles-code/src/upgrade_init.rs b/crates/pallet-dummy-precompiles-code/src/upgrade_init.rs index 929b6e73a..c6568375c 100644 --- a/crates/pallet-dummy-precompiles-code/src/upgrade_init.rs +++ b/crates/pallet-dummy-precompiles-code/src/upgrade_init.rs @@ -3,8 +3,8 @@ #[cfg(feature = "try-runtime")] use frame_support::sp_std::vec::Vec; use frame_support::{ + log::info, sp_std, - sp_tracing::info, traits::{Get, OnRuntimeUpgrade}, weights::Weight, }; @@ -34,7 +34,7 @@ impl OnRuntimeUpgrade for UpgradeInit { >::put(current_force_execute_ask_counter); weight.saturating_accrue(T::DbWeight::get().writes(2)); } else { - info!(message = "Nothing to do. This runtime upgrade probably should be removed"); + info!("Nothing to do. This runtime upgrade probably should be removed"); } weight From 758cddd7272281af09ef13012fc4a9cca98b16d9 Mon Sep 17 00:00:00 2001 From: Dmitry Lavrenov Date: Fri, 8 Nov 2024 11:33:19 +0300 Subject: [PATCH 13/15] Improve log messages --- crates/pallet-humanode-session/src/migrations/v1.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/pallet-humanode-session/src/migrations/v1.rs b/crates/pallet-humanode-session/src/migrations/v1.rs index beacbe074..b2ed8dc53 100644 --- a/crates/pallet-humanode-session/src/migrations/v1.rs +++ b/crates/pallet-humanode-session/src/migrations/v1.rs @@ -75,6 +75,8 @@ pub fn migrate() -> Weight { StorageVersion::new(1).put::>(); weight.saturating_accrue(T::DbWeight::get().writes(1)); + info!("Migrated to v1"); + // Done. weight } From 758a7763c7895b487c37c9252f2d258723b74fcc Mon Sep 17 00:00:00 2001 From: Dmitry Lavrenov Date: Wed, 13 Nov 2024 16:28:43 +0300 Subject: [PATCH 14/15] Properly run on_runtime_upgrade in tests --- Cargo.lock | 2 ++ .../Cargo.toml | 3 +++ .../src/mock/v1.rs | 15 +++++++++++++-- .../src/mock/v2.rs | 15 +++++++++++++-- .../src/tests.rs | 11 ++++------- crates/pallet-dummy-precompiles-code/Cargo.toml | 3 +++ .../pallet-dummy-precompiles-code/src/mock/v1.rs | 16 +++++++++++++--- .../pallet-dummy-precompiles-code/src/tests.rs | 8 +++----- 8 files changed, 54 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 695c24731..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", 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/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 ce6be6787..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,11 +1,8 @@ -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, *}, - swappable_balance, LastForceRebalanceAskCounter, LastInitializerVersion, UpgradeInit, + swappable_balance, LastForceRebalanceAskCounter, LastInitializerVersion, }; /// This test verifies that balanced bridges initialization works in case bridge pot accounts @@ -411,7 +408,7 @@ fn runtime_upgrade() { assert_eq!(>::get(), 0); // Do runtime upgrade hook. - UpgradeInit::::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. - UpgradeInit::::on_runtime_upgrade(); + v2::Executive::execute_on_runtime_upgrade(); // Verify result. assert_eq!( diff --git a/crates/pallet-dummy-precompiles-code/Cargo.toml b/crates/pallet-dummy-precompiles-code/Cargo.toml index dcc0163c5..2aefec4ea 100644 --- a/crates/pallet-dummy-precompiles-code/Cargo.toml +++ b/crates/pallet-dummy-precompiles-code/Cargo.toml @@ -14,6 +14,7 @@ sp-core = { 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"] } @@ -25,6 +26,7 @@ default = ["std"] std = [ "codec/std", "fp-evm/std", + "frame-executive/std", "frame-support/std", "frame-system/std", "pallet-balances/std", @@ -36,6 +38,7 @@ std = [ "sp-core/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/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 678a22ba7..4f565e3c8 100644 --- a/crates/pallet-dummy-precompiles-code/src/tests.rs +++ b/crates/pallet-dummy-precompiles-code/src/tests.rs @@ -1,6 +1,4 @@ -use frame_support::traits::OnRuntimeUpgrade; - -use crate::{mock::*, UpgradeInit, DUMMY_CODE}; +use crate::{mock::*, DUMMY_CODE}; /// This test verifies that genesis initialization properly assigns the state. #[test] @@ -46,8 +44,8 @@ fn runtime_upgrade() { 0 ); - // Do runtime upgrade hook. - UpgradeInit::::on_runtime_upgrade(); + // // Do runtime upgrade hook. + v1::Executive::execute_on_runtime_upgrade(); // Verify precompiles addresses creation. for precompile_address in &v1::PrecompilesAddresses::get() { From 9ff37a18ed609ae7694d2706f61e5ebe65b9eafb Mon Sep 17 00:00:00 2001 From: Dmitry Lavrenov Date: Thu, 21 Nov 2024 15:36:59 +0300 Subject: [PATCH 15/15] Don't run useless migrations at runtime --- crates/humanode-runtime/src/lib.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/crates/humanode-runtime/src/lib.rs b/crates/humanode-runtime/src/lib.rs index a9913be5d..4b5d68aef 100644 --- a/crates/humanode-runtime/src/lib.rs +++ b/crates/humanode-runtime/src/lib.rs @@ -887,10 +887,6 @@ pub type Executive = frame_executive::Executive< BalancedCurrencySwapBridgesInitializer, Runtime, >, - pallet_dummy_precompiles_code::UpgradeInit, - pallet_balanced_currency_swap_bridges_initializer::UpgradeInit, - pallet_erc20_support::MigrationV0ToV1, - pallet_humanode_session::MigrationV0ToV1, ), >;