-
Notifications
You must be signed in to change notification settings - Fork 319
Optimize locks to avoid full iteration of lock map #2701
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
gztensor
wants to merge
4
commits into
devnet-ready
Choose a base branch
from
feat/reverse-lock-map
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.
+688
−175
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
610317e
Optimize locks to avoid full iteration of lock map
gztensor aae2748
Address ai reviewer comment - remove unbounded vector
gztensor 41681d5
Refactor lock aggregate updates, fix dust collection for aggregates
gztensor 9c0510d
Add focused test for rolling locks at stake removal
gztensor 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
Some comments aren't visible on the classic Files Changed page.
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
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
72 changes: 72 additions & 0 deletions
72
pallets/subtensor/src/migrations/migrate_populate_locking_coldkeys.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,72 @@ | ||
| use alloc::string::String; | ||
| use frame_support::{traits::Get, weights::Weight}; | ||
|
|
||
| use crate::{Config, HasMigrationRun, Lock, Pallet as Subtensor, staking::lock::ConvictionModel}; | ||
|
|
||
| const MIGRATION_NAME: &[u8] = b"migrate_populate_locking_coldkeys"; | ||
|
|
||
| pub fn migrate_populate_locking_coldkeys<T: Config>() -> Weight { | ||
| let mut weight = T::DbWeight::get().reads(1); | ||
|
|
||
| if HasMigrationRun::<T>::get(MIGRATION_NAME) { | ||
| log::info!( | ||
| "Migration '{}' already executed - skipping", | ||
| String::from_utf8_lossy(MIGRATION_NAME) | ||
| ); | ||
| return weight; | ||
| } | ||
|
|
||
| log::info!( | ||
| "Running migration '{}'", | ||
| String::from_utf8_lossy(MIGRATION_NAME) | ||
| ); | ||
|
|
||
| let now = Subtensor::<T>::get_current_block_as_u64(); | ||
| let unlock_rate = crate::UnlockRate::<T>::get(); | ||
| let maturity_rate = crate::MaturityRate::<T>::get(); | ||
| let mut scanned_count = 0u64; | ||
| let mut indexed_count = 0u64; | ||
| let mut removed_count = 0u64; | ||
| let mut locks_to_remove = sp_std::vec::Vec::new(); | ||
|
|
||
| for ((coldkey, netuid, hotkey), lock) in Lock::<T>::iter() { | ||
|
gztensor marked this conversation as resolved.
Outdated
|
||
| scanned_count = scanned_count.saturating_add(1); | ||
| let rolled = ConvictionModel::roll_forward_lock( | ||
| lock, | ||
| now, | ||
| unlock_rate, | ||
| maturity_rate, | ||
| Subtensor::<T>::is_subnet_owner_hotkey(netuid, &hotkey), | ||
| Subtensor::<T>::is_perpetual_lock(&coldkey, netuid), | ||
| ); | ||
|
|
||
| if !rolled.is_zero() { | ||
| Subtensor::<T>::add_locking_coldkey(&hotkey, netuid, &coldkey); | ||
| indexed_count = indexed_count.saturating_add(1); | ||
| } else { | ||
| locks_to_remove.push((coldkey, netuid, hotkey)); | ||
| } | ||
| } | ||
|
|
||
| for (coldkey, netuid, hotkey) in locks_to_remove { | ||
| Lock::<T>::remove((coldkey, netuid, hotkey)); | ||
|
gztensor marked this conversation as resolved.
Outdated
|
||
| removed_count = removed_count.saturating_add(1); | ||
| } | ||
|
|
||
| weight = weight.saturating_add(T::DbWeight::get().reads(scanned_count)); | ||
| weight = weight | ||
| .saturating_add(T::DbWeight::get().writes(indexed_count.saturating_add(removed_count))); | ||
|
|
||
| HasMigrationRun::<T>::insert(MIGRATION_NAME, true); | ||
| weight = weight.saturating_add(T::DbWeight::get().writes(1)); | ||
|
|
||
| log::info!( | ||
| "Migration '{}' completed. scanned_entries={}, indexed_entries={}, removed_zero_entries={}", | ||
| String::from_utf8_lossy(MIGRATION_NAME), | ||
| scanned_count, | ||
| indexed_count, | ||
| removed_count | ||
| ); | ||
|
|
||
| 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
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.
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.
Uh oh!
There was an error while loading. Please reload this page.