Skip to content

Commit 4d7b612

Browse files
authored
Merge pull request #1195 from opentensor/fix/ensure-before-write-burned-reg
[RAO] [Fix] make sure to ensure burn reg before writing
2 parents 659592a + 445fae0 commit 4d7b612

File tree

2 files changed

+51
-11
lines changed

2 files changed

+51
-11
lines changed

pallets/subtensor/src/subnets/registration.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -119,29 +119,29 @@ impl<T: Config> Pallet<T> {
119119
Error::<T>::NotEnoughBalanceToStake
120120
);
121121

122-
// --- 8. Ensure the remove operation from the coldkey is a success.
123-
let actual_burn_amount =
124-
Self::remove_balance_from_coldkey_account(&coldkey, registration_cost)?;
125-
126-
// Tokens are swapped and then burned.
127-
let burned_alpha: u64 = Self::swap_tao_for_alpha(netuid, actual_burn_amount);
128-
SubnetAlphaOut::<T>::mutate(netuid, |total| *total = total.saturating_sub(burned_alpha));
129-
130-
// --- 9. If the network account does not exist we will create it here.
122+
// If the network account does not exist we will create it here.
131123
Self::create_account_if_non_existent(&coldkey, &hotkey);
132124

133-
// --- 10. Ensure that the pairing is correct.
125+
// --- 8. Ensure that the pairing is correct.
134126
ensure!(
135127
Self::coldkey_owns_hotkey(&coldkey, &hotkey),
136128
Error::<T>::NonAssociatedColdKey
137129
);
138130

139-
// Possibly there is no neuron slots at all.
131+
// --- 9. Possibly there are no neuron slots at all.
140132
ensure!(
141133
Self::get_max_allowed_uids(netuid) != 0,
142134
Error::<T>::NoNeuronIdAvailable
143135
);
144136

137+
// --- 10. Ensure the remove operation from the coldkey is a success.
138+
let actual_burn_amount =
139+
Self::remove_balance_from_coldkey_account(&coldkey, registration_cost)?;
140+
141+
// Tokens are swapped and then burned.
142+
let burned_alpha: u64 = Self::swap_tao_for_alpha(netuid, actual_burn_amount);
143+
SubnetAlphaOut::<T>::mutate(netuid, |total| *total = total.saturating_sub(burned_alpha));
144+
145145
// Actually perform the registration.
146146
let neuron_uid: u16 = Self::register_neuron(netuid, &hotkey);
147147

pallets/subtensor/src/tests/registration.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,46 @@ fn test_burn_registration_without_neuron_slot() {
490490
});
491491
}
492492

493+
#[test]
494+
fn test_burn_registration_doesnt_write_on_failure() {
495+
new_test_ext(1).execute_with(|| {
496+
let netuid: u16 = 1;
497+
let tempo: u16 = 13;
498+
let hotkey_account_id = U256::from(1);
499+
let burn_cost = 1000;
500+
let initial_balance = burn_cost * 10;
501+
let coldkey_account_id = U256::from(987);
502+
503+
// Add network and set burn cost
504+
add_network(netuid, tempo, 0);
505+
SubtensorModule::set_burn(netuid, burn_cost);
506+
// Give coldkey balance to pay for registration
507+
SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id, initial_balance);
508+
// Set max allowed uids to 0 so registration will fail, but only on last check.
509+
SubtensorModule::set_max_allowed_uids(netuid, 0);
510+
511+
// We expect this to fail at the last ensure check.
512+
assert_err!(
513+
SubtensorModule::burned_register(
514+
<<Test as Config>::RuntimeOrigin>::signed(coldkey_account_id),
515+
netuid,
516+
hotkey_account_id
517+
),
518+
Error::<Test>::NoNeuronIdAvailable
519+
);
520+
521+
// Make sure the coldkey balance is unchanged.
522+
assert_eq!(
523+
SubtensorModule::get_coldkey_balance(&coldkey_account_id),
524+
initial_balance
525+
);
526+
// Make sure the neuron is not registered.
527+
assert_eq!(SubtensorModule::get_subnetwork_n(netuid), 0);
528+
// Make sure the hotkey is not registered.
529+
assert!(SubtensorModule::get_uid_for_net_and_hotkey(netuid, &hotkey_account_id).is_err());
530+
});
531+
}
532+
493533
#[test]
494534
fn test_burn_adjustment() {
495535
new_test_ext(1).execute_with(|| {

0 commit comments

Comments
 (0)