-
Notifications
You must be signed in to change notification settings - Fork 314
fix(coinbase): clear AutoStakeDestination on subnet dissolve #2652
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
RUNECTZ33
wants to merge
3
commits into
opentensor:devnet-ready
Choose a base branch
from
RUNECTZ33:fix/autostake-destination-cleanup-on-dissolve
base: devnet-ready
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+140
−1
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -461,6 +461,10 @@ fn dissolve_clears_all_per_subnet_storages() { | |
| // EVM association indexed by (netuid, uid) | ||
| AssociatedEvmAddress::<Test>::insert(net, 0u16, (sp_core::H160::zero(), 1u64)); | ||
|
|
||
| // Auto-stake destination (cold,netuid) -> hot + reverse index | ||
| AutoStakeDestination::<Test>::insert(owner_cold, net, owner_hot); | ||
| AutoStakeDestinationColdkeys::<Test>::mutate(owner_hot, net, |v| v.push(owner_cold)); | ||
|
|
||
| // (Optional) subnet -> lease link | ||
| SubnetUidToLeaseId::<Test>::insert(net, 42u32); | ||
|
|
||
|
|
@@ -626,13 +630,126 @@ fn dissolve_clears_all_per_subnet_storages() { | |
| // Subnet -> lease link | ||
| assert!(!SubnetUidToLeaseId::<Test>::contains_key(net)); | ||
|
|
||
| // Auto-stake destination + reverse index cleared | ||
| assert!(AutoStakeDestination::<Test>::get(owner_cold, net).is_none()); | ||
| assert!(AutoStakeDestinationColdkeys::<Test>::get(owner_hot, net).is_empty()); | ||
|
|
||
| // ------------------------------------------------------------------ | ||
| // Final subnet removal confirmation | ||
| // ------------------------------------------------------------------ | ||
| assert!(!SubtensorModule::if_subnet_exist(net)); | ||
| }); | ||
| } | ||
|
|
||
| // Focused regression for the AutoStakeDestination orphan: without cleanup on | ||
| // dissolve, a stale (coldkey, netuid) → hotkey mapping would survive the | ||
| // subnet's dissolution and silently redirect mining incentive when the same | ||
| // netuid is later re-registered (see `coinbase::run_coinbase` auto-stake | ||
| // path). This test proves the cleanup wipes both halves of the index. | ||
| #[test] | ||
| fn dissolve_clears_auto_stake_destination_preventing_stale_routing() { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add more netuids to guarantee it will only clear the one for the network being dissolved and not other networks? |
||
| new_test_ext(0).execute_with(|| { | ||
| let owner_cold = U256::from(101); | ||
| let owner_hot = U256::from(102); | ||
| let net = add_dynamic_network(&owner_hot, &owner_cold); | ||
|
|
||
| let staker_cold = U256::from(201); | ||
| let stale_dest_hot = U256::from(202); | ||
|
|
||
| AutoStakeDestination::<Test>::insert(staker_cold, net, stale_dest_hot); | ||
| AutoStakeDestinationColdkeys::<Test>::mutate(stale_dest_hot, net, |v| v.push(staker_cold)); | ||
|
|
||
| // Sanity: both halves of the index are populated before dissolve. | ||
| assert_eq!( | ||
| AutoStakeDestination::<Test>::get(staker_cold, net), | ||
| Some(stale_dest_hot) | ||
| ); | ||
| assert_eq!( | ||
| AutoStakeDestinationColdkeys::<Test>::get(stale_dest_hot, net), | ||
| vec![staker_cold] | ||
| ); | ||
|
|
||
| assert_ok!(SubtensorModule::do_dissolve_network(net)); | ||
|
|
||
| assert!(AutoStakeDestination::<Test>::get(staker_cold, net).is_none()); | ||
| assert!(AutoStakeDestinationColdkeys::<Test>::get(stale_dest_hot, net).is_empty()); | ||
| }); | ||
| } | ||
|
|
||
| // Companion regression: dissolving one subnet must NOT clear | ||
| // AutoStakeDestination entries for *other* live subnets. Without per-netuid | ||
| // scoping in the cleanup, a single dissolve could blow away the auto-stake | ||
| // routing for every subnet a coldkey participates in. This test sets up the | ||
| // same (staker_cold, stale_dest_hot) pair across three distinct netuids and | ||
| // asserts that only the dissolved netuid's entries are removed. | ||
| #[test] | ||
| fn dissolve_only_clears_auto_stake_destination_for_dissolved_netuid() { | ||
| new_test_ext(0).execute_with(|| { | ||
| let owner_cold = U256::from(101); | ||
| let owner_hot = U256::from(102); | ||
| let other_owner_cold = U256::from(103); | ||
| let other_owner_hot = U256::from(104); | ||
| let third_owner_cold = U256::from(105); | ||
| let third_owner_hot = U256::from(106); | ||
|
|
||
| let net = add_dynamic_network(&owner_hot, &owner_cold); | ||
| let other_net = add_dynamic_network(&other_owner_hot, &other_owner_cold); | ||
| let third_net = add_dynamic_network(&third_owner_hot, &third_owner_cold); | ||
|
|
||
| // Same (staker_cold, dest_hot) pair routed across three netuids — only | ||
| // the dissolved one should disappear after the call. | ||
| let staker_cold = U256::from(201); | ||
| let stale_dest_hot = U256::from(202); | ||
|
|
||
| for n in [net, other_net, third_net] { | ||
| AutoStakeDestination::<Test>::insert(staker_cold, n, stale_dest_hot); | ||
| AutoStakeDestinationColdkeys::<Test>::mutate(stale_dest_hot, n, |v| { | ||
| v.push(staker_cold) | ||
| }); | ||
| } | ||
|
|
||
| // Sanity: all three netuids have their auto-stake routing in place. | ||
| for n in [net, other_net, third_net] { | ||
| assert_eq!( | ||
| AutoStakeDestination::<Test>::get(staker_cold, n), | ||
| Some(stale_dest_hot), | ||
| "pre-dissolve forward index missing for netuid {n:?}" | ||
| ); | ||
| assert_eq!( | ||
| AutoStakeDestinationColdkeys::<Test>::get(stale_dest_hot, n), | ||
| vec![staker_cold], | ||
| "pre-dissolve reverse index missing for netuid {n:?}" | ||
| ); | ||
| } | ||
|
|
||
| assert_ok!(SubtensorModule::do_dissolve_network(net)); | ||
|
|
||
| // Dissolved netuid: both halves of the index are gone. | ||
| assert!( | ||
| AutoStakeDestination::<Test>::get(staker_cold, net).is_none(), | ||
| "forward index was not cleared for dissolved netuid {net:?}" | ||
| ); | ||
| assert!( | ||
| AutoStakeDestinationColdkeys::<Test>::get(stale_dest_hot, net).is_empty(), | ||
| "reverse index was not cleared for dissolved netuid {net:?}" | ||
| ); | ||
|
|
||
| // Surviving netuids: routing is intact, untouched by the dissolve. | ||
| for n in [other_net, third_net] { | ||
| assert_eq!( | ||
| AutoStakeDestination::<Test>::get(staker_cold, n), | ||
| Some(stale_dest_hot), | ||
| "forward index was incorrectly cleared for surviving netuid {n:?}" | ||
| ); | ||
| assert_eq!( | ||
| AutoStakeDestinationColdkeys::<Test>::get(stale_dest_hot, n), | ||
| vec![staker_cold], | ||
| "reverse index was incorrectly cleared for surviving netuid {n:?}" | ||
| ); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| fn dissolve_alpha_out_but_zero_tao_no_rewards() { | ||
| new_test_ext(0).execute_with(|| { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are 14861 keys on mainnet and 12066 for AutoStakeDestinationColdkeys, so we should be good.