chore: replace TrieAccount with alloy's (#13397)

This commit is contained in:
Matthias Seitz
2024-12-16 03:57:02 +01:00
committed by GitHub
parent 4e6ed39b6d
commit 091c5499ba
17 changed files with 93 additions and 173 deletions

View File

@ -19,9 +19,7 @@ alloy-trie.workspace = true
alloy-consensus.workspace = true
reth-primitives-traits.workspace = true
reth-codecs = { workspace = true, optional = true }
revm-primitives.workspace = true
alloy-genesis.workspace = true
alloy-rpc-types-eth = { workspace = true, optional = true }
alloy-serde = { workspace = true, optional = true }
@ -43,6 +41,7 @@ arbitrary = { workspace = true, features = ["derive"], optional = true }
[dev-dependencies]
reth-primitives-traits = { workspace = true, features = ["serde"] }
reth-codecs.workspace = true
alloy-genesis.workspace = true
alloy-primitives = { workspace = true, features = ["getrandom"] }
alloy-trie = { workspace = true, features = ["arbitrary", "serde"] }
@ -71,7 +70,6 @@ serde = [
"alloy-consensus/serde",
"alloy-trie/serde",
"alloy-rpc-types-eth?/serde",
"revm-primitives/serde",
"reth-primitives-traits/serde",
"reth-codecs?/serde"
]
@ -101,7 +99,6 @@ arbitrary = [
"alloy-consensus/arbitrary",
"alloy-primitives/arbitrary",
"nybbles/arbitrary",
"revm-primitives/arbitrary",
"reth-codecs/arbitrary",
"alloy-rpc-types-eth?/arbitrary"
]

View File

@ -1,83 +1,18 @@
use crate::root::storage_root_unhashed;
use alloy_consensus::constants::KECCAK_EMPTY;
use alloy_genesis::GenesisAccount;
use alloy_primitives::{keccak256, B256, U256};
use alloy_rlp::{RlpDecodable, RlpEncodable};
use alloy_trie::EMPTY_ROOT_HASH;
use reth_primitives_traits::Account;
use revm_primitives::AccountInfo;
/// An Ethereum account as represented in the trie.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default, RlpEncodable, RlpDecodable)]
pub struct TrieAccount {
/// Account nonce.
pub nonce: u64,
/// Account balance.
pub balance: U256,
/// Account's storage root.
pub storage_root: B256,
/// Hash of the account's bytecode.
pub code_hash: B256,
}
impl TrieAccount {
/// Get account's storage root.
pub const fn storage_root(&self) -> B256 {
self.storage_root
}
}
impl From<GenesisAccount> for TrieAccount {
fn from(account: GenesisAccount) -> Self {
let storage_root = account
.storage
.map(|storage| {
storage_root_unhashed(
storage
.into_iter()
.filter(|(_, value)| !value.is_zero())
.map(|(slot, value)| (slot, U256::from_be_bytes(*value))),
)
})
.unwrap_or(EMPTY_ROOT_HASH);
Self {
nonce: account.nonce.unwrap_or_default(),
balance: account.balance,
storage_root,
code_hash: account.code.map_or(KECCAK_EMPTY, keccak256),
}
}
}
impl From<(Account, B256)> for TrieAccount {
fn from((account, storage_root): (Account, B256)) -> Self {
Self {
nonce: account.nonce,
balance: account.balance,
storage_root,
code_hash: account.bytecode_hash.unwrap_or(KECCAK_EMPTY),
}
}
}
impl From<(AccountInfo, B256)> for TrieAccount {
fn from((account, storage_root): (AccountInfo, B256)) -> Self {
Self {
nonce: account.nonce,
balance: account.balance,
storage_root,
code_hash: account.code_hash,
}
}
}
/// Re-export for convenience.
pub use alloy_trie::TrieAccount;
#[cfg(test)]
mod tests {
use super::*;
use alloy_primitives::Bytes;
use crate::root::storage_root_unhashed;
use alloy_consensus::constants::KECCAK_EMPTY;
use alloy_genesis::GenesisAccount;
use alloy_primitives::{keccak256, Bytes, B256, U256};
use std::collections::BTreeMap;
use alloy_trie::EMPTY_ROOT_HASH;
use reth_primitives_traits::Account;
#[test]
fn test_from_genesis_account_with_default_values() {
let genesis_account = GenesisAccount::default();
@ -88,14 +23,11 @@ mod tests {
// Check the fields are properly set.
assert_eq!(trie_account.nonce, 0);
assert_eq!(trie_account.balance, U256::default());
assert_eq!(trie_account.storage_root(), EMPTY_ROOT_HASH);
assert_eq!(trie_account.storage_root, EMPTY_ROOT_HASH);
assert_eq!(trie_account.code_hash, KECCAK_EMPTY);
// Check that the default Account converts to the same TrieAccount
assert_eq!(TrieAccount::from((Account::default(), EMPTY_ROOT_HASH)), trie_account);
// Check that the default AccountInfo converts to the same TrieAccount
assert_eq!(TrieAccount::from((AccountInfo::default(), EMPTY_ROOT_HASH)), trie_account);
assert_eq!(Account::default().into_trie_account(EMPTY_ROOT_HASH), trie_account);
}
#[test]
@ -123,33 +55,17 @@ mod tests {
// Check that the fields are properly set.
assert_eq!(trie_account.nonce, 10);
assert_eq!(trie_account.balance, U256::from(1000));
assert_eq!(trie_account.storage_root(), expected_storage_root);
assert_eq!(trie_account.storage_root, expected_storage_root);
assert_eq!(trie_account.code_hash, keccak256([0x60, 0x61]));
// Check that the Account converts to the same TrieAccount
assert_eq!(
TrieAccount::from((
Account {
nonce: 10,
balance: U256::from(1000),
bytecode_hash: Some(keccak256([0x60, 0x61]))
},
expected_storage_root
)),
trie_account
);
// Check that the AccountInfo converts to the same TrieAccount
assert_eq!(
TrieAccount::from((
AccountInfo {
nonce: 10,
balance: U256::from(1000),
code_hash: keccak256([0x60, 0x61]),
..Default::default()
},
expected_storage_root
)),
Account {
nonce: 10,
balance: U256::from(1000),
bytecode_hash: Some(keccak256([0x60, 0x61]))
}
.into_trie_account(expected_storage_root),
trie_account
);
}
@ -174,7 +90,7 @@ mod tests {
assert_eq!(trie_account.nonce, 3);
assert_eq!(trie_account.balance, U256::from(300));
// Zero values in storage should result in EMPTY_ROOT_HASH
assert_eq!(trie_account.storage_root(), EMPTY_ROOT_HASH);
assert_eq!(trie_account.storage_root, EMPTY_ROOT_HASH);
// No code provided, so code hash should be KECCAK_EMPTY
assert_eq!(trie_account.code_hash, KECCAK_EMPTY);
}

View File

@ -258,10 +258,9 @@ impl AccountProof {
let expected = if self.info.is_none() && self.storage_root == EMPTY_ROOT_HASH {
None
} else {
Some(alloy_rlp::encode(TrieAccount::from((
self.info.unwrap_or_default(),
self.storage_root,
))))
Some(alloy_rlp::encode(
self.info.unwrap_or_default().into_trie_account(self.storage_root),
))
};
let nibbles = Nibbles::unpack(keccak256(self.address));
verify_proof(root, nibbles, expected, &self.proof)