-
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
base: devnet-ready
Are you sure you want to change the base?
Changes from all commits
610317e
aae2748
41681d5
9c0510d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| use alloc::string::String; | ||
| use frame_support::{traits::Get, weights::Weight}; | ||
|
|
||
| use crate::{Config, HasMigrationRun, Lock, Pallet as Subtensor}; | ||
|
|
||
| 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 lock_keys = Lock::<T>::iter_keys().collect::<sp_std::vec::Vec<_>>(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [HIGH] Unbounded Lock backfill can overweight the runtime upgrade This still materializes every
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is acceptable, especially at this stage when Lock map is barely populated. |
||
|
|
||
| // Note for AI reviewer: Lock has very few records at the time of this migration, | ||
| // so iterating it once here is safe and keeps the migration simple. | ||
| for (coldkey, netuid, hotkey) in lock_keys { | ||
| scanned_count = scanned_count.saturating_add(1); | ||
|
|
||
| let mut model = | ||
| Subtensor::<T>::read_conviction_model_for_hotkey(&coldkey, netuid, &hotkey, now); | ||
| model.roll_forward(now, unlock_rate, maturity_rate); | ||
|
|
||
| if model.individual_lock().is_zero() { | ||
| removed_count = removed_count.saturating_add(1); | ||
| } else { | ||
| indexed_count = indexed_count.saturating_add(1); | ||
| } | ||
|
|
||
| Subtensor::<T>::save_conviction_model(&coldkey, netuid, &hotkey, model); | ||
| } | ||
|
|
||
| weight = weight.saturating_add(T::DbWeight::get().reads(scanned_count)); | ||
| weight = weight.saturating_add( | ||
| T::DbWeight::get().writes( | ||
| indexed_count | ||
| .saturating_mul(2) | ||
| .saturating_add(removed_count.saturating_mul(3)), | ||
| ), | ||
| ); | ||
|
|
||
| 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 | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.