1- use crate :: {
2- BalancesCall , Call , ColdkeySwapScheduled , Config , CustomTransactionError , Error , Pallet ,
3- TransactionType ,
4- } ;
1+ use crate :: { BalancesCall , Call , Config , CustomTransactionError , Error , Pallet , TransactionType } ;
52use codec:: { Decode , DecodeWithMemTracking , Encode } ;
63use frame_support:: dispatch:: { DispatchInfo , PostDispatchInfo } ;
7- use frame_support:: pallet_prelude:: Weight ;
84use frame_support:: traits:: IsSubType ;
95use scale_info:: TypeInfo ;
106use sp_runtime:: traits:: {
117 AsSystemOriginSigner , DispatchInfoOf , Dispatchable , Implication , TransactionExtension ,
128 ValidateResult ,
139} ;
14- use sp_runtime:: transaction_validity:: {
15- TransactionSource , TransactionValidity , TransactionValidityError , ValidTransaction ,
10+ use sp_runtime:: {
11+ impl_tx_ext_default,
12+ transaction_validity:: { TransactionSource , TransactionValidity , ValidTransaction } ,
1613} ;
1714use sp_std:: marker:: PhantomData ;
1815use sp_std:: vec:: Vec ;
1916use subtensor_macros:: freeze_struct;
2017use subtensor_runtime_common:: { NetUid , NetUidStorageIndex } ;
2118
19+ type CallOf < T > = <T as frame_system:: Config >:: RuntimeCall ;
20+ type OriginOf < T > = <T as frame_system:: Config >:: RuntimeOrigin ;
21+
2222#[ freeze_struct( "2e02eb32e5cb25d3" ) ]
2323#[ derive( Default , Encode , Decode , DecodeWithMemTracking , Clone , Eq , PartialEq , TypeInfo ) ]
2424pub struct SubtensorTransactionExtension < T : Config + Send + Sync + TypeInfo > ( pub PhantomData < T > ) ;
@@ -31,9 +31,7 @@ impl<T: Config + Send + Sync + TypeInfo> sp_std::fmt::Debug for SubtensorTransac
3131
3232impl < T : Config + Send + Sync + TypeInfo > SubtensorTransactionExtension < T >
3333where
34- <T as frame_system:: Config >:: RuntimeCall :
35- Dispatchable < Info = DispatchInfo , PostInfo = PostDispatchInfo > ,
36- <T as frame_system:: Config >:: RuntimeCall : IsSubType < Call < T > > ,
34+ CallOf < T > : Dispatchable < Info = DispatchInfo , PostInfo = PostDispatchInfo > + IsSubType < Call < T > > ,
3735{
3836 pub fn new ( ) -> Self {
3937 Self ( Default :: default ( ) )
@@ -52,30 +50,29 @@ where
5250 pub fn result_to_validity ( result : Result < ( ) , Error < T > > , priority : u64 ) -> TransactionValidity {
5351 if let Err ( err) = result {
5452 Err ( match err {
55- Error :: < T > :: AmountTooLow => CustomTransactionError :: StakeAmountTooLow . into ( ) ,
56- Error :: < T > :: SubnetNotExists => CustomTransactionError :: SubnetNotExists . into ( ) ,
57- Error :: < T > :: NotEnoughBalanceToStake => CustomTransactionError :: BalanceTooLow . into ( ) ,
53+ Error :: < T > :: AmountTooLow => CustomTransactionError :: StakeAmountTooLow ,
54+ Error :: < T > :: SubnetNotExists => CustomTransactionError :: SubnetNotExists ,
55+ Error :: < T > :: NotEnoughBalanceToStake => CustomTransactionError :: BalanceTooLow ,
5856 Error :: < T > :: HotKeyAccountNotExists => {
59- CustomTransactionError :: HotkeyAccountDoesntExist . into ( )
57+ CustomTransactionError :: HotkeyAccountDoesntExist
6058 }
6159 Error :: < T > :: NotEnoughStakeToWithdraw => {
62- CustomTransactionError :: NotEnoughStakeToWithdraw . into ( )
63- }
64- Error :: < T > :: InsufficientLiquidity => {
65- CustomTransactionError :: InsufficientLiquidity . into ( )
60+ CustomTransactionError :: NotEnoughStakeToWithdraw
6661 }
67- Error :: < T > :: SlippageTooHigh => CustomTransactionError :: SlippageTooHigh . into ( ) ,
68- Error :: < T > :: TransferDisallowed => CustomTransactionError :: TransferDisallowed . into ( ) ,
62+ Error :: < T > :: InsufficientLiquidity => CustomTransactionError :: InsufficientLiquidity ,
63+ Error :: < T > :: SlippageTooHigh => CustomTransactionError :: SlippageTooHigh ,
64+ Error :: < T > :: TransferDisallowed => CustomTransactionError :: TransferDisallowed ,
6965 Error :: < T > :: HotKeyNotRegisteredInNetwork => {
70- CustomTransactionError :: HotKeyNotRegisteredInNetwork . into ( )
66+ CustomTransactionError :: HotKeyNotRegisteredInNetwork
7167 }
72- Error :: < T > :: InvalidIpAddress => CustomTransactionError :: InvalidIpAddress . into ( ) ,
68+ Error :: < T > :: InvalidIpAddress => CustomTransactionError :: InvalidIpAddress ,
7369 Error :: < T > :: ServingRateLimitExceeded => {
74- CustomTransactionError :: ServingRateLimitExceeded . into ( )
70+ CustomTransactionError :: ServingRateLimitExceeded
7571 }
76- Error :: < T > :: InvalidPort => CustomTransactionError :: InvalidPort . into ( ) ,
77- _ => CustomTransactionError :: BadRequest . into ( ) ,
78- } )
72+ Error :: < T > :: InvalidPort => CustomTransactionError :: InvalidPort ,
73+ _ => CustomTransactionError :: BadRequest ,
74+ }
75+ . into ( ) )
7976 } else {
8077 Ok ( ValidTransaction {
8178 priority,
@@ -89,52 +86,41 @@ impl<T: Config + Send + Sync + TypeInfo + pallet_balances::Config>
8986 TransactionExtension < <T as frame_system:: Config >:: RuntimeCall >
9087 for SubtensorTransactionExtension < T >
9188where
92- <T as frame_system:: Config >:: RuntimeCall :
93- Dispatchable < Info = DispatchInfo , PostInfo = PostDispatchInfo > ,
94- <T as frame_system:: Config >:: RuntimeOrigin : AsSystemOriginSigner < T :: AccountId > + Clone ,
95- <T as frame_system:: Config >:: RuntimeCall : IsSubType < Call < T > > ,
96- <T as frame_system:: Config >:: RuntimeCall : IsSubType < BalancesCall < T > > ,
89+ CallOf < T > : Dispatchable < Info = DispatchInfo , PostInfo = PostDispatchInfo >
90+ + IsSubType < Call < T > >
91+ + IsSubType < BalancesCall < T > > ,
92+ OriginOf < T > : AsSystemOriginSigner < T :: AccountId > + Clone ,
9793{
9894 const IDENTIFIER : & ' static str = "SubtensorTransactionExtension" ;
9995
10096 type Implicit = ( ) ;
101- type Val = Option < T :: AccountId > ;
97+ type Val = ( ) ;
10298 type Pre = ( ) ;
10399
104- fn weight ( & self , _call : & <T as frame_system:: Config >:: RuntimeCall ) -> Weight {
105- // TODO: benchmark transaction extension
106- Weight :: zero ( )
107- }
108-
109100 fn validate (
110101 & self ,
111- origin : < T as frame_system :: Config > :: RuntimeOrigin ,
112- call : & < T as frame_system :: Config > :: RuntimeCall ,
113- _info : & DispatchInfoOf < < T as frame_system :: Config > :: RuntimeCall > ,
102+ origin : OriginOf < T > ,
103+ call : & CallOf < T > ,
104+ _info : & DispatchInfoOf < CallOf < T > > ,
114105 _len : usize ,
115106 _self_implicit : Self :: Implicit ,
116107 _inherited_implication : & impl Implication ,
117108 _source : TransactionSource ,
118- ) -> ValidateResult < Self :: Val , < T as frame_system :: Config > :: RuntimeCall > {
109+ ) -> ValidateResult < Self :: Val , CallOf < T > > {
119110 // Ensure the transaction is signed, else we just skip the extension.
120111 let Some ( who) = origin. as_system_origin_signer ( ) else {
121- return Ok ( ( Default :: default ( ) , None , origin) ) ;
112+ return Ok ( ( Default :: default ( ) , ( ) , origin) ) ;
122113 } ;
123114
124- // Verify ColdkeySwapScheduled map for coldkey
125- match call. is_sub_type ( ) {
126- // Whitelist
127- Some ( Call :: schedule_swap_coldkey { .. } ) => { }
128- _ => {
129- if ColdkeySwapScheduled :: < T > :: contains_key ( who) {
130- return Err ( CustomTransactionError :: ColdkeyInSwapSchedule . into ( ) ) ;
131- }
132- }
115+ // Ensure the origin coldkey is not announced for a swap.
116+ if !matches ! ( call. is_sub_type( ) , Some ( Call :: announce_coldkey_swap { .. } ) ) {
117+ return Err ( CustomTransactionError :: ColdkeySwapAnnounced . into ( ) ) ;
133118 }
119+
134120 match call. is_sub_type ( ) {
135121 Some ( Call :: commit_weights { netuid, .. } ) => {
136122 if Self :: check_weights_min_stake ( who, * netuid) {
137- Ok ( ( Default :: default ( ) , Some ( who . clone ( ) ) , origin) )
123+ Ok ( ( Default :: default ( ) , ( ) , origin) )
138124 } else {
139125 Err ( CustomTransactionError :: StakeAmountTooLow . into ( ) )
140126 }
@@ -158,7 +144,7 @@ where
158144 match Pallet :: < T > :: find_commit_block_via_hash ( provided_hash) {
159145 Some ( commit_block) => {
160146 if Pallet :: < T > :: is_reveal_block_range ( * netuid, commit_block) {
161- Ok ( ( Default :: default ( ) , Some ( who . clone ( ) ) , origin) )
147+ Ok ( ( Default :: default ( ) , ( ) , origin) )
162148 } else {
163149 Err ( CustomTransactionError :: CommitBlockNotInRevealRange . into ( ) )
164150 }
@@ -203,7 +189,7 @@ where
203189 if provided_hashes. len ( ) == batch_reveal_block. len ( ) {
204190 if Pallet :: < T > :: is_batch_reveal_block_range ( * netuid, batch_reveal_block)
205191 {
206- Ok ( ( Default :: default ( ) , Some ( who . clone ( ) ) , origin) )
192+ Ok ( ( Default :: default ( ) , ( ) , origin) )
207193 } else {
208194 Err ( CustomTransactionError :: CommitBlockNotInRevealRange . into ( ) )
209195 }
@@ -219,7 +205,7 @@ where
219205 }
220206 Some ( Call :: set_weights { netuid, .. } ) => {
221207 if Self :: check_weights_min_stake ( who, * netuid) {
222- Ok ( ( Default :: default ( ) , Some ( who . clone ( ) ) , origin) )
208+ Ok ( ( Default :: default ( ) , ( ) , origin) )
223209 } else {
224210 Err ( CustomTransactionError :: StakeAmountTooLow . into ( ) )
225211 }
@@ -233,7 +219,7 @@ where
233219 if * reveal_round < pallet_drand:: LastStoredRound :: < T > :: get ( ) {
234220 return Err ( CustomTransactionError :: InvalidRevealRound . into ( ) ) ;
235221 }
236- Ok ( ( Default :: default ( ) , Some ( who . clone ( ) ) , origin) )
222+ Ok ( ( Default :: default ( ) , ( ) , origin) )
237223 } else {
238224 Err ( CustomTransactionError :: StakeAmountTooLow . into ( ) )
239225 }
@@ -249,7 +235,7 @@ where
249235 return Err ( CustomTransactionError :: RateLimitExceeded . into ( ) ) ;
250236 }
251237
252- Ok ( ( Default :: default ( ) , Some ( who . clone ( ) ) , origin) )
238+ Ok ( ( Default :: default ( ) , ( ) , origin) )
253239 }
254240 Some ( Call :: serve_axon {
255241 netuid,
@@ -276,41 +262,25 @@ where
276262 ) ,
277263 0u64 ,
278264 )
279- . map ( |validity| ( validity, Some ( who . clone ( ) ) , origin. clone ( ) ) )
265+ . map ( |validity| ( validity, ( ) , origin. clone ( ) ) )
280266 }
281267 Some ( Call :: register_network { .. } ) => {
282268 if !TransactionType :: RegisterNetwork . passes_rate_limit :: < T > ( who) {
283269 return Err ( CustomTransactionError :: RateLimitExceeded . into ( ) ) ;
284270 }
285271
286- Ok ( ( Default :: default ( ) , Some ( who . clone ( ) ) , origin) )
272+ Ok ( ( Default :: default ( ) , ( ) , origin) )
287273 }
288274 Some ( Call :: associate_evm_key { netuid, .. } ) => {
289- match Pallet :: < T > :: get_uid_for_net_and_hotkey ( * netuid, who) {
290- Ok ( uid) => {
291- match Pallet :: < T > :: ensure_evm_key_associate_rate_limit ( * netuid, uid) {
292- Ok ( _) => Ok ( ( Default :: default ( ) , Some ( who. clone ( ) ) , origin) ) ,
293- Err ( _) => {
294- Err ( CustomTransactionError :: EvmKeyAssociateRateLimitExceeded . into ( ) )
295- }
296- }
297- }
298- Err ( _) => Err ( CustomTransactionError :: UidNotFound . into ( ) ) ,
299- }
275+ let uid = Pallet :: < T > :: get_uid_for_net_and_hotkey ( * netuid, who)
276+ . map_err ( |_| CustomTransactionError :: UidNotFound ) ?;
277+ Pallet :: < T > :: ensure_evm_key_associate_rate_limit ( * netuid, uid)
278+ . map_err ( |_| CustomTransactionError :: EvmKeyAssociateRateLimitExceeded ) ?;
279+ Ok ( ( Default :: default ( ) , ( ) , origin) )
300280 }
301- _ => Ok ( ( Default :: default ( ) , Some ( who . clone ( ) ) , origin) ) ,
281+ _ => Ok ( ( Default :: default ( ) , ( ) , origin) ) ,
302282 }
303283 }
304284
305- // NOTE: Add later when we put in a pre and post dispatch step.
306- fn prepare (
307- self ,
308- _val : Self :: Val ,
309- _origin : & <T as frame_system:: Config >:: RuntimeOrigin ,
310- _call : & <T as frame_system:: Config >:: RuntimeCall ,
311- _info : & DispatchInfoOf < <T as frame_system:: Config >:: RuntimeCall > ,
312- _len : usize ,
313- ) -> Result < Self :: Pre , TransactionValidityError > {
314- Ok ( ( ) )
315- }
285+ impl_tx_ext_default ! ( <T as frame_system:: Config >:: RuntimeCall ; weight prepare) ;
316286}
0 commit comments