reth_codecs/alloy/
genesis_account.rsuse crate::Compact;
use alloc::vec::Vec;
use alloy_genesis::GenesisAccount as AlloyGenesisAccount;
use alloy_primitives::{Bytes, B256, U256};
use reth_codecs_derive::add_arbitrary_tests;
#[derive(Debug, Clone, PartialEq, Eq, Compact)]
#[reth_codecs(crate = "crate")]
pub(crate) struct GenesisAccountRef<'a> {
nonce: Option<u64>,
balance: &'a U256,
code: Option<&'a Bytes>,
storage: Option<StorageEntries>,
private_key: Option<&'a B256>,
}
#[derive(Debug, Clone, PartialEq, Eq, Default, Compact)]
#[reth_codecs(crate = "crate")]
#[cfg_attr(
any(test, feature = "test-utils"),
derive(arbitrary::Arbitrary, serde::Serialize, serde::Deserialize)
)]
#[cfg_attr(feature = "test-utils", allow(unreachable_pub), visibility::make(pub))]
#[add_arbitrary_tests(crate, compact)]
pub(crate) struct GenesisAccount {
nonce: Option<u64>,
balance: U256,
code: Option<Bytes>,
storage: Option<StorageEntries>,
private_key: Option<B256>,
}
#[derive(Debug, Clone, PartialEq, Eq, Default, Compact)]
#[reth_codecs(crate = "crate")]
#[cfg_attr(
any(test, feature = "test-utils"),
derive(arbitrary::Arbitrary, serde::Serialize, serde::Deserialize)
)]
#[add_arbitrary_tests(crate, compact)]
pub(crate) struct StorageEntries {
entries: Vec<StorageEntry>,
}
#[derive(Debug, Clone, PartialEq, Eq, Default, Compact)]
#[reth_codecs(crate = "crate")]
#[cfg_attr(
any(test, feature = "test-utils"),
derive(arbitrary::Arbitrary, serde::Serialize, serde::Deserialize)
)]
#[add_arbitrary_tests(crate, compact)]
pub(crate) struct StorageEntry {
key: B256,
value: B256,
}
impl Compact for AlloyGenesisAccount {
fn to_compact<B>(&self, buf: &mut B) -> usize
where
B: bytes::BufMut + AsMut<[u8]>,
{
let account = GenesisAccountRef {
nonce: self.nonce,
balance: &self.balance,
code: self.code.as_ref(),
storage: self.storage.as_ref().map(|s| StorageEntries {
entries: s
.iter()
.map(|(key, value)| StorageEntry { key: *key, value: *value })
.collect(),
}),
private_key: self.private_key.as_ref(),
};
account.to_compact(buf)
}
fn from_compact(buf: &[u8], len: usize) -> (Self, &[u8]) {
let (account, _) = GenesisAccount::from_compact(buf, len);
let alloy_account = Self {
nonce: account.nonce,
balance: account.balance,
code: account.code,
storage: account
.storage
.map(|s| s.entries.into_iter().map(|entry| (entry.key, entry.value)).collect()),
private_key: account.private_key,
};
(alloy_account, buf)
}
}