Skip to content

Commit 60d003b

Browse files
authored
light-client-js: remove the syn version pin (informalsystems#1242)
* light-client-js: remove the syn version pin The bug this used to work around is closed and the problem no longer seems to persist. * light-client-js: use serde-wasm-bindgen To remove deprecation warnings and do the right thing, use serde-wasm-bindgen utilities for serializing and deserializing Rust types to and from JsValue. * proto: fix deserialization for PartSerHeader.total Accept also signed integers as these are produced from JSON by serde-wasm-bindgen. * light-client-js: pass by value to the entry point Now that deserialization likes to take ownership of our JS values, it does not make sense to pass them by reference from JavaScript only to clone them immediately. This is not a breaking change for the JavaScript API.
1 parent e9c41de commit 60d003b

File tree

5 files changed

+41
-29
lines changed

5 files changed

+41
-29
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- `[tendermint-light-client-js]` Switch to serde-wasm-bindgen for marshalling
2+
JS values ([#1242](https://github.com/informalsystems/tendermint-rs/pull/1242))

light-client-js/Cargo.toml

+1-2
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,10 @@ default = ["console_error_panic_hook"]
2222
[dependencies]
2323
serde = { version = "1.0", default-features = false, features = [ "derive" ] }
2424
serde_json = { version = "1.0", default-features = false }
25-
# TODO(thane): Remove once https://github.com/rustwasm/wasm-bindgen/issues/2508 is resolved
26-
syn = { version = "=1.0.65", default-features = false }
2725
tendermint = { version = "0.27.0", default-features = false, path = "../tendermint" }
2826
tendermint-light-client-verifier = { version = "0.27.0", default-features = false, path = "../light-client-verifier" }
2927
wasm-bindgen = { version = "0.2.63", default-features = false, features = [ "serde-serialize" ] }
28+
serde-wasm-bindgen = { version = "0.4.5", default-features = false }
3029

3130
# The `console_error_panic_hook` crate provides better debugging of panics by
3231
# logging them with `console.error`. This is great for development, but requires

light-client-js/src/lib.rs

+17-17
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use wasm_bindgen::{prelude::*, JsValue};
2424

2525
/// Check whether a given untrusted block can be trusted.
2626
#[wasm_bindgen]
27-
pub fn verify(untrusted: &JsValue, trusted: &JsValue, options: &JsValue, now: &JsValue) -> JsValue {
27+
pub fn verify(untrusted: JsValue, trusted: JsValue, options: JsValue, now: JsValue) -> JsValue {
2828
let result = deserialize_params(untrusted, trusted, options, now).map(
2929
|(untrusted, trusted, options, now)| {
3030
let verifier = ProdVerifier::default();
@@ -36,35 +36,35 @@ pub fn verify(untrusted: &JsValue, trusted: &JsValue, options: &JsValue, now: &J
3636
)
3737
},
3838
);
39-
JsValue::from_serde(&result).unwrap()
39+
serde_wasm_bindgen::to_value(&result).unwrap()
4040
}
4141

4242
fn deserialize_params(
43-
untrusted: &JsValue,
44-
trusted: &JsValue,
45-
options: &JsValue,
46-
now: &JsValue,
43+
untrusted: JsValue,
44+
trusted: JsValue,
45+
options: JsValue,
46+
now: JsValue,
4747
) -> Result<(LightBlock, LightBlock, Options, Time), Error> {
48-
let untrusted = untrusted.into_serde().map_err(|e| Error::Serialization {
49-
param: "untrusted".to_string(),
50-
msg: e.to_string(),
51-
})?;
48+
let untrusted =
49+
serde_wasm_bindgen::from_value(untrusted).map_err(|e| Error::Serialization {
50+
param: "untrusted".into(),
51+
msg: e.to_string(),
52+
})?;
5253

53-
let trusted = trusted.into_serde().map_err(|e| Error::Serialization {
54-
param: "trusted".to_string(),
54+
let trusted = serde_wasm_bindgen::from_value(trusted).map_err(|e| Error::Serialization {
55+
param: "trusted".into(),
5556
msg: e.to_string(),
5657
})?;
5758

58-
let options = options
59-
.into_serde::<JsOptions>()
59+
let options = serde_wasm_bindgen::from_value::<JsOptions>(options)
6060
.map(Into::into)
6161
.map_err(|e| Error::Serialization {
62-
param: "options".to_string(),
62+
param: "options".into(),
6363
msg: e.to_string(),
6464
})?;
6565

66-
let now = now.into_serde().map_err(|e| Error::Serialization {
67-
param: "now".to_string(),
66+
let now = serde_wasm_bindgen::from_value(now).map_err(|e| Error::Serialization {
67+
param: "now".into(),
6868
msg: e.to_string(),
6969
})?;
7070

light-client-js/tests/web.rs

+13-9
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,11 @@ fn successful_verification() {
161161
let options = test_options();
162162
// Choose a "now" value within the trusting period
163163
let now =
164-
JsValue::from_serde(&Time::parse_from_rfc3339("1970-01-07T00:00:00Z").unwrap()).unwrap();
165-
let js_result = verify(&untrusted_block, &trusted_block, &options, &now);
164+
serde_wasm_bindgen::to_value(&Time::parse_from_rfc3339("1970-01-07T00:00:00Z").unwrap())
165+
.unwrap();
166+
let js_result = verify(untrusted_block, trusted_block, options, now);
166167
console_log!("js_result = {:?}", js_result);
167-
let verdict = JsValue::into_serde::<Result<Verdict, Error>>(&js_result)
168+
let verdict = serde_wasm_bindgen::from_value::<Result<Verdict, Error>>(js_result)
168169
.unwrap()
169170
.unwrap();
170171
assert_eq!(verdict, Verdict::Success);
@@ -176,12 +177,13 @@ fn failed_verification_outside_trusting_period() {
176177
let options = test_options();
177178
// Choose a "now" value outside the trusting period
178179
let now =
179-
JsValue::from_serde(&Time::parse_from_rfc3339("1970-01-16T00:00:00Z").unwrap()).unwrap();
180-
let js_result = verify(&untrusted_block, &trusted_block, &options, &now);
180+
serde_wasm_bindgen::to_value(&Time::parse_from_rfc3339("1970-01-16T00:00:00Z").unwrap())
181+
.unwrap();
182+
let js_result = verify(untrusted_block, trusted_block, options, now);
181183
console_log!("js_result = {:?}", js_result);
182184
// The result is Ok because we successfully obtained a verdict, even if the
183185
// verdict isn't Verdict::Success.
184-
let verdict = JsValue::into_serde::<Result<Verdict, Error>>(&js_result)
186+
let verdict = serde_wasm_bindgen::from_value::<Result<Verdict, Error>>(js_result)
185187
.unwrap()
186188
.unwrap();
187189
match verdict {
@@ -192,14 +194,16 @@ fn failed_verification_outside_trusting_period() {
192194

193195
fn test_blocks() -> (JsValue, JsValue) {
194196
let untrusted_block =
195-
JsValue::from_serde(&serde_json::from_str::<LightBlock>(UNTRUSTED_BLOCK).unwrap()).unwrap();
197+
serde_wasm_bindgen::to_value(&serde_json::from_str::<LightBlock>(UNTRUSTED_BLOCK).unwrap())
198+
.unwrap();
196199
let trusted_block =
197-
JsValue::from_serde(&serde_json::from_str::<LightBlock>(TRUSTED_BLOCK).unwrap()).unwrap();
200+
serde_wasm_bindgen::to_value(&serde_json::from_str::<LightBlock>(TRUSTED_BLOCK).unwrap())
201+
.unwrap();
198202
(untrusted_block, trusted_block)
199203
}
200204

201205
fn test_options() -> JsValue {
202-
JsValue::from_serde(&JsOptions {
206+
serde_wasm_bindgen::to_value(&JsOptions {
203207
trust_threshold: (1, 3),
204208
trusting_period: 1209600, // 2 weeks
205209
clock_drift: 5, // 5 seconds

proto/src/serializers/part_set_header_total.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl<'de> Visitor<'de> for PartSetHeaderTotalStringOrU32 {
3636
type Value = u32;
3737

3838
fn expecting(&self, formatter: &mut Formatter<'_>) -> core::fmt::Result {
39-
formatter.write_str("an u32 integer or string between 0 and 2^32")
39+
formatter.write_str("an integer or string between 0 and 2^32")
4040
}
4141

4242
fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
@@ -46,6 +46,13 @@ impl<'de> Visitor<'de> for PartSetHeaderTotalStringOrU32 {
4646
u32::try_from(v).map_err(|e| E::custom(format!("part_set_header.total {}", e)))
4747
}
4848

49+
fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E>
50+
where
51+
E: Error,
52+
{
53+
u32::try_from(v).map_err(|e| E::custom(format!("part_set_header.total {}", e)))
54+
}
55+
4956
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
5057
where
5158
E: Error,

0 commit comments

Comments
 (0)