reth_revm/
either.rs

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