Skip to content

Commit

Permalink
Add worst-case swap_hotkey benchmark and calculate extrinsic weight d…
Browse files Browse the repository at this point in the history
…uring function
  • Loading branch information
Ayden Brewer committed Oct 27, 2023
1 parent 451fd6d commit 3fbb601
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
11 changes: 6 additions & 5 deletions pallets/subtensor/src/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -561,17 +561,18 @@ benchmarks! {
Subtensor::<T>::set_target_registrations_per_interval(netuid, 256);
Subtensor::<T>::set_max_registrations_per_block(netuid, 256);

Subtensor::<T>::add_balance_to_coldkey_account(&coldkey.clone(), Subtensor::<T>::u64_to_balance(10_000_000_000).unwrap());
assert_ok!(Subtensor::<T>::burned_register(RawOrigin::Signed(coldkey.clone()).into(), netuid, old_hotkey.clone()));
assert_ok!(Subtensor::<T>::become_delegate(RawOrigin::Signed(coldkey.clone()).into(), old_hotkey.clone()));

let max_uids = Subtensor::<T>::get_max_allowed_uids(netuid) as u32;
for i in 0..max_uids - 1 {
let coldkey: T::AccountId = account("Axon", 0, i);
let hotkey: T::AccountId = account("Hotkey", 0, i);

Subtensor::<T>::add_balance_to_coldkey_account(&coldkey.clone(), Subtensor::<T>::u64_to_balance(10_000_000_000).unwrap());
assert_ok!(Subtensor::<T>::burned_register(RawOrigin::Signed(coldkey).into(), netuid, hotkey));
assert_ok!(Subtensor::<T>::burned_register(RawOrigin::Signed(coldkey.clone()).into(), netuid, hotkey));
assert_ok!(Subtensor::<T>::add_stake(RawOrigin::Signed(coldkey).into(), old_hotkey.clone(), 1_000_000_000));
}

Subtensor::<T>::add_balance_to_coldkey_account(&coldkey.clone(), Subtensor::<T>::u64_to_balance(10_000_000_000).unwrap());

assert_ok!(Subtensor::<T>::burned_register(RawOrigin::Signed(coldkey.clone()).into(), netuid, old_hotkey.clone()));
}: _(RawOrigin::Signed(coldkey), old_hotkey, new_hotkey)
}
2 changes: 1 addition & 1 deletion pallets/subtensor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1523,7 +1523,7 @@ pub mod pallet {

#[pallet::call_index(69)]
#[pallet::weight((0, DispatchClass::Operational, Pays::No))]
pub fn swap_hotkey(origin: OriginFor<T>, hotkey: T::AccountId, new_hotkey: T::AccountId) -> DispatchResult {
pub fn swap_hotkey(origin: OriginFor<T>, hotkey: T::AccountId, new_hotkey: T::AccountId) -> DispatchResultWithPostInfo {
Self::do_swap_hotkey(origin, &hotkey, &new_hotkey)
}

Expand Down
39 changes: 34 additions & 5 deletions pallets/subtensor/src/registration.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use super::*;
use crate::system::ensure_root;
use frame_support::pallet_prelude::DispatchResult;
use frame_support::pallet_prelude::{DispatchResult, DispatchResultWithPostInfo};
use frame_system::ensure_signed;
use sp_core::{H256, U256};
use sp_core::{H256, U256, Get};
use sp_io::hashing::{keccak_256, sha2_256};
use sp_runtime::MultiAddress;
use sp_std::convert::TryInto;
Expand Down Expand Up @@ -764,8 +764,10 @@ impl<T: Config> Pallet<T> {
return (nonce, vec_work);
}

pub fn do_swap_hotkey(origin: T::RuntimeOrigin, old_hotkey: &T::AccountId, new_hotkey: &T::AccountId) -> DispatchResult {
pub fn do_swap_hotkey(origin: T::RuntimeOrigin, old_hotkey: &T::AccountId, new_hotkey: &T::AccountId) -> DispatchResultWithPostInfo {
let coldkey = ensure_signed(origin)?;

let mut weight = T::DbWeight::get().reads_writes(2, 0);
ensure!(Self::coldkey_owns_hotkey(&coldkey, old_hotkey), Error::<T>::NonAssociatedColdKey);

let block: u64 = Self::get_current_block_as_u64();
Expand All @@ -774,52 +776,72 @@ impl<T: Config> Pallet<T> {
Error::<T>::TxRateLimitExceeded
);

weight.saturating_accrue(T::DbWeight::get().reads(2));

ensure!(old_hotkey != new_hotkey, Error::<T>::AlreadyRegistered);
ensure!(!Self::is_hotkey_registered_on_any_network(new_hotkey), Error::<T>::AlreadyRegistered);

weight.saturating_accrue(T::DbWeight::get().reads((TotalNetworks::<T>::get() + 1u16) as u64));

// Pay TAO?

Owner::<T>::remove(old_hotkey);
Owner::<T>::insert(new_hotkey, coldkey.clone());
weight.saturating_accrue(T::DbWeight::get().writes(2));

if let Ok(total_hotkey_stake) = TotalHotkeyStake::<T>::try_get(old_hotkey) {
TotalHotkeyStake::<T>::remove(old_hotkey);
TotalHotkeyStake::<T>::insert(new_hotkey, total_hotkey_stake);

weight.saturating_accrue(T::DbWeight::get().writes(2));
}

if let Ok(delegate_take) = Delegates::<T>::try_get(old_hotkey) {
Delegates::<T>::remove(old_hotkey);
Delegates::<T>::insert(new_hotkey, delegate_take);

weight.saturating_accrue(T::DbWeight::get().writes(2));
}

if let Ok(last_tx) = LastTxBlock::<T>::try_get(old_hotkey) {
LastTxBlock::<T>::remove(old_hotkey);
LastTxBlock::<T>::insert(new_hotkey, last_tx);

weight.saturating_accrue(T::DbWeight::get().writes(2));
}

let mut coldkey_stake: Vec<(T::AccountId, u64)> = vec![];

for (coldkey, stake_amount) in Stake::<T>::iter_prefix(old_hotkey) {
coldkey_stake.push((coldkey.clone(), stake_amount));
}

let _ = Stake::<T>::clear_prefix(old_hotkey, coldkey_stake.len() as u32, None);
weight.saturating_accrue(T::DbWeight::get().writes(coldkey_stake.len() as u64));

for (coldkey, stake_amount) in coldkey_stake {
Stake::<T>::insert(new_hotkey, coldkey, stake_amount);
weight.saturating_accrue(T::DbWeight::get().writes(1));
}

let mut netuid_is_member: Vec<u16> = vec![];
for netuid in <IsNetworkMember<T> as IterableStorageDoubleMap<T::AccountId, u16, bool>>::iter_key_prefix(old_hotkey) {
netuid_is_member.push(netuid);
}

let _ = IsNetworkMember::<T>::clear_prefix(old_hotkey, netuid_is_member.len() as u32, None);
weight.saturating_accrue(T::DbWeight::get().writes(netuid_is_member.len() as u64));

for netuid in netuid_is_member.iter() {
IsNetworkMember::<T>::insert(new_hotkey, netuid, true);
weight.saturating_accrue(T::DbWeight::get().writes(1));
}

for netuid in netuid_is_member.iter() {
if let Ok(axon_info) = Axons::<T>::try_get(netuid, old_hotkey) {
Axons::<T>::remove(netuid, old_hotkey);
Axons::<T>::insert(netuid, new_hotkey, axon_info);

weight.saturating_accrue(T::DbWeight::get().writes(2));
}
}

Expand All @@ -828,8 +850,12 @@ impl<T: Config> Pallet<T> {
Uids::<T>::remove(netuid, old_hotkey);
Uids::<T>::insert(netuid, new_hotkey, uid);

weight.saturating_accrue(T::DbWeight::get().writes(2));

Keys::<T>::insert(netuid, uid, new_hotkey);

weight.saturating_accrue(T::DbWeight::get().writes(1));

LoadedEmission::<T>::mutate(netuid, |emission_exists| {
match emission_exists {
Some(emissions) => {
Expand All @@ -842,13 +868,16 @@ impl<T: Config> Pallet<T> {
None => {}
}
});

weight.saturating_accrue(T::DbWeight::get().writes(1));
}
}

Self::set_last_tx_block(&coldkey, block);
weight.saturating_accrue(T::DbWeight::get().writes(1));

Self::deposit_event(Event::HotkeySwapped{coldkey, old_hotkey: old_hotkey.clone(), new_hotkey: new_hotkey.clone()});

Ok(())
Ok(Some(weight).into())
}
}

0 comments on commit 3fbb601

Please sign in to comment.