@@ -16,85 +16,135 @@ use crate::{Command, DkgSignature, DkgVerifyingKey};
16
16
use anyhow:: { Context , Result } ;
17
17
use cumulus_primitives_core:: relay_chain:: ValidatorId ;
18
18
use futures:: {
19
- channel:: { mpsc, oneshot} ,
20
- SinkExt ,
19
+ channel:: { mpsc, oneshot} ,
20
+ SinkExt ,
21
21
} ;
22
+
22
23
use std:: fmt:: Debug ;
23
24
24
- /// `Service` serves as an intermediary to interact with the Worker, handling requests and
25
- /// facilitating communication. It mainly operates on the message passing mechanism between service
26
- /// and worker.
25
+ /// `Service` acts as an intermediary for interacting with a Worker. It handles requests and
26
+ /// facilitates communication between the service and the worker through a message-passing mechanism.
27
+ ///
28
+ /// This struct is primarily used for sending various commands to the worker, such as key rotation,
29
+ /// starting a signing process, setting up validator network parameters, and managing validators.
27
30
#[ derive( Clone ) ]
28
31
pub struct Service {
29
- // Channel sender to send messages to the worker.
30
- to_worker : mpsc:: Sender < Command > ,
32
+ // Channel sender used to send commands to the worker.
33
+ to_worker : mpsc:: Sender < Command > ,
31
34
}
32
35
33
36
impl Debug for Service {
34
- /// Provides a human-readable representation of the Service, useful for debugging.
35
- fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
36
- f. debug_tuple ( "ValidatorNetworkService" ) . finish ( )
37
- }
37
+ /// Provides a custom formatter for the `Service` struct, aiding in debugging by giving a
38
+ /// human-readable representation of the service. This method outputs the struct's name,
39
+ /// "ValidatorNetworkService", for simplicity.
40
+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
41
+ f. debug_tuple ( "ValidatorNetworkService" ) . finish ( )
42
+ }
38
43
}
39
44
40
45
impl Service {
41
- /// Creates a new `Service` instance.
42
- pub ( crate ) fn new ( to_worker : mpsc:: Sender < Command > ) -> Self {
43
- Self { to_worker }
44
- }
46
+ /// Constructs a new instance of `Service`.
47
+ ///
48
+ /// # Arguments
49
+ ///
50
+ /// * `to_worker` - A sender channel used for sending commands to the worker.
51
+ pub ( crate ) fn new ( to_worker : mpsc:: Sender < Command > ) -> Self {
52
+ Self { to_worker }
53
+ }
45
54
46
- /// 轮换密钥,返回新的验证者公钥
47
- pub async fn rotate_key ( & self ) -> Result < DkgVerifyingKey > {
48
- let ( sender, receiver) = oneshot:: channel ( ) ;
49
- self . to_worker
50
- . clone ( )
51
- . send ( Command :: RotateKey { sender } )
52
- . await
53
- . context ( "Failed to send command to worker" ) ?;
54
- receiver. await . context ( "Failed to receive response from worker" ) ?
55
- }
55
+ /// Initiates a key rotation process, resulting in a new verifier public key.
56
+ ///
57
+ /// This method sends a `RotateKey` command to the worker and awaits the response.
58
+ ///
59
+ /// # Returns
60
+ ///
61
+ /// A `Result` which, on success, contains the new `DkgVerifyingKey`.
62
+ pub async fn rotate_key ( & self ) -> Result < DkgVerifyingKey > {
63
+ let ( sender, receiver) = oneshot:: channel ( ) ;
64
+ self . to_worker
65
+ . clone ( )
66
+ . send ( Command :: RotateKey { sender } )
67
+ . await
68
+ . context ( "Failed to send command to worker" ) ?;
69
+ receiver. await . context ( "Failed to receive response from worker" ) ?
70
+ }
56
71
57
- /// 启动一个签名服务,返回签名
58
- pub async fn start_signing ( & self , message : & [ u8 ] ) -> Result < DkgSignature > {
59
- let ( sender, receiver) = oneshot:: channel ( ) ;
60
- self . to_worker
61
- . clone ( )
62
- . send ( Command :: Sign { message : message. to_vec ( ) , sender } )
63
- . await
64
- . context ( "Failed to send command to worker" ) ?;
65
- receiver. await . context ( "Failed to receive response from worker" ) ?
66
- }
72
+ /// Starts a signing service and returns a signature.
73
+ ///
74
+ /// This method sends a `Sign` command with the provided message to the worker and waits for the signature.
75
+ ///
76
+ /// # Arguments
77
+ ///
78
+ /// * `message` - A byte slice representing the message to be signed.
79
+ ///
80
+ /// # Returns
81
+ ///
82
+ /// A `Result` which, on success, contains the `DkgSignature`.
83
+ pub async fn start_signing ( & self , message : & [ u8 ] ) -> Result < DkgSignature > {
84
+ let ( sender, receiver) = oneshot:: channel ( ) ;
85
+ self . to_worker
86
+ . clone ( )
87
+ . send ( Command :: Sign { message : message. to_vec ( ) , sender } )
88
+ . await
89
+ . context ( "Failed to send command to worker" ) ?;
90
+ receiver. await . context ( "Failed to receive response from worker" ) ?
91
+ }
67
92
68
- /// 设置验证者网络的阈值和参与者总数
69
- pub async fn setup ( & self , nt : ( u16 , u16 ) ) -> Result < ( ) > {
70
- let ( sender, receiver) = oneshot:: channel ( ) ;
71
- self . to_worker
72
- . clone ( )
73
- . send ( Command :: Setup { nt, sender } )
74
- . await
75
- . context ( "Failed to send command to worker" ) ?;
76
- receiver. await . context ( "Failed to receive response from worker" ) ?
77
- }
93
+ /// Sets up the validator network with specified threshold and total number of participants.
94
+ ///
95
+ /// # Arguments
96
+ ///
97
+ /// * `nt` - A tuple (u16, u16) where the first element is the threshold and the second is the total number of participants.
98
+ ///
99
+ /// # Returns
100
+ ///
101
+ /// A `Result` indicating the success or failure of the operation.
102
+ pub async fn setup ( & self , nt : ( u16 , u16 ) ) -> Result < ( ) > {
103
+ let ( sender, receiver) = oneshot:: channel ( ) ;
104
+ self . to_worker
105
+ . clone ( )
106
+ . send ( Command :: Setup { nt, sender } )
107
+ . await
108
+ . context ( "Failed to send command to worker" ) ?;
109
+ receiver. await . context ( "Failed to receive response from worker" ) ?
110
+ }
78
111
79
- /// 删除一个验证者,它将被排除在验证者网络之外
80
- pub async fn remove_validators ( & self , validators : Vec < ValidatorId > ) -> Result < ( ) > {
81
- let ( sender, receiver) = oneshot:: channel ( ) ;
82
- self . to_worker
83
- . clone ( )
84
- . send ( Command :: RemoveValidators { validators, sender } )
85
- . await
86
- . context ( "Failed to send command to worker" ) ?;
87
- receiver. await . context ( "Failed to receive response from worker" ) ?
88
- }
112
+ /// Removes validators from the network. These validators will no longer be part of the validator network.
113
+ ///
114
+ /// # Arguments
115
+ ///
116
+ /// * `validators` - A vector of `ValidatorId` representing the validators to be removed.
117
+ ///
118
+ /// # Returns
119
+ ///
120
+ /// A `Result` indicating the success or failure of the operation.
121
+ pub async fn remove_validators ( & self , validators : Vec < ValidatorId > ) -> Result < ( ) > {
122
+ let ( sender, receiver) = oneshot:: channel ( ) ;
123
+ self . to_worker
124
+ . clone ( )
125
+ . send ( Command :: RemoveValidators { validators, sender } )
126
+ . await
127
+ . context ( "Failed to send command to worker" ) ?;
128
+ receiver. await . context ( "Failed to receive response from worker" ) ?
129
+ }
89
130
90
- /// 添加新的验证者,它将被包含在验证者网络中
91
- pub async fn add_validators ( & self , validators : Vec < ValidatorId > ) -> Result < ( ) > {
92
- let ( sender, receiver) = oneshot:: channel ( ) ;
93
- self . to_worker
94
- . clone ( )
95
- . send ( Command :: AddValidators { validators, sender } )
96
- . await
97
- . context ( "Failed to send command to worker" ) ?;
98
- receiver. await . context ( "Failed to receive response from worker" ) ?
99
- }
131
+ /// Adds new validators to the network. These validators will be included in the validator network.
132
+ ///
133
+ /// # Arguments
134
+ ///
135
+ /// * `validators` - A vector of `ValidatorId` representing the validators to be added.
136
+ ///
137
+ /// # Returns
138
+ ///
139
+ /// A `Result` indicating the success or failure of the operation.
140
+ pub async fn add_validators ( & self , validators : Vec < ValidatorId > ) -> Result < ( ) > {
141
+ let ( sender, receiver) = oneshot:: channel ( ) ;
142
+ self . to_worker
143
+ . clone ( )
144
+ . send ( Command :: AddValidators { validators, sender } )
145
+ . await
146
+ . context ( "Failed to send command to worker" ) ?;
147
+ receiver. await . context ( "Failed to receive response from worker" ) ?
148
+ }
100
149
}
150
+
0 commit comments