Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: move trie functions to alloy #12438

Merged
merged 3 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/optimism/consensus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ reth-optimism-chainspec.workspace = true
# ethereum
alloy-primitives.workspace = true
alloy-consensus.workspace = true
alloy-trie.workspace = true

tracing.workspace = true

Expand Down
2 changes: 1 addition & 1 deletion crates/optimism/consensus/src/proof.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//! Helper function for Receipt root calculation for Optimism hardforks.

use alloy_primitives::B256;
use alloy_trie::root::ordered_trie_root_with_encoder;
use reth_chainspec::ChainSpec;
use reth_optimism_forks::OpHardfork;
use reth_primitives::{Receipt, ReceiptWithBloom, ReceiptWithBloomRef};
use reth_trie_common::root::ordered_trie_root_with_encoder;

/// Calculates the receipt root for a header.
pub(crate) fn calculate_receipt_root_optimism(
Expand Down
9 changes: 6 additions & 3 deletions crates/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ workspace = true
reth-primitives-traits.workspace = true
reth-ethereum-forks.workspace = true
reth-static-file-types.workspace = true
reth-trie-common.workspace = true
revm-primitives = { workspace = true, features = ["serde"] }
reth-codecs = { workspace = true, optional = true }

Expand All @@ -28,6 +27,7 @@ alloy-rlp = { workspace = true, features = ["arrayvec"] }
alloy-rpc-types = { workspace = true, optional = true }
alloy-serde = { workspace = true, optional = true }
alloy-eips = { workspace = true, features = ["serde"] }
alloy-trie = { workspace = true, features = ["serde"] }

# optimism
op-alloy-rpc-types = { workspace = true, optional = true }
Expand Down Expand Up @@ -66,6 +66,7 @@ reth-chainspec.workspace = true
reth-codecs = { workspace = true, features = ["test-utils"] }
reth-primitives-traits = { workspace = true, features = ["arbitrary"] }
reth-testing-utils.workspace = true
reth-trie-common.workspace = true
revm-primitives = { workspace = true, features = ["arbitrary"] }

alloy-eips = { workspace = true, features = ["arbitrary"] }
Expand Down Expand Up @@ -102,6 +103,7 @@ std = [
"revm-primitives/std",
"secp256k1?/std",
"serde/std",
"alloy-trie/std"
]
reth-codec = ["dep:reth-codecs", "dep:zstd", "dep:modular-bitfield", "std"]
asm-keccak = ["alloy-primitives/asm-keccak", "revm-primitives/asm-keccak"]
Expand All @@ -115,14 +117,15 @@ arbitrary = [
"revm-primitives/arbitrary",
"secp256k1",
"reth-chainspec/arbitrary",
"reth-trie-common/arbitrary",
"alloy-consensus/arbitrary",
"alloy-primitives/arbitrary",
"alloy-rpc-types?/arbitrary",
"alloy-serde?/arbitrary",
"op-alloy-consensus?/arbitrary",
"op-alloy-rpc-types?/arbitrary",
"reth-codecs?/arbitrary"
"reth-codecs?/arbitrary",
"alloy-trie/arbitrary",
"reth-trie-common/arbitrary"
]
secp256k1 = ["dep:secp256k1"]
c-kzg = [
Expand Down
2 changes: 1 addition & 1 deletion crates/primitives/src/proofs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use alloc::vec::Vec;
use alloy_consensus::EMPTY_OMMER_ROOT_HASH;
use alloy_eips::{eip2718::Encodable2718, eip4895::Withdrawal};
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.
///
Expand Down
3 changes: 2 additions & 1 deletion crates/trie/common/src/proofs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
32 changes: 0 additions & 32 deletions crates/trie/common/src/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 3 additions & 1 deletion crates/trie/trie/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ revm.workspace = true
alloy-rlp.workspace = true
alloy-primitives.workspace = true
alloy-consensus.workspace = true
alloy-trie.workspace = true

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