@@ -50,6 +50,7 @@ impl From<MithrilEvent> for MithrilEventWasm {
50
50
}
51
51
}
52
52
53
+ /// Structure that wraps a [Client] and enables its functions to be used in WASM
53
54
#[ wasm_bindgen]
54
55
pub struct MithrilClient {
55
56
client : Client ,
@@ -199,8 +200,6 @@ impl MithrilClient {
199
200
}
200
201
}
201
202
202
- // The tests are commented for now, as we don't want to run them on a testnet aggregator.
203
- /*
204
203
#[ cfg( test) ]
205
204
mod tests {
206
205
use super :: * ;
@@ -210,60 +209,63 @@ mod tests {
210
209
} ;
211
210
use wasm_bindgen_test:: * ;
212
211
213
- const AGGREGATOR_ENDPOINT: &str =
214
- "https://aggregator.testing-preview.api.mithril.network/aggregator";
215
212
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
+ }
216
231
217
232
wasm_bindgen_test_configure ! ( run_in_browser) ;
218
233
#[ wasm_bindgen_test]
219
234
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 ( )
223
236
. list_snapshots ( )
224
237
. await
225
238
. expect ( "list_snapshots should not fail" ) ;
226
239
let snapshots_list =
227
240
serde_wasm_bindgen:: from_value :: < Vec < SnapshotListItem > > ( snapshots_list_js_value)
228
241
. expect ( "conversion should not fail" ) ;
229
242
230
- assert_eq!(snapshots_list.len(), 20 );
243
+ assert_eq ! ( snapshots_list. len( ) , 3 ) ;
231
244
}
232
245
233
246
#[ wasm_bindgen_test]
234
247
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 )
244
250
. await
245
251
. expect ( "get_snapshot should not fail" ) ;
246
252
let snapshot = serde_wasm_bindgen:: from_value :: < Snapshot > ( snapshot_js_value)
247
253
. expect ( "conversion should not fail" ) ;
248
254
249
- assert_eq!(snapshot.digest, last_digest.to_string() );
255
+ assert_eq ! ( snapshot. digest, FAKE_AGGREGATOR_SNAPSHOT_DIGEST ) ;
250
256
}
251
257
252
258
#[ wasm_bindgen_test]
253
259
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 ( )
257
261
. get_snapshot ( "whatever" )
258
262
. await
259
263
. expect_err ( "get_snapshot should fail" ) ;
260
264
}
261
265
262
266
#[ wasm_bindgen_test]
263
267
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 ( )
267
269
. list_mithril_stake_distributions ( )
268
270
. await
269
271
. expect ( "list_mithril_stake_distributions should not fail" ) ;
@@ -272,44 +274,32 @@ mod tests {
272
274
)
273
275
. expect ( "conversion should not fail" ) ;
274
276
275
- assert_eq!(msd_list.len(), 20 );
277
+ assert_eq ! ( msd_list. len( ) , 3 ) ;
276
278
}
277
279
278
280
#[ wasm_bindgen_test]
279
281
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 )
290
284
. await
291
285
. expect ( "get_mithril_stake_distribution should not fail" ) ;
292
286
let msd = serde_wasm_bindgen:: from_value :: < MithrilStakeDistribution > ( msd_js_value)
293
287
. expect ( "conversion should not fail" ) ;
294
288
295
- assert_eq!(msd.hash, last_hash.to_string() );
289
+ assert_eq ! ( msd. hash, FAKE_AGGREGATOR_MSD_HASH ) ;
296
290
}
297
291
298
292
#[ wasm_bindgen_test]
299
293
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 ( )
303
295
. get_mithril_stake_distribution ( "whatever" )
304
296
. await
305
297
. expect_err ( "get_mithril_stake_distribution should fail" ) ;
306
298
}
307
299
308
300
#[ wasm_bindgen_test]
309
301
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 ( )
313
303
. list_mithril_certificates ( )
314
304
. await
315
305
. expect ( "list_mithril_certificates should not fail" ) ;
@@ -318,35 +308,25 @@ mod tests {
318
308
)
319
309
. expect ( "conversion should not fail" ) ;
320
310
321
- assert_eq!(certificates_list.len(), 20 );
311
+ assert_eq ! ( certificates_list. len( ) , 7 ) ;
322
312
}
323
313
324
314
#[ wasm_bindgen_test]
325
315
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 )
336
318
. await
337
319
. expect ( "get_mithril_certificate should not fail" ) ;
338
320
let certificate =
339
321
serde_wasm_bindgen:: from_value :: < MithrilCertificate > ( certificate_js_value)
340
322
. expect ( "conversion should not fail" ) ;
341
323
342
- assert_eq!(certificate.hash, last_hash.to_string() );
324
+ assert_eq ! ( certificate. hash, FAKE_CERTIFICATE_HASH ) ;
343
325
}
344
326
345
327
#[ wasm_bindgen_test]
346
328
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 ( )
350
330
. get_mithril_certificate ( "whatever" )
351
331
. await
352
332
. expect_err ( "get_mithril_certificate should fail" ) ;
@@ -355,15 +335,9 @@ mod tests {
355
335
#[ wasm_bindgen_test]
356
336
async fn compute_mithril_stake_distribution_message_should_return_value_convertible_in_rust_type (
357
337
) {
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 ( ) ;
365
339
let msd_js_value = client
366
- .get_mithril_stake_distribution(last_hash )
340
+ . get_mithril_stake_distribution ( FAKE_AGGREGATOR_MSD_HASH )
367
341
. await
368
342
. unwrap ( ) ;
369
343
@@ -375,20 +349,11 @@ mod tests {
375
349
. expect ( "conversion should not fail" ) ;
376
350
}
377
351
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
- /*
381
352
#[ wasm_bindgen_test]
382
353
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 ( ) ;
390
355
let msd_js_value = client
391
- .get_mithril_stake_distribution(last_hash )
356
+ . get_mithril_stake_distribution ( FAKE_AGGREGATOR_MSD_HASH )
392
357
. await
393
358
. unwrap ( ) ;
394
359
let msd = serde_wasm_bindgen:: from_value :: < MithrilStakeDistribution > ( msd_js_value) . unwrap ( ) ;
@@ -400,22 +365,12 @@ mod tests {
400
365
serde_wasm_bindgen:: from_value :: < MithrilCertificate > ( certificate_js_value)
401
366
. expect ( "conversion should not fail" ) ;
402
367
}
403
- */
404
368
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
- /*
408
369
#[ wasm_bindgen_test]
409
370
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 ( ) ;
417
372
let msd_js_value = client
418
- .get_mithril_stake_distribution(last_hash )
373
+ . get_mithril_stake_distribution ( FAKE_AGGREGATOR_MSD_HASH )
419
374
. await
420
375
. unwrap ( ) ;
421
376
let msd = serde_wasm_bindgen:: from_value :: < MithrilStakeDistribution > ( msd_js_value. clone ( ) )
@@ -434,6 +389,4 @@ mod tests {
434
389
. await
435
390
. expect ( "verify_message_match_certificate should not fail" ) ;
436
391
}
437
- */
438
392
}
439
- */
0 commit comments