Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 13 additions & 2 deletions crates/database/interface/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ primitives.workspace = true

# misc
auto_impl.workspace = true
either.workspace = true

# Optional
serde = { workspace = true, features = ["derive", "rc"], optional = true }
Expand All @@ -36,6 +37,16 @@ rstest.workspace = true

[features]
default = ["std"]
std = ["serde?/std", "primitives/std", "state/std"]
serde = ["dep:serde", "primitives/serde", "state/serde"]
std = [
"serde?/std",
"primitives/std",
"state/std",
"either/std"
]
serde = [
"dep:serde",
"primitives/serde",
"state/serde",
"either/serde"
]
asyncdb = ["dep:tokio", "tokio/rt-multi-thread"]
99 changes: 99 additions & 0 deletions crates/database/interface/src/either.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
//! Database implementations for `either::Either` type.

use crate::{Database, DatabaseCommit, DatabaseRef};
use either::Either;
use primitives::{Address, HashMap, StorageKey, StorageValue, B256};
use state::{Account, AccountInfo, Bytecode};

impl<L, R> Database for Either<L, R>
where
L: Database,
R: Database<Error = L::Error>,
{
type Error = L::Error;

fn basic(&mut self, address: Address) -> Result<Option<AccountInfo>, Self::Error> {
match self {
Self::Left(db) => db.basic(address),
Self::Right(db) => db.basic(address),
}
}

fn code_by_hash(&mut self, code_hash: B256) -> Result<Bytecode, Self::Error> {
match self {
Self::Left(db) => db.code_by_hash(code_hash),
Self::Right(db) => db.code_by_hash(code_hash),
}
}

fn storage(
&mut self,
address: Address,
index: StorageKey,
) -> Result<StorageValue, Self::Error> {
match self {
Self::Left(db) => db.storage(address, index),
Self::Right(db) => db.storage(address, index),
}
}

fn block_hash(&mut self, number: u64) -> Result<B256, Self::Error> {
match self {
Self::Left(db) => db.block_hash(number),
Self::Right(db) => db.block_hash(number),
}
}
}

impl<L, R> DatabaseCommit for Either<L, R>
where
L: DatabaseCommit,
R: DatabaseCommit,
{
fn commit(&mut self, changes: HashMap<Address, Account>) {
match self {
Self::Left(db) => db.commit(changes),
Self::Right(db) => db.commit(changes),
}
}
}

impl<L, R> DatabaseRef for Either<L, R>
where
L: DatabaseRef,
R: DatabaseRef<Error = L::Error>,
{
type Error = L::Error;

fn basic_ref(&self, address: Address) -> Result<Option<AccountInfo>, Self::Error> {
match self {
Self::Left(db) => db.basic_ref(address),
Self::Right(db) => db.basic_ref(address),
}
}

fn code_by_hash_ref(&self, code_hash: B256) -> Result<Bytecode, Self::Error> {
match self {
Self::Left(db) => db.code_by_hash_ref(code_hash),
Self::Right(db) => db.code_by_hash_ref(code_hash),
}
}

fn storage_ref(
&self,
address: Address,
index: StorageKey,
) -> Result<StorageValue, Self::Error> {
match self {
Self::Left(db) => db.storage_ref(address, index),
Self::Right(db) => db.storage_ref(address, index),
}
}

fn block_hash_ref(&self, number: u64) -> Result<B256, Self::Error> {
match self {
Self::Left(db) => db.block_hash_ref(number),
Self::Right(db) => db.block_hash_ref(number),
}
}
}
1 change: 1 addition & 0 deletions crates/database/interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub const BENCH_CALLER_BALANCE: U256 = U256::from_limbs([10_000_000_000_000_000,

#[cfg(feature = "asyncdb")]
pub mod async_db;
pub mod either;
pub mod empty_db;
pub mod try_commit;

Expand Down
Loading