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(db): add support for custom tables #13125

Closed
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
2 changes: 2 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/cli/commands/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ impl<
db,
dev,
pruning,
table_set,
};

let data_dir = node_config.datadir();
Expand Down
1 change: 1 addition & 0 deletions crates/exex/exex/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ reth-revm.workspace = true
reth-stages-api.workspace = true
reth-tasks.workspace = true
reth-tracing.workspace = true
reth-db.workspace = true

# alloy
alloy-consensus.workspace = true
Expand Down
4 changes: 2 additions & 2 deletions crates/exex/exex/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct ExExContext<Node: FullNodeComponents> {
/// The current head of the blockchain at launch.
pub head: Head,
/// The config of the node
pub config: NodeConfig<<Node::Types as NodeTypes>::ChainSpec>,
pub config: NodeConfig<<Node::Types as NodeTypes>::ChainSpec, Node::TableSet>,
/// The loaded node config
pub reth_config: reth_config::Config,
/// Channel used to send [`ExExEvent`]s to the rest of the node.
Expand Down Expand Up @@ -62,7 +62,7 @@ where
Node::Types: NodeTypes<Primitives: NodePrimitives>,
{
/// Returns dynamic version of the context
pub fn into_dyn(self) -> ExExContextDyn<<Node::Types as NodeTypes>::Primitives> {
pub fn into_dyn(self) -> ExExContextDyn<<Node::Types as NodeTypes>::Primitives, Node::TableSet> {
ExExContextDyn::from(self)
}
}
Expand Down
10 changes: 6 additions & 4 deletions crates/exex/exex/src/dyn_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use std::fmt::Debug;

use reth_chainspec::{EthChainSpec, Head};
use reth_db::{TableSet, Tables};
use reth_node_api::{FullNodeComponents, HeaderTy, NodePrimitives, NodeTypes};
use reth_node_core::node_config::NodeConfig;
use reth_primitives::EthPrimitives;
Expand All @@ -14,11 +15,11 @@ use crate::{ExExContext, ExExEvent, ExExNotificationsStream};

// TODO(0xurb) - add `node` after abstractions
/// Captures the context that an `ExEx` has access to.
pub struct ExExContextDyn<N: NodePrimitives = EthPrimitives> {
pub struct ExExContextDyn<N: NodePrimitives = EthPrimitives, TS: TableSet = Tables> {
/// The current head of the blockchain at launch.
pub head: Head,
/// The config of the node
pub config: NodeConfig<Box<dyn EthChainSpec<Header = N::BlockHeader> + 'static>>,
pub config: NodeConfig<Box<dyn EthChainSpec<Header = N::BlockHeader> + 'static>, TS>,
/// The loaded node config
pub reth_config: reth_config::Config,
/// Channel used to send [`ExExEvent`]s to the rest of the node.
Expand Down Expand Up @@ -50,11 +51,12 @@ impl<N: NodePrimitives> Debug for ExExContextDyn<N> {
}
}

impl<Node> From<ExExContext<Node>> for ExExContextDyn<<Node::Types as NodeTypes>::Primitives>
impl<Node, TS> From<ExExContext<Node>> for ExExContextDyn<<Node::Types as NodeTypes>::Primitives, TS>
where
Node: FullNodeComponents<Types: NodeTypes<Primitives: NodePrimitives>>,
Node: FullNodeComponents<Types: NodeTypes<Primitives: NodePrimitives>, TableSet = TS>,
Node::Provider: Debug + BlockReader,
Node::Executor: Debug,
TS: TableSet,
{
fn from(ctx: ExExContext<Node>) -> Self {
let config = ctx.config.map_chainspec(|chainspec| {
Expand Down
1 change: 1 addition & 0 deletions crates/node/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ reth-tasks.workspace = true
reth-network-api.workspace = true
reth-node-types.workspace = true
reth-node-core.workspace = true
reth-db.workspace = true

alloy-rpc-types-engine.workspace = true
alloy-consensus.workspace = true
Expand Down
12 changes: 10 additions & 2 deletions crates/node/api/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use alloy_consensus::Header;
use alloy_rpc_types_engine::JwtSecret;
use reth_beacon_consensus::BeaconConsensusEngineHandle;
use reth_consensus::FullConsensus;
use reth_db::TableSet;
use reth_evm::execute::BlockExecutorProvider;
use reth_network_api::FullNetwork;
use reth_node_core::node_config::NodeConfig;
Expand Down Expand Up @@ -64,6 +65,9 @@ pub trait FullNodeComponents: FullNodeTypes + Clone + 'static {
/// Builds new blocks.
type PayloadBuilder: PayloadBuilder + Clone;

/// The table set of the node.
type TableSet: TableSet;

/// Returns the transaction pool of the node.
fn pool(&self) -> &Self::Pool;

Expand Down Expand Up @@ -95,7 +99,7 @@ pub struct AddOnsContext<'a, N: FullNodeComponents> {
/// Node with all configured components.
pub node: N,
/// Node configuration.
pub config: &'a NodeConfig<<N::Types as NodeTypes>::ChainSpec>,
pub config: &'a NodeConfig<<N::Types as NodeTypes>::ChainSpec, N::TableSet>,
/// Handle to the beacon consensus engine.
pub beacon_engine_handle:
BeaconConsensusEngineHandle<<N::Types as NodeTypesWithEngine>::Engine>,
Expand All @@ -118,7 +122,11 @@ pub trait NodeAddOns<N: FullNodeComponents>: Send {
impl<N: FullNodeComponents> NodeAddOns<N> for () {
type Handle = ();

async fn launch_add_ons(self, _components: AddOnsContext<'_, N>) -> eyre::Result<Self::Handle> {
async fn launch_add_ons(self, _components: AddOnsContext<'_, N>) -> eyre::Result<Self::Handle>
where
Self: Send,
N: Send,
{
Ok(())
}
}
37 changes: 19 additions & 18 deletions crates/node/builder/src/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use futures::Future;
use reth_blockchain_tree::externals::NodeTypesForTree;
use reth_chainspec::{EthChainSpec, EthereumHardforks, Hardforks};
use reth_cli_util::get_secret_key;
use reth_db::TableSet;
use reth_db_api::{
database::Database,
database_metrics::{DatabaseMetadata, DatabaseMetrics},
Expand Down Expand Up @@ -152,16 +153,16 @@ pub type RethFullAdapter<DB, Types> = FullNodeTypesAdapter<
/// configured by the builder itself during launch. This might change in the future.
///
/// [builder]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
pub struct NodeBuilder<DB, ChainSpec> {
pub struct NodeBuilder<DB, ChainSpec, TS: TableSet> {
/// All settings for how the node should be configured.
config: NodeConfig<ChainSpec>,
config: NodeConfig<ChainSpec, TS>,
/// The configured database for the node.
database: DB,
}

impl<ChainSpec> NodeBuilder<(), ChainSpec> {
impl<ChainSpec, TS: TableSet> NodeBuilder<(), ChainSpec, TS> {
/// Create a new [`NodeBuilder`].
pub const fn new(config: NodeConfig<ChainSpec>) -> Self {
pub const fn new(config: NodeConfig<ChainSpec, TS>) -> Self {
Self { config, database: () }
}

Expand All @@ -186,21 +187,21 @@ impl<ChainSpec> NodeBuilder<(), ChainSpec> {
}
}

impl<DB, ChainSpec> NodeBuilder<DB, ChainSpec> {
impl<DB, ChainSpec, TS: TableSet> NodeBuilder<DB, ChainSpec, TS> {
/// Returns a reference to the node builder's config.
pub const fn config(&self) -> &NodeConfig<ChainSpec> {
pub const fn config(&self) -> &NodeConfig<ChainSpec, TS> {
&self.config
}

/// Returns a mutable reference to the node builder's config.
pub fn config_mut(&mut self) -> &mut NodeConfig<ChainSpec> {
pub fn config_mut(&mut self) -> &mut NodeConfig<ChainSpec, TS> {
&mut self.config
}
}

impl<DB, ChainSpec: EthChainSpec> NodeBuilder<DB, ChainSpec> {
impl<DB, ChainSpec: EthChainSpec, TS: TableSet> NodeBuilder<DB, ChainSpec, TS> {
/// Configures the underlying database that the node will use.
pub fn with_database<D>(self, database: D) -> NodeBuilder<D, ChainSpec> {
pub fn with_database<D>(self, database: D) -> NodeBuilder<D, ChainSpec, TS> {
NodeBuilder { config: self.config, database }
}

Expand All @@ -217,7 +218,7 @@ impl<DB, ChainSpec: EthChainSpec> NodeBuilder<DB, ChainSpec> {
mut self,
task_executor: TaskExecutor,
) -> WithLaunchContext<
NodeBuilder<Arc<reth_db::test_utils::TempDatabase<reth_db::DatabaseEnv>>, ChainSpec>,
NodeBuilder<Arc<reth_db::test_utils::TempDatabase<reth_db::DatabaseEnv>>, ChainSpec, TS>,
> {
let path = reth_node_core::dirs::MaybePlatformPath::<DataDirPath>::from(
reth_db::test_utils::tempdir_path(),
Expand All @@ -236,13 +237,13 @@ impl<DB, ChainSpec: EthChainSpec> NodeBuilder<DB, ChainSpec> {
}
}

impl<DB, ChainSpec> NodeBuilder<DB, ChainSpec>
impl<DB, ChainSpec, TS: TableSet> NodeBuilder<DB, ChainSpec, TS>
where
DB: Database + DatabaseMetrics + DatabaseMetadata + Clone + Unpin + 'static,
ChainSpec: EthChainSpec + EthereumHardforks,
{
/// Configures the types of the node.
pub fn with_types<T>(self) -> NodeBuilderWithTypes<RethFullAdapter<DB, T>>
pub fn with_types<T>(self) -> NodeBuilderWithTypes<RethFullAdapter<DB, T>, TS>
where
T: NodeTypesWithEngine<ChainSpec = ChainSpec> + NodeTypesForTree,
{
Expand All @@ -252,7 +253,7 @@ where
/// Configures the types of the node and the provider type that will be used by the node.
pub fn with_types_and_provider<T, P>(
self,
) -> NodeBuilderWithTypes<FullNodeTypesAdapter<NodeTypesWithDBAdapter<T, DB>, P>>
) -> NodeBuilderWithTypes<FullNodeTypesAdapter<NodeTypesWithDBAdapter<T, DB>, P>, TS>
where
T: NodeTypesWithEngine<ChainSpec = ChainSpec> + NodeTypesForProvider,
P: FullProvider<NodeTypesWithDBAdapter<T, DB>>,
Expand Down Expand Up @@ -570,24 +571,24 @@ where
}

/// Captures the necessary context for building the components of the node.
pub struct BuilderContext<Node: FullNodeTypes> {
pub struct BuilderContext<Node: FullNodeTypes, TS: TableSet> {
/// The current head of the blockchain at launch.
pub(crate) head: Head,
/// The configured provider to interact with the blockchain.
pub(crate) provider: Node::Provider,
/// The executor of the node.
pub(crate) executor: TaskExecutor,
/// Config container
pub(crate) config_container: WithConfigs<<Node::Types as NodeTypes>::ChainSpec>,
pub(crate) config_container: WithConfigs<<Node::Types as NodeTypes>::ChainSpec, TS>,
}

impl<Node: FullNodeTypes> BuilderContext<Node> {
impl<Node: FullNodeTypes, TS: TableSet> BuilderContext<Node, TS> {
/// Create a new instance of [`BuilderContext`]
pub const fn new(
head: Head,
provider: Node::Provider,
executor: TaskExecutor,
config_container: WithConfigs<<Node::Types as NodeTypes>::ChainSpec>,
config_container: WithConfigs<<Node::Types as NodeTypes>::ChainSpec, TS>,
) -> Self {
Self { head, provider, executor, config_container }
}
Expand All @@ -603,7 +604,7 @@ impl<Node: FullNodeTypes> BuilderContext<Node> {
}

/// Returns the config of the node.
pub const fn config(&self) -> &NodeConfig<<Node::Types as NodeTypes>::ChainSpec> {
pub const fn config(&self) -> &NodeConfig<<Node::Types as NodeTypes>::ChainSpec, TS> {
&self.config_container.config
}

Expand Down
14 changes: 8 additions & 6 deletions crates/node/builder/src/builder/states.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::{
rpc::{RethRpcAddOns, RethRpcServerHandles, RpcContext},
AddOns, FullNode,
};
use reth_db::TableSet;
use reth_exex::ExExContext;
use reth_node_api::{
FullNodeComponents, FullNodeTypes, NodeAddOns, NodeTypes, NodeTypesWithDB, PayloadBuilder,
Expand All @@ -21,24 +22,24 @@ use reth_tasks::TaskExecutor;
use std::{fmt, future::Future};

/// A node builder that also has the configured types.
pub struct NodeBuilderWithTypes<T: FullNodeTypes> {
pub struct NodeBuilderWithTypes<T: FullNodeTypes, TS: TableSet> {
/// All settings for how the node should be configured.
config: NodeConfig<<T::Types as NodeTypes>::ChainSpec>,
config: NodeConfig<<T::Types as NodeTypes>::ChainSpec, TS>,
/// The configured database for the node.
adapter: NodeTypesAdapter<T>,
}

impl<T: FullNodeTypes> NodeBuilderWithTypes<T> {
impl<T: FullNodeTypes, TS: TableSet> NodeBuilderWithTypes<T, TS> {
/// Creates a new instance of the node builder with the given configuration and types.
pub const fn new(
config: NodeConfig<<T::Types as NodeTypes>::ChainSpec>,
config: NodeConfig<<T::Types as NodeTypes>::ChainSpec, TS>,
database: <T::Types as NodeTypesWithDB>::DB,
) -> Self {
Self { config, adapter: NodeTypesAdapter::new(database) }
}

/// Advances the state of the node builder to the next state where all components are configured
pub fn with_components<CB>(self, components_builder: CB) -> NodeBuilderWithComponents<T, CB, ()>
pub fn with_components<CB>(self, components_builder: CB) -> NodeBuilderWithComponents<T, CB, (), TS>
where
CB: NodeComponentsBuilder<T>,
{
Expand Down Expand Up @@ -149,9 +150,10 @@ pub struct NodeBuilderWithComponents<
T: FullNodeTypes,
CB: NodeComponentsBuilder<T>,
AO: NodeAddOns<NodeAdapter<T, CB::Components>>,
TS: TableSet,
> {
/// All settings for how the node should be configured.
pub config: NodeConfig<<T::Types as NodeTypes>::ChainSpec>,
pub config: NodeConfig<<T::Types as NodeTypes>::ChainSpec, TS>,
/// Adapter for the underlying node types and database
pub adapter: NodeTypesAdapter<T>,
/// container for type specific components
Expand Down
4 changes: 2 additions & 2 deletions crates/node/builder/src/launch/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -987,9 +987,9 @@ impl<L, R> Attached<L, R> {
/// Helper container type to bundle the initial [`NodeConfig`] and the loaded settings from the
/// reth.toml config
#[derive(Debug, Clone)]
pub struct WithConfigs<ChainSpec> {
pub struct WithConfigs<ChainSpec, TS: TableSet> {
/// The configured, usually derived from the CLI.
pub config: NodeConfig<ChainSpec>,
pub config: NodeConfig<ChainSpec, TS>,
/// The loaded reth.toml config.
pub toml_config: reth_config::Config,
}
Expand Down
2 changes: 1 addition & 1 deletion crates/node/builder/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ pub struct FullNode<Node: FullNodeComponents, AddOns: NodeAddOns<Node>> {
/// Task executor for the node.
pub task_executor: TaskExecutor,
/// The initial node config.
pub config: NodeConfig<<Node::Types as NodeTypes>::ChainSpec>,
pub config: NodeConfig<<Node::Types as NodeTypes>::ChainSpec, Node::TableSet>,
/// The data dir of the node.
pub data_dir: ChainPath<DataDirPath>,
/// The handle to launched add-ons
Expand Down
2 changes: 1 addition & 1 deletion crates/node/builder/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ pub struct RpcContext<'a, Node: FullNodeComponents, EthApi: EthApiTypes> {
pub(crate) node: Node,

/// Gives access to the node configuration.
pub(crate) config: &'a NodeConfig<<Node::Types as NodeTypes>::ChainSpec>,
pub(crate) config: &'a NodeConfig<<Node::Types as NodeTypes>::ChainSpec, Node::TableSet>,

/// A Helper type the holds instances of the configured modules.
///
Expand Down
Loading
Loading