reth_db_models/
accounts.rs

1use alloy_primitives::Address;
2use reth_primitives_traits::Account;
3
4/// Account as it is saved in the database.
5///
6/// [`Address`] is the subkey.
7#[derive(Debug, Default, Clone, Eq, PartialEq)]
8#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
9#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
10#[cfg_attr(any(test, feature = "reth-codec"), reth_codecs::add_arbitrary_tests(compact))]
11pub struct AccountBeforeTx {
12    /// Address for the account. Acts as `DupSort::SubKey`.
13    pub address: Address,
14    /// Account state before the transaction.
15    pub info: Option<Account>,
16}
17
18// NOTE: Removing reth_codec and manually encode subkey
19// and compress second part of the value. If we have compression
20// over whole value (Even SubKey) that would mess up fetching of values with seek_by_key_subkey
21#[cfg(any(test, feature = "reth-codec"))]
22impl reth_codecs::Compact for AccountBeforeTx {
23    fn to_compact<B>(&self, buf: &mut B) -> usize
24    where
25        B: bytes::BufMut + AsMut<[u8]>,
26    {
27        // for now put full bytes and later compress it.
28        buf.put_slice(self.address.as_slice());
29
30        let mut acc_len = 0;
31        if let Some(account) = self.info {
32            acc_len = account.to_compact(buf);
33        }
34        acc_len + 20
35    }
36
37    fn from_compact(mut buf: &[u8], len: usize) -> (Self, &[u8]) {
38        use bytes::Buf;
39        let address = Address::from_slice(&buf[..20]);
40        buf.advance(20);
41
42        let info = (len - 20 > 0).then(|| {
43            let (acc, advanced_buf) = Account::from_compact(buf, len - 20);
44            buf = advanced_buf;
45            acc
46        });
47
48        (Self { address, info }, buf)
49    }
50}