Skip to content

Commit 90512c2

Browse files
author
Samuel Dare
committed
chore: fix tests
1 parent b43bed6 commit 90512c2

File tree

2 files changed

+16
-33
lines changed

2 files changed

+16
-33
lines changed

pallets/subtensor/src/swap.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@ impl<T: Config> Pallet<T> {
6060
T::DbWeight::get().reads((TotalNetworks::<T>::get().saturating_add(1u16)) as u64),
6161
);
6262

63+
let swap_cost = Self::get_hotkey_swap_cost();
64+
log::debug!("Swap cost: {:?}", swap_cost);
65+
66+
ensure!(
67+
Self::can_remove_balance_from_coldkey_account(&coldkey, swap_cost),
68+
Error::<T>::NotEnoughBalanceToPaySwapHotKey
69+
);
70+
let actual_burn_amount = Self::remove_balance_from_coldkey_account(&coldkey, swap_cost)?;
71+
Self::burn_tokens(actual_burn_amount);
72+
6373
Self::swap_owner(old_hotkey, new_hotkey, &coldkey, &mut weight);
6474
Self::swap_total_hotkey_stake(old_hotkey, new_hotkey, &mut weight);
6575
Self::swap_delegates(old_hotkey, new_hotkey, &mut weight);
@@ -76,7 +86,6 @@ impl<T: Config> Pallet<T> {
7686
Self::swap_prometheus(old_hotkey, new_hotkey, &netuid_is_member, &mut weight);
7787

7888
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)?;
8089

8190
Self::set_last_tx_block(&coldkey, block);
8291
weight.saturating_accrue(T::DbWeight::get().writes(1));
@@ -950,15 +959,15 @@ impl<T: Config> Pallet<T> {
950959
weight.saturating_accrue(T::DbWeight::get().reads(TotalNetworks::<T>::get() as u64));
951960
}
952961

953-
/// Swaps the Senate membership from the old hotkey to the new hotkey if applicable.
954962
pub fn swap_senate_member(
955963
old_hotkey: &T::AccountId,
956964
new_hotkey: &T::AccountId,
957965
weight: &mut Weight,
958966
) -> DispatchResult {
967+
weight.saturating_accrue(T::DbWeight::get().reads(1));
959968
if T::SenateMembers::is_member(old_hotkey) {
960969
T::SenateMembers::swap_member(old_hotkey, new_hotkey).map_err(|e| e.error)?;
961-
weight.saturating_accrue(T::DbWeight::get().reads_writes(2, 2));
970+
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 2));
962971
}
963972
Ok(())
964973
}

pallets/subtensor/tests/swap.rs

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use codec::Encode;
44
use frame_support::weights::Weight;
55
use frame_support::{assert_err, assert_noop, assert_ok};
6-
use frame_system::Config;
6+
use frame_system::{Config, RawOrigin};
77
mod mock;
88
use mock::*;
99
use pallet_subtensor::*;
@@ -1675,11 +1675,9 @@ fn test_swap_senate_member() {
16751675
let mut weight = Weight::zero();
16761676

16771677
// 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
1678+
assert_ok!(SenateMembers::add_member(
1679+
RawOrigin::Root.into(),
1680+
old_hotkey
16831681
));
16841682

16851683
// Test 1: Successful swap
@@ -1710,29 +1708,5 @@ fn test_swap_senate_member() {
17101708
// Verify weight update (should only have read operations)
17111709
let expected_weight = <Test as frame_system::Config>::DbWeight::get().reads(1);
17121710
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-
));
17371711
});
17381712
}

0 commit comments

Comments
 (0)