1
1
use dkn_p2p:: libp2p:: { Multiaddr , PeerId } ;
2
2
use dkn_workflows:: split_csv_line;
3
3
use eyre:: Result ;
4
- use std:: { env, fmt:: Debug , str:: FromStr } ;
4
+ use std:: { collections :: HashSet , env, fmt:: Debug , str:: FromStr } ;
5
5
use tokio:: time:: Instant ;
6
6
7
7
mod statics;
8
8
9
9
use crate :: DriaNetworkType ;
10
10
11
11
/// Number of seconds between refreshing the available nodes.
12
- const DEFAULT_REFRESH_INTERVAL_SECS : u64 = 30 ;
12
+ const DEFAULT_REFRESH_INTERVAL_SECS : u64 = 5 ;
13
13
14
14
/// Available nodes within the hybrid P2P network.
15
15
///
@@ -21,10 +21,10 @@ const DEFAULT_REFRESH_INTERVAL_SECS: u64 = 30;
21
21
/// with them via GossipSub only.
22
22
#[ derive( Debug , Clone ) ]
23
23
pub struct AvailableNodes {
24
- pub bootstrap_nodes : Vec < Multiaddr > ,
25
- pub relay_nodes : Vec < Multiaddr > ,
26
- pub rpc_nodes : Vec < PeerId > ,
27
- pub rpc_addrs : Vec < Multiaddr > ,
24
+ pub bootstrap_nodes : HashSet < Multiaddr > ,
25
+ pub relay_nodes : HashSet < Multiaddr > ,
26
+ pub rpc_nodes : HashSet < PeerId > ,
27
+ pub rpc_addrs : HashSet < Multiaddr > ,
28
28
pub last_refreshed : Instant ,
29
29
pub network_type : DriaNetworkType ,
30
30
pub refresh_interval_secs : u64 ,
@@ -34,10 +34,10 @@ impl AvailableNodes {
34
34
/// Creates a new `AvailableNodes` struct for the given network type.
35
35
pub fn new ( network : DriaNetworkType ) -> Self {
36
36
Self {
37
- bootstrap_nodes : vec ! [ ] ,
38
- relay_nodes : vec ! [ ] ,
39
- rpc_nodes : vec ! [ ] ,
40
- rpc_addrs : vec ! [ ] ,
37
+ bootstrap_nodes : HashSet :: new ( ) ,
38
+ relay_nodes : HashSet :: new ( ) ,
39
+ rpc_nodes : HashSet :: new ( ) ,
40
+ rpc_addrs : HashSet :: new ( ) ,
41
41
last_refreshed : Instant :: now ( ) ,
42
42
network_type : network,
43
43
refresh_interval_secs : DEFAULT_REFRESH_INTERVAL_SECS ,
@@ -63,6 +63,8 @@ impl AvailableNodes {
63
63
} else {
64
64
log:: debug!( "Using additional bootstrap nodes: {:#?}" , bootstrap_nodes) ;
65
65
}
66
+ self . bootstrap_nodes
67
+ . extend ( parse_vec ( bootstrap_nodes) . into_iter ( ) ) ;
66
68
67
69
// parse relay nodes
68
70
let relay_nodes = split_csv_line ( & env:: var ( "DKN_RELAY_NODES" ) . unwrap_or_default ( ) ) ;
@@ -71,47 +73,17 @@ impl AvailableNodes {
71
73
} else {
72
74
log:: debug!( "Using additional relay nodes: {:#?}" , relay_nodes) ;
73
75
}
74
-
75
- self . bootstrap_nodes = parse_vec ( bootstrap_nodes) ;
76
- self . relay_nodes = parse_vec ( relay_nodes) ;
76
+ self . relay_nodes . extend ( parse_vec ( relay_nodes) . into_iter ( ) ) ;
77
77
}
78
78
79
79
/// Adds the static nodes to the struct, with respect to network type.
80
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 ( ) ;
84
- }
85
-
86
- /// Joins the struct with another `AvailableNodes` struct.
87
- pub fn join ( mut self , other : Self ) -> Self {
88
- self . bootstrap_nodes . extend ( other. bootstrap_nodes ) ;
89
- self . relay_nodes . extend ( other. relay_nodes ) ;
90
- self . rpc_nodes . extend ( other. rpc_nodes ) ;
91
- self . rpc_addrs . extend ( other. rpc_addrs ) ;
92
- self
93
- }
94
-
95
- /// Removes duplicates within all fields.
96
- pub fn sort_dedup ( mut self ) -> Self {
97
- self . bootstrap_nodes . sort_unstable ( ) ;
98
- self . bootstrap_nodes . dedup ( ) ;
99
-
100
- self . relay_nodes . sort_unstable ( ) ;
101
- self . relay_nodes . dedup ( ) ;
102
-
103
- self . rpc_nodes . sort_unstable ( ) ;
104
- self . rpc_nodes . dedup ( ) ;
105
-
106
- self . rpc_addrs . sort_unstable ( ) ;
107
- self . rpc_addrs . dedup ( ) ;
108
-
109
- self
110
- }
111
-
112
- /// Returns whether enough time has passed since the last refresh.
113
- pub fn can_refresh ( & self ) -> bool {
114
- self . last_refreshed . elapsed ( ) . as_secs ( ) > self . refresh_interval_secs
81
+ self . bootstrap_nodes
82
+ . extend ( self . network_type . get_static_bootstrap_nodes ( ) . into_iter ( ) ) ;
83
+ self . relay_nodes
84
+ . extend ( self . network_type . get_static_relay_nodes ( ) . into_iter ( ) ) ;
85
+ self . rpc_nodes
86
+ . extend ( self . network_type . get_static_rpc_peer_ids ( ) . into_iter ( ) ) ;
115
87
}
116
88
117
89
/// Refresh available nodes using the API.
@@ -132,15 +104,23 @@ impl AvailableNodes {
132
104
} ;
133
105
let response = reqwest:: get ( url) . await ?;
134
106
let response_body = response. json :: < AvailableNodesApiResponse > ( ) . await ?;
135
-
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 ) ;
107
+ self . bootstrap_nodes
108
+ . extend ( parse_vec ( response_body. bootstraps ) . into_iter ( ) ) ;
109
+ self . relay_nodes
110
+ . extend ( parse_vec ( response_body. relays ) . into_iter ( ) ) ;
111
+ self . rpc_addrs
112
+ . extend ( parse_vec ( response_body. rpc_addrs ) . into_iter ( ) ) ;
113
+ self . rpc_nodes
114
+ . extend ( parse_vec :: < PeerId > ( response_body. rpcs ) . into_iter ( ) ) ;
140
115
self . last_refreshed = Instant :: now ( ) ;
141
116
142
117
Ok ( ( ) )
143
118
}
119
+
120
+ /// Returns whether enough time has passed since the last refresh.
121
+ pub fn can_refresh ( & self ) -> bool {
122
+ self . last_refreshed . elapsed ( ) . as_secs ( ) > self . refresh_interval_secs
123
+ }
144
124
}
145
125
146
126
/// Like `parse` of `str` but for vectors.
0 commit comments