Skip to content

Commit 06b62b7

Browse files
committed
client_wasm tests now use autogenerated test data from fake aggregator 'default_data' json files
Add `get_cardano_transaction_set` function
1 parent 5a1e622 commit 06b62b7

File tree

7 files changed

+155
-23
lines changed

7 files changed

+155
-23
lines changed

Diff for: Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: mithril-client-wasm/Cargo.toml

+4-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ crate-type = ["cdylib"]
1515
[dependencies]
1616
async-trait = "0.1.77"
1717
futures = "0.3.30"
18-
mithril-client = { path = "../mithril-client" }
18+
mithril-client = { path = "../mithril-client", features = ["unstable"] }
1919
serde = { version = "1.0.196", features = ["derive"] }
2020
serde-wasm-bindgen = "0.6.3"
2121
wasm-bindgen = "0.2.90"
@@ -25,6 +25,9 @@ web-sys = { version = "0.3.67", features = ["BroadcastChannel"] }
2525
[dev-dependencies]
2626
wasm-bindgen-test = "0.3.40"
2727

28+
[build-dependencies]
29+
mithril-build-script = { path = "../mithril-build-script" }
30+
2831
[features]
2932
# Include nothing by default
3033
default = []

Diff for: mithril-client-wasm/build.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// build.rs
2+
3+
use mithril_build_script::fake_aggregator::FakeAggregatorData;
4+
use std::env;
5+
use std::fs;
6+
use std::path::Path;
7+
8+
fn main() {
9+
let out_dir = env::var_os("OUT_DIR").unwrap();
10+
let dest_path = Path::new(&out_dir).join("imported_data.rs");
11+
let fake_aggregator_crate_path =
12+
mithril_build_script::get_package_path("mithril-aggregator-fake");
13+
14+
let data_folder_path = fake_aggregator_crate_path.join("default_data");
15+
let data = FakeAggregatorData::load_from_folder(&data_folder_path);
16+
let generated_code = data.generate_code_for_ids();
17+
fs::write(dest_path, generated_code).unwrap();
18+
19+
println!(
20+
"cargo:rerun-if-changed={}/",
21+
fake_aggregator_crate_path.display()
22+
);
23+
}

Diff for: mithril-client-wasm/src/client_wasm.rs

+120-22
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,18 @@ impl From<MithrilEvent> for MithrilEventWasm {
5151
}
5252

5353
/// Structure that wraps a [Client] and enables its functions to be used in WASM
54-
#[wasm_bindgen]
54+
#[wasm_bindgen(getter_with_clone)]
5555
pub struct MithrilClient {
5656
client: Client,
57+
58+
/// Unstable fonctions
59+
pub unstable: MithrilUnstableClient,
60+
}
61+
62+
#[wasm_bindgen]
63+
#[derive(Clone)]
64+
pub struct MithrilUnstableClient {
65+
client: Client,
5766
}
5867

5968
#[wasm_bindgen]
@@ -67,7 +76,8 @@ impl MithrilClient {
6776
.build()
6877
.map_err(|err| format!("{err:?}"))
6978
.unwrap();
70-
MithrilClient { client }
79+
let unstable = MithrilUnstableClient::new(client.clone());
80+
MithrilClient { client, unstable }
7181
}
7282

7383
/// Call the client to get a snapshot from a digest
@@ -200,24 +210,58 @@ impl MithrilClient {
200210
}
201211
}
202212

