Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Storage migration asset registry #539

Merged
merged 8 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion runtime/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use frame_system::{
limits::{BlockLength, BlockWeights},
EnsureRoot,
};
use log::info;
pub use orml_tokens;
use orml_tokens::MultiTokenCurrencyExtended;
use orml_traits::{
Expand Down Expand Up @@ -65,9 +66,9 @@ pub use pallet_xyk;
use pallet_xyk::AssetMetadataMutationTrait;

pub mod constants;
pub mod migration;
mod weights;
pub mod xcm_config;
// pub mod xcm_config;

pub mod currency {
use super::Balance;
Expand Down
83 changes: 83 additions & 0 deletions runtime/common/src/migration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
use crate::config::orml_asset_registry::AssetMetadataOf;
use frame_support::{
traits::{Get, OnRuntimeUpgrade},
weights::Weight,
};
use log::info;
use mangata_types::{assets::CustomMetadata, Balance, TokenId};
use sp_runtime::traits::Zero;
use sp_std::marker::PhantomData;

pub struct AssetRegistryMigration<Runtime>(PhantomData<Runtime>);

impl<T> OnRuntimeUpgrade for AssetRegistryMigration<T>
where
T: orml_asset_registry::Config<
CustomMetadata = CustomMetadata,
AssetId = TokenId,
Balance = Balance,
> + orml_tokens::Config<CurrencyId = TokenId>,
{
fn on_runtime_upgrade() -> Weight {
info!(
target: "asset_registry",
"on_runtime_upgrade: Attempted to apply AssetRegistry migration"
);

let mut weight: Weight = Weight::zero();

orml_asset_registry::Metadata::<T>::translate(|token_id, meta: AssetMetadataOf| {
weight
.saturating_accrue(<T as frame_system::Config>::DbWeight::get().reads_writes(1, 1));

let issuance = orml_tokens::Pallet::<T>::total_issuance(token_id);
let name = sp_std::str::from_utf8(&meta.name);
if issuance.is_zero() && name.map_or(false, |n| n.starts_with("Liquidity")) {
// By returning None from f for an element, we’ll remove it from the map.
// Based on the docs of translate method
None
} else {
Some(meta)
}
});

weight
}

#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, &'static str> {
info!(
target: "asset_registry",
"pre_upgrade: checks"
);
let mut has_zero_issuance: Vec<u32> = vec![];
orml_asset_registry::Metadata::<T>::iter().for_each(|(token_id, meta)| {
let issuance = orml_tokens::Pallet::<T>::total_issuance(token_id);
let name = sp_std::str::from_utf8(&meta.name);
if issuance.is_zero() && name.map_or(false, |n| n.starts_with("Liquidity")) {
has_zero_issuance.push(token_id);
}
});

assert!(!has_zero_issuance.is_empty(), "No migration is required as we have identified only those liquidity assets with non-zero issuance.");

Ok(Vec::new())
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(_: Vec<u8>) -> Result<(), &'static str> {
info!(
target: "asset_registry",
"post_upgrade: checks"
);
orml_asset_registry::Metadata::<T>::iter().for_each(|(token_id, meta)| {
let issuance = orml_tokens::Pallet::<T>::total_issuance(token_id);
let name = sp_std::str::from_utf8(&meta.name);
if name.map_or(false, |n| n.starts_with("Liquidity")) {
assert!(!issuance.is_zero());
}
});

Ok(())
}
}
6 changes: 3 additions & 3 deletions runtime/mangata-kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub type Executive = frame_executive::Executive<
frame_system::ChainContext<Runtime>,
Runtime,
AllPalletsWithSystem,
// ()
common_runtime::migration::AssetRegistryMigration<Runtime>,
>;

/// Opaque types. These are used by the CLI to instantiate machinery that don't need to know
Expand Down Expand Up @@ -1182,11 +1182,11 @@ impl_runtime_apis! {

#[cfg(feature = "try-runtime")]
impl frame_try_runtime::TryRuntime<Block> for Runtime {
fn on_runtime_upgrade(checks: frame_try_runtime::UpgradeCheckSelect) -> (Weight, Weight) {
fn on_runtime_upgrade(_checks: frame_try_runtime::UpgradeCheckSelect) -> (Weight, Weight) {
// NOTE: intentional unwrap: we don't want to propagate the error backwards, and want to
// have a backtrace here. If any of the pre/post migration checks fail, we shall stop
// right here and right now.
let weight = Executive::try_runtime_upgrade(checks).unwrap();
let weight = Executive::try_runtime_upgrade(frame_try_runtime::UpgradeCheckSelect::All).unwrap();
(weight, cfg::frame_system::RuntimeBlockWeights::get().max_block)
}

Expand Down
9 changes: 4 additions & 5 deletions runtime/mangata-rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use pallet_vesting_mangata_rpc_runtime_api::VestingInfosWithLockedAt;
// Polkadot Imports
pub use polkadot_runtime_common::BlockHashCount;

pub use common_runtime::{currency::*, deposit, runtime_types, tokens, CallType};
use sp_api::impl_runtime_apis;
pub use sp_consensus_aura::sr25519::AuthorityId as AuraId;
use sp_core::{crypto::KeyTypeId, OpaqueMetadata};
Expand All @@ -44,8 +45,6 @@ use sp_version::NativeVersion;
use sp_version::RuntimeVersion;
use static_assertions::const_assert;
pub use xcm::{latest::prelude::*, VersionedMultiLocation};

pub use common_runtime::{currency::*, deposit, runtime_types, tokens, CallType};
// pub use constants::{fee::*, parachains::*};
use mangata_support::traits::ProofOfStakeRewardsApi;
pub use mangata_types::{
Expand Down Expand Up @@ -90,7 +89,7 @@ pub type Executive = frame_executive::Executive<
frame_system::ChainContext<Runtime>,
Runtime,
AllPalletsWithSystem,
(),
common_runtime::migration::AssetRegistryMigration<Runtime>,
>;

/// Opaque types. These are used by the CLI to instantiate machinery that don't need to know
Expand Down Expand Up @@ -1196,11 +1195,11 @@ impl_runtime_apis! {

#[cfg(feature = "try-runtime")]
impl frame_try_runtime::TryRuntime<Block> for Runtime {
fn on_runtime_upgrade(checks: frame_try_runtime::UpgradeCheckSelect) -> (Weight, Weight) {
fn on_runtime_upgrade(_checks: frame_try_runtime::UpgradeCheckSelect) -> (Weight, Weight) {
// NOTE: intentional unwrap: we don't want to propagate the error backwards, and want to
// have a backtrace here. If any of the pre/post migration checks fail, we shall stop
// right here and right now.
let weight = Executive::try_runtime_upgrade(checks).unwrap();
let weight = Executive::try_runtime_upgrade(frame_try_runtime::UpgradeCheckSelect::All).unwrap();
(weight, cfg::frame_system::RuntimeBlockWeights::get().max_block)
}

Expand Down