reth_beacon_consensus/engine/hooks/
mod.rs1use alloy_primitives::BlockNumber;
2use reth_errors::{RethError, RethResult};
3use std::{
4 fmt,
5 task::{Context, Poll},
6};
7
8mod controller;
9pub(crate) use controller::{EngineHooksController, PolledHook};
10
11mod prune;
12pub use prune::PruneHook;
13
14mod static_file;
15pub use static_file::StaticFileHook;
16
17#[derive(Default)]
19pub struct EngineHooks {
20 inner: Vec<Box<dyn EngineHook>>,
21}
22
23impl fmt::Debug for EngineHooks {
24 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25 f.debug_struct("EngineHooks").field("inner", &self.inner.len()).finish()
26 }
27}
28
29impl EngineHooks {
30 pub fn new() -> Self {
32 Self { inner: Vec::new() }
33 }
34
35 pub fn add<H: EngineHook>(&mut self, hook: H) {
37 self.inner.push(Box::new(hook))
38 }
39}
40
41pub trait EngineHook: Send + Sync + 'static {
44 fn name(&self) -> &'static str;
46
47 fn poll(
49 &mut self,
50 cx: &mut Context<'_>,
51 ctx: EngineHookContext,
52 ) -> Poll<RethResult<EngineHookEvent>>;
53
54 fn db_access_level(&self) -> EngineHookDBAccessLevel;
56}
57
58#[derive(Copy, Clone, Debug)]
60pub struct EngineHookContext {
61 pub tip_block_number: BlockNumber,
63 pub finalized_block_number: Option<BlockNumber>,
65}
66
67#[derive(Debug)]
69pub enum EngineHookEvent {
70 NotReady,
74 Started,
78 Finished(Result<(), EngineHookError>),
82}
83
84impl EngineHookEvent {
85 pub const fn is_started(&self) -> bool {
87 matches!(self, Self::Started)
88 }
89
90 pub const fn is_finished(&self) -> bool {
92 matches!(self, Self::Finished(_))
93 }
94}
95
96#[derive(Debug, thiserror::Error)]
98pub enum EngineHookError {
99 #[error("hook channel closed")]
101 ChannelClosed,
102 #[error(transparent)]
104 Common(#[from] RethError),
105 #[error(transparent)]
107 Internal(#[from] Box<dyn core::error::Error + Send + Sync>),
108}
109
110#[derive(Debug, Copy, Clone, Eq, PartialEq)]
112pub enum EngineHookDBAccessLevel {
113 ReadOnly,
115 ReadWrite,
117}
118
119impl EngineHookDBAccessLevel {
120 pub const fn is_read_only(&self) -> bool {
122 matches!(self, Self::ReadOnly)
123 }
124
125 pub const fn is_read_write(&self) -> bool {
127 matches!(self, Self::ReadWrite)
128 }
129}