Skip to content

Fix orphaned storage cleanup on subnet deregistration#2578

Open
eureka0928 wants to merge 2 commits intoopentensor:devnet-readyfrom
eureka0928:fix/cleanup-orphaned-storage-on-subnet-deregistration
Open

Fix orphaned storage cleanup on subnet deregistration#2578
eureka0928 wants to merge 2 commits intoopentensor:devnet-readyfrom
eureka0928:fix/cleanup-orphaned-storage-on-subnet-deregistration

Conversation

@eureka0928
Copy link
Copy Markdown

Description

Clean up AllowancesStorage, RootAlphaDividendsPerSubnet, and 12 additional storage items that were not being removed during subnet deregistration in remove_network.

AllowancesStorage (EVM staking precompile) uses netuid embedded in the second key (spender, netuid), so it cannot use clear_prefix. A new PrecompileCleanupInterface trait (following the existing CommitmentsInterface pattern) is added to handle this cross-crate cleanup.

RootAlphaDividendsPerSubnet and VotingPower have netuid as the first key and are cleaned via clear_prefix.

11 additional simple StorageMaps were also missing removal: 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 — this only adds cleanup of storage that was previously leaked on subnet deregistration.

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 PrecompileCleanupInterface trait follows the same pattern as the existing CommitmentsInterface — a trait defined in pallet-subtensor, implemented in the precompiles crate, and wired through the runtime Config. This avoids a circular dependency since precompiles depends on pallet-subtensor, not the reverse.

Clean up AllowancesStorage, RootAlphaDividendsPerSubnet, and 12
additional storage items that were not being removed during subnet
deregistration in remove_network.
///
/// Because `netuid` is embedded in the second key `(spender, netuid)`, we must iterate
/// all entries and filter. Called during subnet deregistration.
pub fn purge_allowances_for_netuid(netuid: u16) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This approach is already mentioned at #2478

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks for the context! Yes, the concern about unbounded iteration was raised in #2478. In practice though, remove_network already does the same iterate-and-filter pattern for several other DMAPs where netuid is not the first key (ChildkeyTake, ChildKeys, ParentKeys, LastHotkeyEmissionOnNetuid, TransactionKeyLastBlock, StakingOperationRateLimiter), so this is consistent with the existing approach.

The number of allowance entries per netuid should also be small in practice since each requires an explicit approve call from an EVM user.

@eureka0928 eureka0928 requested a review from plind-dm April 9, 2026 08:07
#[test]
fn test_dissolve_network_clears_orphaned_storage() {
new_test_ext(0).execute_with(|| {
let cold = U256::from(1);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

can you add the check AllowancesStorage is removed after subnet de-reg

Copy link
Copy Markdown

@plind-dm plind-dm left a comment

Choose a reason for hiding this comment

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

Adds a test verifying that AllowancesStorage entries for a given netuid
are fully removed by purge_allowances_for_netuid, while entries for other
netuids remain intact.
@open-junius open-junius added skip-cargo-audit This PR fails cargo audit but needs to be merged anyway labels Apr 9, 2026
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.

3 participants