feat: make addons stateful (#11204)

Co-authored-by: Arsenii Kulikov <klkvrr@gmail.com>
This commit is contained in:
Matthias Seitz
2024-10-04 08:34:37 +02:00
committed by GitHub
parent ab06997986
commit 84370b81d7
19 changed files with 75 additions and 25 deletions

View File

@ -14,6 +14,8 @@ pub struct AddOns<Node: FullNodeComponents, AddOns: NodeAddOns<Node>> {
pub exexs: Vec<(String, Box<dyn BoxedLaunchExEx<Node>>)>,
/// Additional RPC add-ons.
pub rpc: RpcAddOns<Node, AddOns::EthApi>,
/// Additional captured addons.
pub addons: AddOns,
}
/// Captures node specific addons that can be installed on top of the type configured node and are

View File

@ -243,7 +243,7 @@ where
where
N: Node<RethFullAdapter<DB, N>, ChainSpec = ChainSpec>,
{
self.with_types().with_components(node.components_builder()).with_add_ons::<N::AddOns>()
self.with_types().with_components(node.components_builder()).with_add_ons(node.add_ons())
}
}
@ -311,7 +311,7 @@ where
where
N: Node<RethFullAdapter<DB, N>, ChainSpec = ChainSpec>,
{
self.with_types().with_components(node.components_builder()).with_add_ons::<N::AddOns>()
self.with_types().with_components(node.components_builder()).with_add_ons(node.add_ons())
}
/// Launches a preconfigured [Node]
@ -375,12 +375,15 @@ where
{
/// Advances the state of the node builder to the next state where all customizable
/// [`NodeAddOns`] types are configured.
pub fn with_add_ons<AO>(self) -> WithLaunchContext<NodeBuilderWithComponents<T, CB, AO>>
pub fn with_add_ons<AO>(
self,
add_ons: AO,
) -> WithLaunchContext<NodeBuilderWithComponents<T, CB, AO>>
where
AO: NodeAddOns<NodeAdapter<T, CB::Components>>,
{
WithLaunchContext {
builder: self.builder.with_add_ons::<AO>(),
builder: self.builder.with_add_ons(add_ons),
task_executor: self.task_executor,
}
}

View File

@ -58,6 +58,7 @@ impl<T: FullNodeTypes> NodeBuilderWithTypes<T> {
hooks: NodeHooks::default(),
rpc: RpcAddOns { hooks: RpcHooks::default() },
exexs: Vec::new(),
addons: (),
},
}
}
@ -168,7 +169,7 @@ where
{
/// Advances the state of the node builder to the next state where all customizable
/// [`NodeAddOns`] types are configured.
pub fn with_add_ons<AO>(self) -> NodeBuilderWithComponents<T, CB, AO>
pub fn with_add_ons<AO>(self, addons: AO) -> NodeBuilderWithComponents<T, CB, AO>
where
AO: NodeAddOns<NodeAdapter<T, CB::Components>>,
{
@ -182,6 +183,7 @@ where
hooks: NodeHooks::default(),
rpc: RpcAddOns { hooks: RpcHooks::default() },
exexs: Vec::new(),
addons,
},
}
}

View File

@ -91,7 +91,7 @@ where
let NodeBuilderWithComponents {
adapter: NodeTypesAdapter { database },
components_builder,
add_ons: AddOns { hooks, rpc, exexs: installed_exex },
add_ons: AddOns { hooks, rpc, exexs: installed_exex, .. },
config,
} = target;
let NodeHooks { on_component_initialized, on_node_started, .. } = hooks;

View File

@ -126,7 +126,7 @@ where
let NodeBuilderWithComponents {
adapter: NodeTypesAdapter { database },
components_builder,
add_ons: AddOns { hooks, rpc, exexs: installed_exex },
add_ons: AddOns { hooks, rpc, exexs: installed_exex, .. },
config,
} = target;
let NodeHooks { on_component_initialized, on_node_started, .. } = hooks;

View File

@ -34,21 +34,29 @@ pub trait Node<N: FullNodeTypes>: NodeTypesWithEngine + Clone {
/// Returns a [`NodeComponentsBuilder`] for the node.
fn components_builder(&self) -> Self::ComponentsBuilder;
/// Returns the node add-ons.
fn add_ons(&self) -> Self::AddOns;
}
/// A [`Node`] type builder
#[derive(Clone, Default, Debug)]
pub struct AnyNode<N = (), C = (), AO = ()>(PhantomData<(N, AO)>, C);
pub struct AnyNode<N = (), C = (), AO = ()>(PhantomData<N>, C, AO);
impl<N, C> AnyNode<N, C> {
impl<N, C, AO> AnyNode<N, C, AO> {
/// Configures the types of the node.
pub fn types<T>(self) -> AnyNode<T, C> {
AnyNode::<T, C>(PhantomData::<(T, ())>, self.1)
pub fn types<T>(self) -> AnyNode<T, C, AO> {
AnyNode(PhantomData, self.1, self.2)
}
/// Sets the node components builder.
pub const fn components_builder<T>(&self, value: T) -> AnyNode<N, T> {
AnyNode::<N, T>(PhantomData::<(N, ())>, value)
pub fn components_builder<T>(self, value: T) -> AnyNode<N, T, AO> {
AnyNode(PhantomData, value, self.2)
}
/// Sets the node add-ons.
pub fn add_ons<T>(self, value: T) -> AnyNode<N, C, T> {
AnyNode(PhantomData, self.1, value)
}
}
@ -84,6 +92,10 @@ where
fn components_builder(&self) -> Self::ComponentsBuilder {
self.1.clone()
}
fn add_ons(&self) -> Self::AddOns {
self.2.clone()
}
}
/// The launched node with all components including RPC handlers.