Skip to content

Commit 0855df7

Browse files
author
Samuel Dare
committed
chore: pr review, make swaps free
1 parent c82f82f commit 0855df7

File tree

4 files changed

+39
-108
lines changed

4 files changed

+39
-108
lines changed

pallets/subtensor/src/migration.rs

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,7 @@ pub fn migrate_to_v2_fixed_total_stake<T: Config>() -> Weight {
478478
}
479479
}
480480

481+
/// Migrate the OwnedHotkeys map to the new storage format
481482
pub fn migrate_populate_owned<T: Config>() -> Weight {
482483
// Setup migration weight
483484
let mut weight = T::DbWeight::get().reads(1);
@@ -486,27 +487,48 @@ pub fn migrate_populate_owned<T: Config>() -> Weight {
486487
// Check if this migration is needed (if OwnedHotkeys map is empty)
487488
let migrate = OwnedHotkeys::<T>::iter().next().is_none();
488489

489-
// Only runs if we haven't already updated version past above new_storage_version.
490+
// Only runs if the migration is needed
490491
if migrate {
491-
info!(target: LOG_TARGET_1, ">>> Migration: {}", migration_name);
492+
info!(target: LOG_TARGET_1, ">>> Starting Migration: {}", migration_name);
492493

493-
let mut longest_hotkey_vector = 0;
494+
let mut longest_hotkey_vector: usize = 0;
494495
let mut longest_coldkey: Option<T::AccountId> = None;
496+
let mut keys_touched: u64 = 0;
497+
let mut storage_reads: u64 = 0;
498+
let mut storage_writes: u64 = 0;
499+
500+
// Iterate through all Owner entries
495501
Owner::<T>::iter().for_each(|(hotkey, coldkey)| {
502+
storage_reads = storage_reads.saturating_add(1); // Read from Owner storage
496503
let mut hotkeys = OwnedHotkeys::<T>::get(&coldkey);
504+
storage_reads = storage_reads.saturating_add(1); // Read from OwnedHotkeys storage
505+
506+
// Add the hotkey if it's not already in the vector
497507
if !hotkeys.contains(&hotkey) {
498508
hotkeys.push(hotkey);
509+
keys_touched = keys_touched.saturating_add(1);
510+
511+
// Update longest hotkey vector info
499512
if longest_hotkey_vector < hotkeys.len() {
500513
longest_hotkey_vector = hotkeys.len();
501514
longest_coldkey = Some(coldkey.clone());
502515
}
503-
}
504516

505-
OwnedHotkeys::<T>::insert(&coldkey, hotkeys);
517+
// Update the OwnedHotkeys storage
518+
OwnedHotkeys::<T>::insert(&coldkey, hotkeys);
519+
storage_writes = storage_writes.saturating_add(1); // Write to OwnedHotkeys storage
520+
}
506521

507-
weight.saturating_accrue(T::DbWeight::get().reads_writes(2, 1));
522+
// Accrue weight for reads and writes
523+
weight = weight.saturating_add(T::DbWeight::get().reads_writes(2, 1));
508524
});
509-
info!(target: LOG_TARGET_1, "Migration {} finished. Longest hotkey vector: {}", migration_name, longest_hotkey_vector);
525+
526+
// Log migration results
527+
info!(
528+
target: LOG_TARGET_1,
529+
"Migration {} finished. Keys touched: {}, Longest hotkey vector: {}, Storage reads: {}, Storage writes: {}",
530+
migration_name, keys_touched, longest_hotkey_vector, storage_reads, storage_writes
531+
);
510532
if let Some(c) = longest_coldkey {
511533
info!(target: LOG_TARGET_1, "Longest hotkey vector is controlled by: {:?}", c);
512534
}

pallets/subtensor/src/swap.rs

Lines changed: 4 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,6 @@ impl<T: Config> Pallet<T> {
5353
T::DbWeight::get().reads((TotalNetworks::<T>::get().saturating_add(1u16)) as u64),
5454
);
5555

56-
let swap_cost = Self::get_hotkey_swap_cost();
57-
log::debug!("Swap cost: {:?}", swap_cost);
58-
59-
ensure!(
60-
Self::can_remove_balance_from_coldkey_account(&coldkey, swap_cost),
61-
Error::<T>::NotEnoughBalanceToPaySwapHotKey
62-
);
63-
let actual_burn_amount = Self::remove_balance_from_coldkey_account(&coldkey, swap_cost)?;
64-
Self::burn_tokens(actual_burn_amount);
65-
6656
Self::swap_owner(old_hotkey, new_hotkey, &coldkey, &mut weight);
6757
Self::swap_total_hotkey_stake(old_hotkey, new_hotkey, &mut weight);
6858
Self::swap_delegates(old_hotkey, new_hotkey, &mut weight);
@@ -125,38 +115,17 @@ impl<T: Config> Pallet<T> {
125115
old_coldkey: &T::AccountId,
126116
new_coldkey: &T::AccountId,
127117
) -> DispatchResultWithPostInfo {
128-
let caller = ensure_signed(origin)?;
129-
130-
// Ensure the caller is the old coldkey
131-
ensure!(caller == *old_coldkey, Error::<T>::NonAssociatedColdKey);
118+
ensure_signed(origin)?;
132119

133120
let mut weight = T::DbWeight::get().reads(2);
134121

122+
// Check if the new coldkey is already associated with any hotkeys
135123
ensure!(
136-
old_coldkey != new_coldkey,
137-
Error::<T>::NewColdKeyIsSameWithOld
124+
!Self::coldkey_has_associated_hotkeys(new_coldkey),
125+
Error::<T>::ColdKeyAlreadyAssociated
138126
);
139127

140-
// // Check if the new coldkey is already associated with any hotkeys
141-
// ensure!(
142-
// !Self::coldkey_has_associated_hotkeys(new_coldkey),
143-
// Error::<T>::ColdKeyAlreadyAssociated
144-
// );
145-
146128
let block: u64 = Self::get_current_block_as_u64();
147-
// ensure!(
148-
// !Self::exceeds_tx_rate_limit(Self::get_last_tx_block(old_coldkey), block),
149-
// Error::<T>::ColdKeySwapTxRateLimitExceeded
150-
// );
151-
152-
// Note: we probably want to make this free
153-
let swap_cost = Self::get_coldkey_swap_cost();
154-
ensure!(
155-
Self::can_remove_balance_from_coldkey_account(old_coldkey, swap_cost),
156-
Error::<T>::NotEnoughBalanceToPaySwapColdKey
157-
);
158-
let actual_burn_amount = Self::remove_balance_from_coldkey_account(old_coldkey, swap_cost)?;
159-
Self::burn_tokens(actual_burn_amount);
160129

161130
// Swap coldkey references in storage maps
162131
Self::swap_total_coldkey_stake(old_coldkey, new_coldkey, &mut weight);
@@ -671,17 +640,4 @@ impl<T: Config> Pallet<T> {
671640
OwnedHotkeys::<T>::insert(new_coldkey, hotkeys);
672641
weight.saturating_accrue(T::DbWeight::get().reads_writes(0, 2));
673642
}
674-
675-
/// Returns the cost of swapping a coldkey.
676-
///
677-
/// # Returns
678-
///
679-
/// * `u64` - The cost of swapping a coldkey in Rao.
680-
///
681-
/// # Note
682-
///
683-
/// This function returns a hardcoded value. In a production environment, this should be configurable or determined dynamically.
684-
pub fn get_coldkey_swap_cost() -> u64 {
685-
1_000_000 // Example cost in Rao
686-
}
687643
}

pallets/subtensor/tests/swap.rs

Lines changed: 5 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,16 +1057,12 @@ fn test_do_swap_coldkey_success() {
10571057
let hotkey = U256::from(3);
10581058
let netuid = 1u16;
10591059
let stake_amount = 1000u64;
1060-
let swap_cost = SubtensorModule::get_coldkey_swap_cost();
10611060
let free_balance = 12345;
10621061

10631062
// Setup initial state
10641063
add_network(netuid, 13, 0);
10651064
register_ok_neuron(netuid, hotkey, old_coldkey, 0);
1066-
SubtensorModule::add_balance_to_coldkey_account(
1067-
&old_coldkey,
1068-
stake_amount + swap_cost + free_balance,
1069-
);
1065+
SubtensorModule::add_balance_to_coldkey_account(&old_coldkey, stake_amount + free_balance);
10701066

10711067
// Add stake to the neuron
10721068
assert_ok!(SubtensorModule::add_stake(
@@ -1093,7 +1089,7 @@ fn test_do_swap_coldkey_success() {
10931089

10941090
// Get coldkey free balance before swap
10951091
let balance = SubtensorModule::get_coldkey_balance(&old_coldkey);
1096-
assert_eq!(balance, free_balance + swap_cost);
1092+
assert_eq!(balance, free_balance);
10971093

10981094
// Perform the swap
10991095
assert_ok!(SubtensorModule::do_swap_coldkey(
@@ -1112,10 +1108,7 @@ fn test_do_swap_coldkey_success() {
11121108
assert!(!OwnedHotkeys::<Test>::contains_key(old_coldkey));
11131109

11141110
// Verify balance transfer
1115-
assert_eq!(
1116-
SubtensorModule::get_coldkey_balance(&new_coldkey),
1117-
balance - swap_cost
1118-
);
1111+
assert_eq!(SubtensorModule::get_coldkey_balance(&new_coldkey), balance);
11191112
assert_eq!(SubtensorModule::get_coldkey_balance(&old_coldkey), 0);
11201113

11211114
// Verify event emission
@@ -1129,45 +1122,6 @@ fn test_do_swap_coldkey_success() {
11291122
});
11301123
}
11311124

1132-
#[test]
1133-
fn test_do_swap_coldkey_not_enough_balance() {
1134-
new_test_ext(1).execute_with(|| {
1135-
let old_coldkey = U256::from(1);
1136-
let new_coldkey = U256::from(2);
1137-
let swap_cost = SubtensorModule::get_coldkey_swap_cost();
1138-
1139-
// Setup initial state with insufficient balance
1140-
SubtensorModule::add_balance_to_coldkey_account(&old_coldkey, swap_cost - 1);
1141-
1142-
// Attempt the swap
1143-
assert_err!(
1144-
SubtensorModule::do_swap_coldkey(
1145-
<<Test as Config>::RuntimeOrigin>::signed(old_coldkey),
1146-
&old_coldkey,
1147-
&new_coldkey
1148-
),
1149-
Error::<Test>::NotEnoughBalanceToPaySwapColdKey
1150-
);
1151-
});
1152-
}
1153-
1154-
#[test]
1155-
fn test_do_swap_coldkey_same_keys() {
1156-
new_test_ext(1).execute_with(|| {
1157-
let coldkey = U256::from(1);
1158-
1159-
// Attempt the swap with same old and new coldkeys
1160-
assert_err!(
1161-
SubtensorModule::do_swap_coldkey(
1162-
<<Test as Config>::RuntimeOrigin>::signed(coldkey),
1163-
&coldkey,
1164-
&coldkey
1165-
),
1166-
Error::<Test>::NewColdKeyIsSameWithOld
1167-
);
1168-
});
1169-
}
1170-
11711125
#[test]
11721126
fn test_swap_total_coldkey_stake() {
11731127
new_test_ext(1).execute_with(|| {
@@ -1339,8 +1293,7 @@ fn test_do_swap_coldkey_with_subnet_ownership() {
13391293
let new_coldkey = U256::from(2);
13401294
let hotkey = U256::from(3);
13411295
let netuid = 1u16;
1342-
let stake_amount = 1000u64;
1343-
let swap_cost = SubtensorModule::get_coldkey_swap_cost();
1296+
let stake_amount: u64 = 1000u64;
13441297

13451298
// Setup initial state
13461299
add_network(netuid, 13, 0);
@@ -1349,7 +1302,7 @@ fn test_do_swap_coldkey_with_subnet_ownership() {
13491302
// Set TotalNetworks because swap relies on it
13501303
pallet_subtensor::TotalNetworks::<Test>::set(1);
13511304

1352-
SubtensorModule::add_balance_to_coldkey_account(&old_coldkey, stake_amount + swap_cost);
1305+
SubtensorModule::add_balance_to_coldkey_account(&old_coldkey, stake_amount);
13531306
SubnetOwner::<Test>::insert(netuid, old_coldkey);
13541307

13551308
// Populate OwnedHotkeys map

runtime/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
139139
// `spec_version`, and `authoring_version` are the same between Wasm and native.
140140
// This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use
141141
// the compatible custom types.
142-
spec_version: 154,
142+
spec_version: 156,
143143
impl_version: 1,
144144
apis: RUNTIME_API_VERSIONS,
145145
transaction_version: 1,

0 commit comments

Comments
 (0)