|
| 1 | +use crate::Vec; |
| 2 | +use crate::{Config, HasMigrationRun, Pallet}; |
| 3 | +use alloc::string::String; |
| 4 | +use codec::Decode; |
| 5 | +use frame_support::traits::Get; |
| 6 | +use frame_support::weights::Weight; |
| 7 | +use sp_io::hashing::twox_128; |
| 8 | +use sp_io::storage::{clear, get}; |
| 9 | + |
| 10 | +pub fn migrate_obsolete_rate_limiting_last_blocks_storage<T: Config>() -> Weight { |
| 11 | + migrate_network_last_registered::<T>() |
| 12 | +} |
| 13 | + |
| 14 | +pub fn migrate_network_last_registered<T: Config>() -> Weight { |
| 15 | + let migration_name = b"migrate_network_last_registered".to_vec(); |
| 16 | + let pallet_name = "SubtensorModule"; |
| 17 | + let storage_name = "NetworkLastRegistered"; |
| 18 | + |
| 19 | + migrate_value::<T, _>(migration_name, pallet_name, storage_name, |limit| { |
| 20 | + Pallet::<T>::set_network_last_lock_block(limit); |
| 21 | + }) |
| 22 | +} |
| 23 | +fn migrate_value<T, SetValueFunction>( |
| 24 | + migration_name: Vec<u8>, |
| 25 | + pallet_name: &str, |
| 26 | + storage_name: &str, |
| 27 | + set_value: SetValueFunction, |
| 28 | +) -> Weight |
| 29 | +where |
| 30 | + T: Config, |
| 31 | + SetValueFunction: Fn(u64 /*limit in blocks*/), |
| 32 | +{ |
| 33 | + // Initialize the weight with one read operation. |
| 34 | + let mut weight = T::DbWeight::get().reads(1); |
| 35 | + |
| 36 | + // Check if the migration has already run |
| 37 | + if HasMigrationRun::<T>::get(&migration_name) { |
| 38 | + log::info!( |
| 39 | + "Migration '{:?}' has already run. Skipping.", |
| 40 | + migration_name |
| 41 | + ); |
| 42 | + return weight; |
| 43 | + } |
| 44 | + log::info!( |
| 45 | + "Running migration '{}'", |
| 46 | + String::from_utf8_lossy(&migration_name) |
| 47 | + ); |
| 48 | + |
| 49 | + let pallet_name_hash = twox_128(pallet_name.as_bytes()); |
| 50 | + let storage_name_hash = twox_128(storage_name.as_bytes()); |
| 51 | + let full_key = [pallet_name_hash, storage_name_hash].concat(); |
| 52 | + |
| 53 | + if let Some(value_bytes) = get(&full_key) { |
| 54 | + if let Ok(rate_limit) = Decode::decode(&mut &value_bytes[..]) { |
| 55 | + set_value(rate_limit); |
| 56 | + } |
| 57 | + |
| 58 | + clear(&full_key); |
| 59 | + } |
| 60 | + |
| 61 | + weight = weight.saturating_add(T::DbWeight::get().writes(2)); |
| 62 | + weight = weight.saturating_add(T::DbWeight::get().reads(1)); |
| 63 | + |
| 64 | + // Mark the migration as completed |
| 65 | + HasMigrationRun::<T>::insert(&migration_name, true); |
| 66 | + weight = weight.saturating_add(T::DbWeight::get().writes(1)); |
| 67 | + |
| 68 | + log::info!( |
| 69 | + "Migration '{:?}' completed.", |
| 70 | + String::from_utf8_lossy(&migration_name) |
| 71 | + ); |
| 72 | + |
| 73 | + // Return the migration weight. |
| 74 | + weight |
| 75 | +} |
0 commit comments