Skip to content

Commit 85c71ac

Browse files
authored
Merge pull request #1281 from opentensor/update-staking-priority
update staking priority
2 parents 8356aa9 + 1916e1c commit 85c71ac

File tree

2 files changed

+137
-82
lines changed

2 files changed

+137
-82
lines changed

pallets/subtensor/src/lib.rs

Lines changed: 135 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1550,6 +1550,18 @@ pub mod pallet {
15501550
pub type RevealPeriodEpochs<T: Config> =
15511551
StorageMap<_, Twox64Concat, u16, u64, ValueQuery, DefaultRevealPeriodEpochs<T>>;
15521552

1553+
#[pallet::storage]
1554+
/// --- Map (coldkey, hotkey) --> u64 the last block at which stake was added/removed.
1555+
pub type LastColdkeyHotkeyStakeBlock<T: Config> = StorageDoubleMap<
1556+
_,
1557+
Twox64Concat,
1558+
T::AccountId,
1559+
Twox64Concat,
1560+
T::AccountId,
1561+
u64,
1562+
OptionQuery,
1563+
>;
1564+
15531565
/// ==================
15541566
/// ==== Genesis =====
15551567
/// ==================
@@ -1588,6 +1600,19 @@ pub mod pallet {
15881600
0
15891601
}
15901602

1603+
/// Returns the transaction priority for stake operations.
1604+
pub fn get_priority_staking(coldkey: &T::AccountId, hotkey: &T::AccountId) -> u64 {
1605+
match LastColdkeyHotkeyStakeBlock::<T>::get(coldkey, hotkey) {
1606+
Some(last_stake_block) => {
1607+
let current_block_number = Self::get_current_block_as_u64();
1608+
let default_priority = current_block_number.saturating_sub(last_stake_block);
1609+
1610+
default_priority.saturating_add(u32::MAX as u64)
1611+
}
1612+
None => 0,
1613+
}
1614+
}
1615+
15911616
/// Is the caller allowed to set weights
15921617
pub fn check_weights_min_stake(hotkey: &T::AccountId, netuid: u16) -> bool {
15931618
// Blacklist weights transactions for low stake peers.
@@ -1705,11 +1730,15 @@ where
17051730
Pallet::<T>::get_priority_set_weights(who, netuid)
17061731
}
17071732

1733+
pub fn get_priority_staking(coldkey: &T::AccountId, hotkey: &T::AccountId) -> u64 {
1734+
Pallet::<T>::get_priority_staking(coldkey, hotkey)
1735+
}
1736+
17081737
pub fn check_weights_min_stake(who: &T::AccountId, netuid: u16) -> bool {
17091738
Pallet::<T>::check_weights_min_stake(who, netuid)
17101739
}
17111740

1712-
pub fn result_to_validity(result: Result<(), Error<T>>) -> TransactionValidity {
1741+
pub fn result_to_validity(result: Result<(), Error<T>>, priority: u64) -> TransactionValidity {
17131742
if let Err(err) = result {
17141743
match err {
17151744
Error::<T>::AmountTooLow => Err(InvalidTransaction::Custom(
@@ -1750,7 +1779,7 @@ where
17501779
}
17511780
} else {
17521781
Ok(ValidTransaction {
1753-
priority: Self::get_priority_vanilla(),
1782+
priority,
17541783
..Default::default()
17551784
})
17561785
}
@@ -1892,14 +1921,17 @@ where
18921921
.into();
18931922
}
18941923
// Fully validate the user input
1895-
Self::result_to_validity(Pallet::<T>::validate_add_stake(
1896-
who,
1897-
hotkey,
1898-
*netuid,
1899-
*amount_staked,
1900-
*amount_staked,
1901-
false,
1902-
))
1924+
Self::result_to_validity(
1925+
Pallet::<T>::validate_add_stake(
1926+
who,
1927+
hotkey,
1928+
*netuid,
1929+
*amount_staked,
1930+
*amount_staked,
1931+
false,
1932+
),
1933+
Self::get_priority_staking(who, hotkey),
1934+
)
19031935
}
19041936
Some(Call::add_stake_limit {
19051937
hotkey,
@@ -1919,29 +1951,35 @@ where
19191951
let max_amount = Pallet::<T>::get_max_amount_add(*netuid, *limit_price);
19201952

19211953
// Fully validate the user input
1922-
Self::result_to_validity(Pallet::<T>::validate_add_stake(
1923-
who,
1924-
hotkey,
1925-
*netuid,
1926-
*amount_staked,
1927-
max_amount,
1928-
*allow_partial,
1929-
))
1954+
Self::result_to_validity(
1955+
Pallet::<T>::validate_add_stake(
1956+
who,
1957+
hotkey,
1958+
*netuid,
1959+
*amount_staked,
1960+
max_amount,
1961+
*allow_partial,
1962+
),
1963+
Self::get_priority_staking(who, hotkey),
1964+
)
19301965
}
19311966
Some(Call::remove_stake {
19321967
hotkey,
19331968
netuid,
19341969
amount_unstaked,
19351970
}) => {
19361971
// Fully validate the user input
1937-
Self::result_to_validity(Pallet::<T>::validate_remove_stake(
1938-
who,
1939-
hotkey,
1940-
*netuid,
1941-
*amount_unstaked,
1942-
*amount_unstaked,
1943-
false,
1944-
))
1972+
Self::result_to_validity(
1973+
Pallet::<T>::validate_remove_stake(
1974+
who,
1975+
hotkey,
1976+
*netuid,
1977+
*amount_unstaked,
1978+
*amount_unstaked,
1979+
false,
1980+
),
1981+
Self::get_priority_staking(who, hotkey),
1982+
)
19451983
}
19461984
Some(Call::remove_stake_limit {
19471985
hotkey,
@@ -1954,14 +1992,17 @@ where
19541992
let max_amount = Pallet::<T>::get_max_amount_remove(*netuid, *limit_price);
19551993

19561994
// Fully validate the user input
1957-
Self::result_to_validity(Pallet::<T>::validate_remove_stake(
1958-
who,
1959-
hotkey,
1960-
*netuid,
1961-
*amount_unstaked,
1962-
max_amount,
1963-
*allow_partial,
1964-
))
1995+
Self::result_to_validity(
1996+
Pallet::<T>::validate_remove_stake(
1997+
who,
1998+
hotkey,
1999+
*netuid,
2000+
*amount_unstaked,
2001+
max_amount,
2002+
*allow_partial,
2003+
),
2004+
Self::get_priority_staking(who, hotkey),
2005+
)
19652006
}
19662007
Some(Call::move_stake {
19672008
origin_hotkey,
@@ -1978,18 +2019,21 @@ where
19782019
}
19792020

19802021
// Fully validate the user input
1981-
Self::result_to_validity(Pallet::<T>::validate_stake_transition(
1982-
who,
1983-
who,
1984-
origin_hotkey,
1985-
destination_hotkey,
1986-
*origin_netuid,
1987-
*destination_netuid,
1988-
*alpha_amount,
1989-
*alpha_amount,
1990-
None,
1991-
false,
1992-
))
2022+
Self::result_to_validity(
2023+
Pallet::<T>::validate_stake_transition(
2024+
who,
2025+
who,
2026+
origin_hotkey,
2027+
destination_hotkey,
2028+
*origin_netuid,
2029+
*destination_netuid,
2030+
*alpha_amount,
2031+
*alpha_amount,
2032+
None,
2033+
false,
2034+
),
2035+
Self::get_priority_staking(who, origin_hotkey),
2036+
)
19932037
}
19942038
Some(Call::transfer_stake {
19952039
destination_coldkey,
@@ -2006,18 +2050,21 @@ where
20062050
}
20072051

20082052
// Fully validate the user input
2009-
Self::result_to_validity(Pallet::<T>::validate_stake_transition(
2010-
who,
2011-
destination_coldkey,
2012-
hotkey,
2013-
hotkey,
2014-
*origin_netuid,
2015-
*destination_netuid,
2016-
*alpha_amount,
2017-
*alpha_amount,
2018-
None,
2019-
true,
2020-
))
2053+
Self::result_to_validity(
2054+
Pallet::<T>::validate_stake_transition(
2055+
who,
2056+
destination_coldkey,
2057+
hotkey,
2058+
hotkey,
2059+
*origin_netuid,
2060+
*destination_netuid,
2061+
*alpha_amount,
2062+
*alpha_amount,
2063+
None,
2064+
true,
2065+
),
2066+
Self::get_priority_staking(who, hotkey),
2067+
)
20212068
}
20222069
Some(Call::swap_stake {
20232070
hotkey,
@@ -2033,18 +2080,21 @@ where
20332080
}
20342081

20352082
// Fully validate the user input
2036-
Self::result_to_validity(Pallet::<T>::validate_stake_transition(
2037-
who,
2038-
who,
2039-
hotkey,
2040-
hotkey,
2041-
*origin_netuid,
2042-
*destination_netuid,
2043-
*alpha_amount,
2044-
*alpha_amount,
2045-
None,
2046-
false,
2047-
))
2083+
Self::result_to_validity(
2084+
Pallet::<T>::validate_stake_transition(
2085+
who,
2086+
who,
2087+
hotkey,
2088+
hotkey,
2089+
*origin_netuid,
2090+
*destination_netuid,
2091+
*alpha_amount,
2092+
*alpha_amount,
2093+
None,
2094+
false,
2095+
),
2096+
Self::get_priority_staking(who, hotkey),
2097+
)
20482098
}
20492099
Some(Call::swap_stake_limit {
20502100
hotkey,
@@ -2069,18 +2119,21 @@ where
20692119
);
20702120

20712121
// Fully validate the user input
2072-
Self::result_to_validity(Pallet::<T>::validate_stake_transition(
2073-
who,
2074-
who,
2075-
hotkey,
2076-
hotkey,
2077-
*origin_netuid,
2078-
*destination_netuid,
2079-
*alpha_amount,
2080-
max_amount,
2081-
Some(*allow_partial),
2082-
false,
2083-
))
2122+
Self::result_to_validity(
2123+
Pallet::<T>::validate_stake_transition(
2124+
who,
2125+
who,
2126+
hotkey,
2127+
hotkey,
2128+
*origin_netuid,
2129+
*destination_netuid,
2130+
*alpha_amount,
2131+
max_amount,
2132+
Some(*allow_partial),
2133+
false,
2134+
),
2135+
Self::get_priority_staking(who, hotkey),
2136+
)
20842137
}
20852138
Some(Call::register { netuid, .. } | Call::burned_register { netuid, .. }) => {
20862139
if ColdkeySwapScheduled::<T>::contains_key(who) {

pallets/subtensor/src/staking/stake_utils.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,7 @@ impl<T: Config> Pallet<T> {
748748
TotalStake::<T>::mutate(|total| {
749749
*total = total.saturating_add(actual_fee);
750750
});
751+
LastColdkeyHotkeyStakeBlock::<T>::insert(coldkey, hotkey, Self::get_current_block_as_u64());
751752

752753
// Step 5. Deposit and log the unstaking event.
753754
Self::deposit_event(Event::StakeRemoved(
@@ -807,6 +808,7 @@ impl<T: Config> Pallet<T> {
807808
TotalStake::<T>::mutate(|total| {
808809
*total = total.saturating_add(actual_fee);
809810
});
811+
LastColdkeyHotkeyStakeBlock::<T>::insert(coldkey, hotkey, Self::get_current_block_as_u64());
810812

811813
// Step 6. Deposit and log the staking event.
812814
Self::deposit_event(Event::StakeAdded(

0 commit comments

Comments
 (0)