diff --git a/pallets/subtensor/src/subnets/weights.rs b/pallets/subtensor/src/subnets/weights.rs index 6ddc639e0..0734cfed8 100644 --- a/pallets/subtensor/src/subnets/weights.rs +++ b/pallets/subtensor/src/subnets/weights.rs @@ -186,7 +186,15 @@ impl Pallet { let cur_epoch = Self::get_epoch_index(netuid, cur_block); CRV3WeightCommits::::try_mutate(netuid, cur_epoch, |commits| -> DispatchResult { // 6. Verify that the number of unrevealed commits is within the allowed limit. - ensure!(commits.len() < 10, Error::::TooManyUnrevealedCommits); + + let unrevealed_commits_for_who = commits + .iter() + .filter(|(account, _, _)| account == &who) + .count(); + ensure!( + unrevealed_commits_for_who < 10, + Error::::TooManyUnrevealedCommits + ); // 7. Append the new commit with calculated reveal blocks. // Hash the commit before it is moved, for the event diff --git a/pallets/subtensor/src/tests/weights.rs b/pallets/subtensor/src/tests/weights.rs index fd959870e..ea22eda8a 100644 --- a/pallets/subtensor/src/tests/weights.rs +++ b/pallets/subtensor/src/tests/weights.rs @@ -4677,46 +4677,103 @@ fn test_do_commit_crv3_weights_committing_too_fast() { fn test_do_commit_crv3_weights_too_many_unrevealed_commits() { new_test_ext(1).execute_with(|| { let netuid: u16 = 1; - let hotkey: AccountId = U256::from(1); + let hotkey1: AccountId = U256::from(1); + let hotkey2: AccountId = U256::from(2); let reveal_round: u64 = 1000; add_network(netuid, 5, 0); - register_ok_neuron(netuid, hotkey, U256::from(2), 100_000); - SubtensorModule::set_weights_set_rate_limit(netuid, 0); + register_ok_neuron(netuid, hotkey1, U256::from(2), 100_000); + register_ok_neuron(netuid, hotkey2, U256::from(3), 100_000); SubtensorModule::set_commit_reveal_weights_enabled(netuid, true); + SubtensorModule::set_weights_set_rate_limit(netuid, 0); - // Simulate 10 unrevealed commits - let cur_epoch = - SubtensorModule::get_epoch_index(netuid, SubtensorModule::get_current_block_as_u64()); + // Hotkey1 submits 10 commits successfully for i in 0..10 { let commit_data: Vec = vec![i as u8; 5]; let bounded_commit_data = commit_data .try_into() .expect("Failed to convert commit data into bounded vector"); - assert_ok!(CRV3WeightCommits::::try_mutate( + + assert_ok!(SubtensorModule::do_commit_crv3_weights( + RuntimeOrigin::signed(hotkey1), netuid, - cur_epoch, - |commits| -> DispatchResult { - commits.push_back((hotkey, bounded_commit_data, reveal_round)); - Ok(()) - } + bounded_commit_data, + reveal_round )); } - // Attempt to commit an 11th time, should fail + // Hotkey1 attempts to commit an 11th time, should fail with TooManyUnrevealedCommits let new_commit_data: Vec = vec![11; 5]; let bounded_new_commit_data = new_commit_data .try_into() .expect("Failed to convert new commit data into bounded vector"); + assert_err!( SubtensorModule::do_commit_crv3_weights( - RuntimeOrigin::signed(hotkey), + RuntimeOrigin::signed(hotkey1), netuid, bounded_new_commit_data, reveal_round ), Error::::TooManyUnrevealedCommits ); + + // Hotkey2 can still submit commits independently + let commit_data_hotkey2: Vec = vec![0; 5]; + let bounded_commit_data_hotkey2 = commit_data_hotkey2 + .try_into() + .expect("Failed to convert commit data into bounded vector"); + + assert_ok!(SubtensorModule::do_commit_crv3_weights( + RuntimeOrigin::signed(hotkey2), + netuid, + bounded_commit_data_hotkey2, + reveal_round + )); + + // Hotkey2 can submit up to 10 commits + for i in 1..10 { + let commit_data: Vec = vec![i as u8; 5]; + let bounded_commit_data = commit_data + .try_into() + .expect("Failed to convert commit data into bounded vector"); + + assert_ok!(SubtensorModule::do_commit_crv3_weights( + RuntimeOrigin::signed(hotkey2), + netuid, + bounded_commit_data, + reveal_round + )); + } + + // Hotkey2 attempts to commit an 11th time, should fail + let new_commit_data: Vec = vec![11; 5]; + let bounded_new_commit_data = new_commit_data + .try_into() + .expect("Failed to convert new commit data into bounded vector"); + + assert_err!( + SubtensorModule::do_commit_crv3_weights( + RuntimeOrigin::signed(hotkey2), + netuid, + bounded_new_commit_data, + reveal_round + ), + Error::::TooManyUnrevealedCommits + ); + + step_epochs(10, netuid); + + let new_commit_data: Vec = vec![11; 5]; + let bounded_new_commit_data = new_commit_data + .try_into() + .expect("Failed to convert new commit data into bounded vector"); + assert_ok!(SubtensorModule::do_commit_crv3_weights( + RuntimeOrigin::signed(hotkey1), + netuid, + bounded_new_commit_data, + reveal_round + )); }); }