Skip to main content

revm_database_interface/
either.rs

1//! Database implementations for `either::Either` type.
2
3use crate::{Database, DatabaseCommit, DatabaseRef};
4use either::Either;
5use primitives::{Address, AddressMap, StorageKey, StorageValue, B256};
6use state::{Account, AccountId, 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: AccountId,
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: AddressMap<Account>) {
66        match self {
67            Self::Left(db) => db.commit(changes),
68            Self::Right(db) => db.commit(changes),
69        }
70    }
71
72    fn commit_iter(&mut self, changes: &mut dyn Iterator<Item = (Address, Account)>) {
73        match self {
74            Self::Left(db) => db.commit_iter(changes),
75            Self::Right(db) => db.commit_iter(changes),
76        }
77    }
78}
79
80impl<L, R> DatabaseRef for Either<L, R>
81where
82    L: DatabaseRef,
83    R: DatabaseRef<Error = L::Error>,
84{
85    type Error = L::Error;
86
87    fn basic_ref(&self, address: Address) -> Result<Option<AccountInfo>, Self::Error> {
88        match self {
89            Self::Left(db) => db.basic_ref(address),
90            Self::Right(db) => db.basic_ref(address),
91        }
92    }
93
94    fn code_by_hash_ref(&self, code_hash: B256) -> Result<Bytecode, Self::Error> {
95        match self {
96            Self::Left(db) => db.code_by_hash_ref(code_hash),
97            Self::Right(db) => db.code_by_hash_ref(code_hash),
98        }
99    }
100
101    fn storage_ref(
102        &self,
103        address: Address,
104        index: StorageKey,
105    ) -> Result<StorageValue, Self::Error> {
106        match self {
107            Self::Left(db) => db.storage_ref(address, index),
108            Self::Right(db) => db.storage_ref(address, index),
109        }
110    }
111
112    fn block_hash_ref(&self, number: u64) -> Result<B256, Self::Error> {
113        match self {
114            Self::Left(db) => db.block_hash_ref(number),
115            Self::Right(db) => db.block_hash_ref(number),
116        }
117    }
118
119    fn storage_by_account_id_ref(
120        &self,
121        address: Address,
122        account_id: AccountId,
123        storage_key: StorageKey,
124    ) -> Result<StorageValue, Self::Error> {
125        match self {
126            Self::Left(db) => db.storage_by_account_id_ref(address, account_id, storage_key),
127            Self::Right(db) => db.storage_by_account_id_ref(address, account_id, storage_key),
128        }
129    }
130}