Skip to content

Commit 92aa85f

Browse files
author
Damien LACHAUME / PALO-IT
committed
First implementation of tests
This is a first step with data fetched from `testing-preview` network. The second step will be to use the FakeAggregator binary to expand the testing coverage.
1 parent 8d93d2f commit 92aa85f

File tree

1 file changed

+244
-11
lines changed

1 file changed

+244
-11
lines changed

mithril-client-wasm/src/client_wasm.rs

Lines changed: 244 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ pub struct MithrilClient {
5959
impl MithrilClient {
6060
/// Constructor for wasm client
6161
#[wasm_bindgen(constructor)]
62-
pub async fn new(aggregator_endpoint: &str, genesis_verification_key: &str) -> MithrilClient {
62+
pub fn new(aggregator_endpoint: &str, genesis_verification_key: &str) -> MithrilClient {
6363
let feedback_receiver = Arc::new(JSBroadcastChannelFeedbackReceiver::new("mithril-client"));
6464
let client = ClientBuilder::aggregator(aggregator_endpoint, genesis_verification_key)
6565
.add_feedback_receiver(feedback_receiver)
@@ -76,12 +76,16 @@ impl MithrilClient {
7676
.snapshot()
7777
.get(digest)
7878
.await
79-
.map_err(|err| format!("{err:?}"))?;
79+
.map_err(|err| format!("{err:?}"))?
80+
.ok_or(JsValue::from_str(&format!(
81+
"No snapshot found for digest: '{digest}'"
82+
)))?;
8083

8184
Ok(serde_wasm_bindgen::to_value(&result)?)
8285
}
8386

8487
/// Call the client to get the list of available snapshots
88+
#[wasm_bindgen]
8589
pub async fn list_snapshots(&self) -> WasmResult {
8690
let result = self
8791
.client
@@ -94,18 +98,23 @@ impl MithrilClient {
9498
}
9599

96100
/// Call the client to get a mithril stake distribution from a hash
101+
#[wasm_bindgen]
97102
pub async fn get_mithril_stake_distribution(&self, hash: &str) -> WasmResult {
98103
let result = self
99104
.client
100105
.mithril_stake_distribution()
101106
.get(hash)
102107
.await
103-
.map_err(|err| format!("{err:?}"))?;
108+
.map_err(|err| format!("{err:?}"))?
109+
.ok_or(JsValue::from_str(&format!(
110+
"No mithril stake distribution found for hash: '{hash}'"
111+
)))?;
104112

105113
Ok(serde_wasm_bindgen::to_value(&result)?)
106114
}
107115

108116
/// Call the client for the list of available mithril stake distributions
117+
#[wasm_bindgen]
109118
pub async fn list_mithril_stake_distributions(&self) -> WasmResult {
110119
let result = self
111120
.client
@@ -118,6 +127,7 @@ impl MithrilClient {
118127
}
119128

120129
/// Call the client to compute a mithril stake distribution message
130+
#[wasm_bindgen]
121131
pub async fn compute_mithril_stake_distribution_message(
122132
&self,
123133
stake_distribution: JsValue,
@@ -132,6 +142,7 @@ impl MithrilClient {
132142
}
133143

134144
/// Call the client to verify a mithril stake distribution message
145+
#[wasm_bindgen]
135146
pub async fn verify_message_match_certificate(
136147
&self,
137148
message: JsValue,
@@ -145,18 +156,23 @@ impl MithrilClient {
145156
}
146157

147158
/// Call the client to get a mithril certificate from a certificate hash
159+
#[wasm_bindgen]
148160
pub async fn get_mithril_certificate(&self, hash: &str) -> WasmResult {
149161
let result = self
150162
.client
151163
.certificate()
152164
.get(hash)
153165
.await
154-
.map_err(|err| format!("{err:?}"))?;
166+
.map_err(|err| format!("{err:?}"))?
167+
.ok_or(JsValue::from_str(&format!(
168+
"No certificate found for hash: '{hash}'"
169+
)))?;
155170

156171
Ok(serde_wasm_bindgen::to_value(&result)?)
157172
}
158173

159174
/// Call the client for the list of available mithril certificates
175+
#[wasm_bindgen]
160176
pub async fn list_mithril_certificates(&self) -> WasmResult {
161177
let result = self
162178
.client
@@ -169,6 +185,7 @@ impl MithrilClient {
169185
}
170186

171187
/// Call the client to verify the certificate chain from a certificate hash
188+
#[wasm_bindgen]
172189
pub async fn verify_certificate_chain(&self, hash: &str) -> WasmResult {
173190
let result = self
174191
.client
@@ -184,19 +201,235 @@ impl MithrilClient {
184201
#[cfg(test)]
185202
mod tests {
186203
use super::*;
204+
use mithril_client::{
205+
common::ProtocolMessage, MithrilCertificateListItem, MithrilStakeDistribution,
206+
MithrilStakeDistributionListItem, Snapshot, SnapshotListItem,
207+
};
187208
use wasm_bindgen_test::*;
188209

210+
const AGGREGATOR_ENDPOINT: &str =
211+
"https://aggregator.testing-preview.api.mithril.network/aggregator";
212+
const GENESIS_VERIFICATION_KEY: &str = "5b33322c3235332c3138362c3230312c3137372c31312c3131372c3133352c3138372c3136372c3138312c3138382c32322c35392c3230362c3130352c3233312c3135302c3231352c33302c37382c3231322c37362c31362c3235322c3138302c37322c3133342c3133372c3234372c3136312c36385d";
213+
189214
wasm_bindgen_test_configure!(run_in_browser);
190215
#[wasm_bindgen_test]
191-
async fn certificate_get_list() {
192-
let wasm_client = MithrilClient::new(
193-
"https://aggregator.testing-preview.api.mithril.network/aggregator",
194-
"5b33322c3235332c3138362c3230312c3137372c31312c3131372c3133352c3138372c3136372c3138312c3138382c32322c35392c3230362c3130352c3233312c3135302c3231352c33302c37382c3231322c37362c31362c3235322c3138302c37322c3133342c3133372c3234372c3136312c36385d",
195-
).await;
216+
async fn list_snapshots_should_return_value_convertible_in_rust_type() {
217+
let client = MithrilClient::new(AGGREGATOR_ENDPOINT, GENESIS_VERIFICATION_KEY);
218+
219+
let snapshots_list_js_value = client
220+
.list_snapshots()
221+
.await
222+
.expect("list_snapshots should not fail");
223+
let snapshots_list =
224+
serde_wasm_bindgen::from_value::<Vec<SnapshotListItem>>(snapshots_list_js_value)
225+
.expect("conversion should not fail");
226+
227+
assert_eq!(snapshots_list.len(), 20);
228+
}
229+
230+
#[wasm_bindgen_test]
231+
async fn get_snapshot_should_return_value_convertible_in_rust_type() {
232+
let client = MithrilClient::new(AGGREGATOR_ENDPOINT, GENESIS_VERIFICATION_KEY);
233+
let snapshots_list_js_value = client.list_snapshots().await.unwrap();
234+
let snapshots_list =
235+
serde_wasm_bindgen::from_value::<Vec<SnapshotListItem>>(snapshots_list_js_value)
236+
.unwrap();
237+
let last_digest = &snapshots_list.first().unwrap().digest;
238+
239+
let snapshot_js_value = client
240+
.get_snapshot(last_digest)
241+
.await
242+
.expect("get_snapshot should not fail");
243+
let snapshot = serde_wasm_bindgen::from_value::<Snapshot>(snapshot_js_value)
244+
.expect("conversion should not fail");
245+
246+
assert_eq!(snapshot.digest, last_digest.to_string());
247+
}
248+
249+
#[wasm_bindgen_test]
250+
async fn get_snapshot_should_fail_with_unknown_digest() {
251+
let client = MithrilClient::new(AGGREGATOR_ENDPOINT, GENESIS_VERIFICATION_KEY);
252+
253+
client
254+
.get_snapshot("whatever")
255+
.await
256+
.expect_err("get_snapshot should fail");
257+
}
258+
259+
#[wasm_bindgen_test]
260+
async fn list_mithril_stake_distributions_should_return_value_convertible_in_rust_type() {
261+
let wasm_client = MithrilClient::new(AGGREGATOR_ENDPOINT, GENESIS_VERIFICATION_KEY);
196262

197-
wasm_client
263+
let msd_list_js_value = wasm_client
198264
.list_mithril_stake_distributions()
199265
.await
200-
.expect("should not fail");
266+
.expect("list_mithril_stake_distributions should not fail");
267+
let msd_list = serde_wasm_bindgen::from_value::<Vec<MithrilStakeDistributionListItem>>(
268+
msd_list_js_value,
269+
)
270+
.expect("conversion should not fail");
271+
272+
assert_eq!(msd_list.len(), 20);
273+
}
274+
275+
#[wasm_bindgen_test]
276+
async fn get_mithril_stake_distribution_should_return_value_convertible_in_rust_type() {
277+
let client = MithrilClient::new(AGGREGATOR_ENDPOINT, GENESIS_VERIFICATION_KEY);
278+
let msd_list_js_value = client.list_mithril_stake_distributions().await.unwrap();
279+
let msd_list = serde_wasm_bindgen::from_value::<Vec<MithrilStakeDistributionListItem>>(
280+
msd_list_js_value,
281+
)
282+
.unwrap();
283+
let last_hash = &msd_list.first().unwrap().hash;
284+
285+
let msd_js_value = client
286+
.get_mithril_stake_distribution(last_hash)
287+
.await
288+
.expect("get_mithril_stake_distribution should not fail");
289+
let msd = serde_wasm_bindgen::from_value::<MithrilStakeDistribution>(msd_js_value)
290+
.expect("conversion should not fail");
291+
292+
assert_eq!(msd.hash, last_hash.to_string());
293+
}
294+
295+
#[wasm_bindgen_test]
296+
async fn get_mithril_stake_distribution_should_fail_with_unknown_hash() {
297+
let client = MithrilClient::new(AGGREGATOR_ENDPOINT, GENESIS_VERIFICATION_KEY);
298+
299+
client
300+
.get_mithril_stake_distribution("whatever")
301+
.await
302+
.expect_err("get_mithril_stake_distribution should fail");
303+
}
304+
305+
#[wasm_bindgen_test]
306+
async fn list_mithril_certificates_should_return_value_convertible_in_rust_type() {
307+
let client = MithrilClient::new(AGGREGATOR_ENDPOINT, GENESIS_VERIFICATION_KEY);
308+
309+
let certificates_list_js_value = client
310+
.list_mithril_certificates()
311+
.await
312+
.expect("list_mithril_certificates should not fail");
313+
let certificates_list = serde_wasm_bindgen::from_value::<Vec<MithrilCertificateListItem>>(
314+
certificates_list_js_value,
315+
)
316+
.expect("conversion should not fail");
317+
318+
assert_eq!(certificates_list.len(), 20);
319+
}
320+
321+
#[wasm_bindgen_test]
322+
async fn get_mithril_certificate_should_return_value_convertible_in_rust_type() {
323+
let client = MithrilClient::new(AGGREGATOR_ENDPOINT, GENESIS_VERIFICATION_KEY);
324+
let certificates_list_js_value = client.list_mithril_certificates().await.unwrap();
325+
let certificates_list = serde_wasm_bindgen::from_value::<Vec<MithrilCertificateListItem>>(
326+
certificates_list_js_value,
327+
)
328+
.unwrap();
329+
let last_hash = &certificates_list.first().unwrap().hash;
330+
331+
let certificate_js_value = client
332+
.get_mithril_certificate(last_hash)
333+
.await
334+
.expect("get_mithril_certificate should not fail");
335+
let certificate =
336+
serde_wasm_bindgen::from_value::<MithrilCertificate>(certificate_js_value)
337+
.expect("conversion should not fail");
338+
339+
assert_eq!(certificate.hash, last_hash.to_string());
340+
}
341+
342+
#[wasm_bindgen_test]
343+
async fn get_mithril_certificate_should_fail_with_unknown_hash() {
344+
let client = MithrilClient::new(AGGREGATOR_ENDPOINT, GENESIS_VERIFICATION_KEY);
345+
346+
client
347+
.get_mithril_certificate("whatever")
348+
.await
349+
.expect_err("get_mithril_certificate should fail");
350+
}
351+
352+
#[wasm_bindgen_test]
353+
async fn compute_mithril_stake_distribution_message_should_return_value_convertible_in_rust_type(
354+
) {
355+
let client = MithrilClient::new(AGGREGATOR_ENDPOINT, GENESIS_VERIFICATION_KEY);
356+
let msd_list_js_value = client.list_mithril_stake_distributions().await.unwrap();
357+
let msd_list = serde_wasm_bindgen::from_value::<Vec<MithrilStakeDistributionListItem>>(
358+
msd_list_js_value,
359+
)
360+
.unwrap();
361+
let last_hash = &msd_list.first().unwrap().hash;
362+
let msd_js_value = client
363+
.get_mithril_stake_distribution(last_hash)
364+
.await
365+
.unwrap();
366+
367+
let message_js_value = client
368+
.compute_mithril_stake_distribution_message(msd_js_value)
369+
.await
370+
.expect("compute_mithril_stake_distribution_message should not fail");
371+
serde_wasm_bindgen::from_value::<ProtocolMessage>(message_js_value)
372+
.expect("conversion should not fail");
373+
}
374+
375+
// This test is commented for now as it's execution takes too long with a real testnet aggregator
376+
// Timeout in tests is 20s
377+
/*
378+
#[wasm_bindgen_test]
379+
async fn verify_certificate_chain_should_return_value_convertible_in_rust_type() {
380+
let client = MithrilClient::new(AGGREGATOR_ENDPOINT, GENESIS_VERIFICATION_KEY);
381+
let msd_list_js_value = client.list_mithril_stake_distributions().await.unwrap();
382+
let msd_list = serde_wasm_bindgen::from_value::<Vec<MithrilStakeDistributionListItem>>(
383+
msd_list_js_value,
384+
)
385+
.unwrap();
386+
let last_hash = &msd_list.first().unwrap().hash;
387+
let msd_js_value = client
388+
.get_mithril_stake_distribution(last_hash)
389+
.await
390+
.unwrap();
391+
let msd = serde_wasm_bindgen::from_value::<MithrilStakeDistribution>(msd_js_value).unwrap();
392+
393+
let certificate_js_value = client
394+
.verify_certificate_chain(&msd.certificate_hash)
395+
.await
396+
.expect("verify_certificate_chain should not fail");
397+
serde_wasm_bindgen::from_value::<MithrilCertificate>(certificate_js_value)
398+
.expect("conversion should not fail");
399+
}
400+
*/
401+
402+
// This test is commented for now as it's execution takes too long with a real testnet aggregator
403+
// Timeout in tests is 20s
404+
/*
405+
#[wasm_bindgen_test]
406+
async fn verify_message_match_certificate_should_return_true() {
407+
let client = MithrilClient::new(AGGREGATOR_ENDPOINT, GENESIS_VERIFICATION_KEY);
408+
let msd_list_js_value = client.list_mithril_stake_distributions().await.unwrap();
409+
let msd_list = serde_wasm_bindgen::from_value::<Vec<MithrilStakeDistributionListItem>>(
410+
msd_list_js_value,
411+
)
412+
.unwrap();
413+
let last_hash = &msd_list.first().unwrap().hash;
414+
let msd_js_value = client
415+
.get_mithril_stake_distribution(last_hash)
416+
.await
417+
.unwrap();
418+
let msd = serde_wasm_bindgen::from_value::<MithrilStakeDistribution>(msd_js_value.clone())
419+
.unwrap();
420+
let last_certificate_js_value = client
421+
.verify_certificate_chain(&msd.certificate_hash)
422+
.await
423+
.unwrap();
424+
let message_js_value = client
425+
.compute_mithril_stake_distribution_message(msd_js_value)
426+
.await
427+
.unwrap();
428+
429+
client
430+
.verify_message_match_certificate(message_js_value, last_certificate_js_value)
431+
.await
432+
.expect("verify_message_match_certificate should not fail");
201433
}
434+
*/
202435
}

0 commit comments

Comments
 (0)