reth_trie_common/
storage.rs

1use super::{BranchNodeCompact, StoredNibblesSubKey};
2use reth_primitives_traits::ValueWithSubKey;
3
4/// Account storage trie node.
5///
6/// `nibbles` is the subkey when used as a value in the `StorageTrie` table.
7#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
8#[cfg_attr(any(test, feature = "serde"), derive(serde::Serialize, serde::Deserialize))]
9pub struct StorageTrieEntry {
10    /// The nibbles of the intermediate node
11    pub nibbles: StoredNibblesSubKey,
12    /// Encoded node.
13    pub node: BranchNodeCompact,
14}
15
16impl ValueWithSubKey for StorageTrieEntry {
17    type SubKey = StoredNibblesSubKey;
18
19    fn get_subkey(&self) -> Self::SubKey {
20        self.nibbles.clone()
21    }
22}
23
24// NOTE: Removing reth_codec and manually encode subkey
25// and compress second part of the value. If we have compression
26// over whole value (Even SubKey) that would mess up fetching of values with seek_by_key_subkey
27#[cfg(any(test, feature = "reth-codec"))]
28impl reth_codecs::Compact for StorageTrieEntry {
29    fn to_compact<B>(&self, buf: &mut B) -> usize
30    where
31        B: bytes::BufMut + AsMut<[u8]>,
32    {
33        let nibbles_len = self.nibbles.to_compact(buf);
34        let node_len = self.node.to_compact(buf);
35        nibbles_len + node_len
36    }
37
38    fn from_compact(buf: &[u8], len: usize) -> (Self, &[u8]) {
39        let (nibbles, buf) = StoredNibblesSubKey::from_compact(buf, 65);
40        let (node, buf) = BranchNodeCompact::from_compact(buf, len - 65);
41        let this = Self { nibbles, node };
42        (this, buf)
43    }
44}