reth_primitives_traits/
storage.rs

1use alloy_primitives::{B256, U256};
2
3/// Trait for `DupSort` table values that contain a subkey.
4///
5/// This trait allows extracting the subkey from a value during database iteration,
6/// enabling proper range queries and filtering on `DupSort` tables.
7pub trait ValueWithSubKey {
8    /// The type of the subkey.
9    type SubKey;
10
11    /// Extract the subkey from the value.
12    fn get_subkey(&self) -> Self::SubKey;
13}
14
15/// Account storage entry.
16///
17/// `key` is the subkey when used as a value in the `StorageChangeSets` table.
18#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
19#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
20#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
21#[cfg_attr(any(test, feature = "reth-codec"), reth_codecs::add_arbitrary_tests(compact))]
22pub struct StorageEntry {
23    /// Storage key.
24    pub key: B256,
25    /// Value on storage key.
26    pub value: U256,
27}
28
29impl StorageEntry {
30    /// Create a new `StorageEntry` with given key and value.
31    pub const fn new(key: B256, value: U256) -> Self {
32        Self { key, value }
33    }
34}
35
36impl ValueWithSubKey for StorageEntry {
37    type SubKey = B256;
38
39    fn get_subkey(&self) -> Self::SubKey {
40        self.key
41    }
42}
43
44impl From<(B256, U256)> for StorageEntry {
45    fn from((key, value): (B256, U256)) -> Self {
46        Self { key, value }
47    }
48}
49
50// NOTE: Removing reth_codec and manually encode subkey
51// and compress second part of the value. If we have compression
52// over whole value (Even SubKey) that would mess up fetching of values with seek_by_key_subkey
53#[cfg(any(test, feature = "reth-codec"))]
54impl reth_codecs::Compact for StorageEntry {
55    fn to_compact<B>(&self, buf: &mut B) -> usize
56    where
57        B: bytes::BufMut + AsMut<[u8]>,
58    {
59        // for now put full bytes and later compress it.
60        buf.put_slice(&self.key[..]);
61        self.value.to_compact(buf) + 32
62    }
63
64    fn from_compact(buf: &[u8], len: usize) -> (Self, &[u8]) {
65        let key = B256::from_slice(&buf[..32]);
66        let (value, out) = U256::from_compact(&buf[32..], len - 32);
67        (Self { key, value }, out)
68    }
69}