Skip to content

Commit

Permalink
fixes for network resumption
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel Dare committed Jul 12, 2024
1 parent dd87bca commit 606b771
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 13 deletions.
4 changes: 2 additions & 2 deletions pallets/admin-utils/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ parameter_types! {
pub const InitialSubnetLimit: u16 = 10; // Max 10 subnets.
pub const InitialNetworkRateLimit: u64 = 0;
pub const InitialTargetStakesPerInterval: u16 = 1;
pub const InitialHotkeySwapCost: u64 = 1_000_000_000;
pub const InitialKeySwapCost: u64 = 1_000_000_000;
pub const InitialAlphaHigh: u16 = 58982; // Represents 0.9 as per the production default
pub const InitialAlphaLow: u16 = 45875; // Represents 0.7 as per the production default
pub const InitialLiquidAlphaOn: bool = false; // Default value for LiquidAlphaOn
Expand Down Expand Up @@ -166,7 +166,7 @@ impl pallet_subtensor::Config for Test {
type InitialSubnetLimit = InitialSubnetLimit;
type InitialNetworkRateLimit = InitialNetworkRateLimit;
type InitialTargetStakesPerInterval = InitialTargetStakesPerInterval;
type HotkeySwapCost = InitialHotkeySwapCost;
type KeySwapCost = InitialKeySwapCost;
type AlphaHigh = InitialAlphaHigh;
type AlphaLow = InitialAlphaLow;
type LiquidAlphaOn = InitialLiquidAlphaOn;
Expand Down
2 changes: 1 addition & 1 deletion pallets/subtensor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ pub mod pallet {
type InitialTargetStakesPerInterval: Get<u64>;
/// Cost of swapping a hotkey.
#[pallet::constant]
type HotkeySwapCost: Get<u64>;
type KeySwapCost: Get<u64>;
/// The upper bound for the alpha parameter. Used for Liquid Alpha.
#[pallet::constant]
type AlphaHigh: Get<u16>;
Expand Down
16 changes: 15 additions & 1 deletion pallets/subtensor/src/swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl<T: Config> Pallet<T> {
T::DbWeight::get().reads((TotalNetworks::<T>::get().saturating_add(1u16)) as u64),
);

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

ensure!(
Expand Down Expand Up @@ -146,6 +146,20 @@ impl<T: Config> Pallet<T> {
Error::<T>::ColdKeyAlreadyAssociated
);

// Calculate and charge the swap fee
let swap_cost = Self::get_key_swap_cost();
log::debug!("Coldkey swap cost: {:?}", swap_cost);

ensure!(
Self::can_remove_balance_from_coldkey_account(&old_coldkey, swap_cost),
Error::<T>::NotEnoughBalanceToPaySwapColdKey
);
let actual_burn_amount =
Self::remove_balance_from_coldkey_account(&old_coldkey, swap_cost)?;
Self::burn_tokens(actual_burn_amount);

weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1));

// Actually do the swap.
weight = weight.saturating_add(
Self::perform_swap_coldkey(&old_coldkey, new_coldkey)
Expand Down
4 changes: 2 additions & 2 deletions pallets/subtensor/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -671,8 +671,8 @@ impl<T: Config> Pallet<T> {
NominatorMinRequiredStake::<T>::put(min_stake);
}

pub fn get_hotkey_swap_cost() -> u64 {
T::HotkeySwapCost::get()
pub fn get_key_swap_cost() -> u64 {
T::KeySwapCost::get()
}

pub fn get_alpha_values(netuid: u16) -> (u16, u16) {
Expand Down
4 changes: 2 additions & 2 deletions pallets/subtensor/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ parameter_types! {
pub const InitialSubnetLimit: u16 = 10; // Max 10 subnets.
pub const InitialNetworkRateLimit: u64 = 0;
pub const InitialTargetStakesPerInterval: u16 = 2;
pub const InitialHotkeySwapCost: u64 = 1_000_000_000;
pub const InitialKeySwapCost: u64 = 1_000_000_000;
pub const InitialAlphaHigh: u16 = 58982; // Represents 0.9 as per the production default
pub const InitialAlphaLow: u16 = 45875; // Represents 0.7 as per the production default
pub const InitialLiquidAlphaOn: bool = false; // Default value for LiquidAlphaOn
Expand Down Expand Up @@ -369,7 +369,7 @@ impl pallet_subtensor::Config for Test {
type InitialSubnetLimit = InitialSubnetLimit;
type InitialNetworkRateLimit = InitialNetworkRateLimit;
type InitialTargetStakesPerInterval = InitialTargetStakesPerInterval;
type HotkeySwapCost = InitialHotkeySwapCost;
type KeySwapCost = InitialKeySwapCost;
type AlphaHigh = InitialAlphaHigh;
type AlphaLow = InitialAlphaLow;
type LiquidAlphaOn = InitialLiquidAlphaOn;
Expand Down
8 changes: 5 additions & 3 deletions pallets/subtensor/tests/swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1059,7 +1059,8 @@ fn test_do_swap_coldkey_success() {
let netuid = 1u16;
let stake_amount1 = 1000u64;
let stake_amount2 = 2000u64;
let free_balance_old = 12345u64 + MIN_BALANCE_TO_PERFORM_COLDKEY_SWAP;
let swap_cost = SubtensorModule::get_key_swap_cost();
let free_balance_old = 12345u64 + swap_cost;

// Setup initial state
add_network(netuid, 13, 0);
Expand Down Expand Up @@ -1158,7 +1159,7 @@ fn test_do_swap_coldkey_success() {
// Verify balance transfer
assert_eq!(
SubtensorModule::get_coldkey_balance(&new_coldkey),
free_balance_old
free_balance_old - swap_cost
);
assert_eq!(SubtensorModule::get_coldkey_balance(&old_coldkey), 0);

Expand Down Expand Up @@ -1332,6 +1333,7 @@ fn test_do_swap_coldkey_with_subnet_ownership() {
let hotkey = U256::from(3);
let netuid = 1u16;
let stake_amount: u64 = 1000u64;
let swap_cost = SubtensorModule::get_key_swap_cost();

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

SubtensorModule::add_balance_to_coldkey_account(&old_coldkey, stake_amount);
SubtensorModule::add_balance_to_coldkey_account(&old_coldkey, stake_amount + swap_cost);
SubnetOwner::<Test>::insert(netuid, old_coldkey);

// Populate OwnedHotkeys map
Expand Down
4 changes: 2 additions & 2 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,7 @@ parameter_types! {
pub const SubtensorInitialNetworkLockReductionInterval: u64 = 14 * 7200;
pub const SubtensorInitialNetworkRateLimit: u64 = 7200;
pub const SubtensorInitialTargetStakesPerInterval: u16 = 1;
pub const SubtensorInitialHotkeySwapCost: u64 = 1_000_000_000;
pub const SubtensorInitialKeySwapCost: u64 = 1_000_000_000;
pub const InitialAlphaHigh: u16 = 58982; // Represents 0.9 as per the production default
pub const InitialAlphaLow: u16 = 45875; // Represents 0.7 as per the production default
pub const InitialLiquidAlphaOn: bool = false; // Default value for LiquidAlphaOn
Expand Down Expand Up @@ -932,7 +932,7 @@ impl pallet_subtensor::Config for Runtime {
type InitialSubnetLimit = SubtensorInitialSubnetLimit;
type InitialNetworkRateLimit = SubtensorInitialNetworkRateLimit;
type InitialTargetStakesPerInterval = SubtensorInitialTargetStakesPerInterval;
type HotkeySwapCost = SubtensorInitialHotkeySwapCost;
type KeySwapCost = SubtensorInitialKeySwapCost;
type AlphaHigh = InitialAlphaHigh;
type AlphaLow = InitialAlphaLow;
type LiquidAlphaOn = InitialLiquidAlphaOn;
Expand Down

0 comments on commit 606b771

Please sign in to comment.