reth_db_models/
accounts.rs1use alloy_primitives::Address;
2use reth_primitives_traits::{Account, ValueWithSubKey};
3
4#[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 pub address: Address,
14 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#[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 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}