reth_codecs/alloy/
genesis_account.rs
1use crate::Compact;
4use alloc::vec::Vec;
5use alloy_genesis::GenesisAccount as AlloyGenesisAccount;
6use alloy_primitives::{Bytes, B256, U256};
7use reth_codecs_derive::add_arbitrary_tests;
8
9#[derive(Debug, Clone, PartialEq, Eq, Compact)]
14#[reth_codecs(crate = "crate")]
15pub(crate) struct GenesisAccountRef<'a> {
16 nonce: Option<u64>,
18 balance: &'a U256,
20 code: Option<&'a Bytes>,
22 storage: Option<StorageEntries>,
24 private_key: Option<&'a B256>,
26}
27
28#[derive(Debug, Clone, PartialEq, Eq, Default, Compact)]
31#[reth_codecs(crate = "crate")]
32#[cfg_attr(
33 any(test, feature = "test-utils"),
34 derive(arbitrary::Arbitrary, serde::Serialize, serde::Deserialize)
35)]
36#[cfg_attr(feature = "test-utils", allow(unreachable_pub), visibility::make(pub))]
37#[add_arbitrary_tests(crate, compact)]
38pub(crate) struct GenesisAccount {
39 nonce: Option<u64>,
41 balance: U256,
43 code: Option<Bytes>,
45 storage: Option<StorageEntries>,
47 private_key: Option<B256>,
49}
50
51#[derive(Debug, Clone, PartialEq, Eq, Default, Compact)]
52#[reth_codecs(crate = "crate")]
53#[cfg_attr(
54 any(test, feature = "test-utils"),
55 derive(arbitrary::Arbitrary, serde::Serialize, serde::Deserialize)
56)]
57#[add_arbitrary_tests(crate, compact)]
58pub(crate) struct StorageEntries {
59 entries: Vec<StorageEntry>,
60}
61
62#[derive(Debug, Clone, PartialEq, Eq, Default, Compact)]
63#[reth_codecs(crate = "crate")]
64#[cfg_attr(
65 any(test, feature = "test-utils"),
66 derive(arbitrary::Arbitrary, serde::Serialize, serde::Deserialize)
67)]
68#[add_arbitrary_tests(crate, compact)]
69pub(crate) struct StorageEntry {
70 key: B256,
71 value: B256,
72}
73
74impl Compact for AlloyGenesisAccount {
75 fn to_compact<B>(&self, buf: &mut B) -> usize
76 where
77 B: bytes::BufMut + AsMut<[u8]>,
78 {
79 let account = GenesisAccountRef {
80 nonce: self.nonce,
81 balance: &self.balance,
82 code: self.code.as_ref(),
83 storage: self.storage.as_ref().map(|s| StorageEntries {
84 entries: s
85 .iter()
86 .map(|(key, value)| StorageEntry { key: *key, value: *value })
87 .collect(),
88 }),
89 private_key: self.private_key.as_ref(),
90 };
91 account.to_compact(buf)
92 }
93
94 fn from_compact(buf: &[u8], len: usize) -> (Self, &[u8]) {
95 let (account, _) = GenesisAccount::from_compact(buf, len);
96 let alloy_account = Self {
97 nonce: account.nonce,
98 balance: account.balance,
99 code: account.code,
100 storage: account
101 .storage
102 .map(|s| s.entries.into_iter().map(|entry| (entry.key, entry.value)).collect()),
103 private_key: account.private_key,
104 };
105 (alloy_account, buf)
106 }
107}