chore: move primitives/trie to reth-trie-types (#8717)

This commit is contained in:
joshieDo
2024-06-10 17:00:14 +02:00
committed by GitHub
parent 76a1a3d005
commit b16a6ec029
59 changed files with 283 additions and 234 deletions

25
Cargo.lock generated
View File

@ -6472,6 +6472,7 @@ dependencies = [
"reth-prune-types",
"reth-storage-errors",
"reth-tracing",
"reth-trie-types",
"rustc-hash",
"serde",
"serde_json",
@ -6504,6 +6505,7 @@ dependencies = [
"reth-primitives",
"reth-prune-types",
"reth-storage-errors",
"reth-trie-types",
"serde",
"serde_json",
"test-fuzz",
@ -7524,6 +7526,7 @@ dependencies = [
"reth-ethereum-forks",
"reth-network-peers",
"reth-static-file-types",
"reth-trie-types",
"revm",
"revm-primitives",
"roaring",
@ -8084,6 +8087,7 @@ dependencies = [
"reth-primitives",
"reth-provider",
"reth-storage-errors",
"reth-trie-types",
"revm",
"serde_json",
"similar-asserts",
@ -8118,6 +8122,27 @@ dependencies = [
"tracing",
]
[[package]]
name = "reth-trie-types"
version = "0.2.0-beta.9"
dependencies = [
"alloy-primitives",
"alloy-rlp",
"alloy-trie",
"arbitrary",
"assert_matches",
"bytes",
"derive_more",
"nybbles",
"proptest",
"proptest-derive",
"reth-codecs",
"serde",
"serde_json",
"test-fuzz",
"toml",
]
[[package]]
name = "revm"
version = "9.0.0"

View File

@ -96,6 +96,7 @@ members = [
"crates/transaction-pool/",
"crates/trie/parallel/",
"crates/trie/trie",
"crates/trie/types",
"examples/beacon-api-sidecar-fetcher/",
"examples/beacon-api-sse/",
"examples/bsc-p2p",
@ -323,6 +324,7 @@ reth-tracing = { path = "crates/tracing" }
reth-transaction-pool = { path = "crates/transaction-pool" }
reth-trie = { path = "crates/trie/trie" }
reth-trie-parallel = { path = "crates/trie/parallel" }
reth-trie-types = { path = "crates/trie/types" }
# revm
revm = { version = "9.0.0", features = [ "std", "secp256k1", "blst", ], default-features = false }

View File

@ -17,6 +17,7 @@ reth-codecs.workspace = true
reth-ethereum-forks.workspace = true
reth-network-peers.workspace = true
reth-static-file-types.workspace = true
reth-trie-types.workspace = true
revm.workspace = true
revm-primitives = { workspace = true, features = ["serde"] }
@ -29,7 +30,6 @@ alloy-trie = { workspace = true, features = ["serde"] }
alloy-rpc-types = { workspace = true, optional = true }
alloy-genesis.workspace = true
alloy-eips = { workspace = true, features = ["serde"] }
nybbles = { workspace = true, features = ["serde", "rlp"] }
# crypto
secp256k1 = { workspace = true, features = [

View File

@ -41,10 +41,8 @@ criterion_main!(benches);
mod implementations {
use super::*;
use alloy_rlp::Encodable;
use reth_primitives::{
proofs::adjust_index_for_rlp,
trie::{HashBuilder, Nibbles},
};
use reth_primitives::proofs::adjust_index_for_rlp;
use reth_trie_types::{HashBuilder, Nibbles};
pub fn trie_hash_ordered_trie_root(receipts: &[ReceiptWithBloom]) -> B256 {
triehash::ordered_trie_root::<KeccakHasher, _>(receipts.iter().map(|receipt| {

View File

@ -1727,9 +1727,8 @@ impl OptimismGenesisInfo {
#[cfg(test)]
mod tests {
use super::*;
use crate::{b256, hex, trie::TrieAccount, ChainConfig, GenesisAccount};
use crate::{b256, hex, proofs::IntoTrieAccount, ChainConfig, GenesisAccount};
use std::{collections::HashMap, str::FromStr};
fn test_fork_ids(spec: &ChainSpec, cases: &[(Head, ForkId)]) {
for (block, expected_id) in cases {
let computed_id = spec.fork_id(block);
@ -2830,7 +2829,10 @@ Post-merge hard forks (timestamp based):
for (key, expected_rlp) in key_rlp {
let account = chainspec.genesis.alloc.get(&key).expect("account should exist");
assert_eq!(&alloy_rlp::encode(TrieAccount::from(account.clone())), expected_rlp)
assert_eq!(
&alloy_rlp::encode(IntoTrieAccount::to_trie_account(account.clone())),
expected_rlp
);
}
assert_eq!(chainspec.genesis_hash, None);

View File

@ -43,7 +43,6 @@ pub mod stage;
pub use reth_static_file_types as static_file;
mod storage;
pub mod transaction;
pub mod trie;
mod withdrawal;
pub use account::{Account, Bytecode};
#[cfg(any(test, feature = "arbitrary"))]

View File

@ -1,12 +1,16 @@
//! Helper function for calculating Merkle proofs and hashes.
use crate::{
constants::EMPTY_OMMER_ROOT_HASH,
keccak256,
trie::{HashBuilder, Nibbles, TrieAccount},
Address, Header, Receipt, ReceiptWithBloom, ReceiptWithBloomRef, Request, TransactionSigned,
Withdrawal, B256, U256,
constants::EMPTY_OMMER_ROOT_HASH, keccak256, Address, Header, Receipt, ReceiptWithBloom,
ReceiptWithBloomRef, Request, TransactionSigned, Withdrawal, B256, U256,
};
use reth_trie_types::{hash_builder::HashBuilder, Nibbles};
mod types;
pub use types::{AccountProof, StorageProof};
mod traits;
pub use traits::IntoTrieAccount;
use alloy_eips::eip7685::Encodable7685;
use alloy_rlp::Encodable;
use itertools::Itertools;
@ -174,7 +178,7 @@ pub fn calculate_ommers_root(ommers: &[Header]) -> B256 {
/// 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<TrieAccount> + Clone + 'a>(
pub fn state_root_ref_unhashed<'a, A: IntoTrieAccount + Clone + 'a>(
state: impl IntoIterator<Item = (&'a Address, &'a A)>,
) -> B256 {
state_root_unsorted(
@ -185,7 +189,7 @@ pub fn state_root_ref_unhashed<'a, A: Into<TrieAccount> + Clone + 'a>(
/// 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<A: Into<TrieAccount>>(
pub fn state_root_unhashed<A: IntoTrieAccount>(
state: impl IntoIterator<Item = (Address, A)>,
) -> B256 {
state_root_unsorted(state.into_iter().map(|(address, account)| (keccak256(address), account)))
@ -193,9 +197,7 @@ pub fn state_root_unhashed<A: Into<TrieAccount>>(
/// 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<A: Into<TrieAccount>>(
state: impl IntoIterator<Item = (B256, A)>,
) -> B256 {
pub fn state_root_unsorted<A: IntoTrieAccount>(state: impl IntoIterator<Item = (B256, A)>) -> B256 {
state_root(state.into_iter().sorted_by_key(|(key, _)| *key))
}
@ -205,12 +207,12 @@ pub fn state_root_unsorted<A: Into<TrieAccount>>(
/// # Panics
///
/// If the items are not in sorted order.
pub fn state_root<A: Into<TrieAccount>>(state: impl IntoIterator<Item = (B256, A)>) -> B256 {
pub fn state_root<A: IntoTrieAccount>(state: impl IntoIterator<Item = (B256, A)>) -> 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);
account.to_trie_account().encode(&mut account_rlp_buf);
hb.add_leaf(Nibbles::unpack(hashed_key), &account_rlp_buf);
}
hb.root()

View File

@ -0,0 +1,59 @@
use crate::Account;
use alloy_consensus::constants::{EMPTY_ROOT_HASH, KECCAK_EMPTY};
use alloy_genesis::GenesisAccount;
use alloy_primitives::{keccak256, B256, U256};
use reth_trie_types::TrieAccount;
use revm_primitives::AccountInfo;
/// Converts a type into a [`TrieAccount`].
pub trait IntoTrieAccount {
/// Converts to this type into a [`TrieAccount`].
fn to_trie_account(self) -> TrieAccount;
}
impl IntoTrieAccount for GenesisAccount {
fn to_trie_account(self) -> TrieAccount {
let storage_root = self
.storage
.map(|storage| {
super::storage_root_unhashed(
storage
.into_iter()
.filter(|(_, value)| *value != B256::ZERO)
.map(|(slot, value)| (slot, U256::from_be_bytes(*value))),
)
})
.unwrap_or(EMPTY_ROOT_HASH);
TrieAccount {
nonce: self.nonce.unwrap_or_default(),
balance: self.balance,
storage_root,
code_hash: self.code.map_or(KECCAK_EMPTY, keccak256),
}
}
}
impl IntoTrieAccount for (Account, B256) {
fn to_trie_account(self) -> TrieAccount {
let (account, storage_root) = self;
TrieAccount {
nonce: account.nonce,
balance: account.balance,
storage_root,
code_hash: account.bytecode_hash.unwrap_or(KECCAK_EMPTY),
}
}
}
impl IntoTrieAccount for (AccountInfo, B256) {
fn to_trie_account(self) -> TrieAccount {
let (account, storage_root) = self;
TrieAccount {
nonce: account.nonce,
balance: account.balance,
storage_root,
code_hash: account.code_hash,
}
}
}

View File

@ -1,12 +1,12 @@
//! Merkle trie proofs.
use super::{
proof::{verify_proof, ProofVerificationError},
Nibbles, TrieAccount,
};
use super::{traits::IntoTrieAccount, Nibbles};
use crate::{keccak256, Account, Address, Bytes, B256, U256};
use alloy_rlp::encode_fixed_size;
use alloy_trie::EMPTY_ROOT_HASH;
use alloy_trie::{
proof::{verify_proof, ProofVerificationError},
EMPTY_ROOT_HASH,
};
/// The merkle proof with the relevant account info.
#[derive(PartialEq, Eq, Debug)]
@ -64,7 +64,7 @@ impl AccountProof {
let expected = if self.info.is_none() && self.storage_root == EMPTY_ROOT_HASH {
None
} else {
Some(alloy_rlp::encode(TrieAccount::from((
Some(alloy_rlp::encode(IntoTrieAccount::to_trie_account((
self.info.unwrap_or_default(),
self.storage_root,
))))

View File

@ -1,9 +1,7 @@
use crate::{
trie::{hash_builder::HashBuilderState, StoredSubNode},
Address, BlockNumber, B256,
};
use crate::{Address, BlockNumber, B256};
use bytes::Buf;
use reth_codecs::{main_codec, Compact};
use reth_trie_types::{hash_builder::HashBuilderState, StoredSubNode};
use std::ops::RangeInclusive;
use super::StageId;

View File

@ -1,71 +0,0 @@
use crate::{
constants::EMPTY_ROOT_HASH, proofs, Account, GenesisAccount, B256, KECCAK_EMPTY, U256,
};
use alloy_primitives::keccak256;
use alloy_rlp::{RlpDecodable, RlpEncodable};
use revm_primitives::AccountInfo;
/// An Ethereum account as represented in the trie.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default, RlpEncodable, RlpDecodable)]
pub struct TrieAccount {
/// Account nonce.
nonce: u64,
/// Account balance.
balance: U256,
/// Account's storage root.
storage_root: B256,
/// Hash of the account's bytecode.
code_hash: B256,
}
impl From<(Account, B256)> for TrieAccount {
fn from((account, storage_root): (Account, B256)) -> Self {
Self {
nonce: account.nonce,
balance: account.balance,
storage_root,
code_hash: account.bytecode_hash.unwrap_or(KECCAK_EMPTY),
}
}
}
impl From<(AccountInfo, B256)> for TrieAccount {
fn from((account, storage_root): (AccountInfo, B256)) -> Self {
Self {
nonce: account.nonce,
balance: account.balance,
storage_root,
code_hash: account.code_hash,
}
}
}
impl From<GenesisAccount> for TrieAccount {
fn from(account: GenesisAccount) -> Self {
let storage_root = account
.storage
.map(|storage| {
proofs::storage_root_unhashed(
storage
.into_iter()
.filter(|(_, value)| *value != B256::ZERO)
.map(|(slot, value)| (slot, U256::from_be_bytes(*value))),
)
})
.unwrap_or(EMPTY_ROOT_HASH);
Self {
nonce: account.nonce.unwrap_or_default(),
balance: account.balance,
storage_root,
code_hash: account.code.map_or(KECCAK_EMPTY, keccak256),
}
}
}
impl TrieAccount {
/// Get account's storage root.
pub const fn storage_root(&self) -> B256 {
self.storage_root
}
}

View File

@ -1,27 +0,0 @@
//! Collection of trie related types.
/// The implementation of hash builder.
pub mod hash_builder;
mod account;
pub use account::TrieAccount;
mod mask;
pub(crate) use mask::StoredTrieMask;
mod nibbles;
pub use nibbles::{Nibbles, StoredNibbles, StoredNibblesSubKey};
pub mod nodes;
pub use nodes::StoredBranchNode;
mod proofs;
pub use proofs::{AccountProof, StorageProof};
mod storage;
pub use storage::StorageTrieEntry;
mod subnode;
pub use subnode::StoredSubNode;
pub use alloy_trie::{proof, BranchNodeCompact, HashBuilder, TrieMask, EMPTY_ROOT_HASH};

View File

@ -1,5 +1,5 @@
use reth_primitives::{
keccak256, trie::AccountProof, Account, Address, BlockNumber, Bytecode, Bytes, StorageKey,
keccak256, proofs::AccountProof, Account, Address, BlockNumber, Bytecode, Bytes, StorageKey,
B256, U256,
};
use reth_storage_api::{AccountReader, BlockHashReader, StateProvider, StateRootProvider};

View File

@ -1,7 +1,7 @@
//! Compatibility functions for rpc proof related types.
use reth_primitives::{
trie::{AccountProof, StorageProof},
proofs::{AccountProof, StorageProof},
U64,
};
use reth_rpc_types::{

View File

@ -3,13 +3,12 @@
use crate::eth::error::{EthApiError, EthResult};
use reth_errors::ProviderError;
use reth_primitives::{
constants::{eip4844::MAX_DATA_GAS_PER_BLOCK, BEACON_NONCE},
constants::{eip4844::MAX_DATA_GAS_PER_BLOCK, BEACON_NONCE, EMPTY_ROOT_HASH},
proofs,
revm::env::tx_env_with_recovered,
revm_primitives::{
BlockEnv, CfgEnvWithHandlerCfg, EVMError, Env, InvalidTransaction, ResultAndState, SpecId,
},
trie::EMPTY_ROOT_HASH,
Block, BlockId, BlockNumberOrTag, ChainSpec, Header, IntoRecoveredTransaction, Receipt,
Requests, SealedBlockWithSenders, SealedHeader, B256, EMPTY_OMMER_ROOT_HASH, U256,
};

View File

@ -7,7 +7,6 @@ use reth_db_api::{
};
use reth_primitives::{
stage::{EntitiesCheckpoint, MerkleCheckpoint, StageCheckpoint, StageId},
trie::StoredSubNode,
BlockNumber, GotExpected, SealedHeader, B256,
};
use reth_provider::{
@ -17,7 +16,7 @@ use reth_provider::{
use reth_stages_api::{
BlockErrorKind, ExecInput, ExecOutput, Stage, StageError, UnwindInput, UnwindOutput,
};
use reth_trie::{IntermediateStateRootState, StateRoot, StateRootProgress};
use reth_trie::{IntermediateStateRootState, StateRoot, StateRootProgress, StoredSubNode};
use std::fmt::Debug;
use tracing::*;

View File

@ -17,6 +17,7 @@ reth-codecs.workspace = true
reth-primitives.workspace = true
reth-prune-types.workspace = true
reth-storage-errors.workspace = true
reth-trie-types.workspace = true
# codecs
modular-bitfield.workspace = true

View File

@ -5,12 +5,9 @@ use crate::{
DatabaseError,
};
use reth_codecs::{main_codec, Compact};
use reth_primitives::{
stage::StageCheckpoint,
trie::{StoredNibbles, StoredNibblesSubKey, *},
Address, B256, *,
};
use reth_primitives::{stage::StageCheckpoint, Address, B256, *};
use reth_prune_types::{PruneCheckpoint, PruneSegment};
use reth_trie_types::{StoredNibbles, StoredNibblesSubKey, *};
pub mod accounts;
pub mod blocks;

View File

@ -24,6 +24,7 @@ reth-libmdbx = { workspace = true, optional = true, features = [
reth-nippy-jar.workspace = true
reth-prune-types.workspace = true
reth-tracing.workspace = true
reth-trie-types.workspace = true
# codecs
serde = { workspace = true, default-features = false }

View File

@ -32,12 +32,11 @@ use reth_db_api::{
table::{Decode, DupSort, Encode, Table},
};
use reth_primitives::{
stage::StageCheckpoint,
trie::{StorageTrieEntry, StoredBranchNode, StoredNibbles, StoredNibblesSubKey},
Account, Address, BlockHash, BlockNumber, Bytecode, Header, IntegerList, Receipt, Requests,
StorageEntry, TransactionSignedNoHash, TxHash, TxNumber, B256,
stage::StageCheckpoint, Account, Address, BlockHash, BlockNumber, Bytecode, Header,
IntegerList, Receipt, Requests, StorageEntry, TransactionSignedNoHash, TxHash, TxNumber, B256,
};
use reth_prune_types::{PruneCheckpoint, PruneSegment};
use reth_trie_types::{StorageTrieEntry, StoredBranchNode, StoredNibbles, StoredNibblesSubKey};
use serde::{Deserialize, Serialize};
use std::fmt;

View File

@ -1,7 +1,7 @@
use crate::{
AccountReader, BlockHashReader, BundleStateDataProvider, StateProvider, StateRootProvider,
};
use reth_primitives::{trie::AccountProof, Account, Address, BlockNumber, Bytecode, B256};
use reth_primitives::{proofs::AccountProof, Account, Address, BlockNumber, Bytecode, B256};
use reth_storage_errors::provider::{ProviderError, ProviderResult};
use reth_trie::updates::TrieUpdates;
use revm::db::BundleState;

View File

@ -33,7 +33,6 @@ use reth_primitives::{
keccak256,
revm::{config::revm_spec, env::fill_block_env},
stage::{StageCheckpoint, StageId},
trie::Nibbles,
Account, Address, Block, BlockHash, BlockHashOrNumber, BlockNumber, BlockWithSenders,
ChainInfo, ChainSpec, GotExpected, Head, Header, Receipt, Requests, SealedBlock,
SealedBlockWithSenders, SealedHeader, StaticFileSegment, StorageEntry, TransactionMeta,
@ -45,7 +44,7 @@ use reth_storage_errors::provider::{ProviderResult, RootMismatch};
use reth_trie::{
prefix_set::{PrefixSet, PrefixSetMut, TriePrefixSets},
updates::TrieUpdates,
HashedPostState, StateRoot,
HashedPostState, Nibbles, StateRoot,
};
use revm::primitives::{BlockEnv, CfgEnvWithHandlerCfg, SpecId};
use std::{

View File

@ -10,7 +10,7 @@ use reth_db_api::{
transaction::DbTx,
};
use reth_primitives::{
constants::EPOCH_SLOTS, trie::AccountProof, Account, Address, BlockNumber, Bytecode,
constants::EPOCH_SLOTS, proofs::AccountProof, Account, Address, BlockNumber, Bytecode,
StaticFileSegment, StorageKey, StorageValue, B256,
};
use reth_storage_errors::provider::ProviderResult;

View File

@ -8,7 +8,7 @@ use reth_db_api::{
transaction::DbTx,
};
use reth_primitives::{
trie::AccountProof, Account, Address, BlockNumber, Bytecode, StaticFileSegment, StorageKey,
proofs::AccountProof, Account, Address, BlockNumber, Bytecode, StaticFileSegment, StorageKey,
StorageValue, B256,
};
use reth_storage_errors::provider::{ProviderError, ProviderResult};

View File

@ -43,7 +43,7 @@ macro_rules! delegate_provider_impls {
}
StateProvider $(where [$($generics)*])?{
fn storage(&self, account: reth_primitives::Address, storage_key: reth_primitives::StorageKey) -> reth_storage_errors::provider::ProviderResult<Option<reth_primitives::StorageValue>>;
fn proof(&self, address: reth_primitives::Address, keys: &[reth_primitives::B256]) -> reth_storage_errors::provider::ProviderResult<reth_primitives::trie::AccountProof>;
fn proof(&self, address: reth_primitives::Address, keys: &[reth_primitives::B256]) -> reth_storage_errors::provider::ProviderResult<reth_primitives::proofs::AccountProof>;
fn bytecode_by_hash(&self, code_hash: reth_primitives::B256) -> reth_storage_errors::provider::ProviderResult<Option<reth_primitives::Bytecode>>;
}
);

View File

@ -10,8 +10,8 @@ use parking_lot::Mutex;
use reth_db_api::models::{AccountBeforeTx, StoredBlockBodyIndices};
use reth_evm::ConfigureEvmEnv;
use reth_primitives::{
keccak256, trie::AccountProof, Account, Address, Block, BlockHash, BlockHashOrNumber, BlockId,
BlockNumber, BlockWithSenders, Bytecode, Bytes, ChainInfo, ChainSpec, Header, Receipt,
keccak256, proofs::AccountProof, Account, Address, Block, BlockHash, BlockHashOrNumber,
BlockId, BlockNumber, BlockWithSenders, Bytecode, Bytes, ChainInfo, ChainSpec, Header, Receipt,
SealedBlock, SealedBlockWithSenders, SealedHeader, StorageKey, StorageValue, TransactionMeta,
TransactionSigned, TransactionSignedNoHash, TxHash, TxNumber, Withdrawal, Withdrawals, B256,
U256,

View File

@ -9,8 +9,8 @@ use crate::{
use reth_db_api::models::{AccountBeforeTx, StoredBlockBodyIndices};
use reth_evm::ConfigureEvmEnv;
use reth_primitives::{
proofs::AccountProof,
stage::{StageCheckpoint, StageId},
trie::AccountProof,
Account, Address, Block, BlockHash, BlockHashOrNumber, BlockId, BlockNumber, BlockWithSenders,
Bytecode, ChainInfo, ChainSpec, Header, Receipt, SealedBlock, SealedBlockWithSenders,
SealedHeader, StorageKey, StorageValue, TransactionMeta, TransactionSigned,

View File

@ -2,7 +2,7 @@ use super::{AccountReader, BlockHashReader, BlockIdReader, StateRootProvider};
use auto_impl::auto_impl;
use reth_execution_types::BundleStateWithReceipts;
use reth_primitives::{
trie::AccountProof, Address, BlockHash, BlockId, BlockNumHash, BlockNumber, BlockNumberOrTag,
proofs::AccountProof, Address, BlockHash, BlockId, BlockNumHash, BlockNumber, BlockNumberOrTag,
Bytecode, StorageKey, StorageValue, B256, KECCAK_EMPTY, U256,
};
use reth_storage_errors::provider::{ProviderError, ProviderResult};

View File

@ -3,10 +3,7 @@ use alloy_rlp::{BufMut, Encodable};
use itertools::Itertools;
use reth_db_api::database::Database;
use reth_execution_errors::StorageRootError;
use reth_primitives::{
trie::{HashBuilder, Nibbles, TrieAccount},
B256,
};
use reth_primitives::{proofs::IntoTrieAccount, B256};
use reth_provider::{providers::ConsistentDbView, DatabaseProviderFactory, ProviderError};
use reth_tasks::pool::BlockingTaskPool;
use reth_trie::{
@ -15,7 +12,7 @@ use reth_trie::{
trie_cursor::TrieCursorFactory,
updates::TrieUpdates,
walker::TrieWalker,
HashedPostState, StorageRoot,
HashBuilder, HashedPostState, Nibbles, StorageRoot,
};
use std::{collections::HashMap, sync::Arc};
use thiserror::Error;
@ -173,7 +170,7 @@ where
}
account_rlp.clear();
let account = TrieAccount::from((account, storage_root));
let account = IntoTrieAccount::to_trie_account((account, storage_root));
account.encode(&mut account_rlp as &mut dyn BufMut);
hash_builder.add_leaf(Nibbles::unpack(hashed_address), &account_rlp);
}

View File

@ -3,10 +3,7 @@ use alloy_rlp::{BufMut, Encodable};
use rayon::prelude::*;
use reth_db_api::database::Database;
use reth_execution_errors::StorageRootError;
use reth_primitives::{
trie::{HashBuilder, Nibbles, TrieAccount},
B256,
};
use reth_primitives::{proofs::IntoTrieAccount, B256};
use reth_provider::{providers::ConsistentDbView, DatabaseProviderFactory, ProviderError};
use reth_trie::{
hashed_cursor::{HashedCursorFactory, HashedPostStateCursorFactory},
@ -14,7 +11,7 @@ use reth_trie::{
trie_cursor::TrieCursorFactory,
updates::TrieUpdates,
walker::TrieWalker,
HashedPostState, StorageRoot,
HashBuilder, HashedPostState, Nibbles, StorageRoot,
};
use std::collections::HashMap;
use thiserror::Error;
@ -155,7 +152,7 @@ where
}
account_rlp.clear();
let account = TrieAccount::from((account, storage_root));
let account = IntoTrieAccount::to_trie_account((account, storage_root));
account.encode(&mut account_rlp as &mut dyn BufMut);
hash_builder.add_leaf(Nibbles::unpack(hashed_address), &account_rlp);
}

View File

@ -17,6 +17,7 @@ reth-primitives.workspace = true
reth-execution-errors.workspace = true
reth-db.workspace = true
reth-db-api.workspace = true
reth-trie-types.workspace = true
revm.workspace = true

View File

@ -7,8 +7,7 @@ use proptest::{
strategy::ValueTree,
test_runner::{basic_result_cache, TestRunner},
};
use reth_primitives::trie::Nibbles;
use reth_trie::prefix_set::PrefixSetMut;
use reth_trie::{prefix_set::PrefixSetMut, Nibbles};
use std::collections::BTreeSet;
/// Abstractions used for benching

View File

@ -50,6 +50,9 @@ pub use progress::{IntermediateStateRootState, StateRootProgress};
/// Trie calculation stats.
pub mod stats;
// re-export for convenience
pub use reth_trie_types::*;
/// Trie calculation metrics.
#[cfg(feature = "metrics")]
pub mod metrics;

View File

@ -1,6 +1,6 @@
use crate::{hashed_cursor::HashedCursor, trie_cursor::TrieCursor, walker::TrieWalker};
use crate::{hashed_cursor::HashedCursor, trie_cursor::TrieCursor, walker::TrieWalker, Nibbles};
use reth_db::DatabaseError;
use reth_primitives::{trie::Nibbles, B256};
use reth_primitives::B256;
/// Represents a branch node in the trie.
#[derive(Debug)]

View File

@ -1,4 +1,5 @@
use super::{PrefixSetMut, TriePrefixSets};
use crate::Nibbles;
use derive_more::Deref;
use reth_db::tables;
use reth_db_api::{
@ -7,12 +8,11 @@ use reth_db_api::{
transaction::DbTx,
DatabaseError,
};
use reth_primitives::{keccak256, trie::Nibbles, BlockNumber, StorageEntry, B256};
use reth_primitives::{keccak256, BlockNumber, StorageEntry, B256};
use std::{
collections::{HashMap, HashSet},
ops::RangeInclusive,
};
/// A wrapper around a database transaction that loads prefix sets within a given block range.
#[derive(Deref, Debug)]
pub struct PrefixSetLoader<'a, TX>(&'a TX);

View File

@ -1,4 +1,5 @@
use reth_primitives::{trie::Nibbles, B256};
use crate::Nibbles;
use reth_primitives::B256;
use std::{
collections::{HashMap, HashSet},
sync::Arc,
@ -37,8 +38,7 @@ pub struct TriePrefixSets {
/// # Examples
///
/// ```
/// use reth_primitives::trie::Nibbles;
/// use reth_trie::prefix_set::PrefixSetMut;
/// use reth_trie::{prefix_set::PrefixSetMut, Nibbles};
///
/// let mut prefix_set = PrefixSetMut::default();
/// prefix_set.insert(Nibbles::from_nibbles_unchecked(&[0xa, 0xb]));

View File

@ -1,5 +1,5 @@
use crate::{trie_cursor::CursorSubNode, updates::TrieUpdates};
use reth_primitives::{stage::MerkleCheckpoint, trie::hash_builder::HashBuilder, B256};
use crate::{hash_builder::HashBuilder, trie_cursor::CursorSubNode, updates::TrieUpdates};
use reth_primitives::{stage::MerkleCheckpoint, B256};
/// The progress of the state root computation.
#[derive(Debug)]

View File

@ -4,6 +4,7 @@ use crate::{
prefix_set::PrefixSetMut,
trie_cursor::{DatabaseAccountTrieCursor, DatabaseStorageTrieCursor},
walker::TrieWalker,
HashBuilder, Nibbles,
};
use alloy_rlp::{BufMut, Encodable};
use reth_db::tables;
@ -12,10 +13,10 @@ use reth_execution_errors::{StateRootError, StorageRootError};
use reth_primitives::{
constants::EMPTY_ROOT_HASH,
keccak256,
trie::{proof::ProofRetainer, AccountProof, HashBuilder, Nibbles, StorageProof, TrieAccount},
proofs::{AccountProof, IntoTrieAccount, StorageProof},
Address, B256,
};
use reth_trie_types::proof::ProofRetainer;
/// A struct for generating merkle proofs.
///
/// Proof generator adds the target address and slots to the prefix set, enables the proof retainer
@ -82,7 +83,7 @@ where
};
account_rlp.clear();
let account = TrieAccount::from((account, storage_root));
let account = IntoTrieAccount::to_trie_account((account, storage_root));
account.encode(&mut account_rlp as &mut dyn BufMut);
hash_builder.add_leaf(Nibbles::unpack(hashed_address), &account_rlp);

View File

@ -2,7 +2,7 @@ use crate::{
hashed_cursor::HashedPostStateCursorFactory,
prefix_set::{PrefixSetMut, TriePrefixSets},
updates::TrieUpdates,
StateRoot,
Nibbles, StateRoot,
};
use rayon::prelude::{IntoParallelIterator, ParallelIterator};
use reth_db::{tables, DatabaseError};
@ -13,8 +13,7 @@ use reth_db_api::{
};
use reth_execution_errors::StateRootError;
use reth_primitives::{
keccak256, revm::compat::into_reth_acc, trie::Nibbles, Account, Address, BlockNumber, B256,
U256,
keccak256, revm::compat::into_reth_acc, Account, Address, BlockNumber, B256, U256,
};
use revm::db::BundleAccount;
use std::{

View File

@ -1,6 +1,7 @@
use alloy_rlp::encode_fixed_size;
use reth_primitives::{
proofs::triehash::KeccakHasher, trie::TrieAccount, Account, Address, B256, U256,
proofs::{triehash::KeccakHasher, IntoTrieAccount},
Account, Address, B256, U256,
};
/// Re-export of [triehash].
@ -14,7 +15,7 @@ where
{
let encoded_accounts = accounts.into_iter().map(|(address, (account, storage))| {
let storage_root = storage_root(storage);
let account = TrieAccount::from((account, storage_root));
let account = IntoTrieAccount::to_trie_account((account, storage_root));
(address, alloy_rlp::encode(account))
});
triehash::sec_trie_root::<KeccakHasher, _, _, _>(encoded_accounts)
@ -35,7 +36,7 @@ where
{
let encoded_accounts = accounts.into_iter().map(|(address, (account, storage))| {
let storage_root = storage_root_prehashed(storage);
let account = TrieAccount::from((account, storage_root));
let account = IntoTrieAccount::to_trie_account((account, storage_root));
(address, alloy_rlp::encode(account))
});

View File

@ -7,15 +7,13 @@ use crate::{
trie_cursor::TrieCursorFactory,
updates::{TrieKey, TrieOp, TrieUpdates},
walker::TrieWalker,
HashBuilder, Nibbles,
};
use alloy_rlp::{BufMut, Encodable};
use reth_db_api::transaction::DbTx;
use reth_execution_errors::{StateRootError, StorageRootError};
use reth_primitives::{
constants::EMPTY_ROOT_HASH,
keccak256,
trie::{HashBuilder, Nibbles, TrieAccount},
Address, BlockNumber, B256,
constants::EMPTY_ROOT_HASH, keccak256, proofs::IntoTrieAccount, Address, BlockNumber, B256,
};
use std::ops::RangeInclusive;
use tracing::{debug, trace};
@ -284,7 +282,7 @@ where
};
account_rlp.clear();
let account = TrieAccount::from((account, storage_root));
let account = IntoTrieAccount::to_trie_account((account, storage_root));
account.encode(&mut account_rlp as &mut dyn BufMut);
hash_builder.add_leaf(Nibbles::unpack(hashed_address), &account_rlp);
@ -552,6 +550,7 @@ mod tests {
use crate::{
prefix_set::PrefixSetMut,
test_utils::{state_root, state_root_prehashed, storage_root, storage_root_prehashed},
BranchNodeCompact, TrieMask,
};
use proptest::{prelude::ProptestConfig, proptest};
use reth_db::{tables, test_utils::TempDatabase, DatabaseEnv};
@ -560,10 +559,7 @@ mod tests {
transaction::DbTxMut,
};
use reth_primitives::{
hex_literal::hex,
proofs::triehash::KeccakHasher,
trie::{BranchNodeCompact, TrieMask},
Account, StorageEntry, U256,
hex_literal::hex, proofs::triehash::KeccakHasher, Account, StorageEntry, U256,
};
use reth_provider::{test_utils::create_test_provider_factory, DatabaseProviderRW};
use std::{
@ -832,7 +828,8 @@ mod tests {
}
fn encode_account(account: Account, storage_root: Option<B256>) -> Vec<u8> {
let account = TrieAccount::from((account, storage_root.unwrap_or(EMPTY_ROOT_HASH)));
let account =
IntoTrieAccount::to_trie_account((account, storage_root.unwrap_or(EMPTY_ROOT_HASH)));
let mut account_rlp = Vec::with_capacity(account.length());
account.encode(&mut account_rlp);
account_rlp

View File

@ -1,14 +1,11 @@
use super::{TrieCursor, TrieCursorFactory};
use crate::updates::TrieKey;
use crate::{updates::TrieKey, BranchNodeCompact, Nibbles, StoredNibbles, StoredNibblesSubKey};
use reth_db::{tables, DatabaseError};
use reth_db_api::{
cursor::{DbCursorRO, DbDupCursorRO},
transaction::DbTx,
};
use reth_primitives::{
trie::{BranchNodeCompact, Nibbles, StoredNibbles, StoredNibblesSubKey},
B256,
};
use reth_primitives::B256;
/// Implementation of the trie cursor factory for a database transaction.
impl<'a, TX: DbTx> TrieCursorFactory for &'a TX {
@ -116,11 +113,9 @@ where
#[cfg(test)]
mod tests {
use super::*;
use crate::{StorageTrieEntry, StoredBranchNode};
use reth_db_api::{cursor::DbCursorRW, transaction::DbTxMut};
use reth_primitives::{
hex_literal::hex,
trie::{StorageTrieEntry, StoredBranchNode},
};
use reth_primitives::hex_literal::hex;
use reth_provider::test_utils::create_test_provider_factory;
#[test]

View File

@ -1,10 +1,6 @@
use crate::updates::TrieKey;
use crate::{updates::TrieKey, BranchNodeCompact, Nibbles};
use reth_db::DatabaseError;
use reth_primitives::{
trie::{BranchNodeCompact, Nibbles},
B256,
};
use reth_primitives::B256;
mod database_cursors;
mod subnode;

View File

@ -1,7 +1,6 @@
use super::{TrieCursor, TrieCursorFactory};
use crate::updates::TrieKey;
use crate::{updates::TrieKey, BranchNodeCompact, Nibbles};
use reth_db::DatabaseError;
use reth_primitives::trie::{BranchNodeCompact, Nibbles};
/// Noop trie cursor factory.
#[derive(Default, Debug)]

View File

@ -1,7 +1,5 @@
use reth_primitives::{
trie::{nodes::CHILD_INDEX_RANGE, BranchNodeCompact, Nibbles, StoredSubNode},
B256,
};
use crate::{nodes::CHILD_INDEX_RANGE, BranchNodeCompact, Nibbles, StoredSubNode};
use reth_primitives::B256;
/// Cursor for iterating over a subtrie.
#[derive(Clone)]

View File

@ -1,17 +1,14 @@
use crate::walker::TrieWalker;
use crate::{
walker::TrieWalker, BranchNodeCompact, HashBuilder, Nibbles, StorageTrieEntry,
StoredBranchNode, StoredNibbles, StoredNibblesSubKey,
};
use derive_more::Deref;
use reth_db::tables;
use reth_db_api::{
cursor::{DbCursorRO, DbCursorRW, DbDupCursorRO, DbDupCursorRW},
transaction::{DbTx, DbTxMut},
};
use reth_primitives::{
trie::{
BranchNodeCompact, HashBuilder, Nibbles, StorageTrieEntry, StoredBranchNode, StoredNibbles,
StoredNibblesSubKey,
},
B256,
};
use reth_primitives::B256;
use std::collections::{hash_map::IntoIter, HashMap, HashSet};
/// The key of a trie node.

View File

@ -2,12 +2,10 @@ use crate::{
prefix_set::PrefixSet,
trie_cursor::{CursorSubNode, TrieCursor},
updates::TrieUpdates,
BranchNodeCompact, Nibbles,
};
use reth_db::DatabaseError;
use reth_primitives::{
trie::{BranchNodeCompact, Nibbles},
B256,
};
use reth_primitives::B256;
/// `TrieWalker` is a structure that enables traversal of a Merkle trie.
/// It allows moving through the trie in a depth-first manner, skipping certain branches
@ -249,10 +247,10 @@ mod tests {
use crate::{
prefix_set::PrefixSetMut,
trie_cursor::{DatabaseAccountTrieCursor, DatabaseStorageTrieCursor},
StorageTrieEntry, StoredBranchNode,
};
use reth_db::tables;
use reth_db_api::{cursor::DbCursorRW, transaction::DbTxMut};
use reth_primitives::trie::{StorageTrieEntry, StoredBranchNode};
use reth_provider::test_utils::create_test_provider_factory;
#[test]

View File

@ -0,0 +1,33 @@
[package]
name = "reth-trie-types"
version.workspace = true
edition.workspace = true
homepage.workspace = true
license.workspace = true
repository.workspace = true
rust-version.workspace = true
description = "Commonly used types for trie usage in reth."
[lints]
workspace = true
[dependencies]
reth-codecs.workspace = true
alloy-primitives.workspace = true
alloy-rlp = { workspace = true, features = ["arrayvec"] }
alloy-trie = { workspace = true, features = ["serde"] }
bytes.workspace = true
derive_more.workspace = true
serde.workspace = true
nybbles = { workspace = true, features = ["serde", "rlp"] }
[dev-dependencies]
arbitrary = { workspace = true, features = ["derive"] }
assert_matches.workspace = true
proptest.workspace = true
proptest-derive.workspace = true
serde_json.workspace = true
test-fuzz.workspace = true
toml.workspace = true

View File

@ -0,0 +1,22 @@
use alloy_primitives::{B256, U256};
use alloy_rlp::{RlpDecodable, RlpEncodable};
/// An Ethereum account as represented in the trie.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default, RlpEncodable, RlpDecodable)]
pub struct TrieAccount {
/// Account nonce.
pub nonce: u64,
/// Account balance.
pub balance: U256,
/// Account's storage root.
pub storage_root: B256,
/// Hash of the account's bytecode.
pub code_hash: B256,
}
impl TrieAccount {
/// Get account's storage root.
pub const fn storage_root(&self) -> B256 {
self.storage_root
}
}

View File

@ -1,5 +1,5 @@
use super::StoredHashBuilderValue;
use crate::trie::{StoredTrieMask, TrieMask};
use crate::{StoredTrieMask, TrieMask};
use alloy_trie::{hash_builder::HashBuilderValue, HashBuilder};
use bytes::Buf;
use nybbles::Nibbles;

View File

@ -0,0 +1,34 @@
//! Commonly used types for trie usage.
#![doc(
html_logo_url = "https://raw.githubusercontent.com/paradigmxyz/reth/main/assets/reth-docs.png",
html_favicon_url = "https://avatars0.githubusercontent.com/u/97369466?s=256",
issue_tracker_base_url = "https://github.com/paradigmxyz/reth/issues/"
)]
#![cfg_attr(not(test), warn(unused_crate_dependencies))]
// TODO: remove when https://github.com/proptest-rs/proptest/pull/427 is merged
#![allow(unknown_lints, non_local_definitions)]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
/// The implementation of hash builder.
pub mod hash_builder;
mod account;
pub use account::TrieAccount;
mod mask;
pub(crate) use mask::StoredTrieMask;
mod nibbles;
pub use nibbles::{Nibbles, StoredNibbles, StoredNibblesSubKey};
pub mod nodes;
pub use nodes::StoredBranchNode;
mod storage;
pub use storage::StorageTrieEntry;
mod subnode;
pub use subnode::StoredSubNode;
pub use alloy_trie::{proof, BranchNodeCompact, HashBuilder, TrieMask, EMPTY_ROOT_HASH};

View File

@ -1,4 +1,4 @@
use crate::trie::StoredTrieMask;
use crate::StoredTrieMask;
use alloy_primitives::B256;
use alloy_trie::BranchNodeCompact;
use bytes::Buf;

View File

@ -3,7 +3,6 @@ use bytes::Buf;
use reth_codecs::Compact;
/// Walker sub node for storing intermediate state root calculation state in the database.
/// See [`crate::stage::MerkleCheckpoint`].
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct StoredSubNode {
/// The key of the current node.
@ -70,7 +69,8 @@ impl Compact for StoredSubNode {
#[cfg(test)]
mod tests {
use super::*;
use crate::{trie::TrieMask, B256};
use crate::TrieMask;
use alloy_primitives::B256;
#[test]
fn subnode_roundtrip() {