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: add TableSet generic to the node builder pattern #13231

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions Cargo.lock

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

5 changes: 2 additions & 3 deletions bin/reth/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,8 @@ impl<C: ChainSpecParser<ChainSpec = ChainSpec>, 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::<EthereumNode, _, _>(ctx, launcher)),
Commands::Init(command) => {
runner.run_blocking_until_ctrl_c(command.execute::<EthereumNode>())
}
Expand Down
4 changes: 2 additions & 2 deletions crates/cli/commands/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -86,7 +86,7 @@ impl<C: ChainSpecParser> EnvironmentArgs<C> {
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 => (
Expand Down
14 changes: 11 additions & 3 deletions crates/cli/commands/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -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<
Expand Down Expand Up @@ -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<L, Fut>(self, ctx: CliContext, launcher: L) -> eyre::Result<()>
pub async fn execute<N: CliNodeTypes<ChainSpec = C::ChainSpec>, L, Fut>(
self,
ctx: CliContext,
launcher: L,
) -> eyre::Result<()>
where
L: FnOnce(WithLaunchContext<NodeBuilder<Arc<DatabaseEnv>, C::ChainSpec>>, Ext) -> Fut,
Fut: Future<Output = eyre::Result<()>>,
Expand Down Expand Up @@ -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();
Expand Down
8 changes: 6 additions & 2 deletions crates/cli/commands/src/stage/dump/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -125,7 +128,8 @@ pub(crate) fn setup<N: NodeTypesWithDB>(

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::<tables::BlockBodyIndices, _>(
Expand Down
1 change: 1 addition & 0 deletions crates/node/types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
9 changes: 5 additions & 4 deletions crates/node/types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand All @@ -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.
Expand Down Expand Up @@ -153,7 +154,7 @@ where
P: NodePrimitives + Send + Sync + Unpin + 'static,
C: EthChainSpec<Header = P::BlockHeader> + 'static,
SC: StateCommitment,
S: Default + Send + Sync + Unpin + Debug + 'static,
S: TableSet + Default + Send + Sync + Unpin + Debug + 'static,
{
type Primitives = P;
type ChainSpec = C;
Expand Down Expand Up @@ -214,7 +215,7 @@ where
E: EngineTypes + Send + Sync + Unpin,
C: EthChainSpec<Header = P::BlockHeader> + 'static,
SC: StateCommitment,
S: Default + Send + Sync + Unpin + Debug + 'static,
S: TableSet + Default + Send + Sync + Unpin + Debug + 'static,
{
type Primitives = P;
type ChainSpec = C;
Expand All @@ -228,7 +229,7 @@ where
E: EngineTypes + Send + Sync + Unpin,
C: EthChainSpec<Header = P::BlockHeader> + 'static,
SC: StateCommitment,
S: Default + Send + Sync + Unpin + Debug + 'static,
S: TableSet + Default + Send + Sync + Unpin + Debug + 'static,
{
type Engine = E;
}
Expand Down
2 changes: 1 addition & 1 deletion crates/optimism/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<OpNode, _, _>(ctx, launcher))
}
Commands::Init(command) => {
runner.run_blocking_until_ctrl_c(command.execute::<OpNode>())
Expand Down
12 changes: 11 additions & 1 deletion crates/optimism/node/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -105,6 +109,12 @@ impl ChainStorage<OpPrimitives> for OpStorage {
}
}

impl TableSet for OpStorage {
fn tables() -> Box<dyn Iterator<Item = Box<dyn TableInfo>>> {
Tables::tables()
}
}

/// Type configuration for a regular Optimism node.
#[derive(Debug, Default, Clone)]
#[non_exhaustive]
Expand Down
7 changes: 5 additions & 2 deletions crates/storage/provider/src/providers/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -135,7 +138,7 @@ impl<N: NodeTypesWithDB<DB = Arc<DatabaseEnv>>> ProviderFactory<N> {
static_file_provider: StaticFileProvider<N::Primitives>,
) -> RethResult<Self> {
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(),
Expand Down
9 changes: 8 additions & 1 deletion crates/storage/storage-api/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -167,3 +168,9 @@ where
Ok(bodies)
}
}

impl TableSet for EthStorage {
fn tables() -> Box<dyn Iterator<Item = Box<dyn TableInfo>>> {
Tables::tables()
}
}
Loading