1use alloy_primitives::Address;
2use reth_primitives_traits::Account;
34/// 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`.
13pub address: Address,
14/// Account state before the transaction.
15pub info: Option<Account>,
16}
1718// 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 {
23fn to_compact<B>(&self, buf: &mut B) -> usize24where
25B: bytes::BufMut + AsMut<[u8]>,
26 {
27// for now put full bytes and later compress it.
28buf.put_slice(self.address.as_slice());
2930let mut acc_len = 0;
31if let Some(account) = self.info {
32acc_len = account.to_compact(buf);
33 }
34acc_len + 20
35}
3637fn from_compact(mut buf: &[u8], len: usize) -> (Self, &[u8]) {
38use bytes::Buf;
39let address = Address::from_slice(&buf[..20]);
40buf.advance(20);
4142let info = (len - 20 > 0).then(|| {
43let (acc, advanced_buf) = Account::from_compact(buf, len - 20);
44buf = advanced_buf;
45acc46 });
4748 (Self { address, info }, buf)
49 }
50}