example_database_components/
block_hash.rs

1//! BlockHash database component from [`revm::Database`]
2
3use auto_impl::auto_impl;
4use core::{error::Error as StdError, ops::Deref};
5use revm::primitives::B256;
6use std::sync::Arc;
7
8/// Trait for mutable access to block hash data.
9/// This is typically used for database implementations that may cache or
10/// lazily load block hashes.
11#[auto_impl(&mut, Box)]
12pub trait BlockHash {
13    /// Error type for block hash operations
14    type Error: StdError;
15
16    /// Gets block hash by block number.
17    fn block_hash(&mut self, number: u64) -> Result<B256, Self::Error>;
18}
19
20/// Trait for immutable access to block hash data.
21/// This is typically used for read-only database implementations or
22/// when block hash data is pre-loaded.
23#[auto_impl(&, &mut, Box, Rc, Arc)]
24pub trait BlockHashRef {
25    /// Error type for block hash operations
26    type Error: StdError;
27
28    /// Gets block hash by block number.
29    fn block_hash(&self, number: u64) -> Result<B256, Self::Error>;
30}
31
32impl<T> BlockHash for &T
33where
34    T: BlockHashRef,
35{
36    type Error = <T as BlockHashRef>::Error;
37
38    fn block_hash(&mut self, number: u64) -> Result<B256, Self::Error> {
39        BlockHashRef::block_hash(*self, number)
40    }
41}
42
43impl<T> BlockHash for Arc<T>
44where
45    T: BlockHashRef,
46{
47    type Error = <T as BlockHashRef>::Error;
48
49    fn block_hash(&mut self, number: u64) -> Result<B256, Self::Error> {
50        self.deref().block_hash(number)
51    }
52}