Skip to content

Commit 37e6169

Browse files
authored
Merge pull request #700 from opentensor/custom-errors-signed-extension
custom errors signed extension
2 parents 3536a8b + 58196b1 commit 37e6169

File tree

3 files changed

+70
-13
lines changed

3 files changed

+70
-13
lines changed

pallets/subtensor/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,7 +1285,7 @@ where
12851285
..Default::default()
12861286
})
12871287
} else {
1288-
Err(InvalidTransaction::Call.into())
1288+
Err(InvalidTransaction::Custom(1).into())
12891289
}
12901290
}
12911291
Some(Call::reveal_weights { netuid, .. }) => {
@@ -1297,7 +1297,7 @@ where
12971297
..Default::default()
12981298
})
12991299
} else {
1300-
Err(InvalidTransaction::Call.into())
1300+
Err(InvalidTransaction::Custom(2).into())
13011301
}
13021302
}
13031303
Some(Call::set_weights { netuid, .. }) => {
@@ -1309,7 +1309,7 @@ where
13091309
..Default::default()
13101310
})
13111311
} else {
1312-
Err(InvalidTransaction::Call.into())
1312+
Err(InvalidTransaction::Custom(3).into())
13131313
}
13141314
}
13151315
Some(Call::set_root_weights { netuid, hotkey, .. }) => {
@@ -1321,7 +1321,7 @@ where
13211321
..Default::default()
13221322
})
13231323
} else {
1324-
Err(InvalidTransaction::Call.into())
1324+
Err(InvalidTransaction::Custom(4).into())
13251325
}
13261326
}
13271327
Some(Call::add_stake { .. }) => Ok(ValidTransaction {
@@ -1340,7 +1340,7 @@ where
13401340
if registrations_this_interval >= (max_registrations_per_interval.saturating_mul(3))
13411341
{
13421342
// If the registration limit for the interval is exceeded, reject the transaction
1343-
return InvalidTransaction::ExhaustsResources.into();
1343+
return Err(InvalidTransaction::Custom(5).into());
13441344
}
13451345
Ok(ValidTransaction {
13461346
priority: Self::get_priority_vanilla(),

pallets/subtensor/tests/registration.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ fn test_registration_rate_limit_exceeded() {
276276
let result = extension.validate(&who, &call.into(), &info, 10);
277277

278278
// Expectation: The transaction should be rejected
279-
assert_err!(result, InvalidTransaction::ExhaustsResources);
279+
assert_err!(result, InvalidTransaction::Custom(5));
280280

281281
let current_registrants = SubtensorModule::get_registrations_this_interval(netuid);
282282
assert!(current_registrants <= max_registrants);
@@ -360,10 +360,7 @@ fn test_burned_registration_rate_limit_exceeded() {
360360
extension.validate(&who, &call_burned_register.into(), &info, 10);
361361

362362
// Expectation: The transaction should be rejected
363-
assert_err!(
364-
burned_register_result,
365-
InvalidTransaction::ExhaustsResources
366-
);
363+
assert_err!(burned_register_result, InvalidTransaction::Custom(5));
367364

368365
let current_registrants = SubtensorModule::get_registrations_this_interval(netuid);
369366
assert!(current_registrants <= max_registrants);

pallets/subtensor/tests/weights.rs

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ fn test_set_rootweights_validate() {
107107
assert_err!(
108108
// Should get an invalid transaction error
109109
result_no_stake,
110-
TransactionValidityError::Invalid(InvalidTransaction::Call,)
110+
TransactionValidityError::Invalid(InvalidTransaction::Custom(4))
111111
);
112112

113113
// Increase the stake to be equal to the minimum
@@ -207,7 +207,7 @@ fn test_commit_weights_validate() {
207207
assert_err!(
208208
// Should get an invalid transaction error
209209
result_no_stake,
210-
TransactionValidityError::Invalid(InvalidTransaction::Call,)
210+
TransactionValidityError::Invalid(InvalidTransaction::Custom(1))
211211
);
212212

213213
// Increase the stake to be equal to the minimum
@@ -259,6 +259,66 @@ fn test_reveal_weights_dispatch_info_ok() {
259259
});
260260
}
261261

262+
#[test]
263+
fn test_set_weights_validate() {
264+
// Testing the signed extension validate function
265+
// correctly filters the `set_weights` transaction.
266+
267+
new_test_ext(0).execute_with(|| {
268+
let netuid: u16 = 1;
269+
let coldkey = U256::from(0);
270+
let hotkey: U256 = U256::from(1);
271+
assert_ne!(hotkey, coldkey);
272+
273+
let who = hotkey; // The hotkey signs this transaction
274+
275+
let call = RuntimeCall::SubtensorModule(SubtensorCall::set_weights {
276+
netuid,
277+
dests: vec![1, 1],
278+
weights: vec![1, 1],
279+
version_key: 0,
280+
});
281+
282+
// Create netuid
283+
add_network(netuid, 0, 0);
284+
// Register the hotkey
285+
SubtensorModule::append_neuron(netuid, &hotkey, 0);
286+
Owner::<Test>::insert(hotkey, coldkey);
287+
288+
let min_stake = 500_000_000_000;
289+
// Set the minimum stake
290+
SubtensorModule::set_weights_min_stake(min_stake);
291+
292+
// Verify stake is less than minimum
293+
assert!(SubtensorModule::get_total_stake_for_hotkey(&hotkey) < min_stake);
294+
let info: DispatchInfo =
295+
DispatchInfoOf::<<Test as frame_system::Config>::RuntimeCall>::default();
296+
297+
let extension = pallet_subtensor::SubtensorSignedExtension::<Test>::new();
298+
// Submit to the signed extension validate function
299+
let result_no_stake = extension.validate(&who, &call.clone(), &info, 10);
300+
// Should fail due to insufficient stake
301+
assert_err!(
302+
result_no_stake,
303+
TransactionValidityError::Invalid(InvalidTransaction::Custom(3))
304+
);
305+
306+
// Increase the stake to be equal to the minimum
307+
SubtensorModule::increase_stake_on_hotkey_account(&hotkey, min_stake);
308+
309+
// Verify stake is equal to minimum
310+
assert_eq!(
311+
SubtensorModule::get_total_stake_for_hotkey(&hotkey),
312+
min_stake
313+
);
314+
315+
// Submit to the signed extension validate function
316+
let result_min_stake = extension.validate(&who, &call.clone(), &info, 10);
317+
// Now the call should pass
318+
assert_ok!(result_min_stake);
319+
});
320+
}
321+
262322
#[test]
263323
fn test_reveal_weights_validate() {
264324
// Testing the signed extension validate function
@@ -306,7 +366,7 @@ fn test_reveal_weights_validate() {
306366
assert_err!(
307367
// Should get an invalid transaction error
308368
result_no_stake,
309-
TransactionValidityError::Invalid(InvalidTransaction::Call,)
369+
TransactionValidityError::Invalid(InvalidTransaction::Custom(2))
310370
);
311371

312372
// Increase the stake to be equal to the minimum

0 commit comments

Comments
 (0)