@@ -19,78 +19,88 @@ impl<T: Config> Pallet<T> {
19
19
///
20
20
/// # Raises:
21
21
/// * `CommitRevealDisabled`:
22
- /// - Attempting to commit when the commit-reveal mechanism is disabled.
22
+ /// - Raised if commit-reveal is disabled for the specified network.
23
+ ///
24
+ /// * `HotKeyNotRegisteredInSubNet`:
25
+ /// - Raised if the hotkey is not registered on the specified network.
26
+ ///
27
+ /// * `CommittingWeightsTooFast`:
28
+ /// - Raised if the hotkey's commit rate exceeds the permitted limit.
23
29
///
24
30
/// * `TooManyUnrevealedCommits`:
25
- /// - Attempting to commit when the user has more than the allowed limit of unrevealed commits.
31
+ /// - Raised if the hotkey has reached the maximum number of unrevealed commits.
32
+ ///
33
+ /// # Events:
34
+ /// * `WeightsCommitted`:
35
+ /// - Emitted upon successfully storing the weight hash.
26
36
pub fn do_commit_weights (
27
37
origin : T :: RuntimeOrigin ,
28
38
netuid : u16 ,
29
39
commit_hash : H256 ,
30
40
) -> DispatchResult {
31
- // --- 1. Check the caller's signature (hotkey).
41
+ // 1. Verify the caller's signature (hotkey).
32
42
let who = ensure_signed ( origin) ?;
33
43
34
- log:: debug!( "do_commit_weights( hotkey:{:?} netuid:{:?})" , who, netuid) ;
44
+ log:: debug!( "do_commit_weights(hotkey: {:?}, netuid: {:?})" , who, netuid) ;
35
45
36
- // --- 2. Ensure commit-reveal is enabled for the network .
46
+ // 2. Ensure commit-reveal is enabled.
37
47
ensure ! (
38
48
Self :: get_commit_reveal_weights_enabled( netuid) ,
39
49
Error :: <T >:: CommitRevealDisabled
40
50
) ;
41
51
52
+ // 3. Ensure the hotkey is registered on the network.
42
53
ensure ! (
43
54
Self :: is_hotkey_registered_on_network( netuid, & who) ,
44
55
Error :: <T >:: HotKeyNotRegisteredInSubNet
45
56
) ;
46
57
47
- let commit_block: u64 = Self :: get_current_block_as_u64 ( ) ;
48
- let neuron_uid: u16 = Self :: get_uid_for_net_and_hotkey ( netuid, & who) ?;
58
+ // 4. Check that the commit rate does not exceed the allowed frequency.
59
+ let commit_block = Self :: get_current_block_as_u64 ( ) ;
60
+ let neuron_uid = Self :: get_uid_for_net_and_hotkey ( netuid, & who) ?;
49
61
ensure ! (
50
62
Self :: check_rate_limit( netuid, neuron_uid, commit_block) ,
51
63
Error :: <T >:: CommittingWeightsTooFast
52
64
) ;
53
65
54
- // --- 3 . Calculate the reveal blocks based on tempo and reveal period.
66
+ // 5 . Calculate the reveal blocks based on network tempo and reveal period.
55
67
let ( first_reveal_block, last_reveal_block) = Self :: get_reveal_blocks ( netuid, commit_block) ;
56
68
57
- // --- 4. Mutate the WeightCommits to retrieve existing commits for the user .
69
+ // 6. Retrieve or initialize the VecDeque of commits for the hotkey .
58
70
WeightCommits :: < T > :: try_mutate ( netuid, & who, |maybe_commits| -> DispatchResult {
59
- // --- 5. Take the existing commits or create a new VecDeque.
60
71
let mut commits: VecDeque < ( H256 , u64 , u64 , u64 ) > =
61
72
maybe_commits. take ( ) . unwrap_or_default ( ) ;
62
73
63
- // --- 6 . Remove any expired commits from the front of the queue.
74
+ // 7 . Remove any expired commits from the front of the queue.
64
75
while let Some ( ( _, commit_block_existing, _, _) ) = commits. front ( ) {
65
76
if Self :: is_commit_expired ( netuid, * commit_block_existing) {
66
- // Remove the expired commit
67
77
commits. pop_front ( ) ;
68
78
} else {
69
79
break ;
70
80
}
71
81
}
72
82
73
- // --- 7. Check if the current number of unrevealed commits is within the allowed limit.
83
+ // 8. Verify that the number of unrevealed commits is within the allowed limit.
74
84
ensure ! ( commits. len( ) < 10 , Error :: <T >:: TooManyUnrevealedCommits ) ;
75
85
76
- // --- 8 . Append the new commit to the queue .
86
+ // 9 . Append the new commit with calculated reveal blocks .
77
87
commits. push_back ( (
78
88
commit_hash,
79
89
commit_block,
80
90
first_reveal_block,
81
91
last_reveal_block,
82
92
) ) ;
83
93
84
- // --- 9 . Store the updated queue back to storage.
94
+ // 10 . Store the updated commits queue back to storage.
85
95
* maybe_commits = Some ( commits) ;
86
96
87
- // --- 10 . Emit the WeightsCommitted event.
97
+ // 11 . Emit the WeightsCommitted event
88
98
Self :: deposit_event ( Event :: WeightsCommitted ( who. clone ( ) , netuid, commit_hash) ) ;
89
99
90
- // --- 11. Set last update for the UID
100
+ // 12. Update the last commit block for the hotkey's UID.
91
101
Self :: set_last_update_for_uid ( netuid, neuron_uid, commit_block) ;
92
102
93
- // --- 12 . Return ok .
103
+ // 13 . Return success .
94
104
Ok ( ( ) )
95
105
} )
96
106
}
0 commit comments