Skip to content

Commit

Permalink
chore: move database to FullNodeTypes (#13414)
Browse files Browse the repository at this point in the history
  • Loading branch information
klkvr authored Dec 16, 2024
1 parent cdf2904 commit 82f21a3
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 71 deletions.
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.

2 changes: 1 addition & 1 deletion crates/e2e-test-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ where
/// Testing database
pub type TmpDB = Arc<TempDatabase<DatabaseEnv>>;
type TmpNodeAdapter<N, Provider = BlockchainProvider<NodeTypesWithDBAdapter<N, TmpDB>>> =
FullNodeTypesAdapter<NodeTypesWithDBAdapter<N, TmpDB>, Provider>;
FullNodeTypesAdapter<N, TmpDB, Provider>;

/// Type alias for a `NodeAdapter`
pub type Adapter<N, Provider = BlockchainProvider<NodeTypesWithDBAdapter<N, TmpDB>>> = NodeAdapter<
Expand Down
15 changes: 3 additions & 12 deletions crates/ethereum/node/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ use reth_ethereum_payload_builder::EthereumBuilderConfig;
use reth_evm::execute::BasicBlockExecutorProvider;
use reth_evm_ethereum::execute::EthExecutionStrategyFactory;
use reth_network::{EthNetworkPrimitives, NetworkHandle, PeersInfo};
use reth_node_api::{
AddOnsContext, ConfigureEvm, FullNodeComponents, HeaderTy, NodeTypesWithDB, TxTy,
};
use reth_node_api::{AddOnsContext, ConfigureEvm, FullNodeComponents, HeaderTy, TxTy};
use reth_node_builder::{
components::{
ComponentsBuilder, ConsensusBuilder, ExecutorBuilder, NetworkBuilder,
Expand Down Expand Up @@ -94,16 +92,9 @@ pub type EthereumAddOns<N> = RpcAddOns<
EthereumEngineValidatorBuilder,
>;

impl<Types, N> Node<N> for EthereumNode
impl<N> Node<N> for EthereumNode
where
Types: NodeTypesWithDB
+ NodeTypesWithEngine<
Engine = EthEngineTypes,
ChainSpec = ChainSpec,
Primitives = EthPrimitives,
Storage = EthStorage,
>,
N: FullNodeTypes<Types = Types>,
N: FullNodeTypes<Types = Self>,
{
type ComponentsBuilder = ComponentsBuilder<
N,
Expand Down
5 changes: 3 additions & 2 deletions crates/exex/test-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ pub type Adapter = NodeAdapter<
RethFullAdapter<TmpDB, TestNode>,
<<TestNode as Node<
FullNodeTypesAdapter<
NodeTypesWithDBAdapter<TestNode, TmpDB>,
TestNode,
TmpDB,
BlockchainProvider<NodeTypesWithDBAdapter<TestNode, TmpDB>>,
>,
>>::ComponentsBuilder as NodeComponentsBuilder<RethFullAdapter<TmpDB, TestNode>>>::Components,
Expand Down Expand Up @@ -290,7 +291,7 @@ pub async fn test_exex_context_with_chain_spec(

let (_, payload_builder) = NoopPayloadBuilderService::<EthEngineTypes>::new();

let components = NodeAdapter::<FullNodeTypesAdapter<_, _>, _> {
let components = NodeAdapter::<FullNodeTypesAdapter<_, _, _>, _> {
components: Components {
transaction_pool,
evm_config,
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 @@ -12,6 +12,7 @@ workspace = true

[dependencies]
# reth
reth-db-api.workspace = true
reth-beacon-consensus.workspace = true
reth-consensus.workspace = true
reth-evm.workspace = true
Expand Down
27 changes: 15 additions & 12 deletions crates/node/api/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ use crate::ConfigureEvm;
use alloy_rpc_types_engine::JwtSecret;
use reth_beacon_consensus::BeaconConsensusEngineHandle;
use reth_consensus::FullConsensus;
use reth_db_api::{
database_metrics::{DatabaseMetadata, DatabaseMetrics},
Database,
};
use reth_evm::execute::BlockExecutorProvider;
use reth_network_api::FullNetwork;
use reth_node_core::node_config::NodeConfig;
use reth_node_types::{HeaderTy, NodeTypes, NodeTypesWithDB, NodeTypesWithEngine, TxTy};
use reth_node_types::{HeaderTy, NodeTypes, NodeTypesWithDBAdapter, NodeTypesWithEngine, TxTy};
use reth_payload_builder_primitives::PayloadBuilder;
use reth_provider::FullProvider;
use reth_tasks::TaskExecutor;
Expand All @@ -20,26 +24,25 @@ use std::{future::Future, marker::PhantomData};
/// Its types are configured by node internally and are not intended to be user configurable.
pub trait FullNodeTypes: Send + Sync + Unpin + 'static {
/// Node's types with the database.
type Types: NodeTypesWithDB + NodeTypesWithEngine;
type Types: NodeTypesWithEngine;
/// Underlying database type used by the node to store and retrieve data.
type DB: Database + DatabaseMetrics + DatabaseMetadata + Clone + Unpin + 'static;
/// The provider type used to interact with the node.
type Provider: FullProvider<Self::Types>;
type Provider: FullProvider<NodeTypesWithDBAdapter<Self::Types, Self::DB>>;
}

/// An adapter type that adds the builtin provider type to the user configured node types.
#[derive(Debug)]
pub struct FullNodeTypesAdapter<Types, Provider> {
/// An instance of the user configured node types.
pub types: PhantomData<Types>,
/// The provider type used by the node.
pub provider: PhantomData<Provider>,
}
pub struct FullNodeTypesAdapter<Types, DB, Provider>(PhantomData<(Types, DB, Provider)>);

impl<Types, Provider> FullNodeTypes for FullNodeTypesAdapter<Types, Provider>
impl<Types, DB, Provider> FullNodeTypes for FullNodeTypesAdapter<Types, DB, Provider>
where
Types: NodeTypesWithDB + NodeTypesWithEngine,
Provider: FullProvider<Types>,
Types: NodeTypesWithEngine,
DB: Database + DatabaseMetrics + DatabaseMetadata + Clone + Unpin + 'static,
Provider: FullProvider<NodeTypesWithDBAdapter<Types, DB>>,
{
type Types = Types;
type DB = DB;
type Provider = Provider;
}

Expand Down
12 changes: 4 additions & 8 deletions crates/node/builder/src/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,8 @@ pub use states::*;

/// The adapter type for a reth node with the builtin provider type
// Note: we need to hardcode this because custom components might depend on it in associated types.
pub type RethFullAdapter<DB, Types> = FullNodeTypesAdapter<
NodeTypesWithDBAdapter<Types, DB>,
BlockchainProvider<NodeTypesWithDBAdapter<Types, DB>>,
>;
pub type RethFullAdapter<DB, Types> =
FullNodeTypesAdapter<Types, DB, BlockchainProvider<NodeTypesWithDBAdapter<Types, DB>>>;

#[allow(clippy::doc_markdown)]
#[cfg_attr(doc, aquamarine::aquamarine)]
Expand Down Expand Up @@ -252,7 +250,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<T, DB, P>>
where
T: NodeTypesWithEngine<ChainSpec = ChainSpec> + NodeTypesForProvider,
P: FullProvider<NodeTypesWithDBAdapter<T, DB>>,
Expand Down Expand Up @@ -313,9 +311,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,
) -> WithLaunchContext<
NodeBuilderWithTypes<FullNodeTypesAdapter<NodeTypesWithDBAdapter<T, DB>, P>>,
>
) -> WithLaunchContext<NodeBuilderWithTypes<FullNodeTypesAdapter<T, DB, P>>>
where
T: NodeTypesWithEngine<ChainSpec = ChainSpec> + NodeTypesForProvider,
P: FullProvider<NodeTypesWithDBAdapter<T, DB>>,
Expand Down
9 changes: 5 additions & 4 deletions crates/node/builder/src/builder/states.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
AddOns, FullNode,
};
use reth_exex::ExExContext;
use reth_node_api::{FullNodeComponents, FullNodeTypes, NodeAddOns, NodeTypes, NodeTypesWithDB};
use reth_node_api::{FullNodeComponents, FullNodeTypes, NodeAddOns, NodeTypes};
use reth_node_core::node_config::NodeConfig;
use reth_tasks::TaskExecutor;
use std::{fmt, future::Future};
Expand All @@ -30,7 +30,7 @@ impl<T: FullNodeTypes> NodeBuilderWithTypes<T> {
/// 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>,
database: <T::Types as NodeTypesWithDB>::DB,
database: T::DB,
) -> Self {
Self { config, adapter: NodeTypesAdapter::new(database) }
}
Expand All @@ -54,12 +54,12 @@ impl<T: FullNodeTypes> NodeBuilderWithTypes<T> {
/// Container for the node's types and the database the node uses.
pub struct NodeTypesAdapter<T: FullNodeTypes> {
/// The database type used by the node.
pub database: <T::Types as NodeTypesWithDB>::DB,
pub database: T::DB,
}

impl<T: FullNodeTypes> NodeTypesAdapter<T> {
/// Create a new adapter from the given node types.
pub(crate) const fn new(database: <T::Types as NodeTypesWithDB>::DB) -> Self {
pub(crate) const fn new(database: T::DB) -> Self {
Self { database }
}
}
Expand All @@ -83,6 +83,7 @@ pub struct NodeAdapter<T: FullNodeTypes, C: NodeComponents<T>> {

impl<T: FullNodeTypes, C: NodeComponents<T>> FullNodeTypes for NodeAdapter<T, C> {
type Types = T::Types;
type DB = T::DB;
type Provider = T::Provider;
}

Expand Down
46 changes: 30 additions & 16 deletions crates/node/builder/src/launch/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ use rayon::ThreadPoolBuilder;
use reth_chainspec::{Chain, EthChainSpec, EthereumHardforks};
use reth_config::{config::EtlConfig, PruneConfig};
use reth_consensus::noop::NoopConsensus;
use reth_db_api::{database::Database, database_metrics::DatabaseMetrics};
use reth_db_api::{
database::Database,
database_metrics::{DatabaseMetadata, DatabaseMetrics},
};
use reth_db_common::init::{init_genesis, InitStorageError};
use reth_downloaders::{bodies::noop::NoopBodiesDownloader, headers::noop::NoopHeaderDownloader};
use reth_engine_local::MiningMode;
Expand All @@ -24,6 +27,7 @@ use reth_invalid_block_hooks::InvalidBlockWitnessHook;
use reth_network_p2p::headers::client::HeadersClient;
use reth_node_api::{
FullNodePrimitives, FullNodeTypes, NodePrimitives, NodeTypes, NodeTypesWithDB,
NodeTypesWithDBAdapter,
};
use reth_node_core::{
args::InvalidBlockHookType,
Expand All @@ -44,7 +48,7 @@ use reth_node_metrics::{
};
use reth_primitives::{Head, TransactionSigned};
use reth_provider::{
providers::{ProviderNodeTypes, StaticFileProvider},
providers::{NodeTypesForProvider, ProviderNodeTypes, StaticFileProvider},
BlockHashReader, BlockNumReader, ChainSpecProvider, ProviderError, ProviderFactory,
ProviderResult, StageCheckpointReader, StateProviderFactory, StaticFileProviderFactory,
};
Expand Down Expand Up @@ -567,12 +571,16 @@ where
}
}

impl<N> LaunchContextWith<Attached<WithConfigs<N::ChainSpec>, WithMeteredProvider<N>>>
impl<N, DB>
LaunchContextWith<
Attached<WithConfigs<N::ChainSpec>, WithMeteredProvider<NodeTypesWithDBAdapter<N, DB>>>,
>
where
N: NodeTypesWithDB,
N: NodeTypes,
DB: Database + DatabaseMetrics + DatabaseMetadata + Clone + Unpin + 'static,
{
/// Returns the configured `ProviderFactory`.
const fn provider_factory(&self) -> &ProviderFactory<N> {
const fn provider_factory(&self) -> &ProviderFactory<NodeTypesWithDBAdapter<N, DB>> {
&self.right().provider_factory
}

Expand All @@ -588,8 +596,8 @@ where
create_blockchain_provider: F,
) -> eyre::Result<LaunchContextWith<Attached<WithConfigs<N::ChainSpec>, WithMeteredProviders<T>>>>
where
T: FullNodeTypes<Types = N>,
F: FnOnce(ProviderFactory<N>) -> eyre::Result<T::Provider>,
T: FullNodeTypes<Types = N, DB = DB>,
F: FnOnce(ProviderFactory<NodeTypesWithDBAdapter<N, DB>>) -> eyre::Result<T::Provider>,
{
let blockchain_db = create_blockchain_provider(self.provider_factory().clone())?;

Expand All @@ -615,15 +623,17 @@ impl<T>
Attached<WithConfigs<<T::Types as NodeTypes>::ChainSpec>, WithMeteredProviders<T>>,
>
where
T: FullNodeTypes<Types: ProviderNodeTypes>,
T: FullNodeTypes<Types: NodeTypesForProvider>,
{
/// Returns access to the underlying database.
pub const fn database(&self) -> &<T::Types as NodeTypesWithDB>::DB {
pub const fn database(&self) -> &T::DB {
self.provider_factory().db_ref()
}

/// Returns the configured `ProviderFactory`.
pub const fn provider_factory(&self) -> &ProviderFactory<T::Types> {
pub const fn provider_factory(
&self,
) -> &ProviderFactory<NodeTypesWithDBAdapter<T::Types, T::DB>> {
&self.right().db_provider_container.provider_factory
}

Expand Down Expand Up @@ -708,11 +718,13 @@ impl<T, CB>
Attached<WithConfigs<<T::Types as NodeTypes>::ChainSpec>, WithComponents<T, CB>>,
>
where
T: FullNodeTypes<Types: ProviderNodeTypes>,
T: FullNodeTypes<Types: NodeTypesForProvider>,
CB: NodeComponentsBuilder<T>,
{
/// Returns the configured `ProviderFactory`.
pub const fn provider_factory(&self) -> &ProviderFactory<T::Types> {
pub const fn provider_factory(
&self,
) -> &ProviderFactory<NodeTypesWithDBAdapter<T::Types, T::DB>> {
&self.right().db_provider_container.provider_factory
}

Expand All @@ -731,7 +743,9 @@ where
}

/// Creates a new [`StaticFileProducer`] with the attached database.
pub fn static_file_producer(&self) -> StaticFileProducer<ProviderFactory<T::Types>> {
pub fn static_file_producer(
&self,
) -> StaticFileProducer<ProviderFactory<NodeTypesWithDBAdapter<T::Types, T::DB>>> {
StaticFileProducer::new(self.provider_factory().clone(), self.prune_modes())
}

Expand Down Expand Up @@ -865,7 +879,7 @@ impl<T, CB>
where
T: FullNodeTypes<
Provider: StateProviderFactory + ChainSpecProvider,
Types: ProviderNodeTypes<Primitives: NodePrimitives<SignedTx = TransactionSigned>>,
Types: NodeTypesForProvider<Primitives: NodePrimitives<SignedTx = TransactionSigned>>,
>,
CB: NodeComponentsBuilder<T>,
{
Expand Down Expand Up @@ -1008,7 +1022,7 @@ pub struct WithMeteredProviders<T>
where
T: FullNodeTypes,
{
db_provider_container: WithMeteredProvider<T::Types>,
db_provider_container: WithMeteredProvider<NodeTypesWithDBAdapter<T::Types, T::DB>>,
blockchain_db: T::Provider,
}

Expand All @@ -1019,7 +1033,7 @@ where
T: FullNodeTypes,
CB: NodeComponentsBuilder<T>,
{
db_provider_container: WithMeteredProvider<T::Types>,
db_provider_container: WithMeteredProvider<NodeTypesWithDBAdapter<T::Types, T::DB>>,
node_adapter: NodeAdapter<T, CB::Components>,
head: Head,
}
Expand Down
23 changes: 16 additions & 7 deletions crates/node/builder/src/launch/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
use futures::{future::Either, stream, stream_select, StreamExt};
use reth_beacon_consensus::{
hooks::{EngineHooks, StaticFileHook},
BeaconConsensusEngineHandle, EngineNodeTypes,
BeaconConsensusEngineHandle,
};
use reth_chainspec::EthChainSpec;
use reth_consensus_debug_client::{DebugConsensusClient, EtherscanBlockProvider};
use reth_db_api::{
database_metrics::{DatabaseMetadata, DatabaseMetrics},
Database,
};
use reth_engine_local::{LocalEngineService, LocalPayloadAttributesBuilder};
use reth_engine_service::service::{ChainEvent, EngineService};
use reth_engine_tree::{
Expand All @@ -18,8 +22,8 @@ use reth_exex::ExExManagerHandle;
use reth_network::{NetworkSyncUpdater, SyncState};
use reth_network_api::BlockDownloaderProvider;
use reth_node_api::{
BlockTy, BuiltPayload, EngineValidator, FullNodeTypes, NodeTypesWithEngine,
PayloadAttributesBuilder, PayloadBuilder, PayloadTypes,
BlockTy, BuiltPayload, EngineValidator, FullNodeTypes, NodeTypesWithDBAdapter,
NodeTypesWithEngine, PayloadAttributesBuilder, PayloadBuilder, PayloadTypes,
};
use reth_node_core::{
dirs::{ChainPath, DataDirPath},
Expand All @@ -28,7 +32,7 @@ use reth_node_core::{
};
use reth_node_events::{cl::ConsensusLayerHealthEvents, node};
use reth_primitives::{EthPrimitives, EthereumHardforks};
use reth_provider::providers::BlockchainProvider2;
use reth_provider::providers::{BlockchainProvider2, NodeTypesForProvider};
use reth_tasks::TaskExecutor;
use reth_tokio_util::EventSender;
use reth_tracing::tracing::{debug, error, info};
Expand Down Expand Up @@ -67,10 +71,15 @@ impl EngineNodeLauncher {
}
}

impl<Types, T, CB, AO> LaunchNode<NodeBuilderWithComponents<T, CB, AO>> for EngineNodeLauncher
impl<Types, DB, T, CB, AO> LaunchNode<NodeBuilderWithComponents<T, CB, AO>> for EngineNodeLauncher
where
Types: EngineNodeTypes<Primitives = EthPrimitives>,
T: FullNodeTypes<Types = Types, Provider = BlockchainProvider2<Types>>,
Types: NodeTypesForProvider + NodeTypesWithEngine<Primitives = EthPrimitives>,
DB: Database + DatabaseMetrics + DatabaseMetadata + Clone + Unpin + 'static,
T: FullNodeTypes<
Types = Types,
DB = DB,
Provider = BlockchainProvider2<NodeTypesWithDBAdapter<Types, DB>>,
>,
CB: NodeComponentsBuilder<T>,
AO: RethRpcAddOns<NodeAdapter<T, CB::Components>>
+ EngineValidatorAddOn<
Expand Down
Loading

0 comments on commit 82f21a3

Please sign in to comment.