feat: ChainSpec associated type (#10292)

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
This commit is contained in:
Arsenii Kulikov
2024-08-22 20:30:09 +08:00
committed by GitHub
parent a4c30ead7e
commit f2e0bc073a
52 changed files with 254 additions and 116 deletions

View File

@ -14,6 +14,7 @@ workspace = true
# reth
reth-evm.workspace = true
reth-provider.workspace = true
reth-chainspec.workspace = true
reth-db-api.workspace = true
reth-engine-primitives.workspace = true
reth-transaction-pool.workspace = true

View File

@ -2,6 +2,7 @@
use std::marker::PhantomData;
use reth_chainspec::EthChainSpec;
use reth_db_api::{
database::Database,
database_metrics::{DatabaseMetadata, DatabaseMetrics},
@ -26,32 +27,37 @@ pub trait NodeTypes: Send + Sync + Unpin + 'static {
type Primitives: NodePrimitives;
/// The node's engine types, defining the interaction with the consensus engine.
type Engine: EngineTypes;
/// The type used for configuration of the EVM.
type ChainSpec: EthChainSpec;
}
/// A [`NodeTypes`] type builder
#[derive(Default, Debug)]
pub struct AnyNodeTypes<P = (), E = ()>(PhantomData<P>, PhantomData<E>);
pub struct AnyNodeTypes<P = (), E = (), C = ()>(PhantomData<P>, PhantomData<E>, PhantomData<C>);
impl<P, E> AnyNodeTypes<P, E> {
impl<P, E, C> AnyNodeTypes<P, E, C> {
/// Sets the `Primitives` associated type.
pub const fn primitives<T>(self) -> AnyNodeTypes<T, E> {
AnyNodeTypes::<T, E>(PhantomData::<T>, PhantomData::<E>)
pub const fn primitives<T>(self) -> AnyNodeTypes<T, E, C> {
AnyNodeTypes::<T, E, C>(PhantomData::<T>, PhantomData::<E>, PhantomData::<C>)
}
/// Sets the `Engine` associated type.
pub const fn engine<T>(self) -> AnyNodeTypes<P, T> {
AnyNodeTypes::<P, T>(PhantomData::<P>, PhantomData::<T>)
pub const fn engine<T>(self) -> AnyNodeTypes<P, T, C> {
AnyNodeTypes::<P, T, C>(PhantomData::<P>, PhantomData::<T>, PhantomData::<C>)
}
}
impl<P, E> NodeTypes for AnyNodeTypes<P, E>
impl<P, E, C> NodeTypes for AnyNodeTypes<P, E, C>
where
P: NodePrimitives + Send + Sync + Unpin + 'static,
E: EngineTypes + Send + Sync + Unpin,
C: EthChainSpec,
{
type Primitives = P;
type Engine = E;
type ChainSpec = C;
}
/// A helper trait that is downstream of the [`NodeTypes`] trait and adds stateful components to the
@ -62,7 +68,7 @@ pub trait FullNodeTypes: NodeTypes + 'static {
/// 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::DB>;
type Provider: FullProvider<Self::DB, Self::ChainSpec>;
}
/// An adapter type that adds the builtin provider type to the user configured node types.
@ -103,12 +109,13 @@ where
{
type Primitives = Types::Primitives;
type Engine = Types::Engine;
type ChainSpec = Types::ChainSpec;
}
impl<Types, DB, Provider> FullNodeTypes for FullNodeTypesAdapter<Types, DB, Provider>
where
Types: NodeTypes,
Provider: FullProvider<DB>,
Provider: FullProvider<DB, Types::ChainSpec>,
DB: Database + DatabaseMetrics + DatabaseMetadata + Clone + Unpin + 'static,
{
type DB = DB;

View File

@ -206,7 +206,7 @@ where
/// Configures the types of the node.
pub fn with_types<T>(self) -> NodeBuilderWithTypes<RethFullAdapter<DB, T>>
where
T: NodeTypes,
T: NodeTypes<ChainSpec = ChainSpec>,
{
self.with_types_and_provider()
}
@ -216,8 +216,8 @@ where
self,
) -> NodeBuilderWithTypes<FullNodeTypesAdapter<T, DB, P>>
where
T: NodeTypes,
P: FullProvider<DB>,
T: NodeTypes<ChainSpec = ChainSpec>,
P: FullProvider<DB, T::ChainSpec>,
{
NodeBuilderWithTypes::new(self.config, self.database)
}
@ -230,7 +230,7 @@ where
node: N,
) -> NodeBuilderWithComponents<RethFullAdapter<DB, N>, N::ComponentsBuilder, N::AddOns>
where
N: Node<RethFullAdapter<DB, N>>,
N: Node<RethFullAdapter<DB, N>, ChainSpec = ChainSpec>,
{
self.with_types().with_components(node.components_builder()).with_add_ons::<N::AddOns>()
}
@ -264,7 +264,7 @@ where
/// Configures the types of the node.
pub fn with_types<T>(self) -> WithLaunchContext<NodeBuilderWithTypes<RethFullAdapter<DB, T>>>
where
T: NodeTypes,
T: NodeTypes<ChainSpec = ChainSpec>,
{
WithLaunchContext { builder: self.builder.with_types(), task_executor: self.task_executor }
}
@ -274,8 +274,8 @@ where
self,
) -> WithLaunchContext<NodeBuilderWithTypes<FullNodeTypesAdapter<T, DB, P>>>
where
T: NodeTypes,
P: FullProvider<DB>,
T: NodeTypes<ChainSpec = ChainSpec>,
P: FullProvider<DB, T::ChainSpec>,
{
WithLaunchContext {
builder: self.builder.with_types_and_provider(),
@ -293,7 +293,7 @@ where
NodeBuilderWithComponents<RethFullAdapter<DB, N>, N::ComponentsBuilder, N::AddOns>,
>
where
N: Node<RethFullAdapter<DB, N>>,
N: Node<RethFullAdapter<DB, N>, ChainSpec = ChainSpec>,
{
self.with_types().with_components(node.components_builder()).with_add_ons::<N::AddOns>()
}
@ -316,7 +316,7 @@ where
>,
>
where
N: Node<RethFullAdapter<DB, N>>,
N: Node<RethFullAdapter<DB, N>, ChainSpec = ChainSpec>,
N::AddOns: NodeAddOns<
NodeAdapter<
RethFullAdapter<DB, N>,
@ -469,7 +469,7 @@ where
impl<T, DB, CB, AO> WithLaunchContext<NodeBuilderWithComponents<RethFullAdapter<DB, T>, CB, AO>>
where
DB: Database + DatabaseMetrics + DatabaseMetadata + Clone + Unpin + 'static,
T: NodeTypes,
T: NodeTypes<ChainSpec = ChainSpec>,
CB: NodeComponentsBuilder<RethFullAdapter<DB, T>>,
AO: NodeAddOns<
NodeAdapter<RethFullAdapter<DB, T>, CB::Components>,
@ -540,7 +540,7 @@ impl<Node: FullNodeTypes> BuilderContext<Node> {
}
/// Returns the chain spec of the node.
pub fn chain_spec(&self) -> Arc<ChainSpec> {
pub fn chain_spec(&self) -> Arc<Node::ChainSpec> {
self.provider().chain_spec()
}

View File

@ -91,6 +91,7 @@ pub struct NodeAdapter<T: FullNodeTypes, C: NodeComponents<T>> {
impl<T: FullNodeTypes, C: NodeComponents<T>> NodeTypes for NodeAdapter<T, C> {
type Primitives = T::Primitives;
type Engine = T::Engine;
type ChainSpec = T::ChainSpec;
}
impl<T: FullNodeTypes, C: NodeComponents<T>> FullNodeTypes for NodeAdapter<T, C> {

View File

@ -569,7 +569,7 @@ where
) -> eyre::Result<LaunchContextWith<Attached<WithConfigs, WithMeteredProviders<DB, T>>>>
where
T: FullNodeTypes,
T::Provider: FullProvider<DB>,
T::Provider: FullProvider<DB, T::ChainSpec>,
F: FnOnce(ProviderFactory<DB>) -> eyre::Result<T::Provider>,
{
let blockchain_db = create_blockchain_provider(self.provider_factory().clone())?;
@ -598,7 +598,7 @@ where
impl<DB, T> LaunchContextWith<Attached<WithConfigs, WithMeteredProviders<DB, T>>>
where
DB: Database + DatabaseMetrics + Send + Sync + Clone + 'static,
T: FullNodeTypes<Provider: FullProvider<DB> + WithTree>,
T: FullNodeTypes<Provider: FullProvider<DB, T::ChainSpec> + WithTree>,
{
/// Returns access to the underlying database.
pub fn database(&self) -> &DB {
@ -717,7 +717,7 @@ where
impl<DB, T, CB> LaunchContextWith<Attached<WithConfigs, WithComponents<DB, T, CB>>>
where
DB: Database + DatabaseMetrics + Send + Sync + Clone + 'static,
T: FullNodeTypes<Provider: FullProvider<DB> + WithTree>,
T: FullNodeTypes<Provider: FullProvider<DB, T::ChainSpec> + WithTree>,
CB: NodeComponentsBuilder<T>,
{
/// Returns the configured `ProviderFactory`.
@ -915,7 +915,7 @@ pub struct WithMeteredProvider<DB> {
pub struct WithMeteredProviders<DB, T>
where
DB: Database,
T: FullNodeTypes<Provider: FullProvider<DB>>,
T: FullNodeTypes<Provider: FullProvider<DB, T::ChainSpec>>,
{
db_provider_container: WithMeteredProvider<DB>,
blockchain_db: T::Provider,
@ -931,7 +931,7 @@ where
pub struct WithComponents<DB, T, CB>
where
DB: Database,
T: FullNodeTypes<Provider: FullProvider<DB>>,
T: FullNodeTypes<Provider: FullProvider<DB, T::ChainSpec>>,
CB: NodeComponentsBuilder<T>,
{
db_provider_container: WithMeteredProvider<DB>,

View File

@ -6,6 +6,7 @@ use reth_beacon_consensus::{
BeaconConsensusEngineHandle,
};
use reth_blockchain_tree::BlockchainTreeConfig;
use reth_chainspec::ChainSpec;
use reth_engine_service::service::{ChainEvent, EngineService};
use reth_engine_tree::{
engine::{EngineApiRequest, EngineRequestHandler},
@ -59,7 +60,10 @@ impl EngineNodeLauncher {
impl<T, CB, AO> LaunchNode<NodeBuilderWithComponents<T, CB, AO>> for EngineNodeLauncher
where
T: FullNodeTypes<Provider = BlockchainProvider2<<T as FullNodeTypes>::DB>>,
T: FullNodeTypes<
Provider = BlockchainProvider2<<T as FullNodeTypes>::DB>,
ChainSpec = ChainSpec,
>,
CB: NodeComponentsBuilder<T>,
AO: NodeAddOns<
NodeAdapter<T, CB::Components>,

View File

@ -16,6 +16,7 @@ use reth_beacon_consensus::{
BeaconConsensusEngine,
};
use reth_blockchain_tree::{noop::NoopBlockchainTree, BlockchainTreeConfig};
use reth_chainspec::ChainSpec;
use reth_consensus_debug_client::{DebugConsensusClient, EtherscanBlockProvider, RpcBlockProvider};
use reth_engine_util::EngineMessageStreamExt;
use reth_exex::ExExManagerHandle;
@ -101,7 +102,10 @@ impl DefaultNodeLauncher {
impl<T, CB, AO> LaunchNode<NodeBuilderWithComponents<T, CB, AO>> for DefaultNodeLauncher
where
T: FullNodeTypes<Provider = BlockchainProvider<<T as FullNodeTypes>::DB>>,
T: FullNodeTypes<
Provider = BlockchainProvider<<T as FullNodeTypes>::DB>,
ChainSpec = ChainSpec,
>,
CB: NodeComponentsBuilder<T>,
AO: NodeAddOns<
NodeAdapter<T, CB::Components>,

View File

@ -3,7 +3,6 @@ pub use reth_node_api::{FullNodeTypes, NodeTypes};
use std::{marker::PhantomData, sync::Arc};
use reth_chainspec::ChainSpec;
use reth_node_api::FullNodeComponents;
use reth_node_core::{
dirs::{ChainPath, DataDirPath},
@ -62,6 +61,8 @@ where
type Primitives = N::Primitives;
type Engine = N::Engine;
type ChainSpec = N::ChainSpec;
}
impl<N, C, AO> Node<N> for AnyNode<N, C, AO>
@ -112,8 +113,8 @@ where
Node: FullNodeComponents,
AddOns: NodeAddOns<Node>,
{
/// Returns the [`ChainSpec`] of the node.
pub fn chain_spec(&self) -> Arc<ChainSpec> {
/// Returns the chain spec of the node.
pub fn chain_spec(&self) -> Arc<Node::ChainSpec> {
self.provider.chain_spec()
}

View File

@ -6,6 +6,7 @@ use std::{
};
use futures::TryFutureExt;
use reth_chainspec::ChainSpec;
use reth_node_api::{BuilderProvider, FullNodeComponents};
use reth_node_core::{
node_config::NodeConfig,
@ -260,7 +261,7 @@ pub async fn launch_rpc_servers<Node, Engine, EthApi>(
) -> eyre::Result<(RethRpcServerHandles, RpcRegistry<Node, EthApi>)>
where
EthApi: EthApiBuilderProvider<Node> + FullEthApiServer,
Node: FullNodeComponents + Clone,
Node: FullNodeComponents<ChainSpec = ChainSpec> + Clone,
Engine: EngineApiServer<Node::Engine>,
{
let auth_config = config.rpc.auth_server_config(jwt_secret)?;