reth_db_models/
accounts.rs

1use alloy_primitives::Address;
2use reth_primitives_traits::{Account, ValueWithSubKey};
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
18impl ValueWithSubKey for AccountBeforeTx {
19    type SubKey = Address;
20
21    fn get_subkey(&self) -> Self::SubKey {
22        self.address
23    }
24}
25
26// NOTE: Removing reth_codec and manually encode subkey
27// and compress second part of the value. If we have compression
28// over whole value (Even SubKey) that would mess up fetching of values with seek_by_key_subkey
29#[cfg(any(test, feature = "reth-codec"))]
30impl reth_codecs::Compact for AccountBeforeTx {
31    fn to_compact<B>(&self, buf: &mut B) -> usize
32    where
33        B: bytes::BufMut + AsMut<[u8]>,
34    {
35        // for now put full bytes and later compress it.
36        buf.put_slice(self.address.as_slice());
37
38        let acc_len = if let Some(account) = self.info { account.to_compact(buf) } else { 0 };
39        acc_len + 20
40    }
41
42    fn from_compact(mut buf: &[u8], len: usize) -> (Self, &[u8]) {
43        use bytes::Buf;
44        let address = Address::from_slice(&buf[..20]);
45        buf.advance(20);
46
47        let info = (len - 20 > 0).then(|| {
48            let (acc, advanced_buf) = Account::from_compact(buf, len - 20);
49            buf = advanced_buf;
50            acc
51        });
52
53        (Self { address, info }, buf)
54    }
55}