Skip to content

Commit 50b7fa8

Browse files
authored
Merge pull request #1862 from opentensor/block-based-weight-mask-dereg-uids
Block-Based Commit-Reveal Weight Masking for Deregistered UIDs
2 parents a521e2f + b419935 commit 50b7fa8

File tree

11 files changed

+337
-310
lines changed

11 files changed

+337
-310
lines changed

pallets/subtensor/src/coinbase/reveal_commits.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ impl<T: Config> Pallet<T> {
3434
cur_epoch.saturating_sub(Self::get_reveal_period(netuid).saturating_sub(1));
3535

3636
// Clean expired commits
37-
for (epoch, _) in CRV3WeightCommits::<T>::iter_prefix(netuid) {
37+
for (epoch, _) in CRV3WeightCommitsV2::<T>::iter_prefix(netuid) {
3838
if epoch < reveal_epoch {
39-
CRV3WeightCommits::<T>::remove(netuid, epoch);
39+
CRV3WeightCommitsV2::<T>::remove(netuid, epoch);
4040
}
4141
}
4242

@@ -46,10 +46,12 @@ impl<T: Config> Pallet<T> {
4646
return Ok(());
4747
}
4848

49-
let mut entries = CRV3WeightCommits::<T>::take(netuid, reveal_epoch);
49+
let mut entries = CRV3WeightCommitsV2::<T>::take(netuid, reveal_epoch);
5050

5151
// Keep popping item off the end of the queue until we sucessfully reveal a commit.
52-
while let Some((who, serialized_compresssed_commit, round_number)) = entries.pop_front() {
52+
while let Some((who, _commit_block, serialized_compresssed_commit, round_number)) =
53+
entries.pop_front()
54+
{
5355
let reader = &mut &serialized_compresssed_commit[..];
5456
let commit = match TLECiphertext::<TinyBLS381>::deserialize_compressed(reader) {
5557
Ok(c) => c,

pallets/subtensor/src/epoch/run_epoch.rs

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -584,28 +584,49 @@ impl<T: Config> Pallet<T> {
584584
log::trace!("Weights (permit+diag+outdate): {:?}", &weights);
585585

586586
if Self::get_commit_reveal_weights_enabled(netuid) {
587-
// Precompute safe blocks for all UIDs
588-
let safe_blocks: Vec<u64> = block_at_registration
589-
.iter()
590-
.map(|reg_block| {
591-
let reg_epoch = Self::get_epoch_index(netuid, *reg_block);
592-
let safe_epoch =
593-
reg_epoch.saturating_add(Self::get_reveal_period(netuid).saturating_mul(2));
587+
let mut commit_blocks: Vec<u64> = vec![u64::MAX; n as usize]; // MAX ⇒ “no active commit”
594588

595-
Self::get_first_block_of_epoch(netuid, safe_epoch)
596-
})
597-
.collect();
589+
// helper: hotkey → uid
590+
let uid_of = |acct: &T::AccountId| -> Option<usize> {
591+
hotkeys
592+
.iter()
593+
.find(|(_, a)| a == acct)
594+
.map(|(uid, _)| *uid as usize)
595+
};
596+
597+
// ---------- v2 ------------------------------------------------------
598+
for (who, q) in WeightCommits::<T>::iter_prefix(netuid) {
599+
for (_, cb, _, _) in q.iter() {
600+
if !Self::is_commit_expired(netuid, *cb) {
601+
if let Some(i) = uid_of(&who) {
602+
commit_blocks[i] = commit_blocks[i].min(*cb);
603+
}
604+
break; // earliest active found
605+
}
606+
}
607+
}
608+
609+
// ---------- v3 ------------------------------------------------------
610+
for (_epoch, q) in CRV3WeightCommitsV2::<T>::iter_prefix(netuid) {
611+
for (who, cb, ..) in q.iter() {
612+
if !Self::is_commit_expired(netuid, *cb) {
613+
if let Some(i) = uid_of(who) {
614+
commit_blocks[i] = commit_blocks[i].min(*cb);
615+
}
616+
}
617+
}
618+
}
598619

599-
// Mask out weights to recently registered UIDs
600620
weights = vec_mask_sparse_matrix(
601621
&weights,
602-
&last_update,
603-
&safe_blocks,
604-
&|updated, safe_block| updated < safe_block,
622+
&commit_blocks,
623+
&block_at_registration,
624+
&|cb, reg| cb < reg,
605625
);
606626

607627
log::trace!(
608-
"Masking weights to miners inside reveal window (recent registrations masked)"
628+
"Commit-reveal column mask applied ({} masked rows)",
629+
commit_blocks.iter().filter(|&&cb| cb != u64::MAX).count()
609630
);
610631
}
611632

pallets/subtensor/src/lib.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1646,15 +1646,33 @@ pub mod pallet {
16461646
OptionQuery,
16471647
>;
16481648
#[pallet::storage]
1649-
/// --- MAP (netuid, commit_epoch) --> VecDeque<(who, serialized_compressed_commit, reveal_round)> | Stores a queue of v3 commits for an account on a given netuid.
1649+
/// MAP (netuid, epoch) → VecDeque<(who, ciphertext, reveal_round)>
1650+
/// DEPRECATED for CRV3WeightCommitsV2
16501651
pub type CRV3WeightCommits<T: Config> = StorageDoubleMap<
16511652
_,
16521653
Twox64Concat,
16531654
NetUid,
16541655
Twox64Concat,
1655-
u64,
1656+
u64, // epoch key
1657+
VecDeque<(
1658+
T::AccountId,
1659+
BoundedVec<u8, ConstU32<MAX_CRV3_COMMIT_SIZE_BYTES>>,
1660+
RoundNumber,
1661+
)>,
1662+
ValueQuery,
1663+
>;
1664+
#[pallet::storage]
1665+
/// MAP (netuid, epoch) → VecDeque<(who, commit_block, ciphertext, reveal_round)>
1666+
/// Stores a queue of v3 commits for an account on a given netuid.
1667+
pub type CRV3WeightCommitsV2<T: Config> = StorageDoubleMap<
1668+
_,
1669+
Twox64Concat,
1670+
NetUid,
1671+
Twox64Concat,
1672+
u64, // epoch key
16561673
VecDeque<(
16571674
T::AccountId,
1675+
u64, // commit_block
16581676
BoundedVec<u8, ConstU32<MAX_CRV3_COMMIT_SIZE_BYTES>>,
16591677
RoundNumber,
16601678
)>,

pallets/subtensor/src/macros/hooks.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,9 @@ mod hooks {
127127
// Migrate Subnet Identities to V3
128128
.saturating_add(migrations::migrate_subnet_identities_to_v3::migrate_subnet_identities_to_v3::<T>())
129129
// Migrate subnet symbols to fix the shift after subnet 81
130-
.saturating_add(migrations::migrate_subnet_symbols::migrate_subnet_symbols::<T>());
130+
.saturating_add(migrations::migrate_subnet_symbols::migrate_subnet_symbols::<T>())
131+
// Migrate CRV3 add commit_block
132+
.saturating_add(migrations::migrate_crv3_commits_add_block::migrate_crv3_commits_add_block::<T>());
131133
weight
132134
}
133135

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use super::*;
2+
use frame_support::{traits::Get, weights::Weight};
3+
use log;
4+
use scale_info::prelude::string::String;
5+
use sp_std::collections::vec_deque::VecDeque;
6+
7+
/// --------------- Migration ------------------------------------------
8+
/// Upgrades every entry to the new 4-tuple layout by inserting
9+
/// `commit_block = first_block_of_epoch(netuid, epoch)`.
10+
pub fn migrate_crv3_commits_add_block<T: Config>() -> Weight {
11+
let mig_name: Vec<u8> = b"crv3_commits_add_block_v1".to_vec();
12+
let mut total_weight = T::DbWeight::get().reads(1);
13+
14+
// run once
15+
if HasMigrationRun::<T>::get(&mig_name) {
16+
log::info!(
17+
"Migration '{}' already executed - skipping",
18+
String::from_utf8_lossy(&mig_name)
19+
);
20+
return total_weight;
21+
}
22+
log::info!("Running migration '{}'", String::from_utf8_lossy(&mig_name));
23+
24+
// iterate over *all* (netuid, epoch, queue) triples
25+
for (netuid, epoch, old_q) in CRV3WeightCommits::<T>::drain() {
26+
total_weight = total_weight.saturating_add(T::DbWeight::get().reads_writes(1, 1));
27+
28+
let commit_block = Pallet::<T>::get_first_block_of_epoch(netuid, epoch);
29+
30+
// convert VecDeque<(who,cipher,rnd)> → VecDeque<(who,cb,cipher,rnd)>
31+
let new_q: VecDeque<_> = old_q
32+
.into_iter()
33+
.map(|(who, cipher, rnd)| (who, commit_block, cipher, rnd))
34+
.collect();
35+
36+
// write back under *new* storage definition
37+
CRV3WeightCommitsV2::<T>::insert(netuid, epoch, new_q);
38+
}
39+
40+
// mark as done
41+
HasMigrationRun::<T>::insert(&mig_name, true);
42+
total_weight = total_weight.saturating_add(T::DbWeight::get().writes(1));
43+
44+
log::info!(
45+
"Migration '{}' completed",
46+
String::from_utf8_lossy(&mig_name)
47+
);
48+
total_weight
49+
}

pallets/subtensor/src/migrations/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub mod migrate_chain_identity;
88
pub mod migrate_coldkey_swap_scheduled;
99
pub mod migrate_commit_reveal_v2;
1010
pub mod migrate_create_root_network;
11+
pub mod migrate_crv3_commits_add_block;
1112
pub mod migrate_delete_subnet_21;
1213
pub mod migrate_delete_subnet_3;
1314
pub mod migrate_fix_is_network_member;

pallets/subtensor/src/subnets/weights.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,12 +269,12 @@ impl<T: Config> Pallet<T> {
269269
false => Self::get_epoch_index(netuid, cur_block),
270270
};
271271

272-
CRV3WeightCommits::<T>::try_mutate(netuid, cur_epoch, |commits| -> DispatchResult {
272+
CRV3WeightCommitsV2::<T>::try_mutate(netuid, cur_epoch, |commits| -> DispatchResult {
273273
// 6. Verify that the number of unrevealed commits is within the allowed limit.
274274

275275
let unrevealed_commits_for_who = commits
276276
.iter()
277-
.filter(|(account, _, _)| account == &who)
277+
.filter(|(account, _, _, _)| account == &who)
278278
.count();
279279
ensure!(
280280
unrevealed_commits_for_who < 10,
@@ -284,7 +284,7 @@ impl<T: Config> Pallet<T> {
284284
// 7. Append the new commit with calculated reveal blocks.
285285
// Hash the commit before it is moved, for the event
286286
let commit_hash = BlakeTwo256::hash(&commit);
287-
commits.push_back((who.clone(), commit, reveal_round));
287+
commits.push_back((who.clone(), cur_block, commit, reveal_round));
288288

289289
// 8. Emit the WeightsCommitted event
290290
Self::deposit_event(Event::CRV3WeightsCommitted(

0 commit comments

Comments
 (0)