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
    alloc::{boxed::Box, vec, vec::Vec},
24
    core::marker::PhantomData,
25
    frame_support::{
26
        pallet_prelude::GetStorageVersion,
27
        traits::{OnRuntimeUpgrade, PalletInfoAccess},
28
        weights::Weight,
29
    },
30
    pallet_migrations::{GetMigrations, Migration},
31
    sp_core::{Get, H160},
32
};
33

            
34
pub struct MigratePrecompileXcmDummyCode<T>(pub PhantomData<T>);
35
impl<T> Migration for MigratePrecompileXcmDummyCode<T>
36
where
37
    T: pallet_evm::Config,
38
    T: frame_system::Config,
39
{
40
    fn friendly_name(&self) -> &str {
41
        "TM_MigratePrecompileXcmCode"
42
    }
43

            
44
    fn migrate(&self, _available_weight: Weight) -> Weight {
45
        log::info!("Performing migration: TM_MigratePrecompileXcmCode");
46
        let revert_bytecode = vec![0x60, 0x00, 0x60, 0x00, 0xFD];
47

            
48
        let db_weights = T::DbWeight::get();
49

            
50
        // Pallet-xcm precompile address
51
        let address = H160::from_low_u64_be(2052);
52
        let _ = pallet_evm::Pallet::<T>::create_account(address, revert_bytecode.clone(), None);
53

            
54
        // reads: <Suicided<T>> and <AccountCodes<T>>
55
        // writes: <AccountCodesMetadata<T>> and <AccountCodes<T>>
56
        db_weights.reads_writes(2, 2)
57
    }
58

            
59
    /// Run a standard pre-runtime test. This works the same way as in a normal runtime upgrade.
60
    #[cfg(feature = "try-runtime")]
61
    fn pre_upgrade(&self) -> Result<Vec<u8>, sp_runtime::DispatchError> {
62
        log::info!("Performing TM_MigratePrecompileXcmCode - pre_upgrade");
63

            
64
        let address = H160::from_low_u64_be(2052);
65
        assert!(pallet_evm::AccountCodes::<T>::get(address).is_empty());
66
        Ok(vec![])
67
    }
68

            
69
    /// Run a standard post-runtime test. This works the same way as in a normal runtime upgrade.
70
    #[cfg(feature = "try-runtime")]
71
    fn post_upgrade(&self, _: Vec<u8>) -> Result<(), sp_runtime::DispatchError> {
72
        log::info!("Performing TM_MigratePrecompileXcmCode - post_upgrade");
73

            
74
        let revert_bytecode = vec![0x60, 0x00, 0x60, 0x00, 0xFD];
75
        let address = H160::from_low_u64_be(2052);
76
        assert_eq!(pallet_evm::AccountCodes::<T>::get(address), revert_bytecode);
77
        Ok(())
78
    }
79
}
80

            
81
pub struct MigratePrecompileProxyDummyCode<T>(pub PhantomData<T>);
82
impl<T> Migration for MigratePrecompileProxyDummyCode<T>
83
where
84
    T: pallet_evm::Config,
85
    T: frame_system::Config,
86
{
87
    fn friendly_name(&self) -> &str {
88
        "TM_MigratePrecompileProxyCode"
89
    }
90

            
91
    fn migrate(&self, _available_weight: Weight) -> Weight {
92
        log::info!("Performing migration: TM_MigratePrecompileProxyCode");
93
        let revert_bytecode = vec![0x60, 0x00, 0x60, 0x00, 0xFD];
94

            
95
        let db_weights = T::DbWeight::get();
96

            
97
        // Pallet-xcm precompile address
98
        let address = H160::from_low_u64_be(2053);
99
        let _ = pallet_evm::Pallet::<T>::create_account(address, revert_bytecode.clone(), None);
100

            
101
        // reads: <Suicided<T>> and <AccountCodes<T>>
102
        // writes: <AccountCodesMetadata<T>> and <AccountCodes<T>>
103
        db_weights.reads_writes(2, 2)
104
    }
105

            
106
    /// Run a standard pre-runtime test. This works the same way as in a normal runtime upgrade.
107
    #[cfg(feature = "try-runtime")]
108
    fn pre_upgrade(&self) -> Result<Vec<u8>, sp_runtime::DispatchError> {
109
        log::info!("Performing TM_MigratePrecompileProxyCode - pre_upgrade");
110

            
111
        let address = H160::from_low_u64_be(2053);
112
        assert!(pallet_evm::AccountCodes::<T>::get(address).is_empty());
113
        Ok(vec![])
114
    }
115

            
116
    /// Run a standard post-runtime test. This works the same way as in a normal runtime upgrade.
117
    #[cfg(feature = "try-runtime")]
118
    fn post_upgrade(&self, _: Vec<u8>) -> Result<(), sp_runtime::DispatchError> {
119
        log::info!("Performing TM_MigratePrecompileProxyCode - post_upgrade");
120

            
121
        let revert_bytecode = vec![0x60, 0x00, 0x60, 0x00, 0xFD];
122
        let address = H160::from_low_u64_be(2053);
123
        assert_eq!(pallet_evm::AccountCodes::<T>::get(address), revert_bytecode);
124
        Ok(())
125
    }
126
}
127

            
128
pub struct TemplateMigrations<Runtime, XcmpQueue, PolkadotXcm>(
129
    PhantomData<(Runtime, XcmpQueue, PolkadotXcm)>,
130
);
131

            
132
pub struct MigrateToLatestXcmVersion<Runtime>(PhantomData<Runtime>);
133
impl<Runtime> Migration for MigrateToLatestXcmVersion<Runtime>
134
where
135
    pallet_xcm::migration::MigrateToLatestXcmVersion<Runtime>:
