diff --git a/crates/trie/trie/src/updates.rs b/crates/trie/trie/src/updates.rs index d3614895e..c499d7eef 100644 --- a/crates/trie/trie/src/updates.rs +++ b/crates/trie/trie/src/updates.rs @@ -10,6 +10,7 @@ use std::collections::{HashMap, HashSet}; pub struct TrieUpdates { #[cfg_attr(feature = "serde", serde(serialize_with = "serialize_nibbles_map"))] pub(crate) account_nodes: HashMap, + #[cfg_attr(feature = "serde", serde(serialize_with = "serialize_nibbles_set"))] pub(crate) removed_nodes: HashSet, pub(crate) storage_tries: HashMap, } @@ -119,6 +120,7 @@ pub struct StorageTrieUpdates { #[cfg_attr(feature = "serde", serde(serialize_with = "serialize_nibbles_map"))] pub(crate) storage_nodes: HashMap, /// Collection of removed storage trie nodes. + #[cfg_attr(feature = "serde", serde(serialize_with = "serialize_nibbles_set"))] pub(crate) removed_nodes: HashSet, } @@ -220,6 +222,21 @@ impl StorageTrieUpdates { } } +/// Serializes any [`HashSet`] that includes [`Nibbles`] elements, by using the hex-encoded packed +/// representation. +/// +/// This also sorts the set before serializing. +#[cfg(feature = "serde")] +fn serialize_nibbles_set(map: &HashSet, serializer: S) -> Result +where + S: Serializer, +{ + let mut storage_nodes = + Vec::from_iter(map.iter().map(|elem| reth_primitives::hex::encode(elem.pack()))); + storage_nodes.sort_unstable(); + storage_nodes.serialize(serializer) +} + /// Serializes any [`HashMap`] that uses [`Nibbles`] as keys, by using the hex-encoded packed /// representation. ///