Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

fix: deserialization of null storage keys in AccessListItem #955

Merged
Merged
Show file tree
Hide file tree
Changes from all 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::null_as_default"))]
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
3 changes: 3 additions & 0 deletions crates/serde/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ use serde::Serializer;
mod bool;
pub use self::bool::*;

mod optional;
pub use self::optional::*;

#[cfg_attr(not(test), deprecated = "use `quantity::{self, opt, vec}` instead")]
pub mod num;
#[allow(deprecated)]
Expand Down
13 changes: 13 additions & 0 deletions crates/serde/src/optional.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//! Serde functions for encoding optional values.
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 null_as_default<'de, T, D>(deserializer: D) -> Result<T, D::Error>
where
T: Deserialize<'de> + Default,
D: Deserializer<'de>,
{
let s: Option<T> = Deserialize::deserialize(deserializer)?;
Ok(s.unwrap_or_default())
}