reth_codecs/alloy/
genesis_account.rs

1//! Compact implementation for [`AlloyGenesisAccount`]
2
3use crate::Compact;
4use alloc::vec::Vec;
5use seismic_alloy_genesis::GenesisAccount as AlloyGenesisAccount;
6use alloy_primitives::{Bytes, B256, U256};
7use reth_codecs_derive::add_arbitrary_tests;
8use alloy_primitives::FlaggedStorage;
9
10/// `GenesisAccount` acts as bridge which simplifies Compact implementation for
11/// `AlloyGenesisAccount`.
12///
13/// Notice: Make sure this struct is 1:1 with `alloy_genesis::GenesisAccount`
14#[derive(Debug, Clone, PartialEq, Eq, Compact)]
15#[reth_codecs(crate = "crate")]
16pub(crate) struct GenesisAccountRef<'a> {
17    /// The nonce of the account at genesis.
18    nonce: Option<u64>,
19    /// The balance of the account at genesis.
20    balance: &'a U256,
21    /// The account's bytecode at genesis.
22    code: Option<&'a Bytes>,
23    /// The account's storage at genesis.
24    storage: Option<StorageEntries>,
25    /// The account's private key. Should only be used for testing.
26    private_key: Option<&'a B256>,
27}
28
29/// Acts as bridge which simplifies Compact implementation for
30/// `AlloyGenesisAccount`.
31#[derive(Debug, Clone, PartialEq, Eq, Default, Compact)]
32#[reth_codecs(crate = "crate")]
33#[cfg_attr(
34    any(test, feature = "test-utils"),
35    derive(arbitrary::Arbitrary, serde::Serialize, serde::Deserialize)
36)]
37#[cfg_attr(feature = "test-utils", allow(unreachable_pub), visibility::make(pub))]
38#[add_arbitrary_tests(crate, compact)]
39pub(crate) struct GenesisAccount {
40    /// The nonce of the account at genesis.
41    nonce: Option<u64>,
42    /// The balance of the account at genesis.
43    balance: U256,
44    /// The account's bytecode at genesis.
45    code: Option<Bytes>,
46    /// The account's storage at genesis.
47    storage: Option<StorageEntries>,
48    /// The account's private key. Should only be used for testing.
49    private_key: Option<B256>,
50}
51
52#[derive(Debug, Clone, PartialEq, Eq, Default, Compact)]
53#[reth_codecs(crate = "crate")]
54#[cfg_attr(
55    any(test, feature = "test-utils"),
56    derive(arbitrary::Arbitrary, serde::Serialize, serde::Deserialize)
57)]
58#[add_arbitrary_tests(crate, compact)]
59pub(crate) struct StorageEntries {
60    entries: Vec<StorageEntry>,
61}
62
63#[derive(Debug, Clone, PartialEq, Eq, Default, Compact)]
64#[reth_codecs(crate = "crate")]
65#[cfg_attr(
66    any(test, feature = "test-utils"),
67    derive(arbitrary::Arbitrary, serde::Serialize, serde::Deserialize)
68)]
69#[add_arbitrary_tests(crate, compact)]
70pub(crate) struct StorageEntry {
71    key: B256,
72    value: B256,
73    is_private: bool,
74}
75
76impl Compact for AlloyGenesisAccount {
77    fn to_compact<B>(&self, buf: &mut B) -> usize
78    where
79        B: bytes::BufMut + AsMut<[u8]>,
80    {
81        let account = GenesisAccountRef {
82            nonce: self.nonce,
83            balance: &self.balance,
84            code: self.code.as_ref(),
85            storage: self.storage.as_ref().map(|s| StorageEntries {
86                entries: s
87                    .iter()
88                    .map(|(key, value)| StorageEntry {
89                        key: *key,
90                        value: value.value.into(),
91                        is_private: value.is_private,
92                    })
93                    .collect(),
94            }),
95            private_key: self.private_key.as_ref(),
96        };
97        account.to_compact(buf)
98    }
99
100    fn from_compact(buf: &[u8], len: usize) -> (Self, &[u8]) {
101        let (account, _) = GenesisAccount::from_compact(buf, len);
102        let alloy_account = Self {
103            nonce: account.nonce,
104            balance: account.balance,
105            code: account.code,
106            storage: account
107                .storage
108                .map(|s| s.entries.into_iter().map(|entry| (entry.key, FlaggedStorage::new(U256::from_be_bytes(entry.value.0), entry.is_private))).collect()),
109            private_key: account.private_key,
110        };
111        (alloy_account, buf)
112    }
113}