136
        frame_support::traits::OnRuntimeUpgrade,
137
{
138
    fn friendly_name(&self) -> &str {
139
        "MM_MigrateToLatestXcmVersion5"
140
    }
141

            
142
    fn migrate(&self, _available_weight: Weight) -> Weight {
143
        pallet_xcm::migration::MigrateToLatestXcmVersion::<Runtime>::on_runtime_upgrade()
144
    }
145

            
146
    #[cfg(feature = "try-runtime")]
147
    fn pre_upgrade(&self) -> Result<Vec<u8>, sp_runtime::DispatchError> {
148
        pallet_xcm::migration::MigrateToLatestXcmVersion::<Runtime>::pre_upgrade()
149
    }
150

            
151
    #[cfg(feature = "try-runtime")]
152
    fn post_upgrade(&self, state: Vec<u8>) -> Result<(), sp_runtime::DispatchError> {
153
        pallet_xcm::migration::MigrateToLatestXcmVersion::<Runtime>::post_upgrade(state)
154
    }
155
}
156

            
157
impl<Runtime, XcmpQueue, PolkadotXcm> GetMigrations
158
    for TemplateMigrations<Runtime, XcmpQueue, PolkadotXcm>
159
where
160
    PolkadotXcm: GetStorageVersion + PalletInfoAccess + 'static,
161
    XcmpQueue: GetStorageVersion + PalletInfoAccess + 'static,
162
    Runtime: pallet_evm::Config,
163
    Runtime: frame_system::Config,
164
    Runtime: cumulus_pallet_xcmp_queue::Config,
165
    Runtime: pallet_xcm_executor_utils::Config,
166
    Runtime: pallet_xcm::Config,
167
{
168
1365
    fn get_migrations() -> Vec<Box<dyn Migration>> {
169
        // let migrate_precompiles = MigratePrecompileDummyCode::<Runtime>(Default::default());
170
        //let migrate_polkadot_xcm_v1 =
171
        //    PolkadotXcmMigrationFixVersion::<Runtime, PolkadotXcm>(Default::default());
172
        //let migrate_xcmp_queue_v2 =
173
        //    XcmpQueueMigrationFixVersion::<Runtime, XcmpQueue>(Default::default());
174
        //let migrate_xcmp_queue_v3 = XcmpQueueMigrationV3::<Runtime>(Default::default());
175
        //let migrate_xcmp_queue_v4 = XcmpQueueMigrationV4::<Runtime>(Default::default());
176
        //let migrate_xcm_executor_utils_v4 =
177
        //    pallet_xcm_executor_utils::migrations::MigrateToV1::<Runtime>(Default::default());
178
        // let migrate_pallet_xcm_v4 = MigrateToLatestXcmVersion::<Runtime>(Default::default());
179
        //let migrate_precompile_proxy_code =
180
        //    MigratePrecompileProxyDummyCode::<Runtime>(Default::default());
181
        //let migrate_precompile_xcm_code =
182
        //    MigratePrecompileXcmDummyCode::<Runtime>(Default::default());
183

            
184
        //let migrate_pallet_xcm_v5 = MigrateToLatestXcmVersion::<Runtime>(Default::default());
185
1365
        vec![
186
            // Applied in runtime 400
187
            // Box::new(migrate_precompiles),
188
            // Box::new(migrate_polkadot_xcm_v1),
189
            // Box::new(migrate_xcmp_queue_v2),
190
            // Box::new(migrate_xcmp_queue_v3),
191
            // Box::new(migrate_xcmp_queue_v4),
192
            // Box::new(migrate_xcm_executor_utils_v4),
193
            // Box::new(migrate_pallet_xcm_v4),
194
            // Box::new(migrate_precompile_proxy_code),
195
            // Box::new(migrate_precompile_xcm_code),
196
            // Applied in runtime 1200
197
            //Box::new(migrate_pallet_xcm_v5),
198
        ]
199
1365
    }
200
}