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

feat: Feature gate traits inside storage-api that still depend on db-api #14647

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion crates/node/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ reth-primitives-traits = { workspace = true, features = ["rayon"] }
reth-cli-util.workspace = true
reth-db = { workspace = true, features = ["mdbx"] }
reth-storage-errors.workspace = true
reth-storage-api.workspace = true
reth-storage-api = { workspace = true, features = ["std", "db-api"] }
reth-network = { workspace = true, features = ["serde"] }
reth-network-p2p.workspace = true
reth-rpc-eth-types.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion crates/storage/provider/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ reth-primitives-traits = { workspace = true, features = ["reth-codec"] }
reth-fs-util.workspace = true
reth-errors.workspace = true
reth-storage-errors.workspace = true
reth-storage-api = { workspace = true, features = ["std"] }
reth-storage-api = { workspace = true, features = ["std", "db-api"] }
reth-network-p2p.workspace = true
reth-db = { workspace = true, features = ["mdbx"] }
reth-db-api.workspace = true
Expand Down
6 changes: 5 additions & 1 deletion crates/storage/storage-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ workspace = true
# reth
reth-db-models.workspace = true
reth-chainspec.workspace = true
reth-db-api.workspace = true
reth-db-api = { workspace = true, optional = true }
reth-execution-types.workspace = true
reth-primitives-traits.workspace = true
reth-prune-types.workspace = true
Expand Down Expand Up @@ -50,3 +50,7 @@ std = [
"reth-prune-types/std",
"reth-storage-errors/std",
]

db-api = [
"dep:reth-db-api",
]
18 changes: 15 additions & 3 deletions crates/storage/storage-api/src/chain.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
#[cfg(feature = "db-api")]
use crate::{DBProvider, OmmersProvider, StorageLocation};
use alloc::vec::Vec;
use alloy_consensus::Header;
#[cfg(feature = "db-api")]
use alloy_primitives::BlockNumber;
use core::marker::PhantomData;
#[cfg(feature = "db-api")]
use reth_chainspec::{ChainSpecProvider, EthereumHardforks};
#[cfg(feature = "db-api")]
use reth_db_api::{
cursor::{DbCursorRO, DbCursorRW},
models::StoredBlockOmmers,
tables,
transaction::{DbTx, DbTxMut},
DbTxUnwindExt,
};
#[cfg(feature = "db-api")]
use reth_db_models::StoredBlockWithdrawals;
use reth_ethereum_primitives::TransactionSigned;
use reth_primitives_traits::{
Block, BlockBody, FullBlockHeader, FullNodePrimitives, SignedTransaction,
};

use reth_primitives_traits::{Block, BlockBody, FullNodePrimitives};
#[cfg(feature = "db-api")]
use reth_primitives_traits::{FullBlockHeader, SignedTransaction};
use reth_storage_errors::provider::ProviderResult;

/// Trait that implements how block bodies are written to the storage.
///
/// Note: Within the current abstraction, this should only write to tables unrelated to
/// transactions. Writing of transactions is handled separately.
#[cfg(feature = "db-api")]
#[auto_impl::auto_impl(&, Arc)]
pub trait BlockBodyWriter<Provider, Body: BlockBody> {
/// Writes a set of block bodies to the storage.
Expand All @@ -42,10 +49,13 @@ pub trait BlockBodyWriter<Provider, Body: BlockBody> {
}

/// Trait that implements how chain-specific types are written to the storage.
#[cfg(feature = "db-api")]
pub trait ChainStorageWriter<Provider, Primitives: FullNodePrimitives>:
BlockBodyWriter<Provider, <Primitives::Block as Block>::Body>
{
}

#[cfg(feature = "db-api")]
impl<T, Provider, Primitives: FullNodePrimitives> ChainStorageWriter<Provider, Primitives> for T where
T: BlockBodyWriter<Provider, <Primitives::Block as Block>::Body>
{
Expand Down Expand Up @@ -94,6 +104,7 @@ impl<T, H> Default for EthStorage<T, H> {
}
}

#[cfg(feature = "db-api")]
impl<Provider, T, H> BlockBodyWriter<Provider, alloy_consensus::BlockBody<T, H>>
for EthStorage<T, H>
where
Expand Down Expand Up @@ -144,6 +155,7 @@ where
}
}

#[cfg(feature = "db-api")]
impl<Provider, T, H> BlockBodyReader<Provider> for EthStorage<T, H>
where
Provider:
Expand Down
28 changes: 17 additions & 11 deletions crates/storage/storage-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,8 @@ pub use withdrawals::*;
mod ommers;
pub use ommers::*;

mod database_provider;
pub use database_provider::*;

pub mod noop;

mod history;
pub use history::*;

mod hashing;
pub use hashing::*;
mod stats;
pub use stats::*;

mod legacy;
pub use legacy::*;

Expand All @@ -82,3 +71,20 @@ pub use primitives::*;

mod block_indices;
pub use block_indices::*;

mod database_provider;
mod history;
mod hashing;
mod stats;

/// Group all `db-api` related modules into a single `db` module.
#[cfg(feature = "db-api")]
pub mod db {
pub use crate::database_provider::*;
pub use crate::history::*;
pub use crate::hashing::*;
pub use crate::stats::*;
}

#[cfg(feature = "db-api")]
pub use db::*;
3 changes: 1 addition & 2 deletions crates/storage/storage-api/src/stats.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use reth_db_api::table::Table;
use reth_storage_errors::provider::ProviderResult;

/// The trait for fetching provider statistics.
#[auto_impl::auto_impl(&, Arc)]
pub trait StatsReader: Send + Sync {
/// Fetch the number of entries in the corresponding [Table]. Depending on the provider, it may
/// route to different data sources other than [Table].
fn count_entries<T: Table>(&self) -> ProviderResult<usize>;
fn count_entries<T: Table>(&self) -> reth_storage_errors::provider::ProviderResult<usize>;
}
2 changes: 2 additions & 0 deletions crates/storage/storage-api/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use alloc::{
};
use alloy_primitives::{Address, BlockNumber, B256};
use core::ops::RangeInclusive;
#[cfg(feature = "db-api")]
use reth_db_api::models::BlockNumberAddress;
use reth_primitives_traits::StorageEntry;
use reth_storage_errors::provider::ProviderResult;
Expand Down Expand Up @@ -34,6 +35,7 @@ pub trait StorageReader: Send + Sync {
}

/// Storage ChangeSet reader
#[cfg(feature = "db-api")]
#[auto_impl::auto_impl(&, Arc, Box)]
pub trait StorageChangeSetReader: Send + Sync {
/// Iterate over storage changesets and return the storage state from before this block.
Expand Down
Loading