|
| 1 | +use crate::alloc::borrow::ToOwned; |
| 2 | +use codec::Decode; |
| 3 | +use scale_info::prelude::{string::String, vec::Vec}; |
| 4 | +use serde::Deserialize; |
| 5 | +use sp_core::{crypto::Ss58Codec, ConstU32}; |
| 6 | +use sp_runtime::{AccountId32, BoundedVec}; |
| 7 | + |
| 8 | +use super::*; |
| 9 | +use frame_support::{traits::Get, weights::Weight}; |
| 10 | +use log; |
| 11 | + |
| 12 | +#[derive(Deserialize, Debug)] |
| 13 | +struct RegistrationRecordJSON { |
| 14 | + address: String, |
| 15 | + name: String, |
| 16 | + url: String, |
| 17 | + description: String, |
| 18 | +} |
| 19 | + |
| 20 | +fn string_to_bounded_vec(input: &str) -> Result<BoundedVec<u8, ConstU32<1024>>, &'static str> { |
| 21 | + let vec_u8: Vec<u8> = input.to_owned().into_bytes(); |
| 22 | + |
| 23 | + // Check if the length is within bounds |
| 24 | + if vec_u8.len() > 64 { |
| 25 | + return Err("Input string is too long"); |
| 26 | + } |
| 27 | + |
| 28 | + // Convert to BoundedVec |
| 29 | + BoundedVec::<u8, ConstU32<1024>>::try_from(vec_u8) |
| 30 | + .map_err(|_| "Failed to convert to BoundedVec") |
| 31 | +} |
| 32 | + |
| 33 | +pub fn migrate_set_hotkey_identities<T: Config>() -> Weight { |
| 34 | + let migration_name = b"fix_total_coldkey_stake_v7".to_vec(); |
| 35 | + |
| 36 | + // Initialize the weight with one read operation. |
| 37 | + let mut weight = T::DbWeight::get().reads(1); |
| 38 | + |
| 39 | + // Check if the migration has already run |
| 40 | + if HasMigrationRun::<T>::get(&migration_name) { |
| 41 | + log::info!( |
| 42 | + "Migration '{:?}' has already run. Skipping.", |
| 43 | + migration_name |
| 44 | + ); |
| 45 | + return weight; |
| 46 | + } |
| 47 | + log::info!( |
| 48 | + "Running migration '{}'", |
| 49 | + String::from_utf8_lossy(&migration_name) |
| 50 | + ); |
| 51 | + |
| 52 | + // Include the JSON file with delegate info |
| 53 | + let data = include_str!("../../../../docs/delegate-info.json"); |
| 54 | + |
| 55 | + // Iterate over all the delegate records |
| 56 | + if let Ok(delegates) = serde_json::from_str::<Vec<RegistrationRecordJSON>>(data) { |
| 57 | + // Iterate through the delegates |
| 58 | + for delegate in delegates.iter() { |
| 59 | + // Convert fields to bounded vecs |
| 60 | + let name_result = string_to_bounded_vec(&delegate.name); |
| 61 | + let desc_result = string_to_bounded_vec(&delegate.description); |
| 62 | + let url_result = string_to_bounded_vec(&delegate.url); |
| 63 | + let hotkey: AccountId32 = match AccountId32::from_ss58check(&delegate.address) { |
| 64 | + Ok(account) => account, |
| 65 | + Err(_) => { |
| 66 | + log::warn!( |
| 67 | + "Invalid SS58 address: {:?}. Skipping this delegate.", |
| 68 | + delegate.address |
| 69 | + ); |
| 70 | + continue; |
| 71 | + } |
| 72 | + }; |
| 73 | + let decoded_hotkey: T::AccountId = match T::AccountId::decode(&mut hotkey.as_ref()) { |
| 74 | + Ok(decoded) => decoded, |
| 75 | + Err(e) => { |
| 76 | + log::warn!("Failed to decode hotkey: {:?}. Skipping this delegate.", e); |
| 77 | + continue; |
| 78 | + } |
| 79 | + }; |
| 80 | + log::info!("Hotkey unwrapped: {:?}", decoded_hotkey); |
| 81 | + |
| 82 | + // If we should continue with real values. |
| 83 | + let mut name: BoundedVec<u8, ConstU32<1024>> = BoundedVec::default(); |
| 84 | + let mut description: BoundedVec<u8, ConstU32<1024>> = BoundedVec::default(); |
| 85 | + let mut url: BoundedVec<u8, ConstU32<1024>> = BoundedVec::default(); |
| 86 | + if let Ok(n) = name_result { |
| 87 | + name = n; |
| 88 | + } |
| 89 | + if let Ok(d) = desc_result { |
| 90 | + description = d; |
| 91 | + } |
| 92 | + if let Ok(u) = url_result { |
| 93 | + url = u; |
| 94 | + } |
| 95 | + |
| 96 | + // Unwrap the real values. |
| 97 | + let image: BoundedVec<u8, ConstU32<1024>> = BoundedVec::default(); |
| 98 | + let discord: BoundedVec<u8, ConstU32<1024>> = BoundedVec::default(); |
| 99 | + let additional: BoundedVec<u8, ConstU32<1024>> = BoundedVec::default(); |
| 100 | + |
| 101 | + // Create the chain identity. |
| 102 | + let identity = ChainIdentityOf { |
| 103 | + name: name.into(), |
| 104 | + url: url.into(), |
| 105 | + image: image.into(), |
| 106 | + discord: discord.into(), |
| 107 | + description: description.into(), |
| 108 | + additional: additional.into(), |
| 109 | + }; |
| 110 | + |
| 111 | + // Log the identity details |
| 112 | + log::info!("Setting identity for hotkey: {:?}", hotkey); |
| 113 | + log::info!("Name: {:?}", String::from_utf8_lossy(&identity.name)); |
| 114 | + log::info!("URL: {:?}", String::from_utf8_lossy(&identity.url)); |
| 115 | + log::info!("Image: {:?}", String::from_utf8_lossy(&identity.image)); |
| 116 | + log::info!("Discord: {:?}", String::from_utf8_lossy(&identity.discord)); |
| 117 | + log::info!( |
| 118 | + "Description: {:?}", |
| 119 | + String::from_utf8_lossy(&identity.description) |
| 120 | + ); |
| 121 | + log::info!( |
| 122 | + "Additional: {:?}", |
| 123 | + String::from_utf8_lossy(&identity.additional) |
| 124 | + ); |
| 125 | + |
| 126 | + // Check validation. |
| 127 | + let total_length = identity |
| 128 | + .name |
| 129 | + .len() |
| 130 | + .saturating_add(identity.url.len()) |
| 131 | + .saturating_add(identity.image.len()) |
| 132 | + .saturating_add(identity.discord.len()) |
| 133 | + .saturating_add(identity.description.len()) |
| 134 | + .saturating_add(identity.additional.len()); |
| 135 | + let is_valid: bool = total_length <= 256 + 256 + 1024 + 256 + 1024 + 1024 |
| 136 | + && identity.name.len() <= 256 |
| 137 | + && identity.url.len() <= 256 |
| 138 | + && identity.image.len() <= 1024 |
| 139 | + && identity.discord.len() <= 256 |
| 140 | + && identity.description.len() <= 1024 |
| 141 | + && identity.additional.len() <= 1024; |
| 142 | + if !is_valid { |
| 143 | + continue; |
| 144 | + } |
| 145 | + |
| 146 | + // Get the owning coldkey. |
| 147 | + let coldkey = Owner::<T>::get(decoded_hotkey.clone()); |
| 148 | + weight = weight.saturating_add(T::DbWeight::get().reads(1)); |
| 149 | + |
| 150 | + // Sink into the map. |
| 151 | + Identities::<T>::insert(coldkey.clone(), identity.clone()); |
| 152 | + weight = weight.saturating_add(T::DbWeight::get().writes(1)); |
| 153 | + } |
| 154 | + } |
| 155 | + // Mark the migration as completed |
| 156 | + HasMigrationRun::<T>::insert(&migration_name, true); |
| 157 | + weight = weight.saturating_add(T::DbWeight::get().writes(1)); |
| 158 | + |
| 159 | + log::info!( |
| 160 | + "Migration '{:?}' completed. Storage version set to 7.", |
| 161 | + String::from_utf8_lossy(&migration_name) |
| 162 | + ); |
| 163 | + |
| 164 | + // Return the migration weight. |
| 165 | + weight |
| 166 | +} |
0 commit comments