reth_storage_api/
hashing.rs

1use alloy_primitives::{map::HashMap, Address, BlockNumber, B256};
2use auto_impl::auto_impl;
3use reth_db::models::{AccountBeforeTx, BlockNumberAddress};
4use reth_primitives::{Account, StorageEntry};
5use reth_storage_errors::provider::ProviderResult;
6use std::{
7    collections::{BTreeMap, BTreeSet},
8    ops::{RangeBounds, RangeInclusive},
9};
10
11/// Hashing Writer
12#[auto_impl(&, Arc, Box)]
13pub trait HashingWriter: Send + Sync {
14    /// Unwind and clear account hashing.
15    ///
16    /// # Returns
17    ///
18    /// Set of hashed keys of updated accounts.
19    fn unwind_account_hashing<'a>(
20        &self,
21        changesets: impl Iterator<Item = &'a (BlockNumber, AccountBeforeTx)>,
22    ) -> ProviderResult<BTreeMap<B256, Option<Account>>>;
23
24    /// Unwind and clear account hashing in a given block range.
25    ///
26    /// # Returns
27    ///
28    /// Set of hashed keys of updated accounts.
29    fn unwind_account_hashing_range(
30        &self,
31        range: impl RangeBounds<BlockNumber>,
32    ) -> ProviderResult<BTreeMap<B256, Option<Account>>>;
33
34    /// Inserts all accounts into [reth_db::tables::AccountsHistory] table.
35    ///
36    /// # Returns
37    ///
38    /// Set of hashed keys of updated accounts.
39    fn insert_account_for_hashing(
40        &self,
41        accounts: impl IntoIterator<Item = (Address, Option<Account>)>,
42    ) -> ProviderResult<BTreeMap<B256, Option<Account>>>;
43
44    /// Unwind and clear storage hashing
45    ///
46    /// # Returns
47    ///
48    /// Mapping of hashed keys of updated accounts to their respective updated hashed slots.
49    fn unwind_storage_hashing(
50        &self,
51        changesets: impl Iterator<Item = (BlockNumberAddress, StorageEntry)>,
52    ) -> ProviderResult<HashMap<B256, BTreeSet<B256>>>;
53
54    /// Unwind and clear storage hashing in a given block range.
55    ///
56    /// # Returns
57    ///
58    /// Mapping of hashed keys of updated accounts to their respective updated hashed slots.
59    fn unwind_storage_hashing_range(
60        &self,
61        range: impl RangeBounds<BlockNumberAddress>,
62    ) -> ProviderResult<HashMap<B256, BTreeSet<B256>>>;
63
64    /// Iterates over storages and inserts them to hashing table.
65    ///
66    /// # Returns
67    ///
68    /// Mapping of hashed keys of updated accounts to their respective updated hashed slots.
69    fn insert_storage_for_hashing(
70        &self,
71        storages: impl IntoIterator<Item = (Address, impl IntoIterator<Item = StorageEntry>)>,
72    ) -> ProviderResult<HashMap<B256, BTreeSet<B256>>>;
73
74    /// Calculate the hashes of all changed accounts and storages, and finally calculate the state
75    /// root.
76    ///
77    /// The hashes are calculated from `fork_block_number + 1` to `current_block_number`.
78    ///
79    /// The resulting state root is compared with `expected_state_root`.
80    fn insert_hashes(
81        &self,
82        range: RangeInclusive<BlockNumber>,
83        end_block_hash: B256,
84        expected_state_root: B256,
85    ) -> ProviderResult<()>;
86}