Skip to content

Commit 606b771

Browse files
author
Samuel Dare
committed
fixes for network resumption
1 parent dd87bca commit 606b771

File tree

7 files changed

+29
-13
lines changed

7 files changed

+29
-13
lines changed

pallets/admin-utils/tests/mock.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ parameter_types! {
110110
pub const InitialSubnetLimit: u16 = 10; // Max 10 subnets.
111111
pub const InitialNetworkRateLimit: u64 = 0;
112112
pub const InitialTargetStakesPerInterval: u16 = 1;
113-
pub const InitialHotkeySwapCost: u64 = 1_000_000_000;
113+
pub const InitialKeySwapCost: u64 = 1_000_000_000;
114114
pub const InitialAlphaHigh: u16 = 58982; // Represents 0.9 as per the production default
115115
pub const InitialAlphaLow: u16 = 45875; // Represents 0.7 as per the production default
116116
pub const InitialLiquidAlphaOn: bool = false; // Default value for LiquidAlphaOn
@@ -166,7 +166,7 @@ impl pallet_subtensor::Config for Test {
166166
type InitialSubnetLimit = InitialSubnetLimit;
167167
type InitialNetworkRateLimit = InitialNetworkRateLimit;
168168
type InitialTargetStakesPerInterval = InitialTargetStakesPerInterval;
169-
type HotkeySwapCost = InitialHotkeySwapCost;
169+
type KeySwapCost = InitialKeySwapCost;
170170
type AlphaHigh = InitialAlphaHigh;
171171
type AlphaLow = InitialAlphaLow;
172172
type LiquidAlphaOn = InitialLiquidAlphaOn;

pallets/subtensor/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ pub mod pallet {
247247
type InitialTargetStakesPerInterval: Get<u64>;
248248
/// Cost of swapping a hotkey.
249249
#[pallet::constant]
250-
type HotkeySwapCost: Get<u64>;
250+
type KeySwapCost: Get<u64>;
251251
/// The upper bound for the alpha parameter. Used for Liquid Alpha.
252252
#[pallet::constant]
253253
type AlphaHigh: Get<u16>;

pallets/subtensor/src/swap.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ impl<T: Config> Pallet<T> {
5656
T::DbWeight::get().reads((TotalNetworks::<T>::get().saturating_add(1u16)) as u64),
5757
);
5858

59-
let swap_cost = Self::get_hotkey_swap_cost();
59+
let swap_cost = Self::get_key_swap_cost();
6060
log::debug!("Swap cost: {:?}", swap_cost);
6161

6262
ensure!(
@@ -146,6 +146,20 @@ impl<T: Config> Pallet<T> {
146146
Error::<T>::ColdKeyAlreadyAssociated
147147
);
148148

149+
// Calculate and charge the swap fee
150+
let swap_cost = Self::get_key_swap_cost();
151+
log::debug!("Coldkey swap cost: {:?}", swap_cost);
152+
153+
ensure!(
154+
Self::can_remove_balance_from_coldkey_account(&old_coldkey, swap_cost),
155+
Error::<T>::NotEnoughBalanceToPaySwapColdKey
156+
);
157+
let actual_burn_amount =
158+
Self::remove_balance_from_coldkey_account(&old_coldkey, swap_cost)?;
159+
Self::burn_tokens(actual_burn_amount);
160+
161+
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1));
162+
149163
// Actually do the swap.
150164
weight = weight.saturating_add(
151165
Self::perform_swap_coldkey(&old_coldkey, new_coldkey)

pallets/subtensor/src/utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -671,8 +671,8 @@ impl<T: Config> Pallet<T> {
671671
NominatorMinRequiredStake::<T>::put(min_stake);
672672
}
673673

674-
pub fn get_hotkey_swap_cost() -> u64 {
675-
T::HotkeySwapCost::get()
674+
pub fn get_key_swap_cost() -> u64 {
675+
T::KeySwapCost::get()
676676
}
677677

678678
pub fn get_alpha_values(netuid: u16) -> (u16, u16) {

pallets/subtensor/tests/mock.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ parameter_types! {
164164
pub const InitialSubnetLimit: u16 = 10; // Max 10 subnets.
165165
pub const InitialNetworkRateLimit: u64 = 0;
166166
pub const InitialTargetStakesPerInterval: u16 = 2;
167-
pub const InitialHotkeySwapCost: u64 = 1_000_000_000;
167+
pub const InitialKeySwapCost: u64 = 1_000_000_000;
168168
pub const InitialAlphaHigh: u16 = 58982; // Represents 0.9 as per the production default
169169
pub const InitialAlphaLow: u16 = 45875; // Represents 0.7 as per the production default
170170
pub const InitialLiquidAlphaOn: bool = false; // Default value for LiquidAlphaOn
@@ -369,7 +369,7 @@ impl pallet_subtensor::Config for Test {
369369
type InitialSubnetLimit = InitialSubnetLimit;
370370
type InitialNetworkRateLimit = InitialNetworkRateLimit;
371371
type InitialTargetStakesPerInterval = InitialTargetStakesPerInterval;
372-
type HotkeySwapCost = InitialHotkeySwapCost;
372+
type KeySwapCost = InitialKeySwapCost;
373373
type AlphaHigh = InitialAlphaHigh;
374374
type AlphaLow = InitialAlphaLow;
375375
type LiquidAlphaOn = InitialLiquidAlphaOn;

pallets/subtensor/tests/swap.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,7 +1059,8 @@ fn test_do_swap_coldkey_success() {
10591059
let netuid = 1u16;
10601060
let stake_amount1 = 1000u64;
10611061
let stake_amount2 = 2000u64;
1062-
let free_balance_old = 12345u64 + MIN_BALANCE_TO_PERFORM_COLDKEY_SWAP;
1062+
let swap_cost = SubtensorModule::get_key_swap_cost();
1063+
let free_balance_old = 12345u64 + swap_cost;
10631064

10641065
// Setup initial state
10651066
add_network(netuid, 13, 0);
@@ -1158,7 +1159,7 @@ fn test_do_swap_coldkey_success() {
11581159
// Verify balance transfer
11591160
assert_eq!(
11601161
SubtensorModule::get_coldkey_balance(&new_coldkey),
1161-
free_balance_old
1162+
free_balance_old - swap_cost
11621163
);
11631164
assert_eq!(SubtensorModule::get_coldkey_balance(&old_coldkey), 0);
11641165

@@ -1332,6 +1333,7 @@ fn test_do_swap_coldkey_with_subnet_ownership() {
13321333
let hotkey = U256::from(3);
13331334
let netuid = 1u16;
13341335
let stake_amount: u64 = 1000u64;
1336+
let swap_cost = SubtensorModule::get_key_swap_cost();
13351337

13361338
// Setup initial state
13371339
add_network(netuid, 13, 0);
@@ -1340,7 +1342,7 @@ fn test_do_swap_coldkey_with_subnet_ownership() {
13401342
// Set TotalNetworks because swap relies on it
13411343
pallet_subtensor::TotalNetworks::<Test>::set(1);
13421344

1343-
SubtensorModule::add_balance_to_coldkey_account(&old_coldkey, stake_amount);
1345+
SubtensorModule::add_balance_to_coldkey_account(&old_coldkey, stake_amount + swap_cost);
13441346
SubnetOwner::<Test>::insert(netuid, old_coldkey);
13451347

13461348
// Populate OwnedHotkeys map

runtime/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -876,7 +876,7 @@ parameter_types! {
876876
pub const SubtensorInitialNetworkLockReductionInterval: u64 = 14 * 7200;
877877
pub const SubtensorInitialNetworkRateLimit: u64 = 7200;
878878
pub const SubtensorInitialTargetStakesPerInterval: u16 = 1;
879-
pub const SubtensorInitialHotkeySwapCost: u64 = 1_000_000_000;
879+
pub const SubtensorInitialKeySwapCost: u64 = 1_000_000_000;
880880
pub const InitialAlphaHigh: u16 = 58982; // Represents 0.9 as per the production default
881881
pub const InitialAlphaLow: u16 = 45875; // Represents 0.7 as per the production default
882882
pub const InitialLiquidAlphaOn: bool = false; // Default value for LiquidAlphaOn
@@ -932,7 +932,7 @@ impl pallet_subtensor::Config for Runtime {
932932
type InitialSubnetLimit = SubtensorInitialSubnetLimit;
933933
type InitialNetworkRateLimit = SubtensorInitialNetworkRateLimit;
934934
type InitialTargetStakesPerInterval = SubtensorInitialTargetStakesPerInterval;
935-
type HotkeySwapCost = SubtensorInitialHotkeySwapCost;
935+
type KeySwapCost = SubtensorInitialKeySwapCost;
936936
type AlphaHigh = InitialAlphaHigh;
937937
type AlphaLow = InitialAlphaLow;
938938
type LiquidAlphaOn = InitialLiquidAlphaOn;

0 commit comments

Comments
 (0)