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

3
Cargo.lock generated
View File

@ -8191,6 +8191,7 @@ version = "1.1.1"
dependencies = [ dependencies = [
"alloy-consensus", "alloy-consensus",
"alloy-primitives", "alloy-primitives",
"alloy-trie",
"reth-chainspec", "reth-chainspec",
"reth-consensus", "reth-consensus",
"reth-consensus-common", "reth-consensus-common",
@ -8451,6 +8452,7 @@ dependencies = [
"alloy-rlp", "alloy-rlp",
"alloy-rpc-types", "alloy-rpc-types",
"alloy-serde", "alloy-serde",
"alloy-trie",
"arbitrary", "arbitrary",
"assert_matches", "assert_matches",
"bincode", "bincode",
@ -9226,6 +9228,7 @@ dependencies = [
"alloy-consensus", "alloy-consensus",
"alloy-primitives", "alloy-primitives",
"alloy-rlp", "alloy-rlp",
"alloy-trie",
"auto_impl", "auto_impl",
"bincode", "bincode",
"criterion", "criterion",

View File

@ -26,6 +26,7 @@ reth-optimism-chainspec.workspace = true
# ethereum # ethereum
alloy-primitives.workspace = true alloy-primitives.workspace = true
alloy-consensus.workspace = true alloy-consensus.workspace = true
alloy-trie.workspace = true
tracing.workspace = true tracing.workspace = true

View File

@ -1,10 +1,10 @@
//! Helper function for Receipt root calculation for Optimism hardforks. //! Helper function for Receipt root calculation for Optimism hardforks.
use alloy_primitives::B256; use alloy_primitives::B256;
use alloy_trie::root::ordered_trie_root_with_encoder;
use reth_chainspec::ChainSpec; use reth_chainspec::ChainSpec;
use reth_optimism_forks::OpHardfork; use reth_optimism_forks::OpHardfork;
use reth_primitives::{Receipt, ReceiptWithBloom, ReceiptWithBloomRef}; use reth_primitives::{Receipt, ReceiptWithBloom, ReceiptWithBloomRef};
use reth_trie_common::root::ordered_trie_root_with_encoder;
/// Calculates the receipt root for a header. /// Calculates the receipt root for a header.
pub(crate) fn calculate_receipt_root_optimism( pub(crate) fn calculate_receipt_root_optimism(

View File

@ -16,7 +16,6 @@ workspace = true
reth-primitives-traits.workspace = true reth-primitives-traits.workspace = true
reth-ethereum-forks.workspace = true reth-ethereum-forks.workspace = true
reth-static-file-types.workspace = true reth-static-file-types.workspace = true
reth-trie-common.workspace = true
revm-primitives = { workspace = true, features = ["serde"] } revm-primitives = { workspace = true, features = ["serde"] }
reth-codecs = { workspace = true, optional = true } reth-codecs = { workspace = true, optional = true }
@ -28,6 +27,7 @@ alloy-rlp = { workspace = true, features = ["arrayvec"] }
alloy-rpc-types = { workspace = true, optional = true } alloy-rpc-types = { workspace = true, optional = true }
alloy-serde = { workspace = true, optional = true } alloy-serde = { workspace = true, optional = true }
alloy-eips = { workspace = true, features = ["serde"] } alloy-eips = { workspace = true, features = ["serde"] }
alloy-trie = { workspace = true, features = ["serde"] }
# optimism # optimism
op-alloy-rpc-types = { workspace = true, optional = true } op-alloy-rpc-types = { workspace = true, optional = true }
@ -66,6 +66,7 @@ reth-chainspec.workspace = true
reth-codecs = { workspace = true, features = ["test-utils"] } reth-codecs = { workspace = true, features = ["test-utils"] }
reth-primitives-traits = { workspace = true, features = ["arbitrary"] } reth-primitives-traits = { workspace = true, features = ["arbitrary"] }
reth-testing-utils.workspace = true reth-testing-utils.workspace = true
reth-trie-common.workspace = true
revm-primitives = { workspace = true, features = ["arbitrary"] } revm-primitives = { workspace = true, features = ["arbitrary"] }
alloy-eips = { workspace = true, features = ["arbitrary"] } alloy-eips = { workspace = true, features = ["arbitrary"] }
@ -102,6 +103,7 @@ std = [
"revm-primitives/std", "revm-primitives/std",
"secp256k1?/std", "secp256k1?/std",
"serde/std", "serde/std",
"alloy-trie/std"
] ]
reth-codec = ["dep:reth-codecs", "dep:zstd", "dep:modular-bitfield", "std"] reth-codec = ["dep:reth-codecs", "dep:zstd", "dep:modular-bitfield", "std"]
asm-keccak = ["alloy-primitives/asm-keccak", "revm-primitives/asm-keccak"] asm-keccak = ["alloy-primitives/asm-keccak", "revm-primitives/asm-keccak"]
@ -115,14 +117,15 @@ arbitrary = [
"revm-primitives/arbitrary", "revm-primitives/arbitrary",
"secp256k1", "secp256k1",
"reth-chainspec/arbitrary", "reth-chainspec/arbitrary",
"reth-trie-common/arbitrary",
"alloy-consensus/arbitrary", "alloy-consensus/arbitrary",
"alloy-primitives/arbitrary", "alloy-primitives/arbitrary",
"alloy-rpc-types?/arbitrary", "alloy-rpc-types?/arbitrary",
"alloy-serde?/arbitrary", "alloy-serde?/arbitrary",
"op-alloy-consensus?/arbitrary", "op-alloy-consensus?/arbitrary",
"op-alloy-rpc-types?/arbitrary", "op-alloy-rpc-types?/arbitrary",
"reth-codecs?/arbitrary" "reth-codecs?/arbitrary",
"alloy-trie/arbitrary",
"reth-trie-common/arbitrary"
] ]
secp256k1 = ["dep:secp256k1"] secp256k1 = ["dep:secp256k1"]
c-kzg = [ c-kzg = [

View File

@ -5,7 +5,7 @@ use alloc::vec::Vec;
use alloy_consensus::EMPTY_OMMER_ROOT_HASH; use alloy_consensus::EMPTY_OMMER_ROOT_HASH;
use alloy_eips::{eip2718::Encodable2718, eip4895::Withdrawal}; use alloy_eips::{eip2718::Encodable2718, eip4895::Withdrawal};
use alloy_primitives::{keccak256, B256}; use alloy_primitives::{keccak256, B256};
use reth_trie_common::root::{ordered_trie_root, ordered_trie_root_with_encoder}; use alloy_trie::root::{ordered_trie_root, ordered_trie_root_with_encoder};
/// Calculate a transaction root. /// Calculate a transaction root.
/// ///

View File

@ -234,11 +234,12 @@ impl StorageProof {
#[cfg(any(test, feature = "test-utils"))] #[cfg(any(test, feature = "test-utils"))]
pub mod triehash { pub mod triehash {
use alloy_primitives::{keccak256, B256}; use alloy_primitives::{keccak256, B256};
use alloy_rlp::RlpEncodable;
use hash_db::Hasher; use hash_db::Hasher;
use plain_hasher::PlainHasher; use plain_hasher::PlainHasher;
/// A [Hasher] that calculates a keccak256 hash of the given data. /// 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] #[non_exhaustive]
pub struct KeccakHasher; 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 /// Hashes and sorts account keys, then proceeds to calculating the root hash of the state
/// represented as MPT. /// represented as MPT.
/// See [`state_root_unsorted`] for more info. /// See [`state_root_unsorted`] for more info.

View File

@ -25,6 +25,7 @@ revm.workspace = true
alloy-rlp.workspace = true alloy-rlp.workspace = true
alloy-primitives.workspace = true alloy-primitives.workspace = true
alloy-consensus.workspace = true alloy-consensus.workspace = true
alloy-trie.workspace = true
# tracing # tracing
tracing.workspace = true tracing.workspace = true
@ -68,7 +69,8 @@ serde = [
"dep:serde", "dep:serde",
"alloy-consensus/serde", "alloy-consensus/serde",
"alloy-primitives/serde", "alloy-primitives/serde",
"revm/serde" "revm/serde",
"alloy-trie/serde"
] ]
serde-bincode-compat = [ serde-bincode-compat = [
"serde_with", "serde_with",