1use 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 fn storage_by_account_id(
48 &mut self,
49 address: Address,
50 account_id: usize,
51 storage_key: StorageKey,
52 ) -> Result<StorageValue, Self::Error> {
53 match self {
54 Self::Left(db) => db.storage_by_account_id(address, account_id, storage_key),
55 Self::Right(db) => db.storage_by_account_id(address, account_id, storage_key),
56 }
57 }
58}
59
60impl<L, R> DatabaseCommit for Either<L, R>
61where
62 L: DatabaseCommit,
63 R: DatabaseCommit,
64{
65 fn commit(&mut self, changes: HashMap<Address, Account>) {
66 match self {
67 Self::Left(db) => db.commit(changes),
68 Self::Right(db) => db.commit(changes),
69 }
70 }
71}
72
73impl<L, R> DatabaseRef for Either<L, R>
74where
75 L: DatabaseRef,
76 R: DatabaseRef<Error = L::Error>,
77{
78 type Error = L::Error;
79
80 fn basic_ref(&self, address: Address) -> Result<Option<AccountInfo>, Self::Error> {
81 match self {
82 Self::Left(db) => db.basic_ref(address),
83 Self::Right(db) => db.basic_ref(address),
84 }
85 }
86
87 fn code_by_hash_ref(&self, code_hash: B256) -> Result<Bytecode, Self::Error> {
88 match self {
89 Self::Left(db) => db.code_by_hash_ref(code_hash),
90 Self::Right(db) => db.code_by_hash_ref(code_hash),
91 }
92 }
93
94 fn storage_ref(
95 &self,
96 address: Address,
97 index: StorageKey,
98 ) -> Result<StorageValue, Self::Error> {
99 match self {
100 Self::Left(db) => db.storage_ref(address, index),
101 Self::Right(db) => db.storage_ref(address, index),
102 }
103 }
104
105 fn block_hash_ref(&self, number: u64) -> Result<B256, Self::Error> {
106 match self {
107 Self::Left(db) => db.block_hash_ref(number),
108 Self::Right(db) => db.block_hash_ref(number),
109 }
110 }
111
112 fn storage_by_account_id_ref(
113 &self,
114 address: Address,
115 account_id: usize,
116 storage_key: StorageKey,
117 ) -> Result<StorageValue, Self::Error> {
118 match self {
119 Self::Left(db) => db.storage_by_account_id_ref(address, account_id, storage_key),
120 Self::Right(db) => db.storage_by_account_id_ref(address, account_id, storage_key),
121 }
122 }
123}