Skip to content

Commit 1accdfa

Browse files
Fix block indices to claim
1 parent 65bb4f0 commit 1accdfa

File tree

4 files changed

+115
-7
lines changed

4 files changed

+115
-7
lines changed

pallets/subtensor/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1865,7 +1865,11 @@ pub mod pallet {
18651865
>;
18661866

18671867
#[pallet::storage] // --- MAP ( u64 ) --> coldkey | Maps coldkeys that have stake to an index
1868-
pub type StakingColdkeys<T: Config> = StorageMap<_, Identity, u64, T::AccountId, OptionQuery>;
1868+
pub type StakingColdkeysByIndex<T: Config> =
1869+
StorageMap<_, Identity, u64, T::AccountId, OptionQuery>;
1870+
1871+
#[pallet::storage] // --- MAP ( coldkey ) --> index | Maps index that have stake to a coldkey
1872+
pub type StakingColdkeys<T: Config> = StorageMap<_, Identity, T::AccountId, u64, OptionQuery>;
18691873

18701874
#[pallet::storage] // --- Value --> num_staking_coldkeys
18711875
pub type NumStakingColdkeys<T: Config> = StorageValue<_, u64, ValueQuery, DefaultZeroU64<T>>;

pallets/subtensor/src/staking/claim_root.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
use super::*;
22
use frame_support::weights::Weight;
33
use sp_core::Get;
4+
use sp_std::collections::btree_set::BTreeSet;
45
use substrate_fixed::types::I96F32;
56
use subtensor_swap_interface::SwapHandler;
67

78
impl<T: Config> Pallet<T> {
89
pub fn block_hash_to_indices(block_hash: T::Hash, k: u64, n: u64) -> Vec<u64> {
910
let block_hash_bytes = block_hash.as_ref();
10-
let mut indices: Vec<u64> = Vec::new();
11+
let mut indices: BTreeSet<u64> = BTreeSet::new();
1112
// k < n
1213
let start_index: u64 = u64::from_be_bytes(
1314
block_hash_bytes
@@ -30,10 +31,10 @@ impl<T: Config> Pallet<T> {
3031
.saturating_add(idx_step)
3132
.checked_rem(n)
3233
.unwrap_or(0);
33-
indices.push(idx);
34+
indices.insert(idx);
3435
last_idx = idx;
3536
}
36-
indices
37+
indices.into_iter().collect()
3738
}
3839

3940
pub fn increase_root_claimable_for_hotkey_and_subnet(
@@ -277,6 +278,15 @@ impl<T: Config> Pallet<T> {
277278
Weight::default()
278279
}
279280

281+
pub fn maybe_add_coldkey_index(coldkey: &T::AccountId) {
282+
if !StakingColdkeys::<T>::contains_key(coldkey) {
283+
let n = NumStakingColdkeys::<T>::get();
284+
StakingColdkeysByIndex::<T>::insert(n, coldkey.clone());
285+
StakingColdkeys::<T>::insert(coldkey.clone(), n);
286+
NumStakingColdkeys::<T>::mutate(|n| *n = n.saturating_add(1));
287+
}
288+
}
289+
280290
pub fn run_auto_claim_root_divs(last_block_hash: T::Hash) -> Weight {
281291
let mut weight: Weight = Weight::default();
282292

@@ -289,7 +299,7 @@ impl<T: Config> Pallet<T> {
289299

290300
for i in coldkeys_to_claim.iter() {
291301
weight.saturating_accrue(T::DbWeight::get().reads(1));
292-
if let Ok(coldkey) = StakingColdkeys::<T>::try_get(i) {
302+
if let Ok(coldkey) = StakingColdkeysByIndex::<T>::try_get(i) {
293303
weight.saturating_accrue(Self::do_root_claim(coldkey.clone()));
294304
}
295305

pallets/subtensor/src/staking/stake_utils.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,7 @@ impl<T: Config> Pallet<T> {
830830
// Adjust root claimed for this hotkey and coldkey.
831831
let alpha = swap_result.amount_paid_out.into();
832832
Self::add_stake_adjust_root_claimed_for_hotkey_and_coldkey(hotkey, coldkey, alpha);
833+
Self::maybe_add_coldkey_index(coldkey);
833834
}
834835

835836
// Deposit and log the staking event.

pallets/subtensor/src/tests/claim_root.rs

Lines changed: 95 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
use crate::tests::mock::{RuntimeOrigin, SubtensorModule, Test, add_dynamic_network, new_test_ext};
1+
use crate::tests::mock::{
2+
RuntimeOrigin, SubtensorModule, Test, add_dynamic_network, new_test_ext, run_to_block,
3+
};
24
use crate::{
35
NetworksAdded, RootClaimable, SubnetAlphaIn, SubnetMechanism, SubnetTAO, SubtokenEnabled,
46
Tempo, pallet,
57
};
68
use crate::{RootClaimType, RootClaimTypeEnum, RootClaimed};
79
use approx::assert_abs_diff_eq;
810
use frame_support::assert_ok;
9-
use sp_core::U256;
11+
use sp_core::{H256, U256};
1012
use substrate_fixed::types::{I96F32, U96F32};
1113
use subtensor_runtime_common::{AlphaCurrency, Currency, NetUid, TaoCurrency};
1214
use subtensor_swap_interface::SwapHandler;
@@ -771,3 +773,94 @@ fn test_claim_root_with_run_coinbase() {
771773
assert!(new_stake > 0);
772774
});
773775
}
776+
777+
#[test]
778+
fn test_claim_root_bloch_hash_indices() {
779+
new_test_ext(1).execute_with(|| {
780+
let k = 15u64;
781+
let n = 15000u64;
782+
783+
// 1
784+
let hash = sp_core::keccak_256(b"some");
785+
let mut indices = SubtensorModule::block_hash_to_indices(H256(hash), k, n);
786+
indices.sort();
787+
788+
assert!(indices.len() <= k as usize);
789+
assert!(!indices.iter().any(|i| *i >= n));
790+
// precomputed values
791+
let expected_result = vec![
792+
265, 630, 1286, 1558, 4496, 4861, 5517, 5789, 6803, 8096, 9092, 11034, 11399, 12055,
793+
12327,
794+
];
795+
assert_eq!(indices, expected_result);
796+
797+
// 2
798+
let hash = sp_core::keccak_256(b"some2");
799+
let mut indices = SubtensorModule::block_hash_to_indices(H256(hash), k, n);
800+
indices.sort();
801+
802+
assert!(indices.len() <= k as usize);
803+
assert!(!indices.iter().any(|i| *i >= n));
804+
// precomputed values
805+
let expected_result = vec![
806+
61, 246, 1440, 2855, 3521, 5236, 6130, 6615, 8511, 9405, 9890, 11786, 11971, 13165,
807+
14580,
808+
];
809+
assert_eq!(indices, expected_result);
810+
});
811+
}
812+
813+
#[test]
814+
fn test_claim_root_with_block_emissions() {
815+
new_test_ext(0).execute_with(|| {
816+
let owner_coldkey = U256::from(1001);
817+
let hotkey = U256::from(1002);
818+
let coldkey = U256::from(1003);
819+
let netuid = add_dynamic_network(&hotkey, &owner_coldkey);
820+
821+
Tempo::<Test>::insert(netuid, 1);
822+
SubtensorModule::set_tao_weight(u64::MAX); // Set TAO weight to 1.0
823+
824+
let root_stake = 200_000_000u64;
825+
SubnetTAO::<Test>::insert(NetUid::ROOT, TaoCurrency::from(root_stake));
826+
827+
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
828+
&hotkey,
829+
&coldkey,
830+
NetUid::ROOT,
831+
root_stake.into(),
832+
);
833+
SubtensorModule::maybe_add_coldkey_index(&coldkey);
834+
835+
let initial_total_hotkey_alpha = 10_000_000u64;
836+
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
837+
&hotkey,
838+
&owner_coldkey,
839+
netuid,
840+
initial_total_hotkey_alpha.into(),
841+
);
842+
843+
assert_ok!(SubtensorModule::set_root_claim_type(
844+
RuntimeOrigin::signed(coldkey),
845+
RootClaimTypeEnum::Keep
846+
),);
847+
assert_eq!(RootClaimType::<Test>::get(coldkey), RootClaimTypeEnum::Keep);
848+
849+
// Distribute pending root alpha
850+
851+
let initial_stake: u64 =
852+
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey, &coldkey, netuid)
853+
.into();
854+
assert_eq!(initial_stake, 0u64);
855+
856+
run_to_block(2);
857+
858+
// Check stake after block emissions
859+
860+
let new_stake: u64 =
861+
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey, &coldkey, netuid)
862+
.into();
863+
864+
assert!(new_stake > 0);
865+
});
866+
}

0 commit comments

Comments
 (0)