subtensor icon indicating copy to clipboard operation
subtensor copied to clipboard

fix: clean up orphaned storage on subnet deregistration

Open plind-junior opened this issue 7 months ago • 5 comments

Description

When a subnet is dissolved, its netuid gets recycled for future registrations. Several storage items were not being cleared during remove_network, which means a newly registered subnet could inherit stale state from the previous occupant of that netuid.

This PR ensures all per-subnet storage is properly cleaned up on deregistration:

  • EVM allowances: AllowancesStorage embeds netuid in its second key (spender, netuid), so it can't be cleared with clear_prefix. We introduce a PrecompileCleanupInterface trait (mirroring the existing CommitmentsInterface pattern) to let the pallet invoke cleanup across the crate boundary without creating a circular dependency.
  • Double maps (RootAlphaDividendsPerSubnet, VotingPower, RootClaimed): cleared via clear_prefix since netuid is the first key.
  • Simple maps that were previously missed: MinAllowedUids, MaxWeightsLimit, AdjustmentAlpha, AdjustmentInterval, MinNonImmuneUids, RootProp, RecycleOrBurn, RootClaimableThreshold, VotingPowerTrackingEnabled, VotingPowerDisableAtBlock, VotingPowerEmaAlpha.

Related Issue(s)

  • Closes #1867
  • Closes #2573

Type of Change

  • [x] 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):

Breaking Change

N/A — only adds cleanup that was previously missing. No behavioral change for live subnets.

Checklist

  • [x] I have performed a self-review of my own code
  • [x] I have commented my code, particularly in hard-to-understand areas
  • [x] I have run ./scripts/fix_rust.sh to ensure my code is formatted and linted correctly
  • [ ] I have made corresponding changes to the documentation
  • [x] My changes generate no new warnings
  • [x] I have added tests that prove my fix is effective or that my feature works
  • [x] New and existing unit tests pass locally with my changes
  • [ ] Any dependent changes have been merged and published in downstream modules

Additional Notes

Subnet deregistration is infrequent, so iterating AllowancesStorage to filter by netuid is acceptable. The simple map removals are single-key deletes with negligible cost.

plind-junior avatar Apr 09 '26 06:04 plind-junior

pls fix the fmt in CI. and the generation make the solution complicated. subnet deregistration and registration not happens frequently. we can just remove the storage after subnet is deregistered.

open-junius avatar Apr 09 '26 12:04 open-junius

The PR#2592 is merged. please pull devnet-ready and fix the conflict.

open-junius avatar Apr 20 '26 11:04 open-junius

please fix the CI

open-junius avatar Apr 24 '26 03:04 open-junius

merge conflicts

sam0x17 avatar Apr 24 '26 19:04 sam0x17

In general, the PR looks good to me. For these gc code, I am not sure if we really need it. It can identify some unneeded data like LastColdkeyHotkeyStakeBlock, but some operation need to scan a lot of data. It is risky to consume too much weights.

open-junius avatar Apr 29 '26 10:04 open-junius