8
8
9
9
use async_trait:: async_trait;
10
10
use log:: { debug, error, warn} ;
11
+ pub use nym_topology:: providers:: piecewise:: Config ;
11
12
use nym_topology:: {
12
- providers:: piecewise:: { Config , NymTopologyProvider , PiecewiseTopologyProvider } ,
13
- EpochRewardedSet , NymTopology , RoutingNode ,
13
+ providers:: piecewise:: { NymTopologyProvider , PiecewiseTopologyProvider } ,
14
+ EpochRewardedSet , NymTopology , RoutingNode , TopologyProvider ,
14
15
} ;
15
16
use nym_validator_client:: UserAgent ;
16
17
use rand:: { prelude:: SliceRandom , thread_rng} ;
@@ -26,13 +27,13 @@ pub struct NymApiTopologyProvider {
26
27
impl NymApiTopologyProvider {
27
28
/// Construct a new thread safe Cached topology provider using the Nym API
28
29
pub fn new (
29
- user_agent : UserAgent ,
30
+ config : impl Into < Config > ,
30
31
nym_api_urls : Vec < Url > ,
31
- config : Config ,
32
+ user_agent : Option < UserAgent > ,
32
33
initial_topology : Option < NymTopology > ,
33
34
) -> Self {
34
- let manager = NymApiPiecewiseProvider :: new ( nym_api_urls, Some ( user_agent) ) ;
35
- let inner = NymTopologyProvider :: new ( manager, config, initial_topology) ;
35
+ let manager = NymApiPiecewiseProvider :: new ( nym_api_urls, user_agent) ;
36
+ let inner = NymTopologyProvider :: new ( manager, config. into ( ) , initial_topology) ;
36
37
37
38
Self { inner }
38
39
}
@@ -44,6 +45,28 @@ impl AsRef<NymTopologyProvider<NymApiPiecewiseProvider>> for NymApiTopologyProvi
44
45
}
45
46
}
46
47
48
+ impl AsMut < NymTopologyProvider < NymApiPiecewiseProvider > > for NymApiTopologyProvider {
49
+ fn as_mut ( & mut self ) -> & mut NymTopologyProvider < NymApiPiecewiseProvider > {
50
+ & mut self . inner
51
+ }
52
+ }
53
+
54
+ #[ cfg( not( target_arch = "wasm32" ) ) ]
55
+ #[ async_trait]
56
+ impl TopologyProvider for NymApiTopologyProvider {
57
+ async fn get_new_topology ( & mut self ) -> Option < NymTopology > {
58
+ self . as_mut ( ) . get_new_topology ( ) . await
59
+ }
60
+ }
61
+
62
+ #[ cfg( target_arch = "wasm32" ) ]
63
+ #[ async_trait( ?Send ) ]
64
+ impl TopologyProvider for NymApiTopologyProvider {
65
+ async fn get_new_topology ( & mut self ) -> Option < NymTopology > {
66
+ self . as_mut ( ) . get_new_topology ( ) . await
67
+ }
68
+ }
69
+
47
70
#[ derive( Clone ) ]
48
71
struct NymApiPiecewiseProvider {
49
72
validator_client : nym_validator_client:: client:: NymApiClient ,
@@ -81,11 +104,8 @@ impl NymApiPiecewiseProvider {
81
104
self . validator_client
82
105
. change_nym_api ( self . nym_api_urls [ self . currently_used_api ] . clone ( ) )
83
106
}
84
- }
85
107
86
- #[ async_trait]
87
- impl PiecewiseTopologyProvider for NymApiPiecewiseProvider {
88
- async fn get_full_topology ( & mut self ) -> Option < NymTopology > {
108
+ async fn get_full_topology_inner ( & mut self ) -> Option < NymTopology > {
89
109
let layer_assignments = self . get_layer_assignments ( ) . await ?;
90
110
91
111
let mut topology = NymTopology :: new_empty ( layer_assignments) ;
@@ -111,7 +131,7 @@ impl PiecewiseTopologyProvider for NymApiPiecewiseProvider {
111
131
Some ( topology)
112
132
}
113
133
114
- async fn get_descriptor_batch ( & mut self , ids : & [ u32 ] ) -> Option < Vec < RoutingNode > > {
134
+ async fn get_descriptor_batch_inner ( & mut self , ids : & [ u32 ] ) -> Option < Vec < RoutingNode > > {
115
135
// Does this need to return a hashmap of RoutingNodes? that is moderately inconvenient
116
136
// especially when the nodes themselves contain their node_id unless we expect to directly
117
137
// use the result of this fn for lookups where we would otherwise for example, have to
@@ -129,13 +149,13 @@ impl PiecewiseTopologyProvider for NymApiPiecewiseProvider {
129
149
let mut out = Vec :: new ( ) ;
130
150
for node in descriptor_vec {
131
151
if let Ok ( routing_node) = RoutingNode :: try_from ( & node) {
132
- let _ = out. push ( routing_node) ;
152
+ out. push ( routing_node) ;
133
153
}
134
154
}
135
155
Some ( out)
136
156
}
137
157
138
- async fn get_layer_assignments ( & mut self ) -> Option < EpochRewardedSet > {
158
+ async fn get_layer_assignments_inner ( & mut self ) -> Option < EpochRewardedSet > {
139
159
self . validator_client
140
160
. get_current_rewarded_set ( )
141
161
. await
@@ -146,3 +166,35 @@ impl PiecewiseTopologyProvider for NymApiPiecewiseProvider {
146
166
. ok ( )
147
167
}
148
168
}
169
+
170
+ #[ cfg( not( target_arch = "wasm32" ) ) ]
171
+ #[ async_trait]
172
+ impl PiecewiseTopologyProvider for NymApiPiecewiseProvider {
173
+ async fn get_full_topology ( & mut self ) -> Option < NymTopology > {
174
+ self . get_full_topology_inner ( ) . await
175
+ }
176
+
177
+ async fn get_descriptor_batch ( & mut self , ids : & [ u32 ] ) -> Option < Vec < RoutingNode > > {
178
+ self . get_descriptor_batch_inner ( ids) . await
179
+ }
180
+
181
+ async fn get_layer_assignments ( & mut self ) -> Option < EpochRewardedSet > {
182
+ self . get_layer_assignments_inner ( ) . await
183
+ }
184
+ }
185
+
186
+ #[ cfg( target_arch = "wasm32" ) ]
187
+ #[ async_trait( ?Send ) ]
188
+ impl PiecewiseTopologyProvider for NymApiPiecewiseProvider {
189
+ async fn get_full_topology ( & mut self ) -> Option < NymTopology > {
190
+ self . get_full_topology_inner ( ) . await
191
+ }
192
+
193
+ async fn get_descriptor_batch ( & mut self , ids : & [ u32 ] ) -> Option < Vec < RoutingNode > > {
194
+ self . get_descriptor_batch_inner ( ids) . await
195
+ }
196
+
197
+ async fn get_layer_assignments ( & mut self ) -> Option < EpochRewardedSet > {
198
+ self . get_layer_assignments_inner ( ) . await
199
+ }
200
+ }
0 commit comments