Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: hotkey swap + tests #542

Closed
wants to merge 18 commits into from

Conversation

distributedstatemachine
Copy link
Contributor

@distributedstatemachine distributedstatemachine commented Jun 17, 2024

Description

This PR :

  • refactors the hot key swap function to make it more testable
  • adds unit tests

Related Issue(s)

  • Closes #[issue number]

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Other (please describe): Refactor and adding test coverage

Breaking Change

If this PR introduces a breaking change, please provide a detailed description of the impact and the migration path for existing applications.

Checklist

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have run cargo fmt and cargo clippy to ensure my code is formatted and linted correctly
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

Screenshots (if applicable)

Please include any relevant screenshots or GIFs that demonstrate the changes made.

Additional Notes

Please provide any additional information or context that may be helpful for reviewers.

@distributedstatemachine distributedstatemachine added devnet-skip This PR will skip testing on devnet testnet-skip This PR will skip testing on testnet hotfix This PR needs to be merged very quickly and will likely skip testing on devnet and testnet labels Jun 17, 2024
@distributedstatemachine distributedstatemachine requested a review from a team June 17, 2024 11:35
pallets/subtensor/src/swap.rs Outdated Show resolved Hide resolved
pallets/subtensor/src/swap.rs Outdated Show resolved Hide resolved
pallets/subtensor/tests/swap.rs Show resolved Hide resolved
gztensor
gztensor previously approved these changes Jun 17, 2024
pallets/subtensor/src/swap.rs Outdated Show resolved Hide resolved
Comment on lines 150 to 160
pub fn swap_total_hotkey_stake(
old_hotkey: &T::AccountId,
new_hotkey: &T::AccountId,
weight: &mut Weight,
) -> DispatchResult {
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));
}
Ok(())
Copy link
Contributor

@orriin orriin Jun 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These methods are a noop if the old_hotkey doesn't exist, so unless that's sometimes expected, this should prob return an error in that case in case of silently nooping.

Could also add some debug logs in case we need to introspect behavior.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the hotkey doesnt exist , it would be caught here : https://github.com/opentensor/subtensor/pull/542/files#diff-f17d3c10f0bee92182f2bdb18feb020e8e740d95394451f8c8930a219e1c0b7aR43 . Do you think its sufficient , or do we still need to catch the error in this function?

Copy link
Contributor

@keithtensor keithtensor Jun 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additionally, the weight calculation here is missing the read from try_get, and doesn't need to return a Result.

Comment on lines +206 to +210
for (coldkey, stake_amount) in stakes {
Stake::<T>::insert(new_hotkey, &coldkey, stake_amount);
let _ = Stake::<T>::clear_prefix(old_hotkey, stake_count, None);
writes += 2; // One write for insert and one for remove
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have we checked how many keys will be touched here? Could be a very expensive operation

///
/// # Returns
///
/// * `Result<(), Error<T>>` - The result of the operation.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment here needs to be updated.

@distributedstatemachine
Copy link
Contributor Author

closing as github is being silly and not updating my latest changes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
devnet-skip This PR will skip testing on devnet hotfix This PR needs to be merged very quickly and will likely skip testing on devnet and testnet testnet-skip This PR will skip testing on testnet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants