Skip to content

fix: clean up orphaned storage on subnet deregistration#2579

Open
plind-junior wants to merge 5 commits intoopentensor:devnet-readyfrom
plind-junior:fix/cleanup-root-alpha-dividends-on-network-removal
Open

fix: clean up orphaned storage on subnet deregistration#2579
plind-junior wants to merge 5 commits intoopentensor:devnet-readyfrom
plind-junior:fix/cleanup-root-alpha-dividends-on-network-removal

Conversation

@plind-junior
Copy link
Copy Markdown

@plind-junior plind-junior commented Apr 9, 2026

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)

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

Breaking Change

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

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

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 plind-junior force-pushed the fix/cleanup-root-alpha-dividends-on-network-removal branch from f2cbbd4 to a4261ae Compare April 9, 2026 06:54
@plind-junior plind-junior marked this pull request as ready for review April 9, 2026 06:57
@plind-junior plind-junior force-pushed the fix/cleanup-root-alpha-dividends-on-network-removal branch 2 times, most recently from 5f14e48 to 462b882 Compare April 9, 2026 07:32
RootAlphaDividendsPerSubnet is not cleaned up when a subnet is
dissolved. Since netuids are recycled (subnet.rs:178-186), stale root
dividend entries from the old subnet persist and become visible to the
new subnet, potentially affecting run_coinbase dividend calculations.

Add clear_prefix for RootAlphaDividendsPerSubnet alongside the existing
AlphaDividendsPerSubnet cleanup in remove_network.

Closes opentensor#1867
@plind-junior plind-junior force-pushed the fix/cleanup-root-alpha-dividends-on-network-removal branch from 462b882 to 7074c43 Compare April 9, 2026 07:43
@open-junius
Copy link
Copy Markdown
Contributor

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.

Add PrecompileCleanupInterface trait to allow the pallet to invoke
cross-crate storage cleanup when a subnet is removed. The precompiles
crate iterates and removes all AllowancesStorage entries matching the
dissolved netuid, preventing stale EVM approvals from carrying over
when the netuid is recycled.
@plind-junior plind-junior force-pushed the fix/cleanup-root-alpha-dividends-on-network-removal branch from 7074c43 to 0010253 Compare April 9, 2026 13:03
Add PrecompileCleanupInterface no-op to all test mocks (chain-extensions,
eco-tests, admin-utils, transaction-fee). Clean up additional orphaned
storage items on subnet deregistration: MinAllowedUids, MaxWeightsLimit,
AdjustmentAlpha, AdjustmentInterval, MinNonImmuneUids, RootProp,
RecycleOrBurn, RootClaimableThreshold, VotingPower, VotingPowerTrackingEnabled,
VotingPowerDisableAtBlock, VotingPowerEmaAlpha. Move PrecompileCleanup
struct into the precompiles crate for cleaner wiring.
@plind-junior plind-junior changed the title fix: clear RootAlphaDividendsPerSubnet on network removal fix: clean up orphaned storage on subnet deregistration Apr 9, 2026
@open-junius open-junius added skip-cargo-audit This PR fails cargo audit but needs to be merged anyway labels Apr 9, 2026
@sam0x17 sam0x17 added the skip-validate-benchmarks This PR fails validate benchmarks but needs to be merged anyway label Apr 9, 2026
Alpha storage was migrated to AlphaV2 (share-based) in January. The test's
before hook was still calling api.query.SubtensorModule.Alpha.getValue,
which now returns 0 for new stake and caused the stakeAfter > stakeBefore
assertion to fail. Use contract.getStake on both sides instead, since
that's what the rest of the suite does and it exercises the precompile
directly.
@plind-junior
Copy link
Copy Markdown
Author

@sam0x17 Check this pls. If it's unnecessary feature, kindly close it

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 skip-validate-benchmarks This PR fails validate benchmarks but needs to be merged anyway

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants