Skip to content

Commit e30c8f4

Browse files
author
Samuel Dare
committed
feat: coldkey swap info
1 parent 4eefcf9 commit e30c8f4

File tree

8 files changed

+325
-62
lines changed

8 files changed

+325
-62
lines changed

node/src/rpc.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ where
6060
C::Api: subtensor_custom_rpc_runtime_api::NeuronInfoRuntimeApi<Block>,
6161
C::Api: subtensor_custom_rpc_runtime_api::SubnetInfoRuntimeApi<Block>,
6262
C::Api: subtensor_custom_rpc_runtime_api::SubnetRegistrationRuntimeApi<Block>,
63+
C::Api: subtensor_custom_rpc_runtime_api::ColdkeySwapRuntimeApi<Block>,
6364
B: sc_client_api::Backend<Block> + Send + Sync + 'static,
6465
P: TransactionPool + 'static,
6566
{

pallets/subtensor/rpc/src/lib.rs

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use std::sync::Arc;
1212
use sp_api::ProvideRuntimeApi;
1313

1414
pub use subtensor_custom_rpc_runtime_api::{
15-
DelegateInfoRuntimeApi, NeuronInfoRuntimeApi, SubnetInfoRuntimeApi,
15+
ColdkeySwapRuntimeApi, DelegateInfoRuntimeApi, NeuronInfoRuntimeApi, SubnetInfoRuntimeApi,
1616
SubnetRegistrationRuntimeApi,
1717
};
1818

@@ -51,6 +51,24 @@ pub trait SubtensorCustomApi<BlockHash> {
5151

5252
#[method(name = "subnetInfo_getLockCost")]
5353
fn get_network_lock_cost(&self, at: Option<BlockHash>) -> RpcResult<u64>;
54+
#[method(name = "coldkeySwap_getScheduledColdkeySwap")]
55+
fn get_scheduled_coldkey_swap(
56+
&self,
57+
coldkey_account_vec: Vec<u8>,
58+
at: Option<BlockHash>,
59+
) -> RpcResult<Vec<u8>>;
60+
#[method(name = "coldkeySwap_getRemainingArbitrationPeriod")]
61+
fn get_remaining_arbitration_period(
62+
&self,
63+
coldkey_account_vec: Vec<u8>,
64+
at: Option<BlockHash>,
65+
) -> RpcResult<Vec<u8>>;
66+
#[method(name = "coldkeySwap_getColdkeySwapDestinations")]
67+
fn get_coldkey_swap_destinations(
68+
&self,
69+
coldkey_account_vec: Vec<u8>,
70+
at: Option<BlockHash>,
71+
) -> RpcResult<Vec<u8>>;
5472
}
5573

5674
pub struct SubtensorCustom<C, P> {
@@ -99,6 +117,7 @@ where
99117
C::Api: NeuronInfoRuntimeApi<Block>,
100118
C::Api: SubnetInfoRuntimeApi<Block>,
101119
C::Api: SubnetRegistrationRuntimeApi<Block>,
120+
C::Api: ColdkeySwapRuntimeApi<Block>,
102121
{
103122
fn get_delegates(&self, at: Option<<Block as BlockT>::Hash>) -> RpcResult<Vec<u8>> {
104123
let api = self.client.runtime_api();
@@ -223,4 +242,51 @@ where
223242
Error::RuntimeError(format!("Unable to get subnet lock cost: {:?}", e)).into()
224243
})
225244
}
245+
246+
fn get_scheduled_coldkey_swap(
247+
&self,
248+
coldkey_account_vec: Vec<u8>,
249+
at: Option<<Block as BlockT>::Hash>,
250+
) -> RpcResult<Vec<u8>> {
251+
let api = self.client.runtime_api();
252+
let at = at.unwrap_or_else(|| self.client.info().best_hash);
253+
254+
api.get_scheduled_coldkey_swap(at, coldkey_account_vec)
255+
.map_err(|e| {
256+
Error::RuntimeError(format!("Unable to get scheduled coldkey swap: {:?}", e)).into()
257+
})
258+
}
259+
260+
fn get_remaining_arbitration_period(
261+
&self,
262+
coldkey_account_vec: Vec<u8>,
263+
at: Option<<Block as BlockT>::Hash>,
264+
) -> RpcResult<Vec<u8>> {
265+
let api = self.client.runtime_api();
266+
let at = at.unwrap_or_else(|| self.client.info().best_hash);
267+
268+
api.get_remaining_arbitration_period(at, coldkey_account_vec)
269+
.map_err(|e| {
270+
Error::RuntimeError(format!(
271+
"Unable to get remaining arbitration period: {:?}",
272+
e
273+
))
274+
.into()
275+
})
276+
}
277+
278+
fn get_coldkey_swap_destinations(
279+
&self,
280+
coldkey_account_vec: Vec<u8>,
281+
at: Option<<Block as BlockT>::Hash>,
282+
) -> RpcResult<Vec<u8>> {
283+
let api = self.client.runtime_api();
284+
let at = at.unwrap_or_else(|| self.client.info().best_hash);
285+
286+
api.get_coldkey_swap_destinations(at, coldkey_account_vec)
287+
.map_err(|e| {
288+
Error::RuntimeError(format!("Unable to get coldkey swap destinations: {:?}", e))
289+
.into()
290+
})
291+
}
226292
}

pallets/subtensor/runtime-api/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,10 @@ sp_api::decl_runtime_apis! {
3232
pub trait SubnetRegistrationRuntimeApi {
3333
fn get_network_registration_cost() -> u64;
3434
}
35+
36+
pub trait ColdkeySwapRuntimeApi {
37+
fn get_scheduled_coldkey_swap( coldkey_account_vec: Vec<u8> ) -> Vec<u8>;
38+
fn get_remaining_arbitration_period( coldkey_account_vec: Vec<u8> ) -> Vec<u8>;
39+
fn get_coldkey_swap_destinations( coldkey_account_vec: Vec<u8> ) -> Vec<u8>;
40+
}
3541
}

pallets/subtensor/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ mod weights;
5151

5252
pub mod delegate_info;
5353
pub mod neuron_info;
54+
pub mod schedule_coldkey_swap_info;
5455
pub mod stake_info;
5556
pub mod subnet_info;
5657

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
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+
}

pallets/subtensor/src/swap.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -189,15 +189,15 @@ impl<T: Config> Pallet<T> {
189189
///
190190
/// This function calculates the remaining arbitration period by subtracting the current block number
191191
/// from the arbitration block number of the coldkey.
192-
pub fn get_remaining_arbitration_period(coldkey: &T::AccountId) -> u64 {
193-
let current_block: u64 = Self::get_current_block_as_u64();
194-
let arbitration_block: u64 = ColdkeyArbitrationBlock::<T>::get(coldkey);
195-
if arbitration_block > current_block {
196-
arbitration_block.saturating_sub(current_block)
197-
} else {
198-
0
199-
}
200-
}
192+
// pub fn get_remaining_arbitration_period(coldkey: &T::AccountId) -> u64 {
193+
// let current_block: u64 = Self::get_current_block_as_u64();
194+
// let arbitration_block: u64 = ColdkeyArbitrationBlock::<T>::get(coldkey);
195+
// if arbitration_block > current_block {
196+
// arbitration_block.saturating_sub(current_block)
197+
// } else {
198+
// 0
199+
// }
200+
// }
201201

202202
pub fn meets_min_allowed_coldkey_balance(coldkey: &T::AccountId) -> bool {
203203
let all_staked_keys: Vec<T::AccountId> = StakingHotkeys::<T>::get(coldkey);

0 commit comments

Comments
 (0)