Skip to content

fix: Prevent and clean up zero-valued entries in Alpha storage maps during hotkey and coldkey swaps#2513

Open
bittoby wants to merge 4 commits intoopentensor:devnet-readyfrom
bittoby:fix/remove-zero-alpha-entries
Open

fix: Prevent and clean up zero-valued entries in Alpha storage maps during hotkey and coldkey swaps#2513
bittoby wants to merge 4 commits intoopentensor:devnet-readyfrom
bittoby:fix/remove-zero-alpha-entries

Conversation

@bittoby
Copy link

@bittoby bittoby commented Mar 18, 2026

Description

Prevent and clean up zero-valued entries in Alpha storage maps during hotkey and coldkey swaps.

Coldkey and hotkey swap logic was inserting zero-valued entries into Alpha, TotalHotkeyShares, TotalHotkeyAlphaLastEpoch, and AlphaDividendsPerSubnet without checking if the value was zero first. These entries persist indefinitely and bloat storage - a single coldkey swap could produce ~18k zero entries, and roughly 52% of Alpha entries on-chain are zero-valued.

This PR adds zero-guards to all insertion points in swap_coldkey.rs and swap_hotkey.rs, and includes a one-time migration to purge existing zero entries from the four affected storage maps.

Related Issue(s)

Type of Change

  • Bug fix (non-breaking change which fixes an issue)

Breaking Change

No breaking changes.

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 ./scripts/fix_rust.sh 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

Additional Notes

The migration collects the full Alpha map into memory before removing zero entries. This is intentional - mutating a Substrate storage map while iterating over it is unsafe. The other three maps are smaller and iterated directly. All four maps use HasMigrationRun to ensure idempotency.

@bittoby
Copy link
Author

bittoby commented Mar 18, 2026

@open-junius Could you please review this PR? Welcome to any feedbacks.
thank you

@open-junius
Copy link
Contributor

change the target branch as devnet-ready, not main.

@bittoby bittoby changed the base branch from main to devnet-ready March 19, 2026 02:47
@bittoby bittoby changed the base branch from devnet-ready to main March 19, 2026 03:17
@bittoby bittoby changed the base branch from main to devnet-ready March 19, 2026 04:36
@bittoby bittoby closed this Mar 19, 2026
@bittoby bittoby force-pushed the fix/remove-zero-alpha-entries branch from 8cd929a to 369a319 Compare March 19, 2026 04:37
@bittoby bittoby reopened this Mar 19, 2026
@bittoby
Copy link
Author

bittoby commented Mar 19, 2026

I changed target branch. Thanks! Ready to review

@open-junius
Copy link
Contributor

please check https://github.com/opentensor/subtensor/pull/2509/changes, which also prevent some zero value insert. Could you include these storage into the PR.

@bittoby
Copy link
Author

bittoby commented Mar 19, 2026

@open-junius Thanks for your feedback! I updated all following your feedback. Could you please review again?

@open-junius open-junius added the skip-cargo-audit This PR fails cargo audit but needs to be merged anyway label Mar 20, 2026
@open-junius
Copy link
Contributor

please merge the latest devnet-ready, will fix the e2e test.

@open-junius open-junius mentioned this pull request Mar 20, 2026
13 tasks
@bittoby
Copy link
Author

bittoby commented Mar 20, 2026

@open-junius I merged the latest devnet-ready. Could you take a look once more?
thank you

@bittoby
Copy link
Author

bittoby commented Mar 20, 2026

@open-junius All e2e tests passed. thank you. The benchmark testing issue is not relate to my change.

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.

Alpha and hotkey storage maps accumulate persistent zero-valued entries

2 participants