-
Notifications
You must be signed in to change notification settings - Fork 267
Fixing StakingHotkeys index #2300
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
Open
grbIzl
wants to merge
6
commits into
devnet-ready
Choose a base branch
from
feat/staking-hot-keys-migration
base: devnet-ready
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7e93f31
Migration to fix StakingHotKeys added
grbIzl 9142ff3
Merge remote-tracking branch 'origin/devnet-ready' into feat/staking-…
grbIzl f036f7b
Merge remote-tracking branch 'origin/devnet-ready' into feat/staking-…
grbIzl 3b8fcef
Fix the build after the merge
grbIzl 0aa2055
cargo fmt
grbIzl 5e6becf
Consider iteration read
grbIzl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
pallets/subtensor/src/migrations/migrate_fix_staking_hot_keys.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| use super::*; | ||
| use frame_support::{traits::Get, weights::Weight}; | ||
| use log; | ||
| use scale_info::prelude::string::String; | ||
| use sp_std::collections::btree_map::BTreeMap; | ||
|
|
||
| pub fn migrate_fix_staking_hot_keys<T: Config>() -> Weight { | ||
| let migration_name = b"migrate_fix_staking_hot_keys".to_vec(); | ||
| let mut weight = T::DbWeight::get().reads(1); | ||
|
|
||
| // Skip if already executed | ||
| if HasMigrationRun::<T>::get(&migration_name) { | ||
| log::info!( | ||
| target: "runtime", | ||
| "Migration '{}' already run - skipping.", | ||
| String::from_utf8_lossy(&migration_name) | ||
| ); | ||
| return weight; | ||
| } | ||
|
|
||
| let mut cache: BTreeMap<T::AccountId, Vec<T::AccountId>> = BTreeMap::new(); | ||
| let mut storage_reads: u64 = 0; | ||
| let mut storage_writes: u64 = 0; | ||
|
|
||
| for ((hotkey, coldkey, _netuid), alpha) in Alpha::<T>::iter() { | ||
| storage_reads = storage_reads.saturating_add(1); | ||
|
|
||
| if alpha == 0 { | ||
| continue; | ||
| } | ||
|
|
||
| let staking_hotkeys = cache.entry(coldkey.clone()).or_insert_with(|| { | ||
| storage_reads = storage_reads.saturating_add(1); | ||
| StakingHotkeys::<T>::get(&coldkey) | ||
| }); | ||
|
|
||
| if !staking_hotkeys.contains(&hotkey) { | ||
| staking_hotkeys.push(hotkey.clone()); | ||
| storage_writes = storage_writes.saturating_add(1); | ||
| StakingHotkeys::<T>::insert(&coldkey, staking_hotkeys.clone()); | ||
| } | ||
| } | ||
| weight = weight.saturating_add(T::DbWeight::get().reads_writes(storage_reads, storage_writes)); | ||
|
|
||
| // Mark migration done | ||
| HasMigrationRun::<T>::insert(&migration_name, true); | ||
| weight = weight.saturating_add(T::DbWeight::get().writes(1)); | ||
|
|
||
| log::info!( | ||
| target: "runtime", | ||
| "Migration '{}' completed.", | ||
| String::from_utf8_lossy(&migration_name) | ||
| ); | ||
|
|
||
| weight | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a read for Alpha, need update weight. Alpha is a big map, it is better to find other storage to find out missed (cold, hot) relation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @open-junius ! I added the forgotten weight. As for the Alfa map idea: I understand your concern but StakingHotkeys is literally index for Alfa map (please correct me if I'm wrong) and one of its goals is indeed to replace some costly calls to Alfa map. In this case using different map for (cold, hot) relation (like Owner map) doesn't make sense to me. Wdyt?