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

refactor: Separate database integration from trie into its own crate #8599

Closed
Show file tree
Hide file tree
Changes from 1 commit
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
27 changes: 27 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ members = [
"crates/transaction-pool/",
"crates/trie/parallel/",
"crates/trie/trie",
"crates/trie/trie-db",
"examples/beacon-api-sidecar-fetcher/",
"examples/beacon-api-sse/",
"examples/bsc-p2p",
Expand Down Expand Up @@ -311,6 +312,7 @@ reth-tokio-util = { path = "crates/tokio-util" }
reth-tracing = { path = "crates/tracing" }
reth-transaction-pool = { path = "crates/transaction-pool" }
reth-trie = { path = "crates/trie/trie" }
reth-trie-db = { path = "crates/trie/trie-db" }
reth-trie-parallel = { path = "crates/trie/parallel" }

# revm
Expand Down
62 changes: 62 additions & 0 deletions crates/trie/trie-db/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
[package]
name = "reth-trie-db"
RomanHodulak marked this conversation as resolved.
Show resolved Hide resolved
version.workspace = true
edition.workspace = true
rust-version.workspace = true
license.workspace = true
homepage.workspace = true
repository.workspace = true
description = "Database integration with the merkle trie implementation"

[lints]
workspace = true

[dependencies]
# reth
reth-primitives.workspace = true
reth-execution-errors.workspace = true
reth-trie.workspace = true
reth-db.workspace = true

revm.workspace = true

# alloy
alloy-rlp.workspace = true

# tracing
tracing.workspace = true

# misc
rayon.workspace = true
derive_more.workspace = true
auto_impl.workspace = true

# `metrics` feature
reth-metrics = { workspace = true, optional = true }
metrics = { workspace = true, optional = true }

# `test-utils` feature
triehash = { version = "0.8", optional = true }

[dev-dependencies]
# reth
reth-primitives = { workspace = true, features = ["test-utils", "arbitrary"] }
reth-db = { workspace = true, features = ["test-utils"] }
reth-provider = { workspace = true, features = ["test-utils"] }
reth-storage-errors.workspace = true

# trie
triehash = "0.8"

# misc
proptest.workspace = true
tokio = { workspace = true, default-features = false, features = ["sync", "rt", "macros"] }
tokio-stream.workspace = true
once_cell.workspace = true
serde_json.workspace = true
similar-asserts.workspace = true
criterion.workspace = true

[features]
metrics = ["reth-metrics", "dep:metrics"]
test-utils = ["triehash"]
Original file line number Diff line number Diff line change
@@ -1,43 +1,50 @@
use super::{HashedCursor, HashedCursorFactory, HashedStorageCursor};
use reth_trie::hashed_cursor::{HashedCursor, HashedCursorFactory, HashedStorageCursor};
use reth_db::{
cursor::{DbCursorRO, DbDupCursorRO},
tables,
transaction::DbTx,
};
use reth_primitives::{Account, B256, U256};

impl<'a, TX: DbTx> HashedCursorFactory for &'a TX {
/// New-type for a [`DbTx`] reference with [`HashedCursorFactory`] support.
pub struct DbTxRefWrapper<'a, TX: DbTx>(&'a TX);

/// New-type for a [`DbCursorRO`] reference with [`HashedCursor`] support.
pub struct DbCursorROWrapper<T: DbCursorRO<tables::HashedAccounts>>(T);

impl<'a, TX: DbTx> HashedCursorFactory for DbTxRefWrapper<'a, TX> {
type AccountCursor = <TX as DbTx>::Cursor<tables::HashedAccounts>;
type StorageCursor =
DatabaseHashedStorageCursor<<TX as DbTx>::DupCursor<tables::HashedStorages>>;

fn hashed_account_cursor(&self) -> Result<Self::AccountCursor, reth_db::DatabaseError> {
self.cursor_read::<tables::HashedAccounts>()
self.0.cursor_read::<tables::HashedAccounts>()
}

fn hashed_storage_cursor(
&self,
hashed_address: B256,
) -> Result<Self::StorageCursor, reth_db::DatabaseError> {
Ok(DatabaseHashedStorageCursor::new(
self.cursor_dup_read::<tables::HashedStorages>()?,
self.0.cursor_dup_read::<tables::HashedStorages>()?,
hashed_address,
))
}
}

impl<C> HashedCursor for C
impl<C> HashedCursor for DbCursorROWrapper<C>
where
C: DbCursorRO<tables::HashedAccounts>,
{
type Err = reth_db::DatabaseError;
type Value = Account;

fn seek(&mut self, key: B256) -> Result<Option<(B256, Self::Value)>, reth_db::DatabaseError> {
self.seek(key)
fn seek(&mut self, key: B256) -> Result<Option<(B256, Self::Value)>, Self::Err> {
self.0.seek(key)
}

fn next(&mut self) -> Result<Option<(B256, Self::Value)>, reth_db::DatabaseError> {
self.next()
fn next(&mut self) -> Result<Option<(B256, Self::Value)>, Self::Err> {
self.0.next()
}
}

Expand All @@ -63,6 +70,7 @@ impl<C> HashedCursor for DatabaseHashedStorageCursor<C>
where
C: DbCursorRO<tables::HashedStorages> + DbDupCursorRO<tables::HashedStorages>,
{
type Err = reth_db::DatabaseError;
type Value = U256;

fn seek(
Expand Down
3 changes: 3 additions & 0 deletions crates/trie/trie-db/src/hashed_cursor/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/// Default implementation of the hashed state cursor traits.
mod default;
pub use default::DatabaseHashedStorageCursor;
38 changes: 38 additions & 0 deletions crates/trie/trie-db/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//! The implementation of Merkle Patricia Trie, a cryptographically
//! authenticated radix trie that is used to store key-value bindings.
//! <https://ethereum.org/en/developers/docs/data-structures-and-encoding/patricia-merkle-trie/>
//!
//! ## Feature Flags
//!
//! - `test-utils`: Export utilities for testing

#![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(docsrs, feature(doc_cfg, doc_auto_cfg))]

/// The implementation of a container for storing intermediate changes to a trie.
/// The container indicates when the trie has been modified.
pub mod prefix_set;

/// The cursor implementations for navigating account and storage tries.
pub mod trie_cursor;

/// The cursor implementations for navigating hashed state.
pub mod hashed_cursor;

/// In-memory hashed state.
mod state;
pub use state::*;

/// Merkle proof generation.
pub mod proof;

/// The implementation of the Merkle Patricia Trie.
mod trie;
pub use trie::{storage_root, state_root};

/// Buffer for trie updates.
pub mod updates;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{PrefixSetMut, TriePrefixSets};
use reth_trie::prefix_set::{PrefixSetMut, TriePrefixSets};
use derive_more::Deref;
use reth_db::{
cursor::DbCursorRO,
Expand Down
2 changes: 2 additions & 0 deletions crates/trie/trie-db/src/prefix_set/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod loader;
pub use loader::PrefixSetLoader;
Loading