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 acc_len = if let Some(account) = self.info { account.to_compact(buf) } else { 0 };
31        acc_len + 20
32    }
33
34    fn from_compact(mut buf: &[u8], len: usize) -> (Self, &[u8]) {
35        use bytes::Buf;
36        let address = Address::from_slice(&buf[..20]);
37        buf.advance(20);
38
39        let info = (len - 20 > 0).then(|| {
40            let (acc, advanced_buf) = Account::from_compact(buf, len - 20);
41            buf = advanced_buf;
42            acc
43        });
44
45        (Self { address, info }, buf)
46    }
47}