Skip to content

Commit 4e0a7fa

Browse files
Add root claim maps.
1 parent 8def8b5 commit 4e0a7fa

File tree

4 files changed

+198
-4
lines changed

4 files changed

+198
-4
lines changed

pallets/subtensor/src/coinbase/root.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ impl<T: Config> Pallet<T> {
210210
Error::<T>::SubnetNotExists
211211
);
212212

213-
Self::finalize_all_subnet_root_dividends(netuid);
213+
Self::clear_root_claim_root_data(netuid);
214214

215215
// --- Perform the cleanup before removing the network.
216216
T::SwapInterface::dissolve_all_liquidity_providers(netuid)?;

pallets/subtensor/src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2249,6 +2249,16 @@ pub mod pallet {
22492249
#[pallet::storage] // --- Value --> num_root_claim | Number of coldkeys to claim each auto-claim.
22502250
pub type NumRootClaim<T: Config> = StorageValue<_, u64, ValueQuery, DefaultNumRootClaim<T>>;
22512251

2252+
/// --- MAP ( subnet ) --> claimed root alpha (kept)
2253+
#[pallet::storage]
2254+
pub type SubnetRootClaimKeep<T: Config> =
2255+
StorageMap<_, Blake2_128Concat, NetUid, AlphaCurrency, ValueQuery, DefaultZeroAlpha<T>>;
2256+
2257+
/// --- MAP ( subnet ) --> claimed root alpha (swapped)
2258+
#[pallet::storage]
2259+
pub type SubnetRootClaimSwap<T: Config> =
2260+
StorageMap<_, Blake2_128Concat, NetUid, AlphaCurrency, ValueQuery, DefaultZeroAlpha<T>>;
2261+
22522262
/// =============================
22532263
/// ==== EVM related storage ====
22542264
/// =============================

pallets/subtensor/src/staking/claim_root.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,10 @@ impl<T: Config> Pallet<T> {
191191
coldkey,
192192
owed_tao.amount_paid_out.into(),
193193
);
194+
195+
SubnetRootClaimSwap::<T>::mutate(netuid, |alpha| {
196+
*alpha = alpha.saturating_add(owed_u64.into());
197+
});
194198
} else
195199
/* Keep */
196200
{
@@ -207,6 +211,10 @@ impl<T: Config> Pallet<T> {
207211
RootClaimed::<T>::mutate((netuid, hotkey, coldkey), |root_claimed| {
208212
*root_claimed = root_claimed.saturating_add(owed_u64.into());
209213
});
214+
215+
SubnetRootClaimKeep::<T>::mutate(netuid, |alpha| {
216+
*alpha = alpha.saturating_add(owed_u64.into());
217+
});
210218
}
211219

212220
fn root_claim_on_subnet_weight(_root_claim_type: RootClaimTypeEnum) -> Weight {
@@ -388,8 +396,8 @@ impl<T: Config> Pallet<T> {
388396
RootClaimable::<T>::insert(new_hotkey, dst_root_claimable);
389397
}
390398

391-
/// Claim all root dividends for subnet and remove all associated data.
392-
pub fn finalize_all_subnet_root_dividends(netuid: NetUid) {
399+
/// Remove all root claim data for subnet.
400+
pub fn clear_root_claim_root_data(netuid: NetUid) {
393401
let hotkeys = RootClaimable::<T>::iter_keys().collect::<Vec<_>>();
394402

395403
for hotkey in hotkeys.iter() {
@@ -399,5 +407,8 @@ impl<T: Config> Pallet<T> {
399407
}
400408

401409
let _ = RootClaimed::<T>::clear_prefix((netuid,), u32::MAX, None);
410+
411+
SubnetRootClaimKeep::<T>::remove(&netuid);
412+
SubnetRootClaimSwap::<T>::remove(&netuid);
402413
}
403414
}

pallets/subtensor/src/tests/claim_root.rs

Lines changed: 174 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#![allow(clippy::expect_used)]
22

3-
use crate::RootAlphaDividendsPerSubnet;
43
use crate::tests::mock::{
54
RuntimeOrigin, SubtensorModule, Test, add_dynamic_network, new_test_ext, run_to_block,
65
};
@@ -10,6 +9,7 @@ use crate::{
109
StakingColdkeys, StakingColdkeysByIndex, SubnetAlphaIn, SubnetMechanism, SubnetMovingPrice,
1110
SubnetTAO, SubnetTaoFlow, SubtokenEnabled, Tempo, pallet,
1211
};
12+
use crate::{RootAlphaDividendsPerSubnet, SubnetRootClaimKeep, SubnetRootClaimSwap};
1313
use crate::{RootClaimType, RootClaimTypeEnum, RootClaimed};
1414
use approx::assert_abs_diff_eq;
1515
use frame_support::dispatch::RawOrigin;
@@ -1816,3 +1816,176 @@ fn test_claim_root_keep_subnets_swap_claim_type() {
18161816
);
18171817
});
18181818
}
1819+
1820+
#[test]
1821+
fn test_claim_root_subnet_root_claim_map_keep() {
1822+
new_test_ext(1).execute_with(|| {
1823+
let owner_coldkey = U256::from(1001);
1824+
let hotkey = U256::from(1002);
1825+
let coldkey = U256::from(1003);
1826+
let netuid = add_dynamic_network(&hotkey, &owner_coldkey);
1827+
1828+
SubtensorModule::set_tao_weight(u64::MAX); // Set TAO weight to 1.0
1829+
1830+
let root_stake = 2_000_000u64;
1831+
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
1832+
&hotkey,
1833+
&coldkey,
1834+
NetUid::ROOT,
1835+
root_stake.into(),
1836+
);
1837+
1838+
let initial_total_hotkey_alpha = 10_000_000u64;
1839+
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
1840+
&hotkey,
1841+
&owner_coldkey,
1842+
netuid,
1843+
initial_total_hotkey_alpha.into(),
1844+
);
1845+
1846+
// Distribute pending root alpha
1847+
1848+
let pending_root_alpha = 1_000_000u64;
1849+
SubtensorModule::distribute_emission(
1850+
netuid,
1851+
AlphaCurrency::ZERO,
1852+
AlphaCurrency::ZERO,
1853+
pending_root_alpha.into(),
1854+
AlphaCurrency::ZERO,
1855+
);
1856+
1857+
// Claim root alpha
1858+
1859+
assert_ok!(SubtensorModule::set_root_claim_type(
1860+
RuntimeOrigin::signed(coldkey),
1861+
RootClaimTypeEnum::Keep
1862+
),);
1863+
assert_eq!(RootClaimType::<Test>::get(coldkey), RootClaimTypeEnum::Keep);
1864+
assert!(!SubnetRootClaimKeep::<Test>::contains_key(netuid));
1865+
1866+
assert_ok!(SubtensorModule::claim_root(
1867+
RuntimeOrigin::signed(coldkey),
1868+
BTreeSet::from([netuid])
1869+
));
1870+
1871+
// Check SubnetRootClaimKeep
1872+
1873+
let new_stake =
1874+
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey, &coldkey, netuid);
1875+
1876+
assert_eq!(new_stake, SubnetRootClaimKeep::<Test>::get(netuid),);
1877+
1878+
// Check SubnetRootClaimKeep after network deregistration
1879+
1880+
assert_ok!(SubtensorModule::do_dissolve_network(netuid));
1881+
assert!(!SubnetRootClaimKeep::<Test>::contains_key(netuid));
1882+
});
1883+
}
1884+
1885+
#[test]
1886+
fn test_claim_root_subnet_root_claim_map_swap() {
1887+
new_test_ext(1).execute_with(|| {
1888+
let owner_coldkey = U256::from(1001);
1889+
let other_coldkey = U256::from(10010);
1890+
let hotkey = U256::from(1002);
1891+
let coldkey = U256::from(1003);
1892+
let netuid = add_dynamic_network(&hotkey, &owner_coldkey);
1893+
1894+
SubtensorModule::set_tao_weight(u64::MAX); // Set TAO weight to 1.0
1895+
SubnetMechanism::<Test>::insert(netuid, 1);
1896+
1897+
let tao_reserve = TaoCurrency::from(50_000_000_000);
1898+
let alpha_in = AlphaCurrency::from(100_000_000_000);
1899+
SubnetTAO::<Test>::insert(netuid, tao_reserve);
1900+
SubnetAlphaIn::<Test>::insert(netuid, alpha_in);
1901+
let current_price =
1902+
<Test as pallet::Config>::SwapInterface::current_alpha_price(netuid.into())
1903+
.saturating_to_num::<f64>();
1904+
assert_eq!(current_price, 0.5f64);
1905+
1906+
let root_stake = 2_000_000u64;
1907+
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
1908+
&hotkey,
1909+
&coldkey,
1910+
NetUid::ROOT,
1911+
root_stake.into(),
1912+
);
1913+
let root_stake_rate = 0.1f64;
1914+
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
1915+
&hotkey,
1916+
&other_coldkey,
1917+
NetUid::ROOT,
1918+
(9 * root_stake).into(),
1919+
);
1920+
1921+
let initial_total_hotkey_alpha = 10_000_000u64;
1922+
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
1923+
&hotkey,
1924+
&owner_coldkey,
1925+
netuid,
1926+
initial_total_hotkey_alpha.into(),
1927+
);
1928+
1929+
// Distribute pending root alpha
1930+
1931+
let pending_root_alpha = 10_000_000u64;
1932+
SubtensorModule::distribute_emission(
1933+
netuid,
1934+
AlphaCurrency::ZERO,
1935+
AlphaCurrency::ZERO,
1936+
pending_root_alpha.into(),
1937+
AlphaCurrency::ZERO,
1938+
);
1939+
1940+
// Claim root alpha
1941+
1942+
let validator_take_percent = 0.18f64;
1943+
1944+
assert_ok!(SubtensorModule::set_root_claim_type(
1945+
RuntimeOrigin::signed(coldkey),
1946+
RootClaimTypeEnum::Swap
1947+
),);
1948+
assert_eq!(RootClaimType::<Test>::get(coldkey), RootClaimTypeEnum::Swap);
1949+
assert!(!SubnetRootClaimSwap::<Test>::contains_key(netuid));
1950+
1951+
assert_ok!(SubtensorModule::claim_root(
1952+
RuntimeOrigin::signed(coldkey),
1953+
BTreeSet::from([netuid])
1954+
));
1955+
1956+
// Check new stake
1957+
1958+
let new_stake: u64 = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
1959+
&hotkey,
1960+
&coldkey,
1961+
NetUid::ROOT,
1962+
)
1963+
.into();
1964+
1965+
let estimated_stake_increment = (pending_root_alpha as f64)
1966+
* (1f64 - validator_take_percent)
1967+
* current_price
1968+
* root_stake_rate;
1969+
1970+
assert_abs_diff_eq!(
1971+
new_stake,
1972+
root_stake + estimated_stake_increment as u64,
1973+
epsilon = 10000u64,
1974+
);
1975+
1976+
// Check SubnetRootClaimSwap
1977+
1978+
let saved_swapped_alpha: u64 = SubnetRootClaimSwap::<Test>::get(netuid).into();
1979+
1980+
assert_abs_diff_eq!(
1981+
estimated_stake_increment as f64 / current_price,
1982+
saved_swapped_alpha as f64,
1983+
epsilon = 10f64,
1984+
);
1985+
1986+
// Check SubnetRootClaimSwap after network deregistration
1987+
1988+
assert_ok!(SubtensorModule::do_dissolve_network(netuid));
1989+
assert!(!SubnetRootClaimSwap::<Test>::contains_key(netuid));
1990+
});
1991+
}

0 commit comments

Comments
 (0)