mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
chore: move trie functions to alloy (#12438)
This commit is contained in:
@ -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;
|
||||
|
||||
|
||||
@ -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.
|
||||
|
||||
Reference in New Issue
Block a user