From 257c55ce2646ef1eca7575e974d0545241f20d76 Mon Sep 17 00:00:00 2001 From: Trey Del Bonis Date: Tue, 28 May 2024 20:16:13 -0400 Subject: [PATCH] db: added accessors to `Database` trait and added `CommonDatabase` type --- crates/db/src/database.rs | 73 +++++++++++++++++++++++++++++++++++++++ crates/db/src/lib.rs | 2 ++ crates/db/src/traits.rs | 11 +++++- 3 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 crates/db/src/database.rs diff --git a/crates/db/src/database.rs b/crates/db/src/database.rs new file mode 100644 index 000000000..e45943d17 --- /dev/null +++ b/crates/db/src/database.rs @@ -0,0 +1,73 @@ +use std::sync::Arc; + +use super::traits::*; + +/// Shim database type that assumes that all the database impls are wrapped in +/// `Arc`s and that the provider and stores are actually the same types. We +/// might actually use this in practice, it's just for testing. +pub struct CommonDatabase { + l1db: Arc, + l2db: Arc, + sedb: Arc, + csdb: Arc, +} + +impl CommonDatabase { + pub fn new(l1db: Arc, l2db: Arc, sedb: Arc, csdb: Arc) -> Self { + Self { + l1db, + l2db, + sedb, + csdb, + } + } +} + +impl< + L1: L1DataStore + L1DataProvider, + L2: L2DataStore + L2DataProvider, + S: SyncEventStore + SyncEventProvider, + C: ConsensusStateStore + ConsensusStateProvider, + > Database for CommonDatabase +{ + type L1Store = L1; + type L1Prov = L1; + type L2Store = L2; + type L2Prov = L2; + type SeStore = S; + type SeProv = S; + type CsStore = C; + type CsProv = C; + + fn l1_store(&self) -> &Arc { + &self.l1db + } + + fn l1_provider(&self) -> &Arc { + &self.l1db + } + + fn l2_store(&self) -> &Arc { + &self.l2db + } + + fn l2_provider(&self) -> &Arc { + &self.l2db + } + + fn sync_event_store(&self) -> &Arc { + &self.sedb + } + + fn sync_event_provider(&self) -> &Arc { + &self.sedb + } + + fn consensus_state_store(&self) -> &Arc { + &self.csdb + } + + fn consensus_state_provider(&self) -> &Arc { + &self.csdb + } +} diff --git a/crates/db/src/lib.rs b/crates/db/src/lib.rs index 5a660bf3f..d2db64871 100644 --- a/crates/db/src/lib.rs +++ b/crates/db/src/lib.rs @@ -1,4 +1,6 @@ //! Database abstraction layers, building on what Reth supports. +pub mod database; + pub mod errors; pub mod traits; diff --git a/crates/db/src/traits.rs b/crates/db/src/traits.rs index a6273deb2..11ca0f28f 100644 --- a/crates/db/src/traits.rs +++ b/crates/db/src/traits.rs @@ -1,6 +1,8 @@ //! Trait definitions for low level database interfaces. This borrows some of //! its naming conventions from reth. +use std::sync::Arc; + use alpen_vertex_mmr::CompactMmr; use alpen_vertex_primitives::{l1::*, prelude::*}; use alpen_vertex_state::block::{L2Block, L2BlockId}; @@ -23,7 +25,14 @@ pub trait Database { type CsStore: ConsensusStateStore; type CsProv: ConsensusStateProvider; - // TODO accessors as needed + fn l1_store(&self) -> &Arc; + fn l1_provider(&self) -> &Arc; + fn l2_store(&self) -> &Arc; + fn l2_provider(&self) -> &Arc; + fn sync_event_store(&self) -> &Arc; + fn sync_event_provider(&self) -> &Arc; + fn consensus_state_store(&self) -> &Arc; + fn consensus_state_provider(&self) -> &Arc; } /// Storage interface to control our view of L1 data.