Skip to content

Commit

Permalink
refactor migration into separate file in common runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
devdanco committed Jul 28, 2023
1 parent 3c73334 commit cecde1a
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 96 deletions.
87 changes: 2 additions & 85 deletions runtime/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use frame_support::{
traits::{
tokens::currency::{MultiTokenCurrency, MultiTokenImbalanceWithZeroTrait},
Contains, EnsureOrigin, EnsureOriginWithArg, ExistenceRequirement, Get, Imbalance,
OnRuntimeUpgrade, WithdrawReasons,
OriginTrait, WithdrawReasons,
},
unsigned::TransactionValidityError,
weights::{constants::WEIGHT_REF_TIME_PER_SECOND, ConstantMultiplier, Weight},
Expand Down Expand Up @@ -66,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 Expand Up @@ -294,89 +294,6 @@ pub mod config {
pub type ReserveIdentifier = [u8; 8];
}

pub mod migration {
use super::*;
use crate::config::orml_asset_registry::AssetMetadataOf;

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(())
}
}
}

pub mod pallet_xyk {

use super::*;
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(())
}
}
7 changes: 2 additions & 5 deletions runtime/mangata-kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ use sp_version::RuntimeVersion;
use static_assertions::const_assert;
pub use xcm::{latest::prelude::*, VersionedMultiLocation};

pub use common_runtime::{
config::migration::AssetRegistryMigration, currency::*, deposit, runtime_types, tokens,
CallType,
};
pub use common_runtime::{currency::*, deposit, runtime_types, tokens, CallType};

use mangata_support::traits::ProofOfStakeRewardsApi;
pub use mangata_types::{
Expand Down Expand Up @@ -93,7 +90,7 @@ pub type Executive = frame_executive::Executive<
frame_system::ChainContext<Runtime>,
Runtime,
AllPalletsWithSystem,
AssetRegistryMigration<Runtime>,
common_runtime::migration::AssetRegistryMigration<Runtime>,
>;

/// Opaque types. These are used by the CLI to instantiate machinery that don't need to know
Expand Down
8 changes: 2 additions & 6 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,11 +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::{
config::migration::AssetRegistryMigration, currency::*, deposit, runtime_types, tokens,
CallType,
};
// pub use constants::{fee::*, parachains::*};
use mangata_support::traits::ProofOfStakeRewardsApi;
pub use mangata_types::{
Expand Down Expand Up @@ -93,7 +89,7 @@ pub type Executive = frame_executive::Executive<
frame_system::ChainContext<Runtime>,
Runtime,
AllPalletsWithSystem,
AssetRegistryMigration<Runtime>,
common_runtime::migration::AssetRegistryMigration<Runtime>,
>;

/// Opaque types. These are used by the CLI to instantiate machinery that don't need to know
Expand Down

0 comments on commit cecde1a

Please sign in to comment.