Skip to content

Commit df6e32b

Browse files
committed
Convert TimelockedWeightCommits to be per-subnet and use NetUidStorageIndex
1 parent c4d5f98 commit df6e32b

File tree

10 files changed

+238
-194
lines changed

10 files changed

+238
-194
lines changed

pallets/subtensor/src/coinbase/reveal_commits.rs

Lines changed: 142 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use ark_serialize::CanonicalDeserialize;
33
use codec::Decode;
44
use frame_support::{dispatch, traits::OriginTrait};
55
use scale_info::prelude::collections::VecDeque;
6-
use subtensor_runtime_common::NetUid;
6+
use subtensor_runtime_common::{NetUid, SubId};
77
use tle::{
88
curves::drand::TinyBLS381,
99
stream_ciphers::AESGCMStreamCipherProvider,
@@ -44,152 +44,159 @@ impl<T: Config> Pallet<T> {
4444
// Weights revealed must have been committed during epoch `cur_epoch - reveal_period`.
4545
let reveal_epoch = cur_epoch.saturating_sub(reveal_period);
4646

47-
// Clean expired commits
48-
for (epoch, _) in TimelockedWeightCommits::<T>::iter_prefix(netuid) {
49-
if epoch < reveal_epoch {
50-
TimelockedWeightCommits::<T>::remove(netuid, epoch);
51-
}
52-
}
47+
// All subsubnets share the same epoch, so the reveal_period/reveal_epoch are also the same
48+
// Reveal for all subsubnets
49+
for subid in 0..SubsubnetCountCurrent::<T>::get(netuid).into() {
50+
let netuid_index = Self::get_subsubnet_storage_index(netuid, subid.into());
5351

54-
// No commits to reveal until at least epoch reveal_period.
55-
if cur_epoch < reveal_period {
56-
log::trace!("Failed to reveal commit for subnet {netuid} Too early");
57-
return Ok(());
58-
}
59-
60-
let mut entries = TimelockedWeightCommits::<T>::take(netuid, reveal_epoch);
61-
let mut unrevealed = VecDeque::new();
62-
63-
// Keep popping items off the front of the queue until we successfully reveal a commit.
64-
while let Some((who, commit_block, serialized_compresssed_commit, round_number)) =
65-
entries.pop_front()
66-
{
67-
// Try to get the round number from pallet_drand.
68-
let pulse = match pallet_drand::Pulses::<T>::get(round_number) {
69-
Some(p) => p,
70-
None => {
71-
// Round number used was not found on the chain. Skip this commit.
72-
log::trace!(
73-
"Failed to reveal commit for subnet {netuid} submitted by {who:?} on block {commit_block} due to missing round number {round_number}; will retry every block in reveal epoch."
74-
);
75-
unrevealed.push_back((
76-
who,
77-
commit_block,
78-
serialized_compresssed_commit,
79-
round_number,
80-
));
81-
continue;
52+
// Clean expired commits
53+
for (epoch, _) in TimelockedWeightCommits::<T>::iter_prefix(netuid_index) {
54+
if epoch < reveal_epoch {
55+
TimelockedWeightCommits::<T>::remove(netuid_index, epoch);
8256
}
83-
};
57+
}
8458

85-
let reader = &mut &serialized_compresssed_commit[..];
86-
let commit = match TLECiphertext::<TinyBLS381>::deserialize_compressed(reader) {
87-
Ok(c) => c,
88-
Err(e) => {
89-
log::trace!(
90-
"Failed to reveal commit for subnet {netuid} submitted by {who:?} due to error deserializing the commit: {e:?}"
91-
);
92-
continue;
93-
}
94-
};
95-
96-
let signature_bytes = pulse
97-
.signature
98-
.strip_prefix(b"0x")
99-
.unwrap_or(&pulse.signature);
100-
101-
let sig_reader = &mut &signature_bytes[..];
102-
let sig = match <TinyBLS381 as EngineBLS>::SignatureGroup::deserialize_compressed(
103-
sig_reader,
104-
) {
105-
Ok(s) => s,
106-
Err(e) => {
107-
log::trace!(
108-
"Failed to reveal commit for subnet {netuid} submitted by {who:?} due to error deserializing signature from drand pallet: {e:?}"
109-
);
110-
continue;
111-
}
112-
};
59+
// No commits to reveal until at least epoch reveal_period.
60+
if cur_epoch < reveal_period {
61+
log::trace!("Failed to reveal commit for subsubnet {netuid_index} Too early");
62+
return Ok(());
63+
}
11364

114-
let decrypted_bytes: Vec<u8> = match tld::<TinyBLS381, AESGCMStreamCipherProvider>(
115-
commit, sig,
116-
) {
117-
Ok(d) => d,
118-
Err(e) => {
119-
log::trace!(
120-
"Failed to reveal commit for subnet {netuid} submitted by {who:?} due to error decrypting the commit: {e:?}"
121-
);
122-
continue;
123-
}
124-
};
125-
126-
// ------------------------------------------------------------------
127-
// Try to decode payload with the new and legacy formats.
128-
// ------------------------------------------------------------------
129-
let (uids, values, version_key) = {
130-
let mut reader_new = &decrypted_bytes[..];
131-
if let Ok(payload) = WeightsTlockPayload::decode(&mut reader_new) {
132-
// Verify hotkey matches committer
133-
let mut hk_reader = &payload.hotkey[..];
134-
match T::AccountId::decode(&mut hk_reader) {
135-
Ok(decoded_hotkey) if decoded_hotkey == who => {
136-
(payload.uids, payload.values, payload.version_key)
137-
}
138-
Ok(_) => {
139-
log::trace!(
140-
"Failed to reveal commit for subnet {netuid} submitted by {who:?} due to hotkey mismatch in payload"
141-
);
142-
continue;
143-
}
144-
Err(e) => {
145-
let mut reader_legacy = &decrypted_bytes[..];
146-
match LegacyWeightsTlockPayload::decode(&mut reader_legacy) {
147-
Ok(legacy) => (legacy.uids, legacy.values, legacy.version_key),
148-
Err(_) => {
149-
log::trace!(
150-
"Failed to reveal commit for subnet {netuid} submitted by {who:?} due to error deserializing hotkey: {e:?}"
151-
);
152-
continue;
65+
let mut entries = TimelockedWeightCommits::<T>::take(netuid_index, reveal_epoch);
66+
let mut unrevealed = VecDeque::new();
67+
68+
// Keep popping items off the front of the queue until we successfully reveal a commit.
69+
while let Some((who, commit_block, serialized_compresssed_commit, round_number)) =
70+
entries.pop_front()
71+
{
72+
// Try to get the round number from pallet_drand.
73+
let pulse = match pallet_drand::Pulses::<T>::get(round_number) {
74+
Some(p) => p,
75+
None => {
76+
// Round number used was not found on the chain. Skip this commit.
77+
log::trace!(
78+
"Failed to reveal commit for subsubnet {netuid_index} submitted by {who:?} on block {commit_block} due to missing round number {round_number}; will retry every block in reveal epoch."
79+
);
80+
unrevealed.push_back((
81+
who,
82+
commit_block,
83+
serialized_compresssed_commit,
84+
round_number,
85+
));
86+
continue;
87+
}
88+
};
89+
90+
let reader = &mut &serialized_compresssed_commit[..];
91+
let commit = match TLECiphertext::<TinyBLS381>::deserialize_compressed(reader) {
92+
Ok(c) => c,
93+
Err(e) => {
94+
log::trace!(
95+
"Failed to reveal commit for subsubnet {netuid_index} submitted by {who:?} due to error deserializing the commit: {e:?}"
96+
);
97+
continue;
98+
}
99+
};
100+
101+
let signature_bytes = pulse
102+
.signature
103+
.strip_prefix(b"0x")
104+
.unwrap_or(&pulse.signature);
105+
106+
let sig_reader = &mut &signature_bytes[..];
107+
let sig = match <TinyBLS381 as EngineBLS>::SignatureGroup::deserialize_compressed(
108+
sig_reader,
109+
) {
110+
Ok(s) => s,
111+
Err(e) => {
112+
log::trace!(
113+
"Failed to reveal commit for subsubnet {netuid_index} submitted by {who:?} due to error deserializing signature from drand pallet: {e:?}"
114+
);
115+
continue;
116+
}
117+
};
118+
119+
let decrypted_bytes: Vec<u8> = match tld::<TinyBLS381, AESGCMStreamCipherProvider>(
120+
commit, sig,
121+
) {
122+
Ok(d) => d,
123+
Err(e) => {
124+
log::trace!(
125+
"Failed to reveal commit for subsubnet {netuid_index} submitted by {who:?} due to error decrypting the commit: {e:?}"
126+
);
127+
continue;
128+
}
129+
};
130+
131+
// ------------------------------------------------------------------
132+
// Try to decode payload with the new and legacy formats.
133+
// ------------------------------------------------------------------
134+
let (uids, values, version_key) = {
135+
let mut reader_new = &decrypted_bytes[..];
136+
if let Ok(payload) = WeightsTlockPayload::decode(&mut reader_new) {
137+
// Verify hotkey matches committer
138+
let mut hk_reader = &payload.hotkey[..];
139+
match T::AccountId::decode(&mut hk_reader) {
140+
Ok(decoded_hotkey) if decoded_hotkey == who => {
141+
(payload.uids, payload.values, payload.version_key)
142+
}
143+
Ok(_) => {
144+
log::trace!(
145+
"Failed to reveal commit for subsubnet {netuid_index} submitted by {who:?} due to hotkey mismatch in payload"
146+
);
147+
continue;
148+
}
149+
Err(e) => {
150+
let mut reader_legacy = &decrypted_bytes[..];
151+
match LegacyWeightsTlockPayload::decode(&mut reader_legacy) {
152+
Ok(legacy) => (legacy.uids, legacy.values, legacy.version_key),
153+
Err(_) => {
154+
log::trace!(
155+
"Failed to reveal commit for subsubnet {netuid_index} submitted by {who:?} due to error deserializing hotkey: {e:?}"
156+
);
157+
continue;
158+
}
153159
}
154160
}
155161
}
156-
}
157-
} else {
158-
// Fallback to legacy payload
159-
let mut reader_legacy = &decrypted_bytes[..];
160-
match LegacyWeightsTlockPayload::decode(&mut reader_legacy) {
161-
Ok(legacy) => (legacy.uids, legacy.values, legacy.version_key),
162-
Err(e) => {
163-
log::trace!(
164-
"Failed to reveal commit for subnet {netuid} submitted by {who:?} due to error deserializing both payload formats: {e:?}"
165-
);
166-
continue;
162+
} else {
163+
// Fallback to legacy payload
164+
let mut reader_legacy = &decrypted_bytes[..];
165+
match LegacyWeightsTlockPayload::decode(&mut reader_legacy) {
166+
Ok(legacy) => (legacy.uids, legacy.values, legacy.version_key),
167+
Err(e) => {
168+
log::trace!(
169+
"Failed to reveal commit for subsubnet {netuid_index} submitted by {who:?} due to error deserializing both payload formats: {e:?}"
170+
);
171+
continue;
172+
}
167173
}
168174
}
175+
};
176+
177+
// ------------------------------------------------------------------
178+
// Apply weights
179+
// ------------------------------------------------------------------
180+
if let Err(e) = Self::do_set_sub_weights(
181+
T::RuntimeOrigin::signed(who.clone()),
182+
netuid,
183+
SubId::from(subid),
184+
uids,
185+
values,
186+
version_key,
187+
) {
188+
log::trace!(
189+
"Failed to `do_set_sub_weights` for subsubnet {netuid_index} submitted by {who:?}: {e:?}"
190+
);
191+
continue;
169192
}
170-
};
171-
172-
// ------------------------------------------------------------------
173-
// Apply weights
174-
// ------------------------------------------------------------------
175-
if let Err(e) = Self::do_set_weights(
176-
T::RuntimeOrigin::signed(who.clone()),
177-
netuid,
178-
uids,
179-
values,
180-
version_key,
181-
) {
182-
log::trace!(
183-
"Failed to `do_set_weights` for subnet {netuid} submitted by {who:?}: {e:?}"
184-
);
185-
continue;
186-
}
187193

188-
Self::deposit_event(Event::TimelockedWeightsRevealed(netuid, who));
189-
}
194+
Self::deposit_event(Event::TimelockedWeightsRevealed(netuid_index, who));
195+
}
190196

191-
if !unrevealed.is_empty() {
192-
TimelockedWeightCommits::<T>::insert(netuid, reveal_epoch, unrevealed);
197+
if !unrevealed.is_empty() {
198+
TimelockedWeightCommits::<T>::insert(netuid_index, reveal_epoch, unrevealed);
199+
}
193200
}
194201

195202
Ok(())

pallets/subtensor/src/epoch/run_epoch.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,7 @@ impl<T: Config> Pallet<T> {
750750
}
751751

752752
// ---------- v3 ------------------------------------------------------
753-
for (_epoch, q) in TimelockedWeightCommits::<T>::iter_prefix(netuid) {
753+
for (_epoch, q) in TimelockedWeightCommits::<T>::iter_prefix(netuid_index) {
754754
for (who, cb, ..) in q.iter() {
755755
if !Self::is_commit_expired(netuid, *cb) {
756756
if let Some(cell) = uid_of(who).and_then(|i| commit_blocks.get_mut(i)) {

pallets/subtensor/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1689,7 +1689,7 @@ pub mod pallet {
16891689
pub type TimelockedWeightCommits<T: Config> = StorageDoubleMap<
16901690
_,
16911691
Twox64Concat,
1692-
NetUid,
1692+
NetUidStorageIndex,
16931693
Twox64Concat,
16941694
u64, // epoch key
16951695
VecDeque<(
@@ -1706,7 +1706,7 @@ pub mod pallet {
17061706
pub type CRV3WeightCommits<T: Config> = StorageDoubleMap<
17071707
_,
17081708
Twox64Concat,
1709-
NetUid,
1709+
NetUidStorageIndex,
17101710
Twox64Concat,
17111711
u64, // epoch key
17121712
VecDeque<(
@@ -1722,7 +1722,7 @@ pub mod pallet {
17221722
pub type CRV3WeightCommitsV2<T: Config> = StorageDoubleMap<
17231723
_,
17241724
Twox64Concat,
1725-
NetUid,
1725+
NetUidStorageIndex,
17261726
Twox64Concat,
17271727
u64, // epoch key
17281728
VecDeque<(

pallets/subtensor/src/macros/events.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,12 +406,12 @@ mod events {
406406
/// - **netuid**: The network identifier.
407407
/// - **commit_hash**: The hash representing the committed weights.
408408
/// - **reveal_round**: The round at which weights can be revealed.
409-
TimelockedWeightsCommitted(T::AccountId, NetUid, H256, u64),
409+
TimelockedWeightsCommitted(T::AccountId, NetUidStorageIndex, H256, u64),
410410

411411
/// Timelocked Weights have been successfully revealed.
412412
///
413413
/// - **netuid**: The network identifier.
414414
/// - **who**: The account ID of the user revealing the weights.
415-
TimelockedWeightsRevealed(NetUid, T::AccountId),
415+
TimelockedWeightsRevealed(NetUidStorageIndex, T::AccountId),
416416
}
417417
}

pallets/subtensor/src/migrations/migrate_crv3_commits_add_block.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ pub fn migrate_crv3_commits_add_block<T: Config>() -> Weight {
2222
log::info!("Running migration '{}'", String::from_utf8_lossy(&mig_name));
2323

2424
// iterate over *all* (netuid, epoch, queue) triples
25-
for (netuid, epoch, old_q) in CRV3WeightCommits::<T>::drain() {
25+
for (netuid_index, epoch, old_q) in CRV3WeightCommits::<T>::drain() {
2626
total_weight = total_weight.saturating_add(T::DbWeight::get().reads_writes(1, 1));
2727

28+
let (netuid, _) = Pallet::<T>::get_netuid_and_subid(netuid_index).unwrap_or_default();
2829
let commit_block = Pallet::<T>::get_first_block_of_epoch(netuid, epoch);
2930

3031
// convert VecDeque<(who,cipher,rnd)> → VecDeque<(who,cb,cipher,rnd)>
@@ -34,7 +35,7 @@ pub fn migrate_crv3_commits_add_block<T: Config>() -> Weight {
3435
.collect();
3536

3637
// write back under *new* storage definition
37-
CRV3WeightCommitsV2::<T>::insert(netuid, epoch, new_q);
38+
CRV3WeightCommitsV2::<T>::insert(netuid_index, epoch, new_q);
3839
}
3940

4041
// mark as done

pallets/subtensor/src/subnets/subsubnet.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,13 @@ impl<T: Config> Pallet<T> {
142142

143143
// Cleanup WeightCommits
144144
let _ = WeightCommits::<T>::clear_prefix(netuid_index, u32::MAX, None);
145+
146+
// Cleanup TimelockedWeightCommits
147+
let _ = TimelockedWeightCommits::<T>::clear_prefix(
148+
netuid_index,
149+
u32::MAX,
150+
None,
151+
);
145152
}
146153
}
147154

0 commit comments

Comments
 (0)