Skip to content

Commit 31cd6e2

Browse files
authored
Merge pull request #1441 from input-output-hk/ensemble/1407/implement-fake-aggregator-in-wasm-tests
Implement fake aggregator in wasm tests
2 parents a142158 + 7323570 commit 31cd6e2

File tree

6 files changed

+63
-93
lines changed

6 files changed

+63
-93
lines changed

.github/workflows/ci.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,14 @@ jobs:
131131
cp LICENSE ./mithril-client-wasm/
132132
wasm-pack pack mithril-client-wasm/pkg
133133
134+
- name: Build a fake aggregator
135+
shell: bash
136+
run: cargo build -p mithril-aggregator-fake
137+
138+
- name: Start a fake aggregator
139+
shell: bash
140+
run: cargo run -p mithril-aggregator-fake -- -p 8000 &
141+
134142
- name: Test 'mithril-client-wasm' - Chrome
135143
shell: bash
136144
run: |

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mithril-client-wasm/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "mithril-client-wasm"
3-
version = "0.1.5"
3+
version = "0.1.6"
44
description = "Mithril client WASM"
55
authors = { workspace = true }
66
edition = { workspace = true }

mithril-client-wasm/Makefile

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,14 @@ build:
1313
wasm-pack build --target web --release --out-name index
1414

1515
test:
16-
wasm-pack test --headless --firefox --chrome --node --release
16+
pkill -f "mithril-aggregator-fake" || true
17+
${CARGO} run -p mithril-aggregator-fake -- -p 8000 &
18+
if wasm-pack test --headless --firefox --chrome --node --release; then \
19+
pkill -f "mithril-aggregator-fake" || true; \
20+
else \
21+
pkill -f "mithril-aggregator-fake" || true; \
22+
exit 1; \
23+
fi
1724

1825
check:
1926
${CARGO} check --release --all-features --all-targets

mithril-client-wasm/src/client_wasm.rs

Lines changed: 43 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -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]
5455
pub 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)]
205204
mod 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-
*/

mithril-client-wasm/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![warn(missing_docs)]
2+
//! Implementation of the 'mithril-client' library in WASM
13
mod client_wasm;
24

35
pub use client_wasm::MithrilClient;

0 commit comments

Comments
 (0)