fix: SwapHotkey self-loop check for childkeys #2318
+165
−21
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.
Description
This fix addresses the constraint errors seen on mainnet where self-looped child/parent key relationships cause cycle detection errors.
1. Automatic Self-Loop Prevention During Swap
Instead of rejecting swaps when
new_hotkeyis a child ofold_hotkey, the implementation now allows the swap to proceed but automatically filters out the self-loop edge. This is a simpler and more user-friendly approach.Implementation:
new_hotkeyis a child ofold_hotkey, it is automatically removed from the children list before the swap completesPendingChildKeysentriesunlink_childmethod toPCRelationsto support this functionality2. Existing Migration Handles Self-Loops
The existing
migrate_fix_childkeysmigration already removes self-looped entries fromChildKeysandParentKeysstorage by callingclean_self_loops(). This migration:child == parentinChildKeysparent == childinParentKeysHasMigrationRunbefore executing)Changes Made
Files Modified
pallets/subtensor/src/swap/swap_hotkey.rsparent_child_swap_hotkeycall site (no changes needed - filtering happens in the swap function)pallets/subtensor/src/staking/set_children.rsunlink_childmethod toPCRelationsto remove a child from relationsparent_child_swap_hotkeyto automatically filter outnew_hotkeyfrom children if it exists (prevents self-loop)new_hotkeyfrom pending childrenpallets/subtensor/src/tests/swap_hotkey.rstest_swap_hotkey_to_child_removes_self_loop- verifies self-loop is removed when swapping to a childtest_swap_hotkey_filters_pending_child_self_loop- verifies PendingChildKeys self-loops are filteredtest_swap_hotkey_to_child_preserves_other_children- verifies other children are preservedtest_swap_parent_hotkey_self_loops_in_pending- now expects success with self-loop removalTesting
The implementation:
Related Issue(s)
Type of Change
Breaking Change
If this PR introduces a breaking change, please provide a detailed description of the impact and the migration path for existing applications.
Checklist
./scripts/fix_rust.shto ensure my code is formatted and linted correctlyScreenshots (if applicable)
Please include any relevant screenshots or GIFs that demonstrate the changes made.
Additional Notes
Please provide any additional information or context that may be helpful for reviewers.
Contribution by Gittensor, learn more at https://gittensor.io/