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_core::{Get, H160},
30
    sp_std::{marker::PhantomData, prelude::*},
31
};
32

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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