Skip to content

Commit b43bed6

Browse files
author
Samuel Dare
committed
draft: hotkey swap for senate
1 parent 7aaeb09 commit b43bed6

File tree

2 files changed

+85
-22
lines changed

2 files changed

+85
-22
lines changed

pallets/subtensor/src/swap.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ impl<T: Config> Pallet<T> {
7676
Self::swap_prometheus(old_hotkey, new_hotkey, &netuid_is_member, &mut weight);
7777

7878
Self::swap_total_hotkey_coldkey_stakes_this_interval(old_hotkey, new_hotkey, &mut weight);
79+
Self::swap_senate_member(old_hotkey, new_hotkey, &mut weight)?;
7980

8081
Self::set_last_tx_block(&coldkey, block);
8182
weight.saturating_accrue(T::DbWeight::get().writes(1));
@@ -948,4 +949,17 @@ impl<T: Config> Pallet<T> {
948949
}
949950
weight.saturating_accrue(T::DbWeight::get().reads(TotalNetworks::<T>::get() as u64));
950951
}
952+
953+
/// Swaps the Senate membership from the old hotkey to the new hotkey if applicable.
954+
pub fn swap_senate_member(
955+
old_hotkey: &T::AccountId,
956+
new_hotkey: &T::AccountId,
957+
weight: &mut Weight,
958+
) -> DispatchResult {
959+
if T::SenateMembers::is_member(old_hotkey) {
960+
T::SenateMembers::swap_member(old_hotkey, new_hotkey).map_err(|e| e.error)?;
961+
weight.saturating_accrue(T::DbWeight::get().reads_writes(2, 2));
962+
}
963+
Ok(())
964+
}
951965
}

pallets/subtensor/tests/swap.rs

Lines changed: 71 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use codec::Encode;
44
use frame_support::weights::Weight;
5-
use frame_support::{assert_err, assert_ok};
5+
use frame_support::{assert_err, assert_noop, assert_ok};
66
use frame_system::Config;
77
mod mock;
88
use mock::*;
@@ -1666,24 +1666,73 @@ 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!(Senate::set_members(
1679+
RuntimeOrigin::root(),
1680+
vec![old_hotkey],
1681+
None,
1682+
0
1683+
));
1684+
1685+
// Test 1: Successful swap
1686+
assert_ok!(SubtensorModule::swap_senate_member(
1687+
&old_hotkey,
1688+
&new_hotkey,
1689+
&mut weight
1690+
));
1691+
assert!(Senate::is_member(&new_hotkey));
1692+
assert!(!Senate::is_member(&old_hotkey));
1693+
1694+
// Verify weight update
1695+
let expected_weight = <Test as frame_system::Config>::DbWeight::get().reads_writes(2, 2);
1696+
assert_eq!(weight, expected_weight);
1697+
1698+
// Reset weight for next test
1699+
weight = Weight::zero();
1700+
1701+
// Test 2: Swap with non-member (should not change anything)
1702+
assert_ok!(SubtensorModule::swap_senate_member(
1703+
&non_member_hotkey,
1704+
&new_hotkey,
1705+
&mut weight
1706+
));
1707+
assert!(Senate::is_member(&new_hotkey));
1708+
assert!(!Senate::is_member(&non_member_hotkey));
1709+
1710+
// Verify weight update (should only have read operations)
1711+
let expected_weight = <Test as frame_system::Config>::DbWeight::get().reads(1);
1712+
assert_eq!(weight, expected_weight);
1713+
1714+
// Reset weight for next test
1715+
weight = Weight::zero();
1716+
1717+
// Test 3: Setup for swap to an existing member
1718+
let another_member = U256::from(4);
1719+
assert_ok!(Senate::set_members(
1720+
RuntimeOrigin::root(),
1721+
vec![new_hotkey, another_member],
1722+
None,
1723+
1
1724+
));
1725+
1726+
// Test 4: Setup for swap with maximum members reached
1727+
let mut members = vec![new_hotkey, another_member];
1728+
for i in 5..=SenateMaxMembers::get() {
1729+
members.push(U256::from(i as u64));
1730+
}
1731+
assert_ok!(Senate::set_members(
1732+
RuntimeOrigin::root(),
1733+
members.clone(),
1734+
None,
1735+
2
1736+
));
1737+
});
1738+
}

0 commit comments

Comments
 (0)