reth_revm/
either.rs

1use alloy_primitives::{Address, B256, U256};
2use revm::{bytecode::Bytecode, state::AccountInfo, Database};
3
4/// An enum type that can hold either of two different [`Database`] implementations.
5///
6/// This allows flexible usage of different [`Database`] types in the same context.
7#[derive(Debug, Clone)]
8pub enum Either<L, R> {
9    /// A value of type `L`.
10    Left(L),
11    /// A value of type `R`.
12    Right(R),
13}
14
15impl<L, R> Database for Either<L, R>
16where
17    L: Database,
18    R: Database<Error = L::Error>,
19{
20    type Error = L::Error;
21
22    fn basic(&mut self, address: Address) -> Result<Option<AccountInfo>, Self::Error> {
23        match self {
24            Self::Left(db) => db.basic(address),
25            Self::Right(db) => db.basic(address),
26        }
27    }
28
29    fn code_by_hash(&mut self, code_hash: B256) -> Result<Bytecode, Self::Error> {
30        match self {
31            Self::Left(db) => db.code_by_hash(code_hash),
32            Self::Right(db) => db.code_by_hash(code_hash),
33        }
34    }
35
36    fn storage(&mut self, address: Address, index: U256) -> Result<U256, Self::Error> {
37        match self {
38            Self::Left(db) => db.storage(address, index),
39            Self::Right(db) => db.storage(address, index),
40        }
41    }
42
43    fn block_hash(&mut self, number: u64) -> Result<B256, Self::Error> {
44        match self {
45            Self::Left(db) => db.block_hash(number),
46            Self::Right(db) => db.block_hash(number),
47        }
48    }
49}