reth_storage_api/
hashing.rs

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