1
- use dkn_p2p:: { libp2p:: gossipsub, DriaP2PClient } ;
1
+ use dkn_p2p:: { libp2p:: gossipsub, DriaP2PClient , DriaP2PProtocol } ;
2
2
use eyre:: { eyre, Result } ;
3
- use std:: time:: Duration ;
4
3
use tokio_util:: sync:: CancellationToken ;
5
4
6
5
use crate :: {
@@ -9,9 +8,6 @@ use crate::{
9
8
utils:: { crypto:: secret_to_keypair, AvailableNodes , DKNMessage } ,
10
9
} ;
11
10
12
- /// Number of seconds between refreshing the Admin RPC PeerIDs from Dria server.
13
- const RPC_PEER_ID_REFRESH_INTERVAL_SECS : u64 = 30 ;
14
-
15
11
/// **Dria Compute Node**
16
12
///
17
13
/// Internally, the node will create a new P2P client with the given secret key.
@@ -29,7 +25,6 @@ pub struct DriaComputeNode {
29
25
pub config : DriaComputeNodeConfig ,
30
26
pub p2p : DriaP2PClient ,
31
27
pub available_nodes : AvailableNodes ,
32
- pub available_nodes_last_refreshed : tokio:: time:: Instant ,
33
28
pub cancellation : CancellationToken ,
34
29
}
35
30
@@ -41,31 +36,21 @@ impl DriaComputeNode {
41
36
let keypair = secret_to_keypair ( & config. secret_key ) ;
42
37
43
38
// get available nodes (bootstrap, relay, rpc) for p2p
44
- let available_nodes = AvailableNodes :: default ( )
45
- . join ( AvailableNodes :: new_from_statics ( ) )
46
- . join ( AvailableNodes :: new_from_env ( ) )
47
- . join (
48
- AvailableNodes :: get_available_nodes ( )
49
- . await
50
- . unwrap_or_default ( ) ,
51
- )
52
- . sort_dedup ( ) ;
39
+ let mut available_nodes =
40
+ AvailableNodes :: new_from_statics ( ) . join ( AvailableNodes :: new_from_env ( ) ) ;
41
+ available_nodes. refresh ( ) . await ;
53
42
54
43
// we are using the major.minor version as the P2P version
55
44
// so that patch versions do not interfere with the protocol
56
- const P2P_VERSION : & str = concat ! (
57
- env!( "CARGO_PKG_VERSION_MAJOR" ) ,
58
- "." ,
59
- env!( "CARGO_PKG_VERSION_MINOR" )
60
- ) ;
45
+ let protocol = DriaP2PProtocol :: new_major_minor ( "dria" ) ;
61
46
62
47
// create p2p client
63
48
let mut p2p = DriaP2PClient :: new (
64
49
keypair,
65
50
config. p2p_listen_addr . clone ( ) ,
66
51
& available_nodes. bootstrap_nodes ,
67
52
& available_nodes. relay_nodes ,
68
- P2P_VERSION ,
53
+ protocol ,
69
54
) ?;
70
55
71
56
// dial rpc nodes
@@ -83,7 +68,6 @@ impl DriaComputeNode {
83
68
config,
84
69
cancellation,
85
70
available_nodes,
86
- available_nodes_last_refreshed : tokio:: time:: Instant :: now ( ) ,
87
71
} )
88
72
}
89
73
@@ -111,8 +95,10 @@ impl DriaComputeNode {
111
95
112
96
/// Publishes a given message to the network w.r.t the topic of it.
113
97
///
114
- /// Internally, the message is JSON serialized to bytes and then published to the network as is.
115
- pub fn publish ( & mut self , message : DKNMessage ) -> Result < ( ) > {
98
+ /// Internally, identity is attached to the the message which is then JSON serialized to bytes
99
+ /// and then published to the network as is.
100
+ pub fn publish ( & mut self , mut message : DKNMessage ) -> Result < ( ) > {
101
+ message = message. with_identity ( self . p2p . protocol ( ) . identity ( ) ) ;
116
102
let message_bytes = serde_json:: to_vec ( & message) ?;
117
103
let message_id = self . p2p . publish ( & message. topic , message_bytes) ?;
118
104
log:: info!( "Published message ({}) to {}" , message_id, message. topic) ;
@@ -145,11 +131,19 @@ impl DriaComputeNode {
145
131
tokio:: select! {
146
132
event = self . p2p. process_events( ) => {
147
133
// refresh admin rpc peer ids
148
- if self . available_nodes_last_refreshed . elapsed ( ) > Duration :: from_secs ( RPC_PEER_ID_REFRESH_INTERVAL_SECS ) {
134
+ if self . available_nodes . can_refresh ( ) {
149
135
log:: info!( "Refreshing available nodes." ) ;
150
136
151
- self . available_nodes = AvailableNodes :: get_available_nodes( ) . await . unwrap_or_default( ) . join( self . available_nodes. clone( ) ) . sort_dedup( ) ;
152
- self . available_nodes_last_refreshed = tokio:: time:: Instant :: now( ) ;
137
+ self . available_nodes. refresh( ) . await ;
138
+
139
+ // dial all rpc nodes for better connectivity
140
+ for rpc_addr in self . available_nodes. rpc_addrs. iter( ) {
141
+ log:: debug!( "Dialling RPC node: {}" , rpc_addr) ;
142
+ // TODO: does this cause resource issues?
143
+ if let Err ( e) = self . p2p. dial( rpc_addr. clone( ) ) {
144
+ log:: warn!( "Error dialling RPC node: {:?}" , e) ;
145
+ } ;
146
+ }
153
147
154
148
// also print network info
155
149
log:: debug!( "{:?}" , self . p2p. network_info( ) . connection_counters( ) ) ;
@@ -173,12 +167,7 @@ impl DriaComputeNode {
173
167
}
174
168
} ;
175
169
176
- // log::info!(
177
- // "Received {} message ({})\nFrom: {}\nSource: {}",
178
- // topic_str,
179
- // message_id,
180
- // peer_id,
181
- // );
170
+ // log the received message
182
171
log:: info!(
183
172
"Received {} message ({}) from {}" ,
184
173
topic_str,
0 commit comments