Skip to content

Commit

Permalink
fix: deserialization of null storage keys in AccessListItem (#955)
Browse files Browse the repository at this point in the history
* fix: deserialization of null storage keys in AccessListItem

* test: add test

* misc: apply review suggestions

* Update crates/eips/src/eip2930.rs

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>

* misc: apply review suggestion

---------

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
  • Loading branch information
Wodann and mattsse authored Jun 21, 2024
1 parent 8aa5482 commit 194f266
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
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())
}

0 comments on commit 194f266

Please sign in to comment.