@@ -2,31 +2,14 @@ use dkn_p2p::libp2p::{Multiaddr, PeerId};
2
2
use dkn_workflows:: split_csv_line;
3
3
use eyre:: Result ;
4
4
use std:: { env, fmt:: Debug , str:: FromStr } ;
5
+ use tokio:: time:: Instant ;
5
6
6
- /// Static bootstrap nodes for the Kademlia DHT bootstrap step.
7
- const STATIC_BOOTSTRAP_NODES : [ & str ; 4 ] = [
8
- "/ip4/44.206.245.139/tcp/4001/p2p/16Uiu2HAm4q3LZU2T9kgjKK4ysy6KZYKLq8KiXQyae4RHdF7uqSt4" ,
9
- "/ip4/18.234.39.91/tcp/4001/p2p/16Uiu2HAmJqegPzwuGKWzmb5m3RdSUJ7NhEGWB5jNCd3ca9zdQ9dU" ,
10
- "/ip4/54.242.44.217/tcp/4001/p2p/16Uiu2HAmR2sAoh9F8jT9AZup9y79Mi6NEFVUbwRvahqtWamfabkz" ,
11
- "/ip4/52.201.242.227/tcp/4001/p2p/16Uiu2HAmFEUCy1s1gjyHfc8jey4Wd9i5bSDnyFDbWTnbrF2J3KFb" ,
12
- ] ;
7
+ mod statics;
13
8
14
- /// Static relay nodes for the `P2pCircuit`.
15
- const STATIC_RELAY_NODES : [ & str ; 4 ] = [
16
- "/ip4/34.201.33.141/tcp/4001/p2p/16Uiu2HAkuXiV2CQkC9eJgU6cMnJ9SMARa85FZ6miTkvn5fuHNufa" ,
17
- "/ip4/18.232.93.227/tcp/4001/p2p/16Uiu2HAmHeGKhWkXTweHJTA97qwP81ww1W2ntGaebeZ25ikDhd4z" ,
18
- "/ip4/54.157.219.194/tcp/4001/p2p/16Uiu2HAm7A5QVSy5FwrXAJdNNsdfNAcaYahEavyjnFouaEi22dcq" ,
19
- "/ip4/54.88.171.104/tcp/4001/p2p/16Uiu2HAm5WP1J6bZC3aHxd7XCUumMt9txAystmbZSaMS2omHepXa" ,
20
- ] ;
9
+ use crate :: DriaNetworkType ;
21
10
22
- /// Static RPC Peer IDs for the Admin RPC.
23
- const STATIC_RPC_PEER_IDS : [ & str ; 0 ] = [ ] ;
24
-
25
- /// API URL for refreshing the Admin RPC PeerIDs from Dria server.
26
- const RPC_PEER_ID_REFRESH_API_URL : & str = "https://dkn.dria.co/available-nodes" ;
27
-
28
- /// Number of seconds between refreshing the Admin RPC PeerIDs from Dria server.
29
- const RPC_PEER_ID_REFRESH_INTERVAL_SECS : u64 = 30 ;
11
+ /// Number of seconds between refreshing the available nodes.
12
+ const DEFAULT_REFRESH_INTERVAL_SECS : u64 = 30 ;
30
13
31
14
/// Available nodes within the hybrid P2P network.
32
15
///
@@ -42,17 +25,37 @@ pub struct AvailableNodes {
42
25
pub relay_nodes : Vec < Multiaddr > ,
43
26
pub rpc_nodes : Vec < PeerId > ,
44
27
pub rpc_addrs : Vec < Multiaddr > ,
45
- // refreshing
46
- pub last_refreshed : tokio:: time:: Instant ,
28
+ pub last_refreshed : Instant ,
29
+ pub network_type : DriaNetworkType ,
30
+ pub refresh_interval_secs : u64 ,
47
31
}
48
32
49
33
impl AvailableNodes {
34
+ /// Creates a new `AvailableNodes` struct for the given network type.
35
+ pub fn new ( network : DriaNetworkType ) -> Self {
36
+ Self {
37
+ bootstrap_nodes : vec ! [ ] ,
38
+ relay_nodes : vec ! [ ] ,
39
+ rpc_nodes : vec ! [ ] ,
40
+ rpc_addrs : vec ! [ ] ,
41
+ last_refreshed : Instant :: now ( ) ,
42
+ network_type : network,
43
+ refresh_interval_secs : DEFAULT_REFRESH_INTERVAL_SECS ,
44
+ }
45
+ }
46
+
47
+ /// Sets the refresh interval in seconds.
48
+ pub fn with_refresh_interval ( mut self , interval_secs : u64 ) -> Self {
49
+ self . refresh_interval_secs = interval_secs;
50
+ self
51
+ }
52
+
50
53
/// Parses static bootstrap & relay nodes from environment variables.
51
54
///
52
55
/// The environment variables are:
53
56
/// - `DRIA_BOOTSTRAP_NODES`: comma-separated list of bootstrap nodes
54
57
/// - `DRIA_RELAY_NODES`: comma-separated list of relay nodes
55
- pub fn new_from_env ( ) -> Self {
58
+ pub fn populate_with_env ( & mut self ) {
56
59
// parse bootstrap nodes
57
60
let bootstrap_nodes = split_csv_line ( & env:: var ( "DKN_BOOTSTRAP_NODES" ) . unwrap_or_default ( ) ) ;
58
61
if bootstrap_nodes. is_empty ( ) {
@@ -69,24 +72,15 @@ impl AvailableNodes {
69
72
log:: debug!( "Using additional relay nodes: {:#?}" , relay_nodes) ;
70
73
}
71
74
72
- Self {
73
- bootstrap_nodes : parse_vec ( bootstrap_nodes) ,
74
- relay_nodes : parse_vec ( relay_nodes) ,
75
- rpc_nodes : vec ! [ ] ,
76
- rpc_addrs : vec ! [ ] ,
77
- last_refreshed : tokio:: time:: Instant :: now ( ) ,
78
- }
75
+ self . bootstrap_nodes = parse_vec ( bootstrap_nodes) ;
76
+ self . relay_nodes = parse_vec ( relay_nodes) ;
79
77
}
80
78
81
- /// Creates a new `AvailableNodes` struct from the static nodes, hardcoded within the code.
82
- pub fn new_from_statics ( ) -> Self {
83
- Self {
84
- bootstrap_nodes : parse_vec ( STATIC_BOOTSTRAP_NODES . to_vec ( ) ) ,
85
- relay_nodes : parse_vec ( STATIC_RELAY_NODES . to_vec ( ) ) ,
86
- rpc_nodes : parse_vec ( STATIC_RPC_PEER_IDS . to_vec ( ) ) ,
87
- rpc_addrs : vec ! [ ] ,
88
- last_refreshed : tokio:: time:: Instant :: now ( ) ,
89
- }
79
+ /// Adds the static nodes to the struct, with respect to network type.
80
+ pub fn populate_with_statics ( & mut self ) {
81
+ self . bootstrap_nodes = self . network_type . get_static_bootstrap_nodes ( ) ;
82
+ self . relay_nodes = self . network_type . get_static_relay_nodes ( ) ;
83
+ self . rpc_nodes = self . network_type . get_static_rpc_peer_ids ( ) ;
90
84
}
91
85
92
86
/// Joins the struct with another `AvailableNodes` struct.
@@ -115,13 +109,14 @@ impl AvailableNodes {
115
109
self
116
110
}
117
111
112
+ /// Returns whether enough time has passed since the last refresh.
118
113
pub fn can_refresh ( & self ) -> bool {
119
- self . last_refreshed . elapsed ( ) . as_secs ( ) > RPC_PEER_ID_REFRESH_INTERVAL_SECS
114
+ self . last_refreshed . elapsed ( ) . as_secs ( ) > self . refresh_interval_secs
120
115
}
121
116
122
- /// Refreshes the available nodes for Bootstrap, Relay and RPC nodes .
123
- pub async fn get_available_nodes ( ) -> Result < Self > {
124
- #[ derive( serde:: Deserialize , Debug ) ]
117
+ /// Refresh available nodes using the API .
118
+ pub async fn populate_with_api ( & mut self ) -> Result < ( ) > {
119
+ #[ derive( serde:: Deserialize , Default , Debug ) ]
125
120
struct AvailableNodesApiResponse {
126
121
pub bootstraps : Vec < String > ,
127
122
pub relays : Vec < String > ,
@@ -130,21 +125,21 @@ impl AvailableNodes {
130
125
pub rpc_addrs : Vec < String > ,
131
126
}
132
127
133
- let response = reqwest:: get ( RPC_PEER_ID_REFRESH_API_URL ) . await ?;
128
+ // make the request w.r.t network type
129
+ let url = match self . network_type {
130
+ DriaNetworkType :: Community => "https://dkn.dria.co/available-nodes" ,
131
+ DriaNetworkType :: Pro => "https://dkn.dria.co/sdk/available-nodes" ,
132
+ } ;
133
+ let response = reqwest:: get ( url) . await ?;
134
134
let response_body = response. json :: < AvailableNodesApiResponse > ( ) . await ?;
135
135
136
- Ok ( Self {
137
- bootstrap_nodes : parse_vec ( response_body. bootstraps ) ,
138
- relay_nodes : parse_vec ( response_body. relays ) ,
139
- rpc_nodes : parse_vec ( response_body. rpcs ) ,
140
- rpc_addrs : parse_vec ( response_body. rpc_addrs ) ,
141
- last_refreshed : tokio:: time:: Instant :: now ( ) ,
142
- } )
143
- }
136
+ self . bootstrap_nodes = parse_vec ( response_body. bootstraps ) ;
137
+ self . relay_nodes = parse_vec ( response_body. relays ) ;
138
+ self . rpc_nodes = parse_vec ( response_body. rpcs ) ;
139
+ self . rpc_addrs = parse_vec ( response_body. rpc_addrs ) ;
140
+ self . last_refreshed = Instant :: now ( ) ;
144
141
145
- /// Refresh available nodes using the API.
146
- pub async fn refresh ( & mut self ) {
147
- todo ! ( "TODO: refresh the available nodes" )
142
+ Ok ( ( ) )
148
143
}
149
144
}
150
145
@@ -171,7 +166,12 @@ mod tests {
171
166
#[ tokio:: test]
172
167
#[ ignore = "run this manually" ]
173
168
async fn test_get_available_nodes ( ) {
174
- let available_nodes = AvailableNodes :: get_available_nodes ( ) . await . unwrap ( ) ;
175
- println ! ( "{:#?}" , available_nodes) ;
169
+ let mut available_nodes = AvailableNodes :: new ( DriaNetworkType :: Community ) ;
170
+ available_nodes. populate_with_api ( ) . await . unwrap ( ) ;
171
+ println ! ( "Community: {:#?}" , available_nodes) ;
172
+
173
+ let mut available_nodes = AvailableNodes :: new ( DriaNetworkType :: Pro ) ;
174
+ available_nodes. populate_with_api ( ) . await . unwrap ( ) ;
175
+ println ! ( "Pro: {:#?}" , available_nodes) ;
176
176
}
177
177
}
0 commit comments