feat: add impl From GenesisAccount to Account (#10256)

This commit is contained in:
Miguel Tavares
2024-08-17 18:47:54 -03:00
committed by GitHub
parent 71bf5a1d73
commit 153a3e352d
4 changed files with 15 additions and 20 deletions

View File

@ -35,16 +35,6 @@ impl Account {
self.bytecode_hash.map_or(true, |hash| hash == KECCAK_EMPTY) self.bytecode_hash.map_or(true, |hash| hash == KECCAK_EMPTY)
} }
/// Makes an [Account] from [`GenesisAccount`] type
pub fn from_genesis_account(value: &GenesisAccount) -> Self {
Self {
// nonce must exist, so we default to zero when converting a genesis account
nonce: value.nonce.unwrap_or_default(),
balance: value.balance,
bytecode_hash: value.code.as_ref().map(keccak256),
}
}
/// Returns an account bytecode's hash. /// Returns an account bytecode's hash.
/// In case of no bytecode, returns [`KECCAK_EMPTY`]. /// In case of no bytecode, returns [`KECCAK_EMPTY`].
pub fn get_bytecode_hash(&self) -> B256 { pub fn get_bytecode_hash(&self) -> B256 {
@ -129,6 +119,16 @@ impl Compact for Bytecode {
} }
} }
impl From<&GenesisAccount> for Account {
fn from(value: &GenesisAccount) -> Self {
Self {
nonce: value.nonce.unwrap_or_default(),
balance: value.balance,
bytecode_hash: value.code.as_ref().map(keccak256),
}
}
}
impl From<AccountInfo> for Account { impl From<AccountInfo> for Account {
fn from(revm_acc: AccountInfo) -> Self { fn from(revm_acc: AccountInfo) -> Self {
let code_hash = revm_acc.code_hash; let code_hash = revm_acc.code_hash;

View File

@ -225,8 +225,7 @@ pub fn insert_genesis_hashes<'a, 'b, DB: Database>(
alloc: impl Iterator<Item = (&'a Address, &'b GenesisAccount)> + Clone, alloc: impl Iterator<Item = (&'a Address, &'b GenesisAccount)> + Clone,
) -> ProviderResult<()> { ) -> ProviderResult<()> {
// insert and hash accounts to hashing table // insert and hash accounts to hashing table
let alloc_accounts = let alloc_accounts = alloc.clone().map(|(addr, account)| (*addr, Some(Account::from(account))));
alloc.clone().map(|(addr, account)| (*addr, Some(Account::from_genesis_account(account))));
provider.insert_account_for_hashing(alloc_accounts)?; provider.insert_account_for_hashing(alloc_accounts)?;
trace!(target: "reth::cli", "Inserted account hashes"); trace!(target: "reth::cli", "Inserted account hashes");

View File

@ -45,10 +45,8 @@ pub fn insert_genesis<DB: Database>(
// Hash accounts and insert them into hashing table. // Hash accounts and insert them into hashing table.
let genesis = chain_spec.genesis(); let genesis = chain_spec.genesis();
let alloc_accounts = genesis let alloc_accounts =
.alloc genesis.alloc.iter().map(|(addr, account)| (*addr, Some(Account::from(account))));
.iter()
.map(|(addr, account)| (*addr, Some(Account::from_genesis_account(account))));
provider.insert_account_for_hashing(alloc_accounts).unwrap(); provider.insert_account_for_hashing(alloc_accounts).unwrap();
let alloc_storage = genesis.alloc.clone().into_iter().filter_map(|(addr, account)| { let alloc_storage = genesis.alloc.clone().into_iter().filter_map(|(addr, account)| {

View File

@ -48,10 +48,8 @@ fn insert_genesis<DB: Database>(
// Hash accounts and insert them into hashing table. // Hash accounts and insert them into hashing table.
let genesis = chain_spec.genesis(); let genesis = chain_spec.genesis();
let alloc_accounts = genesis let alloc_accounts =
.alloc genesis.alloc.iter().map(|(addr, account)| (*addr, Some(Account::from(account))));
.iter()
.map(|(addr, account)| (*addr, Some(Account::from_genesis_account(account))));
provider.insert_account_for_hashing(alloc_accounts).unwrap(); provider.insert_account_for_hashing(alloc_accounts).unwrap();
let alloc_storage = genesis.alloc.clone().into_iter().filter_map(|(addr, account)| { let alloc_storage = genesis.alloc.clone().into_iter().filter_map(|(addr, account)| {