Skip to main content

reth_trie_common/
proofs.rs

1//! Merkle trie proofs.
2
3use crate::{BranchNodeMasks, BranchNodeMasksMap, Nibbles, ProofTrieNodeV2, TrieAccount};
4use alloc::{borrow::Cow, collections::VecDeque, vec::Vec};
5use alloy_consensus::constants::KECCAK_EMPTY;
6use alloy_primitives::{
7    keccak256,
8    map::{hash_map, B256Map, B256Set},
9    Address, Bytes, B256, U256,
10};
11use alloy_rlp::{encode_fixed_size, Decodable, EMPTY_STRING_CODE};
12use alloy_trie::{
13    nodes::TrieNode,
14    proof::{verify_proof, DecodedProofNodes, ProofNodes, ProofVerificationError},
15    EMPTY_ROOT_HASH,
16};
17use derive_more::{Deref, DerefMut, IntoIterator};
18use itertools::Itertools;
19use reth_primitives_traits::Account;
20
21/// Proof targets map.
22#[derive(Deref, DerefMut, IntoIterator, Clone, PartialEq, Eq, Default, Debug)]
23pub struct MultiProofTargets(B256Map<B256Set>);
24
25impl FromIterator<(B256, B256Set)> for MultiProofTargets {
26    fn from_iter<T: IntoIterator<Item = (B256, B256Set)>>(iter: T) -> Self {
27        Self(B256Map::from_iter(iter))
28    }
29}
30
31impl MultiProofTargets {
32    /// Creates an empty `MultiProofTargets` with at least the specified capacity.
33    pub fn with_capacity(capacity: usize) -> Self {
34        Self(B256Map::with_capacity_and_hasher(capacity, Default::default()))
35    }
36
37    /// Create `MultiProofTargets` with a single account as a target.
38    pub fn account(hashed_address: B256) -> Self {
39        Self::accounts([hashed_address])
40    }
41
42    /// Create `MultiProofTargets` with a single account and slots as targets.
43    pub fn account_with_slots<I: IntoIterator<Item = B256>>(
44        hashed_address: B256,
45        slots_iter: I,
46    ) -> Self {
47        Self(B256Map::from_iter([(hashed_address, slots_iter.into_iter().collect())]))
48    }
49
50    /// Create `MultiProofTargets` only from accounts.
51    pub fn accounts<I: IntoIterator<Item = B256>>(iter: I) -> Self {
52        Self(iter.into_iter().map(|hashed_address| (hashed_address, Default::default())).collect())
53    }
54
55    /// Retains the targets representing the difference,
56    /// i.e., the values that are in `self` but not in `other`.
57    pub fn retain_difference(&mut self, other: &Self) {
58        self.0.retain(|hashed_address, hashed_slots| {
59            if let Some(other_hashed_slots) = other.get(hashed_address) {
60                hashed_slots.retain(|hashed_slot| !other_hashed_slots.contains(hashed_slot));
61                !hashed_slots.is_empty()
62            } else {
63                true
64            }
65        });
66    }
67
68    /// Extend multi proof targets with contents of other.
69    pub fn extend(&mut self, other: Self) {
70        self.extend_inner(Cow::Owned(other));
71    }
72
73    /// Extend multi proof targets with contents of other.
74    ///
75    /// Slightly less efficient than [`Self::extend`], but preferred to `extend(other.clone())`.
76    pub fn extend_ref(&mut self, other: &Self) {
77        self.extend_inner(Cow::Borrowed(other));
78    }
79
80    fn extend_inner(&mut self, other: Cow<'_, Self>) {
81        for (hashed_address, hashed_slots) in other.iter() {
82            match self.entry(*hashed_address) {
83                hash_map::Entry::Vacant(entry) => {
84                    entry.insert(hashed_slots.clone());
85                }
86                hash_map::Entry::Occupied(mut entry) => {
87                    entry.get_mut().extend(hashed_slots);
88                }
89            }
90        }
91    }
92
93    /// Returns an iterator that yields chunks of the specified size.
94    ///
95    /// See [`ChunkedMultiProofTargets`] for more information.
96    pub fn chunks(self, size: usize) -> ChunkedMultiProofTargets {
97        ChunkedMultiProofTargets::new(self, size)
98    }
99
100    /// Returns the number of items that will be considered during chunking in `[Self::chunks]`.
101    pub fn chunking_length(&self) -> usize {
102        self.values().map(|slots| 1 + slots.len().saturating_sub(1)).sum::<usize>()
103    }
104}
105
106/// An iterator that yields chunks of the proof targets of at most `size` account and storage
107/// targets.
108///
109/// For example, for the following proof targets:
110/// ```text
111/// - 0x1: [0x10, 0x20, 0x30]
112/// - 0x2: [0x40]
113/// - 0x3: []
114/// ```
115///
116/// and `size = 2`, the iterator will yield the following chunks:
117/// ```text
118/// - { 0x1: [0x10, 0x20] }
119/// - { 0x1: [0x30], 0x2: [0x40] }
120/// - { 0x3: [] }
121/// ```
122///
123/// It follows two rules:
124/// - If account has associated storage slots, each storage slot is counted towards the chunk size.
125/// - If account has no associated storage slots, the account is counted towards the chunk size.
126#[derive(Debug)]
127pub struct ChunkedMultiProofTargets {
128    flattened_targets: alloc::vec::IntoIter<(B256, Option<B256>)>,
129    size: usize,
130}
131
132impl ChunkedMultiProofTargets {
133    fn new(targets: MultiProofTargets, size: usize) -> Self {
134        let flattened_targets = targets
135            .into_iter()
136            .flat_map(|(address, slots)| {
137                if slots.is_empty() {
138                    // If the account has no storage slots, we still need to yield the account
139                    // address with empty storage slots. `None` here means that
140                    // there's no storage slot to fetch.
141                    itertools::Either::Left(core::iter::once((address, None)))
142                } else {
143                    itertools::Either::Right(
144                        slots.into_iter().map(move |slot| (address, Some(slot))),
145                    )
146                }
147            })
148            .sorted_unstable();
149        Self { flattened_targets, size }
150    }
151}
152
153impl Iterator for ChunkedMultiProofTargets {
154    type Item = MultiProofTargets;
155
156    fn next(&mut self) -> Option<Self::Item> {
157        let chunk = self.flattened_targets.by_ref().take(self.size).fold(
158            MultiProofTargets::default(),
159            |mut acc, (address, slot)| {
160                let entry = acc.entry(address).or_default();
161                if let Some(slot) = slot {
162                    entry.insert(slot);
163                }
164                acc
165            },
166        );
167
168        if chunk.is_empty() {
169            None
170        } else {
171            Some(chunk)
172        }
173    }
174}
175
176/// The state multiproof of target accounts and multiproofs of their storage tries.
177/// Multiproof is effectively a state subtrie that only contains the nodes
178/// in the paths of target accounts.
179#[derive(Clone, Default, Debug, PartialEq, Eq)]
180pub struct MultiProof {
181    /// State trie multiproof for requested accounts.
182    pub account_subtree: ProofNodes,
183    /// Consolidated branch node masks (`hash_mask`, `tree_mask`) for each path in the account
184    /// proof.
185    pub branch_node_masks: BranchNodeMasksMap,
186    /// Storage trie multiproofs.
187    pub storages: B256Map<StorageMultiProof>,
188}
189
190impl MultiProof {
191    /// Returns true if the multiproof is empty.
192    pub fn is_empty(&self) -> bool {
193        self.account_subtree.is_empty() &&
194            self.branch_node_masks.is_empty() &&
195            self.storages.is_empty()
196    }
197
198    /// Return the account proof nodes for the given account path.
199    pub fn account_proof_nodes(&self, path: &Nibbles) -> Vec<(Nibbles, Bytes)> {
200        self.account_subtree.matching_nodes_sorted(path)
201    }
202
203    /// Return the storage proof nodes for the given storage slots of the account path.
204    pub fn storage_proof_nodes(
205        &self,
206        hashed_address: B256,
207        slots: impl IntoIterator<Item = B256>,
208    ) -> Vec<(B256, Vec<(Nibbles, Bytes)>)> {
209        self.storages
210            .get(&hashed_address)
211            .map(|storage_mp| {
212                slots
213                    .into_iter()
214                    .map(|slot| {
215                        let nibbles = Nibbles::unpack(slot);
216                        (slot, storage_mp.subtree.matching_nodes_sorted(&nibbles))
217                    })
218                    .collect()
219            })
220            .unwrap_or_default()
221    }
222
223    /// Construct the account proof from the multiproof.
224    pub fn account_proof(
225        &self,
226        address: Address,
227        slots: &[B256],
228    ) -> Result<AccountProof, alloy_rlp::Error> {
229        let hashed_address = keccak256(address);
230        let nibbles = Nibbles::unpack(hashed_address);
231
232        // Retrieve the account proof.
233        let proof = self
234            .account_proof_nodes(&nibbles)
235            .into_iter()
236            .map(|(_, node)| node)
237            .collect::<Vec<_>>();
238
239        // Inspect the last node in the proof. If it's a leaf node with matching suffix,
240        // then the node contains the encoded trie account.
241        let info = 'info: {
242            if let Some(last) = proof.last() &&
243                let TrieNode::Leaf(leaf) = TrieNode::decode(&mut &last[..])? &&
244                nibbles.ends_with(&leaf.key)
245            {
246                let account = TrieAccount::decode(&mut &leaf.value[..])?;
247                break 'info Some(Account {
248                    balance: account.balance,
249                    nonce: account.nonce,
250                    bytecode_hash: (account.code_hash != KECCAK_EMPTY).then_some(account.code_hash),
251                })
252            }
253            None
254        };
255
256        // Retrieve proofs for requested storage slots.
257        let storage_multiproof = self.storages.get(&hashed_address);
258        let storage_root = storage_multiproof.map(|m| m.root).unwrap_or(EMPTY_ROOT_HASH);
259        let mut storage_proofs = Vec::with_capacity(slots.len());
260        for slot in slots {
261            let proof = if let Some(multiproof) = &storage_multiproof {
262                multiproof.storage_proof(*slot)?
263            } else {
264                StorageProof::new(*slot)
265            };
266            storage_proofs.push(proof);
267        }
268        Ok(AccountProof { address, info, proof, storage_root, storage_proofs })
269    }
270
271    /// Extends this multiproof with another one, merging both account and storage
272    /// proofs.
273    pub fn extend(&mut self, other: Self) {
274        self.account_subtree.extend_from(other.account_subtree);
275        self.branch_node_masks.extend(other.branch_node_masks);
276
277        let reserve = if self.storages.is_empty() {
278            other.storages.len()
279        } else {
280            other.storages.len().div_ceil(2)
281        };
282        self.storages.reserve(reserve);
283        for (hashed_address, storage) in other.storages {
284            match self.storages.entry(hashed_address) {
285                hash_map::Entry::Occupied(mut entry) => {
286                    debug_assert_eq!(entry.get().root, storage.root);
287                    let entry = entry.get_mut();
288                    entry.subtree.extend_from(storage.subtree);
289                    entry.branch_node_masks.extend(storage.branch_node_masks);
290                }
291                hash_map::Entry::Vacant(entry) => {
292                    entry.insert(storage);
293                }
294            }
295        }
296    }
297
298    /// Create a [`MultiProof`] from a [`StorageMultiProof`].
299    pub fn from_storage_proof(hashed_address: B256, storage_proof: StorageMultiProof) -> Self {
300        Self {
301            storages: B256Map::from_iter([(hashed_address, storage_proof)]),
302            ..Default::default()
303        }
304    }
305}
306
307/// This is a type of [`MultiProof`] that uses decoded proofs, meaning these proofs are stored as a
308/// collection of [`TrieNode`]s instead of RLP-encoded bytes.
309#[derive(Clone, Default, Debug, PartialEq, Eq)]
310pub struct DecodedMultiProof {
311    /// State trie multiproof for requested accounts.
312    pub account_subtree: DecodedProofNodes,
313    /// Consolidated branch node masks (`hash_mask`, `tree_mask`) for each path in the account
314    /// proof.
315    pub branch_node_masks: BranchNodeMasksMap,
316    /// Storage trie multiproofs.
317    pub storages: B256Map<DecodedStorageMultiProof>,
318}
319
320impl DecodedMultiProof {
321    /// Returns true if the multiproof is empty.
322    pub fn is_empty(&self) -> bool {
323        self.account_subtree.is_empty() &&
324            self.branch_node_masks.is_empty() &&
325            self.storages.is_empty()
326    }
327
328    /// Return the account proof nodes for the given account path.
329    pub fn account_proof_nodes(&self, path: &Nibbles) -> Vec<(Nibbles, TrieNode)> {
330        self.account_subtree.matching_nodes_sorted(path)
331    }
332
333    /// Return the storage proof nodes for the given storage slots of the account path.
334    pub fn storage_proof_nodes(
335        &self,
336        hashed_address: B256,
337        slots: impl IntoIterator<Item = B256>,
338    ) -> Vec<(B256, Vec<(Nibbles, TrieNode)>)> {
339        self.storages
340            .get(&hashed_address)
341            .map(|storage_mp| {
342                slots
343                    .into_iter()
344                    .map(|slot| {
345                        let nibbles = Nibbles::unpack(slot);
346                        (slot, storage_mp.subtree.matching_nodes_sorted(&nibbles))
347                    })
348                    .collect()
349            })
350            .unwrap_or_default()
351    }
352
353    /// Construct the account proof from the multiproof.
354    pub fn account_proof(
355        &self,
356        address: Address,
357        slots: &[B256],
358    ) -> Result<DecodedAccountProof, alloy_rlp::Error> {
359        let hashed_address = keccak256(address);
360        let nibbles = Nibbles::unpack(hashed_address);
361
362        // Retrieve the account proof.
363        let proof = self
364            .account_proof_nodes(&nibbles)
365            .into_iter()
366            .map(|(_, node)| node)
367            .collect::<Vec<_>>();
368
369        // Inspect the last node in the proof. If it's a leaf node with matching suffix,
370        // then the node contains the encoded trie account.
371        let info = 'info: {
372            if let Some(TrieNode::Leaf(leaf)) = proof.last() &&
373                nibbles.ends_with(&leaf.key)
374            {
375                let account = TrieAccount::decode(&mut &leaf.value[..])?;
376                break 'info Some(Account {
377                    balance: account.balance,
378                    nonce: account.nonce,
379                    bytecode_hash: (account.code_hash != KECCAK_EMPTY).then_some(account.code_hash),
380                })
381            }
382            None
383        };
384
385        // Retrieve proofs for requested storage slots.
386        let storage_multiproof = self.storages.get(&hashed_address);
387        let storage_root = storage_multiproof.map(|m| m.root).unwrap_or(EMPTY_ROOT_HASH);
388        let mut storage_proofs = Vec::with_capacity(slots.len());
389        for slot in slots {
390            let proof = if let Some(multiproof) = &storage_multiproof {
391                multiproof.storage_proof(*slot)?
392            } else {
393                DecodedStorageProof::new(*slot)
394            };
395            storage_proofs.push(proof);
396        }
397        Ok(DecodedAccountProof { address, info, proof, storage_root, storage_proofs })
398    }
399
400    /// Extends this multiproof with another one, merging both account and storage
401    /// proofs.
402    pub fn extend(&mut self, other: Self) {
403        self.account_subtree.extend_from(other.account_subtree);
404        self.branch_node_masks.extend(other.branch_node_masks);
405
406        let reserve = if self.storages.is_empty() {
407            other.storages.len()
408        } else {
409            other.storages.len().div_ceil(2)
410        };
411        self.storages.reserve(reserve);
412        for (hashed_address, storage) in other.storages {
413            match self.storages.entry(hashed_address) {
414                hash_map::Entry::Occupied(mut entry) => {
415                    debug_assert_eq!(entry.get().root, storage.root);
416                    let entry = entry.get_mut();
417                    entry.subtree.extend_from(storage.subtree);
418                    entry.branch_node_masks.extend(storage.branch_node_masks);
419                }
420                hash_map::Entry::Vacant(entry) => {
421                    entry.insert(storage);
422                }
423            }
424        }
425    }
426
427    /// Create a [`DecodedMultiProof`] from a [`DecodedStorageMultiProof`].
428    pub fn from_storage_proof(
429        hashed_address: B256,
430        storage_proof: DecodedStorageMultiProof,
431    ) -> Self {
432        Self {
433            storages: B256Map::from_iter([(hashed_address, storage_proof)]),
434            ..Default::default()
435        }
436    }
437}
438
439impl TryFrom<MultiProof> for DecodedMultiProof {
440    type Error = alloy_rlp::Error;
441
442    fn try_from(multi_proof: MultiProof) -> Result<Self, Self::Error> {
443        let account_subtree = DecodedProofNodes::try_from(multi_proof.account_subtree)?;
444        let storages = multi_proof
445            .storages
446            .into_iter()
447            .map(|(address, storage)| Ok((address, storage.try_into()?)))
448            .collect::<Result<B256Map<_>, alloy_rlp::Error>>()?;
449        Ok(Self { account_subtree, branch_node_masks: multi_proof.branch_node_masks, storages })
450    }
451}
452
453/// V2 decoded multiproof which contains the results of both account and storage V2 proof
454/// calculations.
455#[derive(Clone, Debug, PartialEq, Eq, Default)]
456pub struct DecodedMultiProofV2 {
457    /// Account trie proof nodes
458    pub account_proofs: Vec<ProofTrieNodeV2>,
459    /// Storage trie proof nodes indexed by account
460    pub storage_proofs: B256Map<Vec<ProofTrieNodeV2>>,
461}
462
463impl DecodedMultiProofV2 {
464    /// Returns true if there are no proofs
465    pub fn is_empty(&self) -> bool {
466        self.account_proofs.is_empty() && self.storage_proofs.is_empty()
467    }
468
469    /// Builds a `DecodedMultiProofV2` from a flat witness map (hash → RLP-encoded trie node).
470    ///
471    /// This performs a BFS traversal starting from `state_root`, decoding each witness entry
472    /// as a trie node and organizing them into account and storage proof vectors. This is the
473    /// inverse of witness generation — it reconstructs the structured multiproof from the flat
474    /// format used in `ExecutionWitness`.
475    pub fn from_witness(
476        state_root: B256,
477        witness: &B256Map<impl AsRef<[u8]>>,
478    ) -> Result<Self, alloy_rlp::Error> {
479        let mut account_nodes: Vec<(Nibbles, TrieNode, Option<BranchNodeMasks>)> = Vec::new();
480        let mut storage_nodes: B256Map<Vec<(Nibbles, TrieNode, Option<BranchNodeMasks>)>> =
481            B256Map::default();
482
483        let mut queue: VecDeque<(B256, Nibbles, Option<B256>)> =
484            VecDeque::from([(state_root, Nibbles::default(), None)]);
485
486        while let Some((hash, path, maybe_account)) = queue.pop_front() {
487            let Some(rlp_bytes) = witness.get(&hash) else { continue };
488            let trie_node = TrieNode::decode(&mut rlp_bytes.as_ref())?;
489
490            match &trie_node {
491                TrieNode::Branch(branch) => {
492                    for (idx, maybe_child) in branch.as_ref().children() {
493                        if let Some(child_hash) =
494                            maybe_child.and_then(alloy_trie::nodes::RlpNode::as_hash)
495                        {
496                            let mut child_path = path;
497                            child_path.push_unchecked(idx);
498                            queue.push_back((child_hash, child_path, maybe_account));
499                        }
500                    }
501                }
502                TrieNode::Extension(ext) => {
503                    if let Some(child_hash) = ext.child.as_hash() {
504                        let mut child_path = path;
505                        child_path.extend(&ext.key);
506                        queue.push_back((child_hash, child_path, maybe_account));
507                    }
508                }
509                TrieNode::Leaf(leaf) => {
510                    if maybe_account.is_none() {
511                        let mut full_path = path;
512                        full_path.extend(&leaf.key);
513                        let hashed_address = B256::from_slice(&full_path.pack());
514                        let account = TrieAccount::decode(&mut &leaf.value[..])?;
515                        if account.storage_root != EMPTY_ROOT_HASH {
516                            queue.push_back((
517                                account.storage_root,
518                                Nibbles::default(),
519                                Some(hashed_address),
520                            ));
521                        }
522                    }
523                }
524                TrieNode::EmptyRoot => {}
525            }
526
527            if let Some(account) = maybe_account {
528                storage_nodes.entry(account).or_default().push((path, trie_node, None));
529            } else {
530                account_nodes.push((path, trie_node, None));
531            }
532        }
533
534        account_nodes.sort_by(|(a, _, _), (b, _, _)| b.cmp(a));
535        let account_proofs = ProofTrieNodeV2::from_sorted_trie_nodes(account_nodes);
536
537        let mut storage_proofs = B256Map::default();
538        for (account, mut nodes) in storage_nodes {
539            nodes.sort_by(|(a, _, _), (b, _, _)| b.cmp(a));
540            storage_proofs.insert(account, ProofTrieNodeV2::from_sorted_trie_nodes(nodes));
541        }
542
543        Ok(Self { account_proofs, storage_proofs })
544    }
545
546    /// Appends the given multiproof's data to this one.
547    ///
548    /// This implementation does not deduplicate redundant proofs.
549    pub fn extend(&mut self, other: Self) {
550        self.account_proofs.extend(other.account_proofs);
551        for (hashed_address, other_storage_proofs) in other.storage_proofs {
552            match self.storage_proofs.entry(hashed_address) {
553                hash_map::Entry::Vacant(entry) => {
554                    entry.insert(other_storage_proofs);
555                }
556                hash_map::Entry::Occupied(mut entry) => {
557                    entry.get_mut().extend(other_storage_proofs);
558                }
559            }
560        }
561    }
562}
563
564impl From<DecodedMultiProof> for DecodedMultiProofV2 {
565    fn from(proof: DecodedMultiProof) -> Self {
566        let account_proofs =
567            decoded_proof_nodes_to_v2(proof.account_subtree, &proof.branch_node_masks);
568        let storage_proofs = proof
569            .storages
570            .into_iter()
571            .map(|(address, storage)| {
572                (address, decoded_proof_nodes_to_v2(storage.subtree, &storage.branch_node_masks))
573            })
574            .collect();
575        Self { account_proofs, storage_proofs }
576    }
577}
578
579/// Converts a [`DecodedProofNodes`] (path → [`TrieNode`] map) into a `Vec<ProofTrieNodeV2>`,
580/// merging extension nodes into their child branch nodes.
581fn decoded_proof_nodes_to_v2(
582    nodes: DecodedProofNodes,
583    masks: &BranchNodeMasksMap,
584) -> Vec<ProofTrieNodeV2> {
585    let mut sorted: Vec<_> = nodes.into_inner().into_iter().collect();
586    sorted.sort_unstable_by(|a, b| crate::depth_first_cmp(&a.0, &b.0));
587    ProofTrieNodeV2::from_sorted_trie_nodes(
588        sorted.into_iter().map(|(path, node)| (path, node, masks.get(&path).copied())),
589    )
590}
591
592/// The merkle multiproof of storage trie.
593#[derive(Clone, Debug, PartialEq, Eq)]
594pub struct StorageMultiProof {
595    /// Storage trie root.
596    pub root: B256,
597    /// Storage multiproof for requested slots.
598    pub subtree: ProofNodes,
599    /// Consolidated branch node masks (`hash_mask`, `tree_mask`) for each path in the storage
600    /// proof.
601    pub branch_node_masks: BranchNodeMasksMap,
602}
603
604impl StorageMultiProof {
605    /// Create new storage multiproof for empty trie.
606    pub fn empty() -> Self {
607        Self {
608            root: EMPTY_ROOT_HASH,
609            subtree: ProofNodes::from_iter([(
610                Nibbles::default(),
611                Bytes::from([EMPTY_STRING_CODE]),
612            )]),
613            branch_node_masks: BranchNodeMasksMap::default(),
614        }
615    }
616
617    /// Return storage proofs for the target storage slot (unhashed).
618    pub fn storage_proof(&self, slot: B256) -> Result<StorageProof, alloy_rlp::Error> {
619        let nibbles = Nibbles::unpack(keccak256(slot));
620
621        // Retrieve the storage proof.
622        let proof = self
623            .subtree
624            .matching_nodes_iter(&nibbles)
625            .sorted_by(|a, b| a.0.cmp(b.0))
626            .map(|(_, node)| node.clone())
627            .collect::<Vec<_>>();
628
629        // Inspect the last node in the proof. If it's a leaf node with matching suffix,
630        // then the node contains the encoded slot value.
631        let value = 'value: {
632            if let Some(last) = proof.last() &&
633                let TrieNode::Leaf(leaf) = TrieNode::decode(&mut &last[..])? &&
634                nibbles.ends_with(&leaf.key)
635            {
636                break 'value U256::decode(&mut &leaf.value[..])?
637            }
638            U256::ZERO
639        };
640
641        Ok(StorageProof { key: slot, nibbles, value, proof })
642    }
643}
644
645/// The decoded merkle multiproof for a storage trie.
646#[derive(Clone, Debug, PartialEq, Eq)]
647pub struct DecodedStorageMultiProof {
648    /// Storage trie root.
649    pub root: B256,
650    /// Storage multiproof for requested slots.
651    pub subtree: DecodedProofNodes,
652    /// Consolidated branch node masks (`hash_mask`, `tree_mask`) for each path in the storage
653    /// proof.
654    pub branch_node_masks: BranchNodeMasksMap,
655}
656
657impl DecodedStorageMultiProof {
658    /// Create new storage multiproof for empty trie.
659    pub fn empty() -> Self {
660        Self {
661            root: EMPTY_ROOT_HASH,
662            subtree: DecodedProofNodes::from_iter([(Nibbles::default(), TrieNode::EmptyRoot)]),
663            branch_node_masks: BranchNodeMasksMap::default(),
664        }
665    }
666
667    /// Return storage proofs for the target storage slot (unhashed).
668    pub fn storage_proof(&self, slot: B256) -> Result<DecodedStorageProof, alloy_rlp::Error> {
669        let nibbles = Nibbles::unpack(keccak256(slot));
670
671        // Retrieve the storage proof.
672        let proof = self
673            .subtree
674            .matching_nodes_iter(&nibbles)
675            .sorted_by(|a, b| a.0.cmp(b.0))
676            .map(|(_, node)| node.clone())
677            .collect::<Vec<_>>();
678
679        // Inspect the last node in the proof. If it's a leaf node with matching suffix,
680        // then the node contains the encoded slot value.
681        let value = 'value: {
682            if let Some(TrieNode::Leaf(leaf)) = proof.last() &&
683                nibbles.ends_with(&leaf.key)
684            {
685                break 'value U256::decode(&mut &leaf.value[..])?
686            }
687            U256::ZERO
688        };
689
690        Ok(DecodedStorageProof { key: slot, nibbles, value, proof })
691    }
692}
693
694impl TryFrom<StorageMultiProof> for DecodedStorageMultiProof {
695    type Error = alloy_rlp::Error;
696
697    fn try_from(multi_proof: StorageMultiProof) -> Result<Self, Self::Error> {
698        let subtree = DecodedProofNodes::try_from(multi_proof.subtree)?;
699        Ok(Self {
700            root: multi_proof.root,
701            subtree,
702            branch_node_masks: multi_proof.branch_node_masks,
703        })
704    }
705}
706
707/// The merkle proof with the relevant account info.
708#[derive(Clone, PartialEq, Eq, Debug)]
709#[cfg_attr(any(test, feature = "serde"), derive(serde::Serialize, serde::Deserialize))]
710#[cfg_attr(any(test, feature = "serde"), serde(rename_all = "camelCase"))]
711pub struct AccountProof {
712    /// The address associated with the account.
713    pub address: Address,
714    /// Account info, if any.
715    pub info: Option<Account>,
716    /// Array of rlp-serialized merkle trie nodes which starting from the root node and
717    /// following the path of the hashed address as key.
718    pub proof: Vec<Bytes>,
719    /// The storage trie root.
720    pub storage_root: B256,
721    /// Array of storage proofs as requested.
722    pub storage_proofs: Vec<StorageProof>,
723}
724
725#[cfg(feature = "eip1186")]
726impl AccountProof {
727    /// Convert into an EIP-1186 account proof response.
728    ///
729    /// For non-existent accounts, this returns `KECCAK_EMPTY` for `codeHash` and
730    /// `EMPTY_ROOT_HASH` for `storageHash`, matching reth's default behavior.
731    ///
732    /// Use [`Self::into_eip1186_response_with`] to customize the behavior for
733    /// non-existent accounts (e.g. returning `B256::ZERO` for geth compatibility).
734    pub fn into_eip1186_response(
735        self,
736        slots: Vec<alloy_serde::JsonStorageKey>,
737    ) -> alloy_rpc_types_eth::EIP1186AccountProofResponse {
738        self.into_eip1186_response_with(slots, false)
739    }
740
741    /// Convert into an EIP-1186 account proof response, with optional geth-compatible
742    /// zero hashes for non-existent accounts.
743    ///
744    /// When `zero_empty_account` is `true`, non-existent accounts return `B256::ZERO`
745    /// for both `codeHash` and `storageHash`, matching geth's behavior since v1.13.4
746    /// ([go-ethereum#28357](https://github.com/ethereum/go-ethereum/pull/28357)).
747    ///
748    /// When `false`, returns `KECCAK_EMPTY` / `EMPTY_ROOT_HASH` (reth default).
749    ///
750    /// See: <https://github.com/ethereum/go-ethereum/issues/28441>
751    pub fn into_eip1186_response_with(
752        self,
753        slots: Vec<alloy_serde::JsonStorageKey>,
754        zero_empty_account: bool,
755    ) -> alloy_rpc_types_eth::EIP1186AccountProofResponse {
756        let is_non_existent = self.info.is_none();
757        let info = self.info.unwrap_or_default();
758        let (code_hash, storage_hash) = if is_non_existent && zero_empty_account {
759            (B256::ZERO, B256::ZERO)
760        } else {
761            (info.get_bytecode_hash(), self.storage_root)
762        };
763        alloy_rpc_types_eth::EIP1186AccountProofResponse {
764            address: self.address,
765            balance: info.balance,
766            code_hash,
767            nonce: info.nonce,
768            storage_hash,
769            account_proof: self.proof,
770            storage_proof: self
771                .storage_proofs
772                .into_iter()
773                .filter_map(|proof| {
774                    let input_slot = slots.iter().find(|s| s.as_b256() == proof.key)?;
775                    Some(proof.into_eip1186_proof(*input_slot))
776                })
777                .collect(),
778        }
779    }
780
781    /// Converts an
782    /// [`EIP1186AccountProofResponse`](alloy_rpc_types_eth::EIP1186AccountProofResponse) to an
783    /// [`AccountProof`].
784    ///
785    /// This is the inverse of [`Self::into_eip1186_response`]
786    pub fn from_eip1186_proof(proof: alloy_rpc_types_eth::EIP1186AccountProofResponse) -> Self {
787        let alloy_rpc_types_eth::EIP1186AccountProofResponse {
788            nonce,
789            address,
790            balance,
791            code_hash,
792            storage_hash,
793            account_proof,
794            storage_proof,
795            ..
796        } = proof;
797        let storage_proofs = storage_proof.into_iter().map(Into::into).collect();
798
799        let (storage_root, info) = if nonce == 0 &&
800            balance.is_zero() &&
801            (storage_hash.is_zero() || storage_hash == EMPTY_ROOT_HASH) &&
802            (code_hash == KECCAK_EMPTY || code_hash.is_zero())
803        {
804            // Account does not exist in state. Return `None` here to prevent proof
805            // verification.
806            //
807            // Note: geth (since v1.13.4, go-ethereum#28357) returns `B256::ZERO` for
808            // both `codeHash` and `storageHash` in exclusion proofs, while reth
809            // returns `KECCAK_EMPTY` / `EMPTY_ROOT_HASH`. We accept both formats here
810            // so that proofs obtained from any client can be deserialized correctly.
811            // See: https://github.com/ethereum/go-ethereum/issues/28441
812            (EMPTY_ROOT_HASH, None)
813        } else {
814            (storage_hash, Some(Account { nonce, balance, bytecode_hash: code_hash.into() }))
815        };
816
817        Self { address, info, proof: account_proof, storage_root, storage_proofs }
818    }
819}
820
821#[cfg(feature = "eip1186")]
822impl From<alloy_rpc_types_eth::EIP1186AccountProofResponse> for AccountProof {
823    fn from(proof: alloy_rpc_types_eth::EIP1186AccountProofResponse) -> Self {
824        Self::from_eip1186_proof(proof)
825    }
826}
827
828impl Default for AccountProof {
829    fn default() -> Self {
830        Self::new(Address::default())
831    }
832}
833
834impl AccountProof {
835    /// Create new account proof entity.
836    pub const fn new(address: Address) -> Self {
837        Self {
838            address,
839            info: None,
840            proof: Vec::new(),
841            storage_root: EMPTY_ROOT_HASH,
842            storage_proofs: Vec::new(),
843        }
844    }
845
846    /// Verify the storage proofs and account proof against the provided state root.
847    pub fn verify(&self, root: B256) -> Result<(), ProofVerificationError> {
848        // Verify storage proofs.
849        for storage_proof in &self.storage_proofs {
850            storage_proof.verify(self.storage_root)?;
851        }
852
853        // Verify the account proof.
854        let expected = if self.info.is_none() && self.storage_root == EMPTY_ROOT_HASH {
855            None
856        } else {
857            Some(alloy_rlp::encode(
858                self.info.unwrap_or_default().into_trie_account(self.storage_root),
859            ))
860        };
861        let nibbles = Nibbles::unpack(keccak256(self.address));
862        verify_proof(root, nibbles, expected, &self.proof)
863    }
864}
865
866/// The merkle proof with the relevant account info.
867#[derive(Clone, PartialEq, Eq, Debug)]
868pub struct DecodedAccountProof {
869    /// The address associated with the account.
870    pub address: Address,
871    /// Account info.
872    pub info: Option<Account>,
873    /// Array of merkle trie nodes which starting from the root node and following the path of the
874    /// hashed address as key.
875    pub proof: Vec<TrieNode>,
876    /// The storage trie root.
877    pub storage_root: B256,
878    /// Array of storage proofs as requested.
879    pub storage_proofs: Vec<DecodedStorageProof>,
880}
881
882impl Default for DecodedAccountProof {
883    fn default() -> Self {
884        Self::new(Address::default())
885    }
886}
887
888impl DecodedAccountProof {
889    /// Create new account proof entity.
890    pub const fn new(address: Address) -> Self {
891        Self {
892            address,
893            info: None,
894            proof: Vec::new(),
895            storage_root: EMPTY_ROOT_HASH,
896            storage_proofs: Vec::new(),
897        }
898    }
899}
900
901/// The merkle proof of the storage entry.
902#[derive(Clone, PartialEq, Eq, Default, Debug)]
903#[cfg_attr(any(test, feature = "serde"), derive(serde::Serialize, serde::Deserialize))]
904pub struct StorageProof {
905    /// The raw storage key.
906    pub key: B256,
907    /// The hashed storage key nibbles.
908    pub nibbles: Nibbles,
909    /// The storage value.
910    pub value: U256,
911    /// Array of rlp-serialized merkle trie nodes which starting from the storage root node and
912    /// following the path of the hashed storage slot as key.
913    pub proof: Vec<Bytes>,
914}
915
916impl StorageProof {
917    /// Create new storage proof from the storage slot.
918    pub fn new(key: B256) -> Self {
919        let nibbles = Nibbles::unpack(keccak256(key));
920        Self { key, nibbles, ..Default::default() }
921    }
922
923    /// Create new storage proof from the storage slot and its pre-hashed image.
924    pub fn new_with_hashed(key: B256, hashed_key: B256) -> Self {
925        Self { key, nibbles: Nibbles::unpack(hashed_key), ..Default::default() }
926    }
927
928    /// Create new storage proof from the storage slot and its pre-hashed image.
929    pub fn new_with_nibbles(key: B256, nibbles: Nibbles) -> Self {
930        Self { key, nibbles, ..Default::default() }
931    }
932
933    /// Set proof nodes on storage proof.
934    pub fn with_proof(mut self, proof: Vec<Bytes>) -> Self {
935        self.proof = proof;
936        self
937    }
938
939    /// Verify the proof against the provided storage root.
940    pub fn verify(&self, root: B256) -> Result<(), ProofVerificationError> {
941        let expected =
942            if self.value.is_zero() { None } else { Some(encode_fixed_size(&self.value).to_vec()) };
943        verify_proof(root, self.nibbles, expected, &self.proof)
944    }
945}
946
947#[cfg(feature = "eip1186")]
948impl StorageProof {
949    /// Convert into an EIP-1186 storage proof
950    pub fn into_eip1186_proof(
951        self,
952        slot: alloy_serde::JsonStorageKey,
953    ) -> alloy_rpc_types_eth::EIP1186StorageProof {
954        alloy_rpc_types_eth::EIP1186StorageProof { key: slot, value: self.value, proof: self.proof }
955    }
956
957    /// Convert from an
958    /// [`EIP1186StorageProof`](alloy_rpc_types_eth::EIP1186StorageProof)
959    ///
960    /// This is the inverse of [`Self::into_eip1186_proof`].
961    pub fn from_eip1186_proof(storage_proof: alloy_rpc_types_eth::EIP1186StorageProof) -> Self {
962        Self {
963            value: storage_proof.value,
964            proof: storage_proof.proof,
965            ..Self::new(storage_proof.key.as_b256())
966        }
967    }
968}
969
970#[cfg(feature = "eip1186")]
971impl From<alloy_rpc_types_eth::EIP1186StorageProof> for StorageProof {
972    fn from(proof: alloy_rpc_types_eth::EIP1186StorageProof) -> Self {
973        Self::from_eip1186_proof(proof)
974    }
975}
976
977/// The merkle proof of the storage entry, using decoded proofs.
978#[derive(Clone, PartialEq, Eq, Default, Debug)]
979pub struct DecodedStorageProof {
980    /// The raw storage key.
981    pub key: B256,
982    /// The hashed storage key nibbles.
983    pub nibbles: Nibbles,
984    /// The storage value.
985    pub value: U256,
986    /// Array of merkle trie nodes which starting from the storage root node and following the path
987    /// of the hashed storage slot as key.
988    pub proof: Vec<TrieNode>,
989}
990
991impl DecodedStorageProof {
992    /// Create new storage proof from the storage slot.
993    pub fn new(key: B256) -> Self {
994        let nibbles = Nibbles::unpack(keccak256(key));
995        Self { key, nibbles, ..Default::default() }
996    }
997
998    /// Create new storage proof from the storage slot and its pre-hashed image.
999    pub fn new_with_hashed(key: B256, hashed_key: B256) -> Self {
1000        Self { key, nibbles: Nibbles::unpack(hashed_key), ..Default::default() }
1001    }
1002
1003    /// Create new storage proof from the storage slot and its pre-hashed image.
1004    pub fn new_with_nibbles(key: B256, nibbles: Nibbles) -> Self {
1005        Self { key, nibbles, ..Default::default() }
1006    }
1007
1008    /// Set proof nodes on storage proof.
1009    pub fn with_proof(mut self, proof: Vec<TrieNode>) -> Self {
1010        self.proof = proof;
1011        self
1012    }
1013}
1014
1015/// Implementation of hasher using our keccak256 hashing function
1016/// for compatibility with `triehash` crate.
1017#[cfg(any(test, feature = "test-utils"))]
1018pub mod triehash {
1019    use alloy_primitives::{keccak256, B256};
1020    use alloy_rlp::RlpEncodable;
1021    use hash_db::Hasher;
1022    use plain_hasher::PlainHasher;
1023
1024    /// A [Hasher] that calculates a keccak256 hash of the given data.
1025    #[derive(Default, Debug, Clone, PartialEq, Eq, RlpEncodable)]
1026    #[non_exhaustive]
1027    pub struct KeccakHasher;
1028
1029    #[cfg(any(test, feature = "test-utils"))]
1030    impl Hasher for KeccakHasher {
1031        type Out = B256;
1032        type StdHasher = PlainHasher;
1033
1034        const LENGTH: usize = 32;
1035
1036        fn hash(x: &[u8]) -> Self::Out {
1037            keccak256(x)
1038        }
1039    }
1040}
1041
1042#[cfg(test)]
1043mod tests {
1044    use super::*;
1045
1046    #[test]
1047    fn test_multiproof_extend_account_proofs() {
1048        let mut proof1 = MultiProof::default();
1049        let mut proof2 = MultiProof::default();
1050
1051        let addr1 = B256::random();
1052        let addr2 = B256::random();
1053
1054        proof1.account_subtree.insert(
1055            Nibbles::unpack(addr1),
1056            alloy_rlp::encode_fixed_size(&U256::from(42)).to_vec().into(),
1057        );
1058        proof2.account_subtree.insert(
1059            Nibbles::unpack(addr2),
1060            alloy_rlp::encode_fixed_size(&U256::from(43)).to_vec().into(),
1061        );
1062
1063        proof1.extend(proof2);
1064
1065        assert!(proof1.account_subtree.contains_key(&Nibbles::unpack(addr1)));
1066        assert!(proof1.account_subtree.contains_key(&Nibbles::unpack(addr2)));
1067    }
1068
1069    #[test]
1070    fn test_multiproof_extend_storage_proofs() {
1071        let mut proof1 = MultiProof::default();
1072        let mut proof2 = MultiProof::default();
1073
1074        let addr = B256::random();
1075        let root = B256::random();
1076
1077        let mut subtree1 = ProofNodes::default();
1078        subtree1.insert(
1079            Nibbles::from_nibbles(vec![0]),
1080            alloy_rlp::encode_fixed_size(&U256::from(42)).to_vec().into(),
1081        );
1082        proof1.storages.insert(
1083            addr,
1084            StorageMultiProof {
1085                root,
1086                subtree: subtree1,
1087                branch_node_masks: BranchNodeMasksMap::default(),
1088            },
1089        );
1090
1091        let mut subtree2 = ProofNodes::default();
1092        subtree2.insert(
1093            Nibbles::from_nibbles(vec![1]),
1094            alloy_rlp::encode_fixed_size(&U256::from(43)).to_vec().into(),
1095        );
1096        proof2.storages.insert(
1097            addr,
1098            StorageMultiProof {
1099                root,
1100                subtree: subtree2,
1101                branch_node_masks: BranchNodeMasksMap::default(),
1102            },
1103        );
1104
1105        proof1.extend(proof2);
1106
1107        let storage = proof1.storages.get(&addr).unwrap();
1108        assert_eq!(storage.root, root);
1109        assert!(storage.subtree.contains_key(&Nibbles::from_nibbles(vec![0])));
1110        assert!(storage.subtree.contains_key(&Nibbles::from_nibbles(vec![1])));
1111    }
1112
1113    #[test]
1114    fn test_multi_proof_retain_difference() {
1115        let mut empty = MultiProofTargets::default();
1116        empty.retain_difference(&Default::default());
1117        assert!(empty.is_empty());
1118
1119        let targets = MultiProofTargets::accounts((0..10).map(B256::with_last_byte));
1120
1121        let mut diffed = targets.clone();
1122        diffed.retain_difference(&MultiProofTargets::account(B256::with_last_byte(11)));
1123        assert_eq!(diffed, targets);
1124
1125        diffed.retain_difference(&MultiProofTargets::accounts((0..5).map(B256::with_last_byte)));
1126        assert_eq!(diffed, MultiProofTargets::accounts((5..10).map(B256::with_last_byte)));
1127
1128        diffed.retain_difference(&targets);
1129        assert!(diffed.is_empty());
1130
1131        let mut targets = MultiProofTargets::default();
1132        let (account1, account2, account3) =
1133            (1..=3).map(B256::with_last_byte).collect_tuple().unwrap();
1134        let account2_slots = (1..5).map(B256::with_last_byte).collect::<B256Set>();
1135        targets.insert(account1, B256Set::from_iter([B256::with_last_byte(1)]));
1136        targets.insert(account2, account2_slots.clone());
1137        targets.insert(account3, B256Set::from_iter([B256::with_last_byte(1)]));
1138
1139        let mut diffed = targets.clone();
1140        diffed.retain_difference(&MultiProofTargets::accounts((1..=3).map(B256::with_last_byte)));
1141        assert_eq!(diffed, targets);
1142
1143        // remove last 3 slots for account 2
1144        let mut account2_slots_expected_len = account2_slots.len();
1145        for slot in account2_slots.iter().skip(1) {
1146            diffed.retain_difference(&MultiProofTargets::account_with_slots(account2, [*slot]));
1147            account2_slots_expected_len -= 1;
1148            assert_eq!(
1149                diffed.get(&account2).map(|slots| slots.len()),
1150                Some(account2_slots_expected_len)
1151            );
1152        }
1153
1154        diffed.retain_difference(&targets);
1155        assert!(diffed.is_empty());
1156    }
1157
1158    #[test]
1159    fn test_multi_proof_retain_difference_no_overlap() {
1160        let mut targets = MultiProofTargets::default();
1161
1162        // populate some targets
1163        let (addr1, addr2) = (B256::random(), B256::random());
1164        let (slot1, slot2) = (B256::random(), B256::random());
1165        targets.insert(addr1, std::iter::once(slot1).collect());
1166        targets.insert(addr2, std::iter::once(slot2).collect());
1167
1168        let mut retained = targets.clone();
1169        retained.retain_difference(&Default::default());
1170        assert_eq!(retained, targets);
1171
1172        // add a different addr and slot to fetched proof targets
1173        let mut other_targets = MultiProofTargets::default();
1174        let addr3 = B256::random();
1175        let slot3 = B256::random();
1176        other_targets.insert(addr3, B256Set::from_iter([slot3]));
1177
1178        // check that the prefetch proof targets are the same because the fetched proof targets
1179        // don't overlap with the prefetch targets
1180        let mut retained = targets.clone();
1181        retained.retain_difference(&other_targets);
1182        assert_eq!(retained, targets);
1183    }
1184
1185    #[test]
1186    fn test_get_prefetch_proof_targets_remove_subset() {
1187        // populate some targets
1188        let mut targets = MultiProofTargets::default();
1189        let (addr1, addr2) = (B256::random(), B256::random());
1190        let (slot1, slot2) = (B256::random(), B256::random());
1191        targets.insert(addr1, B256Set::from_iter([slot1]));
1192        targets.insert(addr2, B256Set::from_iter([slot2]));
1193
1194        // add a subset of the first target to other proof targets
1195        let other_targets = MultiProofTargets::account_with_slots(addr1, [slot1]);
1196
1197        let mut retained = targets.clone();
1198        retained.retain_difference(&other_targets);
1199
1200        // check that the prefetch proof targets do not include the subset
1201        assert_eq!(retained.len(), 1);
1202        assert!(!retained.contains_key(&addr1));
1203        assert!(retained.contains_key(&addr2));
1204
1205        // now add one more slot to the prefetch targets
1206        let slot3 = B256::random();
1207        targets.get_mut(&addr1).unwrap().insert(slot3);
1208
1209        let mut retained = targets.clone();
1210        retained.retain_difference(&other_targets);
1211
1212        // check that the prefetch proof targets do not include the subset
1213        // but include the new slot
1214        assert_eq!(retained.len(), 2);
1215        assert!(retained.contains_key(&addr1));
1216        assert_eq!(retained.get(&addr1), Some(&B256Set::from_iter([slot3])));
1217        assert!(retained.contains_key(&addr2));
1218        assert_eq!(retained.get(&addr2), Some(&B256Set::from_iter([slot2])));
1219    }
1220
1221    #[test]
1222    #[cfg(feature = "eip1186")]
1223    fn eip_1186_roundtrip() {
1224        let mut acc = AccountProof {
1225            address: Address::random(),
1226            info: Some(
1227                // non-empty account
1228                Account { nonce: 100, balance: U256::ZERO, bytecode_hash: Some(KECCAK_EMPTY) },
1229            ),
1230            proof: vec![],
1231            storage_root: B256::ZERO,
1232            storage_proofs: vec![],
1233        };
1234
1235        let rpc_proof = acc.clone().into_eip1186_response(Vec::new());
1236        let inverse: AccountProof = rpc_proof.into();
1237        assert_eq!(acc, inverse);
1238
1239        // make account empty
1240        acc.info.as_mut().unwrap().nonce = 0;
1241        let rpc_proof = acc.clone().into_eip1186_response(Vec::new());
1242        let inverse: AccountProof = rpc_proof.into();
1243        acc.info.take();
1244        acc.storage_root = EMPTY_ROOT_HASH;
1245        assert_eq!(acc, inverse);
1246    }
1247
1248    #[test]
1249    #[cfg(feature = "eip1186")]
1250    fn from_eip1186_proof_accepts_geth_zero_hashes() {
1251        // geth (since v1.13.4) returns B256::ZERO for codeHash and storageHash
1252        // in exclusion proofs for non-existent accounts, instead of
1253        // KECCAK_EMPTY / EMPTY_ROOT_HASH. Verify that from_eip1186_proof
1254        // correctly recognizes this format as a non-existent account.
1255        let geth_proof = alloy_rpc_types_eth::EIP1186AccountProofResponse {
1256            address: Address::random(),
1257            balance: U256::ZERO,
1258            code_hash: B256::ZERO,
1259            nonce: 0,
1260            storage_hash: B256::ZERO,
1261            account_proof: vec![],
1262            storage_proof: vec![],
1263        };
1264
1265        let acc: AccountProof = geth_proof.into();
1266        // Should be interpreted as a non-existent account (info = None)
1267        assert!(acc.info.is_none());
1268        assert_eq!(acc.storage_root, EMPTY_ROOT_HASH);
1269    }
1270
1271    #[test]
1272    #[cfg(feature = "eip1186")]
1273    fn from_eip1186_proof_accepts_empty_hashes() {
1274        let proof = alloy_rpc_types_eth::EIP1186AccountProofResponse {
1275            address: Address::random(),
1276            balance: U256::ZERO,
1277            code_hash: KECCAK_EMPTY,
1278            nonce: 0,
1279            storage_hash: EMPTY_ROOT_HASH,
1280            account_proof: vec![],
1281            storage_proof: vec![],
1282        };
1283
1284        let acc: AccountProof = proof.into();
1285        assert!(acc.info.is_none());
1286        assert_eq!(acc.storage_root, EMPTY_ROOT_HASH);
1287    }
1288
1289    #[test]
1290    #[cfg(feature = "eip1186")]
1291    fn into_eip1186_response_zero_empty_account() {
1292        // Non-existent account (info = None)
1293        let acc = AccountProof {
1294            address: Address::random(),
1295            info: None,
1296            proof: vec![],
1297            storage_root: EMPTY_ROOT_HASH,
1298            storage_proofs: vec![],
1299        };
1300
1301        // Default behavior: KECCAK_EMPTY / EMPTY_ROOT_HASH
1302        let rpc_default = acc.clone().into_eip1186_response(Vec::new());
1303        assert_eq!(rpc_default.code_hash, KECCAK_EMPTY);
1304        assert_eq!(rpc_default.storage_hash, EMPTY_ROOT_HASH);
1305
1306        // zero_empty_account = false: same as default
1307        let rpc_compat_off = acc.clone().into_eip1186_response_with(Vec::new(), false);
1308        assert_eq!(rpc_compat_off.code_hash, KECCAK_EMPTY);
1309        assert_eq!(rpc_compat_off.storage_hash, EMPTY_ROOT_HASH);
1310
1311        // zero_empty_account = true: B256::ZERO (geth-compat)
1312        let rpc_compat_on = acc.into_eip1186_response_with(Vec::new(), true);
1313        assert_eq!(rpc_compat_on.code_hash, B256::ZERO);
1314        assert_eq!(rpc_compat_on.storage_hash, B256::ZERO);
1315
1316        // Existing account should NOT be affected by zero_empty_account
1317        let existing_acc = AccountProof {
1318            address: Address::random(),
1319            info: Some(Account {
1320                nonce: 42,
1321                balance: U256::from(100),
1322                bytecode_hash: Some(KECCAK_EMPTY),
1323            }),
1324            proof: vec![],
1325            storage_root: B256::random(),
1326            storage_proofs: vec![],
1327        };
1328        let rpc_existing = existing_acc.clone().into_eip1186_response_with(Vec::new(), true);
1329        assert_eq!(rpc_existing.code_hash, KECCAK_EMPTY);
1330        assert_eq!(rpc_existing.storage_hash, existing_acc.storage_root);
1331    }
1332
1333    #[test]
1334    fn test_multiproof_targets_chunking_length() {
1335        let mut targets = MultiProofTargets::default();
1336        targets.insert(B256::with_last_byte(1), B256Set::default());
1337        targets.insert(
1338            B256::with_last_byte(2),
1339            B256Set::from_iter([B256::with_last_byte(10), B256::with_last_byte(20)]),
1340        );
1341        targets.insert(
1342            B256::with_last_byte(3),
1343            B256Set::from_iter([
1344                B256::with_last_byte(30),
1345                B256::with_last_byte(31),
1346                B256::with_last_byte(32),
1347            ]),
1348        );
1349
1350        let chunking_length = targets.chunking_length();
1351        for size in 1..=targets.clone().chunks(1).count() {
1352            let chunk_count = targets.clone().chunks(size).count();
1353            let expected_count = chunking_length.div_ceil(size);
1354            assert_eq!(
1355                chunk_count, expected_count,
1356                "chunking_length: {}, size: {}",
1357                chunking_length, size
1358            );
1359        }
1360    }
1361}