Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crv3 Hotfix 2 #1061

Merged
merged 1 commit into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion pallets/subtensor/src/subnets/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,15 @@ impl<T: Config> Pallet<T> {
let cur_epoch = Self::get_epoch_index(netuid, cur_block);
CRV3WeightCommits::<T>::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::<T>::TooManyUnrevealedCommits);

let unrevealed_commits_for_who = commits
.iter()
.filter(|(account, _, _)| account == &who)
.count();
ensure!(
unrevealed_commits_for_who < 10,
Error::<T>::TooManyUnrevealedCommits
);

// 7. Append the new commit with calculated reveal blocks.
// Hash the commit before it is moved, for the event
Expand Down
85 changes: 71 additions & 14 deletions pallets/subtensor/src/tests/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8> = 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::<Test>::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<u8> = 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::<Test>::TooManyUnrevealedCommits
);

// Hotkey2 can still submit commits independently
let commit_data_hotkey2: Vec<u8> = 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<u8> = 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<u8> = 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::<Test>::TooManyUnrevealedCommits
);

step_epochs(10, netuid);

let new_commit_data: Vec<u8> = 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
));
});
}

Expand Down
Loading