From b16a6ec029effaf2c1742614d324d9fee81b5fa8 Mon Sep 17 00:00:00 2001 From: joshieDo <93316087+joshieDo@users.noreply.github.com> Date: Mon, 10 Jun 2024 17:00:14 +0200 Subject: [PATCH] chore: move `primitives/trie` to `reth-trie-types` (#8717) --- Cargo.lock | 25 +++++++ Cargo.toml | 2 + crates/primitives/Cargo.toml | 2 +- crates/primitives/benches/trie_root.rs | 6 +- crates/primitives/src/chain/spec.rs | 8 ++- crates/primitives/src/lib.rs | 1 - .../src/{proofs.rs => proofs/mod.rs} | 26 +++---- crates/primitives/src/proofs/traits.rs | 59 +++++++++++++++ .../src/{trie/proofs.rs => proofs/types.rs} | 12 ++-- crates/primitives/src/stage/checkpoints.rs | 6 +- crates/primitives/src/trie/account.rs | 71 ------------------- crates/primitives/src/trie/mod.rs | 27 ------- crates/revm/src/test_utils.rs | 2 +- crates/rpc/rpc-types-compat/src/proof.rs | 2 +- crates/rpc/rpc/src/eth/api/pending_block.rs | 3 +- crates/stages/stages/src/stages/merkle.rs | 3 +- crates/storage/db-api/Cargo.toml | 1 + crates/storage/db-api/src/models/mod.rs | 7 +- crates/storage/db/Cargo.toml | 1 + crates/storage/db/src/tables/mod.rs | 7 +- .../src/providers/bundle_state_provider.rs | 2 +- .../src/providers/database/provider.rs | 3 +- .../src/providers/state/historical.rs | 2 +- .../provider/src/providers/state/latest.rs | 2 +- .../provider/src/providers/state/macros.rs | 2 +- .../storage/provider/src/test_utils/mock.rs | 4 +- .../storage/provider/src/test_utils/noop.rs | 2 +- crates/storage/storage-api/src/state.rs | 2 +- crates/trie/parallel/src/async_root.rs | 9 +-- crates/trie/parallel/src/parallel_root.rs | 9 +-- crates/trie/trie/Cargo.toml | 1 + crates/trie/trie/benches/prefix_set.rs | 3 +- crates/trie/trie/src/lib.rs | 3 + crates/trie/trie/src/node_iter.rs | 4 +- crates/trie/trie/src/prefix_set/loader.rs | 4 +- crates/trie/trie/src/prefix_set/mod.rs | 6 +- crates/trie/trie/src/progress.rs | 4 +- crates/trie/trie/src/proof.rs | 7 +- crates/trie/trie/src/state.rs | 5 +- crates/trie/trie/src/test_utils.rs | 7 +- crates/trie/trie/src/trie.rs | 17 ++--- .../trie/src/trie_cursor/database_cursors.rs | 13 ++-- crates/trie/trie/src/trie_cursor/mod.rs | 8 +-- crates/trie/trie/src/trie_cursor/noop.rs | 3 +- crates/trie/trie/src/trie_cursor/subnode.rs | 6 +- crates/trie/trie/src/updates.rs | 13 ++-- crates/trie/trie/src/walker.rs | 8 +-- crates/trie/types/Cargo.toml | 33 +++++++++ crates/trie/types/src/account.rs | 22 ++++++ .../types/src}/hash_builder/mod.rs | 0 .../types/src}/hash_builder/state.rs | 2 +- .../types/src}/hash_builder/value.rs | 0 crates/trie/types/src/lib.rs | 34 +++++++++ .../src/trie => trie/types/src}/mask.rs | 0 .../src/trie => trie/types/src}/nibbles.rs | 0 .../trie => trie/types/src}/nodes/branch.rs | 2 +- .../src/trie => trie/types/src}/nodes/mod.rs | 0 .../src/trie => trie/types/src}/storage.rs | 0 .../src/trie => trie/types/src}/subnode.rs | 4 +- 59 files changed, 283 insertions(+), 234 deletions(-) rename crates/primitives/src/{proofs.rs => proofs/mod.rs} (98%) create mode 100644 crates/primitives/src/proofs/traits.rs rename crates/primitives/src/{trie/proofs.rs => proofs/types.rs} (96%) delete mode 100644 crates/primitives/src/trie/account.rs delete mode 100644 crates/primitives/src/trie/mod.rs create mode 100644 crates/trie/types/Cargo.toml create mode 100644 crates/trie/types/src/account.rs rename crates/{primitives/src/trie => trie/types/src}/hash_builder/mod.rs (100%) rename crates/{primitives/src/trie => trie/types/src}/hash_builder/state.rs (99%) rename crates/{primitives/src/trie => trie/types/src}/hash_builder/value.rs (100%) create mode 100644 crates/trie/types/src/lib.rs rename crates/{primitives/src/trie => trie/types/src}/mask.rs (100%) rename crates/{primitives/src/trie => trie/types/src}/nibbles.rs (100%) rename crates/{primitives/src/trie => trie/types/src}/nodes/branch.rs (98%) rename crates/{primitives/src/trie => trie/types/src}/nodes/mod.rs (100%) rename crates/{primitives/src/trie => trie/types/src}/storage.rs (100%) rename crates/{primitives/src/trie => trie/types/src}/subnode.rs (96%) diff --git a/Cargo.lock b/Cargo.lock index 540e5dfa2..ea9be80aa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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" diff --git a/Cargo.toml b/Cargo.toml index b3780bfb8..e1f799de7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 } diff --git a/crates/primitives/Cargo.toml b/crates/primitives/Cargo.toml index 7136c575b..20b53eead 100644 --- a/crates/primitives/Cargo.toml +++ b/crates/primitives/Cargo.toml @@ -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 = [ diff --git a/crates/primitives/benches/trie_root.rs b/crates/primitives/benches/trie_root.rs index 49a3a44ff..b61e3aa85 100644 --- a/crates/primitives/benches/trie_root.rs +++ b/crates/primitives/benches/trie_root.rs @@ -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::(receipts.iter().map(|receipt| { diff --git a/crates/primitives/src/chain/spec.rs b/crates/primitives/src/chain/spec.rs index f13b0ccfa..6d9cb9e56 100644 --- a/crates/primitives/src/chain/spec.rs +++ b/crates/primitives/src/chain/spec.rs @@ -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); diff --git a/crates/primitives/src/lib.rs b/crates/primitives/src/lib.rs index 9bbb7aa39..eb02acc02 100644 --- a/crates/primitives/src/lib.rs +++ b/crates/primitives/src/lib.rs @@ -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"))] diff --git a/crates/primitives/src/proofs.rs b/crates/primitives/src/proofs/mod.rs similarity index 98% rename from crates/primitives/src/proofs.rs rename to crates/primitives/src/proofs/mod.rs index ca419acb8..dbce91949 100644 --- a/crates/primitives/src/proofs.rs +++ b/crates/primitives/src/proofs/mod.rs @@ -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 + Clone + 'a>( +pub fn state_root_ref_unhashed<'a, A: IntoTrieAccount + Clone + 'a>( state: impl IntoIterator, ) -> B256 { state_root_unsorted( @@ -185,7 +189,7 @@ pub fn state_root_ref_unhashed<'a, A: Into + 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>( +pub fn state_root_unhashed( state: impl IntoIterator, ) -> B256 { state_root_unsorted(state.into_iter().map(|(address, account)| (keccak256(address), account))) @@ -193,9 +197,7 @@ pub fn state_root_unhashed>( /// 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>( - state: impl IntoIterator, -) -> B256 { +pub fn state_root_unsorted(state: impl IntoIterator) -> B256 { state_root(state.into_iter().sorted_by_key(|(key, _)| *key)) } @@ -205,12 +207,12 @@ pub fn state_root_unsorted>( /// # Panics /// /// If the items are not in sorted order. -pub fn state_root>(state: impl IntoIterator) -> B256 { +pub fn state_root(state: impl IntoIterator) -> 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() diff --git a/crates/primitives/src/proofs/traits.rs b/crates/primitives/src/proofs/traits.rs new file mode 100644 index 000000000..7fef86944 --- /dev/null +++ b/crates/primitives/src/proofs/traits.rs @@ -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, + } + } +} diff --git a/crates/primitives/src/trie/proofs.rs b/crates/primitives/src/proofs/types.rs similarity index 96% rename from crates/primitives/src/trie/proofs.rs rename to crates/primitives/src/proofs/types.rs index 1949867be..f2225df79 100644 --- a/crates/primitives/src/trie/proofs.rs +++ b/crates/primitives/src/proofs/types.rs @@ -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, )))) diff --git a/crates/primitives/src/stage/checkpoints.rs b/crates/primitives/src/stage/checkpoints.rs index bd14f2082..1ab42c56c 100644 --- a/crates/primitives/src/stage/checkpoints.rs +++ b/crates/primitives/src/stage/checkpoints.rs @@ -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; diff --git a/crates/primitives/src/trie/account.rs b/crates/primitives/src/trie/account.rs deleted file mode 100644 index 055701dfc..000000000 --- a/crates/primitives/src/trie/account.rs +++ /dev/null @@ -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 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 - } -} diff --git a/crates/primitives/src/trie/mod.rs b/crates/primitives/src/trie/mod.rs deleted file mode 100644 index ed61aca39..000000000 --- a/crates/primitives/src/trie/mod.rs +++ /dev/null @@ -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}; diff --git a/crates/revm/src/test_utils.rs b/crates/revm/src/test_utils.rs index 782ad763f..bfab663da 100644 --- a/crates/revm/src/test_utils.rs +++ b/crates/revm/src/test_utils.rs @@ -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}; diff --git a/crates/rpc/rpc-types-compat/src/proof.rs b/crates/rpc/rpc-types-compat/src/proof.rs index c0be94e92..17e5cc193 100644 --- a/crates/rpc/rpc-types-compat/src/proof.rs +++ b/crates/rpc/rpc-types-compat/src/proof.rs @@ -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::{ diff --git a/crates/rpc/rpc/src/eth/api/pending_block.rs b/crates/rpc/rpc/src/eth/api/pending_block.rs index 213e62086..ec9d100d0 100644 --- a/crates/rpc/rpc/src/eth/api/pending_block.rs +++ b/crates/rpc/rpc/src/eth/api/pending_block.rs @@ -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, }; diff --git a/crates/stages/stages/src/stages/merkle.rs b/crates/stages/stages/src/stages/merkle.rs index 68cc7d681..defe24894 100644 --- a/crates/stages/stages/src/stages/merkle.rs +++ b/crates/stages/stages/src/stages/merkle.rs @@ -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::*; diff --git a/crates/storage/db-api/Cargo.toml b/crates/storage/db-api/Cargo.toml index 0f52c2743..7a97075f3 100644 --- a/crates/storage/db-api/Cargo.toml +++ b/crates/storage/db-api/Cargo.toml @@ -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 diff --git a/crates/storage/db-api/src/models/mod.rs b/crates/storage/db-api/src/models/mod.rs index 8c62164fd..108095a3c 100644 --- a/crates/storage/db-api/src/models/mod.rs +++ b/crates/storage/db-api/src/models/mod.rs @@ -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; diff --git a/crates/storage/db/Cargo.toml b/crates/storage/db/Cargo.toml index b527b5d5b..63c750ce8 100644 --- a/crates/storage/db/Cargo.toml +++ b/crates/storage/db/Cargo.toml @@ -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 } diff --git a/crates/storage/db/src/tables/mod.rs b/crates/storage/db/src/tables/mod.rs index de1389b26..f94c07d77 100644 --- a/crates/storage/db/src/tables/mod.rs +++ b/crates/storage/db/src/tables/mod.rs @@ -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; diff --git a/crates/storage/provider/src/providers/bundle_state_provider.rs b/crates/storage/provider/src/providers/bundle_state_provider.rs index 123c94308..98f224299 100644 --- a/crates/storage/provider/src/providers/bundle_state_provider.rs +++ b/crates/storage/provider/src/providers/bundle_state_provider.rs @@ -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; diff --git a/crates/storage/provider/src/providers/database/provider.rs b/crates/storage/provider/src/providers/database/provider.rs index 755ec0a36..5548e25a7 100644 --- a/crates/storage/provider/src/providers/database/provider.rs +++ b/crates/storage/provider/src/providers/database/provider.rs @@ -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::{ diff --git a/crates/storage/provider/src/providers/state/historical.rs b/crates/storage/provider/src/providers/state/historical.rs index ed906f1ad..d0304c1c4 100644 --- a/crates/storage/provider/src/providers/state/historical.rs +++ b/crates/storage/provider/src/providers/state/historical.rs @@ -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; diff --git a/crates/storage/provider/src/providers/state/latest.rs b/crates/storage/provider/src/providers/state/latest.rs index dc6d72112..a8e554461 100644 --- a/crates/storage/provider/src/providers/state/latest.rs +++ b/crates/storage/provider/src/providers/state/latest.rs @@ -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}; diff --git a/crates/storage/provider/src/providers/state/macros.rs b/crates/storage/provider/src/providers/state/macros.rs index 0bbd0f4dd..1d5a95978 100644 --- a/crates/storage/provider/src/providers/state/macros.rs +++ b/crates/storage/provider/src/providers/state/macros.rs @@ -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>; - fn proof(&self, address: reth_primitives::Address, keys: &[reth_primitives::B256]) -> reth_storage_errors::provider::ProviderResult; + fn proof(&self, address: reth_primitives::Address, keys: &[reth_primitives::B256]) -> reth_storage_errors::provider::ProviderResult; fn bytecode_by_hash(&self, code_hash: reth_primitives::B256) -> reth_storage_errors::provider::ProviderResult>; } ); diff --git a/crates/storage/provider/src/test_utils/mock.rs b/crates/storage/provider/src/test_utils/mock.rs index f985cf5b9..744888013 100644 --- a/crates/storage/provider/src/test_utils/mock.rs +++ b/crates/storage/provider/src/test_utils/mock.rs @@ -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, diff --git a/crates/storage/provider/src/test_utils/noop.rs b/crates/storage/provider/src/test_utils/noop.rs index 8b40fe652..078ef5d3d 100644 --- a/crates/storage/provider/src/test_utils/noop.rs +++ b/crates/storage/provider/src/test_utils/noop.rs @@ -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, diff --git a/crates/storage/storage-api/src/state.rs b/crates/storage/storage-api/src/state.rs index ad71ab724..d40f5ad64 100644 --- a/crates/storage/storage-api/src/state.rs +++ b/crates/storage/storage-api/src/state.rs @@ -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}; diff --git a/crates/trie/parallel/src/async_root.rs b/crates/trie/parallel/src/async_root.rs index 84c1d0c77..7441bd0a0 100644 --- a/crates/trie/parallel/src/async_root.rs +++ b/crates/trie/parallel/src/async_root.rs @@ -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); } diff --git a/crates/trie/parallel/src/parallel_root.rs b/crates/trie/parallel/src/parallel_root.rs index e47b547dc..f814e2ff8 100644 --- a/crates/trie/parallel/src/parallel_root.rs +++ b/crates/trie/parallel/src/parallel_root.rs @@ -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); } diff --git a/crates/trie/trie/Cargo.toml b/crates/trie/trie/Cargo.toml index d461968ac..665cf51f0 100644 --- a/crates/trie/trie/Cargo.toml +++ b/crates/trie/trie/Cargo.toml @@ -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 diff --git a/crates/trie/trie/benches/prefix_set.rs b/crates/trie/trie/benches/prefix_set.rs index 8af967196..db02b5dd1 100644 --- a/crates/trie/trie/benches/prefix_set.rs +++ b/crates/trie/trie/benches/prefix_set.rs @@ -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 diff --git a/crates/trie/trie/src/lib.rs b/crates/trie/trie/src/lib.rs index 82448bf9d..16251dc11 100644 --- a/crates/trie/trie/src/lib.rs +++ b/crates/trie/trie/src/lib.rs @@ -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; diff --git a/crates/trie/trie/src/node_iter.rs b/crates/trie/trie/src/node_iter.rs index 03d80d34c..29c6fd4f2 100644 --- a/crates/trie/trie/src/node_iter.rs +++ b/crates/trie/trie/src/node_iter.rs @@ -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)] diff --git a/crates/trie/trie/src/prefix_set/loader.rs b/crates/trie/trie/src/prefix_set/loader.rs index 14f0018e0..2f0b7048a 100644 --- a/crates/trie/trie/src/prefix_set/loader.rs +++ b/crates/trie/trie/src/prefix_set/loader.rs @@ -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); diff --git a/crates/trie/trie/src/prefix_set/mod.rs b/crates/trie/trie/src/prefix_set/mod.rs index ea4ebde94..f6a8789e0 100644 --- a/crates/trie/trie/src/prefix_set/mod.rs +++ b/crates/trie/trie/src/prefix_set/mod.rs @@ -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])); diff --git a/crates/trie/trie/src/progress.rs b/crates/trie/trie/src/progress.rs index 94cd757bc..916af7f16 100644 --- a/crates/trie/trie/src/progress.rs +++ b/crates/trie/trie/src/progress.rs @@ -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)] diff --git a/crates/trie/trie/src/proof.rs b/crates/trie/trie/src/proof.rs index 6fb827146..d3438dfaa 100644 --- a/crates/trie/trie/src/proof.rs +++ b/crates/trie/trie/src/proof.rs @@ -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); diff --git a/crates/trie/trie/src/state.rs b/crates/trie/trie/src/state.rs index 88922ca1a..13f91a697 100644 --- a/crates/trie/trie/src/state.rs +++ b/crates/trie/trie/src/state.rs @@ -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::{ diff --git a/crates/trie/trie/src/test_utils.rs b/crates/trie/trie/src/test_utils.rs index 67a392e20..bd0c1936f 100644 --- a/crates/trie/trie/src/test_utils.rs +++ b/crates/trie/trie/src/test_utils.rs @@ -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::(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)) }); diff --git a/crates/trie/trie/src/trie.rs b/crates/trie/trie/src/trie.rs index 3ab134897..91ba2e046 100644 --- a/crates/trie/trie/src/trie.rs +++ b/crates/trie/trie/src/trie.rs @@ -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) -> Vec { - 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 diff --git a/crates/trie/trie/src/trie_cursor/database_cursors.rs b/crates/trie/trie/src/trie_cursor/database_cursors.rs index a2f3a9659..910ae61b4 100644 --- a/crates/trie/trie/src/trie_cursor/database_cursors.rs +++ b/crates/trie/trie/src/trie_cursor/database_cursors.rs @@ -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] diff --git a/crates/trie/trie/src/trie_cursor/mod.rs b/crates/trie/trie/src/trie_cursor/mod.rs index 3914924a7..aae7e773c 100644 --- a/crates/trie/trie/src/trie_cursor/mod.rs +++ b/crates/trie/trie/src/trie_cursor/mod.rs @@ -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; diff --git a/crates/trie/trie/src/trie_cursor/noop.rs b/crates/trie/trie/src/trie_cursor/noop.rs index b168fbfc9..46163180b 100644 --- a/crates/trie/trie/src/trie_cursor/noop.rs +++ b/crates/trie/trie/src/trie_cursor/noop.rs @@ -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)] diff --git a/crates/trie/trie/src/trie_cursor/subnode.rs b/crates/trie/trie/src/trie_cursor/subnode.rs index e40c88c4f..15e2ac31d 100644 --- a/crates/trie/trie/src/trie_cursor/subnode.rs +++ b/crates/trie/trie/src/trie_cursor/subnode.rs @@ -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)] diff --git a/crates/trie/trie/src/updates.rs b/crates/trie/trie/src/updates.rs index e826fcf3f..457266eea 100644 --- a/crates/trie/trie/src/updates.rs +++ b/crates/trie/trie/src/updates.rs @@ -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. diff --git a/crates/trie/trie/src/walker.rs b/crates/trie/trie/src/walker.rs index 4866705ac..6486a9b08 100644 --- a/crates/trie/trie/src/walker.rs +++ b/crates/trie/trie/src/walker.rs @@ -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] diff --git a/crates/trie/types/Cargo.toml b/crates/trie/types/Cargo.toml new file mode 100644 index 000000000..4dc2f15dc --- /dev/null +++ b/crates/trie/types/Cargo.toml @@ -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 \ No newline at end of file diff --git a/crates/trie/types/src/account.rs b/crates/trie/types/src/account.rs new file mode 100644 index 000000000..480a8c6a6 --- /dev/null +++ b/crates/trie/types/src/account.rs @@ -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 + } +} diff --git a/crates/primitives/src/trie/hash_builder/mod.rs b/crates/trie/types/src/hash_builder/mod.rs similarity index 100% rename from crates/primitives/src/trie/hash_builder/mod.rs rename to crates/trie/types/src/hash_builder/mod.rs diff --git a/crates/primitives/src/trie/hash_builder/state.rs b/crates/trie/types/src/hash_builder/state.rs similarity index 99% rename from crates/primitives/src/trie/hash_builder/state.rs rename to crates/trie/types/src/hash_builder/state.rs index 386e2a7a0..c1fa9640f 100644 --- a/crates/primitives/src/trie/hash_builder/state.rs +++ b/crates/trie/types/src/hash_builder/state.rs @@ -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; diff --git a/crates/primitives/src/trie/hash_builder/value.rs b/crates/trie/types/src/hash_builder/value.rs similarity index 100% rename from crates/primitives/src/trie/hash_builder/value.rs rename to crates/trie/types/src/hash_builder/value.rs diff --git a/crates/trie/types/src/lib.rs b/crates/trie/types/src/lib.rs new file mode 100644 index 000000000..0b4927b9c --- /dev/null +++ b/crates/trie/types/src/lib.rs @@ -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}; diff --git a/crates/primitives/src/trie/mask.rs b/crates/trie/types/src/mask.rs similarity index 100% rename from crates/primitives/src/trie/mask.rs rename to crates/trie/types/src/mask.rs diff --git a/crates/primitives/src/trie/nibbles.rs b/crates/trie/types/src/nibbles.rs similarity index 100% rename from crates/primitives/src/trie/nibbles.rs rename to crates/trie/types/src/nibbles.rs diff --git a/crates/primitives/src/trie/nodes/branch.rs b/crates/trie/types/src/nodes/branch.rs similarity index 98% rename from crates/primitives/src/trie/nodes/branch.rs rename to crates/trie/types/src/nodes/branch.rs index 45182d081..a4f10d740 100644 --- a/crates/primitives/src/trie/nodes/branch.rs +++ b/crates/trie/types/src/nodes/branch.rs @@ -1,4 +1,4 @@ -use crate::trie::StoredTrieMask; +use crate::StoredTrieMask; use alloy_primitives::B256; use alloy_trie::BranchNodeCompact; use bytes::Buf; diff --git a/crates/primitives/src/trie/nodes/mod.rs b/crates/trie/types/src/nodes/mod.rs similarity index 100% rename from crates/primitives/src/trie/nodes/mod.rs rename to crates/trie/types/src/nodes/mod.rs diff --git a/crates/primitives/src/trie/storage.rs b/crates/trie/types/src/storage.rs similarity index 100% rename from crates/primitives/src/trie/storage.rs rename to crates/trie/types/src/storage.rs diff --git a/crates/primitives/src/trie/subnode.rs b/crates/trie/types/src/subnode.rs similarity index 96% rename from crates/primitives/src/trie/subnode.rs rename to crates/trie/types/src/subnode.rs index 556282871..0a51b561b 100644 --- a/crates/primitives/src/trie/subnode.rs +++ b/crates/trie/types/src/subnode.rs @@ -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() {