reth_node_builder/
hooks.rsuse std::fmt;
use reth_node_api::{FullNodeComponents, NodeAddOns};
use crate::node::FullNode;
pub struct NodeHooks<Node: FullNodeComponents, AddOns: NodeAddOns<Node>> {
pub on_component_initialized: Box<dyn OnComponentInitializedHook<Node>>,
pub on_node_started: Box<dyn OnNodeStartedHook<Node, AddOns>>,
_marker: std::marker::PhantomData<Node>,
}
impl<Node, AddOns> NodeHooks<Node, AddOns>
where
Node: FullNodeComponents,
AddOns: NodeAddOns<Node>,
{
pub fn new() -> Self {
Self {
on_component_initialized: Box::<()>::default(),
on_node_started: Box::<()>::default(),
_marker: Default::default(),
}
}
pub(crate) fn set_on_component_initialized<F>(&mut self, hook: F) -> &mut Self
where
F: OnComponentInitializedHook<Node> + 'static,
{
self.on_component_initialized = Box::new(hook);
self
}
#[allow(unused)]
pub(crate) fn on_component_initialized<F>(mut self, hook: F) -> Self
where
F: OnComponentInitializedHook<Node> + 'static,
{
self.set_on_component_initialized(hook);
self
}
pub(crate) fn set_on_node_started<F>(&mut self, hook: F) -> &mut Self
where
F: OnNodeStartedHook<Node, AddOns> + 'static,
{
self.on_node_started = Box::new(hook);
self
}
#[allow(unused)]
pub(crate) fn on_node_started<F>(mut self, hook: F) -> Self
where
F: OnNodeStartedHook<Node, AddOns> + 'static,
{
self.set_on_node_started(hook);
self
}
}
impl<Node, AddOns> Default for NodeHooks<Node, AddOns>
where
Node: FullNodeComponents,
AddOns: NodeAddOns<Node>,
{
fn default() -> Self {
Self::new()
}
}
impl<Node, AddOns> fmt::Debug for NodeHooks<Node, AddOns>
where
Node: FullNodeComponents,
AddOns: NodeAddOns<Node>,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("NodeHooks")
.field("on_component_initialized", &"...")
.field("on_node_started", &"...")
.finish()
}
}
pub trait OnComponentInitializedHook<Node>: Send {
fn on_event(self: Box<Self>, node: Node) -> eyre::Result<()>;
}
impl<Node, F> OnComponentInitializedHook<Node> for F
where
F: FnOnce(Node) -> eyre::Result<()> + Send,
{
fn on_event(self: Box<Self>, node: Node) -> eyre::Result<()> {
(*self)(node)
}
}
pub trait OnNodeStartedHook<Node: FullNodeComponents, AddOns: NodeAddOns<Node>>: Send {
fn on_event(self: Box<Self>, node: FullNode<Node, AddOns>) -> eyre::Result<()>;
}
impl<Node, AddOns, F> OnNodeStartedHook<Node, AddOns> for F
where
Node: FullNodeComponents,
AddOns: NodeAddOns<Node>,
F: FnOnce(FullNode<Node, AddOns>) -> eyre::Result<()> + Send,
{
fn on_event(self: Box<Self>, node: FullNode<Node, AddOns>) -> eyre::Result<()> {
(*self)(node)
}
}
impl<Node> OnComponentInitializedHook<Node> for () {
fn on_event(self: Box<Self>, _node: Node) -> eyre::Result<()> {
Ok(())
}
}
impl<Node, AddOns> OnNodeStartedHook<Node, AddOns> for ()
where
Node: FullNodeComponents,
AddOns: NodeAddOns<Node>,
{
fn on_event(self: Box<Self>, _node: FullNode<Node, AddOns>) -> eyre::Result<()> {
Ok(())
}
}