diff --git a/Cargo.lock b/Cargo.lock index 3bcc998cf21d..30090228676a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8330,6 +8330,7 @@ name = "reth-node-types" version = "1.1.2" dependencies = [ "reth-chainspec", + "reth-db", "reth-db-api", "reth-engine-primitives", "reth-primitives-traits", diff --git a/bin/reth/src/cli/mod.rs b/bin/reth/src/cli/mod.rs index 192ab670028a..42a11652b9c8 100644 --- a/bin/reth/src/cli/mod.rs +++ b/bin/reth/src/cli/mod.rs @@ -152,9 +152,8 @@ impl, Ext: clap::Args + fmt::Debug> Cl let runner = CliRunner::default(); match self.command { - Commands::Node(command) => { - runner.run_command_until_exit(|ctx| command.execute(ctx, launcher)) - } + Commands::Node(command) => runner + .run_command_until_exit(|ctx| command.execute::(ctx, launcher)), Commands::Init(command) => { runner.run_blocking_until_ctrl_c(command.execute::()) } diff --git a/crates/cli/commands/src/common.rs b/crates/cli/commands/src/common.rs index e206715fc01d..121fc8ad9f8e 100644 --- a/crates/cli/commands/src/common.rs +++ b/crates/cli/commands/src/common.rs @@ -6,7 +6,7 @@ use reth_chainspec::EthChainSpec; use reth_cli::chainspec::ChainSpecParser; use reth_config::{config::EtlConfig, Config}; use reth_consensus::noop::NoopConsensus; -use reth_db::{init_db, open_db_read_only, DatabaseEnv}; +use reth_db::{mdbx::init_db_for, open_db_read_only, DatabaseEnv}; use reth_db_common::init::init_genesis; use reth_downloaders::{bodies::noop::NoopBodiesDownloader, headers::noop::NoopHeaderDownloader}; use reth_evm::noop::NoopBlockExecutorProvider; @@ -86,7 +86,7 @@ impl EnvironmentArgs { info!(target: "reth::cli", ?db_path, ?sf_path, "Opening storage"); let (db, sfp) = match access { AccessRights::RW => ( - Arc::new(init_db(db_path, self.db.database_args())?), + Arc::new(init_db_for::<_, N::Storage>(db_path, self.db.database_args())?), StaticFileProvider::read_write(sf_path)?, ), AccessRights::RO => ( diff --git a/crates/cli/commands/src/node.rs b/crates/cli/commands/src/node.rs index b099a2c05222..2c43e449f168 100644 --- a/crates/cli/commands/src/node.rs +++ b/crates/cli/commands/src/node.rs @@ -5,7 +5,7 @@ use reth_chainspec::{EthChainSpec, EthereumHardforks}; use reth_cli::chainspec::ChainSpecParser; use reth_cli_runner::CliContext; use reth_cli_util::parse_socket_address; -use reth_db::{init_db, DatabaseEnv}; +use reth_db::{mdbx::init_db_for, DatabaseEnv}; use reth_ethereum_cli::chainspec::EthereumChainSpecParser; use reth_node_builder::{NodeBuilder, WithLaunchContext}; use reth_node_core::{ @@ -18,6 +18,8 @@ use reth_node_core::{ }; use std::{ffi::OsString, fmt, future::Future, net::SocketAddr, path::PathBuf, sync::Arc}; +use crate::common::CliNodeTypes; + /// Start the node #[derive(Debug, Parser)] pub struct NodeCommand< @@ -137,7 +139,11 @@ impl< /// /// This transforms the node command into a node config and launches the node using the given /// closure. - pub async fn execute(self, ctx: CliContext, launcher: L) -> eyre::Result<()> + pub async fn execute, L, Fut>( + self, + ctx: CliContext, + launcher: L, + ) -> eyre::Result<()> where L: FnOnce(WithLaunchContext, C::ChainSpec>>, Ext) -> Fut, Fut: Future>, @@ -183,7 +189,9 @@ impl< let db_path = data_dir.db(); tracing::info!(target: "reth::cli", path = ?db_path, "Opening database"); - let database = Arc::new(init_db(db_path.clone(), self.db.database_args())?.with_metrics()); + let database = Arc::new( + init_db_for::<_, N::Storage>(db_path.clone(), self.db.database_args())?.with_metrics(), + ); if with_unused_ports { node_config = node_config.with_unused_ports(); diff --git a/crates/cli/commands/src/stage/dump/mod.rs b/crates/cli/commands/src/stage/dump/mod.rs index 9cc0f54dd33f..04d92e6131f6 100644 --- a/crates/cli/commands/src/stage/dump/mod.rs +++ b/crates/cli/commands/src/stage/dump/mod.rs @@ -3,7 +3,10 @@ use crate::common::{AccessRights, CliNodeTypes, Environment, EnvironmentArgs}; use clap::Parser; use reth_chainspec::{EthChainSpec, EthereumHardforks}; use reth_cli::chainspec::ChainSpecParser; -use reth_db::{init_db, mdbx::DatabaseArguments, tables, DatabaseEnv}; +use reth_db::{ + mdbx::{init_db_for, DatabaseArguments}, + tables, DatabaseEnv, +}; use reth_db_api::{ cursor::DbCursorRO, database::Database, models::ClientVersion, table::TableImporter, transaction::DbTx, @@ -125,7 +128,8 @@ pub(crate) fn setup( info!(target: "reth::cli", ?output_db, "Creating separate db"); - let output_datadir = init_db(output_db, DatabaseArguments::new(ClientVersion::default()))?; + let output_datadir = + init_db_for::<_, N::Storage>(output_db, DatabaseArguments::new(ClientVersion::default()))?; output_datadir.update(|tx| { tx.import_table_with_range::( diff --git a/crates/node/types/Cargo.toml b/crates/node/types/Cargo.toml index 588fe7c4062f..1760a35db0b7 100644 --- a/crates/node/types/Cargo.toml +++ b/crates/node/types/Cargo.toml @@ -17,6 +17,7 @@ reth-db-api.workspace = true reth-engine-primitives.workspace = true reth-primitives-traits.workspace = true reth-trie-db.workspace = true +reth-db.workspace = true [features] default = ["std"] diff --git a/crates/node/types/src/lib.rs b/crates/node/types/src/lib.rs index 6e1eb81a0c8b..0bc6290b1374 100644 --- a/crates/node/types/src/lib.rs +++ b/crates/node/types/src/lib.rs @@ -10,6 +10,7 @@ #![cfg_attr(not(feature = "std"), no_std)] use core::{fmt::Debug, marker::PhantomData}; +use reth_db::TableSet; pub use reth_primitives_traits::{ Block, BlockBody, FullBlock, FullNodePrimitives, FullReceipt, FullSignedTx, NodePrimitives, }; @@ -35,7 +36,7 @@ pub trait NodeTypes: Send + Sync + Unpin + 'static { /// The type used to perform state commitment operations. type StateCommitment: StateCommitment; /// The type responsible for writing chain primitives to storage. - type Storage: Default + Send + Sync + Unpin + Debug + 'static; + type Storage: TableSet + Default + Send + Sync + Unpin + Debug + 'static; } /// The type that configures an Ethereum-like node with an engine for consensus. @@ -153,7 +154,7 @@ where P: NodePrimitives + Send + Sync + Unpin + 'static, C: EthChainSpec
+ 'static, SC: StateCommitment, - S: Default + Send + Sync + Unpin + Debug + 'static, + S: TableSet + Default + Send + Sync + Unpin + Debug + 'static, { type Primitives = P; type ChainSpec = C; @@ -214,7 +215,7 @@ where E: EngineTypes + Send + Sync + Unpin, C: EthChainSpec
+ 'static, SC: StateCommitment, - S: Default + Send + Sync + Unpin + Debug + 'static, + S: TableSet + Default + Send + Sync + Unpin + Debug + 'static, { type Primitives = P; type ChainSpec = C; @@ -228,7 +229,7 @@ where E: EngineTypes + Send + Sync + Unpin, C: EthChainSpec
+ 'static, SC: StateCommitment, - S: Default + Send + Sync + Unpin + Debug + 'static, + S: TableSet + Default + Send + Sync + Unpin + Debug + 'static, { type Engine = E; } diff --git a/crates/optimism/cli/src/lib.rs b/crates/optimism/cli/src/lib.rs index 23eaa99b5213..e5ae8d3c6f13 100644 --- a/crates/optimism/cli/src/lib.rs +++ b/crates/optimism/cli/src/lib.rs @@ -147,7 +147,7 @@ where let runner = CliRunner::default(); match self.command { Commands::Node(command) => { - runner.run_command_until_exit(|ctx| command.execute(ctx, launcher)) + runner.run_command_until_exit(|ctx| command.execute::(ctx, launcher)) } Commands::Init(command) => { runner.run_blocking_until_ctrl_c(command.execute::()) diff --git a/crates/optimism/node/src/node.rs b/crates/optimism/node/src/node.rs index 54ff36dabac0..cb9d50aa63e0 100644 --- a/crates/optimism/node/src/node.rs +++ b/crates/optimism/node/src/node.rs @@ -9,7 +9,11 @@ use crate::{ use alloy_consensus::Header; use reth_basic_payload_builder::{BasicPayloadJobGenerator, BasicPayloadJobGeneratorConfig}; use reth_chainspec::{EthChainSpec, EthereumHardforks, Hardforks}; -use reth_db::transaction::{DbTx, DbTxMut}; +use reth_db::{ + table::TableInfo, + transaction::{DbTx, DbTxMut}, + TableSet, Tables, +}; use reth_evm::{execute::BasicBlockExecutorProvider, ConfigureEvm}; use reth_network::{NetworkConfig, NetworkHandle, NetworkManager, PeersInfo}; use reth_node_api::{AddOnsContext, EngineValidator, FullNodeComponents, NodeAddOns, TxTy}; @@ -105,6 +109,12 @@ impl ChainStorage for OpStorage { } } +impl TableSet for OpStorage { + fn tables() -> Box>> { + Tables::tables() + } +} + /// Type configuration for a regular Optimism node. #[derive(Debug, Default, Clone)] #[non_exhaustive] diff --git a/crates/storage/provider/src/providers/database/mod.rs b/crates/storage/provider/src/providers/database/mod.rs index 85b734ef6616..c685e11f6dfb 100644 --- a/crates/storage/provider/src/providers/database/mod.rs +++ b/crates/storage/provider/src/providers/database/mod.rs @@ -14,7 +14,10 @@ use alloy_eips::{ use alloy_primitives::{Address, BlockHash, BlockNumber, TxHash, TxNumber, B256, U256}; use core::fmt; use reth_chainspec::{ChainInfo, EthereumHardforks}; -use reth_db::{init_db, mdbx::DatabaseArguments, DatabaseEnv}; +use reth_db::{ + mdbx::{init_db_for, DatabaseArguments}, + DatabaseEnv, +}; use reth_db_api::{database::Database, models::StoredBlockBodyIndices}; use reth_errors::{RethError, RethResult}; use reth_evm::ConfigureEvmEnv; @@ -135,7 +138,7 @@ impl>> ProviderFactory { static_file_provider: StaticFileProvider, ) -> RethResult { Ok(Self { - db: Arc::new(init_db(path, args).map_err(RethError::msg)?), + db: Arc::new(init_db_for::<_, N::Storage>(path, args).map_err(RethError::msg)?), chain_spec, static_file_provider, prune_modes: PruneModes::none(), diff --git a/crates/storage/storage-api/src/chain.rs b/crates/storage/storage-api/src/chain.rs index 9b9c24c68633..c4113bf77ca4 100644 --- a/crates/storage/storage-api/src/chain.rs +++ b/crates/storage/storage-api/src/chain.rs @@ -4,9 +4,10 @@ use reth_chainspec::{ChainSpecProvider, EthereumHardforks}; use reth_db::{ cursor::{DbCursorRO, DbCursorRW}, models::{StoredBlockOmmers, StoredBlockWithdrawals}, + table::TableInfo, tables, transaction::{DbTx, DbTxMut}, - DbTxUnwindExt, + DbTxUnwindExt, TableSet, Tables, }; use reth_primitives_traits::{Block, BlockBody, FullNodePrimitives}; use reth_storage_errors::provider::ProviderResult; @@ -167,3 +168,9 @@ where Ok(bodies) } } + +impl TableSet for EthStorage { + fn tables() -> Box>> { + Tables::tables() + } +}