@@ -3,7 +3,7 @@ use ark_serialize::CanonicalDeserialize;
33use codec:: Decode ;
44use frame_support:: { dispatch, traits:: OriginTrait } ;
55use scale_info:: prelude:: collections:: VecDeque ;
6- use subtensor_runtime_common:: NetUid ;
6+ use subtensor_runtime_common:: { NetUid , SubId } ;
77use 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 ( ( ) )
0 commit comments