Skip to content

Commit

Permalink
fix: migration
Browse files Browse the repository at this point in the history
  • Loading branch information
wischli committed Aug 14, 2024
1 parent 25ebf61 commit 8ec41e9
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 31 deletions.
2 changes: 1 addition & 1 deletion pallets/liquidity-pools-gateway/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ mod tests;
pub mod pallet {
use super::*;

const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);
const STORAGE_VERSION: StorageVersion = StorageVersion::new(2);

#[pallet::pallet]
#[pallet::storage_version(STORAGE_VERSION)]
Expand Down
14 changes: 11 additions & 3 deletions runtime/altair/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,21 @@ use crate::Runtime;
/// The migration set for Altair @ Kusama.
/// It includes all the migrations that have to be applied on that chain.
pub type UpgradeAltair1402 = (
// Remove deprecated DomainRouters entries
runtime_common::migrations::liquidity_pools_gateway::clear_deprecated_domain_router_entries::Migration<Runtime>,
// Clear OutboundMessageNonceStore
frame_support::migrations::VersionedMigration<
0,
1,
runtime_common::migrations::liquidity_pools_gateway::clear_outbound_nonce::Migration<Runtime>,
runtime_common::migrations::liquidity_pools_gateway::clear_outbound_nonce::Migration<
Runtime,
>,
pallet_liquidity_pools_gateway::Pallet<Runtime>,
<Runtime as frame_system::Config>::DbWeight,
>,
// Remove deprecated DomainRouters entries
frame_support::migrations::VersionedMigration<
1,
2,
runtime_common::migrations::liquidity_pools_gateway::clear_deprecated_domain_router_entries::Migration<Runtime>,
pallet_liquidity_pools_gateway::Pallet<Runtime>,
<Runtime as frame_system::Config>::DbWeight,
>,
Expand Down
14 changes: 11 additions & 3 deletions runtime/centrifuge/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,21 @@ use crate::Runtime;
/// The migration set for Centrifuge @ Polkadot.
/// It includes all the migrations that have to be applied on that chain.
pub type UpgradeCentrifuge1402 = (
// Remove deprecated DomainRouters entries
runtime_common::migrations::liquidity_pools_gateway::clear_deprecated_domain_router_entries::Migration<Runtime>,
// Clear OutboundMessageNonceStore
frame_support::migrations::VersionedMigration<
0,
1,
runtime_common::migrations::liquidity_pools_gateway::clear_outbound_nonce::Migration<Runtime>,
runtime_common::migrations::liquidity_pools_gateway::clear_outbound_nonce::Migration<
Runtime,
>,
pallet_liquidity_pools_gateway::Pallet<Runtime>,
<Runtime as frame_system::Config>::DbWeight,
>,
// Remove deprecated DomainRouters entries
frame_support::migrations::VersionedMigration<
1,
2,
runtime_common::migrations::liquidity_pools_gateway::clear_deprecated_domain_router_entries::Migration<Runtime>,
pallet_liquidity_pools_gateway::Pallet<Runtime>,
<Runtime as frame_system::Config>::DbWeight,
>,
Expand Down
62 changes: 40 additions & 22 deletions runtime/common/src/migrations/liquidity_pools_gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
// GNU General Public License for more details.

use cfg_types::domain_address::Domain;
#[cfg(feature = "try-runtime")]
use frame_support::pallet_prelude::{Decode, Encode};
use frame_support::{
pallet_prelude::{Decode, Encode, ValueQuery},
pallet_prelude::ValueQuery,
storage_alias,
traits::{Get, OnRuntimeUpgrade},
weights::Weight,
Expand Down Expand Up @@ -157,34 +159,50 @@ pub mod clear_deprecated_domain_router_entries {
fn on_runtime_upgrade() -> Weight {
let items = v0::DomainRouters::<T>::iter_keys().count().saturated_into();

pallet_liquidity_pools_gateway::DomainRouters::<T>::translate_values::<
v0::DomainRouter<T>,
_,
>(|old| match old {
v0::DomainRouter::AxelarEVM(router) => Some(
liquidity_pools_gateway_routers::DomainRouter::<T>::AxelarEVM(router).into(),
),
// Remove other entries
router => {
log::info!("{LOG_PREFIX}: Removing entry {router:?}!");
None
}
});

log::info!("{LOG_PREFIX}: Migration done with {items:?} items in total!");
pallet_liquidity_pools_gateway::DomainRouters::<T>::translate::<v0::DomainRouter<T>, _>(
|key, old| {
log::debug!("{LOG_PREFIX}: Inspecting key {key:?} with value\n{old:?}");
match old {
v0::DomainRouter::AxelarEVM(router) => Some(
liquidity_pools_gateway_routers::DomainRouter::<T>::AxelarEVM(router)
.into(),
),
// Remove other entries
router => {
log::info!("{LOG_PREFIX} : Removing entry {router:?}!");
None
}
}
},
);

log::info!(
"{LOG_PREFIX} ON_RUNTIME_UPGRADE: Migration done with {items:?} items in total!"
);

T::DbWeight::get().reads_writes(items, items)
}

#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, sp_runtime::TryRuntimeError> {
let count_total = v0::DomainRouters::<T>::iter_keys().count();
let count_axelar_evm_router: u64 = v0::DomainRouters::<T>::iter_values()
.filter(|v| matches!(v, v0::DomainRouter::AxelarEVM(_)))
let count_axelar_evm_router: u64 = v0::DomainRouters::<T>::iter()
.filter(|(domain, router)| {
log::debug!(
"{LOG_PREFIX} PRE: Inspecting key {domain:?} with value\n{router:?}"
);
matches!(router, v0::DomainRouter::AxelarEVM(_))
})
.count()
.saturated_into();
log::info!("{LOG_PREFIX}: Found {count_axelar_evm_router:?} values of total {count_total:?} to migrate!");
log::info!("{LOG_PREFIX}: Pre checks done!");
log::info!(
"{LOG_PREFIX} PRE: Found {count_axelar_evm_router:?} values of
total {count_total:?} to migrate!"
);
log::info!(
"{LOG_PREFIX} PRE: Checks
done!"
);
Ok(count_axelar_evm_router.encode())
}

Expand All @@ -197,10 +215,10 @@ pub mod clear_deprecated_domain_router_entries {
.saturated_into();
assert_eq!(
pre_count, post_count,
"{LOG_PREFIX}: Mismatching number of domain routers after migration!"
"{LOG_PREFIX} POST: Mismatching number of domain routers after migration!"
);

log::info!("{LOG_PREFIX}: Post checks done!");
log::info!("{LOG_PREFIX} POST: Checks done!");

Ok(())
}
Expand Down
5 changes: 3 additions & 2 deletions runtime/development/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

use frame_support::migrations::VersionedMigration;

use crate::Runtime;

pub type UpgradeDevelopment1402 =
(runtime_common::migrations::liquidity_pools_gateway::clear_deprecated_domain_router_entries::Migration<Runtime>,);
pub type UpgradeDevelopment1402 = VersionedMigration<1, 2, runtime_common::migrations::liquidity_pools_gateway::clear_deprecated_domain_router_entries::Migration<Runtime>, pallet_liquidity_pools_gateway::Pallet<Runtime>, <Runtime as frame_system::Config>::DbWeight>;

0 comments on commit 8ec41e9

Please sign in to comment.