Skip to content

Commit

Permalink
fix do_set_weights to use commit_block in commit reveal
Browse files Browse the repository at this point in the history
  • Loading branch information
andreea-popescu-reef committed Nov 7, 2024
1 parent e6683ab commit a1d9d37
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
10 changes: 9 additions & 1 deletion pallets/subtensor/src/macros/dispatches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,15 @@ mod dispatches {
version_key: u64,
) -> DispatchResult {
if !Self::get_commit_reveal_weights_enabled(netuid) {
return Self::do_set_weights(origin, netuid, dests, weights, version_key);
let current_block: u64 = Self::get_current_block_as_u64();
return Self::do_set_weights(
origin,
netuid,
dests,
weights,
version_key,
current_block,
);
}

Err(Error::<T>::CommitRevealEnabled.into())
Expand Down
33 changes: 25 additions & 8 deletions pallets/subtensor/src/subnets/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,13 @@ impl<T: Config> Pallet<T> {
.position(|(hash, _, _, _)| *hash == provided_hash)
{
// --- 8. Get the commit block for the commit being revealed.
let (_, commit_block, _, _) = commits
let (_, commit_block, _, _) = *commits
.get(position)
.ok_or(Error::<T>::NoWeightsCommitFound)?;

// --- 9. Ensure the commit is ready to be revealed in the current block range.
ensure!(
Self::is_reveal_block_range(netuid, *commit_block),
Self::is_reveal_block_range(netuid, commit_block),
Error::<T>::RevealTooEarly
);

Expand All @@ -225,7 +225,14 @@ impl<T: Config> Pallet<T> {
}

// --- 12. Proceed to set the revealed weights.
Self::do_set_weights(origin, netuid, uids.clone(), values.clone(), version_key)?;
Self::do_set_weights(
origin,
netuid,
uids.clone(),
values.clone(),
version_key,
commit_block,
)?;

// --- 13. Emit the WeightsRevealed event.
Self::deposit_event(Event::WeightsRevealed(who.clone(), netuid, provided_hash));
Expand Down Expand Up @@ -391,10 +398,18 @@ impl<T: Config> Pallet<T> {
.position(|(hash, _, _, _)| *hash == provided_hash)
{
// --- 8b. Remove the commit from the queue.
commits.remove(position);
let (_, commit_block, _, _) =
commits.remove(position).expect("commit_block exists");

// --- 8c. Proceed to set the revealed weights.
Self::do_set_weights(origin.clone(), netuid, uids, values, version_key)?;
Self::do_set_weights(
origin.clone(),
netuid,
uids,
values,
version_key,
commit_block,
)?;

// --- 8d. Collect the revealed hash.
revealed_hashes.push(provided_hash);
Expand Down Expand Up @@ -484,15 +499,17 @@ impl<T: Config> Pallet<T> {
uids: Vec<u16>,
values: Vec<u16>,
version_key: u64,
block: u64,
) -> dispatch::DispatchResult {
// --- 1. Check the caller's signature. This is the hotkey of a registered account.
let hotkey = ensure_signed(origin)?;
log::debug!(
"do_set_weights( origin:{:?} netuid:{:?}, uids:{:?}, values:{:?})",
"do_set_weights( origin:{:?} netuid:{:?}, uids:{:?}, values:{:?}, block:{:?})",
hotkey,
netuid,
uids,
values
values,
block
);

// --- Check that the netuid is not the root network.
Expand Down Expand Up @@ -588,7 +605,7 @@ impl<T: Config> Pallet<T> {

// --- 18. Set the activity for the weights on this network.
if !Self::get_commit_reveal_weights_enabled(netuid) {
Self::set_last_update_for_uid(netuid, neuron_uid, current_block);
Self::set_last_update_for_uid(netuid, neuron_uid, block);
}

// --- 19. Emit the tracking event.
Expand Down
4 changes: 4 additions & 0 deletions pallets/subtensor/tests/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1554,6 +1554,7 @@ fn test_commit_reveal_tempo_interval() {
),
Error::<Test>::NoWeightsCommitFound
);
assert_eq!(SubtensorModule::get_last_update(netuid)[1], 0);

assert_ok!(SubtensorModule::commit_weights(
RuntimeOrigin::signed(hotkey),
Expand All @@ -1575,6 +1576,7 @@ fn test_commit_reveal_tempo_interval() {
),
Error::<Test>::ExpiredWeightCommit
);
assert_eq!(SubtensorModule::get_last_update(netuid)[1], 105);

assert_ok!(SubtensorModule::commit_weights(
RuntimeOrigin::signed(hotkey),
Expand All @@ -1595,6 +1597,7 @@ fn test_commit_reveal_tempo_interval() {
),
Error::<Test>::RevealTooEarly
);
assert_eq!(SubtensorModule::get_last_update(netuid)[1], 301);

step_epochs(1, netuid);

Expand All @@ -1606,6 +1609,7 @@ fn test_commit_reveal_tempo_interval() {
salt,
version_key,
));
assert_eq!(SubtensorModule::get_last_update(netuid)[1], 301);
});
}

Expand Down

0 comments on commit a1d9d37

Please sign in to comment.