Skip to content

Refactor childkeys/parentkeys, add migration to clean state#2116

Merged
sam0x17 merged 8 commits intodevnet-readyfrom
fix/child-parent-setting
Oct 9, 2025
Merged

Refactor childkeys/parentkeys, add migration to clean state#2116
sam0x17 merged 8 commits intodevnet-readyfrom
fix/child-parent-setting

Conversation

@gztensor
Copy link
Contributor

@gztensor gztensor commented Oct 8, 2025

Description

Refactor childkeys and parentkeys.

  • State maps stay the same
  • All updates and validation are moved to set_childkeys.rs file.

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):

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

@gztensor gztensor added the skip-cargo-audit This PR fails cargo audit but needs to be merged anyway label Oct 8, 2025
// Migrate subnet burn cost to 2500
.saturating_add(migrations::migrate_network_lock_cost_2500::migrate_network_lock_cost_2500::<T>())
// Cleanup child/parent keys
.saturating_add(migrations::migrate_fix_childkeys::migrate_fix_childkeys::<T>()),
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
.saturating_add(migrations::migrate_fix_childkeys::migrate_fix_childkeys::<T>()),
.saturating_add(migrations::migrate_fix_childkeys::migrate_fix_childkeys::<T>())

ales-otf
ales-otf previously approved these changes Oct 9, 2025
Copy link
Contributor

@ales-otf ales-otf left a comment

Choose a reason for hiding this comment

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

feel free to ignore my comments, they are just hints

Self::set_parentkeys(pivot.clone(), netuid, new_parents_vec.clone());

let prev_parents_set: BTreeSet<T::AccountId> =
prev_parents_vec.iter().map(|(_, p)| p.clone()).collect();
Copy link
Contributor

Choose a reason for hiding this comment

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

since prev_parents_vec goes out of scope here, you can probably avoid cloning by using into_iter. but it depends on prev_parents_vec ownership.


// Updated parents = intersection where proportion changed
for common in new_parents_set.intersection(&prev_parents_set) {
let new_p = match new_parents_map.get(common) {
Copy link
Contributor

Choose a reason for hiding this comment

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

just a hint. this can be probably replaced by a one-liner using ok_or method. something like: new_parents_map.get(common).ok_or(Error::<T>::ChildParentInconsistency.into())?. you just need to figure out dereferencing of the inner value

////////////////////////////////////////////////////////
// Actual migration

Pallet::<T>::clean_zero_childkey_vectors(&mut weight);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Did you consider iterating through the child and parent key maps once and passing collected values into the substeps?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, but after clean_zero_childkey_vectors the number of keys in both maps is significantly reduced, so it is not worth the added complexity. Also, we've done heavier migrations in the past, so this one will pass.

@sam0x17 sam0x17 merged commit cf157c5 into devnet-ready Oct 9, 2025
54 of 56 checks passed
@camfairchild
Copy link
Contributor

@basfroman
This is a breaking change for one of the E2E tests. A new error propogates DuplicateChild instead of the previous TooManyChildren from the test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

skip-cargo-audit This PR fails cargo audit but needs to be merged anyway

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants