|
| 1 | +use alloc::string::String; |
| 2 | +use codec::{Decode, Encode}; |
| 3 | +use frame_support::traits::Get; |
| 4 | +use frame_support::weights::Weight; |
| 5 | +use sp_io::hashing::twox_128; |
| 6 | +use sp_io::storage; |
| 7 | +use sp_std::{collections::btree_set::BTreeSet, vec::Vec}; |
| 8 | +use subtensor_runtime_common::NetUid; |
| 9 | + |
| 10 | +use crate::{ |
| 11 | + ChildKeys, Config, Delegates, HasMigrationRun, LastRateLimitedBlock, ParentKeys, |
| 12 | + PendingChildKeys, RateLimitKey, |
| 13 | +}; |
| 14 | + |
| 15 | +const MIGRATION_NAME: &[u8] = b"migrate_rate_limit_keys"; |
| 16 | + |
| 17 | +#[allow(dead_code)] |
| 18 | +#[derive(Decode)] |
| 19 | +enum RateLimitKeyV0<AccountId> { |
| 20 | + SetSNOwnerHotkey(NetUid), |
| 21 | + NetworkLastRegistered, |
| 22 | + LastTxBlock(AccountId), |
| 23 | + LastTxBlockChildKeyTake(AccountId), |
| 24 | + LastTxBlockDelegateTake(AccountId), |
| 25 | +} |
| 26 | + |
| 27 | +pub fn migrate_rate_limit_keys<T: Config>() -> Weight |
| 28 | +where |
| 29 | + T::AccountId: Ord + Clone, |
| 30 | +{ |
| 31 | + let mut weight = T::DbWeight::get().reads(1); |
| 32 | + |
| 33 | + if HasMigrationRun::<T>::get(MIGRATION_NAME) { |
| 34 | + log::info!( |
| 35 | + "Migration '{}' already executed - skipping", |
| 36 | + String::from_utf8_lossy(MIGRATION_NAME) |
| 37 | + ); |
| 38 | + return weight; |
| 39 | + } |
| 40 | + |
| 41 | + log::info!( |
| 42 | + "Running migration '{}'", |
| 43 | + String::from_utf8_lossy(MIGRATION_NAME) |
| 44 | + ); |
| 45 | + |
| 46 | + let (child_accounts, child_weight) = collect_child_related_accounts::<T>(); |
| 47 | + let (delegate_accounts, delegate_weight) = collect_delegate_accounts::<T>(); |
| 48 | + weight = weight.saturating_add(child_weight); |
| 49 | + weight = weight.saturating_add(delegate_weight); |
| 50 | + |
| 51 | + let prefix = storage_prefix("SubtensorModule", "LastRateLimitedBlock"); |
| 52 | + let mut cursor = prefix.clone(); |
| 53 | + let mut entries = Vec::new(); |
| 54 | + |
| 55 | + while let Some(next_key) = storage::next_key(&cursor) { |
| 56 | + if !next_key.starts_with(&prefix) { |
| 57 | + break; |
| 58 | + } |
| 59 | + if let Some(value) = storage::get(&next_key) { |
| 60 | + entries.push((next_key.clone(), value)); |
| 61 | + } |
| 62 | + cursor = next_key; |
| 63 | + } |
| 64 | + |
| 65 | + weight = weight.saturating_add(T::DbWeight::get().reads(entries.len() as u64)); |
| 66 | + |
| 67 | + let mut migrated_network = 0u64; |
| 68 | + let mut migrated_last_tx = 0u64; |
| 69 | + let mut migrated_child_take = 0u64; |
| 70 | + let mut migrated_delegate_take = 0u64; |
| 71 | + |
| 72 | + for (old_storage_key, value_bytes) in entries { |
| 73 | + if value_bytes.is_empty() { |
| 74 | + continue; |
| 75 | + } |
| 76 | + |
| 77 | + let Some(encoded_key) = old_storage_key.get(prefix.len()..) else { |
| 78 | + continue; |
| 79 | + }; |
| 80 | + if encoded_key.is_empty() { |
| 81 | + continue; |
| 82 | + } |
| 83 | + |
| 84 | + let Some(decoded_legacy) = decode_legacy::<T>(encoded_key) else { |
| 85 | + // Unknown entry – skip to avoid clobbering valid data. |
| 86 | + continue; |
| 87 | + }; |
| 88 | + |
| 89 | + let legacy_value = match decode_value(&value_bytes) { |
| 90 | + Some(v) => v, |
| 91 | + None => continue, |
| 92 | + }; |
| 93 | + |
| 94 | + let Some(modern_key) = |
| 95 | + legacy_to_modern(decoded_legacy, &child_accounts, &delegate_accounts) |
| 96 | + else { |
| 97 | + continue; |
| 98 | + }; |
| 99 | + let new_storage_key = LastRateLimitedBlock::<T>::hashed_key_for(&modern_key); |
| 100 | + weight = weight.saturating_add(T::DbWeight::get().reads(1)); |
| 101 | + |
| 102 | + let merged_value = storage::get(&new_storage_key) |
| 103 | + .and_then(|data| decode_value(&data)) |
| 104 | + .map_or(legacy_value, |current| { |
| 105 | + core::cmp::max(current, legacy_value) |
| 106 | + }); |
| 107 | + |
| 108 | + storage::set(&new_storage_key, &merged_value.encode()); |
| 109 | + if new_storage_key != old_storage_key { |
| 110 | + storage::clear(&old_storage_key); |
| 111 | + weight = weight.saturating_add(T::DbWeight::get().writes(1)); |
| 112 | + } |
| 113 | + |
| 114 | + weight = weight.saturating_add(T::DbWeight::get().writes(1)); |
| 115 | + match &modern_key { |
| 116 | + RateLimitKey::NetworkLastRegistered => { |
| 117 | + migrated_network = migrated_network.saturating_add(1); |
| 118 | + } |
| 119 | + RateLimitKey::LastTxBlock(_) => { |
| 120 | + migrated_last_tx = migrated_last_tx.saturating_add(1); |
| 121 | + } |
| 122 | + RateLimitKey::LastTxBlockChildKeyTake(_) => { |
| 123 | + migrated_child_take = migrated_child_take.saturating_add(1); |
| 124 | + } |
| 125 | + RateLimitKey::LastTxBlockDelegateTake(_) => { |
| 126 | + migrated_delegate_take = migrated_delegate_take.saturating_add(1); |
| 127 | + } |
| 128 | + _ => {} |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + HasMigrationRun::<T>::insert(MIGRATION_NAME, true); |
| 133 | + weight = weight.saturating_add(T::DbWeight::get().writes(1)); |
| 134 | + |
| 135 | + log::info!( |
| 136 | + "Migration '{}' completed. network={}, last_tx={}, child_take={}, delegate_take={}", |
| 137 | + String::from_utf8_lossy(MIGRATION_NAME), |
| 138 | + migrated_network, |
| 139 | + migrated_last_tx, |
| 140 | + migrated_child_take, |
| 141 | + migrated_delegate_take |
| 142 | + ); |
| 143 | + |
| 144 | + weight |
| 145 | +} |
| 146 | + |
| 147 | +fn storage_prefix(pallet: &str, storage: &str) -> Vec<u8> { |
| 148 | + let pallet_hash = twox_128(pallet.as_bytes()); |
| 149 | + let storage_hash = twox_128(storage.as_bytes()); |
| 150 | + [pallet_hash, storage_hash].concat() |
| 151 | +} |
| 152 | + |
| 153 | +fn decode_legacy<T: Config>(bytes: &[u8]) -> Option<RateLimitKeyV0<T::AccountId>> { |
| 154 | + let mut slice = bytes; |
| 155 | + let decoded = RateLimitKeyV0::<T::AccountId>::decode(&mut slice).ok()?; |
| 156 | + if slice.is_empty() { |
| 157 | + Some(decoded) |
| 158 | + } else { |
| 159 | + None |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +fn decode_value(bytes: &[u8]) -> Option<u64> { |
| 164 | + let mut slice = bytes; |
| 165 | + u64::decode(&mut slice).ok() |
| 166 | +} |
| 167 | + |
| 168 | +fn legacy_to_modern<AccountId: Ord + Clone>( |
| 169 | + legacy: RateLimitKeyV0<AccountId>, |
| 170 | + child_accounts: &BTreeSet<AccountId>, |
| 171 | + delegate_accounts: &BTreeSet<AccountId>, |
| 172 | +) -> Option<RateLimitKey<AccountId>> { |
| 173 | + match legacy { |
| 174 | + RateLimitKeyV0::SetSNOwnerHotkey(_) => None, |
| 175 | + RateLimitKeyV0::NetworkLastRegistered => Some(RateLimitKey::NetworkLastRegistered), |
| 176 | + RateLimitKeyV0::LastTxBlock(account) => Some(RateLimitKey::LastTxBlock(account)), |
| 177 | + RateLimitKeyV0::LastTxBlockChildKeyTake(account) => { |
| 178 | + if child_accounts.contains(&account) { |
| 179 | + Some(RateLimitKey::LastTxBlockChildKeyTake(account)) |
| 180 | + } else { |
| 181 | + None |
| 182 | + } |
| 183 | + } |
| 184 | + RateLimitKeyV0::LastTxBlockDelegateTake(account) => { |
| 185 | + if delegate_accounts.contains(&account) { |
| 186 | + Some(RateLimitKey::LastTxBlockDelegateTake(account)) |
| 187 | + } else { |
| 188 | + None |
| 189 | + } |
| 190 | + } |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +fn collect_child_related_accounts<T: Config>() -> (BTreeSet<T::AccountId>, Weight) |
| 195 | +where |
| 196 | + T::AccountId: Ord + Clone, |
| 197 | +{ |
| 198 | + let mut accounts = BTreeSet::new(); |
| 199 | + let mut reads = 0u64; |
| 200 | + |
| 201 | + for (parent, _, children) in ChildKeys::<T>::iter() { |
| 202 | + accounts.insert(parent.clone()); |
| 203 | + for (_, child) in children { |
| 204 | + accounts.insert(child.clone()); |
| 205 | + } |
| 206 | + reads = reads.saturating_add(1); |
| 207 | + } |
| 208 | + |
| 209 | + for (_, parent, (children, _)) in PendingChildKeys::<T>::iter() { |
| 210 | + accounts.insert(parent.clone()); |
| 211 | + for (_, child) in children { |
| 212 | + accounts.insert(child.clone()); |
| 213 | + } |
| 214 | + reads = reads.saturating_add(1); |
| 215 | + } |
| 216 | + |
| 217 | + for (child, _, parents) in ParentKeys::<T>::iter() { |
| 218 | + accounts.insert(child.clone()); |
| 219 | + for (_, parent) in parents { |
| 220 | + accounts.insert(parent.clone()); |
| 221 | + } |
| 222 | + reads = reads.saturating_add(1); |
| 223 | + } |
| 224 | + |
| 225 | + (accounts, T::DbWeight::get().reads(reads)) |
| 226 | +} |
| 227 | + |
| 228 | +fn collect_delegate_accounts<T: Config>() -> (BTreeSet<T::AccountId>, Weight) |
| 229 | +where |
| 230 | + T::AccountId: Ord + Clone, |
| 231 | +{ |
| 232 | + let mut accounts = BTreeSet::new(); |
| 233 | + let mut reads = 0u64; |
| 234 | + |
| 235 | + for (account, _) in Delegates::<T>::iter() { |
| 236 | + accounts.insert(account.clone()); |
| 237 | + reads = reads.saturating_add(1); |
| 238 | + } |
| 239 | + |
| 240 | + (accounts, T::DbWeight::get().reads(reads)) |
| 241 | +} |
0 commit comments