alloy_evm/block/
state_hook.rs

1use revm::state::EvmState;
2
3/// A hook that is called after each state change.
4pub trait OnStateHook: Send + 'static {
5    /// Invoked with the source of the change and the state after each system call.
6    fn on_state(&mut self, source: StateChangeSource, state: &EvmState);
7}
8
9/// Source of the state change
10#[derive(Debug, Clone, Copy)]
11pub enum StateChangeSource {
12    /// Transaction with its index
13    Transaction(usize),
14    /// Pre-block state transition
15    PreBlock(StateChangePreBlockSource),
16    /// Post-block state transition
17    PostBlock(StateChangePostBlockSource),
18}
19
20/// Source of the pre-block state change
21#[derive(Debug, Clone, Copy)]
22pub enum StateChangePreBlockSource {
23    /// EIP-2935 blockhashes contract
24    BlockHashesContract,
25    /// EIP-4788 beacon root contract
26    BeaconRootContract,
27    /// EIP-7002 withdrawal requests contract
28    WithdrawalRequestsContract,
29}
30
31/// Source of the post-block state change
32#[derive(Debug, Clone, Copy)]
33pub enum StateChangePostBlockSource {
34    /// Balance increments from block rewards and withdrawals
35    BalanceIncrements,
36    /// EIP-7002 withdrawal requests contract
37    WithdrawalRequestsContract,
38    /// EIP-7251 consolidation requests contract
39    ConsolidationRequestsContract,
40}
41
42impl<F> OnStateHook for F
43where
44    F: FnMut(StateChangeSource, &EvmState) + Send + 'static,
45{
46    fn on_state(&mut self, source: StateChangeSource, state: &EvmState) {
47        self(source, state)
48    }
49}
50
51/// An [`OnStateHook`] that does nothing.
52#[derive(Default, Debug, Clone)]
53#[non_exhaustive]
54pub struct NoopHook;
55
56impl OnStateHook for NoopHook {
57    fn on_state(&mut self, _source: StateChangeSource, _state: &EvmState) {}
58}