1
// Copyright (C) Moondance Labs Ltd.
2
// This file is part of Tanssi.
3

            
4
// Tanssi is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8

            
9
// Tanssi is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13

            
14
// You should have received a copy of the GNU General Public License
15
// along with Tanssi.  If not, see <http://www.gnu.org/licenses/>
16

            
17
//! # Migrations
18
//!
19
//! This module acts as a registry where each migration is defined. Each migration should implement
20
//! the "Migration" trait declared in the pallet-migrations crate.
21

            
22
use {
23
    frame_support::{
24
        pallet_prelude::GetStorageVersion,
25
        traits::{OnRuntimeUpgrade, PalletInfoAccess},
26
        weights::Weight,
27
    },
28
    pallet_migrations::{GetMigrations, Migration},
29
    sp_std::{marker::PhantomData, prelude::*},
30
    tanssi_runtime_common::migrations::{
31
        ForeignAssetCreatorMigration, PolkadotXcmMigrationFixVersion, XcmpQueueMigrationFixVersion,
32
        XcmpQueueMigrationV3, XcmpQueueMigrationV4,
33
    },
34
};
35

            
36
pub struct TemplateMigrations<Runtime, XcmpQueue, PolkadotXcm>(
37
    PhantomData<(Runtime, XcmpQueue, PolkadotXcm)>,
38
);
39

            
40
pub struct MigrateToLatestXcmVersion<Runtime>(PhantomData<Runtime>);
41
impl<Runtime> Migration for MigrateToLatestXcmVersion<Runtime>
42
where
43
    pallet_xcm::migration::MigrateToLatestXcmVersion<Runtime>:
44
        frame_support::traits::OnRuntimeUpgrade,
45
{
46
89
    fn friendly_name(&self) -> &str {
47
89
        "MM_MigrateToLatestXcmVersion"
48
89
    }
49

            
50
    fn migrate(&self, _available_weight: Weight) -> Weight {
51
        pallet_xcm::migration::MigrateToLatestXcmVersion::<Runtime>::on_runtime_upgrade()
52
    }
53

            
54
    #[cfg(feature = "try-runtime")]
55
    fn pre_upgrade(&self) -> Result<Vec<u8>, sp_runtime::DispatchError> {
56
        pallet_xcm::migration::MigrateToLatestXcmVersion::<Runtime>::pre_upgrade()
57
    }
58

            
59
    #[cfg(feature = "try-runtime")]
60
    fn post_upgrade(&self, state: Vec<u8>) -> Result<(), sp_runtime::DispatchError> {
61
        pallet_xcm::migration::MigrateToLatestXcmVersion::<Runtime>::post_upgrade(state)
62
    }
63
}
64

            
65
impl<Runtime, XcmpQueue, PolkadotXcm> GetMigrations
66
    for TemplateMigrations<Runtime, XcmpQueue, PolkadotXcm>
67
where
68
    PolkadotXcm: GetStorageVersion + PalletInfoAccess + 'static,
69
    XcmpQueue: GetStorageVersion + PalletInfoAccess + 'static,
70
    Runtime: frame_system::Config,
71
    Runtime: cumulus_pallet_xcmp_queue::Config,
72
    Runtime: pallet_xcm_executor_utils::Config,
73
    Runtime: pallet_xcm::Config,
74
    Runtime: pallet_foreign_asset_creator::Config,
75
    <Runtime as pallet_foreign_asset_creator::Config>::ForeignAsset:
76
        TryFrom<staging_xcm::v3::MultiLocation>,
77
{
78
89
    fn get_migrations() -> Vec<Box<dyn Migration>> {
79
89
        let migrate_polkadot_xcm_v1 =
80
89
            PolkadotXcmMigrationFixVersion::<Runtime, PolkadotXcm>(Default::default());
81
89
        let migrate_xcmp_queue_v2 =
82
89
            XcmpQueueMigrationFixVersion::<Runtime, XcmpQueue>(Default::default());
83
89
        let migrate_xcmp_queue_v3 = XcmpQueueMigrationV3::<Runtime>(Default::default());
84
89
        let migrate_xcmp_queue_v4 = XcmpQueueMigrationV4::<Runtime>(Default::default());
85
89
        let migrate_xcm_executor_utils_v4 =
86
89
            pallet_xcm_executor_utils::migrations::MigrateToV1::<Runtime>(Default::default());
87
89
        let migrate_pallet_xcm_v4 = MigrateToLatestXcmVersion::<Runtime>(Default::default());
88
89
        let foreign_asset_creator_migration =
89
89
            ForeignAssetCreatorMigration::<Runtime>(Default::default());
90
89
        vec![
91
89
            Box::new(migrate_polkadot_xcm_v1),
92
89
            Box::new(migrate_xcmp_queue_v2),
93
89
            Box::new(migrate_xcmp_queue_v3),
94
89
            Box::new(migrate_xcmp_queue_v4),
95
89
            Box::new(migrate_xcm_executor_utils_v4),
96
89
            Box::new(migrate_pallet_xcm_v4),
97
89
            Box::new(foreign_asset_creator_migration),
98
89
        ]
99
89
    }
100
}