reth_db_models/
accounts.rs

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