|
13 | 13 | // |
14 | 14 | // You should have received a copy of the GNU General Public License |
15 | 15 | // along with Litentry. If not, see <https://www.gnu.org/licenses/>. |
16 | | - |
17 | | -#[cfg(feature = "try-runtime")] |
18 | | -use frame_support::ensure; |
19 | | -#[cfg(feature = "try-runtime")] |
20 | | -use frame_support::traits::StorageVersion; |
21 | | -#[cfg(feature = "try-runtime")] |
22 | | -use sp_runtime::DispatchError; |
23 | | -#[cfg(feature = "try-runtime")] |
24 | | -use sp_std::vec::Vec; |
25 | | - |
26 | | -use frame_support::StorageHasher; |
27 | | -use frame_support::{ |
28 | | - pallet_prelude::*, |
29 | | - storage::migration::{clear_storage_prefix, put_storage_value}, |
30 | | - traits::{Get, GetStorageVersion, OnRuntimeUpgrade, PalletInfoAccess}, |
31 | | - weights::Weight, |
32 | | -}; |
33 | | -use frame_system::pallet_prelude::BlockNumberFor; |
34 | | -use log; |
35 | | -use pallet_xcm::QueryStatus; |
36 | | -use sp_std::marker::PhantomData; |
37 | | -use xcm::v5::{Junctions, Location}; |
38 | | -use xcm::VersionedLocation; |
39 | | - |
40 | | -pub type Migrations<Runtime> = (IdentityUpdateStorageVersion<Runtime>, RewriteXcmQueries<Runtime>); |
41 | | - |
42 | | -const IDENTITY_LOG_TARGET: &str = "runtime::identity"; |
43 | | -const XCM_LOG_TARGET: &str = "runtime::xcm"; |
44 | | - |
45 | | -pub struct IdentityUpdateStorageVersion<T>(PhantomData<T>); |
46 | | -impl<T> OnRuntimeUpgrade for IdentityUpdateStorageVersion<T> |
47 | | -where |
48 | | - T: frame_system::Config + pallet_identity::Config, |
49 | | -{ |
50 | | - #[cfg(feature = "try-runtime")] |
51 | | - fn pre_upgrade() -> Result<Vec<u8>, DispatchError> { |
52 | | - ensure!( |
53 | | - StorageVersion::get::<pallet_identity::Pallet<T>>() == 1, |
54 | | - "Current pallet_identity StorageVersion is not 1." |
55 | | - ); |
56 | | - Ok(Vec::<u8>::new()) |
57 | | - } |
58 | | - |
59 | | - fn on_runtime_upgrade() -> frame_support::weights::Weight { |
60 | | - let on_chain_version = pallet_identity::Pallet::<T>::on_chain_storage_version(); |
61 | | - let in_code_version = pallet_identity::Pallet::<T>::in_code_storage_version(); |
62 | | - |
63 | | - if on_chain_version < in_code_version { |
64 | | - frame_support::storage::unhashed::kill(&frame_support::storage::storage_prefix( |
65 | | - pallet_identity::Pallet::<T>::name().as_bytes(), |
66 | | - "StorageVersion".as_bytes(), |
67 | | - )); |
68 | | - |
69 | | - in_code_version.put::<pallet_identity::Pallet<T>>(); |
70 | | - |
71 | | - log::info!(target: IDENTITY_LOG_TARGET, "Upgrade pallet_identity StorageVersion to version 2 successfully"); |
72 | | - T::DbWeight::get().reads_writes(1, 3) |
73 | | - } else { |
74 | | - log::info!( |
75 | | - target: IDENTITY_LOG_TARGET, |
76 | | - "No migration executed. This probably should be removed." |
77 | | - ); |
78 | | - T::DbWeight::get().reads(1) |
79 | | - } |
80 | | - } |
81 | | - |
82 | | - #[cfg(feature = "try-runtime")] |
83 | | - fn post_upgrade(_state: Vec<u8>) -> Result<(), DispatchError> { |
84 | | - ensure!( |
85 | | - StorageVersion::get::<pallet_identity::Pallet<T>>() == 2, |
86 | | - "Please upgrade pallet_identity StorageVersion to 2" |
87 | | - ); |
88 | | - Ok(()) |
89 | | - } |
90 | | -} |
91 | | - |
92 | | -pub struct RewriteXcmQueries<T>(PhantomData<T>); |
93 | | -impl<T> OnRuntimeUpgrade for RewriteXcmQueries<T> |
94 | | -where |
95 | | - T: frame_system::Config + pallet_xcm::Config, |
96 | | -{ |
97 | | - #[cfg(feature = "try-runtime")] |
98 | | - fn pre_upgrade() -> Result<Vec<u8>, DispatchError> { |
99 | | - log::info!( |
100 | | - target: "runtime::xcm", |
101 | | - "Starting pre-upgrade check for PolkadotXcm::Queries migration" |
102 | | - ); |
103 | | - |
104 | | - Ok(Vec::new()) // No state needed for this simple check |
105 | | - } |
106 | | - |
107 | | - fn on_runtime_upgrade() -> Weight { |
108 | | - let mut cursor = None; |
109 | | - loop { |
110 | | - let result = |
111 | | - clear_storage_prefix(b"PolkadotXcm", b"Queries", &[], Some(100), cursor.as_deref()); |
112 | | - if let Some(next_cursor) = result.maybe_cursor { |
113 | | - cursor = Some(next_cursor); |
114 | | - } else { |
115 | | - break; |
116 | | - } |
117 | | - } |
118 | | - |
119 | | - // There is only one known entry. But it's v2, which is already removed from stable2412. |
120 | | - // So here manually insert that entry. |
121 | | - let known_query_id: u64 = 0; |
122 | | - let known_new_value: QueryStatus<BlockNumberFor<T>> = QueryStatus::VersionNotifier { |
123 | | - origin: VersionedLocation::V5(Location { parents: 0, interior: Junctions::Here }), |
124 | | - is_active: true, |
125 | | - }; |
126 | | - |
127 | | - // Build Blake2_128Concat key |
128 | | - let encoded_key = known_query_id.encode(); |
129 | | - let key_hashed = Blake2_128Concat::hash(&encoded_key); |
130 | | - |
131 | | - put_storage_value(b"PolkadotXcm", b"Queries", &key_hashed, known_new_value); |
132 | | - |
133 | | - log::info!( |
134 | | - target: XCM_LOG_TARGET, |
135 | | - "Removed PolkadotXcm::Queries entries: {:?}, and inserted new known QueryId={:?}", |
136 | | - 1, known_query_id |
137 | | - ); |
138 | | - |
139 | | - T::DbWeight::get().reads_writes(1, 1) |
140 | | - } |
141 | | - |
142 | | - #[cfg(feature = "try-runtime")] |
143 | | - fn post_upgrade(_state: Vec<u8>) -> Result<(), DispatchError> { |
144 | | - log::info!( |
145 | | - target: "runtime::xcm", |
146 | | - "Post-upgrade: Checking PolkadotXcm::Queries migration" |
147 | | - ); |
148 | | - |
149 | | - let expected_query_id: u64 = 0; |
150 | | - if let Some(status) = pallet_xcm::Pallet::<T>::query(expected_query_id) { |
151 | | - log::info!( |
152 | | - target: "runtime::xcm", |
153 | | - "Post-upgrade: Query ID: {}, Status: {:?}", |
154 | | - expected_query_id, |
155 | | - status |
156 | | - ); |
157 | | - } else { |
158 | | - return Err(DispatchError::Other("Expected QueryId 0 not found in post-upgrade")); |
159 | | - } |
160 | | - |
161 | | - Ok(()) |
162 | | - } |
163 | | -} |
0 commit comments