Skip to content

Commit 1934f33

Browse files
Merge pull request #556 from opentensor/refactor/hotkey_swap
Refactor/hotkey swap
2 parents 74de077 + 2dda611 commit 1934f33

File tree

12 files changed

+1488
-157
lines changed

12 files changed

+1488
-157
lines changed

justfile

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ clippy-fix:
3535
-A clippy::todo \
3636
-A clippy::unimplemented \
3737
-A clippy::indexing_slicing
38+
@echo "Running cargo clippy with automatic fixes on potentially dirty code..."
39+
cargo +{{RUSTV}} clippy --fix --allow-dirty --workspace --all-targets -- \
40+
-A clippy::todo \
41+
-A clippy::unimplemented \
42+
-A clippy::indexing_slicing
3843
fix:
3944
@echo "Running cargo fix..."
4045
cargo +{{RUSTV}} fix --workspace
@@ -46,5 +51,4 @@ lint:
4651
@echo "Running cargo clippy with automatic fixes on potentially dirty code..."
4752
just clippy-fix
4853
@echo "Running cargo clippy..."
49-
just clippy
50-
54+
just clippy

pallets/admin-utils/tests/mock.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +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;
113114
pub const InitialAlphaHigh: u16 = 58982; // Represents 0.9 as per the production default
114115
pub const InitialAlphaLow: u16 = 45875; // Represents 0.7 as per the production default
115116
pub const InitialLiquidAlphaOn: bool = false; // Default value for LiquidAlphaOn
@@ -164,6 +165,7 @@ impl pallet_subtensor::Config for Test {
164165
type InitialSubnetLimit = InitialSubnetLimit;
165166
type InitialNetworkRateLimit = InitialNetworkRateLimit;
166167
type InitialTargetStakesPerInterval = InitialTargetStakesPerInterval;
168+
type HotkeySwapCost = InitialHotkeySwapCost;
167169
type AlphaHigh = InitialAlphaHigh;
168170
type AlphaLow = InitialAlphaLow;
169171
type LiquidAlphaOn = InitialLiquidAlphaOn;

pallets/subtensor/src/lib.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ mod registration;
4343
mod root;
4444
mod serving;
4545
mod staking;
46+
mod swap;
4647
mod uids;
4748
mod utils;
4849
mod weights;
@@ -238,6 +239,9 @@ pub mod pallet {
238239
/// Initial target stakes per interval issuance.
239240
#[pallet::constant]
240241
type InitialTargetStakesPerInterval: Get<u64>;
242+
/// Cost of swapping a hotkey.
243+
#[pallet::constant]
244+
type HotkeySwapCost: Get<u64>;
241245
/// The upper bound for the alpha parameter. Used for Liquid Alpha.
242246
#[pallet::constant]
243247
type AlphaHigh: Get<u16>;
@@ -740,7 +744,7 @@ pub mod pallet {
740744
pub(super) type TxDelegateTakeRateLimit<T> =
741745
StorageValue<_, u64, ValueQuery, DefaultTxDelegateTakeRateLimit<T>>;
742746
#[pallet::storage] // --- MAP ( key ) --> last_block
743-
pub(super) type LastTxBlock<T: Config> =
747+
pub type LastTxBlock<T: Config> =
744748
StorageMap<_, Identity, T::AccountId, u64, ValueQuery, DefaultLastTxBlock<T>>;
745749
#[pallet::storage] // --- MAP ( key ) --> last_block
746750
pub(super) type LastTxBlockDelegateTake<T: Config> =
@@ -756,10 +760,10 @@ pub mod pallet {
756760
pub type ServingRateLimit<T> =
757761
StorageMap<_, Identity, u16, u64, ValueQuery, DefaultServingRateLimit<T>>;
758762
#[pallet::storage] // --- MAP ( netuid, hotkey ) --> axon_info
759-
pub(super) type Axons<T: Config> =
763+
pub type Axons<T: Config> =
760764
StorageDoubleMap<_, Identity, u16, Blake2_128Concat, T::AccountId, AxonInfoOf, OptionQuery>;
761765
#[pallet::storage] // --- MAP ( netuid, hotkey ) --> prometheus_info
762-
pub(super) type Prometheus<T: Config> = StorageDoubleMap<
766+
pub type Prometheus<T: Config> = StorageDoubleMap<
763767
_,
764768
Identity,
765769
u16,
@@ -1013,13 +1017,13 @@ pub mod pallet {
10131017
}
10141018

10151019
#[pallet::storage] // --- DMAP ( netuid, hotkey ) --> uid
1016-
pub(super) type Uids<T: Config> =
1020+
pub type Uids<T: Config> =
10171021
StorageDoubleMap<_, Identity, u16, Blake2_128Concat, T::AccountId, u16, OptionQuery>;
10181022
#[pallet::storage] // --- DMAP ( netuid, uid ) --> hotkey
1019-
pub(super) type Keys<T: Config> =
1023+
pub type Keys<T: Config> =
10201024
StorageDoubleMap<_, Identity, u16, Identity, u16, T::AccountId, ValueQuery, DefaultKey<T>>;
10211025
#[pallet::storage] // --- DMAP ( netuid ) --> (hotkey, se, ve)
1022-
pub(super) type LoadedEmission<T: Config> =
1026+
pub type LoadedEmission<T: Config> =
10231027
StorageMap<_, Identity, u16, Vec<(T::AccountId, u64, u64)>, OptionQuery>;
10241028

10251029
#[pallet::storage] // --- DMAP ( netuid ) --> active

pallets/subtensor/src/registration.rs

Lines changed: 2 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use super::*;
2-
use frame_support::storage::IterableStorageDoubleMap;
3-
use sp_core::{Get, H256, U256};
2+
use sp_core::{H256, U256};
43
use sp_io::hashing::{keccak_256, sha2_256};
54
use sp_runtime::Saturating;
65
use system::pallet_prelude::BlockNumberFor;
@@ -395,7 +394,7 @@ impl<T: Config> Pallet<T> {
395394
UsedWork::<T>::insert(work.clone(), current_block_number);
396395

397396
// --- 5. Add Balance via faucet.
398-
let balance_to_add: u64 = 100_000_000_000;
397+
let balance_to_add: u64 = 1_000_000_000_000;
399398
Self::coinbase(100_000_000_000); // We are creating tokens here from the coinbase.
400399

401400
Self::add_balance_to_coldkey_account(&coldkey, balance_to_add);
@@ -591,140 +590,4 @@ impl<T: Config> Pallet<T> {
591590
let vec_work: Vec<u8> = Self::hash_to_vec(work);
592591
(nonce, vec_work)
593592
}
594-
595-
pub fn do_swap_hotkey(
596-
origin: T::RuntimeOrigin,
597-
old_hotkey: &T::AccountId,
598-
new_hotkey: &T::AccountId,
599-
) -> DispatchResultWithPostInfo {
600-
let coldkey = ensure_signed(origin)?;
601-
602-
let mut weight = T::DbWeight::get().reads_writes(2, 0);
603-
ensure!(
604-
Self::coldkey_owns_hotkey(&coldkey, old_hotkey),
605-
Error::<T>::NonAssociatedColdKey
606-
);
607-
608-
let block: u64 = Self::get_current_block_as_u64();
609-
ensure!(
610-
!Self::exceeds_tx_rate_limit(Self::get_last_tx_block(&coldkey), block),
611-
Error::<T>::HotKeySetTxRateLimitExceeded
612-
);
613-
614-
weight.saturating_accrue(T::DbWeight::get().reads(2));
615-
616-
ensure!(old_hotkey != new_hotkey, Error::<T>::NewHotKeyIsSameWithOld);
617-
ensure!(
618-
!Self::is_hotkey_registered_on_any_network(new_hotkey),
619-
Error::<T>::HotKeyAlreadyRegisteredInSubNet
620-
);
621-
622-
weight.saturating_accrue(
623-
T::DbWeight::get().reads((TotalNetworks::<T>::get().saturating_add(1)) as u64),
624-
);
625-
626-
let swap_cost = 1_000_000_000u64;
627-
ensure!(
628-
Self::can_remove_balance_from_coldkey_account(&coldkey, swap_cost),
629-
Error::<T>::NotEnoughBalanceToPaySwapHotKey
630-
);
631-
let actual_burn_amount = Self::remove_balance_from_coldkey_account(&coldkey, swap_cost)?;
632-
Self::burn_tokens(actual_burn_amount);
633-
634-
Owner::<T>::remove(old_hotkey);
635-
Owner::<T>::insert(new_hotkey, coldkey.clone());
636-
weight.saturating_accrue(T::DbWeight::get().writes(2));
637-
638-
if let Ok(total_hotkey_stake) = TotalHotkeyStake::<T>::try_get(old_hotkey) {
639-
TotalHotkeyStake::<T>::remove(old_hotkey);
640-
TotalHotkeyStake::<T>::insert(new_hotkey, total_hotkey_stake);
641-
642-
weight.saturating_accrue(T::DbWeight::get().writes(2));
643-
}
644-
645-
if let Ok(delegate_take) = Delegates::<T>::try_get(old_hotkey) {
646-
Delegates::<T>::remove(old_hotkey);
647-
Delegates::<T>::insert(new_hotkey, delegate_take);
648-
649-
weight.saturating_accrue(T::DbWeight::get().writes(2));
650-
}
651-
652-
if let Ok(last_tx) = LastTxBlock::<T>::try_get(old_hotkey) {
653-
LastTxBlock::<T>::remove(old_hotkey);
654-
LastTxBlock::<T>::insert(new_hotkey, last_tx);
655-
656-
weight.saturating_accrue(T::DbWeight::get().writes(2));
657-
}
658-
659-
let mut coldkey_stake: Vec<(T::AccountId, u64)> = vec![];
660-
for (coldkey, stake_amount) in Stake::<T>::iter_prefix(old_hotkey) {
661-
coldkey_stake.push((coldkey.clone(), stake_amount));
662-
}
663-
664-
let _ = Stake::<T>::clear_prefix(old_hotkey, coldkey_stake.len() as u32, None);
665-
weight.saturating_accrue(T::DbWeight::get().writes(coldkey_stake.len() as u64));
666-
667-
for (coldkey, stake_amount) in coldkey_stake {
668-
Stake::<T>::insert(new_hotkey, coldkey, stake_amount);
669-
weight.saturating_accrue(T::DbWeight::get().writes(1));
670-
}
671-
672-
let mut netuid_is_member: Vec<u16> = vec![];
673-
for netuid in <IsNetworkMember<T> as IterableStorageDoubleMap<T::AccountId, u16, bool>>::iter_key_prefix(old_hotkey) {
674-
netuid_is_member.push(netuid);
675-
}
676-
677-
let _ = IsNetworkMember::<T>::clear_prefix(old_hotkey, netuid_is_member.len() as u32, None);
678-
weight.saturating_accrue(T::DbWeight::get().writes(netuid_is_member.len() as u64));
679-
680-
for netuid in netuid_is_member.iter() {
681-
IsNetworkMember::<T>::insert(new_hotkey, netuid, true);
682-
weight.saturating_accrue(T::DbWeight::get().writes(1));
683-
}
684-
685-
for netuid in netuid_is_member.iter() {
686-
if let Ok(axon_info) = Axons::<T>::try_get(netuid, old_hotkey) {
687-
Axons::<T>::remove(netuid, old_hotkey);
688-
Axons::<T>::insert(netuid, new_hotkey, axon_info);
689-
690-
weight.saturating_accrue(T::DbWeight::get().writes(2));
691-
}
692-
}
693-
694-
for netuid in netuid_is_member.iter() {
695-
if let Ok(uid) = Uids::<T>::try_get(netuid, old_hotkey) {
696-
Uids::<T>::remove(netuid, old_hotkey);
697-
Uids::<T>::insert(netuid, new_hotkey, uid);
698-
699-
weight.saturating_accrue(T::DbWeight::get().writes(2));
700-
701-
Keys::<T>::insert(netuid, uid, new_hotkey);
702-
703-
weight.saturating_accrue(T::DbWeight::get().writes(1));
704-
705-
LoadedEmission::<T>::mutate(netuid, |emission_exists| match emission_exists {
706-
Some(emissions) => {
707-
if let Some(emission) = emissions.get_mut(uid as usize) {
708-
let (_, se, ve) = emission;
709-
*emission = (new_hotkey.clone(), *se, *ve);
710-
}
711-
}
712-
None => {}
713-
});
714-
715-
weight.saturating_accrue(T::DbWeight::get().writes(1));
716-
}
717-
}
718-
719-
Self::set_last_tx_block(&coldkey, block);
720-
weight.saturating_accrue(T::DbWeight::get().writes(1));
721-
722-
Self::deposit_event(Event::HotkeySwapped {
723-
coldkey,
724-
old_hotkey: old_hotkey.clone(),
725-
new_hotkey: new_hotkey.clone(),
726-
});
727-
728-
Ok(Some(weight).into())
729-
}
730593
}

0 commit comments

Comments
 (0)