213+
#[wasm_bindgen]
214+
impl MithrilUnstableClient {
215+
/// Constructor for unstable wasm client
216+
fn new(inner_client: Client) -> MithrilUnstableClient {
217+
Self {
218+
client: inner_client,
219+
}
220+
}
221+
222+
/// Call the client for the list of available Cardano transactions sets
223+
#[wasm_bindgen]
224+
pub async fn list_cardano_transaction_sets(&self) -> WasmResult {
225+
let result = self
226+
.client
227+
.cardano_transaction_proof()
228+
.list()
229+
.await
230+
.map_err(|err| format!("{err:?}"))?;
231+
232+
Ok(serde_wasm_bindgen::to_value(&result)?)
233+
}
234+
235+
/// Call the client to get a Cardano transactions set from a hash
236+
#[wasm_bindgen]
237+
pub async fn get_cardano_transaction_set(&self, hash: &str) -> WasmResult {
238+
let result = self
239+
.client
240+
.cardano_transaction_proof()
241+
.get(hash)
242+
.await
243+
.map_err(|err| format!("{err:?}"))?
244+
.ok_or(JsValue::from_str(&format!(
245+
"No cardano transactions commitment found for hash: '{hash}'"
246+
)))?;
247+
248+
Ok(serde_wasm_bindgen::to_value(&result)?)
249+
}
250+
}
251+
203252
#[cfg(test)]
204253
mod tests {
205254
use super::*;
255+
use crate::test_data;
206256
use mithril_client::{
207-
common::ProtocolMessage, MithrilCertificateListItem, MithrilStakeDistribution,
208-
MithrilStakeDistributionListItem, Snapshot, SnapshotListItem,
257+
common::ProtocolMessage, CardanoTransactionCommitment, MithrilCertificateListItem,
258+
MithrilStakeDistribution, MithrilStakeDistributionListItem, Snapshot, SnapshotListItem,
209259
};
210260
use wasm_bindgen_test::*;
211261

212262
const GENESIS_VERIFICATION_KEY: &str = "5b33322c3235332c3138362c3230312c3137372c31312c3131372c3133352c3138372c3136372c3138312c3138382c32322c35392c3230362c3130352c3233312c3135302c3231352c33302c37382c3231322c37362c31362c3235322c3138302c37322c3133342c3133372c3234372c3136312c36385d";
213263
const FAKE_AGGREGATOR_IP: &str = "127.0.0.1";
214264
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";
221265

222266
fn get_mithril_client() -> MithrilClient {
223267
MithrilClient::new(
@@ -240,19 +284,23 @@ mod tests {
240284
serde_wasm_bindgen::from_value::<Vec<SnapshotListItem>>(snapshots_list_js_value)
241285
.expect("conversion should not fail");
242286

243-
assert_eq!(snapshots_list.len(), 3);
287+
assert_eq!(
288+
snapshots_list.len(),
289+
// Aggregator return up to 20 items for a list route
290+
test_data::snapshot_digests().len().min(20)
291+
);
244292
}
245293

246294
#[wasm_bindgen_test]
247295
async fn get_snapshot_should_return_value_convertible_in_rust_type() {
248296
let snapshot_js_value = get_mithril_client()
249-
.get_snapshot(FAKE_AGGREGATOR_SNAPSHOT_DIGEST)
297+
.get_snapshot(test_data::snapshot_digests()[0])
250298
.await
251299
.expect("get_snapshot should not fail");
252300
let snapshot = serde_wasm_bindgen::from_value::<Snapshot>(snapshot_js_value)
253301
.expect("conversion should not fail");
254302

255-
assert_eq!(snapshot.digest, FAKE_AGGREGATOR_SNAPSHOT_DIGEST);
303+
assert_eq!(snapshot.digest, test_data::snapshot_digests()[0]);
256304
}
257305

258306
#[wasm_bindgen_test]
@@ -274,19 +322,23 @@ mod tests {
274322
)
275323
.expect("conversion should not fail");
276324

277-
assert_eq!(msd_list.len(), 3);
325+
assert_eq!(
326+
msd_list.len(),
327+
// Aggregator return up to 20 items for a list route
328+
test_data::msd_hashes().len().min(20)
329+
);
278330
}
279331

280332
#[wasm_bindgen_test]
281333
async fn get_mithril_stake_distribution_should_return_value_convertible_in_rust_type() {
282334
let msd_js_value = get_mithril_client()
283-
.get_mithril_stake_distribution(FAKE_AGGREGATOR_MSD_HASH)
335+
.get_mithril_stake_distribution(test_data::msd_hashes()[0])
284336
.await
285337
.expect("get_mithril_stake_distribution should not fail");
286338
let msd = serde_wasm_bindgen::from_value::<MithrilStakeDistribution>(msd_js_value)
287339
.expect("conversion should not fail");
288340

289-
assert_eq!(msd.hash, FAKE_AGGREGATOR_MSD_HASH);
341+
assert_eq!(msd.hash, test_data::msd_hashes()[0]);
290342
}
291343

292344
#[wasm_bindgen_test]
@@ -308,20 +360,24 @@ mod tests {
308360
)
309361
.expect("conversion should not fail");
310362

311-
assert_eq!(certificates_list.len(), 7);
363+
assert_eq!(
364+
certificates_list.len(),
365+
// Aggregator return up to 20 items for a list route
366+
test_data::certificate_hashes().len().min(20)
367+
);
312368
}
313369

314370
#[wasm_bindgen_test]
315371
async fn get_mithril_certificate_should_return_value_convertible_in_rust_type() {
316372
let certificate_js_value = get_mithril_client()
317-
.get_mithril_certificate(FAKE_CERTIFICATE_HASH)
373+
.get_mithril_certificate(test_data::certificate_hashes()[0])
318374
.await
319375
.expect("get_mithril_certificate should not fail");
320376
let certificate =
321377
serde_wasm_bindgen::from_value::<MithrilCertificate>(certificate_js_value)
322378
.expect("conversion should not fail");
323379

324-
assert_eq!(certificate.hash, FAKE_CERTIFICATE_HASH);
380+
assert_eq!(certificate.hash, test_data::certificate_hashes()[0]);
325381
}
326382

327383
#[wasm_bindgen_test]
@@ -337,7 +393,7 @@ mod tests {
337393
) {
338394
let client = get_mithril_client();
339395
let msd_js_value = client
340-
.get_mithril_stake_distribution(FAKE_AGGREGATOR_MSD_HASH)
396+
.get_mithril_stake_distribution(test_data::msd_hashes()[0])
341397
.await
342398
.unwrap();
343399

@@ -353,7 +409,7 @@ mod tests {
353409
async fn verify_certificate_chain_should_return_value_convertible_in_rust_type() {
354410
let client = get_mithril_client();
355411
let msd_js_value = client
356-
.get_mithril_stake_distribution(FAKE_AGGREGATOR_MSD_HASH)
412+
.get_mithril_stake_distribution(test_data::msd_hashes()[0])
357413
.await
358414
.unwrap();
359415
let msd = serde_wasm_bindgen::from_value::<MithrilStakeDistribution>(msd_js_value).unwrap();
@@ -370,7 +426,7 @@ mod tests {
370426
async fn verify_message_match_certificate_should_return_true() {
371427
let client = get_mithril_client();
372428
let msd_js_value = client
373-
.get_mithril_stake_distribution(FAKE_AGGREGATOR_MSD_HASH)
429+
.get_mithril_stake_distribution(test_data::msd_hashes()[0])
374430
.await
375431
.unwrap();
376432
let msd = serde_wasm_bindgen::from_value::<MithrilStakeDistribution>(msd_js_value.clone())
@@ -389,4 +445,46 @@ mod tests {
389445
.await
390446
.expect("verify_message_match_certificate should not fail");
391447
}
448+
449+
#[wasm_bindgen_test]
450+
async fn list_cardano_transaction_sets_should_return_value_convertible_in_rust_type() {
451+
let cardano_tx_sets_js_value = get_mithril_client()
452+
.unstable
453+
.list_cardano_transaction_sets()
454+
.await
455+
.expect("list_cardano_transaction_sets should not fail");
456+
let cardano_tx_sets = serde_wasm_bindgen::from_value::<Vec<CardanoTransactionCommitment>>(
457+
cardano_tx_sets_js_value,
458+
)
459+
.expect("conversion should not fail");
460+
461+
assert_eq!(
462+
cardano_tx_sets.len(),
463+
// Aggregator return up to 20 items for a list route
464+
test_data::ctx_commitment_hashes().len().min(20)
465+
);
466+
}
467+
468+
#[wasm_bindgen_test]
469+
async fn get_cardano_transaction_set_should_return_value_convertible_in_rust_type() {
470+
let cardano_tx_set_js_value = get_mithril_client()
471+
.unstable
472+
.get_cardano_transaction_set(test_data::ctx_commitment_hashes()[0])
473+
.await
474+
.expect("get_cardano_transaction_set should not fail");
475+
let cardano_tx_set =
476+
serde_wasm_bindgen::from_value::<CardanoTransactionCommitment>(cardano_tx_set_js_value)
477+
.expect("conversion should not fail");
478+
479+
assert_eq!(cardano_tx_set.hash, test_data::ctx_commitment_hashes()[0]);
480+
}
481+
482+
#[wasm_bindgen_test]
483+
async fn get_cardano_transaction_set_should_fail_with_unknown_digest() {
484+
get_mithril_client()
485+
.unstable
486+
.get_cardano_transaction_set("whatever")
487+
.await
488+
.expect_err("get_cardano_transaction_set should fail");
489+
}
392490
}

Diff for: mithril-client-wasm/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@
33
mod client_wasm;
44

55
pub use client_wasm::MithrilClient;
6+
7+
#[cfg(test)]
8+
mod test_data;

Diff for: mithril-client-wasm/src/test_data.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#![allow(dead_code)]
2+
3+
include!(concat!(env!("OUT_DIR"), "/imported_data.rs"));

Diff for: mithril-client/src/client.rs

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use crate::MithrilResult;
2020
/// Structure that aggregates the available clients for each of the Mithril types of certified data.
2121
///
2222
/// Use the [ClientBuilder] to instantiate it easily.
23+
#[derive(Clone)]
2324
pub struct Client {
2425
#[cfg(feature = "unstable")]
2526
cardano_transaction_proof_client: Arc<CardanoTransactionProofClient>,

0 commit comments

Comments
 (0)