Skip to content

Commit bf1933c

Browse files
committed
update staking priority
1 parent fd96f59 commit bf1933c

File tree

2 files changed

+145
-82
lines changed

2 files changed

+145
-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: priority,
17541783
..Default::default()
17551784
})
17561785
}
@@ -1886,14 +1915,17 @@ where
18861915
amount_staked,
18871916
}) => {
18881917
// Fully validate the user input
1889-
Self::result_to_validity(Pallet::<T>::validate_add_stake(
1890-
who,
1891-
hotkey,
1892-
*netuid,
1893-
*amount_staked,
1894-
*amount_staked,
1895-
false,
1896-
))
1918+
Self::result_to_validity(
1919+
Pallet::<T>::validate_add_stake(
1920+
who,
1921+
hotkey,
1922+
*netuid,
1923+
*amount_staked,
1924+
*amount_staked,
1925+
false,
1926+
),
1927+
Self::get_priority_staking(who, hotkey),
1928+
)
18971929
}
18981930
Some(Call::add_stake_limit {
18991931
hotkey,
@@ -1906,29 +1938,35 @@ where
19061938
let max_amount = Pallet::<T>::get_max_amount_add(*netuid, *limit_price);
19071939

19081940
// Fully validate the user input
1909-
Self::result_to_validity(Pallet::<T>::validate_add_stake(
1910-
who,
1911-
hotkey,
1912-
*netuid,
1913-
*amount_staked,
1914-
max_amount,
1915-
*allow_partial,
1916-
))
1941+
Self::result_to_validity(
1942+
Pallet::<T>::validate_add_stake(
1943+
who,
1944+
hotkey,
1945+
*netuid,
1946+
*amount_staked,
1947+
max_amount,
1948+
*allow_partial,
1949+
),
1950+
Self::get_priority_vanilla(),
1951+
)
19171952
}
19181953
Some(Call::remove_stake {
19191954
hotkey,
19201955
netuid,
19211956
amount_unstaked,
19221957
}) => {
19231958
// Fully validate the user input
1924-
Self::result_to_validity(Pallet::<T>::validate_remove_stake(
1925-
who,
1926-
hotkey,
1927-
*netuid,
1928-
*amount_unstaked,
1929-
*amount_unstaked,
1930-
false,
1931-
))
1959+
Self::result_to_validity(
1960+
Pallet::<T>::validate_remove_stake(
1961+
who,
1962+
hotkey,
1963+
*netuid,
1964+
*amount_unstaked,
1965+
*amount_unstaked,
1966+
false,
1967+
),
1968+
Self::get_priority_staking(who, hotkey),
1969+
)
19321970
}
19331971
Some(Call::remove_stake_limit {
19341972
hotkey,
@@ -1941,14 +1979,17 @@ where
19411979
let max_amount = Pallet::<T>::get_max_amount_remove(*netuid, *limit_price);
19421980

19431981
// Fully validate the user input
1944-
Self::result_to_validity(Pallet::<T>::validate_remove_stake(
1945-
who,
1946-
hotkey,
1947-
*netuid,
1948-
*amount_unstaked,
1949-
max_amount,
1950-
*allow_partial,
1951-
))
1982+
Self::result_to_validity(
1983+
Pallet::<T>::validate_remove_stake(
1984+
who,
1985+
hotkey,
1986+
*netuid,
1987+
*amount_unstaked,
1988+
max_amount,
1989+
*allow_partial,
1990+
),
1991+
Self::get_priority_vanilla(),
1992+
)
19521993
}
19531994
Some(Call::move_stake {
19541995
origin_hotkey,
@@ -1958,18 +1999,21 @@ where
19581999
alpha_amount,
19592000
}) => {
19602001
// Fully validate the user input
1961-
Self::result_to_validity(Pallet::<T>::validate_stake_transition(
1962-
who,
1963-
who,
1964-
origin_hotkey,
1965-
destination_hotkey,
1966-
*origin_netuid,
1967-
*destination_netuid,
1968-
*alpha_amount,
1969-
*alpha_amount,
1970-
None,
1971-
false,
1972-
))
2002+
Self::result_to_validity(
2003+
Pallet::<T>::validate_stake_transition(
2004+
who,
2005+
who,
2006+
origin_hotkey,
2007+
destination_hotkey,
2008+
*origin_netuid,
2009+
*destination_netuid,
2010+
*alpha_amount,
2011+
*alpha_amount,
2012+
None,
2013+
false,
2014+
),
2015+
Self::get_priority_vanilla(),
2016+
)
19732017
}
19742018
Some(Call::transfer_stake {
19752019
destination_coldkey,
@@ -1979,18 +2023,21 @@ where
19792023
alpha_amount,
19802024
}) => {
19812025
// Fully validate the user input
1982-
Self::result_to_validity(Pallet::<T>::validate_stake_transition(
1983-
who,
1984-
destination_coldkey,
1985-
hotkey,
1986-
hotkey,
1987-
*origin_netuid,
1988-
*destination_netuid,
1989-
*alpha_amount,
1990-
*alpha_amount,
1991-
None,
1992-
true,
1993-
))
2026+
Self::result_to_validity(
2027+
Pallet::<T>::validate_stake_transition(
2028+
who,
2029+
destination_coldkey,
2030+
hotkey,
2031+
hotkey,
2032+
*origin_netuid,
2033+
*destination_netuid,
2034+
*alpha_amount,
2035+
*alpha_amount,
2036+
None,
2037+
true,
2038+
),
2039+
Self::get_priority_vanilla(),
2040+
)
19942041
}
19952042
Some(Call::swap_stake {
19962043
hotkey,
@@ -1999,18 +2046,21 @@ where
19992046
alpha_amount,
20002047
}) => {
20012048
// Fully validate the user input
2002-
Self::result_to_validity(Pallet::<T>::validate_stake_transition(
2003-
who,
2004-
who,
2005-
hotkey,
2006-
hotkey,
2007-
*origin_netuid,
2008-
*destination_netuid,
2009-
*alpha_amount,
2010-
*alpha_amount,
2011-
None,
2012-
false,
2013-
))
2049+
Self::result_to_validity(
2050+
Pallet::<T>::validate_stake_transition(
2051+
who,
2052+
who,
2053+
hotkey,
2054+
hotkey,
2055+
*origin_netuid,
2056+
*destination_netuid,
2057+
*alpha_amount,
2058+
*alpha_amount,
2059+
None,
2060+
false,
2061+
),
2062+
Self::get_priority_vanilla(),
2063+
)
20142064
}
20152065
Some(Call::swap_stake_limit {
20162066
hotkey,
@@ -2028,18 +2078,21 @@ where
20282078
);
20292079

20302080
// Fully validate the user input
2031-
Self::result_to_validity(Pallet::<T>::validate_stake_transition(
2032-
who,
2033-
who,
2034-
hotkey,
2035-
hotkey,
2036-
*origin_netuid,
2037-
*destination_netuid,
2038-
*alpha_amount,
2039-
max_amount,
2040-
Some(*allow_partial),
2041-
false,
2042-
))
2081+
Self::result_to_validity(
2082+
Pallet::<T>::validate_stake_transition(
2083+
who,
2084+
who,
2085+
hotkey,
2086+
hotkey,
2087+
*origin_netuid,
2088+
*destination_netuid,
2089+
*alpha_amount,
2090+
max_amount,
2091+
Some(*allow_partial),
2092+
false,
2093+
),
2094+
Self::get_priority_vanilla(),
2095+
)
20432096
}
20442097
Some(Call::register { netuid, .. } | Call::burned_register { netuid, .. }) => {
20452098
let registrations_this_interval =

pallets/subtensor/src/staking/stake_utils.rs

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

752757
// Step 5. Deposit and log the unstaking event.
753758
Self::deposit_event(Event::StakeRemoved(
@@ -807,6 +812,11 @@ impl<T: Config> Pallet<T> {
807812
TotalStake::<T>::mutate(|total| {
808813
*total = total.saturating_add(actual_fee);
809814
});
815+
LastColdkeyHotkeyStakeBlock::<T>::insert(
816+
&coldkey,
817+
&hotkey,
818+
Self::get_current_block_as_u64(),
819+
);
810820

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

0 commit comments

Comments
 (0)