|
| 1 | +use super::*; |
| 2 | +use codec::Compact; |
| 3 | +use frame_support::pallet_prelude::{Decode, Encode}; |
| 4 | +use sp_core::hexdisplay::AsBytesRef; |
| 5 | + |
| 6 | +#[derive(Decode, Encode, PartialEq, Eq, Clone, Debug)] |
| 7 | +pub struct ScheduledColdkeySwapInfo<T: Config> { |
| 8 | + old_coldkey: T::AccountId, |
| 9 | + new_coldkey: T::AccountId, |
| 10 | + arbitration_block: Compact<u64>, |
| 11 | +} |
| 12 | + |
| 13 | +impl<T: Config> Pallet<T> { |
| 14 | + /// Retrieves the scheduled coldkey swap information for an existing account. |
| 15 | + /// |
| 16 | + /// # Arguments |
| 17 | + /// |
| 18 | + /// * `coldkey` - The account ID of the coldkey to check. |
| 19 | + /// |
| 20 | + /// # Returns |
| 21 | + /// |
| 22 | + /// * `Option<ScheduledColdkeySwapInfo<T>>` - The scheduled coldkey swap information if it exists, otherwise `None`. |
| 23 | + /// |
| 24 | + /// # Notes |
| 25 | + /// |
| 26 | + /// This function checks if there are any destination coldkeys associated with the given coldkey. |
| 27 | + /// If there are, it retrieves the arbitration block and constructs the `ScheduledColdkeySwapInfo` struct. |
| 28 | + fn get_scheduled_coldkey_swap_by_existing_account( |
| 29 | + coldkey: AccountIdOf<T>, |
| 30 | + ) -> Option<ScheduledColdkeySwapInfo<T>> { |
| 31 | + let destinations: Vec<T::AccountId> = ColdkeySwapDestinations::<T>::get(&coldkey); |
| 32 | + if destinations.is_empty() { |
| 33 | + return None; |
| 34 | + } |
| 35 | + |
| 36 | + let arbitration_block: u64 = ColdkeyArbitrationBlock::<T>::get(&coldkey); |
| 37 | + |
| 38 | + Some(ScheduledColdkeySwapInfo { |
| 39 | + old_coldkey: coldkey, |
| 40 | + new_coldkey: destinations[0].clone(), |
| 41 | + arbitration_block: arbitration_block.into(), |
| 42 | + }) |
| 43 | + } |
| 44 | + |
| 45 | + /// Retrieves the scheduled coldkey swap information for a given coldkey account vector. |
| 46 | + /// |
| 47 | + /// # Arguments |
| 48 | + /// |
| 49 | + /// * `coldkey_account_vec` - The vector of bytes representing the coldkey account. |
| 50 | + /// |
| 51 | + /// # Returns |
| 52 | + /// |
| 53 | + /// * `Option<ScheduledColdkeySwapInfo<T>>` - The scheduled coldkey swap information if it exists, otherwise `None`. |
| 54 | + /// |
| 55 | + /// # Notes |
| 56 | + /// |
| 57 | + /// This function decodes the coldkey account vector into an account ID and then calls |
| 58 | + /// `get_scheduled_coldkey_swap_by_existing_account` to retrieve the swap information. |
| 59 | + pub fn get_scheduled_coldkey_swap( |
| 60 | + coldkey_account_vec: Vec<u8>, |
| 61 | + ) -> Option<ScheduledColdkeySwapInfo<T>> { |
| 62 | + if coldkey_account_vec.len() != 32 { |
| 63 | + return None; |
| 64 | + } |
| 65 | + |
| 66 | + let coldkey: AccountIdOf<T> = |
| 67 | + T::AccountId::decode(&mut coldkey_account_vec.as_bytes_ref()).ok()?; |
| 68 | + Self::get_scheduled_coldkey_swap_by_existing_account(coldkey) |
| 69 | + } |
| 70 | + |
| 71 | + /// Retrieves all scheduled coldkey swaps from storage. |
| 72 | + /// |
| 73 | + /// # Returns |
| 74 | + /// |
| 75 | + /// * `Vec<ScheduledColdkeySwapInfo<T>>` - A vector containing all scheduled coldkey swap information. |
| 76 | + /// |
| 77 | + /// # Notes |
| 78 | + /// |
| 79 | + /// This function iterates over all coldkeys in `ColdkeySwapDestinations` and retrieves their swap information |
| 80 | + /// using `get_scheduled_coldkey_swap_by_existing_account`. |
| 81 | + pub fn get_all_scheduled_coldkey_swaps() -> Vec<ScheduledColdkeySwapInfo<T>> { |
| 82 | + let mut scheduled_swaps: Vec<ScheduledColdkeySwapInfo<T>> = Vec::new(); |
| 83 | + for coldkey in ColdkeySwapDestinations::<T>::iter_keys() { |
| 84 | + if let Some(swap_info) = Self::get_scheduled_coldkey_swap_by_existing_account(coldkey) { |
| 85 | + scheduled_swaps.push(swap_info); |
| 86 | + } |
| 87 | + } |
| 88 | + scheduled_swaps |
| 89 | + } |
| 90 | + |
| 91 | + /// Retrieves the scheduled coldkey swaps for a given block. |
| 92 | + /// |
| 93 | + /// # Arguments |
| 94 | + /// |
| 95 | + /// * `block` - The block number to check for scheduled coldkey swaps. |
| 96 | + /// |
| 97 | + /// # Returns |
| 98 | + /// |
| 99 | + /// * `Vec<ScheduledColdkeySwapInfo<T>>` - A vector containing the scheduled coldkey swap information for the given block. |
| 100 | + /// |
| 101 | + /// # Notes |
| 102 | + /// |
| 103 | + /// This function retrieves the coldkeys to swap at the given block and then retrieves their swap information |
| 104 | + /// using `get_scheduled_coldkey_swap_by_existing_account`. |
| 105 | + pub fn get_scheduled_coldkey_swaps_at_block(block: u64) -> Vec<ScheduledColdkeySwapInfo<T>> { |
| 106 | + let coldkeys_to_swap: Vec<T::AccountId> = ColdkeysToSwapAtBlock::<T>::get(block); |
| 107 | + let mut scheduled_swaps: Vec<ScheduledColdkeySwapInfo<T>> = Vec::new(); |
| 108 | + for coldkey in coldkeys_to_swap { |
| 109 | + if let Some(swap_info) = Self::get_scheduled_coldkey_swap_by_existing_account(coldkey) { |
| 110 | + scheduled_swaps.push(swap_info); |
| 111 | + } |
| 112 | + } |
| 113 | + scheduled_swaps |
| 114 | + } |
| 115 | + |
| 116 | + /// Retrieves the remaining arbitration period for a given coldkey account vector. |
| 117 | + /// |
| 118 | + /// # Arguments |
| 119 | + /// |
| 120 | + /// * `coldkey_account_vec` - The vector of bytes representing the coldkey account. |
| 121 | + /// |
| 122 | + /// # Returns |
| 123 | + /// |
| 124 | + /// * `Option<u64>` - The remaining arbitration period in blocks if it exists, otherwise `None`. |
| 125 | + /// |
| 126 | + /// # Notes |
| 127 | + /// |
| 128 | + /// This function decodes the coldkey account vector into an account ID and calculates the remaining arbitration period |
| 129 | + /// by subtracting the current block number from the arbitration block number. |
| 130 | + pub fn get_remaining_arbitration_period(coldkey_account_vec: Vec<u8>) -> Option<u64> { |
| 131 | + if coldkey_account_vec.len() != 32 { |
| 132 | + return None; |
| 133 | + } |
| 134 | + |
| 135 | + let coldkey: AccountIdOf<T> = |
| 136 | + T::AccountId::decode(&mut coldkey_account_vec.as_bytes_ref()).ok()?; |
| 137 | + let current_block: u64 = Self::get_current_block_as_u64(); |
| 138 | + let arbitration_block: u64 = ColdkeyArbitrationBlock::<T>::get(&coldkey); |
| 139 | + |
| 140 | + if arbitration_block > current_block { |
| 141 | + Some(arbitration_block.saturating_sub(current_block)) |
| 142 | + } else { |
| 143 | + Some(0) |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + /// Retrieves the destination coldkeys for a given coldkey account vector. |
| 148 | + /// |
| 149 | + /// # Arguments |
| 150 | + /// |
| 151 | + /// * `coldkey_account_vec` - The vector of bytes representing the coldkey account. |
| 152 | + /// |
| 153 | + /// # Returns |
| 154 | + /// |
| 155 | + /// * `Option<Vec<T::AccountId>>` - A vector containing the destination coldkeys if they exist, otherwise `None`. |
| 156 | + /// |
| 157 | + /// # Notes |
| 158 | + /// |
| 159 | + /// This function decodes the coldkey account vector into an account ID and retrieves the destination coldkeys |
| 160 | + /// from `ColdkeySwapDestinations`. |
| 161 | + pub fn get_coldkey_swap_destinations( |
| 162 | + coldkey_account_vec: Vec<u8>, |
| 163 | + ) -> Option<Vec<T::AccountId>> { |
| 164 | + if coldkey_account_vec.len() != 32 { |
| 165 | + return None; |
| 166 | + } |
| 167 | + |
| 168 | + let coldkey: AccountIdOf<T> = |
| 169 | + T::AccountId::decode(&mut coldkey_account_vec.as_bytes_ref()).ok()?; |
| 170 | + Some(ColdkeySwapDestinations::<T>::get(&coldkey)) |
| 171 | + } |
| 172 | +} |
0 commit comments