chore: move trie functions to alloy (#12438)

This commit is contained in:
c0np4nn4
2024-11-12 17:15:52 +07:00
committed by GitHub
parent f38503c2bc
commit c261532a27
8 changed files with 17 additions and 39 deletions

View File

@ -234,11 +234,12 @@ impl StorageProof {
#[cfg(any(test, feature = "test-utils"))]
pub mod triehash {
use alloy_primitives::{keccak256, B256};
use alloy_rlp::RlpEncodable;
use hash_db::Hasher;
use plain_hasher::PlainHasher;
/// A [Hasher] that calculates a keccak256 hash of the given data.
#[derive(Default, Debug, Clone, PartialEq, Eq)]
#[derive(Default, Debug, Clone, PartialEq, Eq, RlpEncodable)]
#[non_exhaustive]
pub struct KeccakHasher;

View File

@ -18,38 +18,6 @@ pub const fn adjust_index_for_rlp(i: usize, len: usize) -> usize {
}
}
/// Compute a trie root of the collection of rlp encodable items.
pub fn ordered_trie_root<T: Encodable>(items: &[T]) -> B256 {
ordered_trie_root_with_encoder(items, |item, buf| item.encode(buf))
}
/// Compute a trie root of the collection of items with a custom encoder.
pub fn ordered_trie_root_with_encoder<T, F>(items: &[T], mut encode: F) -> B256
where
F: FnMut(&T, &mut Vec<u8>),
{
if items.is_empty() {
return alloy_trie::EMPTY_ROOT_HASH;
}
let mut value_buffer = Vec::new();
let mut hb = HashBuilder::default();
let items_len = items.len();
for i in 0..items_len {
let index = adjust_index_for_rlp(i, items_len);
let index_buffer = alloy_rlp::encode_fixed_size(&index);
value_buffer.clear();
encode(&items[index], &mut value_buffer);
hb.add_leaf(Nibbles::unpack(&index_buffer), &value_buffer);
}
hb.root()
}
/// 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.