Skip to content

Commit d65dbae

Browse files
authored
Merge pull request #2331 from opentensor/hotfix-1-5-2026
taoflow hotfix
2 parents 575262e + 827933f commit d65dbae

File tree

3 files changed

+247
-1
lines changed

3 files changed

+247
-1
lines changed

pallets/subtensor/src/staking/stake_utils.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,13 @@ impl<T: Config> Pallet<T> {
862862
netuid,
863863
alpha,
864864
);
865+
if netuid == NetUid::ROOT {
866+
Self::remove_stake_adjust_root_claimed_for_hotkey_and_coldkey(
867+
origin_hotkey,
868+
origin_coldkey,
869+
alpha,
870+
);
871+
}
865872

866873
// Increase alpha on destination keys
867874
let actual_alpha_moved = Self::increase_stake_for_hotkey_and_coldkey_on_subnet(
@@ -870,6 +877,13 @@ impl<T: Config> Pallet<T> {
870877
netuid,
871878
actual_alpha_decrease,
872879
);
880+
if netuid == NetUid::ROOT {
881+
Self::add_stake_adjust_root_claimed_for_hotkey_and_coldkey(
882+
destination_hotkey,
883+
destination_coldkey,
884+
actual_alpha_decrease.into(),
885+
);
886+
}
873887

874888
// Calculate TAO equivalent based on current price (it is accurate because
875889
// there's no slippage in this move)

pallets/subtensor/src/tests/claim_root.rs

Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1825,3 +1825,235 @@ fn test_claim_root_default_mode_keep() {
18251825
assert_eq!(RootClaimType::<Test>::get(coldkey), RootClaimTypeEnum::Swap);
18261826
});
18271827
}
1828+
1829+
#[test]
1830+
fn test_claim_root_with_moved_stake() {
1831+
new_test_ext(1).execute_with(|| {
1832+
let owner_coldkey = U256::from(1001);
1833+
let hotkey = U256::from(1002);
1834+
let alice_coldkey = U256::from(1003);
1835+
let bob_coldkey = U256::from(1004);
1836+
let eve_coldkey = U256::from(1005);
1837+
let netuid = add_dynamic_network(&hotkey, &owner_coldkey);
1838+
1839+
SubtensorModule::set_tao_weight(u64::MAX); // Set TAO weight to 1.0
1840+
SubtokenEnabled::<Test>::insert(NetUid::ROOT, true);
1841+
NetworksAdded::<Test>::insert(NetUid::ROOT, true);
1842+
1843+
let root_stake = 8_000_000u64;
1844+
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
1845+
&hotkey,
1846+
&alice_coldkey,
1847+
NetUid::ROOT,
1848+
root_stake.into(),
1849+
);
1850+
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
1851+
&hotkey,
1852+
&bob_coldkey,
1853+
NetUid::ROOT,
1854+
root_stake.into(),
1855+
);
1856+
1857+
let initial_total_hotkey_alpha = 10_000_000u64;
1858+
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
1859+
&hotkey,
1860+
&owner_coldkey,
1861+
netuid,
1862+
initial_total_hotkey_alpha.into(),
1863+
);
1864+
1865+
// Claim root alpha
1866+
1867+
assert_ok!(SubtensorModule::set_root_claim_type(
1868+
RuntimeOrigin::signed(alice_coldkey),
1869+
RootClaimTypeEnum::Keep
1870+
),);
1871+
assert_ok!(SubtensorModule::set_root_claim_type(
1872+
RuntimeOrigin::signed(bob_coldkey),
1873+
RootClaimTypeEnum::Keep
1874+
),);
1875+
1876+
assert_ok!(SubtensorModule::set_root_claim_type(
1877+
RuntimeOrigin::signed(eve_coldkey),
1878+
RootClaimTypeEnum::Keep
1879+
),);
1880+
1881+
// Distribute pending root alpha
1882+
1883+
let pending_root_alpha = 10_000_000u64;
1884+
SubtensorModule::distribute_emission(
1885+
netuid,
1886+
AlphaCurrency::ZERO,
1887+
AlphaCurrency::ZERO,
1888+
pending_root_alpha.into(),
1889+
AlphaCurrency::ZERO,
1890+
);
1891+
1892+
assert_ok!(SubtensorModule::claim_root(
1893+
RuntimeOrigin::signed(alice_coldkey),
1894+
BTreeSet::from([netuid])
1895+
));
1896+
assert_ok!(SubtensorModule::claim_root(
1897+
RuntimeOrigin::signed(bob_coldkey),
1898+
BTreeSet::from([netuid])
1899+
));
1900+
1901+
// Check stakes
1902+
let validator_take_percent = 0.18f64;
1903+
1904+
let alice_stake: u64 = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
1905+
&hotkey,
1906+
&alice_coldkey,
1907+
netuid,
1908+
)
1909+
.into();
1910+
1911+
let bob_stake: u64 = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
1912+
&hotkey,
1913+
&bob_coldkey,
1914+
netuid,
1915+
)
1916+
.into();
1917+
1918+
let estimated_stake = (pending_root_alpha as f64) * (1f64 - validator_take_percent) / 2f64;
1919+
1920+
assert_eq!(alice_stake, bob_stake);
1921+
1922+
assert_abs_diff_eq!(alice_stake, estimated_stake as u64, epsilon = 100u64,);
1923+
1924+
// Distribute pending root alpha
1925+
1926+
let pending_root_alpha = 10_000_000u64;
1927+
SubtensorModule::distribute_emission(
1928+
netuid,
1929+
AlphaCurrency::ZERO,
1930+
AlphaCurrency::ZERO,
1931+
pending_root_alpha.into(),
1932+
AlphaCurrency::ZERO,
1933+
);
1934+
1935+
// Transfer stake to other coldkey
1936+
let stake_decrement = root_stake / 2u64;
1937+
1938+
assert_ok!(SubtensorModule::transfer_stake(
1939+
RuntimeOrigin::signed(bob_coldkey,),
1940+
eve_coldkey,
1941+
hotkey,
1942+
NetUid::ROOT,
1943+
NetUid::ROOT,
1944+
stake_decrement.into(),
1945+
));
1946+
1947+
let eve_stake: u64 = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
1948+
&hotkey,
1949+
&eve_coldkey,
1950+
netuid,
1951+
)
1952+
.into();
1953+
1954+
assert_ok!(SubtensorModule::claim_root(
1955+
RuntimeOrigin::signed(alice_coldkey),
1956+
BTreeSet::from([netuid])
1957+
));
1958+
assert_ok!(SubtensorModule::claim_root(
1959+
RuntimeOrigin::signed(bob_coldkey),
1960+
BTreeSet::from([netuid])
1961+
));
1962+
1963+
assert_ok!(SubtensorModule::claim_root(
1964+
RuntimeOrigin::signed(eve_coldkey),
1965+
BTreeSet::from([netuid])
1966+
));
1967+
1968+
// Check new stakes
1969+
1970+
let alice_stake2: u64 = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
1971+
&hotkey,
1972+
&alice_coldkey,
1973+
netuid,
1974+
)
1975+
.into();
1976+
1977+
let bob_stake2: u64 = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
1978+
&hotkey,
1979+
&bob_coldkey,
1980+
netuid,
1981+
)
1982+
.into();
1983+
1984+
let eve_stake2: u64 = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
1985+
&hotkey,
1986+
&eve_coldkey,
1987+
netuid,
1988+
)
1989+
.into();
1990+
1991+
// Eve should not have gotten any root claim
1992+
let eve_stake_diff = eve_stake2 - eve_stake;
1993+
assert_abs_diff_eq!(eve_stake_diff, 0, epsilon = 100u64,);
1994+
1995+
let estimated_stake = (pending_root_alpha as f64) * (1f64 - validator_take_percent) / 2f64;
1996+
1997+
let alice_stake_diff = alice_stake2 - alice_stake;
1998+
let bob_stake_diff = bob_stake2 - bob_stake;
1999+
2000+
assert_abs_diff_eq!(alice_stake_diff, bob_stake_diff, epsilon = 100u64,);
2001+
assert_abs_diff_eq!(bob_stake_diff, estimated_stake as u64, epsilon = 100u64,);
2002+
2003+
// Transfer stake back
2004+
let stake_increment = stake_decrement;
2005+
2006+
assert_ok!(SubtensorModule::transfer_stake(
2007+
RuntimeOrigin::signed(eve_coldkey,),
2008+
bob_coldkey,
2009+
hotkey,
2010+
NetUid::ROOT,
2011+
NetUid::ROOT,
2012+
stake_increment.into(),
2013+
));
2014+
2015+
// Distribute pending root alpha
2016+
2017+
let pending_root_alpha = 10_000_000u64;
2018+
SubtensorModule::distribute_emission(
2019+
netuid,
2020+
AlphaCurrency::ZERO,
2021+
AlphaCurrency::ZERO,
2022+
pending_root_alpha.into(),
2023+
AlphaCurrency::ZERO,
2024+
);
2025+
2026+
assert_ok!(SubtensorModule::claim_root(
2027+
RuntimeOrigin::signed(alice_coldkey),
2028+
BTreeSet::from([netuid])
2029+
));
2030+
assert_ok!(SubtensorModule::claim_root(
2031+
RuntimeOrigin::signed(bob_coldkey),
2032+
BTreeSet::from([netuid])
2033+
));
2034+
2035+
// Check new stakes
2036+
2037+
let alice_stake3: u64 = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
2038+
&hotkey,
2039+
&alice_coldkey,
2040+
netuid,
2041+
)
2042+
.into();
2043+
2044+
let bob_stake3: u64 = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
2045+
&hotkey,
2046+
&bob_coldkey,
2047+
netuid,
2048+
)
2049+
.into();
2050+
2051+
let estimated_stake = (pending_root_alpha as f64) * (1f64 - validator_take_percent) / 2f64;
2052+
2053+
let alice_stake_diff2 = alice_stake3 - alice_stake2;
2054+
let bob_stake_diff2 = bob_stake3 - bob_stake2;
2055+
2056+
assert_abs_diff_eq!(alice_stake_diff2, bob_stake_diff2, epsilon = 100u64,);
2057+
assert_abs_diff_eq!(bob_stake_diff2, estimated_stake as u64, epsilon = 100u64,);
2058+
});
2059+
}

runtime/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
241241
// `spec_version`, and `authoring_version` are the same between Wasm and native.
242242
// This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use
243243
// the compatible custom types.
244-
spec_version: 365,
244+
spec_version: 366,
245245
impl_version: 1,
246246
apis: RUNTIME_API_VERSIONS,
247247
transaction_version: 1,

0 commit comments

Comments
 (0)