@@ -5,7 +5,7 @@ use dkn_p2p::{
5
5
} ,
6
6
DriaP2PClient , DriaP2PCommander , DriaP2PProtocol ,
7
7
} ;
8
- use eyre:: { eyre , Result } ;
8
+ use eyre:: Result ;
9
9
use tokio:: { sync:: mpsc, time:: Duration } ;
10
10
use tokio_util:: { either:: Either , sync:: CancellationToken } ;
11
11
@@ -165,7 +165,10 @@ impl DriaComputeNode {
165
165
}
166
166
167
167
// first, parse the raw gossipsub message to a prepared message
168
- let message = match self . parse_message_to_prepared_message ( & message) {
168
+ let message = match DKNMessage :: try_from_gossipsub_message (
169
+ & message,
170
+ & self . config . admin_public_key ,
171
+ ) {
169
172
Ok ( message) => message,
170
173
Err ( e) => {
171
174
log:: error!( "Error parsing message: {:?}" , e) ;
@@ -193,7 +196,7 @@ impl DriaComputeNode {
193
196
PingpongHandler :: LISTEN_TOPIC => {
194
197
PingpongHandler :: handle_ping ( self , & message) . await
195
198
}
196
- _ => unreachable ! ( ) , // unreachable because of the ` match` above
199
+ _ => unreachable ! ( " unreachable due to match expression" ) ,
197
200
} ;
198
201
199
202
// validate the message based on the result
@@ -216,46 +219,20 @@ impl DriaComputeNode {
216
219
}
217
220
}
218
221
219
- /// Peer refresh simply reports the peer count to the user.
220
- async fn handle_peer_refresh ( & self ) {
221
- match self . p2p . peer_counts ( ) . await {
222
- Ok ( ( mesh, all) ) => log:: info!( "Peer Count (mesh/all): {} / {}" , mesh, all) ,
223
- Err ( e) => log:: error!( "Error getting peer counts: {:?}" , e) ,
224
- }
225
- }
226
-
227
- /// Updates the local list of available nodes by refreshing it.
228
- /// Dials the RPC nodes again for better connectivity.
229
- async fn handle_available_nodes_refresh ( & mut self ) {
230
- log:: info!( "Refreshing available nodes." ) ;
231
-
232
- // refresh available nodes
233
- if let Err ( e) = self . available_nodes . populate_with_api ( ) . await {
234
- log:: error!( "Error refreshing available nodes: {:?}" , e) ;
235
- } ;
236
-
237
- // dial all rpc nodes
238
- for rpc_addr in self . available_nodes . rpc_addrs . iter ( ) {
239
- log:: debug!( "Dialling RPC node: {}" , rpc_addr) ;
240
- if let Err ( e) = self . p2p . dial ( rpc_addr. clone ( ) ) . await {
241
- log:: warn!( "Error dialling RPC node: {:?}" , e) ;
242
- } ;
243
- }
244
- }
245
-
246
222
/// Runs the main loop of the compute node.
247
223
/// This method is not expected to return until cancellation occurs.
248
224
pub async fn run ( & mut self ) -> Result < ( ) > {
225
+ // prepare durations for sleeps
226
+ let peer_refresh_duration = Duration :: from_secs ( PEER_REFRESH_INTERVAL_SECS ) ;
227
+ let available_node_refresh_duration =
228
+ Duration :: from_secs ( AVAILABLE_NODES_REFRESH_INTERVAL_SECS ) ;
229
+
249
230
// subscribe to topics
250
231
self . subscribe ( PingpongHandler :: LISTEN_TOPIC ) . await ?;
251
232
self . subscribe ( PingpongHandler :: RESPONSE_TOPIC ) . await ?;
252
233
self . subscribe ( WorkflowHandler :: LISTEN_TOPIC ) . await ?;
253
234
self . subscribe ( WorkflowHandler :: RESPONSE_TOPIC ) . await ?;
254
235
255
- let peer_refresh_duration = Duration :: from_secs ( PEER_REFRESH_INTERVAL_SECS ) ;
256
- let available_node_refresh_duration =
257
- Duration :: from_secs ( AVAILABLE_NODES_REFRESH_INTERVAL_SECS ) ;
258
-
259
236
loop {
260
237
tokio:: select! {
261
238
// check peer count every now and then
@@ -321,25 +298,31 @@ impl DriaComputeNode {
321
298
Ok ( ( ) )
322
299
}
323
300
324
- /// Parses a given raw Gossipsub message to a prepared P2PMessage object.
325
- /// This prepared message includes the topic, payload, version and timestamp.
326
- ///
327
- /// This also checks the signature of the message, expecting a valid signature from admin node.
328
- // TODO: move this somewhere?
329
- pub fn parse_message_to_prepared_message ( & self , message : & Message ) -> Result < DKNMessage > {
330
- // the received message is expected to use IdentHash for the topic, so we can see the name of the topic immediately.
331
- log:: debug!( "Parsing {} message." , message. topic. as_str( ) ) ;
332
- let message = DKNMessage :: try_from ( message) ?;
333
- log:: debug!( "Parsed: {}" , message) ;
334
-
335
- // check dria signature
336
- // NOTE: when we have many public keys, we should check the signature against all of them
337
- // TODO: public key here will be given dynamically
338
- if !message. is_signed ( & self . config . admin_public_key ) ? {
339
- return Err ( eyre ! ( "Invalid signature." ) ) ;
301
+ /// Peer refresh simply reports the peer count to the user.
302
+ async fn handle_peer_refresh ( & self ) {
303
+ match self . p2p . peer_counts ( ) . await {
304
+ Ok ( ( mesh, all) ) => log:: info!( "Peer Count (mesh/all): {} / {}" , mesh, all) ,
305
+ Err ( e) => log:: error!( "Error getting peer counts: {:?}" , e) ,
340
306
}
307
+ }
341
308
342
- Ok ( message)
309
+ /// Updates the local list of available nodes by refreshing it.
310
+ /// Dials the RPC nodes again for better connectivity.
311
+ async fn handle_available_nodes_refresh ( & mut self ) {
312
+ log:: info!( "Refreshing available nodes." ) ;
313
+
314
+ // refresh available nodes
315
+ if let Err ( e) = self . available_nodes . populate_with_api ( ) . await {
316
+ log:: error!( "Error refreshing available nodes: {:?}" , e) ;
317
+ } ;
318
+
319
+ // dial all rpc nodes
320
+ for rpc_addr in self . available_nodes . rpc_addrs . iter ( ) {
321
+ log:: debug!( "Dialling RPC node: {}" , rpc_addr) ;
322
+ if let Err ( e) = self . p2p . dial ( rpc_addr. clone ( ) ) . await {
323
+ log:: warn!( "Error dialling RPC node: {:?}" , e) ;
324
+ } ;
325
+ }
343
326
}
344
327
}
345
328
0 commit comments