Skip to content

Commit ee0756b

Browse files
committed
clean expired commits
1 parent ba03f23 commit ee0756b

File tree

2 files changed

+84
-13
lines changed

2 files changed

+84
-13
lines changed

pallets/subtensor/src/coinbase/run_coinbase.rs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,15 @@ impl<T: Config> Pallet<T> {
9393
// --- 4.1 Check to see if the subnet should run its epoch.
9494
if Self::should_run_epoch(*netuid, current_block) {
9595
// --- 4.2 Reveal weights from the n-2nd epoch.
96-
if let Err(e) = Self::reveal_crv3_commits(*netuid) {
97-
log::warn!(
98-
"Failed to reveal commits for subnet {} due to error: {:?}",
99-
*netuid,
100-
e
101-
);
102-
};
96+
if Self::get_commit_reveal_weights_enabled(*netuid) {
97+
if let Err(e) = Self::reveal_crv3_commits(*netuid) {
98+
log::warn!(
99+
"Failed to reveal commits for subnet {} due to error: {:?}",
100+
*netuid,
101+
e
102+
);
103+
};
104+
}
103105

104106
// --- 4.3 Drain the subnet emission.
105107
let mut subnet_emission: u64 = PendingEmission::<T>::get(*netuid);
@@ -204,8 +206,6 @@ impl<T: Config> Pallet<T> {
204206
}
205207

206208
/// The `reveal_crv3_commits` function is run at the very beginning of epoch `n`,
207-
/// revealing commitments from epoch `n - 2`.
208-
/// n - 2.
209209
pub fn reveal_crv3_commits(netuid: u16) -> dispatch::DispatchResult {
210210
use ark_serialize::CanonicalDeserialize;
211211
use frame_support::traits::OriginTrait;
@@ -216,16 +216,23 @@ impl<T: Config> Pallet<T> {
216216
let cur_block = Self::get_current_block_as_u64();
217217
let cur_epoch = Self::get_epoch_index(netuid, cur_block);
218218

219+
// Weights revealed must have been committed during epoch `cur_epoch - reveal_period`.
220+
let reveal_epoch =
221+
cur_epoch.saturating_sub(Self::get_reveal_period(netuid).saturating_sub(1));
222+
223+
// Clean expired commits
224+
for (epoch, _) in CRV3WeightCommits::<T>::iter_prefix(netuid) {
225+
if epoch < reveal_epoch {
226+
CRV3WeightCommits::<T>::remove(netuid, epoch);
227+
}
228+
}
229+
219230
// No commits to reveal until at least epoch 2.
220231
if cur_epoch < 2 {
221232
log::warn!("Failed to reveal commit for subnet {} Too early", netuid);
222233
return Ok(());
223234
}
224235

225-
// Weights revealed must have been committed during epoch `cur_epoch - reveal_period`.
226-
let reveal_epoch =
227-
cur_epoch.saturating_sub(Self::get_reveal_period(netuid).saturating_sub(1));
228-
229236
let mut entries = CRV3WeightCommits::<T>::take(netuid, reveal_epoch);
230237

231238
// Keep popping item off the end of the queue until we sucessfully reveal a commit.

pallets/subtensor/src/tests/weights.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5320,3 +5320,67 @@ fn test_multiple_commits_by_same_hotkey_within_limit() {
53205320
);
53215321
});
53225322
}
5323+
5324+
#[test]
5325+
fn test_reveal_crv3_commits_removes_past_epoch_commits() {
5326+
new_test_ext(100).execute_with(|| {
5327+
let netuid: u16 = 1;
5328+
let hotkey: AccountId = U256::from(1);
5329+
let reveal_round: u64 = 1000;
5330+
5331+
// Initialize network and neuron
5332+
add_network(netuid, 5, 0);
5333+
register_ok_neuron(netuid, hotkey, U256::from(2), 100_000);
5334+
SubtensorModule::set_commit_reveal_weights_enabled(netuid, true);
5335+
SubtensorModule::set_reveal_period(netuid, 1);
5336+
SubtensorModule::set_weights_set_rate_limit(netuid, 0);
5337+
5338+
let current_block = SubtensorModule::get_current_block_as_u64();
5339+
let current_epoch = SubtensorModule::get_epoch_index(netuid, current_block);
5340+
5341+
// Simulate commits in past epochs
5342+
let past_epochs = vec![current_epoch - 2, current_epoch - 1];
5343+
for epoch in &past_epochs {
5344+
let commit_data: Vec<u8> = vec![*epoch as u8; 5];
5345+
let bounded_commit_data = commit_data
5346+
.clone()
5347+
.try_into()
5348+
.expect("Failed to convert commit data into bounded vector");
5349+
assert_ok!(CRV3WeightCommits::<Test>::try_mutate(
5350+
netuid,
5351+
*epoch,
5352+
|commits| -> DispatchResult {
5353+
commits.push_back((hotkey, bounded_commit_data, reveal_round));
5354+
Ok(())
5355+
}
5356+
));
5357+
}
5358+
5359+
for epoch in &past_epochs {
5360+
let commits = CRV3WeightCommits::<Test>::get(netuid, *epoch);
5361+
assert!(
5362+
!commits.is_empty(),
5363+
"Expected commits to be present for past epoch {}",
5364+
epoch
5365+
);
5366+
}
5367+
5368+
assert_ok!(SubtensorModule::reveal_crv3_commits(netuid));
5369+
5370+
for epoch in &past_epochs {
5371+
let commits = CRV3WeightCommits::<Test>::get(netuid, *epoch);
5372+
assert!(
5373+
commits.is_empty(),
5374+
"Expected commits for past epoch {} to be removed",
5375+
epoch
5376+
);
5377+
}
5378+
5379+
let current_epoch_commits = CRV3WeightCommits::<Test>::get(netuid, current_epoch);
5380+
assert!(
5381+
current_epoch_commits.is_empty(),
5382+
"Expected no commits for current epoch {}",
5383+
current_epoch
5384+
);
5385+
});
5386+
}

0 commit comments

Comments
 (0)