Skip to content

Commit

Permalink
fix(basic_types): bincode deserialization for L2ChainId (#1835)
Browse files Browse the repository at this point in the history
## What ❔

Skip the `serde_json` workarounds, if the serializer is not human
readable.

## Why ❔

Deserialization with the `bincode` serializer is broken, because it is
assumed to be json.

## Checklist

<!-- Check your PR fulfills the following items. -->
<!-- For draft PRs check the boxes as you complete them. -->

- [x] PR title corresponds to the body of PR (we generate changelog
entries from PRs).
- [x] Tests for the changes have been added / updated.
- [ ] Documentation comments have been added / updated.
- [x] Code has been formatted via `zk fmt` and `zk lint`.
- [x] Spellcheck has been run via `zk spellcheck`.
- [x] Linkcheck has been run via `zk linkcheck`.

Signed-off-by: Harald Hoyer <harald@matterlabs.dev>
  • Loading branch information
haraldh authored May 2, 2024
1 parent 82bf40e commit fde85f4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions core/lib/basic_types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ chrono.workspace = true
strum = { workspace = true, features = ["derive"] }
num_enum.workspace = true
anyhow.workspace = true

[dev-dependencies]
bincode.workspace = true
42 changes: 30 additions & 12 deletions core/lib/basic_types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,21 @@ impl<'de> Deserialize<'de> for L2ChainId {
where
D: Deserializer<'de>,
{
let value: serde_json::Value = Deserialize::deserialize(deserializer)?;
match &value {
serde_json::Value::Number(number) => Self::new(number.as_u64().ok_or(
de::Error::custom(format!("Failed to parse: {}, Expected u64", number)),
)?)
.map_err(de::Error::custom),
serde_json::Value::String(string) => string.parse().map_err(de::Error::custom),
_ => Err(de::Error::custom(format!(
"Failed to parse: {}, Expected number or string",
value
))),
if deserializer.is_human_readable() {
let value: serde_json::Value = Deserialize::deserialize(deserializer)?;
match &value {
serde_json::Value::Number(number) => Self::new(number.as_u64().ok_or(
de::Error::custom(format!("Failed to parse: {}, Expected u64", number)),
)?)
.map_err(de::Error::custom),
serde_json::Value::String(string) => string.parse().map_err(de::Error::custom),
_ => Err(de::Error::custom(format!(
"Failed to parse: {}, Expected number or string",
value
))),
}
} else {
u64::deserialize(deserializer).map(L2ChainId)
}
}
}
Expand Down Expand Up @@ -263,9 +267,23 @@ mod tests {
};
let result_ser = serde_json::to_string(&test).unwrap();
let result_deser: Test = serde_json::from_str(&result_ser).unwrap();
assert_eq!(test.chain_id, result_deser.chain_id)
assert_eq!(test.chain_id, result_deser.chain_id);
assert_eq!(result_ser, "{\"chain_id\":200}")
}

#[test]
fn test_serialize_deserialize_bincode() {
#[derive(Serialize, Deserialize)]
struct Test {
chain_id: L2ChainId,
}
let test = Test {
chain_id: L2ChainId(200),
};
let result_ser = bincode::serialize(&test).unwrap();
let result_deser: Test = bincode::deserialize(&result_ser).unwrap();
assert_eq!(test.chain_id, result_deser.chain_id);
}
#[test]
fn test_from_str_valid_hexadecimal() {
let input = "0x2A";
Expand Down

0 comments on commit fde85f4

Please sign in to comment.