Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix: deserialization of null storage keys in AccessListItem #955

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions crates/eips/src/eip2930.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ pub struct AccessListItem {
strategy = "proptest::collection::vec(proptest::arbitrary::any::<B256>(), 0..=20)"
)
)]
// In JSON, we have to accept `null` for storage key, which is interpreted as an empty array.
#[cfg_attr(feature = "serde", serde(deserialize_with = "alloy_serde::optional::or_default"))]
Wodann marked this conversation as resolved.
Show resolved Hide resolved
pub storage_keys: Vec<B256>,
}

Expand Down Expand Up @@ -166,8 +168,24 @@ pub struct AccessListWithGasUsed {

#[cfg(all(test, feature = "serde"))]
mod tests {
use serde_json::json;

use super::*;

#[test]
fn access_list_null_storage_keys() {
let json = json!([
{
"address": "0x81b7bdd5b89c90b63f604fc7cdd17035cb939707",
"storageKeys": null,
}
]);

let access_list = serde_json::from_value::<AccessList>(json).unwrap();
assert_eq!(access_list.len(), 1);
assert_eq!(access_list[0].storage_keys, Vec::<B256>::default());
}

#[test]
fn access_list_serde() {
let list = AccessList(vec![
Expand Down
2 changes: 2 additions & 0 deletions crates/serde/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ use serde::Serializer;
mod bool;
pub use self::bool::*;

pub mod optional;
DaniPopes marked this conversation as resolved.
Show resolved Hide resolved

#[cfg_attr(not(test), deprecated = "use `quantity::{self, opt, vec}` instead")]
pub mod num;
#[allow(deprecated)]
Expand Down
21 changes: 21 additions & 0 deletions crates/serde/src/optional.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//! Serde functions for encoding optional values.
//!
//! This is defined as a "hex encoded unsigned integer", with a special case of 0 being `0x0`.
//!
//! A regex for this format is: `^0x([1-9a-f]+[0-9a-f]*|0)$`.
//!
//! This is only valid for human-readable [`serde`] implementations.
//! For non-human-readable implementations, the format is unspecified.
//! Currently, it uses a fixed-width big-endian byte-array.
mattsse marked this conversation as resolved.
Show resolved Hide resolved
use serde::{Deserialize, Deserializer};

/// For use with serde's `deserialize_with` on a sequence that must be
/// deserialized as a single but optional (i.e. possibly `null`) value.
pub fn or_default<'de, T, D>(deserializer: D) -> Result<T, D::Error>
mattsse marked this conversation as resolved.
Show resolved Hide resolved
where
T: Deserialize<'de> + Default,
D: Deserializer<'de>,
{
let s: Option<T> = Deserialize::deserialize(deserializer)?;
Ok(s.unwrap_or_default())
}