Skip to content

Commit e846eba

Browse files
authored
Merge pull request #2170 from opentensor/network-dissolution-optimization2
Network dissolution optimization for root claim
2 parents d3211b7 + 3b64fb8 commit e846eba

File tree

4 files changed

+55
-84
lines changed

4 files changed

+55
-84
lines changed

pallets/subtensor/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1958,9 +1958,9 @@ pub mod pallet {
19581958
pub type RootClaimed<T: Config> = StorageNMap<
19591959
_,
19601960
(
1961+
NMapKey<Identity, NetUid>, // subnet
19611962
NMapKey<Blake2_128Concat, T::AccountId>, // hot
19621963
NMapKey<Blake2_128Concat, T::AccountId>, // cold
1963-
NMapKey<Identity, NetUid>, // subnet
19641964
),
19651965
u128,
19661966
ValueQuery,

pallets/subtensor/src/macros/dispatches.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -712,8 +712,8 @@ mod dispatches {
712712
///
713713
#[pallet::call_index(2)]
714714
#[pallet::weight((Weight::from_parts(340_800_000, 0)
715-
.saturating_add(T::DbWeight::get().reads(24_u64))
716-
.saturating_add(T::DbWeight::get().writes(15)), DispatchClass::Normal, Pays::Yes))]
715+
.saturating_add(T::DbWeight::get().reads(25_u64))
716+
.saturating_add(T::DbWeight::get().writes(16_u64)), DispatchClass::Normal, Pays::Yes))]
717717
pub fn add_stake(
718718
origin: OriginFor<T>,
719719
hotkey: T::AccountId,
@@ -1587,8 +1587,8 @@ mod dispatches {
15871587
/// - Thrown if key has hit transaction rate limit
15881588
#[pallet::call_index(84)]
15891589
#[pallet::weight((Weight::from_parts(358_500_000, 0)
1590-
.saturating_add(T::DbWeight::get().reads(39_u64))
1591-
.saturating_add(T::DbWeight::get().writes(24_u64)), DispatchClass::Normal, Pays::Yes))]
1590+
.saturating_add(T::DbWeight::get().reads(41_u64))
1591+
.saturating_add(T::DbWeight::get().writes(26_u64)), DispatchClass::Normal, Pays::Yes))]
15921592
pub fn unstake_all_alpha(origin: OriginFor<T>, hotkey: T::AccountId) -> DispatchResult {
15931593
Self::do_unstake_all_alpha(origin, hotkey)
15941594
}
@@ -1701,8 +1701,8 @@ mod dispatches {
17011701
#[pallet::call_index(87)]
17021702
#[pallet::weight((
17031703
Weight::from_parts(351_300_000, 0)
1704-
.saturating_add(T::DbWeight::get().reads(35_u64))
1705-
.saturating_add(T::DbWeight::get().writes(22_u64)),
1704+
.saturating_add(T::DbWeight::get().reads(37_u64))
1705+
.saturating_add(T::DbWeight::get().writes(24_u64)),
17061706
DispatchClass::Normal,
17071707
Pays::Yes
17081708
))]
@@ -1766,8 +1766,8 @@ mod dispatches {
17661766
///
17671767
#[pallet::call_index(88)]
17681768
#[pallet::weight((Weight::from_parts(402_900_000, 0)
1769-
.saturating_add(T::DbWeight::get().reads(24_u64))
1770-
.saturating_add(T::DbWeight::get().writes(15)), DispatchClass::Normal, Pays::Yes))]
1769+
.saturating_add(T::DbWeight::get().reads(25_u64))
1770+
.saturating_add(T::DbWeight::get().writes(16_u64)), DispatchClass::Normal, Pays::Yes))]
17711771
pub fn add_stake_limit(
17721772
origin: OriginFor<T>,
17731773
hotkey: T::AccountId,
@@ -1830,8 +1830,8 @@ mod dispatches {
18301830
///
18311831
#[pallet::call_index(89)]
18321832
#[pallet::weight((Weight::from_parts(377_400_000, 0)
1833-
.saturating_add(T::DbWeight::get().reads(28_u64))
1834-
.saturating_add(T::DbWeight::get().writes(14)), DispatchClass::Normal, Pays::Yes))]
1833+
.saturating_add(T::DbWeight::get().reads(29_u64))
1834+
.saturating_add(T::DbWeight::get().writes(15_u64)), DispatchClass::Normal, Pays::Yes))]
18351835
pub fn remove_stake_limit(
18361836
origin: OriginFor<T>,
18371837
hotkey: T::AccountId,
@@ -1874,8 +1874,8 @@ mod dispatches {
18741874
#[pallet::call_index(90)]
18751875
#[pallet::weight((
18761876
Weight::from_parts(411_500_000, 0)
1877-
.saturating_add(T::DbWeight::get().reads(35_u64))
1878-
.saturating_add(T::DbWeight::get().writes(22_u64)),
1877+
.saturating_add(T::DbWeight::get().reads(37_u64))
1878+
.saturating_add(T::DbWeight::get().writes(24_u64)),
18791879
DispatchClass::Normal,
18801880
Pays::Yes
18811881
))]
@@ -2052,8 +2052,8 @@ mod dispatches {
20522052
/// Without limit_price it remove all the stake similar to `remove_stake` extrinsic
20532053
#[pallet::call_index(103)]
20542054
#[pallet::weight((Weight::from_parts(395_300_000, 10142)
2055-
.saturating_add(T::DbWeight::get().reads(28_u64))
2056-
.saturating_add(T::DbWeight::get().writes(14_u64)), DispatchClass::Normal, Pays::Yes))]
2055+
.saturating_add(T::DbWeight::get().reads(29_u64))
2056+
.saturating_add(T::DbWeight::get().writes(15_u64)), DispatchClass::Normal, Pays::Yes))]
20572057
pub fn remove_stake_full_limit(
20582058
origin: T::RuntimeOrigin,
20592059
hotkey: T::AccountId,

pallets/subtensor/src/staking/claim_root.rs

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use super::*;
22
use frame_support::weights::Weight;
33
use sp_core::Get;
44
use sp_std::collections::btree_set::BTreeSet;
5-
use substrate_fixed::types::{I96F32, U64F64};
5+
use substrate_fixed::types::I96F32;
66
use subtensor_swap_interface::SwapHandler;
77

88
impl<T: Config> Pallet<T> {
@@ -99,7 +99,7 @@ impl<T: Config> Pallet<T> {
9999

100100
// Attain the root claimed to avoid overclaiming.
101101
let root_claimed: I96F32 =
102-
I96F32::saturating_from_num(RootClaimed::<T>::get((hotkey, coldkey, netuid)));
102+
I96F32::saturating_from_num(RootClaimed::<T>::get((netuid, hotkey, coldkey)));
103103

104104
// Subtract the already claimed alpha.
105105
let owed: I96F32 = claimable.saturating_sub(root_claimed);
@@ -200,7 +200,7 @@ impl<T: Config> Pallet<T> {
200200
};
201201

202202
// Increase root claimed by owed amount.
203-
RootClaimed::<T>::mutate((hotkey, coldkey, netuid), |root_claimed| {
203+
RootClaimed::<T>::mutate((netuid, hotkey, coldkey), |root_claimed| {
204204
*root_claimed = root_claimed.saturating_add(owed_u64.into());
205205
});
206206
}
@@ -250,7 +250,7 @@ impl<T: Config> Pallet<T> {
250250
let root_claimable = RootClaimable::<T>::get(hotkey);
251251
for (netuid, claimable_rate) in root_claimable.iter() {
252252
// Get current staker root claimed value.
253-
let root_claimed: u128 = RootClaimed::<T>::get((hotkey, coldkey, netuid));
253+
let root_claimed: u128 = RootClaimed::<T>::get((netuid, hotkey, coldkey));
254254

255255
// Increase root claimed based on the claimable rate.
256256
let new_root_claimed = root_claimed.saturating_add(
@@ -260,7 +260,7 @@ impl<T: Config> Pallet<T> {
260260
);
261261

262262
// Set the new root claimed value.
263-
RootClaimed::<T>::insert((hotkey, coldkey, netuid), new_root_claimed);
263+
RootClaimed::<T>::insert((netuid, hotkey, coldkey), new_root_claimed);
264264
}
265265
}
266266

@@ -277,7 +277,7 @@ impl<T: Config> Pallet<T> {
277277
}
278278

279279
// Get current staker root claimed value.
280-
let root_claimed: u128 = RootClaimed::<T>::get((hotkey, coldkey, netuid));
280+
let root_claimed: u128 = RootClaimed::<T>::get((netuid, hotkey, coldkey));
281281

282282
// Decrease root claimed based on the claimable rate.
283283
let new_root_claimed = root_claimed.saturating_sub(
@@ -287,7 +287,7 @@ impl<T: Config> Pallet<T> {
287287
);
288288

289289
// Set the new root_claimed value.
290-
RootClaimed::<T>::insert((hotkey, coldkey, netuid), new_root_claimed);
290+
RootClaimed::<T>::insert((netuid, hotkey, coldkey), new_root_claimed);
291291
}
292292
}
293293

@@ -359,10 +359,10 @@ impl<T: Config> Pallet<T> {
359359
old_coldkey: &T::AccountId,
360360
new_coldkey: &T::AccountId,
361361
) {
362-
let old_root_claimed = RootClaimed::<T>::get((old_hotkey, old_coldkey, netuid));
363-
RootClaimed::<T>::remove((old_hotkey, old_coldkey, netuid));
362+
let old_root_claimed = RootClaimed::<T>::get((netuid, old_hotkey, old_coldkey));
363+
RootClaimed::<T>::remove((netuid, old_hotkey, old_coldkey));
364364

365-
RootClaimed::<T>::mutate((new_hotkey, new_coldkey, netuid), |new_root_claimed| {
365+
RootClaimed::<T>::mutate((netuid, new_hotkey, new_coldkey), |new_root_claimed| {
366366
*new_root_claimed = old_root_claimed.saturating_add(*new_root_claimed);
367367
});
368368
}
@@ -386,35 +386,14 @@ impl<T: Config> Pallet<T> {
386386

387387
/// Claim all root dividends for subnet and remove all associated data.
388388
pub fn finalize_all_subnet_root_dividends(netuid: NetUid) {
389-
let mut hotkeys_to_clear = BTreeSet::new();
390-
for (hotkey, root_claimable) in RootClaimable::<T>::iter() {
391-
if root_claimable.contains_key(&netuid) {
392-
let alpha_values: Vec<((T::AccountId, NetUid), U64F64)> =
393-
Alpha::<T>::iter_prefix((&hotkey,)).collect();
394-
395-
for ((coldkey, alpha_netuid), _) in alpha_values.into_iter() {
396-
if alpha_netuid == NetUid::ROOT {
397-
Self::root_claim_on_subnet(
398-
&hotkey,
399-
&coldkey,
400-
netuid,
401-
RootClaimTypeEnum::Swap,
402-
true,
403-
);
404-
405-
RootClaimed::<T>::remove((hotkey.clone(), coldkey, netuid));
406-
if !hotkeys_to_clear.contains(&hotkey) {
407-
hotkeys_to_clear.insert(hotkey.clone());
408-
}
409-
}
410-
}
411-
}
412-
}
389+
let hotkeys = RootClaimable::<T>::iter_keys().collect::<Vec<_>>();
413390

414-
for hotkey in hotkeys_to_clear.into_iter() {
415-
RootClaimable::<T>::mutate(&hotkey, |claimable| {
391+
for hotkey in hotkeys.iter() {
392+
RootClaimable::<T>::mutate(hotkey, |claimable| {
416393
claimable.remove(&netuid);
417394
});
418395
}
396+
397+
let _ = RootClaimed::<T>::clear_prefix((netuid,), u32::MAX, None);
419398
}
420399
}

pallets/subtensor/src/tests/claim_root.rs

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ fn test_claim_root_with_drain_emissions() {
135135

136136
// Check root claimed value saved
137137

138-
let claimed = RootClaimed::<Test>::get((&hotkey, &coldkey, netuid));
138+
let claimed = RootClaimed::<Test>::get((netuid, &hotkey, &coldkey));
139139
assert_eq!(u128::from(new_stake), claimed);
140140

141141
// Distribute pending root alpha (round 2)
@@ -180,7 +180,7 @@ fn test_claim_root_with_drain_emissions() {
180180

181181
// Check root claimed value saved (round 2)
182182

183-
let claimed = RootClaimed::<Test>::get((&hotkey, &coldkey, netuid));
183+
let claimed = RootClaimed::<Test>::get((netuid, &hotkey, &coldkey));
184184
assert_eq!(u128::from(u64::from(new_stake2)), claimed);
185185
});
186186
}
@@ -1125,11 +1125,11 @@ fn test_claim_root_with_swap_coldkey() {
11251125

11261126
assert_eq!(
11271127
u128::from(new_stake),
1128-
RootClaimed::<Test>::get((&hotkey, &coldkey, netuid))
1128+
RootClaimed::<Test>::get((netuid, &hotkey, &coldkey))
11291129
);
11301130
assert_eq!(
11311131
0u128,
1132-
RootClaimed::<Test>::get((&hotkey, &new_coldkey, netuid))
1132+
RootClaimed::<Test>::get((netuid, &hotkey, &new_coldkey))
11331133
);
11341134

11351135
// Swap coldkey
@@ -1143,10 +1143,10 @@ fn test_claim_root_with_swap_coldkey() {
11431143

11441144
// Check swapped keys claimed values
11451145

1146-
assert_eq!(0u128, RootClaimed::<Test>::get((&hotkey, &coldkey, netuid)));
1146+
assert_eq!(0u128, RootClaimed::<Test>::get((netuid, &hotkey, &coldkey)));
11471147
assert_eq!(
11481148
u128::from(new_stake),
1149-
RootClaimed::<Test>::get((&hotkey, &new_coldkey, netuid))
1149+
RootClaimed::<Test>::get((netuid, &hotkey, &new_coldkey,))
11501150
);
11511151
});
11521152
}
@@ -1215,11 +1215,11 @@ fn test_claim_root_with_swap_hotkey() {
12151215

12161216
assert_eq!(
12171217
u128::from(new_stake),
1218-
RootClaimed::<Test>::get((&hotkey, &coldkey, netuid))
1218+
RootClaimed::<Test>::get((netuid, &hotkey, &coldkey,))
12191219
);
12201220
assert_eq!(
12211221
0u128,
1222-
RootClaimed::<Test>::get((&new_hotkey, &coldkey, netuid))
1222+
RootClaimed::<Test>::get((netuid, &new_hotkey, &coldkey,))
12231223
);
12241224

12251225
let _old_claimable = *RootClaimable::<Test>::get(hotkey)
@@ -1239,10 +1239,13 @@ fn test_claim_root_with_swap_hotkey() {
12391239

12401240
// Check swapped keys claimed values
12411241

1242-
assert_eq!(0u128, RootClaimed::<Test>::get((&hotkey, &coldkey, netuid)));
1242+
assert_eq!(
1243+
0u128,
1244+
RootClaimed::<Test>::get((netuid, &hotkey, &coldkey,))
1245+
);
12431246
assert_eq!(
12441247
u128::from(new_stake),
1245-
RootClaimed::<Test>::get((&new_hotkey, &coldkey, netuid))
1248+
RootClaimed::<Test>::get((netuid, &new_hotkey, &coldkey,))
12461249
);
12471250

12481251
assert!(!RootClaimable::<Test>::get(hotkey).contains_key(&netuid));
@@ -1281,7 +1284,6 @@ fn test_claim_root_on_network_deregistration() {
12811284
NetUid::ROOT,
12821285
root_stake.into(),
12831286
);
1284-
let root_stake_rate = 0.1f64;
12851287
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
12861288
&hotkey,
12871289
&other_coldkey,
@@ -1307,33 +1309,23 @@ fn test_claim_root_on_network_deregistration() {
13071309
AlphaCurrency::ZERO,
13081310
);
13091311

1310-
// Claim root via network deregistration
1311-
1312-
assert_ok!(SubtensorModule::do_dissolve_network(netuid));
1312+
assert_ok!(SubtensorModule::claim_root(
1313+
RuntimeOrigin::signed(coldkey),
1314+
BTreeSet::from([netuid])
1315+
));
13131316

1314-
// Check new stake
1315-
let validator_take_percent = 0.18f64;
1317+
assert!(RootClaimable::<Test>::get(hotkey).contains_key(&netuid));
13161318

1317-
let new_stake: u64 = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
1318-
&hotkey,
1319-
&coldkey,
1320-
NetUid::ROOT,
1321-
)
1322-
.into();
1319+
assert!(RootClaimed::<Test>::contains_key((
1320+
netuid, &hotkey, &coldkey,
1321+
)));
13231322

1324-
let estimated_stake_increment = (pending_root_alpha as f64)
1325-
* (1f64 - validator_take_percent)
1326-
* current_price
1327-
* root_stake_rate;
1323+
// Claim root via network deregistration
13281324

1329-
assert_abs_diff_eq!(
1330-
new_stake,
1331-
root_stake + estimated_stake_increment as u64,
1332-
epsilon = 10000u64,
1333-
);
1325+
assert_ok!(SubtensorModule::do_dissolve_network(netuid));
13341326

13351327
assert!(!RootClaimed::<Test>::contains_key((
1336-
&hotkey, &coldkey, netuid
1328+
netuid, &hotkey, &coldkey,
13371329
)));
13381330
assert!(!RootClaimable::<Test>::get(hotkey).contains_key(&netuid));
13391331
});
@@ -1505,7 +1497,7 @@ fn test_claim_root_with_unrelated_subnets() {
15051497

15061498
// Check root claimed value saved
15071499

1508-
let claimed = RootClaimed::<Test>::get((&hotkey, &coldkey, netuid));
1500+
let claimed = RootClaimed::<Test>::get((netuid, &hotkey, &coldkey));
15091501
assert_eq!(u128::from(new_stake), claimed);
15101502
});
15111503
}

0 commit comments

Comments
 (0)