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#[auto_impl(&mut, Box)]
9pub trait BlockHash {
10    type Error: StdError;
11
12    /// Gets block hash by block number.
13    fn block_hash(&mut self, number: u64) -> Result<B256, Self::Error>;
14}
15
16#[auto_impl(&, &mut, Box, Rc, Arc)]
17pub trait BlockHashRef {
18    type Error: StdError;
19
20    /// Gets block hash by block number.
21    fn block_hash(&self, number: u64) -> Result<B256, Self::Error>;
22}
23
24impl<T> BlockHash for &T
25where
26    T: BlockHashRef,
27{
28    type Error = <T as BlockHashRef>::Error;
29
30    fn block_hash(&mut self, number: u64) -> Result<B256, Self::Error> {
31        BlockHashRef::block_hash(*self, number)
32    }
33}
34
35impl<T> BlockHash for Arc<T>
36where
37    T: BlockHashRef,
38{
39    type Error = <T as BlockHashRef>::Error;
40
41    fn block_hash(&mut self, number: u64) -> Result<B256, Self::Error> {
42        self.deref().block_hash(number)
43    }
44}