Skip to content

Commit 498d30a

Browse files
committed
add set weights rate limit to commit-reveal
1 parent 96911e6 commit 498d30a

File tree

3 files changed

+92
-5
lines changed

3 files changed

+92
-5
lines changed

pallets/subtensor/src/macros/errors.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,5 +188,7 @@ mod errors {
188188
RevealTooEarly,
189189
/// Attempted to batch reveal weights with mismatched vector input lenghts.
190190
InputLengthsUnequal,
191+
/// A transactor exceeded the rate limit for setting weights.
192+
CommittingWeightsTooFast,
191193
}
192194
}

pallets/subtensor/src/subnets/weights.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,19 @@ impl<T: Config> Pallet<T> {
3939
Error::<T>::CommitRevealDisabled
4040
);
4141

42-
// --- 3. Calculate the reveal blocks based on tempo and reveal period.
42+
ensure!(
43+
Self::is_hotkey_registered_on_network(netuid, &who),
44+
Error::<T>::HotKeyNotRegisteredInSubNet
45+
);
46+
4347
let commit_block: u64 = Self::get_current_block_as_u64();
48+
let neuron_uid: u16 = Self::get_uid_for_net_and_hotkey(netuid, &who)?;
49+
ensure!(
50+
Self::check_rate_limit(netuid, neuron_uid, commit_block),
51+
Error::<T>::CommittingWeightsTooFast
52+
);
53+
54+
// --- 3. Calculate the reveal blocks based on tempo and reveal period.
4455
let (first_reveal_block, last_reveal_block) = Self::get_reveal_blocks(netuid, commit_block);
4556

4657
// --- 4. Mutate the WeightCommits to retrieve existing commits for the user.
@@ -76,7 +87,10 @@ impl<T: Config> Pallet<T> {
7687
// --- 10. Emit the WeightsCommitted event.
7788
Self::deposit_event(Event::WeightsCommitted(who.clone(), netuid, commit_hash));
7889

79-
// --- 11. Return ok.
90+
// --- 11. Set last update for the UID
91+
Self::set_last_update_for_uid(netuid, neuron_uid, commit_block);
92+
93+
// --- 12. Return ok.
8094
Ok(())
8195
})
8296
}
@@ -563,7 +577,9 @@ impl<T: Config> Pallet<T> {
563577
Weights::<T>::insert(netuid, neuron_uid, zipped_weights);
564578

565579
// --- 18. Set the activity for the weights on this network.
566-
Self::set_last_update_for_uid(netuid, neuron_uid, current_block);
580+
if !Self::get_commit_reveal_weights_enabled(netuid) {
581+
Self::set_last_update_for_uid(netuid, neuron_uid, current_block);
582+
}
567583

568584
// --- 19. Emit the tracking event.
569585
log::debug!(

pallets/subtensor/tests/weights.rs

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1815,7 +1815,7 @@ fn test_toggle_commit_reveal_weights_and_set_weights() {
18151815

18161816
#[test]
18171817
fn test_tempo_change_during_commit_reveal_process() {
1818-
new_test_ext(1).execute_with(|| {
1818+
new_test_ext(0).execute_with(|| {
18191819
let netuid: u16 = 1;
18201820
let uids: Vec<u16> = vec![0, 1];
18211821
let weight_values: Vec<u16> = vec![10, 10];
@@ -1832,7 +1832,7 @@ fn test_tempo_change_during_commit_reveal_process() {
18321832
version_key,
18331833
));
18341834

1835-
System::set_block_number(1);
1835+
System::set_block_number(0);
18361836

18371837
let tempo: u16 = 100;
18381838
add_network(netuid, tempo, 0);
@@ -4059,3 +4059,72 @@ fn test_get_reveal_blocks() {
40594059
);
40604060
})
40614061
}
4062+
4063+
#[test]
4064+
fn test_commit_weights_rate_limit() {
4065+
new_test_ext(1).execute_with(|| {
4066+
let netuid: u16 = 1;
4067+
let uids: Vec<u16> = vec![0, 1];
4068+
let weight_values: Vec<u16> = vec![10, 10];
4069+
let salt: Vec<u16> = vec![1, 2, 3, 4, 5, 6, 7, 8];
4070+
let version_key: u64 = 0;
4071+
let hotkey: U256 = U256::from(1);
4072+
4073+
let commit_hash: H256 = BlakeTwo256::hash_of(&(
4074+
hotkey,
4075+
netuid,
4076+
uids.clone(),
4077+
weight_values.clone(),
4078+
salt.clone(),
4079+
version_key,
4080+
));
4081+
System::set_block_number(11);
4082+
4083+
let tempo: u16 = 5;
4084+
add_network(netuid, tempo, 0);
4085+
4086+
register_ok_neuron(netuid, U256::from(3), U256::from(4), 300_000);
4087+
register_ok_neuron(netuid, U256::from(1), U256::from(2), 100_000);
4088+
SubtensorModule::set_weights_set_rate_limit(netuid, 10); // Rate limit is 10 blocks
4089+
SubtensorModule::set_validator_permit_for_uid(netuid, 0, true);
4090+
SubtensorModule::set_validator_permit_for_uid(netuid, 1, true);
4091+
SubtensorModule::set_commit_reveal_weights_enabled(netuid, true);
4092+
4093+
let neuron_uid = SubtensorModule::get_uid_for_net_and_hotkey(netuid, &hotkey).unwrap();
4094+
SubtensorModule::set_last_update_for_uid(netuid, neuron_uid, 0);
4095+
4096+
assert_ok!(SubtensorModule::commit_weights(
4097+
RuntimeOrigin::signed(hotkey),
4098+
netuid,
4099+
commit_hash
4100+
));
4101+
4102+
let new_salt: Vec<u16> = vec![9; 8];
4103+
let new_commit_hash: H256 = BlakeTwo256::hash_of(&(
4104+
hotkey,
4105+
netuid,
4106+
uids.clone(),
4107+
weight_values.clone(),
4108+
new_salt.clone(),
4109+
version_key,
4110+
));
4111+
assert_err!(
4112+
SubtensorModule::commit_weights(RuntimeOrigin::signed(hotkey), netuid, new_commit_hash),
4113+
Error::<Test>::CommittingWeightsTooFast
4114+
);
4115+
4116+
step_block(5);
4117+
assert_err!(
4118+
SubtensorModule::commit_weights(RuntimeOrigin::signed(hotkey), netuid, new_commit_hash),
4119+
Error::<Test>::CommittingWeightsTooFast
4120+
);
4121+
4122+
step_block(5); // Current block is now 21
4123+
4124+
assert_ok!(SubtensorModule::commit_weights(
4125+
RuntimeOrigin::signed(hotkey),
4126+
netuid,
4127+
new_commit_hash
4128+
));
4129+
});
4130+
}

0 commit comments

Comments
 (0)