revm_database_interface/
either.rs1use crate::{Database, DatabaseCommit, DatabaseRef};
4use either::Either;
5use primitives::{Address, HashMap, StorageKey, StorageValue, B256};
6use state::{Account, AccountInfo, Bytecode};
7
8impl<L, R> Database for Either<L, R>
9where
10 L: Database,
11 R: Database<Error = L::Error>,
12{
13 type Error = L::Error;
14
15 fn basic(&mut self, address: Address) -> Result<Option<AccountInfo>, Self::Error> {
16 match self {
17 Self::Left(db) => db.basic(address),
18 Self::Right(db) => db.basic(address),
19 }
20 }
21
22 fn code_by_hash(&mut self, code_hash: B256) -> Result<Bytecode, Self::Error> {
23 match self {
24 Self::Left(db) => db.code_by_hash(code_hash),
25 Self::Right(db) => db.code_by_hash(code_hash),
26 }
27 }
28
29 fn storage(
30 &mut self,
31 address: Address,
32 index: StorageKey,
33 ) -> Result<StorageValue, Self::Error> {
34 match self {
35 Self::Left(db) => db.storage(address, index),
36 Self::Right(db) => db.storage(address, index),
37 }
38 }
39
40 fn block_hash(&mut self, number: u64) -> Result<B256, Self::Error> {
41 match self {
42 Self::Left(db) => db.block_hash(number),
43 Self::Right(db) => db.block_hash(number),
44 }
45 }
46}
47
48impl<L, R> DatabaseCommit for Either<L, R>
49where
50 L: DatabaseCommit,
51 R: DatabaseCommit,
52{
53 fn commit(&mut self, changes: HashMap<Address, Account>) {
54 match self {
55 Self::Left(db) => db.commit(changes),
56 Self::Right(db) => db.commit(changes),
57 }
58 }
59}
60
61impl<L, R> DatabaseRef for Either<L, R>
62where
63 L: DatabaseRef,
64 R: DatabaseRef<Error = L::Error>,
65{
66 type Error = L::Error;
67
68 fn basic_ref(&self, address: Address) -> Result<Option<AccountInfo>, Self::Error> {
69 match self {
70 Self::Left(db) => db.basic_ref(address),
71 Self::Right(db) => db.basic_ref(address),
72 }
73 }
74
75 fn code_by_hash_ref(&self, code_hash: B256) -> Result<Bytecode, Self::Error> {
76 match self {
77 Self::Left(db) => db.code_by_hash_ref(code_hash),
78 Self::Right(db) => db.code_by_hash_ref(code_hash),
79 }
80 }
81
82 fn storage_ref(
83 &self,
84 address: Address,
85 index: StorageKey,
86 ) -> Result<StorageValue, Self::Error> {
87 match self {
88 Self::Left(db) => db.storage_ref(address, index),
89 Self::Right(db) => db.storage_ref(address, index),
90 }
91 }
92
93 fn block_hash_ref(&self, number: u64) -> Result<B256, Self::Error> {
94 match self {
95 Self::Left(db) => db.block_hash_ref(number),
96 Self::Right(db) => db.block_hash_ref(number),
97 }
98 }
99}