From a0dcb0c522406d93201b94716648e8e65e65be30 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Mon, 23 Dec 2024 11:46:06 +0100 Subject: [PATCH] chore: replace root fns with alloys (#13501) --- crates/trie/common/Cargo.toml | 2 +- crates/trie/common/src/root.rs | 84 +++------------------------------- 2 files changed, 7 insertions(+), 79 deletions(-) diff --git a/crates/trie/common/Cargo.toml b/crates/trie/common/Cargo.toml index 02994c13d..f05731e87 100644 --- a/crates/trie/common/Cargo.toml +++ b/crates/trie/common/Cargo.toml @@ -15,7 +15,7 @@ workspace = true # alloy alloy-primitives.workspace = true alloy-rlp = { workspace = true, features = ["arrayvec"] } -alloy-trie.workspace = true +alloy-trie = { workspace = true, features = ["ethereum"] } alloy-consensus.workspace = true reth-primitives-traits.workspace = true reth-codecs = { workspace = true, optional = true } diff --git a/crates/trie/common/src/root.rs b/crates/trie/common/src/root.rs index b23d3b42d..43c8d8641 100644 --- a/crates/trie/common/src/root.rs +++ b/crates/trie/common/src/root.rs @@ -1,80 +1,8 @@ //! Common root computation functions. -use crate::TrieAccount; -use alloc::vec::Vec; -use alloy_primitives::{keccak256, Address, B256, U256}; -use alloy_rlp::Encodable; -use alloy_trie::HashBuilder; -use itertools::Itertools; -use nybbles::Nibbles; - -/// Hashes and sorts account keys, then proceeds to calculating the root hash of the state -/// represented as MPT. -/// See [`state_root_unsorted`] for more info. -pub fn state_root_ref_unhashed<'a, A: Into + Clone + 'a>( - state: impl IntoIterator, -) -> B256 { - state_root_unsorted( - state.into_iter().map(|(address, account)| (keccak256(address), account.clone())), - ) -} - -/// Hashes and sorts account keys, then proceeds to calculating the root hash of the state -/// represented as MPT. -/// See [`state_root_unsorted`] for more info. -pub fn state_root_unhashed>( - state: impl IntoIterator, -) -> B256 { - state_root_unsorted(state.into_iter().map(|(address, account)| (keccak256(address), account))) -} - -/// Sorts the hashed account keys and calculates the root hash of the state represented as MPT. -/// See [`state_root`] for more info. -pub fn state_root_unsorted>( - state: impl IntoIterator, -) -> B256 { - state_root(state.into_iter().sorted_unstable_by_key(|(key, _)| *key)) -} - -/// Calculates the root hash of the state represented as MPT. -/// -/// Corresponds to [geth's `deriveHash`](https://github.com/ethereum/go-ethereum/blob/6c149fd4ad063f7c24d726a73bc0546badd1bc73/core/genesis.go#L119). -/// -/// # Panics -/// -/// If the items are not in sorted order. -pub fn state_root>(state: impl IntoIterator) -> B256 { - let mut hb = HashBuilder::default(); - let mut account_rlp_buf = Vec::new(); - for (hashed_key, account) in state { - account_rlp_buf.clear(); - account.into().encode(&mut account_rlp_buf); - hb.add_leaf(Nibbles::unpack(hashed_key), &account_rlp_buf); - } - hb.root() -} - -/// Hashes storage keys, sorts them and them calculates the root hash of the storage trie. -/// See [`storage_root_unsorted`] for more info. -pub fn storage_root_unhashed(storage: impl IntoIterator) -> B256 { - storage_root_unsorted(storage.into_iter().map(|(slot, value)| (keccak256(slot), value))) -} - -/// Sorts and calculates the root hash of account storage trie. -/// See [`storage_root`] for more info. -pub fn storage_root_unsorted(storage: impl IntoIterator) -> B256 { - storage_root(storage.into_iter().sorted_unstable_by_key(|(key, _)| *key)) -} - -/// Calculates the root hash of account storage trie. -/// -/// # Panics -/// -/// If the items are not in sorted order. -pub fn storage_root(storage: impl IntoIterator) -> B256 { - let mut hb = HashBuilder::default(); - for (hashed_slot, value) in storage { - hb.add_leaf(Nibbles::unpack(hashed_slot), alloy_rlp::encode_fixed_size(&value).as_ref()); - } - hb.root() -} +// Re-export for convenience. +#[doc(inline)] +pub use alloy_trie::root::{ + state_root, state_root_ref_unhashed, state_root_unhashed, state_root_unsorted, storage_root, + storage_root_unhashed, storage_root_unsorted, +};