diff --git a/pallets/subtensor/src/subnets/uids.rs b/pallets/subtensor/src/subnets/uids.rs index d21509f83d..3665b139ff 100644 --- a/pallets/subtensor/src/subnets/uids.rs +++ b/pallets/subtensor/src/subnets/uids.rs @@ -46,6 +46,8 @@ impl Pallet { } Dividends::::mutate(netuid, |v| Self::set_element_at(v, neuron_index, 0)); StakeWeight::::mutate(netuid, |v| Self::set_element_at(v, neuron_index, 0)); + ValidatorTrust::::mutate(netuid, |v| Self::set_element_at(v, neuron_index, 0)); + ValidatorPermit::::mutate(netuid, |v| Self::set_element_at(v, neuron_index, false)); } /// Replace the neuron under this uid. diff --git a/pallets/subtensor/src/tests/uids.rs b/pallets/subtensor/src/tests/uids.rs index 37d9733991..7679f2b727 100644 --- a/pallets/subtensor/src/tests/uids.rs +++ b/pallets/subtensor/src/tests/uids.rs @@ -225,6 +225,52 @@ fn test_replace_neuron_resets_last_update() { }); } +#[test] +fn test_replace_neuron_clears_validator_trust_and_permit() { + new_test_ext(1).execute_with(|| { + let registration_block: u64 = 0; + let replacement_block: u64 = 123; + let netuid = NetUid::from(1); + let tempo: u16 = 13; + let hotkey_account_id = U256::from(1); + let coldkey_account_id = U256::from(1234); + let new_hotkey_account_id = U256::from(2); + + System::set_block_number(registration_block); + add_network(netuid, tempo, 0); + register_ok_neuron(netuid, hotkey_account_id, coldkey_account_id, 0); + + let neuron_uid = + SubtensorModule::get_uid_for_net_and_hotkey(netuid, &hotkey_account_id).unwrap(); + let idx = neuron_uid as usize; + + // Simulate the previous occupant having earned a validator_permit and trust score. + ValidatorTrust::::mutate(netuid, |v| { + if v.len() <= idx { + v.resize(idx + 1, 0); + } + v[idx] = 42; + }); + ValidatorPermit::::mutate(netuid, |v| { + if v.len() <= idx { + v.resize(idx + 1, false); + } + v[idx] = true; + }); + + SubtensorModule::replace_neuron( + netuid, + neuron_uid, + &new_hotkey_account_id, + replacement_block, + ); + + // The replaced neuron must not inherit the previous occupant's validator state. + assert_eq!(ValidatorTrust::::get(netuid)[idx], 0); + assert!(!ValidatorPermit::::get(netuid)[idx]); + }); +} + #[test] fn test_replace_neuron_multiple_subnets() { new_test_ext(1).execute_with(|| {