-
Notifications
You must be signed in to change notification settings - Fork 310
Fix orphaned storage cleanup on subnet deregistration #2578
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
base: devnet-ready
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -74,6 +74,25 @@ pub type AllowancesStorage = StorageDoubleMap< | |
| ValueQuery, | ||
| >; | ||
|
|
||
| /// Remove all allowance entries for the given `netuid`. | ||
| /// | ||
| /// 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) { | ||
|
Contributor
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. This approach is already mentioned at #2478
Author
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. Thanks for the context! Yes, the concern about unbounded iteration was raised in #2478. In practice though, The number of allowance entries per netuid should also be small in practice since each requires an explicit |
||
| let to_rm: Vec<(H160, H160)> = AllowancesStorage::iter() | ||
| .filter_map(|(approver, (spender, n), _)| { | ||
| if n == netuid { | ||
| Some((approver, spender)) | ||
| } else { | ||
| None | ||
| } | ||
| }) | ||
| .collect(); | ||
| for (approver, spender) in to_rm { | ||
| AllowancesStorage::remove(approver, (spender, netuid)); | ||
| } | ||
| } | ||
|
|
||
| // Old StakingPrecompile had ETH-precision in values, which was not alligned with Substrate API. So | ||
| // it's kinda deprecated, but exists for backward compatibility. Eventually, we should remove it | ||
| // to stop supporting both precompiles. | ||
|
|
@@ -895,3 +914,43 @@ fn try_u64_from_u256(value: U256) -> Result<u64, PrecompileFailure> { | |
| exit_status: ExitError::Other("the value is outside of u64 bounds".into()), | ||
| }) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use sp_core::H160; | ||
|
|
||
| fn addr(n: u64) -> H160 { | ||
| H160::from_low_u64_be(n) | ||
| } | ||
|
|
||
| #[test] | ||
| fn purge_allowances_for_netuid_removes_matching_entries() { | ||
| sp_io::TestExternalities::default().execute_with(|| { | ||
| let approver1 = addr(1); | ||
| let approver2 = addr(2); | ||
| let spender1 = addr(10); | ||
| let spender2 = addr(20); | ||
|
|
||
| // Insert entries for netuid 1 and netuid 2. | ||
| AllowancesStorage::insert(approver1, (spender1, 1u16), U256::from(100u64)); | ||
| AllowancesStorage::insert(approver1, (spender2, 1u16), U256::from(200u64)); | ||
| AllowancesStorage::insert(approver2, (spender1, 1u16), U256::from(300u64)); | ||
| // This one belongs to a different netuid and must not be touched. | ||
| AllowancesStorage::insert(approver1, (spender1, 2u16), U256::from(999u64)); | ||
|
|
||
| purge_allowances_for_netuid(1u16); | ||
|
|
||
| // All netuid-1 entries should be gone. | ||
| assert!(AllowancesStorage::get(approver1, (spender1, 1u16)).is_zero()); | ||
| assert!(AllowancesStorage::get(approver1, (spender2, 1u16)).is_zero()); | ||
| assert!(AllowancesStorage::get(approver2, (spender1, 1u16)).is_zero()); | ||
|
|
||
| // Netuid-2 entry should be untouched. | ||
| assert_eq!( | ||
| AllowancesStorage::get(approver1, (spender1, 2u16)), | ||
| U256::from(999u64) | ||
| ); | ||
| }); | ||
| } | ||
| } | ||
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.
can you add the check AllowancesStorage is removed after subnet de-reg