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

Remove old migrations #3072

Merged
merged 1 commit into from
Dec 4, 2024
Merged
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
191 changes: 3 additions & 188 deletions runtime/common/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,190 +48,6 @@ where
}
}

pub struct MigrateCodeToStateTrieV1<Runtime>(PhantomData<Runtime>);
impl<Runtime> Migration for MigrateCodeToStateTrieV1<Runtime>
where
Runtime: frame_system::Config,
{
fn friendly_name(&self) -> &str {
"MM_MigrateCodeToStateTrieVersion1"
}

fn migrate(&self, _available_weight: Weight) -> Weight {
use cumulus_primitives_storage_weight_reclaim::get_proof_size;
use sp_core::Get;

let proof_size_before: u64 = get_proof_size().unwrap_or(0);

let key = sp_core::storage::well_known_keys::CODE;
let data = sp_io::storage::get(&key);
if let Some(data) = data {
sp_io::storage::set(&key, &data);
}

let proof_size_after: u64 = get_proof_size().unwrap_or(0);
let proof_size_diff = proof_size_after.saturating_sub(proof_size_before);

Weight::from_parts(0, proof_size_diff)
.saturating_add(<Runtime as frame_system::Config>::DbWeight::get().reads_writes(1, 1))
}

#[cfg(feature = "try-runtime")]
fn pre_upgrade(&self) -> Result<Vec<u8>, sp_runtime::DispatchError> {
use parity_scale_codec::Encode;

let key = sp_core::storage::well_known_keys::CODE;
let data = sp_io::storage::get(&key);
Ok(Encode::encode(&data))
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(&self, state: Vec<u8>) -> Result<(), sp_runtime::DispatchError> {
use frame_support::ensure;
use parity_scale_codec::Encode;
use sp_core::storage::StorageKey;

let key = StorageKey(sp_core::storage::well_known_keys::CODE.to_vec());
let data = sp_io::storage::get(key.as_ref());

ensure!(Encode::encode(&data) == state, "Invalid state");

Ok(())
}
}

#[derive(parity_scale_codec::Decode, Eq, Ord, PartialEq, PartialOrd)]
enum OldAssetType {
Xcm(xcm::v3::Location),
}

pub struct MigrateXcmFeesAssetsMeatdata<Runtime>(PhantomData<Runtime>);
impl<Runtime> Migration for MigrateXcmFeesAssetsMeatdata<Runtime>
where
Runtime: pallet_transaction_payment::Config,
Runtime: pallet_xcm_weight_trader::Config,
{
fn friendly_name(&self) -> &str {
"MM_MigrateXcmFeesAssetsMetadata"
}

fn migrate(&self, _available_weight: Weight) -> Weight {
let supported_assets =
if let Some(supported_assets) = frame_support::storage::migration::get_storage_value::<
Vec<OldAssetType>,
>(b"AssetManager", b"SupportedFeePaymentAssets", &[])
{
sp_std::collections::btree_set::BTreeSet::from_iter(
supported_assets
.into_iter()
.map(|OldAssetType::Xcm(location_v3)| location_v3),
)
} else {
return Weight::default();
};

let mut assets: Vec<(xcm::v4::Location, (bool, u128))> = Vec::new();

for (OldAssetType::Xcm(location_v3), units_per_seconds) in
frame_support::storage::migration::storage_key_iter::<
OldAssetType,
u128,
frame_support::Blake2_128Concat,
>(b"AssetManager", b"AssetTypeUnitsPerSecond")
{
let enabled = supported_assets.get(&location_v3).is_some();

if let Ok(location_v4) = location_v3.try_into() {
assets.push((location_v4, (enabled, units_per_seconds)));
}
}

//***** Start mutate storage *****//

// Write asset metadata in new pallet_xcm_weight_trader
use frame_support::weights::WeightToFee as _;
for (asset_location, (enabled, units_per_second)) in assets {
let native_amount_per_second: u128 =
<Runtime as pallet_transaction_payment::Config>::WeightToFee::weight_to_fee(
&Weight::from_parts(
frame_support::weights::constants::WEIGHT_REF_TIME_PER_SECOND,
0,
),
)
.try_into()
.unwrap_or(u128::MAX);
let relative_price: u128 = native_amount_per_second
.saturating_mul(10u128.pow(pallet_xcm_weight_trader::RELATIVE_PRICE_DECIMALS))
.saturating_div(units_per_second);
pallet_xcm_weight_trader::SupportedAssets::<Runtime>::insert(
asset_location,
(enabled, relative_price),
);
}

// Remove storage value AssetManager::SupportedFeePaymentAssets
frame_support::storage::unhashed::kill(&frame_support::storage::storage_prefix(
b"AssetManager",
b"SupportedFeePaymentAssets",
));

// Remove storage map AssetManager::AssetTypeUnitsPerSecond
let _ = frame_support::storage::migration::clear_storage_prefix(
b"AssetManager",
b"AssetTypeUnitsPerSecond",
&[],
None,
None,
);

Weight::default()
}

#[cfg(feature = "try-runtime")]
fn pre_upgrade(&self) -> Result<Vec<u8>, sp_runtime::DispatchError> {
Ok(Default::default())
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(&self, state: Vec<u8>) -> Result<(), sp_runtime::DispatchError> {
assert!(frame_support::storage::migration::storage_key_iter::<
OldAssetType,
u128,
frame_support::Blake2_128Concat,
>(b"AssetManager", b"AssetTypeUnitsPerSecond")
.next()
.is_none());

Ok(())
}
}

pub struct MigrateStakingParachainBondConfig<Runtime>(PhantomData<Runtime>);
impl<Runtime> Migration for MigrateStakingParachainBondConfig<Runtime>
where
Runtime: pallet_parachain_staking::Config,
{
fn friendly_name(&self) -> &str {
"MM_MigrateStakingParachainBondConfig"
}

fn migrate(&self, _available_weight: Weight) -> Weight {
pallet_parachain_staking::migrations::MigrateParachainBondConfig::<Runtime>::on_runtime_upgrade()
}

#[cfg(feature = "try-runtime")]
fn pre_upgrade(&self) -> Result<Vec<u8>, sp_runtime::DispatchError> {
pallet_parachain_staking::migrations::MigrateParachainBondConfig::<Runtime>::pre_upgrade()
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(&self, state: Vec<u8>) -> Result<(), sp_runtime::DispatchError> {
pallet_parachain_staking::migrations::MigrateParachainBondConfig::<Runtime>::post_upgrade(
state,
)
}
}

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

impl<Runtime> GetMigrations for CommonMigrations<Runtime>
Expand Down Expand Up @@ -382,10 +198,9 @@ where
// completed in runtime 3200
// Box::new(MigrateXcmFeesAssetsMeatdata::<Runtime>(Default::default())),
// complete in runtime 3300
Box::new(MigrateCodeToStateTrieV1::<Runtime>(Default::default())),
Box::new(MigrateStakingParachainBondConfig::<Runtime>(
Default::default(),
)),
// Box::new(MigrateCodeToStateTrieV1::<Runtime>(Default::default())),
// Box::new(MigrateStakingParachainBondConfig::<Runtime>(Default::default())),

// permanent migrations
Box::new(MigrateToLatestXcmVersion::<Runtime>(Default::default())),
]
Expand Down
Loading