Skip to content

Commit bc0f55d

Browse files
authored
Merge pull request #1959 from opentensor/refactor-extension2
Refactor transaction extension
2 parents 9ca9888 + bf552a3 commit bc0f55d

File tree

3 files changed

+39
-72
lines changed

3 files changed

+39
-72
lines changed

pallets/subtensor/src/macros/dispatches.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2156,7 +2156,7 @@ mod dispatches {
21562156
/// Emits a `SymbolUpdated` event on success.
21572157
#[pallet::call_index(112)]
21582158
#[pallet::weight((
2159-
Weight::from_parts(26_880_000, 0).saturating_add(T::DbWeight::get().reads_writes(4, 1)),
2159+
Weight::from_parts(26_200_000, 0).saturating_add(T::DbWeight::get().reads_writes(4, 1)),
21602160
DispatchClass::Operational,
21612161
Pays::Yes
21622162
))]

pallets/subtensor/src/tests/swap_coldkey.rs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2493,8 +2493,12 @@ fn test_coldkey_in_swap_schedule_prevents_funds_usage() {
24932493
&TxBaseImplication(()),
24942494
TransactionSource::External,
24952495
);
2496-
// Should pass, not in list.
2497-
assert_ok!(result);
2496+
// Should fail
2497+
assert_eq!(
2498+
// Should get an invalid transaction error
2499+
result.unwrap_err(),
2500+
CustomTransactionError::ColdkeyInSwapSchedule.into()
2501+
);
24982502

24992503
// Remove stake limit
25002504
let call = RuntimeCall::SubtensorModule(SubtensorCall::remove_stake_limit {
@@ -2513,7 +2517,27 @@ fn test_coldkey_in_swap_schedule_prevents_funds_usage() {
25132517
&TxBaseImplication(()),
25142518
TransactionSource::External,
25152519
);
2516-
// Should pass, not in list.
2520+
// Should fail
2521+
assert_eq!(
2522+
// Should get an invalid transaction error
2523+
result.unwrap_err(),
2524+
CustomTransactionError::ColdkeyInSwapSchedule.into()
2525+
);
2526+
2527+
// Schedule swap should succeed
2528+
let call = RuntimeCall::SubtensorModule(SubtensorCall::schedule_swap_coldkey {
2529+
new_coldkey: hotkey,
2530+
});
2531+
let result = extension.validate(
2532+
RawOrigin::Signed(who).into(),
2533+
&call.clone(),
2534+
&info,
2535+
10,
2536+
(),
2537+
&TxBaseImplication(()),
2538+
TransactionSource::External,
2539+
);
2540+
// Should be ok
25172541
assert_ok!(result);
25182542
});
25192543
}

pallets/subtensor/src/transaction_extension.rs

Lines changed: 11 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,16 @@ where
120120
return Ok((Default::default(), None, origin));
121121
};
122122

123+
// Verify ColdkeySwapScheduled map for coldkey
124+
match call.is_sub_type() {
125+
// Whitelist
126+
Some(Call::schedule_swap_coldkey { .. }) => {}
127+
_ => {
128+
if ColdkeySwapScheduled::<T>::contains_key(who) {
129+
return Err(CustomTransactionError::ColdkeyInSwapSchedule.into());
130+
}
131+
}
132+
}
123133
match call.is_sub_type() {
124134
Some(Call::commit_weights { netuid, .. }) => {
125135
if Self::check_weights_min_stake(who, *netuid) {
@@ -248,54 +258,7 @@ where
248258
Err(CustomTransactionError::StakeAmountTooLow.into())
249259
}
250260
}
251-
Some(Call::add_stake { .. }) => {
252-
if ColdkeySwapScheduled::<T>::contains_key(who) {
253-
return Err(CustomTransactionError::ColdkeyInSwapSchedule.into());
254-
}
255-
256-
Ok((Default::default(), Some(who.clone()), origin))
257-
}
258-
Some(Call::add_stake_limit { .. }) => {
259-
if ColdkeySwapScheduled::<T>::contains_key(who) {
260-
return Err(CustomTransactionError::ColdkeyInSwapSchedule.into());
261-
}
262-
263-
Ok((Default::default(), Some(who.clone()), origin))
264-
}
265-
Some(Call::remove_stake { .. }) => Ok((Default::default(), Some(who.clone()), origin)),
266-
Some(Call::remove_stake_limit { .. }) => {
267-
Ok((Default::default(), Some(who.clone()), origin))
268-
}
269-
Some(Call::move_stake { .. }) => {
270-
if ColdkeySwapScheduled::<T>::contains_key(who) {
271-
return Err(CustomTransactionError::ColdkeyInSwapSchedule.into());
272-
}
273-
Ok((Default::default(), Some(who.clone()), origin))
274-
}
275-
Some(Call::transfer_stake { .. }) => {
276-
if ColdkeySwapScheduled::<T>::contains_key(who) {
277-
return Err(CustomTransactionError::ColdkeyInSwapSchedule.into());
278-
}
279-
280-
Ok((Default::default(), Some(who.clone()), origin))
281-
}
282-
Some(Call::swap_stake { .. }) => {
283-
if ColdkeySwapScheduled::<T>::contains_key(who) {
284-
return Err(CustomTransactionError::ColdkeyInSwapSchedule.into());
285-
}
286-
Ok((Default::default(), Some(who.clone()), origin))
287-
}
288-
Some(Call::swap_stake_limit { .. }) => {
289-
if ColdkeySwapScheduled::<T>::contains_key(who) {
290-
return Err(CustomTransactionError::ColdkeyInSwapSchedule.into());
291-
}
292-
Ok((Default::default(), Some(who.clone()), origin))
293-
}
294261
Some(Call::register { netuid, .. } | Call::burned_register { netuid, .. }) => {
295-
if ColdkeySwapScheduled::<T>::contains_key(who) {
296-
return Err(CustomTransactionError::ColdkeyInSwapSchedule.into());
297-
}
298-
299262
let registrations_this_interval =
300263
Pallet::<T>::get_registrations_this_interval(*netuid);
301264
let max_registrations_per_interval =
@@ -308,13 +271,6 @@ where
308271

309272
Ok((Default::default(), Some(who.clone()), origin))
310273
}
311-
Some(Call::dissolve_network { .. }) => {
312-
if ColdkeySwapScheduled::<T>::contains_key(who) {
313-
Err(CustomTransactionError::ColdkeyInSwapSchedule.into())
314-
} else {
315-
Ok((Default::default(), Some(who.clone()), origin))
316-
}
317-
}
318274
Some(Call::serve_axon {
319275
netuid,
320276
version,
@@ -342,20 +298,7 @@ where
342298
)
343299
.map(|validity| (validity, Some(who.clone()), origin.clone()))
344300
}
345-
_ => {
346-
if let Some(
347-
BalancesCall::transfer_keep_alive { .. }
348-
| BalancesCall::transfer_all { .. }
349-
| BalancesCall::transfer_allow_death { .. },
350-
) = call.is_sub_type()
351-
{
352-
if ColdkeySwapScheduled::<T>::contains_key(who) {
353-
return Err(CustomTransactionError::ColdkeyInSwapSchedule.into());
354-
}
355-
}
356-
357-
Ok((Default::default(), Some(who.clone()), origin))
358-
}
301+
_ => Ok((Default::default(), Some(who.clone()), origin)),
359302
}
360303
}
361304

0 commit comments

Comments
 (0)