Skip to content

Commit 8a4962c

Browse files
committed
throw TooManyUnrevealedCommits per hotkey
1 parent 7f5bd9d commit 8a4962c

File tree

2 files changed

+80
-15
lines changed

2 files changed

+80
-15
lines changed

pallets/subtensor/src/subnets/weights.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,15 @@ impl<T: Config> Pallet<T> {
186186
let cur_epoch = Self::get_epoch_index(netuid, cur_block);
187187
CRV3WeightCommits::<T>::try_mutate(netuid, cur_epoch, |commits| -> DispatchResult {
188188
// 6. Verify that the number of unrevealed commits is within the allowed limit.
189-
ensure!(commits.len() < 10, Error::<T>::TooManyUnrevealedCommits);
189+
190+
let unrevealed_commits_for_who = commits
191+
.iter()
192+
.filter(|(account, _, _)| account == &who)
193+
.count();
194+
ensure!(
195+
unrevealed_commits_for_who < 10,
196+
Error::<T>::TooManyUnrevealedCommits
197+
);
190198

191199
// 7. Append the new commit with calculated reveal blocks.
192200
// Hash the commit before it is moved, for the event

pallets/subtensor/src/tests/weights.rs

Lines changed: 71 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4677,46 +4677,103 @@ fn test_do_commit_crv3_weights_committing_too_fast() {
46774677
fn test_do_commit_crv3_weights_too_many_unrevealed_commits() {
46784678
new_test_ext(1).execute_with(|| {
46794679
let netuid: u16 = 1;
4680-
let hotkey: AccountId = U256::from(1);
4680+
let hotkey1: AccountId = U256::from(1);
4681+
let hotkey2: AccountId = U256::from(2);
46814682
let reveal_round: u64 = 1000;
46824683

46834684
add_network(netuid, 5, 0);
4684-
register_ok_neuron(netuid, hotkey, U256::from(2), 100_000);
4685-
SubtensorModule::set_weights_set_rate_limit(netuid, 0);
4685+
register_ok_neuron(netuid, hotkey1, U256::from(2), 100_000);
4686+
register_ok_neuron(netuid, hotkey2, U256::from(3), 100_000);
46864687
SubtensorModule::set_commit_reveal_weights_enabled(netuid, true);
4688+
SubtensorModule::set_weights_set_rate_limit(netuid, 0);
46874689

4688-
// Simulate 10 unrevealed commits
4689-
let cur_epoch =
4690-
SubtensorModule::get_epoch_index(netuid, SubtensorModule::get_current_block_as_u64());
4690+
// Hotkey1 submits 10 commits successfully
46914691
for i in 0..10 {
46924692
let commit_data: Vec<u8> = vec![i as u8; 5];
46934693
let bounded_commit_data = commit_data
46944694
.try_into()
46954695
.expect("Failed to convert commit data into bounded vector");
4696-
assert_ok!(CRV3WeightCommits::<Test>::try_mutate(
4696+
4697+
assert_ok!(SubtensorModule::do_commit_crv3_weights(
4698+
RuntimeOrigin::signed(hotkey1),
46974699
netuid,
4698-
cur_epoch,
4699-
|commits| -> DispatchResult {
4700-
commits.push_back((hotkey, bounded_commit_data, reveal_round));
4701-
Ok(())
4702-
}
4700+
bounded_commit_data,
4701+
reveal_round
47034702
));
47044703
}
47054704

4706-
// Attempt to commit an 11th time, should fail
4705+
// Hotkey1 attempts to commit an 11th time, should fail with TooManyUnrevealedCommits
47074706
let new_commit_data: Vec<u8> = vec![11; 5];
47084707
let bounded_new_commit_data = new_commit_data
47094708
.try_into()
47104709
.expect("Failed to convert new commit data into bounded vector");
4710+
47114711
assert_err!(
47124712
SubtensorModule::do_commit_crv3_weights(
4713-
RuntimeOrigin::signed(hotkey),
4713+
RuntimeOrigin::signed(hotkey1),
47144714
netuid,
47154715
bounded_new_commit_data,
47164716
reveal_round
47174717
),
47184718
Error::<Test>::TooManyUnrevealedCommits
47194719
);
4720+
4721+
// Hotkey2 can still submit commits independently
4722+
let commit_data_hotkey2: Vec<u8> = vec![0; 5];
4723+
let bounded_commit_data_hotkey2 = commit_data_hotkey2
4724+
.try_into()
4725+
.expect("Failed to convert commit data into bounded vector");
4726+
4727+
assert_ok!(SubtensorModule::do_commit_crv3_weights(
4728+
RuntimeOrigin::signed(hotkey2),
4729+
netuid,
4730+
bounded_commit_data_hotkey2,
4731+
reveal_round
4732+
));
4733+
4734+
// Hotkey2 can submit up to 10 commits
4735+
for i in 1..10 {
4736+
let commit_data: Vec<u8> = vec![i as u8; 5];
4737+
let bounded_commit_data = commit_data
4738+
.try_into()
4739+
.expect("Failed to convert commit data into bounded vector");
4740+
4741+
assert_ok!(SubtensorModule::do_commit_crv3_weights(
4742+
RuntimeOrigin::signed(hotkey2),
4743+
netuid,
4744+
bounded_commit_data,
4745+
reveal_round
4746+
));
4747+
}
4748+
4749+
// Hotkey2 attempts to commit an 11th time, should fail
4750+
let new_commit_data: Vec<u8> = vec![11; 5];
4751+
let bounded_new_commit_data = new_commit_data
4752+
.try_into()
4753+
.expect("Failed to convert new commit data into bounded vector");
4754+
4755+
assert_err!(
4756+
SubtensorModule::do_commit_crv3_weights(
4757+
RuntimeOrigin::signed(hotkey2),
4758+
netuid,
4759+
bounded_new_commit_data,
4760+
reveal_round
4761+
),
4762+
Error::<Test>::TooManyUnrevealedCommits
4763+
);
4764+
4765+
step_epochs(10, netuid);
4766+
4767+
let new_commit_data: Vec<u8> = vec![11; 5];
4768+
let bounded_new_commit_data = new_commit_data
4769+
.try_into()
4770+
.expect("Failed to convert new commit data into bounded vector");
4771+
assert_ok!(SubtensorModule::do_commit_crv3_weights(
4772+
RuntimeOrigin::signed(hotkey1),
4773+
netuid,
4774+
bounded_new_commit_data,
4775+
reveal_round
4776+
));
47204777
});
47214778
}
47224779

0 commit comments

Comments
 (0)