@@ -7,6 +7,7 @@ use pallet_rate_limiting::{RateLimitScopeResolver, RateLimitUsageResolver};
77use pallet_subtensor:: { Call as SubtensorCall , Tempo } ;
88use scale_info:: TypeInfo ;
99use serde:: { Deserialize , Serialize } ;
10+ use sp_std:: { vec, vec:: Vec } ;
1011use subtensor_runtime_common:: { BlockNumber , MechId , NetUid } ;
1112
1213use crate :: { AccountId , Runtime , RuntimeCall , RuntimeOrigin } ;
@@ -107,11 +108,15 @@ impl RateLimitScopeResolver<RuntimeOrigin, RuntimeCall, NetUid, BlockNumber> for
107108 }
108109
109110 fn should_bypass ( origin : & RuntimeOrigin , call : & RuntimeCall ) -> BypassDecision {
110- if matches ! ( origin. clone( ) . into( ) , Ok ( RawOrigin :: Root ) ) {
111- return BypassDecision :: bypass_and_skip ( ) ;
112- }
113-
114111 if let RuntimeCall :: SubtensorModule ( inner) = call {
112+ if matches ! ( origin. clone( ) . into( ) , Ok ( RawOrigin :: Root ) ) {
113+ // swap_coldkey should record last-seen but never fail; other root calls skip.
114+ if matches ! ( inner, SubtensorCall :: swap_coldkey { .. } ) {
115+ return BypassDecision :: bypass_and_record ( ) ;
116+ }
117+ return BypassDecision :: bypass_and_skip ( ) ;
118+ }
119+
115120 match inner {
116121 SubtensorCall :: set_childkey_take {
117122 hotkey,
@@ -129,7 +134,8 @@ impl RateLimitScopeResolver<RuntimeOrigin, RuntimeCall, NetUid, BlockNumber> for
129134 }
130135 SubtensorCall :: add_stake { .. }
131136 | SubtensorCall :: add_stake_limit { .. }
132- | SubtensorCall :: decrease_take { .. } => {
137+ | SubtensorCall :: decrease_take { .. }
138+ | SubtensorCall :: swap_coldkey { .. } => {
133139 return BypassDecision :: bypass_and_record ( ) ;
134140 }
135141 SubtensorCall :: reveal_weights { netuid, .. }
@@ -179,32 +185,48 @@ pub struct UsageResolver;
179185impl RateLimitUsageResolver < RuntimeOrigin , RuntimeCall , RateLimitUsageKey < AccountId > >
180186 for UsageResolver
181187{
182- fn context ( origin : & RuntimeOrigin , call : & RuntimeCall ) -> Option < RateLimitUsageKey < AccountId > > {
188+ fn context (
189+ origin : & RuntimeOrigin ,
190+ call : & RuntimeCall ,
191+ ) -> Option < Vec < RateLimitUsageKey < AccountId > > > {
183192 match call {
184193 RuntimeCall :: SubtensorModule ( inner) => match inner {
185- SubtensorCall :: swap_hotkey { .. } => {
186- signed_origin ( origin) . map ( RateLimitUsageKey :: < AccountId > :: Account )
194+ SubtensorCall :: swap_coldkey { new_coldkey, .. } => {
195+ Some ( vec ! [ RateLimitUsageKey :: <AccountId >:: Account (
196+ new_coldkey. clone( ) ,
197+ ) ] )
198+ }
199+ SubtensorCall :: swap_hotkey { new_hotkey, .. } => {
200+ // Record against the coldkey (enforcement) and the new hotkey to mirror legacy
201+ // writes.
202+ let coldkey = signed_origin ( origin) ?;
203+ Some ( vec ! [
204+ RateLimitUsageKey :: <AccountId >:: Account ( coldkey) ,
205+ RateLimitUsageKey :: <AccountId >:: Account ( new_hotkey. clone( ) ) ,
206+ ] )
187207 }
188208 SubtensorCall :: increase_take { hotkey, .. } => {
189- Some ( RateLimitUsageKey :: < AccountId > :: Account ( hotkey. clone ( ) ) )
209+ Some ( vec ! [ RateLimitUsageKey :: <AccountId >:: Account (
210+ hotkey. clone( ) ,
211+ ) ] )
190212 }
191213 SubtensorCall :: set_childkey_take { hotkey, netuid, .. }
192214 | SubtensorCall :: set_children { hotkey, netuid, .. } => {
193- Some ( RateLimitUsageKey :: < AccountId > :: AccountSubnet {
215+ Some ( vec ! [ RateLimitUsageKey :: <AccountId >:: AccountSubnet {
194216 account: hotkey. clone( ) ,
195217 netuid: * netuid,
196- } )
218+ } ] )
197219 }
198220 SubtensorCall :: set_weights { netuid, .. }
199221 | SubtensorCall :: commit_weights { netuid, .. }
200222 | SubtensorCall :: reveal_weights { netuid, .. }
201223 | SubtensorCall :: batch_reveal_weights { netuid, .. }
202224 | SubtensorCall :: commit_timelocked_weights { netuid, .. } => {
203225 let ( _, uid) = neuron_identity ( origin, * netuid) ?;
204- Some ( RateLimitUsageKey :: < AccountId > :: SubnetNeuron {
226+ Some ( vec ! [ RateLimitUsageKey :: <AccountId >:: SubnetNeuron {
205227 netuid: * netuid,
206228 uid,
207- } )
229+ } ] )
208230 }
209231 // legacy implementation still used netuid only, but it was recalculating it using
210232 // mecid, so switching to netuid AND mecid is logical here
@@ -214,39 +236,41 @@ impl RateLimitUsageResolver<RuntimeOrigin, RuntimeCall, RateLimitUsageKey<Accoun
214236 | SubtensorCall :: commit_crv3_mechanism_weights { netuid, mecid, .. }
215237 | SubtensorCall :: commit_timelocked_mechanism_weights { netuid, mecid, .. } => {
216238 let ( _, uid) = neuron_identity ( origin, * netuid) ?;
217- Some ( RateLimitUsageKey :: < AccountId > :: SubnetMechanismNeuron {
218- netuid : * netuid,
219- mecid : * mecid,
220- uid,
221- } )
239+ Some ( vec ! [
240+ RateLimitUsageKey :: <AccountId >:: SubnetMechanismNeuron {
241+ netuid: * netuid,
242+ mecid: * mecid,
243+ uid,
244+ } ,
245+ ] )
222246 }
223247 SubtensorCall :: serve_axon { netuid, .. }
224248 | SubtensorCall :: serve_axon_tls { netuid, .. } => {
225249 let hotkey = signed_origin ( origin) ?;
226- Some ( RateLimitUsageKey :: < AccountId > :: AccountSubnetServing {
250+ Some ( vec ! [ RateLimitUsageKey :: <AccountId >:: AccountSubnetServing {
227251 account: hotkey,
228252 netuid: * netuid,
229253 endpoint: ServingEndpoint :: Axon ,
230- } )
254+ } ] )
231255 }
232256 SubtensorCall :: serve_prometheus { netuid, .. } => {
233257 let hotkey = signed_origin ( origin) ?;
234- Some ( RateLimitUsageKey :: < AccountId > :: AccountSubnetServing {
258+ Some ( vec ! [ RateLimitUsageKey :: <AccountId >:: AccountSubnetServing {
235259 account: hotkey,
236260 netuid: * netuid,
237261 endpoint: ServingEndpoint :: Prometheus ,
238- } )
262+ } ] )
239263 }
240264 SubtensorCall :: associate_evm_key { netuid, .. } => {
241265 let hotkey = signed_origin ( origin) ?;
242266 let uid = pallet_subtensor:: Pallet :: < Runtime > :: get_uid_for_net_and_hotkey (
243267 * netuid, & hotkey,
244268 )
245269 . ok ( ) ?;
246- Some ( RateLimitUsageKey :: < AccountId > :: SubnetNeuron {
270+ Some ( vec ! [ RateLimitUsageKey :: <AccountId >:: SubnetNeuron {
247271 netuid: * netuid,
248272 uid,
249- } )
273+ } ] )
250274 }
251275 // Staking calls share a group lock; only add_* write usage, the rest are read-only.
252276 // Keep the usage key granular so the lock applies per (coldkey, hotkey, netuid).
@@ -276,31 +300,31 @@ impl RateLimitUsageResolver<RuntimeOrigin, RuntimeCall, RateLimitUsageKey<Accoun
276300 ..
277301 } => {
278302 let coldkey = signed_origin ( origin) ?;
279- Some ( RateLimitUsageKey :: < AccountId > :: ColdkeyHotkeySubnet {
303+ Some ( vec ! [ RateLimitUsageKey :: <AccountId >:: ColdkeyHotkeySubnet {
280304 coldkey,
281305 hotkey: hotkey. clone( ) ,
282306 netuid: * netuid,
283- } )
307+ } ] )
284308 }
285309 _ => None ,
286310 } ,
287311 RuntimeCall :: AdminUtils ( inner) => {
288312 if let Some ( netuid) = owner_hparam_netuid ( inner) {
289- Some ( RateLimitUsageKey :: < AccountId > :: Subnet ( netuid) )
313+ Some ( vec ! [ RateLimitUsageKey :: <AccountId >:: Subnet ( netuid) ] )
290314 } else {
291315 match inner {
292316 AdminUtilsCall :: sudo_set_sn_owner_hotkey { netuid, .. } => {
293- Some ( RateLimitUsageKey :: < AccountId > :: Subnet ( * netuid) )
317+ Some ( vec ! [ RateLimitUsageKey :: <AccountId >:: Subnet ( * netuid) ] )
294318 }
295319 AdminUtilsCall :: sudo_set_weights_version_key { netuid, .. }
296320 | AdminUtilsCall :: sudo_set_mechanism_count { netuid, .. }
297321 | AdminUtilsCall :: sudo_set_mechanism_emission_split { netuid, .. }
298322 | AdminUtilsCall :: sudo_trim_to_max_allowed_uids { netuid, .. } => {
299323 let who = signed_origin ( origin) ?;
300- Some ( RateLimitUsageKey :: < AccountId > :: AccountSubnet {
324+ Some ( vec ! [ RateLimitUsageKey :: <AccountId >:: AccountSubnet {
301325 account: who,
302326 netuid: * netuid,
303- } )
327+ } ] )
304328 }
305329 _ => None ,
306330 }
0 commit comments