Skip to content

Commit

Permalink
Remove OnRuntimeUpgrade hooks (#614)
Browse files Browse the repository at this point in the history
* Remove OnRuntimeUpgrade hooks

Signed-off-by: Dengjianping <djptux@gmail.com>

* [no ci]Update changelog

Signed-off-by: Dengjianping <djptux@gmail.com>

* [no ci]Fix changelog

Signed-off-by: Dengjianping <djptux@gmail.com>

* Move storage migration to migration file

Signed-off-by: Dengjianping <djptux@gmail.com>

* Fix build

Signed-off-by: Dengjianping <djptux@gmail.com>
Signed-off-by: Shumo Chu <shumo.chu@pm.me>
  • Loading branch information
Dengjianping authored and stechu committed Jul 11, 2022
1 parent c4a7351 commit 384c27f
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 85 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@
## [Unreleased]

### Breaking changes
-[\628](https://github.com/Manta-Network/Manta/pull/628) Improve RPC performance, add `max_receivers` and `max_senders` fields in the RPC request.
- [\#628](https://github.com/Manta-Network/Manta/pull/628) Improve RPC performance, add `max_receivers` and `max_senders` fields in the RPC request.

### Features
- [\#576](https://github.com/Manta-Network/Manta/pull/576) Unfilter xtokens.transfer_multicurrencies and bump MaxAssetsForTransfer to 2.

### Improvements
- [\#449](https://github.com/Manta-Network/Manta/pull/449) Remove strip from CI, and add strip profile to production.
- [\#563](https://github.com/Manta-Network/Manta/pull/563) Re-implement the `TransactAsset` trait with the unified interface of `FungibleLedger` trait, and `AssetConfig` trait.
- [\#576](https://github.com/Manta-Network/Manta/pull/576) Unfilter xtokens.transfer_multicurrencies and bump MaxAssetsForTransfer to 2.
- [\#607](https://github.com/Manta-Network/Manta/pull/607) Turn node client code into library for CLI project
- [\#609](https://github.com/Manta-Network/Manta/pull/609) Update parameter path from `sdk` to `manta-parameters`.
- [\#614](https://github.com/Manta-Network/Manta/pull/614) Remove `OnRuntimeUpgrade` from calamari-runtime.
- [\#619](https://github.com/Manta-Network/Manta/pull/619) Add CI runtime upgrade test for Dolphin and improve test scenario.
- [\#622](https://github.com/Manta-Network/Manta/pull/622) Update parameter path from `sdk` to `manta-parameters`.

### Bug fixes

Expand Down
87 changes: 4 additions & 83 deletions pallets/asset-manager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;

pub mod migrations;
pub mod weights;
pub use crate::weights::WeightInfo;

Expand All @@ -44,11 +44,7 @@ mod tests;
pub mod pallet {

use crate::weights::WeightInfo;
use frame_support::{
pallet_prelude::*,
traits::{Contains, StorageVersion},
transactional, PalletId,
};
use frame_support::{pallet_prelude::*, traits::Contains, transactional, PalletId};
use frame_system::pallet_prelude::*;
use manta_primitives::{
assets::{
Expand Down Expand Up @@ -485,9 +481,7 @@ pub mod pallet {
}

/// Get para id from asset location
pub(crate) fn get_para_id_from_multilocation(
location: Option<&MultiLocation>,
) -> Option<ParaId> {
pub fn get_para_id_from_multilocation(location: Option<&MultiLocation>) -> Option<ParaId> {
if let Some(MultiLocation { interior, .. }) = location {
match interior {
Junctions::X1(Junction::Parachain(para_id))
Expand All @@ -506,7 +500,7 @@ pub mod pallet {
}

/// Increases the count of associated assets for the para id.
pub(crate) fn increase_count_of_associated_assets(para_id: ParaId) -> DispatchResult {
pub fn increase_count_of_associated_assets(para_id: ParaId) -> DispatchResult {
// If it's a new para id, which will be inserted with AssetCount as 1.
// If not, AssetCount will increased by 1.
if AllowedDestParaIds::<T>::contains_key(para_id) {
Expand Down Expand Up @@ -558,79 +552,6 @@ pub mod pallet {
}
}

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_runtime_upgrade() -> Weight {
// currently, it's 0 on calamari.
let storage_version = Self::on_chain_storage_version();
if storage_version < 1 {
log::info!(target: "asset-manager", "Start to execute storage migration for asset-manager.");

let mut reads: Weight = 0;
let mut writes: Weight = 0;
LocationAssetId::<T>::iter().for_each(|(location, _asset_id)| {
reads += 1;
if let Some(para_id) =
Self::get_para_id_from_multilocation(location.into().as_ref())
{
if para_id != 2084 {
let _ = Self::increase_count_of_associated_assets(para_id);
reads += 1; // There's one read in method increase_count_of_associated_assets.
writes += 1; // There's one write in method increase_count_of_associated_assets.
}
}
});

// Update storage version.
StorageVersion::new(1u16).put::<Self>();
writes += 1;

T::DbWeight::get()
.reads(reads)
.saturating_add(T::DbWeight::get().writes(writes))
} else {
log::info!("✅ no migration for asset-manager.");
// only 1 read
T::DbWeight::get().reads(1)
}
}

#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<(), &'static str> {
let storage_version = Self::on_chain_storage_version();

if storage_version >= 1 {
return Err("Storage version is >= 1, the migration won't be executed.");
}

Ok(())
}

#[cfg(feature = "try-runtime")]
fn post_upgrade() -> Result<(), &'static str> {
let storage_version = Self::on_chain_storage_version();

if storage_version < 1 {
return Err("Storage version is >= 1, the migration won't be executed.");
}

let acala = (2000, 3); // karura has 3 asset locations on calamari.
let moonbeam = (2023, 1); // moonbean has 1 asset location on calamari.
let calamari = 2084; // our own asset location won't be counted.
if AllowedDestParaIds::<T>::get(acala.0) == Some(acala.1)
&& AllowedDestParaIds::<T>::get(moonbeam.0) == Some(moonbeam.1)
&& AllowedDestParaIds::<T>::get(calamari).is_none()
{
log::info!(
"✅ Storage migration for asset-manager has been executed successfully."
);
Ok(())
} else {
Err("Failed to executed storage migration for asset-manager.")
}
}
}

#[cfg(feature = "std")]
impl<T: Config> GenesisConfig<T> {
/// Direct implementation of `GenesisBuild::build_storage`.
Expand Down
97 changes: 97 additions & 0 deletions pallets/asset-manager/src/migrations.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Copyright 2020-2022 Manta Network.
// This file is part of Manta.
//
// Manta is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Manta is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Manta. If not, see <http://www.gnu.org/licenses/>.

///! Do storage migration for AllowedDestParaIds which records the count of associated assets for each para id.
use super::*;
use core::marker::PhantomData;
use frame_support::{
dispatch::GetStorageVersion,
pallet_prelude::Weight,
traits::{Get, OnRuntimeUpgrade, PalletInfoAccess, StorageVersion},
};

pub struct AllowedDestParaIdsMigration<T>(PhantomData<T>);
impl<T: GetStorageVersion + Config + PalletInfoAccess> OnRuntimeUpgrade
for AllowedDestParaIdsMigration<T>
{
fn on_runtime_upgrade() -> Weight {
// currently, it's 0 on calamari.
let storage_version = <T as GetStorageVersion>::on_chain_storage_version();
if storage_version < 1 {
log::info!(target: "asset-manager", "Start to execute storage migration for asset-manager.");

let mut reads: Weight = 0;
let mut writes: Weight = 0;
LocationAssetId::<T>::iter().for_each(|(location, _asset_id)| {
reads += 1;
if let Some(para_id) =
Pallet::<T>::get_para_id_from_multilocation(location.into().as_ref())
{
if para_id != 2084 {
let _ = Pallet::<T>::increase_count_of_associated_assets(para_id);
reads += 1; // There's one read in method increase_count_of_associated_assets.
writes += 1; // There's one write in method increase_count_of_associated_assets.
}
}
});

// Update storage version.
StorageVersion::new(1u16).put::<T>();
writes += 1;

T::DbWeight::get()
.reads(reads)
.saturating_add(T::DbWeight::get().writes(writes))
} else {
log::info!("✅ no migration for asset-manager.");
// only 1 read
T::DbWeight::get().reads(1)
}
}

#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<(), &'static str> {
let storage_version = <T as GetStorageVersion>::on_chain_storage_version();

if storage_version >= 1 {
return Err("Storage version is >= 1, the migration won't be executed.");
}

Ok(())
}

#[cfg(feature = "try-runtime")]
fn post_upgrade() -> Result<(), &'static str> {
let storage_version = <T as GetStorageVersion>::on_chain_storage_version();

if storage_version < 1 {
return Err("Storage version is >= 1, the migration won't be executed.");
}

let acala = (2000, 3); // karura has 3 asset locations on calamari.
let moonbeam = (2023, 1); // moonbean has 1 asset location on calamari.
let calamari = 2084; // our own asset location won't be counted.
if AllowedDestParaIds::<T>::get(acala.0) == Some(acala.1)
&& AllowedDestParaIds::<T>::get(moonbeam.0) == Some(moonbeam.1)
&& AllowedDestParaIds::<T>::get(calamari).is_none()
{
log::info!("✅ Storage migration for asset-manager has been executed successfully.");
Ok(())
} else {
Err("Failed to executed storage migration for asset-manager.")
}
}
}
2 changes: 1 addition & 1 deletion runtime/calamari/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@ pub type Executive = frame_executive::Executive<
frame_system::ChainContext<Runtime>,
Runtime,
AllPalletsReversedWithSystemFirst,
crate::migrations::sudo::RemoveSudo<Runtime>,
(),
>;

#[cfg(feature = "runtime-benchmarks")]
Expand Down

0 comments on commit 384c27f

Please sign in to comment.