Skip to content

Commit

Permalink
lints
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel Dare committed Jul 2, 2024
1 parent 099c405 commit 5d670f8
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pallets/subtensor/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ impl<T: Config> Pallet<T> {
// Check if the nonce already exists
ensure!(!commits.contains_key(&nonce), Error::<T>::DuplicateNonce);

// Check if there are already 10 commits
if commits.len() >= 10 {
// Remove the oldest commit (lowest nonce)
if let Some(oldest_nonce) = commits.keys().next().cloned() {
commits.remove(&oldest_nonce);
}
}

// Insert the new commit
commits.insert(
nonce,
(
Expand Down
98 changes: 98 additions & 0 deletions pallets/subtensor/tests/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use sp_runtime::{
DispatchError,
};
use substrate_fixed::types::I32F32;
use pallet_subtensor::WeightCommits;

/***************************
pub fn set_weights() tests
Expand Down Expand Up @@ -1958,6 +1959,103 @@ fn test_multiple_commit_reveal() {
});
}

#[test]
fn test_do_commit_weights_limit_to_ten_commits() {
new_test_ext(1).execute_with(|| {
let netuid: u16 = 1;
let hotkey = U256::from(1);
let coldkey = U256::from(2);
add_network(netuid, 0, 0);
register_ok_neuron(netuid, hotkey, coldkey, 0);
SubtensorModule::set_commit_reveal_weights_enabled(netuid, true);

// Submit 11 commits
for i in 1..=11 {
let commit_hash = H256::from_low_u64_be(i);
assert_ok!(SubtensorModule::do_commit_weights(
RuntimeOrigin::signed(hotkey),
netuid,
commit_hash,
i
));
}

// Check that only 10 commits are stored
let commits = WeightCommits::<Test>::get(netuid, hotkey);
assert_eq!(commits.len(), 10, "Should only store 10 commits");

// Check that the oldest commit (nonce 1) was removed
assert!(!commits.contains_key(&1), "Oldest commit should be removed");

// Check that the newest commit (nonce 11) is present
assert!(commits.contains_key(&11), "Newest commit should be present");
});
}

#[test]
fn test_do_commit_weights_maintains_monotonicity() {
new_test_ext(1).execute_with(|| {
let netuid: u16 = 1;
let hotkey = U256::from(1);
let coldkey = U256::from(2);
add_network(netuid, 0, 0);
register_ok_neuron(netuid, hotkey, coldkey, 0);
SubtensorModule::set_commit_reveal_weights_enabled(netuid, true);

// Submit 10 commits
for i in 1..=10 {
let commit_hash = H256::from_low_u64_be(i);
assert_ok!(SubtensorModule::do_commit_weights(
RuntimeOrigin::signed(hotkey),
netuid,
commit_hash,
i
));
}

// Try to submit a commit with a lower nonce
let commit_hash = H256::from_low_u64_be(5);
assert_err!(
SubtensorModule::do_commit_weights(RuntimeOrigin::signed(hotkey), netuid, commit_hash, 5),
Error::<Test>::NonMonotonicNonce
);

// Submit a commit with a higher nonce
let commit_hash = H256::from_low_u64_be(11);
assert_ok!(SubtensorModule::do_commit_weights(
RuntimeOrigin::signed(hotkey),
netuid,
commit_hash,
11
));

// Check that the new commit is present and the oldest one is removed
let commits = WeightCommits::<Test>::get(netuid, hotkey);
assert!(!commits.contains_key(&1), "Oldest commit should be removed");
assert!(commits.contains_key(&11), "Newest commit should be present");
});
}


#[test]
fn test_do_commit_weights_commit_reveal_disabled() {
new_test_ext(1).execute_with(|| {
let netuid: u16 = 1;
let hotkey = U256::from(1);
let coldkey = U256::from(2);
add_network(netuid, 0, 0);
register_ok_neuron(netuid, hotkey, coldkey, 0);
SubtensorModule::set_commit_reveal_weights_enabled(netuid, false);

// Try to submit a commit when commit/reveal is disabled
let commit_hash = H256::from_low_u64_be(1);
assert_err!(
SubtensorModule::do_commit_weights(RuntimeOrigin::signed(hotkey), netuid, commit_hash, 1),
Error::<Test>::CommitRevealDisabled
);
});
}

fn commit_reveal_set_weights(
hotkey: U256,
netuid: u16,
Expand Down

0 comments on commit 5d670f8

Please sign in to comment.