Skip to main content

ethrex_levm/hooks/
hook.rs

1use crate::{
2    errors::{ContextResult, VMError},
3    hooks::{L2Hook, backup_hook::BackupHook, default_hook::DefaultHook},
4    vm::{VM, VMType},
5};
6use ethrex_common::types::fee_config::FeeConfig;
7use std::{cell::RefCell, rc::Rc};
8
9pub trait Hook {
10    fn prepare_execution(&mut self, vm: &mut VM<'_>) -> Result<(), VMError>;
11
12    fn finalize_execution(
13        &mut self,
14        vm: &mut VM<'_>,
15        report: &mut ContextResult,
16    ) -> Result<(), VMError>;
17
18    /// True iff this hook reads the top-level call-frame backup (`db.tx_backup`) in
19    /// `finalize_execution`. Drives `VM::preserve_top_level_backup`: when any installed hook
20    /// returns true, the backup is deep-cloned (not moved out) on the revert / invalid-tx paths
21    /// so the hook still sees it. Defaults to false; only `BackupHook` overrides it.
22    fn reads_top_level_backup(&self) -> bool {
23        false
24    }
25}
26
27pub fn get_hooks(vm_type: &VMType) -> Vec<Rc<RefCell<dyn Hook + 'static>>> {
28    match vm_type {
29        VMType::L1 => l1_hooks(),
30        VMType::L2(fee_config) => l2_hooks(*fee_config),
31    }
32}
33
34pub fn l1_hooks() -> Vec<Rc<RefCell<dyn Hook + 'static>>> {
35    vec![Rc::new(RefCell::new(DefaultHook))]
36}
37
38pub fn l2_hooks(fee_config: FeeConfig) -> Vec<Rc<RefCell<dyn Hook + 'static>>> {
39    vec![
40        Rc::new(RefCell::new(L2Hook::new(fee_config))),
41        Rc::new(RefCell::new(BackupHook::default())),
42    ]
43}