-
Notifications
You must be signed in to change notification settings - Fork 279
[WIP]store pending root alpha #2332
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
open-junius
wants to merge
4
commits into
devnet-ready
Choose a base branch
from
pending-root-alpha-impl
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
4 commits
Select commit
Hold shift + click to select a range
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
79 changes: 79 additions & 0 deletions
79
pallets/subtensor/src/migrations/migrate_init_pending_root_alpha.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,79 @@ | ||
| use super::*; | ||
| use alloc::{collections::BTreeMap, string::String}; | ||
| use frame_support::{traits::Get, weights::Weight}; | ||
| use substrate_fixed::types::I96F32; | ||
|
|
||
| /// Migration to initialize PendingRootAlpha storage based on RootClaimed storage. | ||
| /// This aggregates all RootClaimed values across all netuids and coldkeys for each hotkey. | ||
| pub fn migrate_init_pending_root_alpha<T: Config>() -> Weight { | ||
| let migration_name = b"migrate_init_pending_root_alpha".to_vec(); | ||
| let mut weight = T::DbWeight::get().reads(1); | ||
|
|
||
| // Check if the migration has already run | ||
| if HasMigrationRun::<T>::get(&migration_name) { | ||
| log::info!( | ||
| "Migration '{:?}' has already run. Skipping.", | ||
| String::from_utf8_lossy(&migration_name) | ||
| ); | ||
| return weight; | ||
| } | ||
|
|
||
| log::info!( | ||
| "Running migration '{}'", | ||
| String::from_utf8_lossy(&migration_name) | ||
| ); | ||
|
|
||
| // Aggregate RootClaimable values by hotkey | ||
| let mut root_claimable_alpha_map: BTreeMap<T::AccountId, u128> = BTreeMap::new(); | ||
| for (hotkey, root_claimable) in RootClaimable::<T>::iter() { | ||
| // Sum rates as I96F32 (not converting to u128 first, which would truncate) | ||
| let claimable_rate: I96F32 = root_claimable | ||
| .values() | ||
| .fold(I96F32::from(0), |acc, x| acc.saturating_add(*x)); | ||
|
|
||
| let root_stake = Pallet::<T>::get_stake_for_hotkey_on_subnet(&hotkey, NetUid::ROOT); | ||
| let total = claimable_rate.saturating_mul(I96F32::saturating_from_num(root_stake)); | ||
|
|
||
| root_claimable_alpha_map.insert(hotkey, total.saturating_to_num::<u128>()); | ||
| weight = weight.saturating_add(T::DbWeight::get().reads(1)); | ||
| } | ||
|
|
||
| // Aggregate RootClaimed values by hotkey | ||
| // Key: hotkey, Value: sum of all RootClaimed values for that hotkey | ||
| let mut root_claimed_alpha_map: BTreeMap<T::AccountId, u128> = BTreeMap::new(); | ||
|
|
||
| // Iterate over all RootClaimed entries: (netuid, hotkey, coldkey) -> claimed_value | ||
| for ((_netuid, hotkey, _coldkey), claimed_value) in RootClaimed::<T>::iter() { | ||
| // Aggregate the claimed value for this hotkey | ||
| root_claimed_alpha_map | ||
| .entry(hotkey.clone()) | ||
| .and_modify(|total| *total = total.saturating_add(claimed_value)) | ||
| .or_insert(claimed_value); | ||
|
|
||
| // Account for read operation | ||
| weight = weight.saturating_add(T::DbWeight::get().reads(1)); | ||
| } | ||
|
|
||
| // Set PendingRootAlpha for each hotkey | ||
| let mut migrated_count = 0u64; | ||
| for (hotkey, claimable) in root_claimable_alpha_map { | ||
| let claimed = root_claimed_alpha_map.get(&hotkey).unwrap_or(&0); | ||
| let pending = claimable.saturating_sub(*claimed); | ||
| PendingRootAlpha::<T>::insert(&hotkey, pending); | ||
| migrated_count = migrated_count.saturating_add(1); | ||
| } | ||
|
|
||
| weight = weight.saturating_add(T::DbWeight::get().writes(migrated_count)); | ||
|
|
||
| log::info!( | ||
| "Migration '{}' completed successfully. Initialized PendingRootAlpha for {} hotkeys.", | ||
| String::from_utf8_lossy(&migration_name), | ||
| migrated_count | ||
| ); | ||
|
|
||
| // Mark the migration as completed | ||
| HasMigrationRun::<T>::insert(&migration_name, true); | ||
| weight = weight.saturating_add(T::DbWeight::get().writes(1)); | ||
|
|
||
| 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.
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.
Why is alpha detached from netuid? What exactly do we want to achieve?
I'd appreciate a detailed PR description to review it properly.
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 for review. It is still in implementation. will add explanation later, and add [WIP]
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.
Also, PR drafts are helpful sometimes.