@@ -50,6 +50,7 @@ impl From<MithrilEvent> for MithrilEventWasm {
5050 }
5151}
5252
53+ /// Structure that wraps a [Client] and enables its functions to be used in WASM
5354#[ wasm_bindgen]
5455pub struct MithrilClient {
5556 client : Client ,
@@ -199,8 +200,6 @@ impl MithrilClient {
199200 }
200201}
201202
202- // The tests are commented for now, as we don't want to run them on a testnet aggregator.
203- /*
204203#[ cfg( test) ]
205204mod tests {
206205 use super :: * ;
@@ -210,60 +209,63 @@ mod tests {
210209 } ;
211210 use wasm_bindgen_test:: * ;
212211
213- const AGGREGATOR_ENDPOINT: &str =
214- "https://aggregator.testing-preview.api.mithril.network/aggregator";
215212 const GENESIS_VERIFICATION_KEY : & str = "5b33322c3235332c3138362c3230312c3137372c31312c3131372c3133352c3138372c3136372c3138312c3138382c32322c35392c3230362c3130352c3233312c3135302c3231352c33302c37382c3231322c37362c31362c3235322c3138302c37322c3133342c3133372c3234372c3136312c36385d" ;
213+ const FAKE_AGGREGATOR_IP : & str = "127.0.0.1" ;
214+ const FAKE_AGGREGATOR_PORT : & str = "8000" ;
215+ const FAKE_AGGREGATOR_SNAPSHOT_DIGEST : & str =
216+ "000ee4c84c7b64a62dc30ec78a765a1f3bb81cd9dd4bd1eccf9f2da785e70877" ;
217+ const FAKE_AGGREGATOR_MSD_HASH : & str =
218+ "03ebb00e6626037f2e58eb7cc50d308fd57c253baa1fe2b04eb5945ced16b5bd" ;
219+ const FAKE_CERTIFICATE_HASH : & str =
220+ "05bf6740e781e649dd2fe7e3319818747d8038ca759c67711c90cf24cdade8a9" ;
221+
222+ fn get_mithril_client ( ) -> MithrilClient {
223+ MithrilClient :: new (
224+ & format ! (
225+ "http://{}:{}/aggregator" ,
226+ FAKE_AGGREGATOR_IP , FAKE_AGGREGATOR_PORT
227+ ) ,
228+ GENESIS_VERIFICATION_KEY ,
229+ )
230+ }
216231
217232 wasm_bindgen_test_configure ! ( run_in_browser) ;
218233 #[ wasm_bindgen_test]
219234 async fn list_snapshots_should_return_value_convertible_in_rust_type ( ) {
220- let client = MithrilClient::new(AGGREGATOR_ENDPOINT, GENESIS_VERIFICATION_KEY);
221-
222- let snapshots_list_js_value = client
235+ let snapshots_list_js_value = get_mithril_client ( )
223236 . list_snapshots ( )
224237 . await
225238 . expect ( "list_snapshots should not fail" ) ;
226239 let snapshots_list =
227240 serde_wasm_bindgen:: from_value :: < Vec < SnapshotListItem > > ( snapshots_list_js_value)
228241 . expect ( "conversion should not fail" ) ;
229242
230- assert_eq!(snapshots_list.len(), 20 );
243+ assert_eq ! ( snapshots_list. len( ) , 3 ) ;
231244 }
232245
233246 #[ wasm_bindgen_test]
234247 async fn get_snapshot_should_return_value_convertible_in_rust_type ( ) {
235- let client = MithrilClient::new(AGGREGATOR_ENDPOINT, GENESIS_VERIFICATION_KEY);
236- let snapshots_list_js_value = client.list_snapshots().await.unwrap();
237- let snapshots_list =
238- serde_wasm_bindgen::from_value::<Vec<SnapshotListItem>>(snapshots_list_js_value)
239- .unwrap();
240- let last_digest = &snapshots_list.first().unwrap().digest;
241-
242- let snapshot_js_value = client
243- .get_snapshot(last_digest)
248+ let snapshot_js_value = get_mithril_client ( )
249+ . get_snapshot ( FAKE_AGGREGATOR_SNAPSHOT_DIGEST )
244250 . await
245251 . expect ( "get_snapshot should not fail" ) ;
246252 let snapshot = serde_wasm_bindgen:: from_value :: < Snapshot > ( snapshot_js_value)
247253 . expect ( "conversion should not fail" ) ;
248254
249- assert_eq!(snapshot.digest, last_digest.to_string() );
255+ assert_eq ! ( snapshot. digest, FAKE_AGGREGATOR_SNAPSHOT_DIGEST ) ;
250256 }
251257
252258 #[ wasm_bindgen_test]
253259 async fn get_snapshot_should_fail_with_unknown_digest ( ) {
254- let client = MithrilClient::new(AGGREGATOR_ENDPOINT, GENESIS_VERIFICATION_KEY);
255-
256- client
260+ get_mithril_client ( )
257261 . get_snapshot ( "whatever" )
258262 . await
259263 . expect_err ( "get_snapshot should fail" ) ;
260264 }
261265
262266 #[ wasm_bindgen_test]
263267 async fn list_mithril_stake_distributions_should_return_value_convertible_in_rust_type ( ) {
264- let wasm_client = MithrilClient::new(AGGREGATOR_ENDPOINT, GENESIS_VERIFICATION_KEY);
265-
266- let msd_list_js_value = wasm_client
268+ let msd_list_js_value = get_mithril_client ( )
267269 . list_mithril_stake_distributions ( )
268270 . await
269271 . expect ( "list_mithril_stake_distributions should not fail" ) ;
@@ -272,44 +274,32 @@ mod tests {
272274 )
273275 . expect ( "conversion should not fail" ) ;
274276
275- assert_eq!(msd_list.len(), 20 );
277+ assert_eq ! ( msd_list. len( ) , 3 ) ;
276278 }
277279
278280 #[ wasm_bindgen_test]
279281 async fn get_mithril_stake_distribution_should_return_value_convertible_in_rust_type ( ) {
280- let client = MithrilClient::new(AGGREGATOR_ENDPOINT, GENESIS_VERIFICATION_KEY);
281- let msd_list_js_value = client.list_mithril_stake_distributions().await.unwrap();
282- let msd_list = serde_wasm_bindgen::from_value::<Vec<MithrilStakeDistributionListItem>>(
283- msd_list_js_value,
284- )
285- .unwrap();
286- let last_hash = &msd_list.first().unwrap().hash;
287-
288- let msd_js_value = client
289- .get_mithril_stake_distribution(last_hash)
282+ let msd_js_value = get_mithril_client ( )
283+ . get_mithril_stake_distribution ( FAKE_AGGREGATOR_MSD_HASH )
290284 . await
291285 . expect ( "get_mithril_stake_distribution should not fail" ) ;
292286 let msd = serde_wasm_bindgen:: from_value :: < MithrilStakeDistribution > ( msd_js_value)
293287 . expect ( "conversion should not fail" ) ;
294288
295- assert_eq!(msd.hash, last_hash.to_string() );
289+ assert_eq ! ( msd. hash, FAKE_AGGREGATOR_MSD_HASH ) ;
296290 }
297291
298292 #[ wasm_bindgen_test]
299293 async fn get_mithril_stake_distribution_should_fail_with_unknown_hash ( ) {
300- let client = MithrilClient::new(AGGREGATOR_ENDPOINT, GENESIS_VERIFICATION_KEY);
301-
302- client
294+ get_mithril_client ( )
303295 . get_mithril_stake_distribution ( "whatever" )
304296 . await
305297 . expect_err ( "get_mithril_stake_distribution should fail" ) ;
306298 }
307299
308300 #[ wasm_bindgen_test]
309301 async fn list_mithril_certificates_should_return_value_convertible_in_rust_type ( ) {
310- let client = MithrilClient::new(AGGREGATOR_ENDPOINT, GENESIS_VERIFICATION_KEY);
311-
312- let certificates_list_js_value = client
302+ let certificates_list_js_value = get_mithril_client ( )
313303 . list_mithril_certificates ( )
314304 . await
315305 . expect ( "list_mithril_certificates should not fail" ) ;
@@ -318,35 +308,25 @@ mod tests {
318308 )
319309 . expect ( "conversion should not fail" ) ;
320310
321- assert_eq!(certificates_list.len(), 20 );
311+ assert_eq ! ( certificates_list. len( ) , 7 ) ;
322312 }
323313
324314 #[ wasm_bindgen_test]
325315 async fn get_mithril_certificate_should_return_value_convertible_in_rust_type ( ) {
326- let client = MithrilClient::new(AGGREGATOR_ENDPOINT, GENESIS_VERIFICATION_KEY);
327- let certificates_list_js_value = client.list_mithril_certificates().await.unwrap();
328- let certificates_list = serde_wasm_bindgen::from_value::<Vec<MithrilCertificateListItem>>(
329- certificates_list_js_value,
330- )
331- .unwrap();
332- let last_hash = &certificates_list.first().unwrap().hash;
333-
334- let certificate_js_value = client
335- .get_mithril_certificate(last_hash)
316+ let certificate_js_value = get_mithril_client ( )
317+ . get_mithril_certificate ( FAKE_CERTIFICATE_HASH )
336318 . await
337319 . expect ( "get_mithril_certificate should not fail" ) ;
338320 let certificate =
339321 serde_wasm_bindgen:: from_value :: < MithrilCertificate > ( certificate_js_value)
340322 . expect ( "conversion should not fail" ) ;
341323
342- assert_eq!(certificate.hash, last_hash.to_string() );
324+ assert_eq ! ( certificate. hash, FAKE_CERTIFICATE_HASH ) ;
343325 }
344326
345327 #[ wasm_bindgen_test]
346328 async fn get_mithril_certificate_should_fail_with_unknown_hash ( ) {
347- let client = MithrilClient::new(AGGREGATOR_ENDPOINT, GENESIS_VERIFICATION_KEY);
348-
349- client
329+ get_mithril_client ( )
350330 . get_mithril_certificate ( "whatever" )
351331 . await
352332 . expect_err ( "get_mithril_certificate should fail" ) ;
@@ -355,15 +335,9 @@ mod tests {
355335 #[ wasm_bindgen_test]
356336 async fn compute_mithril_stake_distribution_message_should_return_value_convertible_in_rust_type (
357337 ) {
358- let client = MithrilClient::new(AGGREGATOR_ENDPOINT, GENESIS_VERIFICATION_KEY);
359- let msd_list_js_value = client.list_mithril_stake_distributions().await.unwrap();
360- let msd_list = serde_wasm_bindgen::from_value::<Vec<MithrilStakeDistributionListItem>>(
361- msd_list_js_value,
362- )
363- .unwrap();
364- let last_hash = &msd_list.first().unwrap().hash;
338+ let client = get_mithril_client ( ) ;
365339 let msd_js_value = client
366- .get_mithril_stake_distribution(last_hash )
340+ . get_mithril_stake_distribution ( FAKE_AGGREGATOR_MSD_HASH )
367341 . await
368342 . unwrap ( ) ;
369343
@@ -375,20 +349,11 @@ mod tests {
375349 . expect ( "conversion should not fail" ) ;
376350 }
377351
378- // This test is commented for now as it's execution takes too long with a real testnet aggregator
379- // Timeout in tests is 20s
380- /*
381352 #[ wasm_bindgen_test]
382353 async fn verify_certificate_chain_should_return_value_convertible_in_rust_type ( ) {
383- let client = MithrilClient::new(AGGREGATOR_ENDPOINT, GENESIS_VERIFICATION_KEY);
384- let msd_list_js_value = client.list_mithril_stake_distributions().await.unwrap();
385- let msd_list = serde_wasm_bindgen::from_value::<Vec<MithrilStakeDistributionListItem>>(
386- msd_list_js_value,
387- )
388- .unwrap();
389- let last_hash = &msd_list.first().unwrap().hash;
354+ let client = get_mithril_client ( ) ;
390355 let msd_js_value = client
391- .get_mithril_stake_distribution(last_hash )
356+ . get_mithril_stake_distribution ( FAKE_AGGREGATOR_MSD_HASH )
392357 . await
393358 . unwrap ( ) ;
394359 let msd = serde_wasm_bindgen:: from_value :: < MithrilStakeDistribution > ( msd_js_value) . unwrap ( ) ;
@@ -400,22 +365,12 @@ mod tests {
400365 serde_wasm_bindgen:: from_value :: < MithrilCertificate > ( certificate_js_value)
401366 . expect ( "conversion should not fail" ) ;
402367 }
403- */
404368
405- // This test is commented for now as it's execution takes too long with a real testnet aggregator
406- // Timeout in tests is 20s
407- /*
408369 #[ wasm_bindgen_test]
409370 async fn verify_message_match_certificate_should_return_true ( ) {
410- let client = MithrilClient::new(AGGREGATOR_ENDPOINT, GENESIS_VERIFICATION_KEY);
411- let msd_list_js_value = client.list_mithril_stake_distributions().await.unwrap();
412- let msd_list = serde_wasm_bindgen::from_value::<Vec<MithrilStakeDistributionListItem>>(
413- msd_list_js_value,
414- )
415- .unwrap();
416- let last_hash = &msd_list.first().unwrap().hash;
371+ let client = get_mithril_client ( ) ;
417372 let msd_js_value = client
418- .get_mithril_stake_distribution(last_hash )
373+ . get_mithril_stake_distribution ( FAKE_AGGREGATOR_MSD_HASH )
419374 . await
420375 . unwrap ( ) ;
421376 let msd = serde_wasm_bindgen:: from_value :: < MithrilStakeDistribution > ( msd_js_value. clone ( ) )
@@ -434,6 +389,4 @@ mod tests {
434389 . await
435390 . expect ( "verify_message_match_certificate should not fail" ) ;
436391 }
437- */
438392}
439- */
0 commit comments