Skip to content

Commit dd87bca

Browse files
author
Samuel Dare
committed
Merge branch 'fix/hotkeyswap_senate' into feat/remove_schedule_coldkey_swap
2 parents 9d1f6c9 + 90512c2 commit dd87bca

File tree

2 files changed

+59
-23
lines changed

2 files changed

+59
-23
lines changed

pallets/subtensor/src/swap.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -954,4 +954,17 @@ impl<T: Config> Pallet<T> {
954954
}
955955
weight.saturating_accrue(T::DbWeight::get().reads(TotalNetworks::<T>::get() as u64));
956956
}
957+
958+
pub fn swap_senate_member(
959+
old_hotkey: &T::AccountId,
960+
new_hotkey: &T::AccountId,
961+
weight: &mut Weight,
962+
) -> DispatchResult {
963+
weight.saturating_accrue(T::DbWeight::get().reads(1));
964+
if T::SenateMembers::is_member(old_hotkey) {
965+
T::SenateMembers::swap_member(old_hotkey, new_hotkey).map_err(|e| e.error)?;
966+
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 2));
967+
}
968+
Ok(())
969+
}
957970
}

pallets/subtensor/tests/swap.rs

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
use codec::Encode;
44
use frame_support::weights::Weight;
5-
use frame_support::{assert_err, assert_ok};
6-
use frame_system::Config;
5+
use frame_support::{assert_err, assert_noop, assert_ok};
6+
use frame_system::{Config, RawOrigin};
77
mod mock;
88
use mock::*;
99
use pallet_subtensor::*;
@@ -1666,24 +1666,47 @@ fn test_coldkey_swap_total() {
16661666
});
16671667
}
16681668

1669-
// #[test]
1670-
// fn test_coldkey_arbitrated_sw() {
1671-
// new_test_ext(1).execute_with(|| {
1672-
// let coldkey = U256::from(1);
1673-
// let hotkey = U256::from(2);
1674-
// let netuid = 1u16;
1675-
1676-
// // Setup initial state
1677-
// add_network(netuid, 13, 0);
1678-
// register_ok_neuron(netuid, hotkey, coldkey, 0);
1679-
1680-
// // Check if coldkey has associated hotkeys
1681-
// assert!(SubtensorModule::coldkey_has_associated_hotkeys(&coldkey));
1682-
1683-
// // Check for a coldkey without associated hotkeys
1684-
// let unassociated_coldkey = U256::from(3);
1685-
// assert!(!SubtensorModule::coldkey_has_associated_hotkeys(
1686-
// &unassociated_coldkey
1687-
// ));
1688-
// });
1689-
// }
1669+
#[test]
1670+
fn test_swap_senate_member() {
1671+
new_test_ext(1).execute_with(|| {
1672+
let old_hotkey = U256::from(1);
1673+
let new_hotkey = U256::from(2);
1674+
let non_member_hotkey = U256::from(3);
1675+
let mut weight = Weight::zero();
1676+
1677+
// Setup: Add old_hotkey as a Senate member
1678+
assert_ok!(SenateMembers::add_member(
1679+
RawOrigin::Root.into(),
1680+
old_hotkey
1681+
));
1682+
1683+
// Test 1: Successful swap
1684+
assert_ok!(SubtensorModule::swap_senate_member(
1685+
&old_hotkey,
1686+
&new_hotkey,
1687+
&mut weight
1688+
));
1689+
assert!(Senate::is_member(&new_hotkey));
1690+
assert!(!Senate::is_member(&old_hotkey));
1691+
1692+
// Verify weight update
1693+
let expected_weight = <Test as frame_system::Config>::DbWeight::get().reads_writes(2, 2);
1694+
assert_eq!(weight, expected_weight);
1695+
1696+
// Reset weight for next test
1697+
weight = Weight::zero();
1698+
1699+
// Test 2: Swap with non-member (should not change anything)
1700+
assert_ok!(SubtensorModule::swap_senate_member(
1701+
&non_member_hotkey,
1702+
&new_hotkey,
1703+
&mut weight
1704+
));
1705+
assert!(Senate::is_member(&new_hotkey));
1706+
assert!(!Senate::is_member(&non_member_hotkey));
1707+
1708+
// Verify weight update (should only have read operations)
1709+
let expected_weight = <Test as frame_system::Config>::DbWeight::get().reads(1);
1710+
assert_eq!(weight, expected_weight);
1711+
});
1712+
}

0 commit comments

Comments
 (0)